blob: aec73945f00b8b56bacf225779c8186ecf968371 [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),
32 mStarted(false) {
33}
34
35AMRWriter::AMRWriter(int fd)
36 : mFile(fdopen(fd, "wb")),
37 mInitCheck(mFile != NULL ? OK : NO_INIT),
38 mStarted(false) {
39}
40
41AMRWriter::~AMRWriter() {
42 if (mStarted) {
43 stop();
44 }
45
46 if (mFile != NULL) {
47 fclose(mFile);
48 mFile = NULL;
49 }
50}
51
52status_t AMRWriter::initCheck() const {
53 return mInitCheck;
54}
55
56status_t AMRWriter::addSource(const sp<MediaSource> &source) {
Andreas Huber07bf09d2010-01-25 14:27:12 -080057 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
96status_t AMRWriter::start() {
Andreas Huber07bf09d2010-01-25 14:27:12 -080097 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 Huber996dddf2010-01-25 15:30:31 -0800115 mReachedEOS = false;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800116 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
126void AMRWriter::stop() {
Andreas Hubere2018ca2010-03-23 14:33:02 -0700127 if (!mStarted) {
128 return;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800129 }
130
Andreas Hubere2018ca2010-03-23 14:33:02 -0700131 mDone = true;
132
Andreas Huber07bf09d2010-01-25 14:27:12 -0800133 void *dummy;
134 pthread_join(mThread, &dummy);
135
136 mSource->stop();
137
138 mStarted = false;
139}
140
James Dong18244862010-05-11 14:57:02 -0700141bool AMRWriter::exceedsFileSizeLimit() {
142 if (mMaxFileSizeLimitBytes == 0) {
143 return false;
144 }
145 return mEstimatedSizeBytes >= mMaxFileSizeLimitBytes;
146}
147
148bool AMRWriter::exceedsFileDurationLimit() {
149 if (mMaxFileDurationLimitUs == 0) {
150 return false;
151 }
152 return mEstimatedDurationUs >= mMaxFileDurationLimitUs;
153}
154
Andreas Huber07bf09d2010-01-25 14:27:12 -0800155// static
156void *AMRWriter::ThreadWrapper(void *me) {
157 static_cast<AMRWriter *>(me)->threadFunc();
158
159 return NULL;
160}
161
162void AMRWriter::threadFunc() {
James Dong18244862010-05-11 14:57:02 -0700163 mEstimatedDurationUs = 0;
164 mEstimatedSizeBytes = 0;
James Dong68510e62010-05-14 11:48:00 -0700165 bool stoppedPrematurely = true;
Andreas Hubere2018ca2010-03-23 14:33:02 -0700166 while (!mDone) {
Andreas Huber07bf09d2010-01-25 14:27:12 -0800167 MediaBuffer *buffer;
168 status_t err = mSource->read(&buffer);
169
170 if (err != OK) {
171 break;
172 }
173
James Dong18244862010-05-11 14:57:02 -0700174 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, &timestampUs));
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 Huber07bf09d2010-01-25 14:27:12 -0800193 ssize_t n = fwrite(
194 (const uint8_t *)buffer->data() + buffer->range_offset(),
195 1,
196 buffer->range_length(),
197 mFile);
198
Andreas Huber07bf09d2010-01-25 14:27:12 -0800199 if (n < (ssize_t)buffer->range_length()) {
Andreas Huber259b7c12010-02-10 15:04:31 -0800200 buffer->release();
201 buffer = NULL;
202
Andreas Huber07bf09d2010-01-25 14:27:12 -0800203 break;
204 }
Andreas Huber259b7c12010-02-10 15:04:31 -0800205
James Dong68510e62010-05-14 11:48:00 -0700206 // XXX: How to tell it is stopped prematurely?
207 if (stoppedPrematurely) {
208 stoppedPrematurely = false;
209 }
210
Andreas Huber259b7c12010-02-10 15:04:31 -0800211 buffer->release();
212 buffer = NULL;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800213 }
Andreas Huber996dddf2010-01-25 15:30:31 -0800214
James Dong68510e62010-05-14 11:48:00 -0700215 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 Huber996dddf2010-01-25 15:30:31 -0800222 mReachedEOS = true;
223}
224
225bool AMRWriter::reachedEOS() {
Andreas Huber996dddf2010-01-25 15:30:31 -0800226 return mReachedEOS;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800227}
228
229} // namespace android