|
@@ -0,0 +1,296 @@
|
|
|
+package com.wdkl.ncs.android.component.home.fragment
|
|
|
+
|
|
|
+import android.content.Context
|
|
|
+import android.os.Bundle
|
|
|
+import android.os.Handler
|
|
|
+import android.os.Looper
|
|
|
+import android.os.SystemClock
|
|
|
+import android.support.v4.app.Fragment
|
|
|
+import android.text.TextUtils
|
|
|
+import android.util.Log
|
|
|
+import android.view.LayoutInflater
|
|
|
+import android.view.View
|
|
|
+import android.view.ViewGroup
|
|
|
+import android.widget.Chronometer
|
|
|
+import android.widget.FrameLayout
|
|
|
+import android.widget.ImageView
|
|
|
+import android.widget.TextView
|
|
|
+import com.wdkl.core.voip.VoipEvent
|
|
|
+import com.wdkl.ncs.android.component.home.R
|
|
|
+import com.wdkl.ncs.android.component.home.activity.HomeActivity
|
|
|
+import com.wdkl.ncs.android.component.nursehome.common.Constants
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.channel.DeviceChannel
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.channel.VideoUtil
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.dto.TcpModel
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.enums.TcpAction
|
|
|
+import com.wdkl.ncs.android.middleware.utils.MessageEvent
|
|
|
+import com.wdkl.skywebrtc.CallSession
|
|
|
+import com.wdkl.skywebrtc.EnumType
|
|
|
+import com.wdkl.skywebrtc.EnumType.CallState
|
|
|
+import com.wdkl.skywebrtc.SkyEngineKit
|
|
|
+import org.greenrobot.eventbus.EventBus
|
|
|
+import org.greenrobot.eventbus.Subscribe
|
|
|
+import org.greenrobot.eventbus.ThreadMode
|
|
|
+import org.webrtc.SurfaceViewRenderer
|
|
|
+import java.util.*
|
|
|
+
|
|
|
+class VisitFragment: Fragment(), CallSession.CallSessionCallback, View.OnClickListener {
|
|
|
+
|
|
|
+ private var activity: HomeActivity? = null
|
|
|
+ private var gEngineKit: SkyEngineKit? = null
|
|
|
+
|
|
|
+ var currentState: CallState? = null
|
|
|
+
|
|
|
+ private var fullscreenRenderer: FrameLayout? = null
|
|
|
+ private var pipRenderer: FrameLayout? = null
|
|
|
+ private var localSurfaceView: SurfaceViewRenderer? = null
|
|
|
+ private var remoteSurfaceView: SurfaceViewRenderer? = null
|
|
|
+ private var durationTextView: Chronometer? = null
|
|
|
+ private var outgoingHangupImageView: ImageView? = null
|
|
|
+ private var stateView: TextView? = null
|
|
|
+
|
|
|
+ private val handler = Handler(Looper.getMainLooper())
|
|
|
+
|
|
|
+ private var outGoing = false
|
|
|
+ private var text: String? = null
|
|
|
+ private var targetId: String? = null
|
|
|
+
|
|
|
+ override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
+ super.onCreate(savedInstanceState)
|
|
|
+ if (arguments != null) {
|
|
|
+ outGoing = arguments.getBoolean("out_going")
|
|
|
+ text = arguments.getString("show_text")
|
|
|
+ targetId = arguments.getString("target_id")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onAttach(context: Context?) {
|
|
|
+ super.onAttach(context)
|
|
|
+ activity = getActivity() as HomeActivity
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onCreateView(
|
|
|
+ inflater: LayoutInflater?,
|
|
|
+ container: ViewGroup?,
|
|
|
+ savedInstanceState: Bundle?
|
|
|
+ ): View? {
|
|
|
+ val view = inflater?.inflate(com.wdkl.ncs.android.component.home.R.layout.fragment_visit, container, false)
|
|
|
+ initView(view)
|
|
|
+ init()
|
|
|
+
|
|
|
+ /*if (!EventBus.getDefault().isRegistered(this)) {
|
|
|
+ EventBus.getDefault().register(this)
|
|
|
+ }*/
|
|
|
+
|
|
|
+ return view
|
|
|
+ }
|
|
|
+
|
|
|
+ fun initView(view: View?) {
|
|
|
+ fullscreenRenderer = view!!.findViewById(R.id.visit_fullscreen_video_view)
|
|
|
+ pipRenderer = view.findViewById(R.id.visit_pip_video_view)
|
|
|
+ durationTextView = view.findViewById(R.id.duration_text)
|
|
|
+ outgoingHangupImageView = view.findViewById(R.id.hangup_aciton)
|
|
|
+ stateView = view.findViewById(R.id.visit_status)
|
|
|
+
|
|
|
+ durationTextView?.visibility = View.GONE
|
|
|
+ stateView?.text = text
|
|
|
+ outgoingHangupImageView?.setOnClickListener(this)
|
|
|
+ }
|
|
|
+
|
|
|
+ fun init() {
|
|
|
+ gEngineKit = SkyEngineKit.Instance()
|
|
|
+ SkyEngineKit.init(VoipEvent())
|
|
|
+ if (outGoing) {
|
|
|
+ if (!TextUtils.isEmpty(targetId)) {
|
|
|
+ // 创建会话
|
|
|
+ val room = UUID.randomUUID().toString() + System.currentTimeMillis()
|
|
|
+ val b = gEngineKit?.startOutCall(activity?.applicationContext, room, targetId, false)
|
|
|
+ val session = gEngineKit?.getCurrentSession()
|
|
|
+ if (session != null) {
|
|
|
+ session.setSessionCallback(this)
|
|
|
+ session.toggleSpeaker(true)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ val session = gEngineKit?.getCurrentSession()
|
|
|
+ if (session != null) {
|
|
|
+ session.setSessionCallback(this)
|
|
|
+ session.toggleSpeaker(true)
|
|
|
+
|
|
|
+ if (session.state == CallState.Incoming) {
|
|
|
+ session.joinHome(session.roomId)
|
|
|
+ } else {
|
|
|
+ session.sendRefuse()
|
|
|
+ EventBus.getDefault().post(MessageEvent("back_to_main", Constants.BACK_TO_MAIN_MSG))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ val session = gEngineKit?.getCurrentSession()
|
|
|
+ if (session != null) {
|
|
|
+ currentState = session.state
|
|
|
+ if (currentState == CallState.Incoming) {
|
|
|
+ val surfaceView = session.setupLocalVideo(false)
|
|
|
+
|
|
|
+ if (surfaceView != null) {
|
|
|
+ localSurfaceView = surfaceView as SurfaceViewRenderer
|
|
|
+ localSurfaceView?.setZOrderMediaOverlay(false)
|
|
|
+ fullscreenRenderer!!.addView(localSurfaceView)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onClick(v: View?) {
|
|
|
+ val id = v!!.id
|
|
|
+ val session = gEngineKit!!.currentSession
|
|
|
+ // 挂断电话
|
|
|
+ if (id == R.id.hangup_aciton) {
|
|
|
+ if (session != null) {
|
|
|
+ SkyEngineKit.Instance().endCall()
|
|
|
+ }
|
|
|
+ if (Constants.visitHostId != -1) {
|
|
|
+ VideoUtil.handoffVideoCall(Constants.deviceId, Constants.visitHostId, Constants.interactionId)
|
|
|
+ }
|
|
|
+ EventBus.getDefault().post(MessageEvent("back_to_main", Constants.BACK_TO_MAIN_MSG))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fun startRefreshTime() {
|
|
|
+ val session = SkyEngineKit.Instance().currentSession
|
|
|
+ if (session == null) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (durationTextView != null) {
|
|
|
+ durationTextView!!.visibility = View.VISIBLE
|
|
|
+ durationTextView!!.base = SystemClock.elapsedRealtime() - (System.currentTimeMillis() - session.startTime)
|
|
|
+ durationTextView!!.start()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun didCallEndWithReason(var1: EnumType.CallEndReason?) {
|
|
|
+ DeviceChannel.calling = false
|
|
|
+ /*if (Constants.visitHostId != -1) {
|
|
|
+ VideoUtil.handoffVideoCall(Constants.deviceId, Constants.visitHostId, Constants.interactionId)
|
|
|
+ }*/
|
|
|
+ EventBus.getDefault().post(MessageEvent("back_to_main", Constants.BACK_TO_MAIN_MSG))
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun didChangeState(var1: CallState?) {
|
|
|
+ currentState = var1
|
|
|
+ handler.post {
|
|
|
+ if (currentState == CallState.Connected) {
|
|
|
+ // 开启计时器
|
|
|
+ startRefreshTime()
|
|
|
+ stateView?.visibility = View.GONE
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun didChangeMode(isAudioOnly: Boolean) {
|
|
|
+ //
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun didCreateLocalVideoTrack() {
|
|
|
+ Log.d("wzlll", "didCreateLocalVideoTrack")
|
|
|
+ handler.post {
|
|
|
+ if (localSurfaceView == null) {
|
|
|
+ val surfaceView = gEngineKit!!.currentSession.setupLocalVideo(true)
|
|
|
+ if (surfaceView != null) {
|
|
|
+ localSurfaceView = surfaceView as SurfaceViewRenderer
|
|
|
+ } else {
|
|
|
+ EventBus.getDefault().post(MessageEvent("back_to_main", Constants.BACK_TO_MAIN_MSG))
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ localSurfaceView!!.setZOrderMediaOverlay(true)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (localSurfaceView!!.parent != null) {
|
|
|
+ (localSurfaceView!!.parent as ViewGroup).removeView(localSurfaceView)
|
|
|
+ }
|
|
|
+ if (outGoing && remoteSurfaceView == null) {
|
|
|
+ if (fullscreenRenderer != null && fullscreenRenderer!!.childCount != 0) {
|
|
|
+ fullscreenRenderer!!.removeAllViews()
|
|
|
+ }
|
|
|
+ fullscreenRenderer!!.addView(localSurfaceView)
|
|
|
+ } else {
|
|
|
+ if (pipRenderer!!.childCount != 0) {
|
|
|
+ pipRenderer!!.removeAllViews()
|
|
|
+ }
|
|
|
+ pipRenderer!!.addView(localSurfaceView)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun didReceiveRemoteVideoTrack(userId: String?) {
|
|
|
+ Log.d("wzlll", "didReceiveRemoteVideoTrack user: " + userId)
|
|
|
+
|
|
|
+ handler.post {
|
|
|
+ pipRenderer!!.visibility = View.VISIBLE
|
|
|
+ if (localSurfaceView != null) {
|
|
|
+ localSurfaceView!!.setZOrderMediaOverlay(true)
|
|
|
+ if (outGoing) {
|
|
|
+ if (localSurfaceView!!.parent != null) {
|
|
|
+ (localSurfaceView!!.parent as ViewGroup).removeView(localSurfaceView)
|
|
|
+ }
|
|
|
+ pipRenderer!!.addView(localSurfaceView)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ val surfaceView = gEngineKit!!.currentSession.setupRemoteVideo(userId, false)
|
|
|
+ if (surfaceView != null) {
|
|
|
+ fullscreenRenderer!!.visibility = View.VISIBLE
|
|
|
+ remoteSurfaceView = surfaceView as SurfaceViewRenderer
|
|
|
+ fullscreenRenderer!!.removeAllViews()
|
|
|
+ if (remoteSurfaceView?.getParent() != null) {
|
|
|
+ (remoteSurfaceView?.getParent() as ViewGroup).removeView(remoteSurfaceView)
|
|
|
+ }
|
|
|
+ fullscreenRenderer!!.addView(remoteSurfaceView)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun didUserLeave(userId: String?) {
|
|
|
+ //
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun didError(error: String?) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun didDisconnected(userId: String?) {
|
|
|
+ //
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onDestroyView() {
|
|
|
+ super.onDestroyView()
|
|
|
+ DeviceChannel.calling = false
|
|
|
+/* if (EventBus.getDefault().isRegistered(this)) {
|
|
|
+ EventBus.getDefault().unregister(this)
|
|
|
+ }*/
|
|
|
+ if (durationTextView != null) {
|
|
|
+ durationTextView!!.stop()
|
|
|
+ }
|
|
|
+ fullscreenRenderer!!.removeAllViews()
|
|
|
+ pipRenderer!!.removeAllViews()
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+/* @Subscribe(threadMode = ThreadMode.MAIN)
|
|
|
+ fun onMoonEvent(messageEvent: MessageEvent) {
|
|
|
+ if (messageEvent.getType() == Constants.VIDEO_MSG) {
|
|
|
+ if (messageEvent.getMessage() is TcpModel) {
|
|
|
+ val tcpModel = messageEvent.getMessage() as TcpModel?
|
|
|
+ if (tcpModel!!.action == TcpAction.VideoAction.HANDOFF) {
|
|
|
+ val session = gEngineKit!!.currentSession
|
|
|
+ if (session != null) {
|
|
|
+ SkyEngineKit.Instance().endCall()
|
|
|
+ }
|
|
|
+ EventBus.getDefault().post(MessageEvent("back_to_main", Constants.BACK_TO_MAIN_MSG))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }*/
|
|
|
+
|
|
|
+}
|