AMRWriter.cpp revision 2dce41ad26cb3e9e15c9e456a84bcf5309548ca0
1/*
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    mReachedEOS = false;
119    mDone = false;
120
121    pthread_create(&mThread, &attr, ThreadWrapper, this);
122    pthread_attr_destroy(&attr);
123
124    mStarted = true;
125
126    return OK;
127}
128
129void AMRWriter::stop() {
130    {
131        Mutex::Autolock autoLock(mLock);
132
133        if (!mStarted) {
134            return;
135        }
136
137        mDone = true;
138    }
139
140    void *dummy;
141    pthread_join(mThread, &dummy);
142
143    mSource->stop();
144
145    mStarted = false;
146}
147
148// static
149void *AMRWriter::ThreadWrapper(void *me) {
150    static_cast<AMRWriter *>(me)->threadFunc();
151
152    return NULL;
153}
154
155void AMRWriter::threadFunc() {
156    for (;;) {
157        Mutex::Autolock autoLock(mLock);
158
159        if (mDone) {
160            break;
161        }
162
163        MediaBuffer *buffer;
164        status_t err = mSource->read(&buffer);
165
166        if (err != OK) {
167            break;
168        }
169
170        ssize_t n = fwrite(
171                (const uint8_t *)buffer->data() + buffer->range_offset(),
172                1,
173                buffer->range_length(),
174                mFile);
175
176        buffer->release();
177        buffer = NULL;
178
179        if (n < (ssize_t)buffer->range_length()) {
180            break;
181        }
182    }
183
184    Mutex::Autolock autoLock(mLock);
185    mReachedEOS = true;
186}
187
188bool AMRWriter::reachedEOS() {
189    Mutex::Autolock autoLock(mLock);
190    return mReachedEOS;
191}
192
193}  // namespace android
194