AMRWriter.cpp revision f0ce2fb0c7bf3a414279e5aba61105f3d9025c0e
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#include <media/mediarecorder.h>
26
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) {
57    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() {
97    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
115    mReachedEOS = false;
116    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() {
127    if (!mStarted) {
128        return;
129    }
130
131    mDone = true;
132
133    void *dummy;
134    pthread_join(mThread, &dummy);
135
136    mSource->stop();
137
138    mStarted = false;
139}
140
141bool 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
155// static
156void *AMRWriter::ThreadWrapper(void *me) {
157    static_cast<AMRWriter *>(me)->threadFunc();
158
159    return NULL;
160}
161
162void AMRWriter::threadFunc() {
163    mEstimatedDurationUs = 0;
164    mEstimatedSizeBytes = 0;
165    bool stoppedPrematurely = true;
166    while (!mDone) {
167        MediaBuffer *buffer;
168        status_t err = mSource->read(&buffer);
169
170        if (err != OK) {
171            break;
172        }
173
174        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        }
193        ssize_t n = fwrite(
194                (const uint8_t *)buffer->data() + buffer->range_offset(),
195                1,
196                buffer->range_length(),
197                mFile);
198
199        if (n < (ssize_t)buffer->range_length()) {
200            buffer->release();
201            buffer = NULL;
202
203            break;
204        }
205
206        // XXX: How to tell it is stopped prematurely?
207        if (stoppedPrematurely) {
208            stoppedPrematurely = false;
209        }
210
211        buffer->release();
212        buffer = NULL;
213    }
214
215    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;
222    mReachedEOS = true;
223}
224
225bool AMRWriter::reachedEOS() {
226    return mReachedEOS;
227}
228
229}  // namespace android
230