Skip to content

Commit

Permalink
Add pause recording function
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Ma <[email protected]>
  • Loading branch information
begeekmyfriend committed May 30, 2016
1 parent b2b4b3f commit 2a8bbbe
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
14 changes: 9 additions & 5 deletions app/src/main/java/net/ossrs/yasea/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public void onClick(View v) {
stopPublish();
btnPublish.setEnabled(true);
btnStop.setEnabled(false);
btnRecord.setText("record");
}
});

Expand All @@ -204,12 +205,15 @@ public void onClick(View v) {
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");
}
}
});
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/net/ossrs/yasea/SrsEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ public int start() {
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();
}
}

Expand Down
40 changes: 33 additions & 7 deletions app/src/main/java/net/ossrs/yasea/SrsMp4Muxer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<>();

Expand Down Expand Up @@ -161,6 +163,16 @@ public void run() {
worker.start();
}

/**
* pause recording.
*/
public void pause() {
bPaused = !bPaused;
if (!bPaused) {
needToSearchKeyFrame = true;
}
}

/**
* finish recording.
*/
Expand Down Expand Up @@ -266,7 +278,7 @@ class SrsAvcNaluType
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);
Expand Down Expand Up @@ -305,20 +317,31 @@ public void writeAudioSample(final ByteBuffer bb, MediaCodec.BufferInfo bi) {
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();
}
}
}
}
Expand Down Expand Up @@ -346,6 +369,7 @@ class SrsEsFrame {
public ByteBuffer bb;
public MediaCodec.BufferInfo bi;
public int track;
public boolean isKeyFrame;

public boolean is_video() {
return track == VIDEO_TRACK;
Expand Down Expand Up @@ -788,6 +812,7 @@ private void writeSampleData(int trackIndex, ByteBuffer byteBuf, MediaCodec.Buff
private void finishMovie() throws IOException {
if (flushBytes > 0) {
fos.flush();
flushBytes = 0;
}
if (mdat.getSize() != 0) {
// flush cached mdat box
Expand Down Expand Up @@ -819,6 +844,7 @@ private void finishMovie() throws IOException {
track2SampleSizes.clear();
aacSpecConfig = false;
recFileSize = 0;
flushBytes = 0;
}

private FileTypeBox createFileTypeBox() {
Expand Down

0 comments on commit 2a8bbbe

Please sign in to comment.