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
package ezviz.ezopensdk.preview
 
import android.content.Context
import android.graphics.SurfaceTexture
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.support.v7.app.AlertDialog
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.TextureView
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.Toast
import com.videogo.EzvizApplication
import com.videogo.exception.ErrorCode.ERROR_INNER_VERIFYCODE_ERROR
import com.videogo.openapi.EZConstants.EZRealPlayConstants.MSG_REALPLAY_PLAY_FAIL
import com.videogo.openapi.EZConstants.EZRealPlayConstants.MSG_REALPLAY_PLAY_SUCCESS
import com.videogo.openapi.bean.EZCameraInfo
import com.videogo.openapi.bean.EZDeviceInfo
import com.videogo.ui.realplay.EZRealPlayActivity
import ezviz.ezopensdk.R
import ezviz.ezopensdkcommon.common.LogUtil
import kotlin.concurrent.thread
 
/**
 * 此处简要说明此文件用途
 * Created by zhuwen6 on 2020/4/23
 */
class MultiScreenPreviewAdapter(private val mContext: Context, private val mDeviceList: List<EZDeviceInfo>,
                                private val mCameraList: List<EZCameraInfo>?, private var mColumn: Int = 1) :
        RecyclerView.Adapter<RecyclerView.ViewHolder>() {
 
    fun stopAll(){
        for (playInfo in PlayInfoManager.getAll().values){
            playInfo.player?.stopRealPlay()
        }
    }
 
    fun startAll(){
        for (playInfo in PlayInfoManager.getAll().values){
            playInfo.player?.startRealPlay()
        }
    }
 
    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): RecyclerView.ViewHolder {
        LogUtil.d(TAG, "onCreateViewHolder")
        val view = LayoutInflater.from(mContext).inflate(R.layout.item_multi_screen_preview, viewGroup, false).apply {
            layoutParams.width = viewGroup.width / mColumn
            layoutParams.height = layoutParams.width / 16 * 9
        }
        return MultiScreenViewHolder(view)
    }
 
    override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, i: Int) {
        LogUtil.d(TAG, "onBindViewHolder")
        val holder = viewHolder as MultiScreenViewHolder
        mCameraList?.get(i)?.apply {
            LogUtil.d(TAG, "onBindViewHolder: $cameraName")
            val playInfoKey = getPlayInfoKeyBy(this)
            var playInfo = PlayInfoManager.get(playInfoKey)
            if (playInfo == null){
                var deviceInfo: EZDeviceInfo? = null
                for (device in mDeviceList){
                    if (device.deviceSerial == deviceSerial){
                        deviceInfo = device
                        break
                    }
                }
                playInfo = PlayInfo(playInfoKey, deviceInfo!!, this, this@MultiScreenPreviewAdapter, holder)
                PlayInfoManager.put(playInfo)
            }
            playInfo.holder = holder
            if (playInfo.deviceInfo.status == 1){
                changePlayViewsBy(PlayStatusEnum.LOADING, playInfoKey)
            }else{
                changePlayViewsBy(PlayStatusEnum.OFFLINE, playInfoKey)
            }
        }
    }
 
    fun changePlayViewsBy(playStatus: PlayStatusEnum, playInfoKey: String){
        val playInfo = PlayInfoManager.get(playInfoKey)
        if (playInfo == null){
            LogUtil.e(TAG, "changePlayViewsBy: return")
            return
        }
        playInfo.playStatus = playStatus
        val viewHolder = playInfo.holder
        val cameraInfo = playInfo.cameraInfo
        viewHolder.apply {
            cameraNameTv?.text = cameraInfo.cameraName
            if (playStatus != PlayStatusEnum.OFFLINE){
                playWindowTextureView?.surfaceTextureListener = SurfaceTextureListerWithControlPreview(playInfoKey)
            }
        }
        when(playStatus){
            PlayStatusEnum.OFFLINE -> {
                viewHolder.playLoadingPb?.visibility = View.GONE
                viewHolder.playErrorTv?.apply {
                    visibility = View.VISIBLE
                    text = "设备不在线"
                }
            }
            PlayStatusEnum.LOADING -> {
                viewHolder.playLoadingPb?.visibility = View.VISIBLE
                viewHolder.playErrorTv?.apply {
                    visibility = View.GONE
                    setOnClickListener(ErrorMessageOnClickListener(playInfoKey))
                }
                viewHolder.itemView.setOnClickListener(PlayWindowOnClickListener(playInfoKey))
            }
            PlayStatusEnum.PLAYING -> {
                viewHolder.playLoadingPb?.visibility = View.GONE
                viewHolder.playErrorTv?.apply {
                    visibility = View.GONE
                }?.setOnClickListener(null)
            }
            PlayStatusEnum.ERROR_VERIFY_CODE -> {
                viewHolder.playLoadingPb?.visibility = View.GONE
                viewHolder.playErrorTv?.apply {
                    visibility = View.VISIBLE
                    text = "视频已加密"
                }
            }
            else -> {
                LogUtil.e(TAG, "未知状态")
            }
        }
    }
 
    override fun getItemCount(): Int {
        val cnt = mCameraList?.size ?: 0
        LogUtil.d(TAG, "getItemCount: $cnt")
        return cnt
    }
 
    class PlayWindowOnClickListener(private val mPlayInfoKey: String): View.OnClickListener{
 
        override fun onClick(view: View?) {
            val context = view?.context!!
            val playInfo = PlayInfoManager.get(mPlayInfoKey)
            if (playInfo?.playStatus == PlayStatusEnum.PLAYING){
                EZRealPlayActivity.launch(context, playInfo.deviceInfo, playInfo.cameraInfo)
            }else{
                LogUtil.d(TAG, "非播放状态,点击无效")
            }
        }
 
    }
 
    class ErrorMessageOnClickListener(private val mPlayInfoKey: String): View.OnClickListener{
 
        override fun onClick(view: View?) {
            val playInfo = PlayInfoManager.get(mPlayInfoKey)
            when(playInfo?.lastError){
                ERROR_INNER_VERIFYCODE_ERROR -> {
                    val context = view?.context!!
                    val verifyCodeEt = EditText(context)
                    AlertDialog.Builder(context)
                            .setTitle("请输入验证码")
                            .setView(verifyCodeEt)
                            .setPositiveButton("确定") {
                                dialog, _ ->
                                run {
                                    val verifyCode = verifyCodeEt.text.toString()
                                    if (TextUtils.isEmpty(verifyCode)){
                                        Toast.makeText(context, "请输入有效的验证码", Toast.LENGTH_SHORT).show()
                                    }else{
                                        dialog.dismiss()
                                        playInfo.verifyCode = verifyCode
                                        playInfo.adapter.changePlayViewsBy(PlayStatusEnum.LOADING, mPlayInfoKey)
                                        playInfo.player?.apply {
                                            setPlayVerifyCode(verifyCode)
                                            startRealPlay()
                                        }
                                    }
                                }
                            }
                            .show()
                }
                else -> {
                    LogUtil.d(TAG, "无需处理此点击事件")
                }
            }
        }
 
    }
 
    class SurfaceTextureListerWithControlPreview(private val mPlayInfoKey: String): TextureView.SurfaceTextureListener{
 
        override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) {
 
        }
 
        override fun onSurfaceTextureUpdated(surface: SurfaceTexture?) {
 
        }
 
        override fun onSurfaceTextureDestroyed(surface: SurfaceTexture?): Boolean {
            releasePlayer()
            return true
        }
 
        private fun releasePlayer() {
            LogUtil.d(TAG, "stopPlay")
            thread {
                PlayInfoManager.get(mPlayInfoKey)?.player?.apply {
                    setHandler(null)
                    release()
                }
            }
        }
 
        override fun onSurfaceTextureAvailable(surface: SurfaceTexture?, width: Int, height: Int) {
            initPlayer(surface)
        }
 
        private fun initPlayer(surface: SurfaceTexture?) {
            LogUtil.d(TAG, "startPlay")
            thread {
                val playInfo = PlayInfoManager.get(mPlayInfoKey)
                EzvizApplication.getOpenSDK().createPlayer(playInfo?.cameraInfo?.deviceSerial, playInfo?.cameraInfo?.cameraNo!!)?.apply {
                    playInfo.player = this
                    Handler(Looper.getMainLooper()).post{
                        PlayMessageHandler(mPlayInfoKey).apply {
                            playInfo.handler = this
                            setHandler(this)
                        }
                    }
                    setSurfaceEx(surface)
                    startRealPlay()
                }
            }
        }
    }
 
    class PlayMessageHandler(private val mPlayInfoKey: String): Handler(){
        override fun handleMessage(msg: Message?) {
            LogUtil.d(TAG, "handleMessage" + msg?.toString())
            when(msg?.what){
                // 播放成功
                MSG_REALPLAY_PLAY_SUCCESS -> {
                    PlayInfoManager.get(mPlayInfoKey)?.apply {
                        adapter.changePlayViewsBy(PlayStatusEnum.PLAYING, mPlayInfoKey)
                    }
                }
                // 播放失败
                MSG_REALPLAY_PLAY_FAIL -> {
                    val errCd = msg.arg1
                    PlayInfoManager.get(mPlayInfoKey)?.apply {
                        lastError = errCd
                        when(errCd) {
                            // 验证码错误
                            ERROR_INNER_VERIFYCODE_ERROR -> adapter.changePlayViewsBy(PlayStatusEnum.ERROR_VERIFY_CODE, mPlayInfoKey)
                            else -> LogUtil.e(TAG, "未知错误,摄像头:$mPlayInfoKey, 错误码:$errCd")
                        }
                    }
                }
            }
        }
    }
 
    enum class PlayStatusEnum(){
        LOADING, PLAYING, PAUSED, OFFLINE, ERROR_VERIFY_CODE
    }
 
    class MultiScreenViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
 
        var playWindowTextureView: TextureView? = null
        var cameraNameTv: TextView? = null
        var playLoadingPb: ProgressBar? = null
        var playErrorTv: TextView? = null
 
        init {
            findViews()
        }
 
        private fun findViews() {
            itemView.apply {
                playWindowTextureView = findViewById(R.id.texture_view_play_window)
                cameraNameTv = findViewById(R.id.tv_camera_name)
                playLoadingPb = findViewById(R.id.pb_play_loading)
                playErrorTv = findViewById(R.id.tv_play_error)
            }
        }
 
    }
 
    companion object{
        private val TAG = MultiScreenPreviewAdapter::class.java.simpleName
 
        fun getPlayInfoKeyBy(cameraInfo: EZCameraInfo): String{
            return cameraInfo.deviceSerial + "-" + cameraInfo.cameraNo
        }
    }
 
}