令GstLibrary的callback可以針對多個實例作區分

This commit is contained in:
Barney 2022-08-05 14:12:23 +08:00
parent 80233d8b6e
commit 97cac9ba9a
9 changed files with 96 additions and 41 deletions

View File

@ -3,8 +3,10 @@
<component name="DesignSurface">
<option name="filePathToZoomLevelMap">
<map>
<entry key="..\:/Users/hisharp/Downloads/gstreamer-main/Gstreamer/app/src/main/res/drawable/bg_video_view.xml" value="0.151" />
<entry key="..\:/Users/hisharp/Downloads/gstreamer-main/Gstreamer/app/src/main/res/layout/activity_main.xml" value="0.25" />
<entry key="..\:/Users/hisharp/Downloads/gstreamer-main/Gstreamer/app/src/main/res/layout/fragment_grid_video.xml" value="0.14114583333333333" />
<entry key="..\:/Users/hisharp/Downloads/gstreamer-main/Gstreamer/app/src/main/res/layout/item_video_view.xml" value="0.14010416666666667" />
<entry key="..\:/Users/hisharp/Downloads/gstreamer-main/Gstreamer/gstreamer_player/res/layout/main.xml" value="0.16574074074074074" />
<entry key="..\:/Users/hisharp/Downloads/gstreamer-main/android/android-tutorial-5/res/layout/main.xml" value="0.1" />
<entry key="..\:/Users/hisharp/Downloads/gstreamer-main/android/android-tutorial-6/res/layout/confmanager.xml" value="0.1" />

View File

