AACWriter.cpp revision 29357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47
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 21760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <media/stagefright/AACWriter.h> 22760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <media/stagefright/MediaBuffer.h> 23760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <media/stagefright/foundation/ADebug.h> 24760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <media/stagefright/MediaDefs.h> 25760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <media/stagefright/MediaErrors.h> 26760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <media/stagefright/MediaSource.h> 27760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <media/stagefright/MetaData.h> 28760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <media/mediarecorder.h> 29760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <sys/prctl.h> 30760943b5e7a09b602aba04ec451e97662f48b0a4James Dong#include <fcntl.h> 31760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 32760943b5e7a09b602aba04ec451e97662f48b0a4James Dongnamespace android { 33760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 34760943b5e7a09b602aba04ec451e97662f48b0a4James DongAACWriter::AACWriter(const char *filename) 35760943b5e7a09b602aba04ec451e97662f48b0a4James Dong : mFd(-1), 36760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mInitCheck(NO_INIT), 37760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mStarted(false), 38760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mPaused(false), 39760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mResumed(false), 40760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mChannelCount(-1), 41760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mSampleRate(-1) { 42760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 433856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("AACWriter Constructor"); 44760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 45760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mFd = open(filename, O_CREAT | O_LARGEFILE | O_TRUNC | O_RDWR); 46760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (mFd >= 0) { 47760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mInitCheck = OK; 48760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 49760943b5e7a09b602aba04ec451e97662f48b0a4James Dong} 50760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 51760943b5e7a09b602aba04ec451e97662f48b0a4James DongAACWriter::AACWriter(int fd) 52760943b5e7a09b602aba04ec451e97662f48b0a4James Dong : mFd(dup(fd)), 53760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mInitCheck(mFd < 0? NO_INIT: OK), 54760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mStarted(false), 55760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mPaused(false), 56760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mResumed(false), 57760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mChannelCount(-1), 58760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mSampleRate(-1) { 59760943b5e7a09b602aba04ec451e97662f48b0a4James Dong} 60760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 61760943b5e7a09b602aba04ec451e97662f48b0a4James DongAACWriter::~AACWriter() { 62760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (mStarted) { 63760943b5e7a09b602aba04ec451e97662f48b0a4James Dong stop(); 64760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 65760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 66760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (mFd != -1) { 67760943b5e7a09b602aba04ec451e97662f48b0a4James Dong close(mFd); 68760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mFd = -1; 69760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 70760943b5e7a09b602aba04ec451e97662f48b0a4James Dong} 71760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 72760943b5e7a09b602aba04ec451e97662f48b0a4James Dongstatus_t AACWriter::initCheck() const { 73760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return mInitCheck; 74760943b5e7a09b602aba04ec451e97662f48b0a4James Dong} 75760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 76760943b5e7a09b602aba04ec451e97662f48b0a4James Dongstatic int writeInt8(int fd, uint8_t x) { 77760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return ::write(fd, &x, 1); 78760943b5e7a09b602aba04ec451e97662f48b0a4James Dong} 79760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 80760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 81760943b5e7a09b602aba04ec451e97662f48b0a4James Dongstatus_t AACWriter::addSource(const sp<MediaSource> &source) { 82760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (mInitCheck != OK) { 83760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return mInitCheck; 84760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 85760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 86760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (mSource != NULL) { 8729357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("AAC files only support a single track of audio."); 88760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return UNKNOWN_ERROR; 89760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 90760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 91760943b5e7a09b602aba04ec451e97662f48b0a4James Dong sp<MetaData> meta = source->getFormat(); 92760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 93760943b5e7a09b602aba04ec451e97662f48b0a4James Dong const char *mime; 94760943b5e7a09b602aba04ec451e97662f48b0a4James Dong CHECK(meta->findCString(kKeyMIMEType, &mime)); 95760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 96760943b5e7a09b602aba04ec451e97662f48b0a4James Dong CHECK(!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC)); 97760943b5e7a09b602aba04ec451e97662f48b0a4James Dong CHECK(meta->findInt32(kKeyChannelCount, &mChannelCount)); 98760943b5e7a09b602aba04ec451e97662f48b0a4James Dong CHECK(meta->findInt32(kKeySampleRate, &mSampleRate)); 99760943b5e7a09b602aba04ec451e97662f48b0a4James Dong CHECK(mChannelCount >= 1 && mChannelCount <= 2); 100760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 101760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mSource = source; 102760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return OK; 103760943b5e7a09b602aba04ec451e97662f48b0a4James Dong} 104760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 105760943b5e7a09b602aba04ec451e97662f48b0a4James Dongstatus_t AACWriter::start(MetaData *params) { 106760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (mInitCheck != OK) { 107760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return mInitCheck; 108760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 109760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 110760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (mSource == NULL) { 111760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return UNKNOWN_ERROR; 112760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 113760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 114760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (mStarted && mPaused) { 115760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mPaused = false; 116760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mResumed = true; 117760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return OK; 118760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } else if (mStarted) { 119760943b5e7a09b602aba04ec451e97662f48b0a4James Dong // Already started, does nothing 120760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return OK; 121760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 122760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 123760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mFrameDurationUs = (kSamplesPerFrame * 1000000LL + (mSampleRate >> 1)) 124760943b5e7a09b602aba04ec451e97662f48b0a4James Dong / mSampleRate; 125760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 126760943b5e7a09b602aba04ec451e97662f48b0a4James Dong status_t err = mSource->start(); 127760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 128760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (err != OK) { 129760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return err; 130760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 131760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 132760943b5e7a09b602aba04ec451e97662f48b0a4James Dong pthread_attr_t attr; 133760943b5e7a09b602aba04ec451e97662f48b0a4James Dong pthread_attr_init(&attr); 134760943b5e7a09b602aba04ec451e97662f48b0a4James Dong pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); 135760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 136760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mReachedEOS = false; 137760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mDone = false; 138760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 139760943b5e7a09b602aba04ec451e97662f48b0a4James Dong pthread_create(&mThread, &attr, ThreadWrapper, this); 140760943b5e7a09b602aba04ec451e97662f48b0a4James Dong pthread_attr_destroy(&attr); 141760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 142760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mStarted = true; 143760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 144760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return OK; 145760943b5e7a09b602aba04ec451e97662f48b0a4James Dong} 146760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 147760943b5e7a09b602aba04ec451e97662f48b0a4James Dongstatus_t AACWriter::pause() { 148760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (!mStarted) { 149760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return OK; 150760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 151760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mPaused = true; 152760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return OK; 153760943b5e7a09b602aba04ec451e97662f48b0a4James Dong} 154760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 155760943b5e7a09b602aba04ec451e97662f48b0a4James Dongstatus_t AACWriter::stop() { 156760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (!mStarted) { 157760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return OK; 158760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 159760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 160760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mDone = true; 161760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 162760943b5e7a09b602aba04ec451e97662f48b0a4James Dong void *dummy; 163760943b5e7a09b602aba04ec451e97662f48b0a4James Dong pthread_join(mThread, &dummy); 164760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 165760943b5e7a09b602aba04ec451e97662f48b0a4James Dong status_t err = (status_t) dummy; 166760943b5e7a09b602aba04ec451e97662f48b0a4James Dong { 167760943b5e7a09b602aba04ec451e97662f48b0a4James Dong status_t status = mSource->stop(); 168760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (err == OK && 169760943b5e7a09b602aba04ec451e97662f48b0a4James Dong (status != OK && status != ERROR_END_OF_STREAM)) { 170760943b5e7a09b602aba04ec451e97662f48b0a4James Dong err = status; 171760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 172760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 173760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 174760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mStarted = false; 175760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return err; 176760943b5e7a09b602aba04ec451e97662f48b0a4James Dong} 177760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 178760943b5e7a09b602aba04ec451e97662f48b0a4James Dongbool AACWriter::exceedsFileSizeLimit() { 179760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (mMaxFileSizeLimitBytes == 0) { 180760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return false; 181760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 182760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return mEstimatedSizeBytes >= mMaxFileSizeLimitBytes; 183760943b5e7a09b602aba04ec451e97662f48b0a4James Dong} 184760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 185760943b5e7a09b602aba04ec451e97662f48b0a4James Dongbool AACWriter::exceedsFileDurationLimit() { 186760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (mMaxFileDurationLimitUs == 0) { 187760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return false; 188760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 189760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return mEstimatedDurationUs >= mMaxFileDurationLimitUs; 190760943b5e7a09b602aba04ec451e97662f48b0a4James Dong} 191760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 192760943b5e7a09b602aba04ec451e97662f48b0a4James Dong// static 193760943b5e7a09b602aba04ec451e97662f48b0a4James Dongvoid *AACWriter::ThreadWrapper(void *me) { 194760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return (void *) static_cast<AACWriter *>(me)->threadFunc(); 195760943b5e7a09b602aba04ec451e97662f48b0a4James Dong} 196760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 197760943b5e7a09b602aba04ec451e97662f48b0a4James Dong/* 198760943b5e7a09b602aba04ec451e97662f48b0a4James Dong* Returns an index into the sample rate table if the 199760943b5e7a09b602aba04ec451e97662f48b0a4James Dong* given sample rate is found; otherwise, returns -1. 200760943b5e7a09b602aba04ec451e97662f48b0a4James Dong*/ 201760943b5e7a09b602aba04ec451e97662f48b0a4James Dongstatic bool getSampleRateTableIndex(int sampleRate, uint8_t* tableIndex) { 202760943b5e7a09b602aba04ec451e97662f48b0a4James Dong static const int kSampleRateTable[] = { 203760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 96000, 88200, 64000, 48000, 44100, 32000, 204760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 24000, 22050, 16000, 12000, 11025, 8000 205760943b5e7a09b602aba04ec451e97662f48b0a4James Dong }; 206760943b5e7a09b602aba04ec451e97662f48b0a4James Dong const int tableSize = 207760943b5e7a09b602aba04ec451e97662f48b0a4James Dong sizeof(kSampleRateTable) / sizeof(kSampleRateTable[0]); 208760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 209760943b5e7a09b602aba04ec451e97662f48b0a4James Dong *tableIndex = 0; 210760943b5e7a09b602aba04ec451e97662f48b0a4James Dong for (int index = 0; index < tableSize; ++index) { 211760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (sampleRate == kSampleRateTable[index]) { 2123856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("Sample rate: %d and index: %d", 213760943b5e7a09b602aba04ec451e97662f48b0a4James Dong sampleRate, index); 214760943b5e7a09b602aba04ec451e97662f48b0a4James Dong *tableIndex = index; 215760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return true; 216760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 217760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 218760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 21929357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("Sampling rate %d bps is not supported", sampleRate); 220760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return false; 221760943b5e7a09b602aba04ec451e97662f48b0a4James Dong} 222760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 223760943b5e7a09b602aba04ec451e97662f48b0a4James Dong/* 224760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * ADTS (Audio data transport stream) header structure. 225760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * It consists of 7 or 9 bytes (with or without CRC): 226760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 12 bits of syncword 0xFFF, all bits must be 1 227760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 1 bit of field ID. 0 for MPEG-4, and 1 for MPEG-2 228760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 2 bits of MPEG layer. If in MPEG-TS, set to 0 229760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 1 bit of protection absense. Set to 1 if no CRC. 230760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 2 bits of profile code. Set to 1 (The MPEG-4 Audio 231760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * object type minus 1. We are using AAC-LC = 2) 232760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 4 bits of sampling frequency index code (15 is not allowed) 233760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 1 bit of private stream. Set to 0. 234760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 3 bits of channel configuration code. 0 resevered for inband PCM 235760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 1 bit of originality. Set to 0. 236760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 1 bit of home. Set to 0. 237760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 1 bit of copyrighted steam. Set to 0. 238760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 1 bit of copyright start. Set to 0. 239760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 13 bits of frame length. It included 7 ot 9 bytes header length. 240760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * it is set to (protection absense? 7: 9) + size(AAC frame) 241760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 11 bits of buffer fullness. 0x7FF for VBR. 242760943b5e7a09b602aba04ec451e97662f48b0a4James Dong * 2 bits of frames count in one packet. Set to 0. 243760943b5e7a09b602aba04ec451e97662f48b0a4James Dong */ 244760943b5e7a09b602aba04ec451e97662f48b0a4James Dongstatus_t AACWriter::writeAdtsHeader(uint32_t frameLength) { 245760943b5e7a09b602aba04ec451e97662f48b0a4James Dong uint8_t data = 0xFF; 246760943b5e7a09b602aba04ec451e97662f48b0a4James Dong write(mFd, &data, 1); 247760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 248760943b5e7a09b602aba04ec451e97662f48b0a4James Dong const uint8_t kFieldId = 0; 249760943b5e7a09b602aba04ec451e97662f48b0a4James Dong const uint8_t kMpegLayer = 0; 250760943b5e7a09b602aba04ec451e97662f48b0a4James Dong const uint8_t kProtectionAbsense = 1; // 1: kAdtsHeaderLength = 7 251760943b5e7a09b602aba04ec451e97662f48b0a4James Dong data = 0xF0; 252760943b5e7a09b602aba04ec451e97662f48b0a4James Dong data |= (kFieldId << 3); 253760943b5e7a09b602aba04ec451e97662f48b0a4James Dong data |= (kMpegLayer << 1); 254760943b5e7a09b602aba04ec451e97662f48b0a4James Dong data |= kProtectionAbsense; 255760943b5e7a09b602aba04ec451e97662f48b0a4James Dong write(mFd, &data, 1); 256760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 257760943b5e7a09b602aba04ec451e97662f48b0a4James Dong const uint8_t kProfileCode = 1; // AAC-LC 258760943b5e7a09b602aba04ec451e97662f48b0a4James Dong uint8_t kSampleFreqIndex; 259760943b5e7a09b602aba04ec451e97662f48b0a4James Dong CHECK(getSampleRateTableIndex(mSampleRate, &kSampleFreqIndex)); 260760943b5e7a09b602aba04ec451e97662f48b0a4James Dong const uint8_t kPrivateStream = 0; 261760943b5e7a09b602aba04ec451e97662f48b0a4James Dong const uint8_t kChannelConfigCode = mChannelCount; 262760943b5e7a09b602aba04ec451e97662f48b0a4James Dong data = (kProfileCode << 6); 263760943b5e7a09b602aba04ec451e97662f48b0a4James Dong data |= (kSampleFreqIndex << 2); 264760943b5e7a09b602aba04ec451e97662f48b0a4James Dong data |= (kPrivateStream << 1); 265760943b5e7a09b602aba04ec451e97662f48b0a4James Dong data |= (kChannelConfigCode >> 2); 266760943b5e7a09b602aba04ec451e97662f48b0a4James Dong write(mFd, &data, 1); 267760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 268760943b5e7a09b602aba04ec451e97662f48b0a4James Dong // 4 bits from originality to copyright start 269760943b5e7a09b602aba04ec451e97662f48b0a4James Dong const uint8_t kCopyright = 0; 270760943b5e7a09b602aba04ec451e97662f48b0a4James Dong const uint32_t kFrameLength = frameLength; 271760943b5e7a09b602aba04ec451e97662f48b0a4James Dong data = ((kChannelConfigCode & 3) << 6); 272760943b5e7a09b602aba04ec451e97662f48b0a4James Dong data |= (kCopyright << 2); 273760943b5e7a09b602aba04ec451e97662f48b0a4James Dong data |= ((kFrameLength & 0x1800) >> 11); 274760943b5e7a09b602aba04ec451e97662f48b0a4James Dong write(mFd, &data, 1); 275760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 276760943b5e7a09b602aba04ec451e97662f48b0a4James Dong data = ((kFrameLength & 0x07F8) >> 3); 277760943b5e7a09b602aba04ec451e97662f48b0a4James Dong write(mFd, &data, 1); 278760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 279760943b5e7a09b602aba04ec451e97662f48b0a4James Dong const uint32_t kBufferFullness = 0x7FF; // VBR 280760943b5e7a09b602aba04ec451e97662f48b0a4James Dong data = ((kFrameLength & 0x07) << 5); 281760943b5e7a09b602aba04ec451e97662f48b0a4James Dong data |= ((kBufferFullness & 0x07C0) >> 6); 282760943b5e7a09b602aba04ec451e97662f48b0a4James Dong write(mFd, &data, 1); 283760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 284760943b5e7a09b602aba04ec451e97662f48b0a4James Dong const uint8_t kFrameCount = 0; 285760943b5e7a09b602aba04ec451e97662f48b0a4James Dong data = ((kBufferFullness & 0x03F) << 2); 286760943b5e7a09b602aba04ec451e97662f48b0a4James Dong data |= kFrameCount; 287760943b5e7a09b602aba04ec451e97662f48b0a4James Dong write(mFd, &data, 1); 288760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 289760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return OK; 290760943b5e7a09b602aba04ec451e97662f48b0a4James Dong} 291760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 292760943b5e7a09b602aba04ec451e97662f48b0a4James Dongstatus_t AACWriter::threadFunc() { 293760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mEstimatedDurationUs = 0; 294760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mEstimatedSizeBytes = 0; 295760943b5e7a09b602aba04ec451e97662f48b0a4James Dong int64_t previousPausedDurationUs = 0; 296760943b5e7a09b602aba04ec451e97662f48b0a4James Dong int64_t maxTimestampUs = 0; 297760943b5e7a09b602aba04ec451e97662f48b0a4James Dong status_t err = OK; 298760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 299760943b5e7a09b602aba04ec451e97662f48b0a4James Dong prctl(PR_SET_NAME, (unsigned long)"AACWriterThread", 0, 0, 0); 300760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 301760943b5e7a09b602aba04ec451e97662f48b0a4James Dong while (!mDone && err == OK) { 302760943b5e7a09b602aba04ec451e97662f48b0a4James Dong MediaBuffer *buffer; 303760943b5e7a09b602aba04ec451e97662f48b0a4James Dong err = mSource->read(&buffer); 304760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 305760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (err != OK) { 306760943b5e7a09b602aba04ec451e97662f48b0a4James Dong break; 307760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 308760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 309760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (mPaused) { 310760943b5e7a09b602aba04ec451e97662f48b0a4James Dong buffer->release(); 311760943b5e7a09b602aba04ec451e97662f48b0a4James Dong buffer = NULL; 312760943b5e7a09b602aba04ec451e97662f48b0a4James Dong continue; 313760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 314760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 315760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mEstimatedSizeBytes += kAdtsHeaderLength + buffer->range_length(); 316760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (exceedsFileSizeLimit()) { 317760943b5e7a09b602aba04ec451e97662f48b0a4James Dong buffer->release(); 318760943b5e7a09b602aba04ec451e97662f48b0a4James Dong buffer = NULL; 319760943b5e7a09b602aba04ec451e97662f48b0a4James Dong notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED, 0); 320760943b5e7a09b602aba04ec451e97662f48b0a4James Dong break; 321760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 322760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 323760943b5e7a09b602aba04ec451e97662f48b0a4James Dong int32_t isCodecSpecific = 0; 324760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (buffer->meta_data()->findInt32(kKeyIsCodecConfig, &isCodecSpecific) && isCodecSpecific) { 3253856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("Drop codec specific info buffer"); 326760943b5e7a09b602aba04ec451e97662f48b0a4James Dong buffer->release(); 327760943b5e7a09b602aba04ec451e97662f48b0a4James Dong buffer = NULL; 328760943b5e7a09b602aba04ec451e97662f48b0a4James Dong continue; 329760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 330760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 331760943b5e7a09b602aba04ec451e97662f48b0a4James Dong int64_t timestampUs; 332760943b5e7a09b602aba04ec451e97662f48b0a4James Dong CHECK(buffer->meta_data()->findInt64(kKeyTime, ×tampUs)); 333760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (timestampUs > mEstimatedDurationUs) { 334760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mEstimatedDurationUs = timestampUs; 335760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 336760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (mResumed) { 337760943b5e7a09b602aba04ec451e97662f48b0a4James Dong previousPausedDurationUs += (timestampUs - maxTimestampUs - mFrameDurationUs); 338760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mResumed = false; 339760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 340760943b5e7a09b602aba04ec451e97662f48b0a4James Dong timestampUs -= previousPausedDurationUs; 3413856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("time stamp: %lld, previous paused duration: %lld", 342760943b5e7a09b602aba04ec451e97662f48b0a4James Dong timestampUs, previousPausedDurationUs); 343760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (timestampUs > maxTimestampUs) { 344760943b5e7a09b602aba04ec451e97662f48b0a4James Dong maxTimestampUs = timestampUs; 345760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 346760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 347760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (exceedsFileDurationLimit()) { 348760943b5e7a09b602aba04ec451e97662f48b0a4James Dong buffer->release(); 349760943b5e7a09b602aba04ec451e97662f48b0a4James Dong buffer = NULL; 350760943b5e7a09b602aba04ec451e97662f48b0a4James Dong notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_DURATION_REACHED, 0); 351760943b5e7a09b602aba04ec451e97662f48b0a4James Dong break; 352760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 353760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 354760943b5e7a09b602aba04ec451e97662f48b0a4James Dong // Each output AAC audio frame to the file contains 355760943b5e7a09b602aba04ec451e97662f48b0a4James Dong // 1. an ADTS header, followed by 356760943b5e7a09b602aba04ec451e97662f48b0a4James Dong // 2. the compressed audio data. 357760943b5e7a09b602aba04ec451e97662f48b0a4James Dong ssize_t dataLength = buffer->range_length(); 358760943b5e7a09b602aba04ec451e97662f48b0a4James Dong uint8_t *data = (uint8_t *)buffer->data() + buffer->range_offset(); 359760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (writeAdtsHeader(kAdtsHeaderLength + dataLength) != OK || 360760943b5e7a09b602aba04ec451e97662f48b0a4James Dong dataLength != write(mFd, data, dataLength)) { 361760943b5e7a09b602aba04ec451e97662f48b0a4James Dong err = ERROR_IO; 362760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 363760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 364760943b5e7a09b602aba04ec451e97662f48b0a4James Dong buffer->release(); 365760943b5e7a09b602aba04ec451e97662f48b0a4James Dong buffer = NULL; 366760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 367760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 368760943b5e7a09b602aba04ec451e97662f48b0a4James Dong close(mFd); 369760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mFd = -1; 370760943b5e7a09b602aba04ec451e97662f48b0a4James Dong mReachedEOS = true; 371760943b5e7a09b602aba04ec451e97662f48b0a4James Dong if (err == ERROR_END_OF_STREAM) { 372760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return OK; 373760943b5e7a09b602aba04ec451e97662f48b0a4James Dong } 374760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return err; 375760943b5e7a09b602aba04ec451e97662f48b0a4James Dong} 376760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 377760943b5e7a09b602aba04ec451e97662f48b0a4James Dongbool AACWriter::reachedEOS() { 378760943b5e7a09b602aba04ec451e97662f48b0a4James Dong return mReachedEOS; 379760943b5e7a09b602aba04ec451e97662f48b0a4James Dong} 380760943b5e7a09b602aba04ec451e97662f48b0a4James Dong 381760943b5e7a09b602aba04ec451e97662f48b0a4James Dong} // namespace android 382