1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "OggWriter"
18
19 #include <fcntl.h>
20 #include <inttypes.h>
21 #include <sys/prctl.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24
25 #include <media/stagefright/MediaSource.h>
26 #include <media/mediarecorder.h>
27 #include <media/stagefright/MediaBuffer.h>
28 #include <media/stagefright/MediaDefs.h>
29 #include <media/stagefright/MediaErrors.h>
30 #include <media/stagefright/MetaData.h>
31 #include <media/stagefright/OggWriter.h>
32 #include <media/stagefright/foundation/ADebug.h>
33 #include <media/stagefright/foundation/OpusHeader.h>
34
35 extern "C" {
36 #include <ogg/ogg.h>
37 }
38
39 // store the int32 value in little-endian order.
writeint(char * buf,int base,int32_t val)40 static inline void writeint(char *buf, int base, int32_t val) {
41 buf[base + 3] = ((val) >> 24) & 0xff;
42 buf[base + 2] = ((val) >> 16) & 0xff;
43 buf[base + 1] = ((val) >> 8) & 0xff;
44 buf[base] = (val)&0xff;
45 }
46
47 // linkage between our header OggStreamState and the underlying ogg_stream_state
48 // so that consumers of our interface do not require the ogg headers themselves.
49 struct OggStreamState : public ogg_stream_state {};
50
51 namespace android {
52
OggWriter(int fd)53 OggWriter::OggWriter(int fd)
54 : mFd(dup(fd)),
55 mHaveAllCodecSpecificData(false),
56 mInitCheck(mFd < 0 ? NO_INIT : OK) {
57 // empty
58 }
59
~OggWriter()60 OggWriter::~OggWriter() {
61 if (mStarted) {
62 reset();
63 }
64
65 if (mFd != -1) {
66 close(mFd);
67 mFd = -1;
68 }
69
70 if (mOs != nullptr) {
71 ogg_stream_clear(mOs);
72 free(mOs);
73 mOs = nullptr;
74 }
75 }
76
initCheck() const77 status_t OggWriter::initCheck() const {
78 return mInitCheck;
79 }
80
addSource(const sp<MediaSource> & source)81 status_t OggWriter::addSource(const sp<MediaSource>& source) {
82 if (mInitCheck != OK) {
83 return mInitCheck;
84 }
85
86 if (mSource != NULL) {
87 return UNKNOWN_ERROR;
88 }
89
90 // Support is limited to single track of Opus audio.
91 const char* mime;
92 source->getFormat()->findCString(kKeyMIMEType, &mime);
93 const char* opus = MEDIA_MIMETYPE_AUDIO_OPUS;
94 if (strncasecmp(mime, opus, strlen(opus))) {
95 ALOGE("Track (%s) other than %s is not supported", mime, opus);
96 return ERROR_UNSUPPORTED;
97 }
98
99 mOs = (OggStreamState*) malloc(sizeof(ogg_stream_state));
100 if (ogg_stream_init((ogg_stream_state*)mOs, rand()) == -1) {
101 ALOGE("ogg stream init failed");
102 return UNKNOWN_ERROR;
103 }
104
105 // Write Ogg headers.
106 int32_t nChannels = 0;
107 if (!source->getFormat()->findInt32(kKeyChannelCount, &nChannels)) {
108 ALOGE("Missing format keys for audio track");
109 source->getFormat()->dumpToLog();
110 return BAD_VALUE;
111 }
112 source->getFormat()->dumpToLog();
113
114 int32_t sampleRate = 0;
115 if (!source->getFormat()->findInt32(kKeySampleRate, &sampleRate)) {
116 ALOGE("Missing format key for sample rate");
117 source->getFormat()->dumpToLog();
118 return UNKNOWN_ERROR;
119 }
120
121 mSampleRate = sampleRate;
122 uint32_t type;
123 const void *header_data = NULL;
124 size_t packet_size = 0;
125
126 if (!source->getFormat()->findData(kKeyOpusHeader, &type, &header_data, &packet_size)) {
127 ALOGV("opus header not found in format");
128 } else if (header_data && packet_size) {
129 writeOggHeaderPackets((unsigned char *)header_data, packet_size);
130 } else {
131 ALOGD("ignoring incomplete opus header data in format");
132 }
133
134 mSource = source;
135 return OK;
136 }
137
writeOggHeaderPackets(unsigned char * buf,size_t size)138 status_t OggWriter::writeOggHeaderPackets(unsigned char *buf, size_t size) {
139 ogg_packet op;
140 ogg_page og;
141 op.packet = buf;
142 op.bytes = size;
143 op.b_o_s = 1;
144 op.e_o_s = 0;
145 op.granulepos = 0;
146 op.packetno = 0;
147 ogg_stream_packetin((ogg_stream_state*)mOs, &op);
148
149 int ret;
150 while ((ret = ogg_stream_flush((ogg_stream_state*)mOs, &og))) {
151 if (!ret) break;
152 write(mFd, og.header, og.header_len);
153 write(mFd, og.body, og.body_len);
154 }
155
156
157 const char* vendor_string = "libopus";
158 const int vendor_length = strlen(vendor_string);
159 int user_comment_list_length = 0;
160
161 const int comments_length = 8 + 4 + vendor_length + 4 + user_comment_list_length;
162 char* comments = (char*)malloc(comments_length);
163 if (comments == NULL) {
164 ALOGE("failed to allocate ogg comment buffer");
165 return UNKNOWN_ERROR;
166 }
167 memcpy(comments, "OpusTags", 8);
168 writeint(comments, 8, vendor_length);
169 memcpy(comments + 12, vendor_string, vendor_length);
170 writeint(comments, 12 + vendor_length, user_comment_list_length);
171
172 op.packet = (unsigned char*)comments;
173 op.bytes = comments_length;
174 op.b_o_s = 0;
175 op.e_o_s = 0;
176 op.granulepos = 0;
177 op.packetno = 1;
178 ogg_stream_packetin((ogg_stream_state*)mOs, &op);
179
180 while ((ret = ogg_stream_flush((ogg_stream_state*)mOs, &og))) {
181 if (!ret) break;
182 write(mFd, og.header, og.header_len);
183 write(mFd, og.body, og.body_len);
184 }
185
186 free(comments);
187 mHaveAllCodecSpecificData = true;
188 return OK;
189 }
190
start(MetaData *)191 status_t OggWriter::start(MetaData* /* params */) {
192 if (mInitCheck != OK) {
193 return mInitCheck;
194 }
195
196 if (mSource == NULL) {
197 return UNKNOWN_ERROR;
198 }
199
200 if (mStarted && mPaused) {
201 mPaused = false;
202 mResumed = true;
203 return OK;
204 } else if (mStarted) {
205 // Already started, does nothing
206 return OK;
207 }
208
209 status_t err = mSource->start();
210
211 if (err != OK) {
212 return err;
213 }
214
215 pthread_attr_t attr;
216 pthread_attr_init(&attr);
217 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
218
219 mReachedEOS = false;
220 mDone = false;
221
222 pthread_create(&mThread, &attr, ThreadWrapper, this);
223 pthread_attr_destroy(&attr);
224
225 mStarted = true;
226
227 return OK;
228 }
229
pause()230 status_t OggWriter::pause() {
231 if (!mStarted) {
232 return OK;
233 }
234 mPaused = true;
235 return OK;
236 }
237
reset()238 status_t OggWriter::reset() {
239 if (!mStarted) {
240 return OK;
241 }
242
243 mDone = true;
244
245 void* dummy;
246 pthread_join(mThread, &dummy);
247
248 status_t err = static_cast<status_t>(reinterpret_cast<uintptr_t>(dummy));
249 {
250 status_t status = mSource->stop();
251 if (err == OK && (status != OK && status != ERROR_END_OF_STREAM)) {
252 err = status;
253 }
254 }
255
256 mStarted = false;
257 return err;
258 }
259
exceedsFileSizeLimit()260 bool OggWriter::exceedsFileSizeLimit() {
261 if (mMaxFileSizeLimitBytes == 0) {
262 return false;
263 }
264 return mEstimatedSizeBytes > mMaxFileSizeLimitBytes;
265 }
266
exceedsFileDurationLimit()267 bool OggWriter::exceedsFileDurationLimit() {
268 if (mMaxFileDurationLimitUs == 0) {
269 return false;
270 }
271 return mEstimatedDurationUs > mMaxFileDurationLimitUs;
272 }
273
274 // static
ThreadWrapper(void * me)275 void* OggWriter::ThreadWrapper(void* me) {
276 return (void*)(uintptr_t) static_cast<OggWriter*>(me)->threadFunc();
277 }
278
threadFunc()279 status_t OggWriter::threadFunc() {
280 mEstimatedDurationUs = 0;
281 mEstimatedSizeBytes = 0;
282 bool stoppedPrematurely = true;
283 int64_t previousPausedDurationUs = 0;
284 int64_t maxTimestampUs = 0;
285 status_t err = OK;
286
287 prctl(PR_SET_NAME, (unsigned long)"OggWriter", 0, 0, 0);
288
289 while (!mDone) {
290 MediaBufferBase* buffer = nullptr;
291 err = mSource->read(&buffer);
292
293 if (err != OK) {
294 ALOGW("failed to read next buffer");
295 break;
296 }
297
298 if (mPaused) {
299 buffer->release();
300 buffer = nullptr;
301 continue;
302 }
303 mEstimatedSizeBytes += buffer->range_length();
304 if (exceedsFileSizeLimit()) {
305 buffer->release();
306 buffer = nullptr;
307 notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED, 0);
308 ALOGW("estimated size(%" PRId64 ") exceeds limit (%" PRId64 ")",
309 mEstimatedSizeBytes, mMaxFileSizeLimitBytes);
310 break;
311 }
312
313 int32_t isCodecSpecific;
314 if ((buffer->meta_data().findInt32(kKeyIsCodecConfig, &isCodecSpecific)
315 && isCodecSpecific)
316 || IsOpusHeader((uint8_t*)buffer->data() + buffer->range_offset(),
317 buffer->range_length())) {
318 if (mHaveAllCodecSpecificData == false) {
319 size_t opusHeadSize = 0;
320 size_t codecDelayBufSize = 0;
321 size_t seekPreRollBufSize = 0;
322 void *opusHeadBuf = NULL;
323 void *codecDelayBuf = NULL;
324 void *seekPreRollBuf = NULL;
325 GetOpusHeaderBuffers((uint8_t*)buffer->data() + buffer->range_offset(),
326 buffer->range_length(), &opusHeadBuf,
327 &opusHeadSize, &codecDelayBuf,
328 &codecDelayBufSize, &seekPreRollBuf,
329 &seekPreRollBufSize);
330 writeOggHeaderPackets((unsigned char *)opusHeadBuf, opusHeadSize);
331 } else {
332 ALOGV("ignoring later copy of CSD contained in info buffer");
333 }
334 buffer->release();
335 buffer = nullptr;
336 continue;
337 }
338
339 if (mHaveAllCodecSpecificData == false) {
340 ALOGE("Did not get valid opus header before first sample data");
341 buffer->release();
342 buffer = nullptr;
343 err = ERROR_MALFORMED;
344 break;
345 }
346
347 int64_t timestampUs;
348 CHECK(buffer->meta_data().findInt64(kKeyTime, ×tampUs));
349 if (timestampUs > mEstimatedDurationUs) {
350 mEstimatedDurationUs = timestampUs;
351 }
352 if (mResumed) {
353 previousPausedDurationUs += (timestampUs - maxTimestampUs - 20000);
354 mResumed = false;
355 }
356
357 timestampUs -= previousPausedDurationUs;
358
359 ALOGV("time stamp: %" PRId64 ", previous paused duration: %" PRId64, timestampUs,
360 previousPausedDurationUs);
361 if (timestampUs > maxTimestampUs) {
362 maxTimestampUs = timestampUs;
363 }
364
365 if (exceedsFileDurationLimit()) {
366 buffer->release();
367 buffer = nullptr;
368 notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_DURATION_REACHED, 0);
369 ALOGW("estimated duration(%" PRId64 " us) exceeds limit(%" PRId64 " us)",
370 mEstimatedDurationUs, mMaxFileDurationLimitUs);
371 break;
372 }
373
374 ogg_packet op;
375 ogg_page og;
376 op.packet = (uint8_t*)buffer->data() + buffer->range_offset();
377 op.bytes = (long)buffer->range_length();
378 op.b_o_s = 0;
379 op.e_o_s = mReachedEOS ? 1 : 0;
380 // granulepos is the total number of PCM audio samples @ 48 kHz, up to and
381 // including the current packet.
382 ogg_int64_t granulepos = (48000 * mEstimatedDurationUs) / 1000000;
383 op.granulepos = granulepos;
384
385 // Headers are at packets 0 and 1.
386 op.packetno = 2 + (ogg_int32_t)mCurrentPacketId++;
387 ogg_stream_packetin((ogg_stream_state*)mOs, &op);
388 size_t n = 0;
389
390 while (ogg_stream_flush((ogg_stream_state*)mOs, &og) > 0) {
391 write(mFd, og.header, og.header_len);
392 write(mFd, og.body, og.body_len);
393 n = n + og.header_len + og.body_len;
394 }
395
396 if (n < buffer->range_length()) {
397 buffer->release();
398 buffer = nullptr;
399 err = ERROR_IO;
400 break;
401 }
402
403 if (err != OK) {
404 break;
405 }
406
407 stoppedPrematurely = false;
408
409 buffer->release();
410 buffer = nullptr;
411 }
412
413 // end of stream is an ok thing
414 if (err == ERROR_END_OF_STREAM) {
415 err = OK;
416 }
417
418 if (err == OK && stoppedPrematurely) {
419 err = ERROR_MALFORMED;
420 }
421
422 close(mFd);
423 mFd = -1;
424 mReachedEOS = true;
425
426 return err;
427 }
428
reachedEOS()429 bool OggWriter::reachedEOS() {
430 return mReachedEOS;
431 }
432
433 } // namespace android
434