NuPlayerDecoder.cpp revision e370bb62b89d2f2980f519392010ea08a24c558e
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//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayerDecoder"
19#include <utils/Log.h>
20
21#include "NuPlayerDecoder.h"
22
23#include "ESDS.h"
24
25#include <media/stagefright/foundation/ABuffer.h>
26#include <media/stagefright/foundation/ADebug.h>
27#include <media/stagefright/foundation/AMessage.h>
28#include <media/stagefright/ACodec.h>
29#include <media/stagefright/MediaDefs.h>
30#include <media/stagefright/MetaData.h>
31#include <media/stagefright/Utils.h>
32
33namespace android {
34
35NuPlayer::Decoder::Decoder(
36        const sp<AMessage> &notify,
37        const sp<NativeWindowWrapper> &nativeWindow)
38    : mNotify(notify),
39      mNativeWindow(nativeWindow) {
40}
41
42NuPlayer::Decoder::~Decoder() {
43}
44
45void NuPlayer::Decoder::configure(const sp<MetaData> &meta) {
46    CHECK(mCodec == NULL);
47
48    const char *mime;
49    CHECK(meta->findCString(kKeyMIMEType, &mime));
50
51    sp<AMessage> notifyMsg =
52        new AMessage(kWhatCodecNotify, id());
53
54    sp<AMessage> format = makeFormat(meta);
55
56    if (mNativeWindow != NULL) {
57        format->setObject("native-window", mNativeWindow);
58    }
59
60    // Current video decoders do not return from OMX_FillThisBuffer
61    // quickly, violating the OpenMAX specs, until that is remedied
62    // we need to invest in an extra looper to free the main event
63    // queue.
64    bool needDedicatedLooper = !strncasecmp(mime, "video/", 6);
65
66    mCodec = new ACodec;
67
68    if (needDedicatedLooper && mCodecLooper == NULL) {
69        mCodecLooper = new ALooper;
70        mCodecLooper->setName("NuPlayerDecoder");
71        mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
72    }
73
74    (needDedicatedLooper ? mCodecLooper : looper())->registerHandler(mCodec);
75
76    mCodec->setNotificationMessage(notifyMsg);
77    mCodec->initiateSetup(format);
78}
79
80void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
81    switch (msg->what()) {
82        case kWhatCodecNotify:
83        {
84            int32_t what;
85            CHECK(msg->findInt32("what", &what));
86
87            if (what == ACodec::kWhatFillThisBuffer) {
88                onFillThisBuffer(msg);
89            } else {
90                sp<AMessage> notify = mNotify->dup();
91                notify->setMessage("codec-request", msg);
92                notify->post();
93            }
94            break;
95        }
96
97        default:
98            TRESPASS();
99            break;
100    }
101}
102
103sp<AMessage> NuPlayer::Decoder::makeFormat(const sp<MetaData> &meta) {
104    CHECK(mCSD.isEmpty());
105
106    const char *mime;
107    CHECK(meta->findCString(kKeyMIMEType, &mime));
108
109    sp<AMessage> msg = new AMessage;
110    msg->setString("mime", mime);
111
112    if (!strncasecmp("video/", mime, 6)) {
113        int32_t width, height;
114        CHECK(meta->findInt32(kKeyWidth, &width));
115        CHECK(meta->findInt32(kKeyHeight, &height));
116
117        msg->setInt32("width", width);
118        msg->setInt32("height", height);
119    } else if (!strncasecmp("audio/", mime, 6)) {
120        int32_t numChannels, sampleRate;
121        CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
122        CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
123
124        msg->setInt32("channel-count", numChannels);
125        msg->setInt32("sample-rate", sampleRate);
126
127        int32_t isADTS;
128        if (meta->findInt32(kKeyIsADTS, &isADTS) && isADTS != 0) {
129            msg->setInt32("is-adts", true);
130        }
131    }
132
133    int32_t maxInputSize;
134    if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
135        msg->setInt32("max-input-size", maxInputSize);
136    }
137
138    mCSDIndex = 0;
139
140    uint32_t type;
141    const void *data;
142    size_t size;
143    if (meta->findData(kKeyAVCC, &type, &data, &size)) {
144        // Parse the AVCDecoderConfigurationRecord
145
146        const uint8_t *ptr = (const uint8_t *)data;
147
148        CHECK(size >= 7);
149        CHECK_EQ((unsigned)ptr[0], 1u);  // configurationVersion == 1
150        uint8_t profile = ptr[1];
151        uint8_t level = ptr[3];
152
153        // There is decodable content out there that fails the following
154        // assertion, let's be lenient for now...
155        // CHECK((ptr[4] >> 2) == 0x3f);  // reserved
156
157        size_t lengthSize = 1 + (ptr[4] & 3);
158
159        // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
160        // violates it...
161        // CHECK((ptr[5] >> 5) == 7);  // reserved
162
163        size_t numSeqParameterSets = ptr[5] & 31;
164
165        ptr += 6;
166        size -= 6;
167
168        sp<ABuffer> buffer = new ABuffer(1024);
169        buffer->setRange(0, 0);
170
171        for (size_t i = 0; i < numSeqParameterSets; ++i) {
172            CHECK(size >= 2);
173            size_t length = U16_AT(ptr);
174
175            ptr += 2;
176            size -= 2;
177
178            CHECK(size >= length);
179
180            memcpy(buffer->data() + buffer->size(), "\x00\x00\x00\x01", 4);
181            memcpy(buffer->data() + buffer->size() + 4, ptr, length);
182            buffer->setRange(0, buffer->size() + 4 + length);
183
184            ptr += length;
185            size -= length;
186        }
187
188        buffer->meta()->setInt32("csd", true);
189        mCSD.push(buffer);
190
191        buffer = new ABuffer(1024);
192        buffer->setRange(0, 0);
193
194        CHECK(size >= 1);
195        size_t numPictureParameterSets = *ptr;
196        ++ptr;
197        --size;
198
199        for (size_t i = 0; i < numPictureParameterSets; ++i) {
200            CHECK(size >= 2);
201            size_t length = U16_AT(ptr);
202
203            ptr += 2;
204            size -= 2;
205
206            CHECK(size >= length);
207
208            memcpy(buffer->data() + buffer->size(), "\x00\x00\x00\x01", 4);
209            memcpy(buffer->data() + buffer->size() + 4, ptr, length);
210            buffer->setRange(0, buffer->size() + 4 + length);
211
212            ptr += length;
213            size -= length;
214        }
215
216        buffer->meta()->setInt32("csd", true);
217        mCSD.push(buffer);
218    } else if (meta->findData(kKeyESDS, &type, &data, &size)) {
219        ESDS esds((const char *)data, size);
220        CHECK_EQ(esds.InitCheck(), (status_t)OK);
221
222        const void *codec_specific_data;
223        size_t codec_specific_data_size;
224        esds.getCodecSpecificInfo(
225                &codec_specific_data, &codec_specific_data_size);
226
227        sp<ABuffer> buffer = new ABuffer(codec_specific_data_size);
228
229        memcpy(buffer->data(), codec_specific_data,
230               codec_specific_data_size);
231
232        buffer->meta()->setInt32("csd", true);
233        mCSD.push(buffer);
234    } else if (meta->findData(kKeyVorbisInfo, &type, &data, &size)) {
235        sp<ABuffer> buffer = new ABuffer(size);
236        memcpy(buffer->data(), data, size);
237
238        buffer->meta()->setInt32("csd", true);
239        mCSD.push(buffer);
240
241        CHECK(meta->findData(kKeyVorbisBooks, &type, &data, &size));
242
243        buffer = new ABuffer(size);
244        memcpy(buffer->data(), data, size);
245
246        buffer->meta()->setInt32("csd", true);
247        mCSD.push(buffer);
248    }
249
250    return msg;
251}
252
253void NuPlayer::Decoder::onFillThisBuffer(const sp<AMessage> &msg) {
254    sp<AMessage> reply;
255    CHECK(msg->findMessage("reply", &reply));
256
257#if 0
258    sp<ABuffer> outBuffer;
259    CHECK(msg->findBuffer("buffer", &outBuffer));
260#else
261    sp<ABuffer> outBuffer;
262#endif
263
264    if (mCSDIndex < mCSD.size()) {
265        outBuffer = mCSD.editItemAt(mCSDIndex++);
266        outBuffer->meta()->setInt64("timeUs", 0);
267
268        reply->setBuffer("buffer", outBuffer);
269        reply->post();
270        return;
271    }
272
273    sp<AMessage> notify = mNotify->dup();
274    notify->setMessage("codec-request", msg);
275    notify->post();
276}
277
278void NuPlayer::Decoder::signalFlush() {
279    if (mCodec != NULL) {
280        mCodec->signalFlush();
281    }
282}
283
284void NuPlayer::Decoder::signalResume() {
285    if (mCodec != NULL) {
286        mCodec->signalResume();
287    }
288}
289
290void NuPlayer::Decoder::initiateShutdown() {
291    if (mCodec != NULL) {
292        mCodec->initiateShutdown();
293    }
294}
295
296}  // namespace android
297
298