blob: 36b41ec8f5594eb069e6839a4560c00236700ed2 [file] [log] [blame]
Chong Zhang7137ec72014-11-12 16:41:05 -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 "NuPlayerDecoderBase"
19#include <utils/Log.h>
20#include <inttypes.h>
21
22#include "NuPlayerDecoderBase.h"
23
24#include "NuPlayerRenderer.h"
25
26#include <media/stagefright/foundation/ADebug.h>
27#include <media/stagefright/foundation/AMessage.h>
28
29namespace android {
30
Andy Hung202bce12014-12-03 11:47:36 -080031NuPlayer::DecoderBase::DecoderBase(const sp<AMessage> &notify)
32 : mNotify(notify),
33 mBufferGeneration(0),
34 mRequestInputBuffersPending(false) {
Chong Zhang7137ec72014-11-12 16:41:05 -080035 // Every decoder has its own looper because MediaCodec operations
36 // are blocking, but NuPlayer needs asynchronous operations.
37 mDecoderLooper = new ALooper;
38 mDecoderLooper->setName("NPDecoder");
39 mDecoderLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
40}
41
42NuPlayer::DecoderBase::~DecoderBase() {
43 mDecoderLooper->unregisterHandler(id());
44 mDecoderLooper->stop();
45}
46
47static
48status_t PostAndAwaitResponse(
49 const sp<AMessage> &msg, sp<AMessage> *response) {
50 status_t err = msg->postAndAwaitResponse(response);
51
52 if (err != OK) {
53 return err;
54 }
55
56 if (!(*response)->findInt32("err", &err)) {
57 err = OK;
58 }
59
60 return err;
61}
62
63void NuPlayer::DecoderBase::configure(const sp<AMessage> &format) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -080064 sp<AMessage> msg = new AMessage(kWhatConfigure, this);
Chong Zhang7137ec72014-11-12 16:41:05 -080065 msg->setMessage("format", format);
66 msg->post();
67}
68
69void NuPlayer::DecoderBase::init() {
70 mDecoderLooper->registerHandler(this);
71}
72
73void NuPlayer::DecoderBase::setRenderer(const sp<Renderer> &renderer) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -080074 sp<AMessage> msg = new AMessage(kWhatSetRenderer, this);
Chong Zhang7137ec72014-11-12 16:41:05 -080075 msg->setObject("renderer", renderer);
76 msg->post();
77}
78
79status_t NuPlayer::DecoderBase::getInputBuffers(Vector<sp<ABuffer> > *buffers) const {
Lajos Molnar1d15ab52015-03-04 16:46:34 -080080 sp<AMessage> msg = new AMessage(kWhatGetInputBuffers, this);
Chong Zhang7137ec72014-11-12 16:41:05 -080081 msg->setPointer("buffers", buffers);
82
83 sp<AMessage> response;
84 return PostAndAwaitResponse(msg, &response);
85}
86
87void NuPlayer::DecoderBase::signalFlush() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -080088 (new AMessage(kWhatFlush, this))->post();
Chong Zhang7137ec72014-11-12 16:41:05 -080089}
90
Chong Zhangf8d71772014-11-26 15:08:34 -080091void NuPlayer::DecoderBase::signalResume(bool notifyComplete) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -080092 sp<AMessage> msg = new AMessage(kWhatResume, this);
Chong Zhangf8d71772014-11-26 15:08:34 -080093 msg->setInt32("notifyComplete", notifyComplete);
94 msg->post();
Chong Zhang7137ec72014-11-12 16:41:05 -080095}
96
97void NuPlayer::DecoderBase::initiateShutdown() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -080098 (new AMessage(kWhatShutdown, this))->post();
Chong Zhang7137ec72014-11-12 16:41:05 -080099}
100
101void NuPlayer::DecoderBase::onRequestInputBuffers() {
102 if (mRequestInputBuffersPending) {
103 return;
104 }
105
Chong Zhang3b032b32015-04-17 15:49:06 -0700106 // doRequestBuffers() return true if we should request more data
107 if (doRequestBuffers()) {
108 mRequestInputBuffersPending = true;
Chong Zhang7137ec72014-11-12 16:41:05 -0800109
Chong Zhang3b032b32015-04-17 15:49:06 -0700110 sp<AMessage> msg = new AMessage(kWhatRequestInputBuffers, this);
111 msg->post(10 * 1000ll);
Chong Zhang7137ec72014-11-12 16:41:05 -0800112 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800113}
114
115void NuPlayer::DecoderBase::onMessageReceived(const sp<AMessage> &msg) {
116
117 switch (msg->what()) {
118 case kWhatConfigure:
119 {
120 sp<AMessage> format;
121 CHECK(msg->findMessage("format", &format));
122 onConfigure(format);
123 break;
124 }
125
126 case kWhatSetRenderer:
127 {
128 sp<RefBase> obj;
129 CHECK(msg->findObject("renderer", &obj));
130 onSetRenderer(static_cast<Renderer *>(obj.get()));
131 break;
132 }
133
134 case kWhatGetInputBuffers:
135 {
Lajos Molnar3f274362015-03-05 14:35:41 -0800136 sp<AReplyToken> replyID;
Chong Zhang7137ec72014-11-12 16:41:05 -0800137 CHECK(msg->senderAwaitsResponse(&replyID));
138
139 Vector<sp<ABuffer> > *dstBuffers;
140 CHECK(msg->findPointer("buffers", (void **)&dstBuffers));
141
142 onGetInputBuffers(dstBuffers);
143
144 (new AMessage)->postReply(replyID);
145 break;
146 }
147
148 case kWhatRequestInputBuffers:
149 {
150 mRequestInputBuffersPending = false;
151 onRequestInputBuffers();
152 break;
153 }
154
155 case kWhatFlush:
156 {
Chong Zhang66704af2015-03-03 19:32:35 -0800157 onFlush();
Chong Zhang7137ec72014-11-12 16:41:05 -0800158 break;
159 }
160
161 case kWhatResume:
162 {
Chong Zhangf8d71772014-11-26 15:08:34 -0800163 int32_t notifyComplete;
164 CHECK(msg->findInt32("notifyComplete", &notifyComplete));
165
166 onResume(notifyComplete);
Chong Zhang7137ec72014-11-12 16:41:05 -0800167 break;
168 }
169
170 case kWhatShutdown:
171 {
172 onShutdown(true);
173 break;
174 }
175
176 default:
177 TRESPASS();
178 break;
179 }
180}
181
Andy Hung202bce12014-12-03 11:47:36 -0800182void NuPlayer::DecoderBase::handleError(int32_t err)
183{
184 // We cannot immediately release the codec due to buffers still outstanding
185 // in the renderer. We signal to the player the error so it can shutdown/release the
186 // decoder after flushing and increment the generation to discard unnecessary messages.
187
188 ++mBufferGeneration;
189
190 sp<AMessage> notify = mNotify->dup();
191 notify->setInt32("what", kWhatError);
192 notify->setInt32("err", err);
193 notify->post();
194}
195
Chong Zhang7137ec72014-11-12 16:41:05 -0800196} // namespace android
197