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