加入重連機制

This commit is contained in:
Raymond Yang 2023-01-30 16:15:34 +08:00
parent cdd2c3775d
commit ac15111ab3

View File

@ -47,14 +47,20 @@ class VideoView : ConstraintLayout, SurfaceHolder.Callback, GstCallback {
field = value field = value
} }
private var retryCount = 0
private var retryRunnable = object : Runnable {
override fun run() {
play()
Log.e("${TAG}_$tag", "Retry count: $retryCount")
}
}
private val videoView: SurfaceView by lazy { view.videoView } private val videoView: SurfaceView by lazy { view.videoView }
private lateinit var gstLibrary: GstLibrary private lateinit var gstLibrary: GstLibrary
private var streamType = MAIN_STREAM private var streamType = MAIN_STREAM
private var retryCount = 0
private val mHandler: MyHandler by lazy { private val mHandler: MyHandler by lazy {
MyHandler(Looper.getMainLooper()) MyHandler(Looper.getMainLooper())
} }
@ -88,17 +94,19 @@ class VideoView : ConstraintLayout, SurfaceHolder.Callback, GstCallback {
} }
fun play() { fun play() {
if (retryCount == RETRY_OFF) {
return
}
videoView.postInvalidate() videoView.postInvalidate()
gstLibrary.play() gstLibrary.play()
retryCount = 0
} }
fun stop() { fun stop() {
isPlaying = false isPlaying = false
retryCount = RETRY_OFF
if (this::gstLibrary.isInitialized) { if (this::gstLibrary.isInitialized) {
gstLibrary.stop() gstLibrary.stop()
} }
retryCount = 999
} }
override fun surfaceCreated(holder: SurfaceHolder) { override fun surfaceCreated(holder: SurfaceHolder) {
@ -124,8 +132,9 @@ class VideoView : ConstraintLayout, SurfaceHolder.Callback, GstCallback {
when (gstStatus) { when (gstStatus) {
GstStatus.PLAYING -> mHandler.sendMessage(Message().apply { what = MSG_PLAY }) GstStatus.PLAYING -> mHandler.sendMessage(Message().apply { what = MSG_PLAY })
GstStatus.PAUSE -> mHandler.sendMessage(Message().apply { what = MSG_PAUSE }) GstStatus.PAUSE -> mHandler.sendMessage(Message().apply { what = MSG_PAUSE })
GstStatus.ERROR_WHEN_OPENING -> mHandler.sendMessage(Message().apply { what = MSG_PAUSE }) //GstStatus.ERROR_WHEN_OPENING -> mHandler.sendMessage(Message().apply { what = MSG_PAUSE })
GstStatus.BUFFERING -> mHandler.sendMessage(Message().apply { what = MSG_BUFFERING }) GstStatus.BUFFERING -> mHandler.sendMessage(Message().apply { what = MSG_BUFFERING })
GstStatus.ERROR_WHEN_OPENING -> mHandler.sendMessage(Message().apply { what = MSG_ERROR })
else -> {} else -> {}
} }
Log.e("${TAG}_$tag", "onStatus: $gstStatus") Log.e("${TAG}_$tag", "onStatus: $gstStatus")
@ -145,11 +154,21 @@ class VideoView : ConstraintLayout, SurfaceHolder.Callback, GstCallback {
MSG_PLAY -> { MSG_PLAY -> {
isLoading = false isLoading = false
isPlaying = true isPlaying = true
retryCount = 0
} }
MSG_BUFFERING -> { MSG_BUFFERING -> {
isLoading = true isLoading = true
isPlaying = true isPlaying = true
} }
MSG_ERROR -> {
if (retryCount != RETRY_OFF && retryCount in 0 until 5) {
mHandler.post(retryRunnable)
retryCount++
} else {
retryCount = RETRY_OFF
Log.e("${TAG}_$tag", "Retry count = 5, stopped retry...")
}
}
} }
} }
} }
@ -163,5 +182,8 @@ class VideoView : ConstraintLayout, SurfaceHolder.Callback, GstCallback {
private const val MSG_PAUSE = 1 private const val MSG_PAUSE = 1
private const val MSG_PLAY = 2 private const val MSG_PLAY = 2
private const val MSG_BUFFERING = 3 private const val MSG_BUFFERING = 3
private const val MSG_ERROR = 4
private const val RETRY_OFF = 999
} }
} }