Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 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> |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 25 | #include <media/mediarecorder.h> |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | AMRWriter::AMRWriter(const char *filename) |
| 30 | : mFile(fopen(filename, "wb")), |
| 31 | mInitCheck(mFile != NULL ? OK : NO_INIT), |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 32 | mStarted(false), |
| 33 | mPaused(false), |
| 34 | mResumed(false) { |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | AMRWriter::AMRWriter(int fd) |
| 38 | : mFile(fdopen(fd, "wb")), |
| 39 | mInitCheck(mFile != NULL ? OK : NO_INIT), |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 40 | mStarted(false), |
| 41 | mPaused(false), |
| 42 | mResumed(false) { |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | AMRWriter::~AMRWriter() { |
| 46 | if (mStarted) { |
| 47 | stop(); |
| 48 | } |
| 49 | |
| 50 | if (mFile != NULL) { |
| 51 | fclose(mFile); |
| 52 | mFile = NULL; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | status_t AMRWriter::initCheck() const { |
| 57 | return mInitCheck; |
| 58 | } |
| 59 | |
| 60 | status_t AMRWriter::addSource(const sp<MediaSource> &source) { |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 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 | |
James Dong | 6feaa46 | 2010-06-20 08:20:54 -0700 | [diff] [blame] | 100 | status_t AMRWriter::start(MetaData *params) { |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 101 | if (mInitCheck != OK) { |
| 102 | return mInitCheck; |
| 103 | } |
| 104 | |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 105 | if (mSource == NULL) { |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 106 | return UNKNOWN_ERROR; |
| 107 | } |
| 108 | |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 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 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 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 | |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 128 | mReachedEOS = false; |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 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 | |
James Dong | d036662 | 2010-08-18 19:10:39 -0700 | [diff] [blame^] | 139 | status_t AMRWriter::pause() { |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 140 | if (!mStarted) { |
James Dong | d036662 | 2010-08-18 19:10:39 -0700 | [diff] [blame^] | 141 | return OK; |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 142 | } |
| 143 | mPaused = true; |
James Dong | d036662 | 2010-08-18 19:10:39 -0700 | [diff] [blame^] | 144 | return OK; |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 145 | } |
| 146 | |
James Dong | d036662 | 2010-08-18 19:10:39 -0700 | [diff] [blame^] | 147 | status_t AMRWriter::stop() { |
Andreas Huber | e2018ca | 2010-03-23 14:33:02 -0700 | [diff] [blame] | 148 | if (!mStarted) { |
James Dong | d036662 | 2010-08-18 19:10:39 -0700 | [diff] [blame^] | 149 | return OK; |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Andreas Huber | e2018ca | 2010-03-23 14:33:02 -0700 | [diff] [blame] | 152 | mDone = true; |
| 153 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 154 | void *dummy; |
| 155 | pthread_join(mThread, &dummy); |
| 156 | |
James Dong | d036662 | 2010-08-18 19:10:39 -0700 | [diff] [blame^] | 157 | status_t err = (status_t) dummy; |
| 158 | { |
| 159 | status_t status = mSource->stop(); |
| 160 | if (err == OK && |
| 161 | (status != OK && status != ERROR_END_OF_STREAM)) { |
| 162 | err = status; |
| 163 | } |
| 164 | } |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 165 | |
| 166 | mStarted = false; |
James Dong | d036662 | 2010-08-18 19:10:39 -0700 | [diff] [blame^] | 167 | return err; |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 168 | } |
| 169 | |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 170 | bool AMRWriter::exceedsFileSizeLimit() { |
| 171 | if (mMaxFileSizeLimitBytes == 0) { |
| 172 | return false; |
| 173 | } |
| 174 | return mEstimatedSizeBytes >= mMaxFileSizeLimitBytes; |
| 175 | } |
| 176 | |
| 177 | bool AMRWriter::exceedsFileDurationLimit() { |
| 178 | if (mMaxFileDurationLimitUs == 0) { |
| 179 | return false; |
| 180 | } |
| 181 | return mEstimatedDurationUs >= mMaxFileDurationLimitUs; |
| 182 | } |
| 183 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 184 | // static |
| 185 | void *AMRWriter::ThreadWrapper(void *me) { |
James Dong | d036662 | 2010-08-18 19:10:39 -0700 | [diff] [blame^] | 186 | return (void *) static_cast<AMRWriter *>(me)->threadFunc(); |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 187 | } |
| 188 | |
James Dong | d036662 | 2010-08-18 19:10:39 -0700 | [diff] [blame^] | 189 | status_t AMRWriter::threadFunc() { |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 190 | mEstimatedDurationUs = 0; |
| 191 | mEstimatedSizeBytes = 0; |
James Dong | 68510e6 | 2010-05-14 11:48:00 -0700 | [diff] [blame] | 192 | bool stoppedPrematurely = true; |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 193 | int64_t previousPausedDurationUs = 0; |
| 194 | int64_t maxTimestampUs = 0; |
James Dong | d036662 | 2010-08-18 19:10:39 -0700 | [diff] [blame^] | 195 | status_t err = OK; |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 196 | |
Andreas Huber | e2018ca | 2010-03-23 14:33:02 -0700 | [diff] [blame] | 197 | while (!mDone) { |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 198 | MediaBuffer *buffer; |
James Dong | d036662 | 2010-08-18 19:10:39 -0700 | [diff] [blame^] | 199 | err = mSource->read(&buffer); |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 200 | |
| 201 | if (err != OK) { |
| 202 | break; |
| 203 | } |
| 204 | |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 205 | if (mPaused) { |
| 206 | buffer->release(); |
| 207 | buffer = NULL; |
| 208 | continue; |
| 209 | } |
| 210 | |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 211 | mEstimatedSizeBytes += buffer->range_length(); |
| 212 | if (exceedsFileSizeLimit()) { |
| 213 | buffer->release(); |
| 214 | buffer = NULL; |
| 215 | notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED, 0); |
| 216 | break; |
| 217 | } |
| 218 | |
| 219 | int64_t timestampUs; |
| 220 | CHECK(buffer->meta_data()->findInt64(kKeyTime, ×tampUs)); |
| 221 | if (timestampUs > mEstimatedDurationUs) { |
| 222 | mEstimatedDurationUs = timestampUs; |
| 223 | } |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 224 | if (mResumed) { |
| 225 | previousPausedDurationUs += (timestampUs - maxTimestampUs - 20000); |
| 226 | mResumed = false; |
| 227 | } |
| 228 | timestampUs -= previousPausedDurationUs; |
| 229 | LOGV("time stamp: %lld, previous paused duration: %lld", |
| 230 | timestampUs, previousPausedDurationUs); |
| 231 | if (timestampUs > maxTimestampUs) { |
| 232 | maxTimestampUs = timestampUs; |
| 233 | } |
| 234 | |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 235 | if (exceedsFileDurationLimit()) { |
| 236 | buffer->release(); |
| 237 | buffer = NULL; |
| 238 | notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_DURATION_REACHED, 0); |
| 239 | break; |
| 240 | } |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 241 | ssize_t n = fwrite( |
| 242 | (const uint8_t *)buffer->data() + buffer->range_offset(), |
| 243 | 1, |
| 244 | buffer->range_length(), |
| 245 | mFile); |
| 246 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 247 | if (n < (ssize_t)buffer->range_length()) { |
Andreas Huber | 259b7c1 | 2010-02-10 15:04:31 -0800 | [diff] [blame] | 248 | buffer->release(); |
| 249 | buffer = NULL; |
| 250 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 251 | break; |
| 252 | } |
Andreas Huber | 259b7c1 | 2010-02-10 15:04:31 -0800 | [diff] [blame] | 253 | |
James Dong | 68510e6 | 2010-05-14 11:48:00 -0700 | [diff] [blame] | 254 | // XXX: How to tell it is stopped prematurely? |
| 255 | if (stoppedPrematurely) { |
| 256 | stoppedPrematurely = false; |
| 257 | } |
| 258 | |
Andreas Huber | 259b7c1 | 2010-02-10 15:04:31 -0800 | [diff] [blame] | 259 | buffer->release(); |
| 260 | buffer = NULL; |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 261 | } |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 262 | |
James Dong | 68510e6 | 2010-05-14 11:48:00 -0700 | [diff] [blame] | 263 | if (stoppedPrematurely) { |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame] | 264 | notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_COMPLETION_STATUS, UNKNOWN_ERROR); |
James Dong | 68510e6 | 2010-05-14 11:48:00 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | fflush(mFile); |
| 268 | fclose(mFile); |
| 269 | mFile = NULL; |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 270 | mReachedEOS = true; |
James Dong | d036662 | 2010-08-18 19:10:39 -0700 | [diff] [blame^] | 271 | if (err == ERROR_END_OF_STREAM) { |
| 272 | return OK; |
| 273 | } |
| 274 | return err; |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | bool AMRWriter::reachedEOS() { |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 278 | return mReachedEOS; |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | } // namespace android |