1760943b5e7a09b602aba04ec451e97662f48b0a4James Dong/*
2760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * Copyright (C) 2011 The Android Open Source Project
3760943b5e7a09b602aba04ec451e97662f48b0a4James Dong *
4760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * Licensed under the Apache License, Version 2.0 (the "License");
5760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * you may not use this file except in compliance with the License.
6760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * You may obtain a copy of the License at
7760943b5e7a09b602aba04ec451e97662f48b0a4James Dong *
8760943b5e7a09b602aba04ec451e97662f48b0a4James Dong *      http://www.apache.org/licenses/LICENSE-2.0
9760943b5e7a09b602aba04ec451e97662f48b0a4James Dong *
10760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * Unless required by applicable law or agreed to in writing, software
11760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * distributed under the License is distributed on an "AS IS" BASIS,
12760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * See the License for the specific language governing permissions and
14760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * limitations under the License.
15760943b5e7a09b602aba04ec451e97662f48b0a4James Dong */
16760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
17760943b5e7a09b602aba04ec451e97662f48b0a4James Dong//#define LOG_NDEBUG 0
18760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#define LOG_TAG "AACWriter"
19760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <utils/Log.h>
20760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
21aeb8fd460ed87d032b3fb8bb61e21eb542ce0f5bDave Burke#include <media/openmax/OMX_Audio.h>
22760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <media/stagefright/AACWriter.h>
23760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <media/stagefright/MediaBuffer.h>
24760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <media/stagefright/foundation/ADebug.h>
25760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <media/stagefright/MediaDefs.h>
26760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <media/stagefright/MediaErrors.h>
27760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <media/stagefright/MediaSource.h>
28760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <media/stagefright/MetaData.h>
29760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <media/mediarecorder.h>
30760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <sys/prctl.h>
31af8e8aa1ada2948972555592570ec9ad90cbf372Nick Kralevich#include <sys/types.h>
32af8e8aa1ada2948972555592570ec9ad90cbf372Nick Kralevich#include <sys/stat.h>
33760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <fcntl.h>
34760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
35760943b5e7a09b602aba04ec451e97662f48b0a4James Dongnamespace android {
36760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
37760943b5e7a09b602aba04ec451e97662f48b0a4James DongAACWriter::AACWriter(const char *filename)
38760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    : mFd(-1),
39760943b5e7a09b602aba04ec451e97662f48b0a4James Dong      mInitCheck(NO_INIT),
40760943b5e7a09b602aba04ec451e97662f48b0a4James Dong      mStarted(false),
41760943b5e7a09b602aba04ec451e97662f48b0a4James Dong      mPaused(false),
42760943b5e7a09b602aba04ec451e97662f48b0a4James Dong      mResumed(false),
43760943b5e7a09b602aba04ec451e97662f48b0a4James Dong      mChannelCount(-1),
44aeb8fd460ed87d032b3fb8bb61e21eb542ce0f5bDave Burke      mSampleRate(-1),
45aeb8fd460ed87d032b3fb8bb61e21eb542ce0f5bDave Burke      mAACProfile(OMX_AUDIO_AACObjectLC) {
46760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
473856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AACWriter Constructor");
48760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
49af8e8aa1ada2948972555592570ec9ad90cbf372Nick Kralevich    mFd = open(filename, O_CREAT | O_LARGEFILE | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR);
50760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    if (mFd >= 0) {
51760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        mInitCheck = OK;
52760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    }
53760943b5e7a09b602aba04ec451e97662f48b0a4James Dong}
54760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
55760943b5e7a09b602aba04ec451e97662f48b0a4James DongAACWriter::AACWriter(int fd)
56760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    : mFd(dup(fd)),
57760943b5e7a09b602aba04ec451e97662f48b0a4James Dong      mInitCheck(mFd < 0? NO_INIT: OK),
58760943b5e7a09b602aba04ec451e97662f48b0a4James Dong      mStarted(false),
59760943b5e7a09b602aba04ec451e97662f48b0a4James Dong      mPaused(false),
60760943b5e7a09b602aba04ec451e97662f48b0a4James Dong      mResumed(false),
61760943b5e7a09b602aba04ec451e97662f48b0a4James Dong      mChannelCount(-1),
62760943b5e7a09b602aba04ec451e97662f48b0a4James Dong      mSampleRate(-1) {
63760943b5e7a09b602aba04ec451e97662f48b0a4James Dong}
64760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
65760943b5e7a09b602aba04ec451e97662f48b0a4James DongAACWriter::~AACWriter() {
66760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    if (mStarted) {
678bcc65c753085fe3328592cceda0cf0e8f8b0a45James Dong        reset();
68760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    }
69760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
70760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    if (mFd != -1) {
71760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        close(mFd);
72760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        mFd = -1;
73760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    }
74760943b5e7a09b602aba04ec451e97662f48b0a4James Dong}
75760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
76760943b5e7a09b602aba04ec451e97662f48b0a4James Dongstatus_t AACWriter::initCheck() const {
77760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    return mInitCheck;
78760943b5e7a09b602aba04ec451e97662f48b0a4James Dong}
79760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
80760943b5e7a09b602aba04ec451e97662f48b0a4James Dongstatic int writeInt8(int fd, uint8_t x) {
81760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    return ::write(fd, &x, 1);
82760943b5e7a09b602aba04ec451e97662f48b0a4James Dong}
83760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
84760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
85760943b5e7a09b602aba04ec451e97662f48b0a4James Dongstatus_t AACWriter::addSource(const sp<MediaSource> &source) {
86760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    if (mInitCheck != OK) {
87760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        return mInitCheck;
88760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    }
89760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
90760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    if (mSource != NULL) {
9129357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("AAC files only support a single track of audio.");
92760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        return UNKNOWN_ERROR;
93760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    }
94760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
95760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    sp<MetaData> meta = source->getFormat();
96760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
97760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    const char *mime;
98760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    CHECK(meta->findCString(kKeyMIMEType, &mime));
99760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
100760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    CHECK(!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC));
101760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    CHECK(meta->findInt32(kKeyChannelCount, &mChannelCount));
102760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    CHECK(meta->findInt32(kKeySampleRate, &mSampleRate));
103760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    CHECK(mChannelCount >= 1 && mChannelCount <= 2);
104760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
1052d94235a343151bed9aa74e86697fa01241ea5cfJames Dong    // Optionally, we want to check whether AACProfile is also set.
1062d94235a343151bed9aa74e86697fa01241ea5cfJames Dong    if (meta->findInt32(kKeyAACProfile, &mAACProfile)) {
1072d94235a343151bed9aa74e86697fa01241ea5cfJames Dong        ALOGI("AAC profile is changed to %d", mAACProfile);
1082d94235a343151bed9aa74e86697fa01241ea5cfJames Dong    }
1092d94235a343151bed9aa74e86697fa01241ea5cfJames Dong
110760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    mSource = source;
111760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    return OK;
112760943b5e7a09b602aba04ec451e97662f48b0a4James Dong}
113760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
114760943b5e7a09b602aba04ec451e97662f48b0a4James Dongstatus_t AACWriter::start(MetaData *params) {
115760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    if (mInitCheck != OK) {
116760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        return mInitCheck;
117760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    }
118760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
119760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    if (mSource == NULL) {
120760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        return UNKNOWN_ERROR;
121760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    }
122760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
123760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    if (mStarted && mPaused) {
124760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        mPaused = false;
125760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        mResumed = true;
126760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        return OK;
127760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    } else if (mStarted) {
128760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        // Already started, does nothing
129760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        return OK;
130760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    }
131760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
132760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    mFrameDurationUs = (kSamplesPerFrame * 1000000LL + (mSampleRate >> 1))
133760943b5e7a09b602aba04ec451e97662f48b0a4James Dong                            / mSampleRate;
134760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
135760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    status_t err = mSource->start();
136760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
137760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    if (err != OK) {
138760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        return err;
139760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    }
140760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
141760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    pthread_attr_t attr;
142760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    pthread_attr_init(&attr);
143760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
144760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
145760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    mReachedEOS = false;
146760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    mDone = false;
147760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
148760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    pthread_create(&mThread, &attr, ThreadWrapper, this);
149760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    pthread_attr_destroy(&attr);
150760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
151760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    mStarted = true;
152760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
153760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    return OK;
154760943b5e7a09b602aba04ec451e97662f48b0a4James Dong}
155760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
156760943b5e7a09b602aba04ec451e97662f48b0a4James Dongstatus_t AACWriter::pause() {
157760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    if (!mStarted) {
158760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        return OK;
159760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    }
160760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    mPaused = true;
161760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    return OK;
162760943b5e7a09b602aba04ec451e97662f48b0a4James Dong}
163760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
1648bcc65c753085fe3328592cceda0cf0e8f8b0a45James Dongstatus_t AACWriter::reset() {
165760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    if (!mStarted) {
166760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        return OK;
167760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    }
168760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
169760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    mDone = true;
170760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
171760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    void *dummy;
172760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    pthread_join(mThread, &dummy);
173760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
174760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    status_t err = (status_t) dummy;
175760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    {
176760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        status_t status = mSource->stop();
177760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        if (err == OK &&
178760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            (status != OK && status != ERROR_END_OF_STREAM)) {
179760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            err = status;
180760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        }
181760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    }
182760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
183760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    mStarted = false;
184760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    return err;
185760943b5e7a09b602aba04ec451e97662f48b0a4James Dong}
186760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
187760943b5e7a09b602aba04ec451e97662f48b0a4James Dongbool AACWriter::exceedsFileSizeLimit() {
188760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    if (mMaxFileSizeLimitBytes == 0) {
189760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        return false;
190760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    }
191760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    return mEstimatedSizeBytes >= mMaxFileSizeLimitBytes;
192760943b5e7a09b602aba04ec451e97662f48b0a4James Dong}
193760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
194760943b5e7a09b602aba04ec451e97662f48b0a4James Dongbool AACWriter::exceedsFileDurationLimit() {
195760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    if (mMaxFileDurationLimitUs == 0) {
196760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        return false;
197760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    }
198760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    return mEstimatedDurationUs >= mMaxFileDurationLimitUs;
199760943b5e7a09b602aba04ec451e97662f48b0a4James Dong}
200760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
201760943b5e7a09b602aba04ec451e97662f48b0a4James Dong// static
202760943b5e7a09b602aba04ec451e97662f48b0a4James Dongvoid *AACWriter::ThreadWrapper(void *me) {
203760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    return (void *) static_cast<AACWriter *>(me)->threadFunc();
204760943b5e7a09b602aba04ec451e97662f48b0a4James Dong}
205760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
206760943b5e7a09b602aba04ec451e97662f48b0a4James Dong/*
207760943b5e7a09b602aba04ec451e97662f48b0a4James Dong* Returns an index into the sample rate table if the
208760943b5e7a09b602aba04ec451e97662f48b0a4James Dong* given sample rate is found; otherwise, returns -1.
209760943b5e7a09b602aba04ec451e97662f48b0a4James Dong*/
210760943b5e7a09b602aba04ec451e97662f48b0a4James Dongstatic bool getSampleRateTableIndex(int sampleRate, uint8_t* tableIndex) {
211760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    static const int kSampleRateTable[] = {
212760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        96000, 88200, 64000, 48000, 44100, 32000,
213760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        24000, 22050, 16000, 12000, 11025, 8000
214760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    };
215760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    const int tableSize =
216760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        sizeof(kSampleRateTable) / sizeof(kSampleRateTable[0]);
217760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
218760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    *tableIndex = 0;
219760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    for (int index = 0; index < tableSize; ++index) {
220760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        if (sampleRate == kSampleRateTable[index]) {
2213856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("Sample rate: %d and index: %d",
222760943b5e7a09b602aba04ec451e97662f48b0a4James Dong                sampleRate, index);
223760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            *tableIndex = index;
224760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            return true;
225760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        }
226760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    }
227760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
22829357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block    ALOGE("Sampling rate %d bps is not supported", sampleRate);
229760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    return false;
230760943b5e7a09b602aba04ec451e97662f48b0a4James Dong}
231760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
232760943b5e7a09b602aba04ec451e97662f48b0a4James Dong/*
233760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * ADTS (Audio data transport stream) header structure.
234760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * It consists of 7 or 9 bytes (with or without CRC):
235760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 12 bits of syncword 0xFFF, all bits must be 1
236760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 1 bit of field ID. 0 for MPEG-4, and 1 for MPEG-2
237760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 2 bits of MPEG layer. If in MPEG-TS, set to 0
238760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 1 bit of protection absense. Set to 1 if no CRC.
239760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 2 bits of profile code. Set to 1 (The MPEG-4 Audio
240760943b5e7a09b602aba04ec451e97662f48b0a4James Dong *   object type minus 1. We are using AAC-LC = 2)
241760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 4 bits of sampling frequency index code (15 is not allowed)
242760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 1 bit of private stream. Set to 0.
243760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 3 bits of channel configuration code. 0 resevered for inband PCM
244760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 1 bit of originality. Set to 0.
245760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 1 bit of home. Set to 0.
246760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 1 bit of copyrighted steam. Set to 0.
247760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 1 bit of copyright start. Set to 0.
248760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 13 bits of frame length. It included 7 ot 9 bytes header length.
249760943b5e7a09b602aba04ec451e97662f48b0a4James Dong *   it is set to (protection absense? 7: 9) + size(AAC frame)
250760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 11 bits of buffer fullness. 0x7FF for VBR.
251760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 2 bits of frames count in one packet. Set to 0.
252760943b5e7a09b602aba04ec451e97662f48b0a4James Dong */
253760943b5e7a09b602aba04ec451e97662f48b0a4James Dongstatus_t AACWriter::writeAdtsHeader(uint32_t frameLength) {
254760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    uint8_t data = 0xFF;
255760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    write(mFd, &data, 1);
256760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
257760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    const uint8_t kFieldId = 0;
258760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    const uint8_t kMpegLayer = 0;
259760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    const uint8_t kProtectionAbsense = 1;  // 1: kAdtsHeaderLength = 7
260760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    data = 0xF0;
261760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    data |= (kFieldId << 3);
262760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    data |= (kMpegLayer << 1);
263760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    data |= kProtectionAbsense;
264760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    write(mFd, &data, 1);
265760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
266aeb8fd460ed87d032b3fb8bb61e21eb542ce0f5bDave Burke    const uint8_t kProfileCode = mAACProfile - 1;
267760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    uint8_t kSampleFreqIndex;
268760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    CHECK(getSampleRateTableIndex(mSampleRate, &kSampleFreqIndex));
269760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    const uint8_t kPrivateStream = 0;
270760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    const uint8_t kChannelConfigCode = mChannelCount;
271760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    data = (kProfileCode << 6);
272760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    data |= (kSampleFreqIndex << 2);
273760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    data |= (kPrivateStream << 1);
274760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    data |= (kChannelConfigCode >> 2);
275760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    write(mFd, &data, 1);
276760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
277760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    // 4 bits from originality to copyright start
278760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    const uint8_t kCopyright = 0;
279760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    const uint32_t kFrameLength = frameLength;
280760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    data = ((kChannelConfigCode & 3) << 6);
281760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    data |= (kCopyright << 2);
282760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    data |= ((kFrameLength & 0x1800) >> 11);
283760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    write(mFd, &data, 1);
284760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
285760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    data = ((kFrameLength & 0x07F8) >> 3);
286760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    write(mFd, &data, 1);
287760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
288760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    const uint32_t kBufferFullness = 0x7FF;  // VBR
289760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    data = ((kFrameLength & 0x07) << 5);
290760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    data |= ((kBufferFullness & 0x07C0) >> 6);
291760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    write(mFd, &data, 1);
292760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
293760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    const uint8_t kFrameCount = 0;
294760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    data = ((kBufferFullness & 0x03F) << 2);
295760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    data |= kFrameCount;
296760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    write(mFd, &data, 1);
297760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
298760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    return OK;
299760943b5e7a09b602aba04ec451e97662f48b0a4James Dong}
300760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
301760943b5e7a09b602aba04ec451e97662f48b0a4James Dongstatus_t AACWriter::threadFunc() {
302760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    mEstimatedDurationUs = 0;
303760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    mEstimatedSizeBytes = 0;
304760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    int64_t previousPausedDurationUs = 0;
305760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    int64_t maxTimestampUs = 0;
306760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    status_t err = OK;
307fa514f007bd144eb99cdd68f2fe5302a4508db28James Dong    bool stoppedPrematurely = true;
308760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
309760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    prctl(PR_SET_NAME, (unsigned long)"AACWriterThread", 0, 0, 0);
310760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
311760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    while (!mDone && err == OK) {
312760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        MediaBuffer *buffer;
313760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        err = mSource->read(&buffer);
314760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
315760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        if (err != OK) {
316760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            break;
317760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        }
318760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
319760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        if (mPaused) {
320760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            buffer->release();
321760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            buffer = NULL;
322760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            continue;
323760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        }
324760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
325760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        mEstimatedSizeBytes += kAdtsHeaderLength + buffer->range_length();
326760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        if (exceedsFileSizeLimit()) {
327760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            buffer->release();
328760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            buffer = NULL;
329760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED, 0);
330760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            break;
331760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        }
332760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
333760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        int32_t isCodecSpecific = 0;
334760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        if (buffer->meta_data()->findInt32(kKeyIsCodecConfig, &isCodecSpecific) && isCodecSpecific) {
3353856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("Drop codec specific info buffer");
336760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            buffer->release();
337760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            buffer = NULL;
338760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            continue;
339760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        }
340760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
341760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        int64_t timestampUs;
342760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        CHECK(buffer->meta_data()->findInt64(kKeyTime, &timestampUs));
343760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        if (timestampUs > mEstimatedDurationUs) {
344760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            mEstimatedDurationUs = timestampUs;
345760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        }
346760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        if (mResumed) {
347760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            previousPausedDurationUs += (timestampUs - maxTimestampUs - mFrameDurationUs);
348760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            mResumed = false;
349760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        }
350760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        timestampUs -= previousPausedDurationUs;
3513856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("time stamp: %lld, previous paused duration: %lld",
352760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            timestampUs, previousPausedDurationUs);
353760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        if (timestampUs > maxTimestampUs) {
354760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            maxTimestampUs = timestampUs;
355760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        }
356760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
357760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        if (exceedsFileDurationLimit()) {
358760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            buffer->release();
359760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            buffer = NULL;
360760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_DURATION_REACHED, 0);
361760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            break;
362760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        }
363760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
364760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        // Each output AAC audio frame to the file contains
365760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        // 1. an ADTS header, followed by
366760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        // 2. the compressed audio data.
367760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        ssize_t dataLength = buffer->range_length();
368760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        uint8_t *data = (uint8_t *)buffer->data() + buffer->range_offset();
369760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        if (writeAdtsHeader(kAdtsHeaderLength + dataLength) != OK ||
370760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            dataLength != write(mFd, data, dataLength)) {
371760943b5e7a09b602aba04ec451e97662f48b0a4James Dong            err = ERROR_IO;
372760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        }
373760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
374760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        buffer->release();
375760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        buffer = NULL;
376fa514f007bd144eb99cdd68f2fe5302a4508db28James Dong
377fa514f007bd144eb99cdd68f2fe5302a4508db28James Dong        if (err != OK) {
378fa514f007bd144eb99cdd68f2fe5302a4508db28James Dong            break;
379fa514f007bd144eb99cdd68f2fe5302a4508db28James Dong        }
380fa514f007bd144eb99cdd68f2fe5302a4508db28James Dong
381fa514f007bd144eb99cdd68f2fe5302a4508db28James Dong        if (stoppedPrematurely) {
382fa514f007bd144eb99cdd68f2fe5302a4508db28James Dong            stoppedPrematurely = false;
383fa514f007bd144eb99cdd68f2fe5302a4508db28James Dong        }
384fa514f007bd144eb99cdd68f2fe5302a4508db28James Dong    }
385fa514f007bd144eb99cdd68f2fe5302a4508db28James Dong
386fa514f007bd144eb99cdd68f2fe5302a4508db28James Dong    if ((err == OK || err == ERROR_END_OF_STREAM) && stoppedPrematurely) {
387fa514f007bd144eb99cdd68f2fe5302a4508db28James Dong        err = ERROR_MALFORMED;
388760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    }
389760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
390760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    close(mFd);
391760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    mFd = -1;
392760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    mReachedEOS = true;
393760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    if (err == ERROR_END_OF_STREAM) {
394760943b5e7a09b602aba04ec451e97662f48b0a4James Dong        return OK;
395760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    }
396760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    return err;
397760943b5e7a09b602aba04ec451e97662f48b0a4James Dong}
398760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
399760943b5e7a09b602aba04ec451e97662f48b0a4James Dongbool AACWriter::reachedEOS() {
400760943b5e7a09b602aba04ec451e97662f48b0a4James Dong    return mReachedEOS;
401760943b5e7a09b602aba04ec451e97662f48b0a4James Dong}
402760943b5e7a09b602aba04ec451e97662f48b0a4James Dong
403760943b5e7a09b602aba04ec451e97662f48b0a4James Dong}  // namespace android
404