AMRWriter.cpp revision 2dec2b5be2056c6d9428897dc672185872d30d17
1/* 2 * Copyright (C) 2010 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#include <media/stagefright/AMRWriter.h> 18 19#include <media/stagefright/MediaBuffer.h> 20#include <media/stagefright/MediaDebug.h> 21#include <media/stagefright/MediaDefs.h> 22#include <media/stagefright/MediaErrors.h> 23#include <media/stagefright/MediaSource.h> 24#include <media/stagefright/MetaData.h> 25#include <media/mediarecorder.h> 26 27namespace android { 28 29AMRWriter::AMRWriter(const char *filename) 30 : mFile(fopen(filename, "wb")), 31 mInitCheck(mFile != NULL ? OK : NO_INIT), 32 mStarted(false), 33 mPaused(false), 34 mResumed(false) { 35} 36 37AMRWriter::AMRWriter(int fd) 38 : mFile(fdopen(fd, "wb")), 39 mInitCheck(mFile != NULL ? OK : NO_INIT), 40 mStarted(false), 41 mPaused(false), 42 mResumed(false) { 43} 44 45AMRWriter::~AMRWriter() { 46 if (mStarted) { 47 stop(); 48 } 49 50 if (mFile != NULL) { 51 fclose(mFile); 52 mFile = NULL; 53 } 54} 55 56status_t AMRWriter::initCheck() const { 57 return mInitCheck; 58} 59 60status_t AMRWriter::addSource(const sp<MediaSource> &source) { 61 if (mInitCheck != OK) { 62 return mInitCheck; 63 } 64 65 if (mSource != NULL) { 66 // AMR files only support a single track of audio. 67 return UNKNOWN_ERROR; 68 } 69 70 sp<MetaData> meta = source->getFormat(); 71 72 const char *mime; 73 CHECK(meta->findCString(kKeyMIMEType, &mime)); 74 75 bool isWide = false; 76 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_WB)) { 77 isWide = true; 78 } else if (strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_NB)) { 79 return ERROR_UNSUPPORTED; 80 } 81 82 int32_t channelCount; 83 int32_t sampleRate; 84 CHECK(meta->findInt32(kKeyChannelCount, &channelCount)); 85 CHECK_EQ(channelCount, 1); 86 CHECK(meta->findInt32(kKeySampleRate, &sampleRate)); 87 CHECK_EQ(sampleRate, (isWide ? 16000 : 8000)); 88 89 mSource = source; 90 91 const char *kHeader = isWide ? "#!AMR-WB\n" : "#!AMR\n"; 92 size_t n = strlen(kHeader); 93 if (fwrite(kHeader, 1, n, mFile) != n) { 94 return ERROR_IO; 95 } 96 97 return OK; 98} 99 100status_t AMRWriter::start(MetaData *params) { 101 if (mInitCheck != OK) { 102 return mInitCheck; 103 } 104 105 if (mSource == NULL) { 106 return UNKNOWN_ERROR; 107 } 108 109 if (mStarted && mPaused) { 110 mPaused = false; 111 mResumed = true; 112 return OK; 113 } else if (mStarted) { 114 // Already started, does nothing 115 return OK; 116 } 117 118 status_t err = mSource->start(); 119 120 if (err != OK) { 121 return err; 122 } 123 124 pthread_attr_t attr; 125 pthread_attr_init(&attr); 126 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); 127 128 mReachedEOS = false; 129 mDone = false; 130 131 pthread_create(&mThread, &attr, ThreadWrapper, this); 132 pthread_attr_destroy(&attr); 133 134 mStarted = true; 135 136 return OK; 137} 138 139void AMRWriter::pause() { 140 if (!mStarted) { 141 return; 142 } 143 mPaused = true; 144} 145 146void AMRWriter::stop() { 147 if (!mStarted) { 148 return; 149 } 150 151 mDone = true; 152 153 void *dummy; 154 pthread_join(mThread, &dummy); 155 156 mSource->stop(); 157 158 mStarted = false; 159} 160 161bool AMRWriter::exceedsFileSizeLimit() { 162 if (mMaxFileSizeLimitBytes == 0) { 163 return false; 164 } 165 return mEstimatedSizeBytes >= mMaxFileSizeLimitBytes; 166} 167 168bool AMRWriter::exceedsFileDurationLimit() { 169 if (mMaxFileDurationLimitUs == 0) { 170 return false; 171 } 172 return mEstimatedDurationUs >= mMaxFileDurationLimitUs; 173} 174 175// static 176void *AMRWriter::ThreadWrapper(void *me) { 177 static_cast<AMRWriter *>(me)->threadFunc(); 178 179 return NULL; 180} 181 182void AMRWriter::threadFunc() { 183 mEstimatedDurationUs = 0; 184 mEstimatedSizeBytes = 0; 185 bool stoppedPrematurely = true; 186 int64_t previousPausedDurationUs = 0; 187 int64_t maxTimestampUs = 0; 188 189 while (!mDone) { 190 MediaBuffer *buffer; 191 status_t err = mSource->read(&buffer); 192 193 if (err != OK) { 194 break; 195 } 196 197 if (mPaused) { 198 buffer->release(); 199 buffer = NULL; 200 continue; 201 } 202 203 mEstimatedSizeBytes += buffer->range_length(); 204 if (exceedsFileSizeLimit()) { 205 buffer->release(); 206 buffer = NULL; 207 notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED, 0); 208 break; 209 } 210 211 int64_t timestampUs; 212 CHECK(buffer->meta_data()->findInt64(kKeyTime, ×tampUs)); 213 if (timestampUs > mEstimatedDurationUs) { 214 mEstimatedDurationUs = timestampUs; 215 } 216 if (mResumed) { 217 previousPausedDurationUs += (timestampUs - maxTimestampUs - 20000); 218 mResumed = false; 219 } 220 timestampUs -= previousPausedDurationUs; 221 LOGV("time stamp: %lld, previous paused duration: %lld", 222 timestampUs, previousPausedDurationUs); 223 if (timestampUs > maxTimestampUs) { 224 maxTimestampUs = timestampUs; 225 } 226 227 if (exceedsFileDurationLimit()) { 228 buffer->release(); 229 buffer = NULL; 230 notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_DURATION_REACHED, 0); 231 break; 232 } 233 ssize_t n = fwrite( 234 (const uint8_t *)buffer->data() + buffer->range_offset(), 235 1, 236 buffer->range_length(), 237 mFile); 238 239 if (n < (ssize_t)buffer->range_length()) { 240 buffer->release(); 241 buffer = NULL; 242 243 break; 244 } 245 246 // XXX: How to tell it is stopped prematurely? 247 if (stoppedPrematurely) { 248 stoppedPrematurely = false; 249 } 250 251 buffer->release(); 252 buffer = NULL; 253 } 254 255 if (stoppedPrematurely) { 256 notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_STOP_PREMATURELY, 0); 257 } 258 259 fflush(mFile); 260 fclose(mFile); 261 mFile = NULL; 262 mReachedEOS = true; 263} 264 265bool AMRWriter::reachedEOS() { 266 return mReachedEOS; 267} 268 269} // namespace android 270