NuPlayerDecoder.cpp revision afed0e1fa37473a4cd30018577b560acc79d9a3f
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 {
120        CHECK(!strncasecmp("audio/", mime, 6));
121
122        int32_t numChannels, sampleRate;
123        CHECK(meta->findInt32(kKeyChannelCount, &numChannels));
124        CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
125
126        msg->setInt32("channel-count", numChannels);
127        msg->setInt32("sample-rate", sampleRate);
128    }
129
130    int32_t maxInputSize;
131    if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
132        msg->setInt32("max-input-size", maxInputSize);
133    }
134
135    mCSDIndex = 0;
136
137    uint32_t type;
138    const void *data;
139    size_t size;
140    if (meta->findData(kKeyAVCC, &type, &data, &size)) {
141        // Parse the AVCDecoderConfigurationRecord
142
143        const uint8_t *ptr = (const uint8_t *)data;
144
145        CHECK(size >= 7);
146        CHECK_EQ((unsigned)ptr[0], 1u);  // configurationVersion == 1
147        uint8_t profile = ptr[1];
148        uint8_t level = ptr[3];
149
150        // There is decodable content out there that fails the following
151        // assertion, let's be lenient for now...
152        // CHECK((ptr[4] >> 2) == 0x3f);  // reserved
153
154        size_t lengthSize = 1 + (ptr[4] & 3);
155
156        // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
157        // violates it...
158        // CHECK((ptr[5] >> 5) == 7);  // reserved
159
160        size_t numSeqParameterSets = ptr[5] & 31;
161
162        ptr += 6;
163        size -= 6;
164
165        sp<ABuffer> buffer = new ABuffer(1024);
166        buffer->setRange(0, 0);
167
168        for (size_t i = 0; i < numSeqParameterSets; ++i) {
169            CHECK(size >= 2);
170            size_t length = U16_AT(ptr);
171
172            ptr += 2;
173            size -= 2;
174
175            CHECK(size >= length);
176
177            memcpy(buffer->data() + buffer->size(), "\x00\x00\x00\x01", 4);
178            memcpy(buffer->data() + buffer->size() + 4, ptr, length);
179            buffer->setRange(0, buffer->size() + 4 + length);
180
181            ptr += length;
182            size -= length;
183        }
184
185        buffer->meta()->setInt32("csd", true);
186        mCSD.push(buffer);
187
188        buffer = new ABuffer(1024);
189        buffer->setRange(0, 0);
190
191        CHECK(size >= 1);
192        size_t numPictureParameterSets = *ptr;
193        ++ptr;
194        --size;
195
196        for (size_t i = 0; i < numPictureParameterSets; ++i) {
197            CHECK(size >= 2);
198            size_t length = U16_AT(ptr);
199
200            ptr += 2;
201            size -= 2;
202
203            CHECK(size >= length);
204
205            memcpy(buffer->data() + buffer->size(), "\x00\x00\x00\x01", 4);
206            memcpy(buffer->data() + buffer->size() + 4, ptr, length);
207            buffer->setRange(0, buffer->size() + 4 + length);
208
209            ptr += length;
210            size -= length;
211        }
212
213        buffer->meta()->setInt32("csd", true);
214        mCSD.push(buffer);
215    } else if (meta->findData(kKeyESDS, &type, &data, &size)) {
216        ESDS esds((const char *)data, size);
217        CHECK_EQ(esds.InitCheck(), (status_t)OK);
218
219        const void *codec_specific_data;
220        size_t codec_specific_data_size;
221        esds.getCodecSpecificInfo(
222                &codec_specific_data, &codec_specific_data_size);
223
224        sp<ABuffer> buffer = new ABuffer(codec_specific_data_size);
225
226        memcpy(buffer->data(), codec_specific_data,
227               codec_specific_data_size);
228
229        buffer->meta()->setInt32("csd", true);
230        mCSD.push(buffer);
231    } else if (meta->findData(kKeyVorbisInfo, &type, &data, &size)) {
232        sp<ABuffer> buffer = new ABuffer(size);
233        memcpy(buffer->data(), data, size);
234
235        buffer->meta()->setInt32("csd", true);
236        mCSD.push(buffer);
237
238        CHECK(meta->findData(kKeyVorbisBooks, &type, &data, &size));
239
240        buffer = new ABuffer(size);
241        memcpy(buffer->data(), data, size);
242
243        buffer->meta()->setInt32("csd", true);
244        mCSD.push(buffer);
245    }
246
247    return msg;
248}
249
250void NuPlayer::Decoder::onFillThisBuffer(const sp<AMessage> &msg) {
251    sp<AMessage> reply;
252    CHECK(msg->findMessage("reply", &reply));
253
254#if 0
255    sp<ABuffer> outBuffer;
256    CHECK(msg->findBuffer("buffer", &outBuffer));
257#else
258    sp<ABuffer> outBuffer;
259#endif
260
261    if (mCSDIndex < mCSD.size()) {
262        outBuffer = mCSD.editItemAt(mCSDIndex++);
263        outBuffer->meta()->setInt64("timeUs", 0);
264
265        reply->setBuffer("buffer", outBuffer);
266        reply->post();
267        return;
268    }
269
270    sp<AMessage> notify = mNotify->dup();
271    notify->setMessage("codec-request", msg);
272    notify->post();
273}
274
275void NuPlayer::Decoder::signalFlush() {
276    if (mCodec != NULL) {
277        mCodec->signalFlush();
278    }
279}
280
281void NuPlayer::Decoder::signalResume() {
282    if (mCodec != NULL) {
283        mCodec->signalResume();
284    }
285}
286
287void NuPlayer::Decoder::initiateShutdown() {
288    if (mCodec != NULL) {
289        mCodec->initiateShutdown();
290    }
291}
292
293}  // namespace android
294
295