blob: 6b27e5274aab71a1ea675de08a0d3d8b30bb1732 [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -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//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayerDecoder"
19#include <utils/Log.h>
20
21#include "NuPlayerDecoder.h"
22
Andreas Huberf9334412010-12-15 15:17:42 -080023#include <media/stagefright/foundation/ABuffer.h>
24#include <media/stagefright/foundation/ADebug.h>
Andreas Huber5bc087c2010-12-23 10:27:40 -080025#include <media/stagefright/foundation/AMessage.h>
Andreas Huberf9334412010-12-15 15:17:42 -080026#include <media/stagefright/ACodec.h>
27#include <media/stagefright/MediaDefs.h>
Alok Kediya601e9b52013-09-27 11:55:10 +053028#include <media/stagefright/ExtendedCodec.h>
Andreas Huberf9334412010-12-15 15:17:42 -080029
30namespace android {
31
32NuPlayer::Decoder::Decoder(
Glenn Kasten11731182011-02-08 17:26:17 -080033 const sp<AMessage> &notify,
34 const sp<NativeWindowWrapper> &nativeWindow)
Andreas Huberf9334412010-12-15 15:17:42 -080035 : mNotify(notify),
Glenn Kasten11731182011-02-08 17:26:17 -080036 mNativeWindow(nativeWindow) {
Andreas Huberf9334412010-12-15 15:17:42 -080037}
38
39NuPlayer::Decoder::~Decoder() {
40}
41
Andreas Huber84066782011-08-16 09:34:26 -070042void NuPlayer::Decoder::configure(const sp<AMessage> &format) {
Andreas Huberf9334412010-12-15 15:17:42 -080043 CHECK(mCodec == NULL);
Andreas Huberf9334412010-12-15 15:17:42 -080044
Andreas Huber84066782011-08-16 09:34:26 -070045 AString mime;
46 CHECK(format->findString("mime", &mime));
Andreas Huberf9334412010-12-15 15:17:42 -080047
48 sp<AMessage> notifyMsg =
49 new AMessage(kWhatCodecNotify, id());
50
Andreas Huber84066782011-08-16 09:34:26 -070051 mCSDIndex = 0;
52 for (size_t i = 0;; ++i) {
53 sp<ABuffer> csd;
54 if (!format->findBuffer(StringPrintf("csd-%d", i).c_str(), &csd)) {
55 break;
56 }
57
58 mCSD.push(csd);
59 }
Andreas Huberf9334412010-12-15 15:17:42 -080060
Alok Kediya601e9b52013-09-27 11:55:10 +053061 sp<ABuffer> extendedCSD = ExtendedCodec::getRawCodecSpecificData(format);
62 if (extendedCSD != NULL) {
63 ALOGV("pushing extended CSD of size %d", extendedCSD->size());
64 mCSD.push(extendedCSD);
65 }
66
67 sp<ABuffer> aacCSD = ExtendedCodec::getAacCodecSpecificData(format);
68 if (aacCSD != NULL) {
69 ALOGV("pushing AAC CSD of size %d", aacCSD->size());
70 mCSD.push(aacCSD);
71 }
72
Glenn Kasten11731182011-02-08 17:26:17 -080073 if (mNativeWindow != NULL) {
74 format->setObject("native-window", mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -080075 }
76
Andreas Huber078cfcf2011-09-15 12:25:04 -070077 // Current video decoders do not return from OMX_FillThisBuffer
78 // quickly, violating the OpenMAX specs, until that is remedied
79 // we need to invest in an extra looper to free the main event
80 // queue.
Andreas Huber84066782011-08-16 09:34:26 -070081 bool needDedicatedLooper = !strncasecmp(mime.c_str(), "video/", 6);
Andreas Huber078cfcf2011-09-15 12:25:04 -070082
Andreas Huber00f49512011-05-11 14:15:13 -070083 mCodec = new ACodec;
Andreas Huber078cfcf2011-09-15 12:25:04 -070084
85 if (needDedicatedLooper && mCodecLooper == NULL) {
86 mCodecLooper = new ALooper;
87 mCodecLooper->setName("NuPlayerDecoder");
88 mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
89 }
90
91 (needDedicatedLooper ? mCodecLooper : looper())->registerHandler(mCodec);
Andreas Huberf9334412010-12-15 15:17:42 -080092
Andreas Huber00f49512011-05-11 14:15:13 -070093 mCodec->setNotificationMessage(notifyMsg);
94 mCodec->initiateSetup(format);
Andreas Huberf9334412010-12-15 15:17:42 -080095}
96
97void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
98 switch (msg->what()) {
99 case kWhatCodecNotify:
100 {
101 int32_t what;
102 CHECK(msg->findInt32("what", &what));
103
104 if (what == ACodec::kWhatFillThisBuffer) {
105 onFillThisBuffer(msg);
106 } else {
107 sp<AMessage> notify = mNotify->dup();
108 notify->setMessage("codec-request", msg);
109 notify->post();
110 }
111 break;
112 }
113
114 default:
115 TRESPASS();
116 break;
117 }
118}
119
Andreas Huberf9334412010-12-15 15:17:42 -0800120void NuPlayer::Decoder::onFillThisBuffer(const sp<AMessage> &msg) {
121 sp<AMessage> reply;
122 CHECK(msg->findMessage("reply", &reply));
123
124#if 0
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800125 sp<ABuffer> outBuffer;
126 CHECK(msg->findBuffer("buffer", &outBuffer));
Andreas Huberf9334412010-12-15 15:17:42 -0800127#else
128 sp<ABuffer> outBuffer;
129#endif
130
131 if (mCSDIndex < mCSD.size()) {
132 outBuffer = mCSD.editItemAt(mCSDIndex++);
133 outBuffer->meta()->setInt64("timeUs", 0);
134
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800135 reply->setBuffer("buffer", outBuffer);
Andreas Huberf9334412010-12-15 15:17:42 -0800136 reply->post();
137 return;
138 }
139
140 sp<AMessage> notify = mNotify->dup();
141 notify->setMessage("codec-request", msg);
142 notify->post();
143}
144
145void NuPlayer::Decoder::signalFlush() {
146 if (mCodec != NULL) {
147 mCodec->signalFlush();
Andreas Huberf9334412010-12-15 15:17:42 -0800148 }
149}
150
151void NuPlayer::Decoder::signalResume() {
152 if (mCodec != NULL) {
153 mCodec->signalResume();
Andreas Huberf9334412010-12-15 15:17:42 -0800154 }
155}
156
Andreas Huber3831a062010-12-21 10:22:33 -0800157void NuPlayer::Decoder::initiateShutdown() {
158 if (mCodec != NULL) {
159 mCodec->initiateShutdown();
Andreas Huber3831a062010-12-21 10:22:33 -0800160 }
161}
162
Andreas Huberf9334412010-12-15 15:17:42 -0800163} // namespace android
164