Skip to content

Commit b099f49

Browse files
committed
Merge pull request #354 from dronekit/video_recording
video recording bug fixes.
2 parents c07310c + 40232a2 commit b099f49

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

ServiceApp/src/org/droidplanner/services/android/utils/video/MediaCodecManager.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ private boolean processNALUChunk(NALUChunk naluChunk) {
207207
inputBuffer.put(payloadData, 0, dataLength);
208208

209209
totalLength += dataLength;
210-
211-
streamRecorder.onNaluChunkUpdated(payloadData, 0, dataLength);
212210
}
213211

212+
streamRecorder.onNaluChunkUpdated(naluChunkAssembler.getParametersSet(), naluChunk);
213+
214214
mediaCodec.queueInputBuffer(index, 0, totalLength, naluChunk.presentationTime, naluChunk.flags);
215215
}
216216
} catch (IllegalStateException e) {

ServiceApp/src/org/droidplanner/services/android/utils/video/NALUChunkAssembler.java

+7
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ NALUChunk getEndOfStream(){
6464
private int naluCounter = 0;
6565
private final static long DELTA_PRESENTATION_TIME = 42000L;
6666

67+
NALUChunk getParametersSet(){
68+
if(areParametersSet())
69+
return paramsNaluChunk;
70+
71+
return null;
72+
}
73+
6774
NALUChunk assembleNALUChunk(byte[] buffer, int bufferLength) {
6875

6976
//The first 12 bytes are the rtp header.

ServiceApp/src/org/droidplanner/services/android/utils/video/StreamRecorder.java

+39-5
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
import java.io.FileNotFoundException;
1919
import java.io.FileOutputStream;
2020
import java.io.IOException;
21+
import java.nio.ByteBuffer;
2122
import java.nio.channels.FileChannel;
2223
import java.util.concurrent.ExecutorService;
2324
import java.util.concurrent.Executors;
25+
import java.util.concurrent.atomic.AtomicBoolean;
2426
import java.util.concurrent.atomic.AtomicReference;
2527

2628
import timber.log.Timber;
@@ -31,6 +33,7 @@
3133
class StreamRecorder {
3234

3335
private final AtomicReference<String> recordingFilename = new AtomicReference<>();
36+
private final AtomicBoolean areParametersSet = new AtomicBoolean(false);
3437

3538
private final File mediaRootDir;
3639
private final Context context;
@@ -75,6 +78,7 @@ boolean isRecordingEnabled() {
7578

7679
boolean enableRecording(String mediaFilename) {
7780
if (!isRecordingEnabled()) {
81+
areParametersSet.set(false);
7882
recordingFilename.set(mediaFilename);
7983

8084
Timber.i("Enabling local recording to %s", mediaFilename);
@@ -117,18 +121,48 @@ boolean disableRecording() {
117121
}
118122
}
119123

124+
areParametersSet.set(false);
125+
120126
return true;
121127
}
122128

123129
//TODO: Maybe put this on a background thread to avoid blocking on the write to file.
124-
void onNaluChunkUpdated(byte[] payload, int index, int payloadLength) {
130+
void onNaluChunkUpdated(NALUChunk parametersSet, NALUChunk dataChunk) {
125131
if (isRecordingEnabled() && h264Writer != null) {
126-
try {
127-
h264Writer.write(payload, index, payloadLength);
128-
} catch (IOException e) {
129-
Timber.e(e, e.getMessage());
132+
if(areParametersSet.get()) {
133+
try {
134+
writeNaluChunk(h264Writer, dataChunk);
135+
} catch (IOException e) {
136+
Timber.e(e, e.getMessage());
137+
}
130138
}
139+
else{
140+
try {
141+
areParametersSet.set(writeNaluChunk(h264Writer, parametersSet));
142+
} catch (IOException e) {
143+
Timber.e(e, e.getMessage());
144+
}
145+
}
146+
}
147+
}
148+
149+
private boolean writeNaluChunk(BufferedOutputStream bos, NALUChunk naluChunk) throws IOException {
150+
if(naluChunk == null)
151+
return false;
152+
153+
int payloadCount = naluChunk.payloads.length;
154+
for (int i = 0; i < payloadCount; i++) {
155+
ByteBuffer payload = naluChunk.payloads[i];
156+
157+
if (payload.capacity() == 0)
158+
continue;
159+
160+
final int dataLength = payload.position();
161+
byte[] payloadData = payload.array();
162+
bos.write(payloadData, 0, dataLength);
131163
}
164+
165+
return true;
132166
}
133167

134168
void convertToMp4(final String filename) {

0 commit comments

Comments
 (0)