Fix MP4 recording bug

AV tracks and samples should be cleared when recording finishes.

Signed-off-by: Leo Ma <begeekmyfriend@gmail.com>
camera2
Leo Ma 9 years ago
parent 6c922d3026
commit b2ec6c8e55

@ -131,6 +131,11 @@ public class SrsMp4Muxer {
createMovie(mRecFile); createMovie(mRecFile);
mHandler.onRecordStarted(mRecFile.getPath()); mHandler.onRecordStarted(mRecFile.getPath());
if (!spsList.isEmpty() && !ppsList.isEmpty()) {
mp4Movie.addTrack(videoFormat, false);
}
mp4Movie.addTrack(audioFormat, true);
worker = new Thread(new Runnable() { worker = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -187,8 +192,9 @@ public class SrsMp4Muxer {
public void stop() { public void stop() {
bRecording = false; bRecording = false;
bPaused = false; bPaused = false;
needToFindKeyFrame = false; needToFindKeyFrame = true;
aacSpecConfig = false; aacSpecConfig = false;
frameCache.clear();
if (worker != null) { if (worker != null) {
try { try {
@ -315,17 +321,12 @@ public class SrsMp4Muxer {
continue; continue;
} }
} }
if (!spsList.isEmpty() && !ppsList.isEmpty()) {
mp4Movie.addTrack(videoFormat, false);
}
} }
} }
public void writeAudioSample(final ByteBuffer bb, MediaCodec.BufferInfo bi) { public void writeAudioSample(final ByteBuffer bb, MediaCodec.BufferInfo bi) {
if (!aacSpecConfig) { if (!aacSpecConfig) {
aacSpecConfig = true; aacSpecConfig = true;
mp4Movie.addTrack(audioFormat, true);
} else { } else {
writeFrameByte(AUDIO_TRACK, bb, bi, false); writeFrameByte(AUDIO_TRACK, bb, bi, false);
} }
@ -611,6 +612,13 @@ public class SrsMp4Muxer {
first = false; first = false;
} }
public void clearSample() {
first = true;
samples.clear();
syncSamples.clear();
sampleDurations.clear();
}
public ArrayList<Sample> getSamples() { public ArrayList<Sample> getSamples() {
return samples; return samples;
} }
@ -699,12 +707,16 @@ public class SrsMp4Muxer {
tracks.put(VIDEO_TRACK, new Track(tracks.size(), format, false)); tracks.put(VIDEO_TRACK, new Track(tracks.size(), format, false));
} }
} }
public void removeTrack(int trackIndex) {
tracks.remove(trackIndex);
}
} }
public class InterleaveChunkMdat implements Box { public class InterleaveChunkMdat implements Box {
private boolean first = true; private boolean first = true;
private ContainerBox parent; private ContainerBox parent;
private ByteBuffer header; private ByteBuffer header = ByteBuffer.allocateDirect(16);
private long contentSize = 1024 * 1024 * 1024; private long contentSize = 1024 * 1024 * 1024;
public ContainerBox getParent() { public ContainerBox getParent() {
@ -740,7 +752,7 @@ public class SrsMp4Muxer {
} }
public void getBox(WritableByteChannel writableByteChannel) throws IOException { public void getBox(WritableByteChannel writableByteChannel) throws IOException {
header = ByteBuffer.allocate(16); header.rewind();
long size = getSize(); long size = getSize();
if (isSmallBox(size)) { if (isSmallBox(size)) {
IsoTypeWriter.writeUInt32(header, size); IsoTypeWriter.writeUInt32(header, size);

Loading…
Cancel
Save