Add pause recording function

Signed-off-by: Leo Ma <begeekmyfriend@gmail.com>
camera2
Leo Ma 9 years ago
parent b2b4b3f75b
commit 2a8bbbe5f3

@ -186,6 +186,7 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback, Ca
stopPublish();
btnPublish.setEnabled(true);
btnStop.setEnabled(false);
btnRecord.setText("record");
}
});
@ -204,12 +205,15 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback, Ca
btnRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (btnRecord.getText() == "Record") {
if (btnRecord.getText().toString().contentEquals("record")) {
mEncoder.record();
btnRecord.setText("Stop");
} else {
mEncoder.stopRecord();
btnRecord.setText("Record");
btnRecord.setText("pause");
} else if (btnRecord.getText().toString().contentEquals("pause")) {
mEncoder.pauseRecord();
btnRecord.setText("resume");
} else if (btnRecord.getText().toString().contentEquals("resume")) {
mEncoder.pauseRecord();
btnRecord.setText("pause");
}
}
});

@ -155,10 +155,10 @@ public class SrsEncoder {
return 0;
}
public void stopRecord() {
public void pauseRecord() {
if (mp4Muxer != null) {
Log.i(TAG, "stop MP4 muxer");
mp4Muxer.stop();
Log.i(TAG, "pause MP4 muxer");
mp4Muxer.pause();
}
}

@ -82,6 +82,8 @@ public class SrsMp4Muxer {
private Thread worker;
private volatile boolean bRecording = false;
private volatile boolean bPaused = false;
private volatile boolean needToSearchKeyFrame = false;
private final Object writeLock = new Object();
private ConcurrentLinkedQueue<SrsEsFrame> frameCache = new ConcurrentLinkedQueue<>();
@ -161,6 +163,16 @@ public class SrsMp4Muxer {
worker.start();
}
/**
* pause recording.
*/
public void pause() {
bPaused = !bPaused;
if (!bPaused) {
needToSearchKeyFrame = true;
}
}
/**
* finish recording.
*/
@ -266,7 +278,7 @@ public class SrsMp4Muxer {
public void writeVideoSample(final ByteBuffer bb, MediaCodec.BufferInfo bi) throws IllegalArgumentException {
int nal_unit_type = bb.get(4) & 0x1f;
if (nal_unit_type == SrsAvcNaluType.IDR || nal_unit_type == SrsAvcNaluType.NonIDR) {
writeFrameByte(VIDEO_TRACK, bb, bi);
writeFrameByte(VIDEO_TRACK, bb, bi, nal_unit_type == SrsAvcNaluType.IDR);
} else {
while (bb.position() < bi.size) {
SrsEsFrameBytes frame = avc.annexb_demux(bb, bi);
@ -305,20 +317,31 @@ public class SrsMp4Muxer {
aacSpecConfig = true;
mp4Movie.addTrack(audioFormat, true);
} else {
writeFrameByte(AUDIO_TRACK, bb, bi);
writeFrameByte(AUDIO_TRACK, bb, bi, false);
}
}
private void writeFrameByte(int track, ByteBuffer bb, MediaCodec.BufferInfo bi) {
private void writeFrameByte(int track, ByteBuffer bb, MediaCodec.BufferInfo bi, boolean isKeyFrame) {
SrsEsFrame frame = new SrsEsFrame();
frame.bb = bb;
frame.bi = bi;
frame.isKeyFrame = isKeyFrame;
frame.track = track;
if (bRecording) {
frameCache.add(frame);
synchronized (writeLock) {
writeLock.notifyAll();
if (bRecording && !bPaused) {
if (needToSearchKeyFrame) {
if (frame.isKeyFrame) {
needToSearchKeyFrame = false;
frameCache.add(frame);
synchronized (writeLock) {
writeLock.notifyAll();
}
}
} else {
frameCache.add(frame);
synchronized (writeLock) {
writeLock.notifyAll();
}
}
}
}
@ -346,6 +369,7 @@ public class SrsMp4Muxer {
public ByteBuffer bb;
public MediaCodec.BufferInfo bi;
public int track;
public boolean isKeyFrame;
public boolean is_video() {
return track == VIDEO_TRACK;
@ -788,6 +812,7 @@ public class SrsMp4Muxer {
private void finishMovie() throws IOException {
if (flushBytes > 0) {
fos.flush();
flushBytes = 0;
}
if (mdat.getSize() != 0) {
// flush cached mdat box
@ -819,6 +844,7 @@ public class SrsMp4Muxer {
track2SampleSizes.clear();
aacSpecConfig = false;
recFileSize = 0;
flushBytes = 0;
}
private FileTypeBox createFileTypeBox() {

Loading…
Cancel
Save