Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 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/stagefright/foundation/ADebug.h> |
| 27 | #include <media/stagefright/foundation/AMessage.h> |
| 28 | |
| 29 | namespace android { |
| 30 | |
Andy Hung | 202bce1 | 2014-12-03 11:47:36 -0800 | [diff] [blame] | 31 | NuPlayer::DecoderBase::DecoderBase(const sp<AMessage> ¬ify) |
| 32 | : mNotify(notify), |
| 33 | mBufferGeneration(0), |
| 34 | mRequestInputBuffersPending(false) { |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 35 | // 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 | |
| 42 | NuPlayer::DecoderBase::~DecoderBase() { |
| 43 | mDecoderLooper->unregisterHandler(id()); |
| 44 | mDecoderLooper->stop(); |
| 45 | } |
| 46 | |
| 47 | static |
| 48 | status_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 | |
| 63 | void NuPlayer::DecoderBase::configure(const sp<AMessage> &format) { |
Lajos Molnar | 1d15ab5 | 2015-03-04 16:46:34 -0800 | [diff] [blame] | 64 | sp<AMessage> msg = new AMessage(kWhatConfigure, this); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 65 | msg->setMessage("format", format); |
| 66 | msg->post(); |
| 67 | } |
| 68 | |
| 69 | void NuPlayer::DecoderBase::init() { |
| 70 | mDecoderLooper->registerHandler(this); |
| 71 | } |
| 72 | |
| 73 | void NuPlayer::DecoderBase::setRenderer(const sp<Renderer> &renderer) { |
Lajos Molnar | 1d15ab5 | 2015-03-04 16:46:34 -0800 | [diff] [blame] | 74 | sp<AMessage> msg = new AMessage(kWhatSetRenderer, this); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 75 | msg->setObject("renderer", renderer); |
| 76 | msg->post(); |
| 77 | } |
| 78 | |
| 79 | status_t NuPlayer::DecoderBase::getInputBuffers(Vector<sp<ABuffer> > *buffers) const { |
Lajos Molnar | 1d15ab5 | 2015-03-04 16:46:34 -0800 | [diff] [blame] | 80 | sp<AMessage> msg = new AMessage(kWhatGetInputBuffers, this); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 81 | msg->setPointer("buffers", buffers); |
| 82 | |
| 83 | sp<AMessage> response; |
| 84 | return PostAndAwaitResponse(msg, &response); |
| 85 | } |
| 86 | |
| 87 | void NuPlayer::DecoderBase::signalFlush() { |
Lajos Molnar | 1d15ab5 | 2015-03-04 16:46:34 -0800 | [diff] [blame] | 88 | (new AMessage(kWhatFlush, this))->post(); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Chong Zhang | f8d7177 | 2014-11-26 15:08:34 -0800 | [diff] [blame] | 91 | void NuPlayer::DecoderBase::signalResume(bool notifyComplete) { |
Lajos Molnar | 1d15ab5 | 2015-03-04 16:46:34 -0800 | [diff] [blame] | 92 | sp<AMessage> msg = new AMessage(kWhatResume, this); |
Chong Zhang | f8d7177 | 2014-11-26 15:08:34 -0800 | [diff] [blame] | 93 | msg->setInt32("notifyComplete", notifyComplete); |
| 94 | msg->post(); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void NuPlayer::DecoderBase::initiateShutdown() { |
Lajos Molnar | 1d15ab5 | 2015-03-04 16:46:34 -0800 | [diff] [blame] | 98 | (new AMessage(kWhatShutdown, this))->post(); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void NuPlayer::DecoderBase::onRequestInputBuffers() { |
| 102 | if (mRequestInputBuffersPending) { |
| 103 | return; |
| 104 | } |
| 105 | |
Chong Zhang | 3b032b3 | 2015-04-17 15:49:06 -0700 | [diff] [blame^] | 106 | // doRequestBuffers() return true if we should request more data |
| 107 | if (doRequestBuffers()) { |
| 108 | mRequestInputBuffersPending = true; |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 109 | |
Chong Zhang | 3b032b3 | 2015-04-17 15:49:06 -0700 | [diff] [blame^] | 110 | sp<AMessage> msg = new AMessage(kWhatRequestInputBuffers, this); |
| 111 | msg->post(10 * 1000ll); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 112 | } |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void 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 Molnar | 3f27436 | 2015-03-05 14:35:41 -0800 | [diff] [blame] | 136 | sp<AReplyToken> replyID; |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 137 | 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 Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 157 | onFlush(); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 158 | break; |
| 159 | } |
| 160 | |
| 161 | case kWhatResume: |
| 162 | { |
Chong Zhang | f8d7177 | 2014-11-26 15:08:34 -0800 | [diff] [blame] | 163 | int32_t notifyComplete; |
| 164 | CHECK(msg->findInt32("notifyComplete", ¬ifyComplete)); |
| 165 | |
| 166 | onResume(notifyComplete); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 167 | break; |
| 168 | } |
| 169 | |
| 170 | case kWhatShutdown: |
| 171 | { |
| 172 | onShutdown(true); |
| 173 | break; |
| 174 | } |
| 175 | |
| 176 | default: |
| 177 | TRESPASS(); |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
Andy Hung | 202bce1 | 2014-12-03 11:47:36 -0800 | [diff] [blame] | 182 | void 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 Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 196 | } // namespace android |
| 197 | |