blob: c71743ef717bda5b1ffb312f6355d76a60e8407a [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 Dong08c74732010-06-10 12:28:15 -0700139void AMRWriter::pause() {
140 if (!mStarted) {
141 return;
142 }
143 mPaused = true;
144}
145
Andreas Huber07bf09d2010-01-25 14:27:12 -0800146void AMRWriter::stop() {
Andreas Hubere2018ca2010-03-23 14:33:02 -0700147 if (!mStarted) {
148 return;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800149 }
150
Andreas Hubere2018ca2010-03-23 14:33:02 -0700151 mDone = true;
152
Andreas Huber07bf09d2010-01-25 14:27:12 -0800153 void *dummy;
154 pthread_join(mThread, &dummy);
155
156 mSource->stop();
157
158 mStarted = false;
159}
160
James Dong18244862010-05-11 14:57:02 -0700161bool AMRWriter::exceedsFileSizeLimit() {
162 if (mMaxFileSizeLimitBytes == 0) {
163 return false;
164 }
165 return mEstimatedSizeBytes >= mMaxFileSizeLimitBytes;
166}
167
168bool AMRWriter::exceedsFileDurationLimit() {
169 if (mMaxFileDurationLimitUs == 0) {
170 return false;
171 }
172 return mEstimatedDurationUs >= mMaxFileDurationLimitUs;
173}
174
Andreas Huber07bf09d2010-01-25 14:27:12 -0800175// static
176void *AMRWriter::ThreadWrapper(void *me) {
177 static_cast<AMRWriter *>(me)->threadFunc();
178
179 return NULL;
180}
181
182void AMRWriter::threadFunc() {
James Dong18244862010-05-11 14:57:02 -0700183 mEstimatedDurationUs = 0;
184 mEstimatedSizeBytes = 0;
James Dong68510e62010-05-14 11:48:00 -0700185 bool stoppedPrematurely = true;
James Dong08c74732010-06-10 12:28:15 -0700186 int64_t previousPausedDurationUs = 0;
187 int64_t maxTimestampUs = 0;
188
Andreas Hubere2018ca2010-03-23 14:33:02 -0700189 while (!mDone) {
Andreas Huber07bf09d2010-01-25 14:27:12 -0800190 MediaBuffer *buffer;
191 status_t err = mSource->read(&buffer);
192
193 if (err != OK) {
194 break;
195 }
196
James Dong08c74732010-06-10 12:28:15 -0700197 if (mPaused) {
198 buffer->release();
199 buffer = NULL;
200 continue;
201 }
202
James Dong18244862010-05-11 14:57:02 -0700203 mEstimatedSizeBytes += buffer->range_length();
204 if (exceedsFileSizeLimit()) {
205 buffer->release();
206 buffer = NULL;
207 notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED, 0);
208 break;
209 }
210
211 int64_t timestampUs;
212 CHECK(buffer->meta_data()->findInt64(kKeyTime, &timestampUs));
213 if (timestampUs > mEstimatedDurationUs) {
214 mEstimatedDurationUs = timestampUs;
215 }
James Dong08c74732010-06-10 12:28:15 -0700216 if (mResumed) {
217 previousPausedDurationUs += (timestampUs - maxTimestampUs - 20000);
218 mResumed = false;
219 }
220 timestampUs -= previousPausedDurationUs;
221 LOGV("time stamp: %lld, previous paused duration: %lld",
222 timestampUs, previousPausedDurationUs);
223 if (timestampUs > maxTimestampUs) {
224 maxTimestampUs = timestampUs;
225 }
226
James Dong18244862010-05-11 14:57:02 -0700227 if (exceedsFileDurationLimit()) {
228 buffer->release();
229 buffer = NULL;
230 notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_DURATION_REACHED, 0);
231 break;
232 }
Andreas Huber07bf09d2010-01-25 14:27:12 -0800233 ssize_t n = fwrite(
234 (const uint8_t *)buffer->data() + buffer->range_offset(),
235 1,
236 buffer->range_length(),
237 mFile);
238
Andreas Huber07bf09d2010-01-25 14:27:12 -0800239 if (n < (ssize_t)buffer->range_length()) {
Andreas Huber259b7c12010-02-10 15:04:31 -0800240 buffer->release();
241 buffer = NULL;
242
Andreas Huber07bf09d2010-01-25 14:27:12 -0800243 break;
244 }
Andreas Huber259b7c12010-02-10 15:04:31 -0800245
James Dong68510e62010-05-14 11:48:00 -0700246 // XXX: How to tell it is stopped prematurely?
247 if (stoppedPrematurely) {
248 stoppedPrematurely = false;
249 }
250
Andreas Huber259b7c12010-02-10 15:04:31 -0800251 buffer->release();
252 buffer = NULL;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800253 }
Andreas Huber996dddf2010-01-25 15:30:31 -0800254
James Dong68510e62010-05-14 11:48:00 -0700255 if (stoppedPrematurely) {
James Dong09936ed2010-06-24 19:04:27 -0700256 notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_COMPLETION_STATUS, UNKNOWN_ERROR);
James Dong68510e62010-05-14 11:48:00 -0700257 }
258
259 fflush(mFile);
260 fclose(mFile);
261 mFile = NULL;
Andreas Huber996dddf2010-01-25 15:30:31 -0800262 mReachedEOS = true;
263}
264
265bool AMRWriter::reachedEOS() {
Andreas Huber996dddf2010-01-25 15:30:31 -0800266 return mReachedEOS;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800267}
268
269} // namespace android