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     // NOLINTNEXTLINE(clang-analyzer-unix.MallocSizeof)
100     mOs = (OggStreamState*) malloc(sizeof(ogg_stream_state));
101     if (ogg_stream_init((ogg_stream_state*)mOs, rand()) == -1) {
102         ALOGE("ogg stream init failed");
103         return UNKNOWN_ERROR;
104     }
105 
106     // Write Ogg headers.
107     int32_t nChannels = 0;
108     if (!source->getFormat()->findInt32(kKeyChannelCount, &nChannels)) {
109         ALOGE("Missing format keys for audio track");
110         source->getFormat()->dumpToLog();
111         return BAD_VALUE;
112     }
113     source->getFormat()->dumpToLog();
114 
115     int32_t sampleRate = 0;
116     if (!source->getFormat()->findInt32(kKeySampleRate, &sampleRate)) {
117         ALOGE("Missing format key for sample rate");
118         source->getFormat()->dumpToLog();
119         return UNKNOWN_ERROR;
120     }
121 
122     mSampleRate = sampleRate;
123     uint32_t type;
124     const void *header_data = NULL;
125     size_t packet_size = 0;
126 
127     if (!source->getFormat()->findData(kKeyOpusHeader, &type, &header_data, &packet_size)) {
128         ALOGV("opus header not found in format");
129     } else if (header_data && packet_size) {
130         writeOggHeaderPackets((unsigned char *)header_data, packet_size);
131     } else {
132         ALOGD("ignoring incomplete opus header data in format");
133     }
134 
135     mSource = source;
136     return OK;
137 }
138 
writeOggHeaderPackets(unsigned char * buf,size_t size)139 status_t OggWriter::writeOggHeaderPackets(unsigned char *buf, size_t size) {
140     ogg_packet op;
141     ogg_page og;
142     op.packet = buf;
143     op.bytes = size;
144     op.b_o_s = 1;
145     op.e_o_s = 0;
146     op.granulepos = 0;
147     op.packetno = 0;
148     ogg_stream_packetin((ogg_stream_state*)mOs, &op);
149 
150     int ret;
151     while ((ret = ogg_stream_flush((ogg_stream_state*)mOs, &og))) {
152         if (!ret) break;
153         write(mFd, og.header, og.header_len);
154         write(mFd, og.body, og.body_len);
155     }
156 
157 
158     const char* vendor_string = "libopus";
159     const int vendor_length = strlen(vendor_string);
160     int user_comment_list_length = 0;
161 
162     const int comments_length = 8 + 4 + vendor_length + 4 + user_comment_list_length;
163     char* comments = (char*)malloc(comments_length);
164     if (comments == NULL) {
165         ALOGE("failed to allocate ogg comment buffer");
166         return UNKNOWN_ERROR;
167     }
168     memcpy(comments, "OpusTags", 8);
169     writeint(comments, 8, vendor_length);
170     memcpy(comments + 12, vendor_string, vendor_length);
171     writeint(comments, 12 + vendor_length, user_comment_list_length);
172 
173     op.packet = (unsigned char*)comments;
174     op.bytes = comments_length;
175     op.b_o_s = 0;
176     op.e_o_s = 0;
177     op.granulepos = 0;
178     op.packetno = 1;
179     ogg_stream_packetin((ogg_stream_state*)mOs, &op);
180 
181     while ((ret = ogg_stream_flush((ogg_stream_state*)mOs, &og))) {
182         if (!ret) break;
183         write(mFd, og.header, og.header_len);
184         write(mFd, og.body, og.body_len);
185     }
186 
187     free(comments);
188     mHaveAllCodecSpecificData = true;
189     return OK;
190 }
191 
start(MetaData *)192 status_t OggWriter::start(MetaData* /* params */) {
193     if (mInitCheck != OK) {
194         return mInitCheck;
195     }
196 
197     if (mSource == NULL) {
198         return UNKNOWN_ERROR;
199     }
200 
201     if (mStarted && mPaused) {
202         mPaused = false;
203         mResumed = true;
204         return OK;
205     } else if (mStarted) {
206         // Already started, does nothing
207         return OK;
208     }
209 
210     status_t err = mSource->start();
211 
212     if (err != OK) {
213         return err;
214     }
215 
216     pthread_attr_t attr;
217     pthread_attr_init(&attr);
218     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
219 
220     mReachedEOS = false;
221     mDone = false;
222 
223     pthread_create(&mThread, &attr, ThreadWrapper, this);
224     pthread_attr_destroy(&attr);
225 
226     mStarted = true;
227 
228     return OK;
229 }
230 
pause()231 status_t OggWriter::pause() {
232     if (!mStarted) {
233         return OK;
234     }
235     mPaused = true;
236     return OK;
237 }
238 
reset()239 status_t OggWriter::reset() {
240     if (!mStarted) {
241         return OK;
242     }
243 
244     mDone = true;
245 
246     status_t status = mSource->stop();
247 
248     void *pthread_res;
249     pthread_join(mThread, &pthread_res);
250 
251     status_t err = static_cast<status_t>(reinterpret_cast<uintptr_t>(pthread_res));
252     {
253         if (err == OK &&
254             (status != OK && status != ERROR_END_OF_STREAM)) {
255             err = status;
256         }
257     }
258 
259     mStarted = false;
260     return err;
261 }
262 
exceedsFileSizeLimit()263 bool OggWriter::exceedsFileSizeLimit() {
264     if (mMaxFileSizeLimitBytes == 0) {
265         return false;
266     }
267     return mEstimatedSizeBytes > mMaxFileSizeLimitBytes;
268 }
269 
exceedsFileDurationLimit()270 bool OggWriter::exceedsFileDurationLimit() {
271     if (mMaxFileDurationLimitUs == 0) {
272         return false;
273     }
274     return mEstimatedDurationUs > mMaxFileDurationLimitUs;
275 }
276 
277 // static
ThreadWrapper(void * me)278 void* OggWriter::ThreadWrapper(void* me) {
279     return (void*)(uintptr_t) static_cast<OggWriter*>(me)->threadFunc();
280 }
281 
threadFunc()282 status_t OggWriter::threadFunc() {
283     mEstimatedDurationUs = 0;
284     mEstimatedSizeBytes = 0;
285     bool stoppedPrematurely = true;
286     int64_t previousPausedDurationUs = 0;
287     int64_t maxTimestampUs = 0;
288     status_t err = OK;
289 
290     prctl(PR_SET_NAME, (unsigned long)"OggWriter", 0, 0, 0);
291 
292     while (!mDone) {
293         MediaBufferBase* buffer = nullptr;
294         err = mSource->read(&buffer);
295 
296         if (err != OK) {
297             ALOGW("failed to read next buffer");
298             break;
299         }
300 
301         if (mPaused) {
302             buffer->release();
303             buffer = nullptr;
304             continue;
305         }
306         mEstimatedSizeBytes += buffer->range_length();
307         if (exceedsFileSizeLimit()) {
308             buffer->release();
309             buffer = nullptr;
310             notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED, 0);
311             ALOGW("estimated size(%" PRId64 ") exceeds limit (%" PRId64 ")",
312                   mEstimatedSizeBytes, mMaxFileSizeLimitBytes);
313             break;
314         }
315 
316         int32_t isCodecSpecific;
317         if ((buffer->meta_data().findInt32(kKeyIsCodecConfig, &isCodecSpecific)
318              && isCodecSpecific)
319             || IsOpusHeader((uint8_t*)buffer->data() + buffer->range_offset(),
320                          buffer->range_length())) {
321             if (mHaveAllCodecSpecificData == false) {
322                 size_t opusHeadSize = 0;
323                 size_t codecDelayBufSize = 0;
324                 size_t seekPreRollBufSize = 0;
325                 void *opusHeadBuf = NULL;
326                 void *codecDelayBuf = NULL;
327                 void *seekPreRollBuf = NULL;
328                 GetOpusHeaderBuffers((uint8_t*)buffer->data() + buffer->range_offset(),
329                                     buffer->range_length(), &opusHeadBuf,
330                                     &opusHeadSize, &codecDelayBuf,
331                                     &codecDelayBufSize, &seekPreRollBuf,
332                                     &seekPreRollBufSize);
333                 writeOggHeaderPackets((unsigned char *)opusHeadBuf, opusHeadSize);
334             } else {
335                 ALOGV("ignoring later copy of CSD contained in info buffer");
336             }
337             buffer->release();
338             buffer = nullptr;
339             continue;
340         }
341 
342         if (mHaveAllCodecSpecificData == false) {
343             ALOGE("Did not get valid opus header before first sample data");
344             buffer->release();
345             buffer = nullptr;
346             err = ERROR_MALFORMED;
347             break;
348         }
349 
350         int64_t timestampUs;
351         CHECK(buffer->meta_data().findInt64(kKeyTime, &timestampUs));
352         if (timestampUs > mEstimatedDurationUs) {
353             mEstimatedDurationUs = timestampUs;
354         }
355         if (mResumed) {
356             previousPausedDurationUs += (timestampUs - maxTimestampUs - 20000);
357             mResumed = false;
358         }
359 
360         timestampUs -= previousPausedDurationUs;
361 
362         ALOGV("time stamp: %" PRId64 ", previous paused duration: %" PRId64, timestampUs,
363               previousPausedDurationUs);
364         if (timestampUs > maxTimestampUs) {
365             maxTimestampUs = timestampUs;
366         }
367 
368         if (exceedsFileDurationLimit()) {
369             buffer->release();
370             buffer = nullptr;
371             notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_DURATION_REACHED, 0);
372             ALOGW("estimated duration(%" PRId64 " us) exceeds limit(%" PRId64 " us)",
373                   mEstimatedDurationUs, mMaxFileDurationLimitUs);
374             break;
375         }
376 
377         ogg_packet op;
378         ogg_page og;
379         op.packet = (uint8_t*)buffer->data() + buffer->range_offset();
380         op.bytes = (long)buffer->range_length();
381         op.b_o_s = 0;
382         op.e_o_s = mReachedEOS ? 1 : 0;
383         // granulepos is the total number of PCM audio samples @ 48 kHz, up to and
384         // including the current packet.
385         ogg_int64_t granulepos = (48000 * mEstimatedDurationUs) / 1000000;
386         op.granulepos = granulepos;
387 
388         // Headers are at packets 0 and 1.
389         op.packetno = 2 + (ogg_int32_t)mCurrentPacketId++;
390         ogg_stream_packetin((ogg_stream_state*)mOs, &op);
391         size_t n = 0;
392 
393         while (ogg_stream_flush((ogg_stream_state*)mOs, &og) > 0) {
394             write(mFd, og.header, og.header_len);
395             write(mFd, og.body, og.body_len);
396             n = n + og.header_len + og.body_len;
397         }
398 
399         if (n < buffer->range_length()) {
400             buffer->release();
401             buffer = nullptr;
402             err = ERROR_IO;
403             break;
404         }
405 
406         if (err != OK) {
407             break;
408         }
409 
410         stoppedPrematurely = false;
411 
412         buffer->release();
413         buffer = nullptr;
414     }
415 
416     // end of stream is an ok thing
417     if (err == ERROR_END_OF_STREAM) {
418         err = OK;
419     }
420 
421     if (err == OK && stoppedPrematurely) {
422         err = ERROR_MALFORMED;
423     }
424 
425     close(mFd);
426     mFd = -1;
427     mReachedEOS = true;
428 
429     return err;
430 }
431 
reachedEOS()432 bool OggWriter::reachedEOS() {
433     return mReachedEOS;
434 }
435 
436 }  // namespace android
437