加入重連機制

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
}
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 lateinit var gstLibrary: GstLibrary
private var streamType = MAIN_STREAM
private var retryCount = 0
private val mHandler: MyHandler by lazy {
MyHandler(Looper.getMainLooper())
}
@ -88,17 +94,19 @@ class VideoView : ConstraintLayout, SurfaceHolder.Callback, GstCallback {
}
fun play() {
if (retryCount == RETRY_OFF) {
return
}
videoView.postInvalidate()
gstLibrary.play()
retryCount = 0
}
fun stop() {
isPlaying = false
retryCount = RETRY_OFF
if (this::gstLibrary.isInitialized) {
gstLibrary.stop()
}
retryCount = 999
}
override fun surfaceCreated(holder: SurfaceHolder) {
@ -124,8 +132,9 @@ class VideoView : ConstraintLayout, SurfaceHolder.Callback, GstCallback {
when (gstStatus) {
GstStatus.PLAYING -> mHandler.sendMessage(Message().apply { what = MSG_PLAY })
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.ERROR_WHEN_OPENING -> mHandler.sendMessage(Message().apply { what = MSG_ERROR })
else -> {}
}
Log.e("${TAG}_$tag", "onStatus: $gstStatus")
@ -145,11 +154,21 @@ class VideoView : ConstraintLayout, SurfaceHolder.Callback, GstCallback {
MSG_PLAY -> {
isLoading = false
isPlaying = true
retryCount = 0
}
MSG_BUFFERING -> {
isLoading = 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_PLAY = 2
private const val MSG_BUFFERING = 3
private const val MSG_ERROR = 4
private const val RETRY_OFF = 999
}
}