blob: 22f699e6d0da3f3324609ab12a1ddab205dce34f [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>
Andreas Huberf9334412010-12-15 15:17:42 -080028
29namespace android {
30
31NuPlayer::Decoder::Decoder(
Glenn Kasten11731182011-02-08 17:26:17 -080032 const sp<AMessage> &notify,
33 const sp<NativeWindowWrapper> &nativeWindow)
Andreas Huberf9334412010-12-15 15:17:42 -080034 : mNotify(notify),
Glenn Kasten11731182011-02-08 17:26:17 -080035 mNativeWindow(nativeWindow) {
Andreas Huberf9334412010-12-15 15:17:42 -080036}
37
38NuPlayer::Decoder::~Decoder() {
39}
40
Andreas Huber84066782011-08-16 09:34:26 -070041void NuPlayer::Decoder::configure(const sp<AMessage> &format) {
Andreas Huberf9334412010-12-15 15:17:42 -080042 CHECK(mCodec == NULL);
Andreas Huberf9334412010-12-15 15:17:42 -080043
Andreas Huber84066782011-08-16 09:34:26 -070044 AString mime;
45 CHECK(format->findString("mime", &mime));
Andreas Huberf9334412010-12-15 15:17:42 -080046
47 sp<AMessage> notifyMsg =
48 new AMessage(kWhatCodecNotify, id());
49
Andreas Huber84066782011-08-16 09:34:26 -070050 mCSDIndex = 0;
51 for (size_t i = 0;; ++i) {
52 sp<ABuffer> csd;
53 if (!format->findBuffer(StringPrintf("csd-%d", i).c_str(), &csd)) {
54 break;
55 }
56
57 mCSD.push(csd);
58 }
Andreas Huberf9334412010-12-15 15:17:42 -080059
Glenn Kasten11731182011-02-08 17:26:17 -080060 if (mNativeWindow != NULL) {
61 format->setObject("native-window", mNativeWindow);
Andreas Huberf9334412010-12-15 15:17:42 -080062 }
63
Andreas Huber078cfcf2011-09-15 12:25:04 -070064 // Current video decoders do not return from OMX_FillThisBuffer
65 // quickly, violating the OpenMAX specs, until that is remedied
66 // we need to invest in an extra looper to free the main event
67 // queue.
Andreas Huber84066782011-08-16 09:34:26 -070068 bool needDedicatedLooper = !strncasecmp(mime.c_str(), "video/", 6);
Andreas Huber078cfcf2011-09-15 12:25:04 -070069
Andreas Huber00f49512011-05-11 14:15:13 -070070 mCodec = new ACodec;
Andreas Huber078cfcf2011-09-15 12:25:04 -070071
72 if (needDedicatedLooper && mCodecLooper == NULL) {
73 mCodecLooper = new ALooper;
74 mCodecLooper->setName("NuPlayerDecoder");
75 mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
76 }
77
78 (needDedicatedLooper ? mCodecLooper : looper())->registerHandler(mCodec);
Andreas Huberf9334412010-12-15 15:17:42 -080079
Andreas Huber00f49512011-05-11 14:15:13 -070080 mCodec->setNotificationMessage(notifyMsg);
81 mCodec->initiateSetup(format);
Andreas Huberf9334412010-12-15 15:17:42 -080082}
83
84void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) {
85 switch (msg->what()) {
86 case kWhatCodecNotify:
87 {
88 int32_t what;
89 CHECK(msg->findInt32("what", &what));
90
91 if (what == ACodec::kWhatFillThisBuffer) {
92 onFillThisBuffer(msg);
93 } else {
94 sp<AMessage> notify = mNotify->dup();
95 notify->setMessage("codec-request", msg);
96 notify->post();
97 }
98 break;
99 }
100
101 default:
102 TRESPASS();
103 break;
104 }
105}
106
Andreas Huberf9334412010-12-15 15:17:42 -0800107void NuPlayer::Decoder::onFillThisBuffer(const sp<AMessage> &msg) {
108 sp<AMessage> reply;
109 CHECK(msg->findMessage("reply", &reply));
110
111#if 0
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800112 sp<ABuffer> outBuffer;
113 CHECK(msg->findBuffer("buffer", &outBuffer));
Andreas Huberf9334412010-12-15 15:17:42 -0800114#else
115 sp<ABuffer> outBuffer;
116#endif
117
118 if (mCSDIndex < mCSD.size()) {
119 outBuffer = mCSD.editItemAt(mCSDIndex++);
120 outBuffer->meta()->setInt64("timeUs", 0);
121
Andreas Huber2d8bedd2012-02-21 14:38:23 -0800122 reply->setBuffer("buffer", outBuffer);
Andreas Huberf9334412010-12-15 15:17:42 -0800123 reply->post();
124 return;
125 }
126
127 sp<AMessage> notify = mNotify->dup();
128 notify->setMessage("codec-request", msg);
129 notify->post();
130}
131
132void NuPlayer::Decoder::signalFlush() {
133 if (mCodec != NULL) {
134 mCodec->signalFlush();
Andreas Huberf9334412010-12-15 15:17:42 -0800135 }
136}
137
138void NuPlayer::Decoder::signalResume() {
139 if (mCodec != NULL) {
140 mCodec->signalResume();
Andreas Huberf9334412010-12-15 15:17:42 -0800141 }
142}
143
Andreas Huber3831a062010-12-21 10:22:33 -0800144void NuPlayer::Decoder::initiateShutdown() {
145 if (mCodec != NULL) {
146 mCodec->initiateShutdown();
Andreas Huber3831a062010-12-21 10:22:33 -0800147 }
148}
149
Andreas Huberf9334412010-12-15 15:17:42 -0800150} // namespace android
151