@ -9,7 +9,6 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.gridlayout.widget.GridLayout
import com.hisharp.gstreamer_player.GStreamerSurfaceView
import com.hisharp.gstreamer_player.GstCallback
import com.hisharp.gstreamer_player.GstLibrary
import com.hisharp.gstreamer_player.GstStatus
@ -30,7 +29,7 @@ class GridVideoFragment : Fragment(), GstCallback {
private var gstPlayers: ArrayList<GstLibrary?> = ArrayList()
private var videoViews: ArrayList<GStreamerSurfaceView> = ArrayList()
private var videoViews: ArrayList<VideoView> = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -102,7 +101,6 @@ class GridVideoFragment : Fragment(), GstCallback {
val displayMetrics = DisplayMetrics()
requireActivity().windowManager.defaultDisplay.getMetrics(displayMetrics)
val screenHeight = displayMetrics.heightPixels
val screenWidth = displayMetrics.widthPixels
val cellWidth = when (splitMode) {
@ -110,43 +108,21 @@ class GridVideoFragment : Fragment(), GstCallback {
else -> (screenWidth / maxRow) - maxRow.dp
}
val cellHeight = (cellWidth * (0.625)).toInt()
val cellHeight = (cellWidth * (0.62)).toInt()
Log.e(TAG, "cellWidth: $cellWidth, cellHeight: $cellHeight")
// 生成 VideoView 分割畫面
binding.apply {
//Log.d(TAG, "maxRow: $maxRow, maxCol: $maxCol")
baseView.rowCount = maxRow
baseView.columnCount = maxCol
/*baseView.post {
// 根據分割數量決定子項目的寬度
val cellWidth: Int
val cellHeight: Int
when (splitMode) {
1 -> {
cellWidth = (baseView.width / maxRow)
cellHeight = (baseView.height / maxCol)
}
else -> {
cellWidth = (baseView.width / maxRow) - maxRow.dp
cellHeight = (baseView.height / maxCol) - maxCol.dp
}
}
Log.e(TAG, "cellWidth: $cellWidth, cellHeight: $cellHeight")
videoViews.forEach { videoView ->
videoView.layoutParams?.width = cellWidth
videoView.layoutParams?.height = cellHeight
}
}*/
for (col in 0 until maxCol) {
for (row in 0 until maxRow) {
val videoView = GStreamerSurfaceView(requireContext()).apply {
val videoView = VideoView(requireContext()).apply {
layoutParams = GridLayout.LayoutParams().apply {
height = cellHeight
width = cellWidth
topMargin = 0.dp
bottomMargin = 0.dp
marginEnd = 0.dp
@ -185,7 +161,7 @@ class GridVideoFragment : Fragment(), GstCallback {
for (index in videoViews.indices) {
gstPlayers.add(GstLibrary(requireContext(), data[index]))
gstPlayers[index]?.setSurfaceView(videoViews[index])
gstPlayers[index]?.setSurfaceView(videoViews[index].videoView)
gstPlayers[index]?.setOnStatusChangeListener(this)
}
}
@ -208,15 +184,16 @@ class GridVideoFragment : Fragment(), GstCallback {
}
}
override fun onStatus(gstStatus: GstStatus?) {
override fun onStatus(gstInstance: GstLibrary, gstStatus: GstStatus?) {
//Log.d(TAG, GstStatus.values());
}
override fun onMessage(message: String?) {
Log.d(TAG, message!!)
override fun onMessage(gstInstance: GstLibrary, message: String?) {
val index = gstPlayers.indexOf(gstInstance)
Log.d("${TAG}_$mPageNum", "GstPlayer #$index: $message")
}
override fun onMediaSizeChanged(width: Int, height: Int) {}
override fun onMediaSizeChanged(gstInstance: GstLibrary, width: Int, height: Int) {}
companion object {
private val TAG = GridVideoFragment::class.java.simpleName

View File

@ -10,6 +10,7 @@ import com.hisharp.gstreamer_player.GStreamerSurfaceView
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import com.hisharp.gstreamer_player.GstStatus
import com.ray650128.gstreamer_demo_app.databinding.ActivityMainBinding
import java.util.ArrayList
@ -48,6 +49,7 @@ class MainActivity : AppCompatActivity() {
)
override fun onCreate(savedInstanceState: Bundle?) {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

View File

@ -0,0 +1,37 @@
package com.ray650128.gstreamer_demo_app
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import com.hisharp.gstreamer_player.GStreamerSurfaceView
import com.ray650128.gstreamer_demo_app.databinding.ItemVideoViewBinding
class VideoView : ConstraintLayout {
constructor(context: Context) : super(context) {
initView(context)
}
constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {
initView(context)
}
constructor(context: Context, attributeSet: AttributeSet, intRes: Int) : super(context, attributeSet, intRes) {
initView(context)
}
private lateinit var view: ItemVideoViewBinding
val videoView: GStreamerSurfaceView by lazy { view.videoView }
private fun initView(context: Context) {
val layoutInflater = LayoutInflater.from(context)
view = ItemVideoViewBinding.inflate(layoutInflater, this, true)
view.baseView.clipToOutline = true
}
companion object {
private val TAG = VideoView::class.java.simpleName
}
}

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp" />
<solid android:color="@android:color/black" />
</shape>

View File

@ -12,7 +12,7 @@
android:id="@+id/viewPager"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="w,1:1.6"
app:layout_constraintDimensionRatio="w,0.625:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/baseView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_video_view"
android:outlineProvider="background">
<com.hisharp.gstreamer_player.GStreamerSurfaceView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/pbLoading"
style="?android:attr/progressBarStyle"
android:layout_width="64dp"
android:layout_height="0dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="@+id/videoView"
app:layout_constraintDimensionRatio="h,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -1,7 +1,7 @@
package com.hisharp.gstreamer_player;
public interface GstCallback {
public void onStatus(GstStatus gstStatus);
public void onMessage(String message);
public void onMediaSizeChanged (int width, int height);
public void onStatus(GstLibrary gstInstance, GstStatus gstStatus);
public void onMessage(GstLibrary gstInstance, String message);
public void onMediaSizeChanged (GstLibrary gstInstance, int width, int height);
}

View File

@ -77,11 +77,11 @@ public class GstLibrary implements SurfaceHolder.Callback {
private void setMessage(final String message) {
if (gstCallback == null) return;
if (message.contains("State changed to PAUSED")) {
gstCallback.onStatus(GstStatus.PAUSE);
gstCallback.onStatus(this, GstStatus.PAUSE);
} else if (message.contains("State changed to PLAYING")) {
gstCallback.onStatus(GstStatus.PLAYING);
gstCallback.onStatus(this, GstStatus.PLAYING);
}
gstCallback.onMessage(message);
gstCallback.onMessage(this, message);
}
// Called from native code. Native code calls this once it has created its pipeline and
@ -90,7 +90,7 @@ public class GstLibrary implements SurfaceHolder.Callback {
Log.i ("GStreamer", "GStreamer initialized:");
if (gstCallback != null) {
gstCallback.onStatus(GstStatus.READY);
gstCallback.onStatus(this, GstStatus.READY);
}
// Restore previous playing state