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