加入TextureView支援

This commit is contained in:
Raymond Yang 2023-05-19 15:37:47 +08:00
parent 02a26445be
commit 7357085fdf
4 changed files with 48 additions and 17 deletions

View File

@ -22,8 +22,6 @@ import kotlin.math.sqrt
class SplitViewFragment : Fragment() {
val viewModel: SplitViewModel by activityViewModels()
private var mPageNum: Int = 0
private var splitMode = Constants.SPLIT_MODE_SINGLE
private var streamType = VideoView.SUB_STREAM
@ -213,32 +211,31 @@ class SplitViewFragment : Fragment() {
fun playAll() = MainScope().launch(Dispatchers.Main) {
if (videoViews.isEmpty()) return@launch
delay(splitMode * Constants.CONF_DELAY_BASE_MILLIS)
//delay(splitMode * Constants.CONF_DELAY_BASE_MILLIS)
for (index in data.indices) {
if (!videoViews[index].isReady) continue
videoViews[index].resetRetryCount()
videoViews[index].play()
//delay(300)
delay(300)
}
}.start()
fun stopAll() = MainScope().launch(Dispatchers.Main) {
if (videoViews.isEmpty()) return@launch
fun stopAll() {
if (videoViews.isEmpty()) return
for (index in data.indices) {
videoViews[index].stopRetryCount()
if (!videoViews[index].isPlaying || videoViews[index].isLoading) continue
videoViews[index].pause()
//delay(300)
}
}.start()
}
fun destroyAll() = MainScope().launch(Dispatchers.Main) {
if (videoViews.isEmpty()) return@launch
fun destroyAll() {
if (videoViews.isEmpty()) return
for (index in data.indices) {
//videoViews[index].destroy()
videoViews[index].destroySurface()
}
}.start()
}
companion object {
private val TAG = SplitViewFragment::class.java.simpleName

View File

@ -9,6 +9,7 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.TextureView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import com.hisharp.gstreamer_player.GstCallback
@ -62,7 +63,7 @@ class VideoView : ConstraintLayout, GstCallback {
}
}
private val videoView: SurfaceView by lazy { view.videoView }
private val videoView: TextureView by lazy { view.videoView }
private lateinit var gstLibrary: GstLibrary
@ -77,7 +78,8 @@ class VideoView : ConstraintLayout, GstCallback {
//videoView.holder.addCallback(this)
gstLibrary = GstLibrary(context)
gstLibrary.setSurfaceHolder(videoView.holder)
//gstLibrary.setSurfaceHolder(videoView.holder)
gstLibrary.setTextureView(videoView)
gstLibrary.setOnStatusChangeListener(this)
// View 預設狀態

View File

@ -7,7 +7,7 @@
android:background="@drawable/bg_video_view"
android:outlineProvider="background">
<SurfaceView
<TextureView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"

View File

@ -1,13 +1,16 @@
package com.hisharp.gstreamer_player
import android.content.Context
import android.graphics.SurfaceTexture
import android.util.Log
import android.view.Surface
import android.view.SurfaceHolder
import android.view.TextureView
import org.freedesktop.gstreamer.GStreamer
import java.io.Closeable
import java.io.IOException
class GstLibrary(context: Context) : Closeable, SurfaceHolder.Callback {
class GstLibrary(context: Context) : Closeable, SurfaceHolder.Callback, TextureView.SurfaceTextureListener {
private val mAppContext: Context
private var gstCallback: GstCallback? = null
@ -15,6 +18,8 @@ class GstLibrary(context: Context) : Closeable, SurfaceHolder.Callback {
private var tag = ""
private var isInit = false
private var surface: Surface? = null
private external fun nativeInit() // Initialize native code, build pipeline, etc
private external fun nativeFinalize() // Destroy pipeline and shutdown native code
private external fun nativeSetUri(uri: String) // Set the URI of the media to play
@ -59,6 +64,10 @@ class GstLibrary(context: Context) : Closeable, SurfaceHolder.Callback {
holder.addCallback(this)
}
fun setTextureView(textureView: TextureView) {
textureView.surfaceTextureListener = this
}
// Called from native code. This sets the content of the TextView from the UI thread.
private fun setMessage(message: String) {
if (gstCallback == null) return
@ -128,7 +137,8 @@ class GstLibrary(context: Context) : Closeable, SurfaceHolder.Callback {
override fun surfaceCreated(holder: SurfaceHolder) {
Log.d("$TAG+$tag", "Surface created: " + holder.surface)
nativeSurfaceInit(holder.surface)
this.surface = holder.surface
this.surface?.let { nativeSurfaceInit(it) }
}
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
@ -138,11 +148,33 @@ class GstLibrary(context: Context) : Closeable, SurfaceHolder.Callback {
override fun surfaceDestroyed(holder: SurfaceHolder) {
Log.d("$TAG+$tag", "Surface destroyed")
isInit = false
this.isInit = false
this.surface = null
//pause()
//releaseSurface()
}
override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
this.surface = Surface(surface)
this.surface?.let { nativeSurfaceInit(it) }
Log.d("$TAG+$tag", "Surface onSurfaceTextureAvailable: $surface")
}
override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {
Log.d("$TAG+$tag", "Surface onSurfaceTextureSizeChanged: $surface")
}
override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
Log.d("$TAG+$tag", "Surface onSurfaceTextureDestroyed: $surface")
this.isInit = false
this.surface = null
return isInit
}
override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {
Log.d("$TAG+$tag", "Surface onSurfaceTextureUpdated: $surface")
}
companion object {
private val TAG = GstLibrary::class.java.simpleName