blob: 0db3d1dafc300ba20bb2e6d39158efa077ef2b98 [file] [log] [blame]
Andreas Huber07bf09d2010-01-25 14:27:12 -08001/*
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 Huber07bf09d2010-01-25 14:27:12 -080018#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 Dong18244862010-05-11 14:57:02 -070024#include <media/mediarecorder.h>
James Dongc67acb22010-10-07 20:20:59 -070025#include <sys/prctl.h>
26#include <sys/resource.h>
James Dong2747e0e2010-11-18 20:59:13 -080027#include <sys/types.h>
28#include <sys/stat.h>
29#include <fcntl.h>
Andreas Huber07bf09d2010-01-25 14:27:12 -080030
31namespace android {
32
33AMRWriter::AMRWriter(const char *filename)
James Dong2747e0e2010-11-18 20:59:13 -080034 : mFd(-1),
35 mInitCheck(NO_INIT),
James Dong08c74732010-06-10 12:28:15 -070036 mStarted(false),
37 mPaused(false),
38 mResumed(false) {
James Dong2747e0e2010-11-18 20:59:13 -080039
40 mFd = open(filename, O_CREAT | O_LARGEFILE | O_TRUNC);
41 if (mFd >= 0) {
42 mInitCheck = OK;
43 }
Andreas Huber07bf09d2010-01-25 14:27:12 -080044}
45
46AMRWriter::AMRWriter(int fd)
James Dong2747e0e2010-11-18 20:59:13 -080047 : mFd(dup(fd)),
48 mInitCheck(mFd < 0? NO_INIT: OK),
James Dong08c74732010-06-10 12:28:15 -070049 mStarted(false),
50 mPaused(false),
51 mResumed(false) {
Andreas Huber07bf09d2010-01-25 14:27:12 -080052}
53
54AMRWriter::~AMRWriter() {
55 if (mStarted) {
56 stop();
57 }
58
James Dong2747e0e2010-11-18 20:59:13 -080059 if (mFd != -1) {
60 close(mFd);
61 mFd = -1;
Andreas Huber07bf09d2010-01-25 14:27:12 -080062 }
63}
64
65status_t AMRWriter::initCheck() const {
66 return mInitCheck;
67}
68
69status_t AMRWriter::addSource(const sp<MediaSource> &source) {
Andreas Huber07bf09d2010-01-25 14:27:12 -080070 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 Dong2747e0e2010-11-18 20:59:13 -0800101 ssize_t n = strlen(kHeader);
James Dongb1262a82010-11-16 14:04:54 -0800102 if (write(mFd, kHeader, n) != n) {
Andreas Huber07bf09d2010-01-25 14:27:12 -0800103 return ERROR_IO;
104 }
105
106 return OK;
107}
108
James Dong6feaa462010-06-20 08:20:54 -0700109status_t AMRWriter::start(MetaData *params) {
Andreas Huber07bf09d2010-01-25 14:27:12 -0800110 if (mInitCheck != OK) {
111 return mInitCheck;
112 }
113
James Dong08c74732010-06-10 12:28:15 -0700114 if (mSource == NULL) {
Andreas Huber07bf09d2010-01-25 14:27:12 -0800115 return UNKNOWN_ERROR;
116 }
117
James Dong08c74732010-06-10 12:28:15 -0700118 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 Huber07bf09d2010-01-25 14:27:12 -0800127 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 Huber996dddf2010-01-25 15:30:31 -0800137 mReachedEOS = false;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800138 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 Dongd0366622010-08-18 19:10:39 -0700148status_t AMRWriter::pause() {
James Dong08c74732010-06-10 12:28:15 -0700149 if (!mStarted) {
James Dongd0366622010-08-18 19:10:39 -0700150 return OK;
James Dong08c74732010-06-10 12:28:15 -0700151 }
152 mPaused = true;
James Dongd0366622010-08-18 19:10:39 -0700153 return OK;
James Dong08c74732010-06-10 12:28:15 -0700154}
155
James Dongd0366622010-08-18 19:10:39 -0700156status_t AMRWriter::stop() {
Andreas Hubere2018ca2010-03-23 14:33:02 -0700157 if (!mStarted) {
James Dongd0366622010-08-18 19:10:39 -0700158 return OK;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800159 }
160
Andreas Hubere2018ca2010-03-23 14:33:02 -0700161 mDone = true;
162
Andreas Huber07bf09d2010-01-25 14:27:12 -0800163 void *dummy;
164 pthread_join(mThread, &dummy);
165
James Dongd0366622010-08-18 19:10:39 -0700166 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 Huber07bf09d2010-01-25 14:27:12 -0800174
175 mStarted = false;
James Dongd0366622010-08-18 19:10:39 -0700176 return err;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800177}
178
James Dong18244862010-05-11 14:57:02 -0700179bool AMRWriter::exceedsFileSizeLimit() {
180 if (mMaxFileSizeLimitBytes == 0) {
181 return false;
182 }
183 return mEstimatedSizeBytes >= mMaxFileSizeLimitBytes;
184}
185
186bool AMRWriter::exceedsFileDurationLimit() {
187 if (mMaxFileDurationLimitUs == 0) {
188 return false;
189 }
190 return mEstimatedDurationUs >= mMaxFileDurationLimitUs;
191}
192
Andreas Huber07bf09d2010-01-25 14:27:12 -0800193// static
194void *AMRWriter::ThreadWrapper(void *me) {
James Dongd0366622010-08-18 19:10:39 -0700195 return (void *) static_cast<AMRWriter *>(me)->threadFunc();
Andreas Huber07bf09d2010-01-25 14:27:12 -0800196}
197
James Dongd0366622010-08-18 19:10:39 -0700198status_t AMRWriter::threadFunc() {
James Dong18244862010-05-11 14:57:02 -0700199 mEstimatedDurationUs = 0;
200 mEstimatedSizeBytes = 0;
James Dong68510e62010-05-14 11:48:00 -0700201 bool stoppedPrematurely = true;
James Dong08c74732010-06-10 12:28:15 -0700202 int64_t previousPausedDurationUs = 0;
203 int64_t maxTimestampUs = 0;
James Dongd0366622010-08-18 19:10:39 -0700204 status_t err = OK;
James Dong08c74732010-06-10 12:28:15 -0700205
James Dongc67acb22010-10-07 20:20:59 -0700206 prctl(PR_SET_NAME, (unsigned long)"AMRWriter", 0, 0, 0);
Andreas Hubere2018ca2010-03-23 14:33:02 -0700207 while (!mDone) {
Andreas Huber07bf09d2010-01-25 14:27:12 -0800208 MediaBuffer *buffer;
James Dongd0366622010-08-18 19:10:39 -0700209 err = mSource->read(&buffer);
Andreas Huber07bf09d2010-01-25 14:27:12 -0800210
211 if (err != OK) {
212 break;
213 }
214
James Dong08c74732010-06-10 12:28:15 -0700215 if (mPaused) {
216 buffer->release();
217 buffer = NULL;
218 continue;
219 }
220
James Dong18244862010-05-11 14:57:02 -0700221 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, &timestampUs));
231 if (timestampUs > mEstimatedDurationUs) {
232 mEstimatedDurationUs = timestampUs;
233 }
James Dong08c74732010-06-10 12:28:15 -0700234 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 Dong18244862010-05-11 14:57:02 -0700245 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 Dongb1262a82010-11-16 14:04:54 -0800251 ssize_t n = write(mFd,
252 (const uint8_t *)buffer->data() + buffer->range_offset(),
253 buffer->range_length());
Andreas Huber07bf09d2010-01-25 14:27:12 -0800254
Andreas Huber07bf09d2010-01-25 14:27:12 -0800255 if (n < (ssize_t)buffer->range_length()) {
Andreas Huber259b7c12010-02-10 15:04:31 -0800256 buffer->release();
257 buffer = NULL;
258
Andreas Huber07bf09d2010-01-25 14:27:12 -0800259 break;
260 }
Andreas Huber259b7c12010-02-10 15:04:31 -0800261
James Dong68510e62010-05-14 11:48:00 -0700262 // XXX: How to tell it is stopped prematurely?
263 if (stoppedPrematurely) {
264 stoppedPrematurely = false;
265 }
266
Andreas Huber259b7c12010-02-10 15:04:31 -0800267 buffer->release();
268 buffer = NULL;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800269 }
Andreas Huber996dddf2010-01-25 15:30:31 -0800270
James Dong68510e62010-05-14 11:48:00 -0700271 if (stoppedPrematurely) {
James Dong09936ed2010-06-24 19:04:27 -0700272 notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_COMPLETION_STATUS, UNKNOWN_ERROR);
James Dong68510e62010-05-14 11:48:00 -0700273 }
274
James Dong2747e0e2010-11-18 20:59:13 -0800275 close(mFd);
276 mFd = -1;
Andreas Huber996dddf2010-01-25 15:30:31 -0800277 mReachedEOS = true;
James Dongd0366622010-08-18 19:10:39 -0700278 if (err == ERROR_END_OF_STREAM) {
279 return OK;
280 }
281 return err;
Andreas Huber996dddf2010-01-25 15:30:31 -0800282}
283
284bool AMRWriter::reachedEOS() {
Andreas Huber996dddf2010-01-25 15:30:31 -0800285 return mReachedEOS;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800286}
287
288} // namespace android