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.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
@ -38,8 +39,9 @@ import it.danieleverducci.ojo.utils.DpiUtils;
* 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
*/
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[]{
"--aout=opensles",
//"--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
public void onStart() {
super.onStart();
public void onResume() {
super.onResume();
// Leanback mode (fullscreen)
Window window = getActivity().getWindow();
@ -124,6 +88,8 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL
}
}
addAllCameras();
// Start playback for all streams
for (CameraView cv : cameraViews) {
cv.startPlayback();
@ -134,53 +100,61 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL
public void onPause() {
super.onPause();
for (CameraView cv : cameraViews) {
cv.pausePlayback();
disposeAllCameras();
}
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
public void onDestroy() {
super.onDestroy();
private void disposeAllCameras() {
// Destroy players, libs etc
for (CameraView cv : cameraViews) {
cv.destroy();
}
cameraViews.clear();
// Remove views
binding.gridRowContainer.removeAllViews();
}
@Override
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) {
private CameraView addCameraView(Camera camera, LinearLayout rowContainer) {
CameraView cv = new CameraView(
getContext(),
camera,
this
camera
);
// Add to layout
@ -194,6 +168,7 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL
rowContainer.addView(cv.surfaceView, params);
cameraViews.add(cv);
return cv;
}
/**
@ -215,11 +190,9 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL
protected IVLCVout ivlcVout;
protected Camera camera;
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.callback = callback;
this.libvlc = new LibVLC(context, new ArrayList<>(Arrays.asList(VLC_OPTIONS)));
surfaceView = new SurfaceView(context);
@ -229,12 +202,10 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL
// Create media player
mediaPlayer = new MediaPlayer(libvlc);
// mediaPlayer.setEventListener(this);
// Set up video output
ivlcVout = mediaPlayer.getVLCVout();
ivlcVout.setVideoView(surfaceView);
ivlcVout.addCallback(this.callback);
ivlcVout.attachViews();
// Load media and start playing
@ -256,23 +227,22 @@ public class SurveillanceFragment extends Fragment implements MediaPlayer.EventL
mediaPlayer.play();
}
/**
* Pauses the playback.
*/
public void pausePlayback() {
mediaPlayer.pause();
}
/**
* Destroys the object and frees the memory
*/
public void destroy() {
if (libvlc == null) {
Log.e(TAG, this.toString() + " already destroyed");
return;
}
mediaPlayer.stop();
final IVLCVout vout = mediaPlayer.getVLCVout();
vout.removeCallback(this.callback);
vout.detachViews();
libvlc.release();
libvlc = null;
mediaPlayer.release();
mediaPlayer = null;
}
}
}