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), |
| 32 | mStarted(false) { |
| 33 | } |
| 34 | |
| 35 | AMRWriter::AMRWriter(int fd) |
| 36 | : mFile(fdopen(fd, "wb")), |
| 37 | mInitCheck(mFile != NULL ? OK : NO_INIT), |
| 38 | mStarted(false) { |
| 39 | } |
| 40 | |
| 41 | AMRWriter::~AMRWriter() { |
| 42 | if (mStarted) { |
| 43 | stop(); |
| 44 | } |
| 45 | |
| 46 | if (mFile != NULL) { |
| 47 | fclose(mFile); |
| 48 | mFile = NULL; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | status_t AMRWriter::initCheck() const { |
| 53 | return mInitCheck; |
| 54 | } |
| 55 | |
| 56 | status_t AMRWriter::addSource(const sp<MediaSource> &source) { |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 57 | if (mInitCheck != OK) { |
| 58 | return mInitCheck; |
| 59 | } |
| 60 | |
| 61 | if (mSource != NULL) { |
| 62 | // AMR files only support a single track of audio. |
| 63 | return UNKNOWN_ERROR; |
| 64 | } |
| 65 | |
| 66 | sp<MetaData> meta = source->getFormat(); |
| 67 | |
| 68 | const char *mime; |
| 69 | CHECK(meta->findCString(kKeyMIMEType, &mime)); |
| 70 | |
| 71 | bool isWide = false; |
| 72 | if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_WB)) { |
| 73 | isWide = true; |
| 74 | } else if (strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_NB)) { |
| 75 | return ERROR_UNSUPPORTED; |
| 76 | } |
| 77 | |
| 78 | int32_t channelCount; |
| 79 | int32_t sampleRate; |
| 80 | CHECK(meta->findInt32(kKeyChannelCount, &channelCount)); |
| 81 | CHECK_EQ(channelCount, 1); |
| 82 | CHECK(meta->findInt32(kKeySampleRate, &sampleRate)); |
| 83 | CHECK_EQ(sampleRate, (isWide ? 16000 : 8000)); |
| 84 | |
| 85 | mSource = source; |
| 86 | |
| 87 | const char *kHeader = isWide ? "#!AMR-WB\n" : "#!AMR\n"; |
| 88 | size_t n = strlen(kHeader); |
| 89 | if (fwrite(kHeader, 1, n, mFile) != n) { |
| 90 | return ERROR_IO; |
| 91 | } |
| 92 | |
| 93 | return OK; |
| 94 | } |
| 95 | |
| 96 | status_t AMRWriter::start() { |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 97 | if (mInitCheck != OK) { |
| 98 | return mInitCheck; |
| 99 | } |
| 100 | |
| 101 | if (mStarted || mSource == NULL) { |
| 102 | return UNKNOWN_ERROR; |
| 103 | } |
| 104 | |
| 105 | status_t err = mSource->start(); |
| 106 | |
| 107 | if (err != OK) { |
| 108 | return err; |
| 109 | } |
| 110 | |
| 111 | pthread_attr_t attr; |
| 112 | pthread_attr_init(&attr); |
| 113 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
| 114 | |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 115 | mReachedEOS = false; |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 116 | mDone = false; |
| 117 | |
| 118 | pthread_create(&mThread, &attr, ThreadWrapper, this); |
| 119 | pthread_attr_destroy(&attr); |
| 120 | |
| 121 | mStarted = true; |
| 122 | |
| 123 | return OK; |
| 124 | } |
| 125 | |
| 126 | void AMRWriter::stop() { |
Andreas Huber | e2018ca | 2010-03-23 14:33:02 -0700 | [diff] [blame] | 127 | if (!mStarted) { |
| 128 | return; |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Andreas Huber | e2018ca | 2010-03-23 14:33:02 -0700 | [diff] [blame] | 131 | mDone = true; |
| 132 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 133 | void *dummy; |
| 134 | pthread_join(mThread, &dummy); |
| 135 | |
| 136 | mSource->stop(); |
| 137 | |
| 138 | mStarted = false; |
| 139 | } |
| 140 | |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 141 | bool AMRWriter::exceedsFileSizeLimit() { |
| 142 | if (mMaxFileSizeLimitBytes == 0) { |
| 143 | return false; |
| 144 | } |
| 145 | return mEstimatedSizeBytes >= mMaxFileSizeLimitBytes; |
| 146 | } |
| 147 | |
| 148 | bool AMRWriter::exceedsFileDurationLimit() { |
| 149 | if (mMaxFileDurationLimitUs == 0) { |
| 150 | return false; |
| 151 | } |
| 152 | return mEstimatedDurationUs >= mMaxFileDurationLimitUs; |
| 153 | } |
| 154 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 155 | // static |
| 156 | void *AMRWriter::ThreadWrapper(void *me) { |
| 157 | static_cast<AMRWriter *>(me)->threadFunc(); |
| 158 | |
| 159 | return NULL; |
| 160 | } |
| 161 | |
| 162 | void AMRWriter::threadFunc() { |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 163 | mEstimatedDurationUs = 0; |
| 164 | mEstimatedSizeBytes = 0; |
James Dong | 68510e6 | 2010-05-14 11:48:00 -0700 | [diff] [blame^] | 165 | bool stoppedPrematurely = true; |
Andreas Huber | e2018ca | 2010-03-23 14:33:02 -0700 | [diff] [blame] | 166 | while (!mDone) { |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 167 | MediaBuffer *buffer; |
| 168 | status_t err = mSource->read(&buffer); |
| 169 | |
| 170 | if (err != OK) { |
| 171 | break; |
| 172 | } |
| 173 | |
James Dong | 1824486 | 2010-05-11 14:57:02 -0700 | [diff] [blame] | 174 | mEstimatedSizeBytes += buffer->range_length(); |
| 175 | if (exceedsFileSizeLimit()) { |
| 176 | buffer->release(); |
| 177 | buffer = NULL; |
| 178 | notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED, 0); |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | int64_t timestampUs; |
| 183 | CHECK(buffer->meta_data()->findInt64(kKeyTime, ×tampUs)); |
| 184 | if (timestampUs > mEstimatedDurationUs) { |
| 185 | mEstimatedDurationUs = timestampUs; |
| 186 | } |
| 187 | if (exceedsFileDurationLimit()) { |
| 188 | buffer->release(); |
| 189 | buffer = NULL; |
| 190 | notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_DURATION_REACHED, 0); |
| 191 | break; |
| 192 | } |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 193 | ssize_t n = fwrite( |
| 194 | (const uint8_t *)buffer->data() + buffer->range_offset(), |
| 195 | 1, |
| 196 | buffer->range_length(), |
| 197 | mFile); |
| 198 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 199 | if (n < (ssize_t)buffer->range_length()) { |
Andreas Huber | 259b7c1 | 2010-02-10 15:04:31 -0800 | [diff] [blame] | 200 | buffer->release(); |
| 201 | buffer = NULL; |
| 202 | |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 203 | break; |
| 204 | } |
Andreas Huber | 259b7c1 | 2010-02-10 15:04:31 -0800 | [diff] [blame] | 205 | |
James Dong | 68510e6 | 2010-05-14 11:48:00 -0700 | [diff] [blame^] | 206 | // XXX: How to tell it is stopped prematurely? |
| 207 | if (stoppedPrematurely) { |
| 208 | stoppedPrematurely = false; |
| 209 | } |
| 210 | |
Andreas Huber | 259b7c1 | 2010-02-10 15:04:31 -0800 | [diff] [blame] | 211 | buffer->release(); |
| 212 | buffer = NULL; |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 213 | } |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 214 | |
James Dong | 68510e6 | 2010-05-14 11:48:00 -0700 | [diff] [blame^] | 215 | if (stoppedPrematurely) { |
| 216 | notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_STOP_PREMATURELY, 0); |
| 217 | } |
| 218 | |
| 219 | fflush(mFile); |
| 220 | fclose(mFile); |
| 221 | mFile = NULL; |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 222 | mReachedEOS = true; |
| 223 | } |
| 224 | |
| 225 | bool AMRWriter::reachedEOS() { |
Andreas Huber | 996dddf | 2010-01-25 15:30:31 -0800 | [diff] [blame] | 226 | return mReachedEOS; |
Andreas Huber | 07bf09d | 2010-01-25 14:27:12 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | } // namespace android |