blob: 73ea56d2b612fc9dc27a9e0186ee9f2538343890 [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) {
Andreas Huber07bf09d2010-01-25 14:27:12 -080056 if (mInitCheck != OK) {
57 return mInitCheck;
58 }
59
60 if (mSource != NULL) {
61 // AMR files only support a single track of audio.
62 return UNKNOWN_ERROR;
63 }
64
65 sp<MetaData> meta = source->getFormat();
66
67 const char *mime;
68 CHECK(meta->findCString(kKeyMIMEType, &mime));
69
70 bool isWide = false;
71 if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_WB)) {
72 isWide = true;
73 } else if (strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AMR_NB)) {
74 return ERROR_UNSUPPORTED;
75 }
76
77 int32_t channelCount;
78 int32_t sampleRate;
79 CHECK(meta->findInt32(kKeyChannelCount, &channelCount));
80 CHECK_EQ(channelCount, 1);
81 CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
82 CHECK_EQ(sampleRate, (isWide ? 16000 : 8000));
83
84 mSource = source;
85
86 const char *kHeader = isWide ? "#!AMR-WB\n" : "#!AMR\n";
87 size_t n = strlen(kHeader);
88 if (fwrite(kHeader, 1, n, mFile) != n) {
89 return ERROR_IO;
90 }
91
92 return OK;
93}
94
95status_t AMRWriter::start() {
Andreas Huber07bf09d2010-01-25 14:27:12 -080096 if (mInitCheck != OK) {
97 return mInitCheck;
98 }
99
100 if (mStarted || mSource == NULL) {
101 return UNKNOWN_ERROR;
102 }
103
104 status_t err = mSource->start();
105
106 if (err != OK) {
107 return err;
108 }
109
110 pthread_attr_t attr;
111 pthread_attr_init(&attr);
112 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
113
Andreas Huber996dddf2010-01-25 15:30:31 -0800114 mReachedEOS = false;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800115 mDone = false;
116
117 pthread_create(&mThread, &attr, ThreadWrapper, this);
118 pthread_attr_destroy(&attr);
119
120 mStarted = true;
121
122 return OK;
123}
124
125void AMRWriter::stop() {
Andreas Hubere2018ca2010-03-23 14:33:02 -0700126 if (!mStarted) {
127 return;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800128 }
129
Andreas Hubere2018ca2010-03-23 14:33:02 -0700130 mDone = true;
131
Andreas Huber07bf09d2010-01-25 14:27:12 -0800132 void *dummy;
133 pthread_join(mThread, &dummy);
134
135 mSource->stop();
136
137 mStarted = false;
138}
139
140// static
141void *AMRWriter::ThreadWrapper(void *me) {
142 static_cast<AMRWriter *>(me)->threadFunc();
143
144 return NULL;
145}
146
147void AMRWriter::threadFunc() {
Andreas Hubere2018ca2010-03-23 14:33:02 -0700148 while (!mDone) {
Andreas Huber07bf09d2010-01-25 14:27:12 -0800149 MediaBuffer *buffer;
150 status_t err = mSource->read(&buffer);
151
152 if (err != OK) {
153 break;
154 }
155
156 ssize_t n = fwrite(
157 (const uint8_t *)buffer->data() + buffer->range_offset(),
158 1,
159 buffer->range_length(),
160 mFile);
161
Andreas Huber07bf09d2010-01-25 14:27:12 -0800162 if (n < (ssize_t)buffer->range_length()) {
Andreas Huber259b7c12010-02-10 15:04:31 -0800163 buffer->release();
164 buffer = NULL;
165
Andreas Huber07bf09d2010-01-25 14:27:12 -0800166 break;
167 }
Andreas Huber259b7c12010-02-10 15:04:31 -0800168
169 buffer->release();
170 buffer = NULL;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800171 }
Andreas Huber996dddf2010-01-25 15:30:31 -0800172
Andreas Huber996dddf2010-01-25 15:30:31 -0800173 mReachedEOS = true;
174}
175
176bool AMRWriter::reachedEOS() {
Andreas Huber996dddf2010-01-25 15:30:31 -0800177 return mReachedEOS;
Andreas Huber07bf09d2010-01-25 14:27:12 -0800178}
179
180} // namespace android