Working single stream in fixed-size tile

This commit is contained in:
Daniele 2021-10-12 16:12:48 +02:00
parent 59a7fed77a
commit 876ab519e9
5 changed files with 94 additions and 30 deletions

1
.idea/.name Normal file
View File

@ -0,0 +1 @@
Ojo

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="CompilerConfiguration"> <component name="CompilerConfiguration">
<bytecodeTargetLevel target="1.8" /> <bytecodeTargetLevel target="11" />
</component> </component>
</project> </project>

View File

@ -7,7 +7,8 @@
</map> </map>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

View File

@ -3,4 +3,17 @@ package it.danieleverducci.ojo.entities;
public class Camera { public class Camera {
private String name; private String name;
private String rtspUrl; 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;
}
} }

View File

@ -1,13 +1,18 @@
package it.danieleverducci.ojo.ui; package it.danieleverducci.ojo.ui;
import android.content.Context;
import android.graphics.Color;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
import android.view.SurfaceView; import android.view.SurfaceView;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
@ -20,12 +25,16 @@ import org.videolan.libvlc.MediaPlayer;
import java.util.ArrayList; import java.util.ArrayList;
import it.danieleverducci.ojo.R;
import it.danieleverducci.ojo.databinding.FragmentSurveillanceBinding; import it.danieleverducci.ojo.databinding.FragmentSurveillanceBinding;
import it.danieleverducci.ojo.entities.Camera;
public class SurveillanceFragment extends Fragment implements MediaPlayer.EventListener, IVLCVout.Callback { public class SurveillanceFragment extends Fragment implements MediaPlayer.EventListener, IVLCVout.Callback {
private FragmentSurveillanceBinding binding; private FragmentSurveillanceBinding binding;
private CameraView cameraView;
@Override @Override
public View onCreateView( public View onCreateView(
LayoutInflater inflater, ViewGroup container, LayoutInflater inflater, ViewGroup container,
@ -49,22 +58,9 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL
// }); // });
// Start stream on surface view 1 // 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"; String rtspUrl = "rtsp://admin:Du4gdtPmXGCWT29@192.168.1.200:554/Streaming/Channels/101";
Camera camera = new Camera("Camera 1", rtspUrl);
ArrayList<String> options = new ArrayList<String>(); ArrayList<String> options = new ArrayList<String>();
options.add("--aout=opensles"); options.add("--aout=opensles");
@ -75,28 +71,30 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL
options.add("--file-logging"); options.add("--file-logging");
options.add("--logfile=vlc-log.txt"); options.add("--logfile=vlc-log.txt");
LibVLC libvlc = new LibVLC(getContext(), options); LibVLC libvlc = new LibVLC(getContext(), options);
holder.setKeepScreenOn(true);
// Create media player cameraView = new CameraView(
mMediaPlayer = new MediaPlayer(libvlc); getContext(),
mMediaPlayer.setEventListener(this); libvlc,
camera,
this
);
// Set up video output // Add to layout
final IVLCVout vout = mMediaPlayer.getVLCVout(); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(1000, 500);
vout.setVideoView(mSurface); binding.gridRowContainer.addView(cameraView.surfaceView, params);
vout.setWindowSize(videoParams.width,videoParams.height);
vout.addCallback(this);
vout.attachViews();
Media m = new Media(libvlc, Uri.parse(rtspUrl));
mMediaPlayer.setMedia(m);
mMediaPlayer.play();
} }
@Override
public void onStart() {
super.onStart();
cameraView.startPlayback();
}
@Override @Override
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();
@ -124,4 +122,55 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL
public void onSurfacesDestroyed(IVLCVout vlcVout) { 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();
}
}
} }