|
18 | 18 | import java.io.FileNotFoundException;
|
19 | 19 | import java.io.FileOutputStream;
|
20 | 20 | import java.io.IOException;
|
| 21 | +import java.nio.ByteBuffer; |
21 | 22 | import java.nio.channels.FileChannel;
|
22 | 23 | import java.util.concurrent.ExecutorService;
|
23 | 24 | import java.util.concurrent.Executors;
|
| 25 | +import java.util.concurrent.atomic.AtomicBoolean; |
24 | 26 | import java.util.concurrent.atomic.AtomicReference;
|
25 | 27 |
|
26 | 28 | import timber.log.Timber;
|
|
31 | 33 | class StreamRecorder {
|
32 | 34 |
|
33 | 35 | private final AtomicReference<String> recordingFilename = new AtomicReference<>();
|
| 36 | + private final AtomicBoolean areParametersSet = new AtomicBoolean(false); |
34 | 37 |
|
35 | 38 | private final File mediaRootDir;
|
36 | 39 | private final Context context;
|
@@ -75,6 +78,7 @@ boolean isRecordingEnabled() {
|
75 | 78 |
|
76 | 79 | boolean enableRecording(String mediaFilename) {
|
77 | 80 | if (!isRecordingEnabled()) {
|
| 81 | + areParametersSet.set(false); |
78 | 82 | recordingFilename.set(mediaFilename);
|
79 | 83 |
|
80 | 84 | Timber.i("Enabling local recording to %s", mediaFilename);
|
@@ -117,18 +121,48 @@ boolean disableRecording() {
|
117 | 121 | }
|
118 | 122 | }
|
119 | 123 |
|
| 124 | + areParametersSet.set(false); |
| 125 | + |
120 | 126 | return true;
|
121 | 127 | }
|
122 | 128 |
|
123 | 129 | //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) { |
125 | 131 | 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 | + } |
130 | 138 | }
|
| 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); |
131 | 163 | }
|
| 164 | + |
| 165 | + return true; |
132 | 166 | }
|
133 | 167 |
|
134 | 168 | void convertToMp4(final String filename) {
|
|
0 commit comments