From ac15111ab3e8ca633e272f69c0078e987d9b02ae Mon Sep 17 00:00:00 2001 From: Raymond Yang Date: Mon, 30 Jan 2023 16:15:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E9=87=8D=E9=80=A3=E6=A9=9F?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ray650128/gstreamer_demo_app/VideoView.kt | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/ray650128/gstreamer_demo_app/VideoView.kt b/app/src/main/java/com/ray650128/gstreamer_demo_app/VideoView.kt index c9a7688..10d6aee 100644 --- a/app/src/main/java/com/ray650128/gstreamer_demo_app/VideoView.kt +++ b/app/src/main/java/com/ray650128/gstreamer_demo_app/VideoView.kt @@ -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 } } \ No newline at end of file