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