Almost working suspend

This commit is contained in:
Daniele 2021-10-13 15:06:19 +02:00
parent a99da8fc4d
commit 2d33b8d246

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
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;
@ -38,8 +39,9 @@ import it.danieleverducci.ojo.utils.DpiUtils;
* rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov * rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov
* rtsp://demo:demo@ipvmdemo.dyndns.org:5541/onvif-media/media.amp?profile=profile_1_h264&sessiontimeout=60&streamtype=unicast * rtsp://demo:demo@ipvmdemo.dyndns.org:5541/onvif-media/media.amp?profile=profile_1_h264&sessiontimeout=60&streamtype=unicast
*/ */
public class SurveillanceFragment extends Fragment implements MediaPlayer.EventListener, IVLCVout.Callback { public class SurveillanceFragment extends Fragment {
final static private String TAG = "SurveillanceFragment";
final static private String[] VLC_OPTIONS = new String[]{ final static private String[] VLC_OPTIONS = new String[]{
"--aout=opensles", "--aout=opensles",
//"--audio-time-stretch", // time stretching //"--audio-time-stretch", // time stretching
@ -63,47 +65,9 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL
} }
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Settings settings = Settings.fromDisk(getContext());
List<Camera> cc = settings.getCameras();
int elemsPerSide = calcGridSideElements(cc.size());
int camIdx = 0;
for (int r = 0; r < elemsPerSide; r++) {
// Create row and add to row container
LinearLayout row = new LinearLayout(getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
0,
1.0f
);
binding.gridRowContainer.addView(row, params);
// Add camera viewers to the row
for (int c = 0; c < elemsPerSide; c++) {
if ( camIdx < cc.size() ) {
Camera cam = cc.get(camIdx);
addCameraView(cam, row);
} else {
// Cameras are less than the maximum number of cells in grid: fill remaining cells with empty views
View ev = new View(getContext());
ev.setBackgroundColor(getResources().getColor(R.color.purple_500));
LinearLayout.LayoutParams evParams = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT,
1.0f
);
row.addView(ev, evParams);
}
camIdx++;
}
}
}
@Override @Override
public void onStart() { public void onResume() {
super.onStart(); super.onResume();
// Leanback mode (fullscreen) // Leanback mode (fullscreen)
Window window = getActivity().getWindow(); Window window = getActivity().getWindow();
@ -124,6 +88,8 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL
} }
} }
addAllCameras();
// Start playback for all streams // Start playback for all streams
for (CameraView cv : cameraViews) { for (CameraView cv : cameraViews) {
cv.startPlayback(); cv.startPlayback();
@ -134,53 +100,61 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
for (CameraView cv : cameraViews) { disposeAllCameras();
cv.pausePlayback(); }
private void addAllCameras() {
Settings settings = Settings.fromDisk(getContext());
List<Camera> cc = settings.getCameras();
int elemsPerSide = calcGridSideElements(cc.size());
int camIdx = 0;
for (int r = 0; r < elemsPerSide; r++) {
// Create row and add to row container
LinearLayout row = new LinearLayout(getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
0,
1.0f
);
binding.gridRowContainer.addView(row, params);
// Add camera viewers to the row
for (int c = 0; c < elemsPerSide; c++) {
if ( camIdx < cc.size() ) {
Camera cam = cc.get(camIdx);
CameraView cv = addCameraView(cam, row);
cv.startPlayback();
} else {
// Cameras are less than the maximum number of cells in grid: fill remaining cells with empty views
View ev = new View(getContext());
ev.setBackgroundColor(getResources().getColor(R.color.purple_500));
LinearLayout.LayoutParams evParams = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT,
1.0f
);
row.addView(ev, evParams);
}
camIdx++;
}
} }
} }
@Override private void disposeAllCameras() {
public void onDestroy() { // Destroy players, libs etc
super.onDestroy();
for (CameraView cv : cameraViews) { for (CameraView cv : cameraViews) {
cv.destroy(); cv.destroy();
} }
cameraViews.clear();
// Remove views
binding.gridRowContainer.removeAllViews();
} }
@Override private CameraView addCameraView(Camera camera, LinearLayout rowContainer) {
public void onDestroyView() {
super.onDestroyView();
binding = null;
// TODO Release player
}
/**
* VLC media listener
*/
@Override
public void onEvent(MediaPlayer.Event event) {
//TODO
}
/**
* Video output callbacks
*/
@Override
public void onSurfacesCreated(IVLCVout vlcVout) {
}
@Override
public void onSurfacesDestroyed(IVLCVout vlcVout) {
}
private void addCameraView(Camera camera, LinearLayout rowContainer) {
CameraView cv = new CameraView( CameraView cv = new CameraView(
getContext(), getContext(),
camera, camera
this
); );
// Add to layout // Add to layout
@ -194,6 +168,7 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL
rowContainer.addView(cv.surfaceView, params); rowContainer.addView(cv.surfaceView, params);
cameraViews.add(cv); cameraViews.add(cv);
return cv;
} }
/** /**
@ -215,11 +190,9 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL
protected IVLCVout ivlcVout; protected IVLCVout ivlcVout;
protected Camera camera; protected Camera camera;
protected LibVLC libvlc; protected LibVLC libvlc;
protected IVLCVout.Callback callback;
public CameraView(Context context, Camera camera, IVLCVout.Callback callback) { public CameraView(Context context, Camera camera) {
this.camera = camera; this.camera = camera;
this.callback = callback;
this.libvlc = new LibVLC(context, new ArrayList<>(Arrays.asList(VLC_OPTIONS))); this.libvlc = new LibVLC(context, new ArrayList<>(Arrays.asList(VLC_OPTIONS)));
surfaceView = new SurfaceView(context); surfaceView = new SurfaceView(context);
@ -229,12 +202,10 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL
// Create media player // Create media player
mediaPlayer = new MediaPlayer(libvlc); mediaPlayer = new MediaPlayer(libvlc);
// mediaPlayer.setEventListener(this);
// Set up video output // Set up video output
ivlcVout = mediaPlayer.getVLCVout(); ivlcVout = mediaPlayer.getVLCVout();
ivlcVout.setVideoView(surfaceView); ivlcVout.setVideoView(surfaceView);
ivlcVout.addCallback(this.callback);
ivlcVout.attachViews(); ivlcVout.attachViews();
// Load media and start playing // Load media and start playing
@ -256,23 +227,22 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL
mediaPlayer.play(); mediaPlayer.play();
} }
/**
* Pauses the playback.
*/
public void pausePlayback() {
mediaPlayer.pause();
}
/** /**
* Destroys the object and frees the memory * Destroys the object and frees the memory
*/ */
public void destroy() { public void destroy() {
if (libvlc == null) {
Log.e(TAG, this.toString() + " already destroyed");
return;
}
mediaPlayer.stop(); mediaPlayer.stop();
final IVLCVout vout = mediaPlayer.getVLCVout(); final IVLCVout vout = mediaPlayer.getVLCVout();
vout.removeCallback(this.callback);
vout.detachViews(); vout.detachViews();
libvlc.release(); libvlc.release();
libvlc = null; libvlc = null;
mediaPlayer.release();
mediaPlayer = null;
} }
} }
} }