NuPlayerDecoderBase.cpp revision 29b7dcf6d3cdb97103467dc8106151c6260c239a
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 "NuPlayerDecoderBase" 19#include <utils/Log.h> 20#include <inttypes.h> 21 22#include "NuPlayerDecoderBase.h" 23 24#include "NuPlayerRenderer.h" 25 26#include <media/MediaCodecBuffer.h> 27#include <media/stagefright/foundation/ADebug.h> 28#include <media/stagefright/foundation/AMessage.h> 29 30namespace android { 31 32NuPlayer::DecoderBase::DecoderBase(const sp<AMessage> ¬ify) 33 : mNotify(notify), 34 mBufferGeneration(0), 35 mPaused(false), 36 mStats(new AMessage), 37 mRequestInputBuffersPending(false) { 38 // 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) { 67 sp<AMessage> msg = new AMessage(kWhatConfigure, this); 68 msg->setMessage("format", format); 69 msg->post(); 70} 71 72void NuPlayer::DecoderBase::init() { 73 mDecoderLooper->registerHandler(this); 74} 75 76void NuPlayer::DecoderBase::setParameters(const sp<AMessage> ¶ms) { 77 sp<AMessage> msg = new AMessage(kWhatSetParameters, this); 78 msg->setMessage("params", params); 79 msg->post(); 80} 81 82void NuPlayer::DecoderBase::setRenderer(const sp<Renderer> &renderer) { 83 sp<AMessage> msg = new AMessage(kWhatSetRenderer, this); 84 msg->setObject("renderer", renderer); 85 msg->post(); 86} 87 88void NuPlayer::DecoderBase::pause() { 89 sp<AMessage> msg = new AMessage(kWhatPause, this); 90 91 sp<AMessage> response; 92 PostAndAwaitResponse(msg, &response); 93} 94 95void NuPlayer::DecoderBase::signalFlush() { 96 (new AMessage(kWhatFlush, this))->post(); 97} 98 99void NuPlayer::DecoderBase::signalResume(bool notifyComplete) { 100 sp<AMessage> msg = new AMessage(kWhatResume, this); 101 msg->setInt32("notifyComplete", notifyComplete); 102 msg->post(); 103} 104 105void NuPlayer::DecoderBase::initiateShutdown() { 106 (new AMessage(kWhatShutdown, this))->post(); 107} 108 109void NuPlayer::DecoderBase::onRequestInputBuffers() { 110 if (mRequestInputBuffersPending) { 111 return; 112 } 113 114 // doRequestBuffers() return true if we should request more data 115 if (doRequestBuffers()) { 116 mRequestInputBuffersPending = true; 117 118 sp<AMessage> msg = new AMessage(kWhatRequestInputBuffers, this); 119 msg->post(10 * 1000ll); 120 } 121} 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 134 case kWhatSetParameters: 135 { 136 sp<AMessage> params; 137 CHECK(msg->findMessage("params", ¶ms)); 138 onSetParameters(params); 139 break; 140 } 141 142 case kWhatSetRenderer: 143 { 144 sp<RefBase> obj; 145 CHECK(msg->findObject("renderer", &obj)); 146 onSetRenderer(static_cast<Renderer *>(obj.get())); 147 break; 148 } 149 150 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 161 case kWhatRequestInputBuffers: 162 { 163 mRequestInputBuffersPending = false; 164 onRequestInputBuffers(); 165 break; 166 } 167 168 case kWhatFlush: 169 { 170 onFlush(); 171 break; 172 } 173 174 case kWhatResume: 175 { 176 int32_t notifyComplete; 177 CHECK(msg->findInt32("notifyComplete", ¬ifyComplete)); 178 179 onResume(notifyComplete); 180 break; 181 } 182 183 case kWhatShutdown: 184 { 185 onShutdown(true); 186 break; 187 } 188 189 default: 190 TRESPASS(); 191 break; 192 } 193} 194 195void 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 209} // namespace android 210 211