1
wxr
2023-04-23 2cd55265ccff3b0a267d7953b2dd9e5dca437aa6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package com.ezviz.demo.videotalk
 
import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.SurfaceTexture
import android.os.Build
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.view.Surface
import android.view.TextureView
import android.view.View
import android.widget.Toast
import com.ezviz.sdk.videotalk.*
import com.ezviz.sdk.videotalk.EvcParamValueEnum.EvcOperationEnum
import ezviz.ezopensdk.R
import ezviz.ezopensdkcommon.common.LogUtil
import kotlinx.android.synthetic.main.activity_video_talk.*
import org.json.JSONObject
import java.util.*
import kotlin.concurrent.thread
 
class WatchVideoTalkActivity : Activity(){
 
    companion object {
        /*视频通话服务器域名*/
        private const val SERVER_DOMAIN = "vtm.ys7.com"
        /*视频通话服务器地址*/
        private const val SERVER_PORT = 8554
        /*对应手表联系人ID*/
        private const val SELF_ID = "1234567891"
        private const val TAG = "@@VideoTalkActivity"
        private const val ERROR = -1
    }
 
    @SuppressLint("ObsoleteSdkInt")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 不支持AP19以下版本
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            finish()
            runOnUiThread { toast3s("手机版本太低,不支持视频通话功能") }
        }
        setContentView(R.layout.activity_video_talk)
        initData()
        initViews()
        initListeners()
        // 实例化视频通话对象
        EzvizVideoCall(mCameraView as EvcLocalWindowView?, mEvcMsgCallback).run {
            mEzvizVideoCall = this
            setLogPrintEnable(true)
        }
    }
 
    @Suppress("UNUSED_PARAMETER")
    fun onClickCreateRoom(view: View) {
        thread { createCall() }
    }
 
    @Suppress("UNUSED_PARAMETER")
    fun onClickJoinRoom(view: View) {
        thread { answerCall() }
    }
 
    @Suppress("UNUSED_PARAMETER")
    fun onClickLeaveRoom(view: View) {
        thread { mEzvizVideoCall?.stopVideoTalk() }
    }
 
    /**
     * 初始化通话参数
     */
    private fun initData() {
        mWatchSerial = intent.getStringExtra(InIntentKeysAndValues.KEY_DEVICE_SERIAL)
        mServerDomain = SERVER_DOMAIN
        mServerPort = SERVER_PORT
        mSelfId = SELF_ID
    }
 
    /**
     * 初始化视图
     */
    private fun initViews() {
        mCameraView = findViewById(R.id.view_child_watch_video_talk_camera)
        mPlayerView = findViewById(R.id.view_child_watch_video_talk_player)
    }
 
    /**
     * 初始化监听器
     */
    private fun initListeners() {
        mPlayerView!!.surfaceTextureListener = mSurfaceTextureListener
    }
 
    fun dip2px(dipValue: Float): Int {
        val scale = resources.displayMetrics.density
        return (dipValue * scale + 0.5f).toInt()
    }
 
    private fun createCall() {
        mCurrentTalkState = TalkStateEnum.CALLER_CALLING
        startVideoTalk(EvcOperationEnum.CALL, 0)
    }
 
    private fun answerCall() {
        updateRoomId()
        if (isValidRoomId()){
            startVideoTalk(EvcOperationEnum.ANSWER, mInputtedRoomID)
        }
    }
 
    private fun refuseTalk() {
        updateRoomId()
        if (isValidRoomId()){
            startVideoTalk(EvcOperationEnum.REFUSE, mInputtedRoomID)
        }
    }
 
    private fun updateRoomId(){
        val inputted = video_talk_et_room_id.text
        return try {
            mInputtedRoomID = inputted.toString().toInt()
        }catch (e: java.lang.Exception){
            e.printStackTrace()
            showToast("无效的房间号")
        }
    }
 
    private fun isValidRoomId(): Boolean{
        return mInputtedRoomID >= 0
    }
 
    private fun startVideoTalk(operation: EvcOperationEnum, roomId: Int) {
        if (!hasNeededPermissions()){
            return
        }
        val param = EvcParam()
        param.operation = operation
        param.roomId = roomId
        param.serverIp = mServerDomain
        param.serverPort = mServerPort
        param.selfClientType = EvcParamValueEnum.EvcClientType.ANDROID_PHONE
        param.otherClientType = EvcParamValueEnum.EvcClientType.CHILD_WATCH
        param.otherId = mWatchSerial
        param.selfId = mSelfId
        mEzvizVideoCall?.startVideoTalk(param)
    }
 
    /**
     * 发起或者加入通话前检查权限
     */
    private fun hasNeededPermissions(): Boolean {
        if(isLackOfCameraPermission){
            showToast(EvcErrorMessage.LACK_OF_CAMERA_PERMISSION)
            return false
        }
        if(isLackOfRecordAudioPermission){
            showToast(EvcErrorMessage.LACK_OF_RECORD_AUDIO_PERMISSION)
            return false
        }
        return true
    }
 
    private fun showToast(error: EvcErrorMessage) {
        showToast("${error.code}\n${error.desc}")
    }
 
    private fun showToast(toastMessage: String) {
        LogUtil.e(TAG, toastMessage)
        runOnUiThread {
            toast3s(toastMessage)
        }
    }
 
    private fun toast3s(toastMessage: String) {
        runOnUiThread {
            if (mCurrentToast != null) {
                mCurrentToast!!.cancel()
                mCurrentToast = null
            }
            val toast = Toast.makeText(applicationContext, toastMessage, Toast.LENGTH_LONG)
            toast.show()
            mCurrentToast = toast
            Timer().schedule(object : TimerTask() {
                override fun run() {
                    if (mCurrentToast === toast) {
                        mCurrentToast!!.cancel()
                    }
                }
            }, 3000)
        }
    }
 
    private val mEvcMsgCallback: EvcMsgCallback = object : EvcMsgCallback() {
        override fun onMessage(code: Int, desc: String) {
            showToast("onMessage: $code\n$desc")
            if (isFinishing) {
                return
            }
            if (code == 50017) {
                runOnUiThread {
                    AlertDialog.Builder(this@WatchVideoTalkActivity).setMessage(R.string.video_talk_call_is_accepted).setCancelable(false).setPositiveButton(R.string.confirm) { dialog, _ ->
                        dialog.dismiss()
                        finish()
                    }.show()
                }
            } else if (code == 50103 || code == 50106) {
                runOnUiThread {
                    AlertDialog.Builder(this@WatchVideoTalkActivity).setMessage(R.string.video_talk_watch_is_busy).setCancelable(false).setPositiveButton(R.string.confirm) { dialog, _ ->
                        dialog.dismiss()
                        finish()
                    }.show()
                }
            } else if (code == 50104) {
                runOnUiThread {
                    AlertDialog.Builder(this@WatchVideoTalkActivity).setMessage(R.string.video_talk_watch_temperature_high_reject).setCancelable(false).setPositiveButton(R.string.confirm) { dialog, _ ->
                        dialog.dismiss()
                        finish()
                    }.show()
                }
            } else if (code == 50203) {
                runOnUiThread {
                    AlertDialog.Builder(this@WatchVideoTalkActivity).setMessage(R.string.video_talk_watch_temperature_high_hang).setCancelable(false).setPositiveButton(R.string.confirm) { dialog, _ ->
                        dialog.dismiss()
                        finish()
                    }.show()
                }
            } else if (code == 50105) {
                runOnUiThread {
                    AlertDialog.Builder(this@WatchVideoTalkActivity).setMessage(R.string.video_talk_is_playing).setCancelable(false).setPositiveButton(R.string.confirm) { dialog, _ ->
                        dialog.dismiss()
                        finish()
                    }.show()
                }
            }else if (code == 10152) {
                isLackOfRecordAudioPermission = true
            }else if (code == 20153) {
                isLackOfCameraPermission = true
            }
        }
 
        override fun onRcvLucidMsg(msg: String) {
            runOnUiThread {
                try {
                    val json = JSONObject(msg)
                    val type = json.optInt("type", -1)
                    if (type == 0) {
                        showToast(getString(R.string.video_talk_watch_temperature_high_warn))
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
 
        override fun onRoomCreated(roomId: Int) {
            showToast("onRoomCreated: $roomId")
        }
 
        override fun onCallEstablished(width: Int, height: Int) {
            runOnUiThread {
                if (InIntentKeysAndValues.VALUE_CALLER == mRole) {
                    mCurrentTalkState = TalkStateEnum.CALLER_TALKING
                }
                if (InIntentKeysAndValues.VALUE_ANSWER == mRole) {
                    mCurrentTalkState = TalkStateEnum.ANSWER_TALKING
                }
            }
        }
 
        override fun onOtherRefused() {
            showToast("对方已拒绝")
            finish()
        }
 
        override fun onOtherNoneAnswered() {
            if (mCurrentTalkState == TalkStateEnum.CALLER_CALLING && !isFinishing) {
                toast3s(getString(R.string.video_talk_sdk_toast_nobody))
                finish()
            } else if (TalkStateEnum.ANSWER_TALKING == mCurrentTalkState) {
                toast3s(getString(R.string.video_talk_sdk_toast_hang_up))
                refuseTalk()
                finish()
            }
        }
 
        override fun onOtherHangedUp() {
            toast3s(getString(R.string.video_talk_sdk_toast_hang_up))
            finish()
        }
 
        override fun onBadNet(delayTimeMs: Int) {
            showToast(getString(R.string.video_talk_signal_weak))
        }
    }
 
    enum class TalkStateEnum {
        //呼叫方
        CALLER_CALLING,  /*呼叫中*/
        CALLER_TALKING,  /*通话中*/
        CALLER_TALKED,  /*通话后*/ //被呼方
        ANSWER_BEING_CALLED,  /*呼叫中*/
        ANSWER_TALKING,  /*通话中*/
        ANSWER_TALKED /*通话后*/
    }
 
    @Suppress("unused")
    object InIntentKeysAndValues {
        const val KEY_ROLE = "role"
        const val VALUE_CALLER = 0
        const val VALUE_ANSWER = 1
        const val VALUE_REFUSE = 2
        const val KEY_ROOM_ID = "room_id"
        const val KEY_NICK_NAME = "nick_name"
        const val KEY_HEAD_PORTRAIT_REMOTE = "head_portrait_remote"
        const val KEY_HEAD_PORTRAIT_LOCAL = "head_portrait_remote"
        const val KEY_DEVICE_SERIAL = "device_serial"
        const val KEY_TOKEN = "token"
        const val KEY_SERVER = "server"
        const val KEY_SERVER_PORT = "server_port"
        const val KEY_SELF_ID = "caller_id"
    }
 
    override fun onBackPressed() {
        if (TalkStateEnum.CALLER_TALKING == mCurrentTalkState || TalkStateEnum.ANSWER_TALKING == mCurrentTalkState) {
            val ensureDialog = AlertDialog.Builder(this)
                    .setTitle("退出将结束视频聊天")
                    .setNegativeButton("取消") { dialog, _ -> dialog.dismiss() }
                    .setPositiveButton("确定") { dialog, _ ->
                        dialog.dismiss()
                        finish()
                    }
                    .create()
            ensureDialog.show()
        } else {
            super.onBackPressed()
        }
    }
 
    override fun onStop() {
        super.onStop()
        if (mEzvizVideoCall != null) {
            mEzvizVideoCall?.stopVideoTalk()
        }
        if (TalkStateEnum.CALLER_TALKING == mCurrentTalkState
                || TalkStateEnum.CALLER_CALLING == mCurrentTalkState) {
            if (TalkStateEnum.CALLER_TALKING == mCurrentTalkState) {
                toast3s(getString(R.string.video_talk_sdk_toast_hang_up))
            }
            mCurrentTalkState = TalkStateEnum.CALLER_TALKED
            finish()
        }
        if (TalkStateEnum.ANSWER_TALKING == mCurrentTalkState
                || TalkStateEnum.ANSWER_BEING_CALLED == mCurrentTalkState) {
            if (TalkStateEnum.ANSWER_TALKING == mCurrentTalkState) {
                toast3s(getString(R.string.video_talk_sdk_toast_hang_up))
            }
            mCurrentTalkState = TalkStateEnum.ANSWER_TALKED
            finish()
        }
    }
 
    override fun onDestroy() {
        super.onDestroy()
        if (mEzvizVideoCall != null) {
            mEzvizVideoCall?.release()
        }
    }
 
    private val mSurfaceTextureListener = object : TextureView.SurfaceTextureListener{
 
        override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) {
            // do nothing
        }
 
        override fun onSurfaceTextureUpdated(surface: SurfaceTexture?) {
            // do nothing
        }
 
        override fun onSurfaceTextureDestroyed(surface: SurfaceTexture?): Boolean {
            mEzvizVideoCall?.setDisplay(null)
            return true
        }
 
        override fun onSurfaceTextureAvailable(surface: SurfaceTexture?, width: Int, height: Int) {
            mEzvizVideoCall?.setDisplay(Surface(surface!!))
        }
 
    }
 
    private var mRole = ERROR
    private var mInputtedRoomID = ERROR
    private var mCurrentTalkState: TalkStateEnum? = null
    private var mCurrentToast: Toast? = null
    private var mEzvizVideoCall: EzvizVideoCall? = null
    private var mCameraView: View? = null
    private var mPlayerView: TextureView? = null
    // 发起或者接听视频通话的参数
    private var mServerDomain: String? = null
    private var mServerPort = 0
    private var mWatchSerial: String? = null
    private var mSelfId: String? = null
    // 应用权限信息
    private var isLackOfCameraPermission = false
    private var isLackOfRecordAudioPermission = false
 
}