blob: 7b681f12c53b911f17bd0f71b360238f521390bf [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>
25
26namespace android {
27
28AMRWriter::AMRWriter(const char *filename)
29 : mFile(fopen(filename, "wb")),
30 mInitCheck(mFile != NULL ? OK : NO_INIT),
31 mStarted(false) {
32}
33
34AMRWriter::AMRWriter(int fd)
35 : mFile(fdopen(fd, "wb")),
36 mInitCheck(mFile != NULL ? OK : NO_INIT),
37 mStarted(false) {
38}
39
40AMRWriter::~AMRWriter() {
41 if (mStarted) {
42 stop();
43 }
44
45 if (mFile != NULL) {
46 fclose(mFile);
47 mFile = NULL;
48 }
49}
50
51status_t AMRWriter::initCheck() const {
52 return mInitCheck;
53}
54
55status_t AMRWriter::addSource(const sp<MediaSource> &source) {
56 Mutex::Autolock autoLock(mLock);
57
58 if (mInitCheck != OK) {
59 return mInitCheck;
60 }
61
62 if (mSource != NULL) {
63 // AMR files only support a single track of audio.
64 return UNKNOWN_ERROR;
65 }
66
67 sp<MetaData> meta = source->getFormat();
68
69 const char *mime;
70 CHECK(meta->findCString(kKeyMIMEType, &mime));
71
72 bool isWide = false;
73 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_WB)) {
74 isWide = true;
75 } else if (strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_NB)) {
76 return ERROR_UNSUPPORTED;
77 }
78
79 int32_t channelCount;
80 int32_t sampleRate;
81 CHECK(meta->findInt32(kKeyChannelCount, &channelCount));
82 CHECK_EQ(channelCount, 1);
83 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
84 CHECK_EQ(sampleRate, (isWide ? 16000 : 8000));
85
86 mSource = source;
87
88 const char *kHeader = isWide ? "#!AMR-WB\n" : "#!AMR\n";
89 size_t n = strlen(kHeader);
90 if (fwrite(kHeader, 1, n, mFile) != n) {
91 return ERROR_IO;
92 }
93
94 return OK;
95}
96
97status_t AMRWriter::start() {
98 Mutex::Autolock autoLock(mLock);
99
100 if (mInitCheck != OK) {
101 return mInitCheck;
102 }
103
104 if (mStarted || mSource == NULL) {
105 return UNKNOWN_ERROR;
106 }
107
108 status_t err = mSource->start();
109
110 if (err != OK) {
111 return err;
112 }
113
114 pthread_attr_t attr;
115 pthread_attr_init(&attr);
116 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
117
118 mDone = false;
119
120 pthread_create(&mThread, &attr, ThreadWrapper, this);
121 pthread_attr_destroy(&attr);
122
123 mStarted = true;
124
125 return OK;
126}
127
128void AMRWriter::stop() {
129 {
130 Mutex::Autolock autoLock(mLock);
131
132 if (!mStarted) {
133 return;
134 }
135
136 mDone = true;
137 }
138
139 void *dummy;
140 pthread_join(mThread, &dummy);
141
142 mSource->stop();
143
144 mStarted = false;
145}
146
147// static
148void *AMRWriter::ThreadWrapper(void *me) {
149 static_cast<AMRWriter *>(me)->threadFunc();
150
151 return NULL;
152}
153
154void AMRWriter::threadFunc() {
155 for (;;) {
156 Mutex::Autolock autoLock(mLock);
157
158 if (mDone) {
159 break;
160 }
161
162 MediaBuffer *buffer;
163 status_t err = mSource->read(&buffer);
164
165 if (err != OK) {
166 break;
167 }
168
169 ssize_t n = fwrite(
170 (const uint8_t *)buffer->data() + buffer->range_offset(),
171 1,
172 buffer->range_length(),
173 mFile);
174
175 buffer->release();
176 buffer = NULL;
177
178 if (n < (ssize_t)buffer->range_length()) {
179 break;
180 }
181 }
182}
183
184} // namespace android