diff --git a/.idea/compiler.xml b/.idea/compiler.xml
index 659bf43..fb7f4a8 100644
--- a/.idea/compiler.xml
+++ b/.idea/compiler.xml
@@ -1,6 +1,6 @@
-
+
\ No newline at end of file
diff --git a/.idea/gradle.xml b/.idea/gradle.xml
index 823f760..f3e97d6 100644
--- a/.idea/gradle.xml
+++ b/.idea/gradle.xml
@@ -7,7 +7,7 @@
-
+
-
+
\ No newline at end of file
diff --git a/app/build.gradle b/app/build.gradle
index c3294ad..5b37e5f 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -4,12 +4,12 @@ plugins {
apply plugin: 'kotlin-android'
android {
- compileSdk 32
+ compileSdk 33
defaultConfig {
applicationId "com.ray650128.gstreamer_demo_app"
minSdk 26
- targetSdk 32
+ targetSdk 33
versionCode 1
versionName "1.0"
diff --git a/app/src/main/java/com/ray650128/gstreamer_demo_app/GridVideoFragment.kt b/app/src/main/java/com/ray650128/gstreamer_demo_app/GridVideoFragment.kt
index b3e5b00..87ed4a1 100644
--- a/app/src/main/java/com/ray650128/gstreamer_demo_app/GridVideoFragment.kt
+++ b/app/src/main/java/com/ray650128/gstreamer_demo_app/GridVideoFragment.kt
@@ -4,6 +4,7 @@ import android.os.Bundle
import android.util.DisplayMetrics
import android.util.Log
import android.view.LayoutInflater
+import android.view.SurfaceHolder
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
@@ -15,7 +16,7 @@ import com.ray650128.gstreamer_demo_app.databinding.FragmentGridVideoBinding
import kotlin.math.sqrt
-class GridVideoFragment : Fragment(), GstCallback {
+class GridVideoFragment : Fragment(), GstCallback, SurfaceHolder.Callback {
private var mPageNum: Int = 0
private var splitMode = 0
@@ -26,10 +27,12 @@ class GridVideoFragment : Fragment(), GstCallback {
private lateinit var binding: FragmentGridVideoBinding
- private var gstPlayers: ArrayList = ArrayList()
+ private var gstPlayers: ArrayList = ArrayList()
private var videoViews: ArrayList = ArrayList()
+ private var surfaceHolders: ArrayList = ArrayList()
+
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -76,15 +79,14 @@ class GridVideoFragment : Fragment(), GstCallback {
}
override fun onDestroyView() {
+ for (i in gstPlayers.indices) {
+ gstPlayers[i].close()
+ }
Log.d("${TAG}_$mPageNum", "onDestroyView()")
super.onDestroyView()
}
override fun onDestroy() {
- for (i in gstPlayers.indices) {
- gstPlayers[i]?.release()
- gstPlayers[i] = null
- }
Log.d("${TAG}_$mPageNum", "onDestroy()")
super.onDestroy()
}
@@ -112,63 +114,66 @@ class GridVideoFragment : Fragment(), GstCallback {
for (col in 0 until maxCol) {
for (row in 0 until maxRow) {
- val videoView = VideoView(requireContext()).apply {
- layoutParams = GridLayout.LayoutParams().apply {
- height = cellHeight
- width = cellWidth
+ val videoView = VideoView(requireContext())
+ val layoutParams = GridLayout.LayoutParams().apply {
+ height = cellHeight
+ width = cellWidth
- topMargin = 0.dp
- bottomMargin = 0.dp
- marginEnd = 0.dp
- marginStart = 0.dp
+ topMargin = 0.dp
+ bottomMargin = 0.dp
+ marginEnd = 0.dp
+ marginStart = 0.dp
- // 調整間距
- when (splitMode) {
- 4 -> {
- when (col) {
- 0 -> bottomMargin = 2.dp
- 1 -> topMargin = 2.dp
- }
- when (row) {
- 0 -> marginEnd = 2.dp
- 1 -> marginStart = 2.dp
- }
+ // 調整間距
+ when (splitMode) {
+ 4 -> {
+ when (col) {
+ 0 -> bottomMargin = 2.dp
+ 1 -> topMargin = 2.dp
}
- 9 -> {
- if (col == 1) {
- topMargin = 4.dp
- bottomMargin = 4.dp
- }
- if (row == 1) {
- marginEnd = 4.dp
- marginStart = 4.dp
- }
+ when (row) {
+ 0 -> marginEnd = 2.dp
+ 1 -> marginStart = 2.dp
+ }
+ }
+ 9 -> {
+ if (col == 1) {
+ topMargin = 4.dp
+ bottomMargin = 4.dp
+ }
+ if (row == 1) {
+ marginEnd = 4.dp
+ marginStart = 4.dp
}
}
}
}
- baseView.addView(videoView)
+
+ baseView.addView(videoView, layoutParams)
videoViews.add(videoView)
+
+ videoView.videoView.holder.addCallback(this@GridVideoFragment)
+ surfaceHolders.add(videoView.videoView.holder)
}
}
}
for (index in videoViews.indices) {
gstPlayers.add(GstLibrary(requireContext(), data[index]))
- gstPlayers[index]?.setSurfaceView(videoViews[index].videoView)
- gstPlayers[index]?.setOnStatusChangeListener(this)
+ gstPlayers[index].setOnStatusChangeListener(this)
}
}
private fun playAll() {
for (index in data.indices) {
- gstPlayers[index]?.play()
+ gstPlayers[index].play()
}
}
private fun stopAll() {
for (index in data.indices) {
- gstPlayers[index]?.stop()
+ gstPlayers[index].stop()
+ gstPlayers[index].close()
}
}
@@ -189,6 +194,23 @@ class GridVideoFragment : Fragment(), GstCallback {
Log.d("${TAG}_$mPageNum", "GstPlayer #$index: $message")
}
+ override fun surfaceCreated(holder: SurfaceHolder) {
+ val index = surfaceHolders.indexOf(holder)
+ gstPlayers[index].setSurfaceHolder(holder)
+ Log.d("GStreamer", "Surface created: " + holder.surface)
+ }
+
+ override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
+ //val index = surfaceHolders.indexOf(holder)
+ Log.d("GStreamer", "Surface changed to format $format width $width height $height")
+ }
+
+ override fun surfaceDestroyed(holder: SurfaceHolder) {
+ val index = surfaceHolders.indexOf(holder)
+ gstPlayers[index].releaseSurface()
+ Log.d("GStreamer", "Surface destroyed")
+ }
+
companion object {
private val TAG = GridVideoFragment::class.java.simpleName
private const val ARG_PAGE_NUM = "page_number"
diff --git a/app/src/main/java/com/ray650128/gstreamer_demo_app/MainActivity.kt b/app/src/main/java/com/ray650128/gstreamer_demo_app/MainActivity.kt
index d2df805..a70960c 100644
--- a/app/src/main/java/com/ray650128/gstreamer_demo_app/MainActivity.kt
+++ b/app/src/main/java/com/ray650128/gstreamer_demo_app/MainActivity.kt
@@ -22,20 +22,31 @@ class MainActivity : AppCompatActivity() {
private lateinit var splitVideoViewAdapter: ViewPager2Adapter
- /*@SuppressLint("AuthLeak")
+ @SuppressLint("AuthLeak")
private val defaultMediaUris = arrayListOf(
arrayListOf(
+ "rtsp://admin:admin@192.168.0.77:554/media/video2",
+ "rtsp://admin:1q2w3e4r!@192.168.0.79:554/media/video2",
+ "rtsp://admin:1q2w3e4r~@211.23.78.226:8588/media/video2",
"rtsp://admin:admin@211.23.78.226:8574/v02",
"rtsp://admin:admin@211.23.78.226:8575/v02",
"rtsp://admin:admin@192.168.0.77:554/media/video2",
- "rtsp://admin:123456@192.168.0.80:554/profile2",
- "rtsp://admin:123456@192.168.0.83:554/profile2",
- "rtsp://admin:123456@192.168.0.84:554/profile2",
- "rtsp://admin:admin@192.168.0.86:554/v2",
- "rtsp://admin:admin@192.168.0.89:554/v02",
- "rtsp://admin:1q2w3e4r!@60.249.32.50:554/unicast/c8/s1/live",
+ "rtsp://admin:1q2w3e4r!@192.168.0.79:554/media/video2",
+ "rtsp://admin:1q2w3e4r~@211.23.78.226:8588/media/video2",
+ "rtsp://admin:admin@211.23.78.226:8574/v02",
),
arrayListOf(
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ ),
+ /*arrayListOf(
"rtsp://admin:1q2w3e4r!@60.249.32.50:554/unicast/c1/s1/live",
"rtsp://admin:1q2w3e4r!@60.249.32.50:554/unicast/c2/s1/live",
"rtsp://admin:1q2w3e4r!@60.249.32.50:554/unicast/c3/s1/live",
@@ -45,7 +56,7 @@ class MainActivity : AppCompatActivity() {
"rtsp://admin:1q2w3e4r!@60.249.32.50:554/unicast/c7/s1/live",
"rtsp://admin:1q2w3e4r!@60.249.32.50:554/unicast/c8/s1/live",
"rtsp://admin:1q2w3e4r!@60.249.32.50:554/unicast/c9/s1/live"
- ),
+ ),*/
arrayListOf(
"",
"",
@@ -57,9 +68,9 @@ class MainActivity : AppCompatActivity() {
"",
"",
)
- )*/
+ )
- @SuppressLint("AuthLeak")
+ /*@SuppressLint("AuthLeak")
private val defaultMediaUris = arrayListOf(
arrayListOf(
"rtsp://admin:admin@211.23.78.226:8574/v02"
@@ -69,8 +80,11 @@ class MainActivity : AppCompatActivity() {
),
arrayListOf(
"rtsp://admin:admin@192.168.0.77:554/media/video2"
+ ),
+ arrayListOf(
+ "http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_60fps_normal.mp4"
)
- )
+ )*/
override fun onCreate(savedInstanceState: Bundle?) {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
diff --git a/gstreamer_player/build.gradle b/gstreamer_player/build.gradle
index f170c34..7634486 100644
--- a/gstreamer_player/build.gradle
+++ b/gstreamer_player/build.gradle
@@ -1,11 +1,11 @@
apply plugin: 'com.android.library'
android {
- compileSdkVersion 32
+ compileSdkVersion 33
defaultConfig {
minSdkVersion 15
- targetSdkVersion 32
+ targetSdkVersion 33
versionCode 1
versionName "1.0"
diff --git a/gstreamer_player/src/com/hisharp/gstreamer_player/GstLibrary.java b/gstreamer_player/src/com/hisharp/gstreamer_player/GstLibrary.java
index 76e33a3..de28c34 100644
--- a/gstreamer_player/src/com/hisharp/gstreamer_player/GstLibrary.java
+++ b/gstreamer_player/src/com/hisharp/gstreamer_player/GstLibrary.java
@@ -10,16 +10,15 @@ import android.view.SurfaceView;
import org.freedesktop.gstreamer.GStreamer;
-public class GstLibrary implements SurfaceHolder.Callback {
+import java.io.Closeable;
+import java.io.IOException;
+
+public class GstLibrary implements Closeable {
final Context mAppContext;
private GstCallback gstCallback;
- private GStreamerSurfaceView surfaceView;
-
- private final Handler mainHandler = new Handler(Looper.getMainLooper());
-
private final String rtspUrl;
public GstLibrary(Context context, String rtspUrl) {
@@ -61,18 +60,11 @@ public class GstLibrary implements SurfaceHolder.Callback {
nativeSurfaceFinalize();
}
- public void release() {
- nativeFinalize();
- }
-
public void setOnStatusChangeListener(GstCallback callback) {
this.gstCallback = callback;
}
- public void setSurfaceView(GStreamerSurfaceView surfaceView) {
- this.surfaceView = surfaceView;
- SurfaceHolder holder = this.surfaceView.getHolder();
- holder.addCallback(this);
+ public void setSurfaceHolder(SurfaceHolder holder) {
nativeSurfaceInit(holder.getSurface());
}
@@ -110,11 +102,11 @@ public class GstLibrary implements SurfaceHolder.Callback {
// Inform the video surface about the new size and recalculate the layout.
private void onMediaSizeChanged (int width, int height) {
Log.i ("GStreamer", "Media size changed to " + width + "x" + height);
- mainHandler.post(() -> {
+ /*mainHandler.post(() -> {
surfaceView.media_width = width;
surfaceView.media_height = height;
surfaceView.requestLayout();
- });
+ });*/
}
static {
@@ -123,18 +115,12 @@ public class GstLibrary implements SurfaceHolder.Callback {
nativeClassInit();
}
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
- Log.d("GStreamer", "Surface changed to format " + format + " width " + width + " height " + height);
- }
-
- public void surfaceCreated(SurfaceHolder holder) {
- Log.d("GStreamer", "Surface created: " + holder.getSurface());
- nativeSurfaceInit (holder.getSurface());
- }
-
- public void surfaceDestroyed(SurfaceHolder holder) {
- Log.d("GStreamer", "Surface destroyed");
- nativePause();
- nativeSurfaceFinalize();
+ @Override
+ public void close() throws IOException {
+ try {
+ nativeFinalize();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
}
}