James Dong | f84bfab | 2011-03-21 14:29:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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_NDEBUG 0 |
| 18 | #define LOG_TAG "AACWriter" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <media/stagefright/AACWriter.h> |
| 22 | #include <media/stagefright/MediaBuffer.h> |
| 23 | #include <media/stagefright/foundation/ADebug.h> |
| 24 | #include <media/stagefright/MediaDefs.h> |
| 25 | #include <media/stagefright/MediaErrors.h> |
| 26 | #include <media/stagefright/MediaSource.h> |
| 27 | #include <media/stagefright/MetaData.h> |
| 28 | #include <media/mediarecorder.h> |
| 29 | #include <sys/prctl.h> |
| 30 | #include <sys/resource.h> |
| 31 | #include <fcntl.h> |
| 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | AACWriter::AACWriter(const char *filename) |
| 36 | : mFd(-1), |
| 37 | mInitCheck(NO_INIT), |
| 38 | mStarted(false), |
| 39 | mPaused(false), |
| 40 | mResumed(false), |
| 41 | mChannelCount(-1), |
| 42 | mSampleRate(-1) { |
| 43 | |
| 44 | LOGV("AACWriter Constructor"); |
| 45 | |
| 46 | mFd = open(filename, O_CREAT | O_LARGEFILE | O_TRUNC | O_RDWR); |
| 47 | if (mFd >= 0) { |
| 48 | mInitCheck = OK; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | AACWriter::AACWriter(int fd) |
| 53 | : mFd(dup(fd)), |
| 54 | mInitCheck(mFd < 0? NO_INIT: OK), |
| 55 | mStarted(false), |
| 56 | mPaused(false), |
| 57 | mResumed(false), |
| 58 | mChannelCount(-1), |
| 59 | mSampleRate(-1) { |
| 60 | } |
| 61 | |
| 62 | AACWriter::~AACWriter() { |
| 63 | if (mStarted) { |
| 64 | stop(); |
| 65 | } |
| 66 | |
| 67 | if (mFd != -1) { |
| 68 | close(mFd); |
| 69 | mFd = -1; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | status_t AACWriter::initCheck() const { |
| 74 | return mInitCheck; |
| 75 | } |
| 76 | |
| 77 | static int writeInt8(int fd, uint8_t x) { |
| 78 | return ::write(fd, &x, 1); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | status_t AACWriter::addSource(const sp<MediaSource> &source) { |
| 83 | if (mInitCheck != OK) { |
| 84 | return mInitCheck; |
| 85 | } |
| 86 | |
| 87 | if (mSource != NULL) { |
| 88 | LOGE("AAC files only support a single track of audio."); |
| 89 | return UNKNOWN_ERROR; |
| 90 | } |
| 91 | |
| 92 | sp<MetaData> meta = source->getFormat(); |
| 93 | |
| 94 | const char *mime; |
| 95 | CHECK(meta->findCString(kKeyMIMEType, &mime)); |
| 96 | |
| 97 | CHECK(!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC)); |
| 98 | CHECK(meta->findInt32(kKeyChannelCount, &mChannelCount)); |
| 99 | CHECK(meta->findInt32(kKeySampleRate, &mSampleRate)); |
| 100 | CHECK(mChannelCount >= 1 && mChannelCount <= 2); |
| 101 | |
| 102 | mSource = source; |
| 103 | return OK; |
| 104 | } |
| 105 | |
| 106 | status_t AACWriter::start(MetaData *params) { |
| 107 | if (mInitCheck != OK) { |
| 108 | return mInitCheck; |
| 109 | } |
| 110 | |
| 111 | if (mSource == NULL) { |
| 112 | return UNKNOWN_ERROR; |
| 113 | } |
| 114 | |
| 115 | if (mStarted && mPaused) { |
| 116 | mPaused = false; |
| 117 | mResumed = true; |
| 118 | return OK; |
| 119 | } else if (mStarted) { |
| 120 | // Already started, does nothing |
| 121 | return OK; |
| 122 | } |
| 123 | |
| 124 | mFrameDurationUs = (kSamplesPerFrame * 1000000LL + (mSampleRate >> 1)) |
| 125 | / mSampleRate; |
| 126 | |
| 127 | status_t err = mSource->start(); |
| 128 | |
| 129 | if (err != OK) { |
| 130 | return err; |
| 131 | } |
| 132 | |
| 133 | pthread_attr_t attr; |
| 134 | pthread_attr_init(&attr); |
| 135 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
| 136 | |
| 137 | mReachedEOS = false; |
| 138 | mDone = false; |
| 139 | |
| 140 | pthread_create(&mThread, &attr, ThreadWrapper, this); |
| 141 | pthread_attr_destroy(&attr); |
| 142 | |
| 143 | mStarted = true; |
| 144 | |
| 145 | return OK; |
| 146 | } |
| 147 | |
| 148 | status_t AACWriter::pause() { |
| 149 | if (!mStarted) { |
| 150 | return OK; |
| 151 | } |
| 152 | mPaused = true; |
| 153 | return OK; |
| 154 | } |
| 155 | |
| 156 | status_t AACWriter::stop() { |
| 157 | if (!mStarted) { |
| 158 | return OK; |
| 159 | } |
| 160 | |
| 161 | mDone = true; |
| 162 | |
| 163 | void *dummy; |
| 164 | pthread_join(mThread, &dummy); |
| 165 | |
| 166 | status_t err = (status_t) dummy; |
| 167 | { |
| 168 | status_t status = mSource->stop(); |
| 169 | if (err == OK && |
| 170 | (status != OK && status != ERROR_END_OF_STREAM)) { |
| 171 | err = status; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | mStarted = false; |
| 176 | return err; |
| 177 | } |
| 178 | |
| 179 | bool AACWriter::exceedsFileSizeLimit() { |
| 180 | if (mMaxFileSizeLimitBytes == 0) { |
| 181 | return false; |
| 182 | } |
| 183 | return mEstimatedSizeBytes >= mMaxFileSizeLimitBytes; |
| 184 | } |
| 185 | |
| 186 | bool AACWriter::exceedsFileDurationLimit() { |
| 187 | if (mMaxFileDurationLimitUs == 0) { |
| 188 | return false; |
| 189 | } |
| 190 | return mEstimatedDurationUs >= mMaxFileDurationLimitUs; |
| 191 | } |
| 192 | |
| 193 | // static |
| 194 | void *AACWriter::ThreadWrapper(void *me) { |
| 195 | return (void *) static_cast<AACWriter *>(me)->threadFunc(); |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Returns an index into the sample rate table if the |
| 200 | * given sample rate is found; otherwise, returns -1. |
| 201 | */ |
| 202 | static bool getSampleRateTableIndex(int sampleRate, uint8_t* tableIndex) { |
| 203 | static const int kSampleRateTable[] = { |
| 204 | 96000, 88200, 64000, 48000, 44100, 32000, |
| 205 | 24000, 22050, 16000, 12000, 11025, 8000 |
| 206 | }; |
| 207 | const int tableSize = |
| 208 | sizeof(kSampleRateTable) / sizeof(kSampleRateTable[0]); |
| 209 | |
| 210 | *tableIndex = 0; |
| 211 | for (int index = 0; index < tableSize; ++index) { |
| 212 | if (sampleRate == kSampleRateTable[index]) { |
| 213 | LOGV("Sample rate: %d and index: %d", |
| 214 | sampleRate, index); |
| 215 | *tableIndex = index; |
| 216 | return true; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | LOGE("Sampling rate %d bps is not supported", sampleRate); |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * ADTS (Audio data transport stream) header structure. |
| 226 | * It consists of 7 or 9 bytes (with or without CRC): |
| 227 | * 12 bits of syncword 0xFFF, all bits must be 1 |
| 228 | * 1 bit of field ID. 0 for MPEG-4, and 1 for MPEG-2 |
| 229 | * 2 bits of MPEG layer. If in MPEG-TS, set to 0 |
| 230 | * 1 bit of protection absense. Set to 1 if no CRC. |
| 231 | * 2 bits of profile code. Set to 1 (The MPEG-4 Audio |
| 232 | * object type minus 1. We are using AAC-LC = 2) |
| 233 | * 4 bits of sampling frequency index code (15 is not allowed) |
| 234 | * 1 bit of private stream. Set to 0. |
| 235 | * 3 bits of channel configuration code. 0 resevered for inband PCM |
| 236 | * 1 bit of originality. Set to 0. |
| 237 | * 1 bit of home. Set to 0. |
| 238 | * 1 bit of copyrighted steam. Set to 0. |
| 239 | * 1 bit of copyright start. Set to 0. |
| 240 | * 13 bits of frame length. It included 7 ot 9 bytes header length. |
| 241 | * it is set to (protection absense? 7: 9) + size(AAC frame) |
| 242 | * 11 bits of buffer fullness. 0x7FF for VBR. |
| 243 | * 2 bits of frames count in one packet. Set to 0. |
| 244 | */ |
| 245 | status_t AACWriter::writeAdtsHeader(uint32_t frameLength) { |
| 246 | uint8_t data = 0xFF; |
| 247 | write(mFd, &data, 1); |
| 248 | |
| 249 | const uint8_t kFieldId = 0; |
| 250 | const uint8_t kMpegLayer = 0; |
| 251 | const uint8_t kProtectionAbsense = 1; // 1: kAdtsHeaderLength = 7 |
| 252 | data = 0xF0; |
| 253 | data |= (kFieldId << 3); |
| 254 | data |= (kMpegLayer << 1); |
| 255 | data |= kProtectionAbsense; |
| 256 | write(mFd, &data, 1); |
| 257 | |
| 258 | const uint8_t kProfileCode = 1; // AAC-LC |
| 259 | uint8_t kSampleFreqIndex; |
| 260 | CHECK(getSampleRateTableIndex(mSampleRate, &kSampleFreqIndex)); |
| 261 | const uint8_t kPrivateStream = 0; |
| 262 | const uint8_t kChannelConfigCode = mChannelCount; |
| 263 | data = (kProfileCode << 6); |
| 264 | data |= (kSampleFreqIndex << 2); |
| 265 | data |= (kPrivateStream << 1); |
| 266 | data |= (kChannelConfigCode >> 2); |
| 267 | write(mFd, &data, 1); |
| 268 | |
| 269 | // 4 bits from originality to copyright start |
| 270 | const uint8_t kCopyright = 0; |
| 271 | const uint32_t kFrameLength = frameLength; |
| 272 | data = ((kChannelConfigCode & 3) << 6); |
| 273 | data |= (kCopyright << 2); |
| 274 | data |= ((kFrameLength & 0x1800) >> 11); |
| 275 | write(mFd, &data, 1); |
| 276 | |
| 277 | data = ((kFrameLength & 0x07F8) >> 3); |
| 278 | write(mFd, &data, 1); |
| 279 | |
| 280 | const uint32_t kBufferFullness = 0x7FF; // VBR |
| 281 | data = ((kFrameLength & 0x07) << 5); |
| 282 | data |= ((kBufferFullness & 0x07C0) >> 6); |
| 283 | write(mFd, &data, 1); |
| 284 | |
| 285 | const uint8_t kFrameCount = 0; |
| 286 | data = ((kBufferFullness & 0x03F) << 2); |
| 287 | data |= kFrameCount; |
| 288 | write(mFd, &data, 1); |
| 289 | |
| 290 | return OK; |
| 291 | } |
| 292 | |
| 293 | status_t AACWriter::threadFunc() { |
| 294 | mEstimatedDurationUs = 0; |
| 295 | mEstimatedSizeBytes = 0; |
| 296 | int64_t previousPausedDurationUs = 0; |
| 297 | int64_t maxTimestampUs = 0; |
| 298 | status_t err = OK; |
| 299 | |
| 300 | prctl(PR_SET_NAME, (unsigned long)"AACWriterThread", 0, 0, 0); |
| 301 | |
| 302 | while (!mDone && err == OK) { |
| 303 | MediaBuffer *buffer; |
| 304 | err = mSource->read(&buffer); |
| 305 | |
| 306 | if (err != OK) { |
| 307 | break; |
| 308 | } |
| 309 | |
| 310 | if (mPaused) { |
| 311 | buffer->release(); |
| 312 | buffer = NULL; |
| 313 | continue; |
| 314 | } |
| 315 | |
| 316 | mEstimatedSizeBytes += kAdtsHeaderLength + buffer->range_length(); |
| 317 | if (exceedsFileSizeLimit()) { |
| 318 | buffer->release(); |
| 319 | buffer = NULL; |
| 320 | notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED, 0); |
| 321 | break; |
| 322 | } |
| 323 | |
| 324 | int32_t isCodecSpecific = 0; |
| 325 | if (buffer->meta_data()->findInt32(kKeyIsCodecConfig, &isCodecSpecific) && isCodecSpecific) { |
| 326 | LOGV("Drop codec specific info buffer"); |
| 327 | buffer->release(); |
| 328 | buffer = NULL; |
| 329 | continue; |
| 330 | } |
| 331 | |
| 332 | int64_t timestampUs; |
| 333 | CHECK(buffer->meta_data()->findInt64(kKeyTime, ×tampUs)); |
| 334 | if (timestampUs > mEstimatedDurationUs) { |
| 335 | mEstimatedDurationUs = timestampUs; |
| 336 | } |
| 337 | if (mResumed) { |
| 338 | previousPausedDurationUs += (timestampUs - maxTimestampUs - mFrameDurationUs); |
| 339 | mResumed = false; |
| 340 | } |
| 341 | timestampUs -= previousPausedDurationUs; |
| 342 | LOGV("time stamp: %lld, previous paused duration: %lld", |
| 343 | timestampUs, previousPausedDurationUs); |
| 344 | if (timestampUs > maxTimestampUs) { |
| 345 | maxTimestampUs = timestampUs; |
| 346 | } |
| 347 | |
| 348 | if (exceedsFileDurationLimit()) { |
| 349 | buffer->release(); |
| 350 | buffer = NULL; |
| 351 | notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_DURATION_REACHED, 0); |
| 352 | break; |
| 353 | } |
| 354 | |
| 355 | // Each output AAC audio frame to the file contains |
| 356 | // 1. an ADTS header, followed by |
| 357 | // 2. the compressed audio data. |
| 358 | ssize_t dataLength = buffer->range_length(); |
| 359 | uint8_t *data = (uint8_t *)buffer->data() + buffer->range_offset(); |
| 360 | if (writeAdtsHeader(kAdtsHeaderLength + dataLength) != OK || |
| 361 | dataLength != write(mFd, data, dataLength)) { |
| 362 | err = ERROR_IO; |
| 363 | } |
| 364 | |
| 365 | buffer->release(); |
| 366 | buffer = NULL; |
| 367 | } |
| 368 | |
| 369 | close(mFd); |
| 370 | mFd = -1; |
| 371 | mReachedEOS = true; |
| 372 | if (err == ERROR_END_OF_STREAM) { |
| 373 | return OK; |
| 374 | } |
| 375 | return err; |
| 376 | } |
| 377 | |
| 378 | bool AACWriter::reachedEOS() { |
| 379 | return mReachedEOS; |
| 380 | } |
| 381 | |
| 382 | } // namespace android |