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 | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 139 | void AMRWriter::pause() { |
| 140 | if (!mStarted) { |
| 141 | return; |
| 142 | } |
| 143 | mPaused = true; |
| 144 | } |
| 145 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 146 | void AMRWriter::stop() { |
Andreas Huber | e2018ca | 2010-03-23 14:33:02 -0700 | [diff] [blame] | 147 | if (!mStarted) { |
| 148 | return; |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Andreas Huber | e2018ca | 2010-03-23 14:33:02 -0700 | [diff] [blame] | 151 | mDone = true; |
| 152 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 153 | void *dummy; |
| 154 | pthread_join(mThread, &dummy); |
| 155 | |
| 156 | mSource->stop(); |
| 157 | |
| 158 | mStarted = false; |
| 159 | } |
| 160 | |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 161 | bool AMRWriter::exceedsFileSizeLimit() { |
| 162 | if (mMaxFileSizeLimitBytes == 0) { |
| 163 | return false; |
| 164 | } |
| 165 | return mEstimatedSizeBytes >= mMaxFileSizeLimitBytes; |
| 166 | } |
| 167 | |
| 168 | bool AMRWriter::exceedsFileDurationLimit() { |
| 169 | if (mMaxFileDurationLimitUs == 0) { |
| 170 | return false; |
| 171 | } |
| 172 | return mEstimatedDurationUs >= mMaxFileDurationLimitUs; |
| 173 | } |
| 174 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 175 | // static |
| 176 | void *AMRWriter::ThreadWrapper(void *me) { |
| 177 | static_cast<AMRWriter *>(me)->threadFunc(); |
| 178 | |
| 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | void AMRWriter::threadFunc() { |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 183 | mEstimatedDurationUs = 0; |
| 184 | mEstimatedSizeBytes = 0; |
James Dong | 68510e6 | 2010-05-14 11:48:00 -0700 | [diff] [blame] | 185 | bool stoppedPrematurely = true; |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 186 | int64_t previousPausedDurationUs = 0; |
| 187 | int64_t maxTimestampUs = 0; |
| 188 | |
Andreas Huber | e2018ca | 2010-03-23 14:33:02 -0700 | [diff] [blame] | 189 | while (!mDone) { |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 190 | MediaBuffer *buffer; |
| 191 | status_t err = mSource->read(&buffer); |
| 192 | |
| 193 | if (err != OK) { |
| 194 | break; |
| 195 | } |
| 196 | |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 197 | if (mPaused) { |
| 198 | buffer->release(); |
| 199 | buffer = NULL; |
| 200 | continue; |
| 201 | } |
| 202 | |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 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 | } |
James Dong | 08c7473 | 2010-06-10 12:28:15 -0700 | [diff] [blame] | 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 | |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 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 | } |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 233 | ssize_t n = fwrite( |
| 234 | (const uint8_t *)buffer->data() + buffer->range_offset(), |
| 235 | 1, |
| 236 | buffer->range_length(), |
| 237 | mFile); |
| 238 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 239 | if (n < (ssize_t)buffer->range_length()) { |
Andreas Huber | 259b7c1 | 2010-02-10 15:04:31 -0800 | [diff] [blame] | 240 | buffer->release(); |
| 241 | buffer = NULL; |
| 242 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 243 | break; |
| 244 | } |
Andreas Huber | 259b7c1 | 2010-02-10 15:04:31 -0800 | [diff] [blame] | 245 | |
James Dong | 68510e6 | 2010-05-14 11:48:00 -0700 | [diff] [blame] | 246 | // XXX: How to tell it is stopped prematurely? |
| 247 | if (stoppedPrematurely) { |
| 248 | stoppedPrematurely = false; |
| 249 | } |
| 250 | |
Andreas Huber | 259b7c1 | 2010-02-10 15:04:31 -0800 | [diff] [blame] | 251 | buffer->release(); |
| 252 | buffer = NULL; |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 253 | } |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 254 | |
James Dong | 68510e6 | 2010-05-14 11:48:00 -0700 | [diff] [blame] | 255 | if (stoppedPrematurely) { |
James Dong | 09936ed | 2010-06-24 19:04:27 -0700 | [diff] [blame^] | 256 | notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_COMPLETION_STATUS, UNKNOWN_ERROR); |
James Dong | 68510e6 | 2010-05-14 11:48:00 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | fflush(mFile); |
| 260 | fclose(mFile); |
| 261 | mFile = NULL; |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 262 | mReachedEOS = true; |
| 263 | } |
| 264 | |
| 265 | bool AMRWriter::reachedEOS() { |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 266 | return mReachedEOS; |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | } // namespace android |