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