From 876ab519e9007a3ef2d24a63f94a11ac463201c5 Mon Sep 17 00:00:00 2001 From: Daniele Date: Tue, 12 Oct 2021 16:12:48 +0200 Subject: [PATCH] Working single stream in fixed-size tile --- .idea/.name | 1 + .idea/compiler.xml | 2 +- .idea/misc.xml | 3 +- .../danieleverducci/ojo/entities/Camera.java | 13 +++ .../ojo/ui/SurveillanceFragment.java | 105 +++++++++++++----- 5 files changed, 94 insertions(+), 30 deletions(-) create mode 100644 .idea/.name diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..b6e5b6a --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Ojo \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 61a9130..fb7f4a8 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 827c06e..10e58bc 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -7,7 +7,8 @@ - + + diff --git a/app/src/main/java/it/danieleverducci/ojo/entities/Camera.java b/app/src/main/java/it/danieleverducci/ojo/entities/Camera.java index d635ad4..3f9e8c9 100644 --- a/app/src/main/java/it/danieleverducci/ojo/entities/Camera.java +++ b/app/src/main/java/it/danieleverducci/ojo/entities/Camera.java @@ -3,4 +3,17 @@ package it.danieleverducci.ojo.entities; public class Camera { private String name; private String rtspUrl; + + public Camera(String name, String rtspUrl) { + this.name = name; + this.rtspUrl = rtspUrl; + } + + public String getName() { + return name; + } + + public String getRtspUrl() { + return rtspUrl; + } } diff --git a/app/src/main/java/it/danieleverducci/ojo/ui/SurveillanceFragment.java b/app/src/main/java/it/danieleverducci/ojo/ui/SurveillanceFragment.java index 085ab73..fc232cc 100644 --- a/app/src/main/java/it/danieleverducci/ojo/ui/SurveillanceFragment.java +++ b/app/src/main/java/it/danieleverducci/ojo/ui/SurveillanceFragment.java @@ -1,13 +1,18 @@ package it.danieleverducci.ojo.ui; +import android.content.Context; +import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.util.DisplayMetrics; +import android.util.Log; import android.view.LayoutInflater; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.ViewGroup; +import android.view.ViewTreeObserver; +import android.widget.LinearLayout; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; @@ -20,12 +25,16 @@ import org.videolan.libvlc.MediaPlayer; import java.util.ArrayList; +import it.danieleverducci.ojo.R; import it.danieleverducci.ojo.databinding.FragmentSurveillanceBinding; +import it.danieleverducci.ojo.entities.Camera; public class SurveillanceFragment extends Fragment implements MediaPlayer.EventListener, IVLCVout.Callback { private FragmentSurveillanceBinding binding; + private CameraView cameraView; + @Override public View onCreateView( LayoutInflater inflater, ViewGroup container, @@ -49,22 +58,9 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL // }); // Start stream on surface view 1 - SurfaceView mSurface = binding.surfaceView1; - SurfaceHolder holder = mSurface.getHolder(); - - // Obtain surfaceview size - DisplayMetrics displayMetrics = new DisplayMetrics(); - getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); - - ViewGroup.LayoutParams videoParams = mSurface.getLayoutParams(); - videoParams.width = displayMetrics.widthPixels; - videoParams.height = displayMetrics.heightPixels; - - - // media player - MediaPlayer mMediaPlayer = null; String rtspUrl = "rtsp://admin:Du4gdtPmXGCWT29@192.168.1.200:554/Streaming/Channels/101"; + Camera camera = new Camera("Camera 1", rtspUrl); ArrayList options = new ArrayList(); options.add("--aout=opensles"); @@ -75,28 +71,30 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL options.add("--file-logging"); options.add("--logfile=vlc-log.txt"); - LibVLC libvlc = new LibVLC(getContext(), options); - holder.setKeepScreenOn(true); - // Create media player - mMediaPlayer = new MediaPlayer(libvlc); - mMediaPlayer.setEventListener(this); + cameraView = new CameraView( + getContext(), + libvlc, + camera, + this + ); - // Set up video output - final IVLCVout vout = mMediaPlayer.getVLCVout(); - vout.setVideoView(mSurface); - vout.setWindowSize(videoParams.width,videoParams.height); - vout.addCallback(this); - vout.attachViews(); + // Add to layout + LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(1000, 500); + binding.gridRowContainer.addView(cameraView.surfaceView, params); - Media m = new Media(libvlc, Uri.parse(rtspUrl)); - mMediaPlayer.setMedia(m); - mMediaPlayer.play(); } + @Override + public void onStart() { + super.onStart(); + + cameraView.startPlayback(); + } + @Override public void onDestroyView() { super.onDestroyView(); @@ -124,4 +122,55 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL public void onSurfacesDestroyed(IVLCVout vlcVout) { } + + private void addCameraView(Camera camera) { + + } + + private class CameraView { + protected SurfaceView surfaceView; + protected MediaPlayer mediaPlayer; + protected IVLCVout ivlcVout; + protected Camera camera; + protected LibVLC libvlc; + + public CameraView(Context context, LibVLC libvlc, Camera camera, IVLCVout.Callback callback) { + this.camera = camera; + this.libvlc = libvlc; + + surfaceView = new SurfaceView(context); + SurfaceHolder holder = surfaceView.getHolder(); + + holder.setKeepScreenOn(true); + + // Create media player + mediaPlayer = new MediaPlayer(libvlc); +// mediaPlayer.setEventListener(this); + + // Set up video output + ivlcVout = mediaPlayer.getVLCVout(); + ivlcVout.setVideoView(surfaceView); + ivlcVout.addCallback(callback); + ivlcVout.attachViews(); + + // Load media and start playing + Media m = new Media(libvlc, Uri.parse(camera.getRtspUrl())); + mediaPlayer.setMedia(m); + + // Register for view resize events + final ViewTreeObserver observer= surfaceView.getViewTreeObserver(); + observer.addOnGlobalLayoutListener(() -> { + // Set rendering size + ivlcVout.setWindowSize(surfaceView.getWidth(), surfaceView.getHeight()); + }); + } + + /** + * Starts the playback. + * This must be called after the view has been laid out + */ + public void startPlayback() { + mediaPlayer.play(); + } + } } \ No newline at end of file