blob: 71d48b3dd08699aa44dc36d214771b736fc75dd3 [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>
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 Dong18244862010-05-11 14:57:02 -070025#include <media/mediarecorder.h>
Andreas Huber07bf09d2010-01-25 14:27:12 -080026
27namespace android {
28
29AMRWriter::AMRWriter(const char *filename)
30 : mFile(fopen(filename, "wb")),
31 mInitCheck(mFile != NULL ? OK : NO_INIT),
James Dong08c74732010-06-10 12:28:15 -070032 mStarted(false),
33 mPaused(false),
34 mResumed(false) {
Andreas Huber07bf09d2010-01-25 14:27:12 -080035}
36
37AMRWriter::AMRWriter(int fd)
38 : mFile(fdopen(fd, "wb")),
39 mInitCheck(mFile != NULL ? OK : NO_INIT),
James Dong08c74732010-06-10 12:28:15 -070040 mStarted(false),
41 mPaused(false),
42 mResumed(false) {
Andreas Huber07bf09d2010-01-25 14:27:12 -080043}
44
45AMRWriter::~AMRWriter() {
46 if (mStarted) {
47 stop();
48 }
49
50 if (mFile != NULL) {
51 fclose(mFile);
52 mFile = NULL;
53 }
54}
55
56status_t AMRWriter::initCheck() const {
57 return mInitCheck;
58}
59
60status_t AMRWriter::addSource(const sp<MediaSource> &source) {
Andreas Huber07bf09d2010-01-25 14:27:12 -080061 if (mInitCheck != OK) {
62 return mInitCheck;
63 }
64
65 if (mSource != NULL) {
66 // AMR files only support a single track of audio.
67 return UNKNOWN_ERROR;
68 }
69
70 sp<MetaData> meta = source->getFormat();
71
72 const char *mime;
73 CHECK(meta->findCString(kKeyMIMEType, &mime));
74
75 bool isWide = false;
76 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_WB)) {
77 isWide = true;
78 } else if (strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_NB)) {
79 return ERROR_UNSUPPORTED;
80 }
81
82 int32_t channelCount;
83 int32_t sampleRate;
84 CHECK(meta->findInt32(kKeyChannelCount, &channelCount));
85 CHECK_EQ(channelCount, 1);
86 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
87 CHECK_EQ(sampleRate, (isWide ? 16000 : 8000));
88
89 mSource = source;
90
91 const char *kHeader = isWide ? "#!AMR-WB\n" : "#!AMR\n";
92 size_t n = strlen(kHeader);
93 if (fwrite(kHeader, 1, n, mFile) != n) {
94 return ERROR_IO;
95 }
96
97 return OK;
98}
99
James Dong6feaa462010-06-20 08:20:54 -0700100status_t AMRWriter::start(MetaData *params) {
Andreas Huber07bf09d2010-01-25 14:27:12 -0800101 if (mInitCheck != OK) {
102 return mInitCheck;
103 }
104
James Dong08c74732010-06-10 12:28:15 -0700105 if (mSource == NULL) {
Andreas Huber07bf09d2010-01-25 14:27:12 -0800106 return UNKNOWN_ERROR;
107 }
108
James Dong08c74732010-06-10 12:28:15 -0700109 if (mStarted && mPaused) {
110 mPaused = false;
111 mResumed = true;
112 return OK;
113 } else if (mStarted) {
114 // Already started, does nothing
115 return OK;
116 }
117
Andreas Huber07bf09d2010-01-25 14:27:12 -0800118 status_t err = mSource->start();
119
120 if (err != OK) {
121 return err;
122 }
123
124 pthread_attr_t attr;
125 pthread_attr_init(&attr);
126 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
127
Andreas Huber996dddf2010-01-25 15:30:31 -0800128 mReachedEOS = false;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800129 mDone = false;
130
131 pthread_create(&mThread, &attr, ThreadWrapper, this);
132 pthread_attr_destroy(&attr);
133
134 mStarted = true;
135
136 return OK;
137}
138
James Dongd0366622010-08-18 19:10:39 -0700139status_t AMRWriter::pause() {
James Dong08c74732010-06-10 12:28:15 -0700140 if (!mStarted) {
James Dongd0366622010-08-18 19:10:39 -0700141 return OK;
James Dong08c74732010-06-10 12:28:15 -0700142 }
143 mPaused = true;
James Dongd0366622010-08-18 19:10:39 -0700144 return OK;
James Dong08c74732010-06-10 12:28:15 -0700145}
146
James Dongd0366622010-08-18 19:10:39 -0700147status_t AMRWriter::stop() {
Andreas Hubere2018ca2010-03-23 14:33:02 -0700148 if (!mStarted) {
James Dongd0366622010-08-18 19:10:39 -0700149 return OK;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800150 }
151
Andreas Hubere2018ca2010-03-23 14:33:02 -0700152 mDone = true;
153
Andreas Huber07bf09d2010-01-25 14:27:12 -0800154 void *dummy;
155 pthread_join(mThread, &dummy);
156
James Dongd0366622010-08-18 19:10:39 -0700157 status_t err = (status_t) dummy;
158 {
159 status_t status = mSource->stop();
160 if (err == OK &&
161 (status != OK && status != ERROR_END_OF_STREAM)) {
162 err = status;
163 }
164 }
Andreas Huber07bf09d2010-01-25 14:27:12 -0800165
166 mStarted = false;
James Dongd0366622010-08-18 19:10:39 -0700167 return err;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800168}
169
James Dong18244862010-05-11 14:57:02 -0700170bool AMRWriter::exceedsFileSizeLimit() {
171 if (mMaxFileSizeLimitBytes == 0) {
172 return false;
173 }
174 return mEstimatedSizeBytes >= mMaxFileSizeLimitBytes;
175}
176
177bool AMRWriter::exceedsFileDurationLimit() {
178 if (mMaxFileDurationLimitUs == 0) {
179 return false;
180 }
181 return mEstimatedDurationUs >= mMaxFileDurationLimitUs;
182}
183
Andreas Huber07bf09d2010-01-25 14:27:12 -0800184// static
185void *AMRWriter::ThreadWrapper(void *me) {
James Dongd0366622010-08-18 19:10:39 -0700186 return (void *) static_cast<AMRWriter *>(me)->threadFunc();
Andreas Huber07bf09d2010-01-25 14:27:12 -0800187}
188
James Dongd0366622010-08-18 19:10:39 -0700189status_t AMRWriter::threadFunc() {
James Dong18244862010-05-11 14:57:02 -0700190 mEstimatedDurationUs = 0;
191 mEstimatedSizeBytes = 0;
James Dong68510e62010-05-14 11:48:00 -0700192 bool stoppedPrematurely = true;
James Dong08c74732010-06-10 12:28:15 -0700193 int64_t previousPausedDurationUs = 0;
194 int64_t maxTimestampUs = 0;
James Dongd0366622010-08-18 19:10:39 -0700195 status_t err = OK;
James Dong08c74732010-06-10 12:28:15 -0700196
Andreas Hubere2018ca2010-03-23 14:33:02 -0700197 while (!mDone) {
Andreas Huber07bf09d2010-01-25 14:27:12 -0800198 MediaBuffer *buffer;
James Dongd0366622010-08-18 19:10:39 -0700199 err = mSource->read(&buffer);
Andreas Huber07bf09d2010-01-25 14:27:12 -0800200
201 if (err != OK) {
202 break;
203 }
204
James Dong08c74732010-06-10 12:28:15 -0700205 if (mPaused) {
206 buffer->release();
207 buffer = NULL;
208 continue;
209 }
210
James Dong18244862010-05-11 14:57:02 -0700211 mEstimatedSizeBytes += buffer->range_length();
212 if (exceedsFileSizeLimit()) {
213 buffer->release();
214 buffer = NULL;
215 notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED, 0);
216 break;
217 }
218
219 int64_t timestampUs;
220 CHECK(buffer->meta_data()->findInt64(kKeyTime, &timestampUs));
221 if (timestampUs > mEstimatedDurationUs) {
222 mEstimatedDurationUs = timestampUs;
223 }
James Dong08c74732010-06-10 12:28:15 -0700224 if (mResumed) {
225 previousPausedDurationUs += (timestampUs - maxTimestampUs - 20000);
226 mResumed = false;
227 }
228 timestampUs -= previousPausedDurationUs;
229 LOGV("time stamp: %lld, previous paused duration: %lld",
230 timestampUs, previousPausedDurationUs);
231 if (timestampUs > maxTimestampUs) {
232 maxTimestampUs = timestampUs;
233 }
234
James Dong18244862010-05-11 14:57:02 -0700235 if (exceedsFileDurationLimit()) {
236 buffer->release();
237 buffer = NULL;
238 notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_DURATION_REACHED, 0);
239 break;
240 }
Andreas Huber07bf09d2010-01-25 14:27:12 -0800241 ssize_t n = fwrite(
242 (const uint8_t *)buffer->data() + buffer->range_offset(),
243 1,
244 buffer->range_length(),
245 mFile);
246
Andreas Huber07bf09d2010-01-25 14:27:12 -0800247 if (n < (ssize_t)buffer->range_length()) {
Andreas Huber259b7c12010-02-10 15:04:31 -0800248 buffer->release();
249 buffer = NULL;
250
Andreas Huber07bf09d2010-01-25 14:27:12 -0800251 break;
252 }
Andreas Huber259b7c12010-02-10 15:04:31 -0800253
James Dong68510e62010-05-14 11:48:00 -0700254 // XXX: How to tell it is stopped prematurely?
255 if (stoppedPrematurely) {
256 stoppedPrematurely = false;
257 }
258
Andreas Huber259b7c12010-02-10 15:04:31 -0800259 buffer->release();
260 buffer = NULL;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800261 }
Andreas Huber996dddf2010-01-25 15:30:31 -0800262
James Dong68510e62010-05-14 11:48:00 -0700263 if (stoppedPrematurely) {
James Dong09936ed2010-06-24 19:04:27 -0700264 notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_COMPLETION_STATUS, UNKNOWN_ERROR);
James Dong68510e62010-05-14 11:48:00 -0700265 }
266
267 fflush(mFile);
268 fclose(mFile);
269 mFile = NULL;
Andreas Huber996dddf2010-01-25 15:30:31 -0800270 mReachedEOS = true;
James Dongd0366622010-08-18 19:10:39 -0700271 if (err == ERROR_END_OF_STREAM) {
272 return OK;
273 }
274 return err;
Andreas Huber996dddf2010-01-25 15:30:31 -0800275}
276
277bool AMRWriter::reachedEOS() {
Andreas Huber996dddf2010-01-25 15:30:31 -0800278 return mReachedEOS;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800279}
280
281} // namespace android