Implement Pause and Resume functionality

camera2
Zohaad 6 years ago committed by Zohaad Afzal
parent 189aea48b0
commit 417c99c7f5

@ -37,6 +37,7 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
private Button btnSwitchCamera; private Button btnSwitchCamera;
private Button btnRecord; private Button btnRecord;
private Button btnSwitchEncoder; private Button btnSwitchEncoder;
private Button btnPause;
private SharedPreferences sp; private SharedPreferences sp;
private String rtmpUrl = "rtmp://ossrs.net/" + getRandomAlphaString(3) + '/' + getRandomAlphaDigitString(5); private String rtmpUrl = "rtmp://ossrs.net/" + getRandomAlphaString(3) + '/' + getRandomAlphaDigitString(5);
@ -66,6 +67,8 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
btnSwitchCamera = (Button) findViewById(R.id.swCam); btnSwitchCamera = (Button) findViewById(R.id.swCam);
btnRecord = (Button) findViewById(R.id.record); btnRecord = (Button) findViewById(R.id.record);
btnSwitchEncoder = (Button) findViewById(R.id.swEnc); btnSwitchEncoder = (Button) findViewById(R.id.swEnc);
btnPause = (Button) findViewById(R.id.pause);
btnPause.setEnabled(false);
mPublisher = new SrsPublisher((SrsCameraView) findViewById(R.id.glsurfaceview_camera)); mPublisher = new SrsPublisher((SrsCameraView) findViewById(R.id.glsurfaceview_camera));
mPublisher.setEncodeHandler(new SrsEncodeHandler(this)); mPublisher.setEncodeHandler(new SrsEncodeHandler(this));
@ -95,12 +98,26 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
} }
btnPublish.setText("stop"); btnPublish.setText("stop");
btnSwitchEncoder.setEnabled(false); btnSwitchEncoder.setEnabled(false);
btnPause.setEnabled(true);
} else if (btnPublish.getText().toString().contentEquals("stop")) { } else if (btnPublish.getText().toString().contentEquals("stop")) {
mPublisher.stopPublish(); mPublisher.stopPublish();
mPublisher.stopRecord(); mPublisher.stopRecord();
btnPublish.setText("publish"); btnPublish.setText("publish");
btnRecord.setText("record"); btnRecord.setText("record");
btnSwitchEncoder.setEnabled(true); btnSwitchEncoder.setEnabled(true);
btnPause.setEnabled(false);
}
}
});
btnPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(btnPause.getText().toString().equals("Pause")){
mPublisher.pausePublish();
btnPause.setText("resume");
}else{
mPublisher.resumePublish();
btnPause.setText("Pause");
} }
} }
}); });

@ -54,6 +54,7 @@ public class SrsEncoder {
private boolean canSoftEncode = false; private boolean canSoftEncode = false;
private long mPresentTimeUs; private long mPresentTimeUs;
private long mPausetime;
private int mVideoColorFormat; private int mVideoColorFormat;
@ -170,6 +171,14 @@ public class SrsEncoder {
return true; return true;
} }
public void pause(){
mPausetime = System.nanoTime() / 1000;
}
public void resume(){
long resumeTime = (System.nanoTime() / 1000) - mPausetime;
mPresentTimeUs = mPresentTimeUs + resumeTime;
mPausetime = 0;
}
public void stop() { public void stop() {
if (useSoftEncoder) { if (useSoftEncoder) {
closeSoftEncoder(); closeSoftEncoder();

@ -159,6 +159,15 @@ public class SrsPublisher {
stopCamera(); stopCamera();
mEncoder.stop(); mEncoder.stop();
} }
public void pauseEncode(){
stopAudio();
mCameraView.disableEncoding();
mCameraView.stopTorch();
}
private void resumeEncode() {
startAudio();
mCameraView.enableEncoding();
}
public void startPublish(String rtmpUrl) { public void startPublish(String rtmpUrl) {
if (mFlvMuxer != null) { if (mFlvMuxer != null) {
@ -167,6 +176,12 @@ public class SrsPublisher {
startEncode(); startEncode();
} }
} }
public void resumePublish(){
if(mFlvMuxer != null) {
mEncoder.resume();
resumeEncode();
}
}
public void stopPublish() { public void stopPublish() {
if (mFlvMuxer != null) { if (mFlvMuxer != null) {
@ -175,6 +190,12 @@ public class SrsPublisher {
} }
} }
public void pausePublish(){
if (mFlvMuxer != null) {
mEncoder.pause();
pauseEncode();
}
}
public boolean startRecord(String recPath) { public boolean startRecord(String recPath) {
return mMp4Muxer != null && mMp4Muxer.record(new File(recPath)); return mMp4Muxer != null && mMp4Muxer.record(new File(recPath));
} }

Loading…
Cancel
Save