blob: 1210dc9c34feda01a344e3393fe17cbe30af8aaa [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
Wonsik Kim7e34bf52016-08-23 00:09:18 +090026#include <media/MediaCodecBuffer.h>
Chong Zhang7137ec72014-11-12 16:41:05 -080027#include <media/stagefright/foundation/ADebug.h>
28#include <media/stagefright/foundation/AMessage.h>
29
30namespace android {
31
Andy Hung202bce12014-12-03 11:47:36 -080032NuPlayer::DecoderBase::DecoderBase(const sp<AMessage> &notify)
33 : mNotify(notify),
34 mBufferGeneration(0),
Wei Jia3bc66702015-11-18 15:45:06 -080035 mPaused(false),
Praveen Chavane1e5d7a2015-05-19 19:09:48 -070036 mStats(new AMessage),
Andy Hung202bce12014-12-03 11:47:36 -080037 mRequestInputBuffersPending(false) {
Chong Zhang7137ec72014-11-12 16:41:05 -080038 // Every decoder has its own looper because MediaCodec operations
39 // are blocking, but NuPlayer needs asynchronous operations.
40 mDecoderLooper = new ALooper;
41 mDecoderLooper->setName("NPDecoder");
42 mDecoderLooper->start(false, false, ANDROID_PRIORITY_AUDIO);
43}
44
45NuPlayer::DecoderBase::~DecoderBase() {
46 mDecoderLooper->unregisterHandler(id());
47 mDecoderLooper->stop();
48}
49
50static
51status_t PostAndAwaitResponse(
52 const sp<AMessage> &msg, sp<AMessage> *response) {
53 status_t err = msg->postAndAwaitResponse(response);
54
55 if (err != OK) {
56 return err;
57 }
58
59 if (!(*response)->findInt32("err", &err)) {
60 err = OK;
61 }
62
63 return err;
64}
65
66void NuPlayer::DecoderBase::configure(const sp<AMessage> &format) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -080067 sp<AMessage> msg = new AMessage(kWhatConfigure, this);
Chong Zhang7137ec72014-11-12 16:41:05 -080068 msg->setMessage("format", format);
69 msg->post();
70}
71
72void NuPlayer::DecoderBase::init() {
73 mDecoderLooper->registerHandler(this);
74}
75
Ronghua Wu8db88132015-04-22 13:51:35 -070076void NuPlayer::DecoderBase::setParameters(const sp<AMessage> &params) {
77 sp<AMessage> msg = new AMessage(kWhatSetParameters, this);
78 msg->setMessage("params", params);
79 msg->post();
80}
81
Chong Zhang7137ec72014-11-12 16:41:05 -080082void NuPlayer::DecoderBase::setRenderer(const sp<Renderer> &renderer) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -080083 sp<AMessage> msg = new AMessage(kWhatSetRenderer, this);
Chong Zhang7137ec72014-11-12 16:41:05 -080084 msg->setObject("renderer", renderer);
85 msg->post();
86}
87
Wei Jia3bc66702015-11-18 15:45:06 -080088void NuPlayer::DecoderBase::pause() {
89 sp<AMessage> msg = new AMessage(kWhatPause, this);
90
91 sp<AMessage> response;
92 PostAndAwaitResponse(msg, &response);
93}
94
Chong Zhang7137ec72014-11-12 16:41:05 -080095void NuPlayer::DecoderBase::signalFlush() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -080096 (new AMessage(kWhatFlush, this))->post();
Chong Zhang7137ec72014-11-12 16:41:05 -080097}
98
Chong Zhangf8d71772014-11-26 15:08:34 -080099void NuPlayer::DecoderBase::signalResume(bool notifyComplete) {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800100 sp<AMessage> msg = new AMessage(kWhatResume, this);
Chong Zhangf8d71772014-11-26 15:08:34 -0800101 msg->setInt32("notifyComplete", notifyComplete);
102 msg->post();
Chong Zhang7137ec72014-11-12 16:41:05 -0800103}
104
105void NuPlayer::DecoderBase::initiateShutdown() {
Lajos Molnar1d15ab52015-03-04 16:46:34 -0800106 (new AMessage(kWhatShutdown, this))->post();
Chong Zhang7137ec72014-11-12 16:41:05 -0800107}
108
109void NuPlayer::DecoderBase::onRequestInputBuffers() {
110 if (mRequestInputBuffersPending) {
111 return;
112 }
113
Chong Zhang3b032b32015-04-17 15:49:06 -0700114 // doRequestBuffers() return true if we should request more data
115 if (doRequestBuffers()) {
116 mRequestInputBuffersPending = true;
Chong Zhang7137ec72014-11-12 16:41:05 -0800117
Chong Zhang3b032b32015-04-17 15:49:06 -0700118 sp<AMessage> msg = new AMessage(kWhatRequestInputBuffers, this);
119 msg->post(10 * 1000ll);
Chong Zhang7137ec72014-11-12 16:41:05 -0800120 }
Chong Zhang7137ec72014-11-12 16:41:05 -0800121}
122
123void NuPlayer::DecoderBase::onMessageReceived(const sp<AMessage> &msg) {
124
125 switch (msg->what()) {
126 case kWhatConfigure:
127 {
128 sp<AMessage> format;
129 CHECK(msg->findMessage("format", &format));
130 onConfigure(format);
131 break;
132 }
133
Ronghua Wu8db88132015-04-22 13:51:35 -0700134 case kWhatSetParameters:
135 {
136 sp<AMessage> params;
137 CHECK(msg->findMessage("params", &params));
138 onSetParameters(params);
139 break;
140 }
141
Chong Zhang7137ec72014-11-12 16:41:05 -0800142 case kWhatSetRenderer:
143 {
144 sp<RefBase> obj;
145 CHECK(msg->findObject("renderer", &obj));
146 onSetRenderer(static_cast<Renderer *>(obj.get()));
147 break;
148 }
149
Wei Jia3bc66702015-11-18 15:45:06 -0800150 case kWhatPause:
151 {
152 sp<AReplyToken> replyID;
153 CHECK(msg->senderAwaitsResponse(&replyID));
154
155 mPaused = true;
156
157 (new AMessage)->postReply(replyID);
158 break;
159 }
160
Chong Zhang7137ec72014-11-12 16:41:05 -0800161 case kWhatRequestInputBuffers:
162 {
163 mRequestInputBuffersPending = false;
164 onRequestInputBuffers();
165 break;
166 }
167
168 case kWhatFlush:
169 {
Chong Zhang66704af2015-03-03 19:32:35 -0800170 onFlush();
Chong Zhang7137ec72014-11-12 16:41:05 -0800171 break;
172 }
173
174 case kWhatResume:
175 {
Chong Zhangf8d71772014-11-26 15:08:34 -0800176 int32_t notifyComplete;
177 CHECK(msg->findInt32("notifyComplete", &notifyComplete));
178
179 onResume(notifyComplete);
Chong Zhang7137ec72014-11-12 16:41:05 -0800180 break;
181 }
182
183 case kWhatShutdown:
184 {
185 onShutdown(true);
186 break;
187 }
188
189 default:
190 TRESPASS();
191 break;
192 }
193}
194
Andy Hung202bce12014-12-03 11:47:36 -0800195void NuPlayer::DecoderBase::handleError(int32_t err)
196{
197 // We cannot immediately release the codec due to buffers still outstanding
198 // in the renderer. We signal to the player the error so it can shutdown/release the
199 // decoder after flushing and increment the generation to discard unnecessary messages.
200
201 ++mBufferGeneration;
202
203 sp<AMessage> notify = mNotify->dup();
204 notify->setInt32("what", kWhatError);
205 notify->setInt32("err", err);
206 notify->post();
207}
208
Chong Zhang7137ec72014-11-12 16:41:05 -0800209} // namespace android
210