Andreas Huber | f933441 | 2010-12-15 15:17:42 -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 "NuPlayerDecoder" |
| 19 | #include <utils/Log.h> |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 20 | #include <inttypes.h> |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 21 | |
| 22 | #include "NuPlayerDecoder.h" |
| 23 | |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame^] | 24 | #include "NuPlayerRenderer.h" |
| 25 | #include "NuPlayerSource.h" |
| 26 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 27 | #include <media/ICrypto.h> |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 28 | #include <media/stagefright/foundation/ABitReader.h> |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 29 | #include <media/stagefright/foundation/ABuffer.h> |
| 30 | #include <media/stagefright/foundation/ADebug.h> |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 31 | #include <media/stagefright/foundation/AMessage.h> |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 32 | #include <media/stagefright/MediaBuffer.h> |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 33 | #include <media/stagefright/MediaCodec.h> |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 34 | #include <media/stagefright/MediaDefs.h> |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 35 | #include <media/stagefright/MediaErrors.h> |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 36 | |
| 37 | namespace android { |
| 38 | |
| 39 | NuPlayer::Decoder::Decoder( |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 40 | const sp<AMessage> ¬ify, |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame^] | 41 | const sp<Source> &source, |
| 42 | const sp<Renderer> &renderer, |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 43 | const sp<NativeWindowWrapper> &nativeWindow) |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 44 | : mNotify(notify), |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 45 | mNativeWindow(nativeWindow), |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame^] | 46 | mSource(source), |
| 47 | mRenderer(renderer), |
| 48 | mSkipRenderingUntilMediaTimeUs(-1ll), |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 49 | mBufferGeneration(0), |
Wei Jia | 704e726 | 2014-06-04 16:21:56 -0700 | [diff] [blame] | 50 | mPaused(true), |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 51 | mComponentName("decoder") { |
| 52 | // Every decoder has its own looper because MediaCodec operations |
| 53 | // are blocking, but NuPlayer needs asynchronous operations. |
| 54 | mDecoderLooper = new ALooper; |
Marco Nelissen | 9e2b791 | 2014-08-18 16:13:03 -0700 | [diff] [blame] | 55 | mDecoderLooper->setName("NPDecoder"); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 56 | mDecoderLooper->start(false, false, ANDROID_PRIORITY_AUDIO); |
| 57 | |
| 58 | mCodecLooper = new ALooper; |
Marco Nelissen | 9e2b791 | 2014-08-18 16:13:03 -0700 | [diff] [blame] | 59 | mCodecLooper->setName("NPDecoder-CL"); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 60 | mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | NuPlayer::Decoder::~Decoder() { |
Wei Jia | 4923cee | 2014-09-24 14:25:19 -0700 | [diff] [blame] | 64 | mDecoderLooper->unregisterHandler(id()); |
| 65 | mDecoderLooper->stop(); |
| 66 | |
| 67 | releaseAndResetMediaBuffers(); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 70 | static |
| 71 | status_t PostAndAwaitResponse( |
| 72 | const sp<AMessage> &msg, sp<AMessage> *response) { |
| 73 | status_t err = msg->postAndAwaitResponse(response); |
| 74 | |
| 75 | if (err != OK) { |
| 76 | return err; |
| 77 | } |
| 78 | |
| 79 | if (!(*response)->findInt32("err", &err)) { |
| 80 | err = OK; |
| 81 | } |
| 82 | |
| 83 | return err; |
| 84 | } |
| 85 | |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 86 | void NuPlayer::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) { |
| 87 | mCSDsForCurrentFormat.clear(); |
| 88 | for (int32_t i = 0; ; ++i) { |
| 89 | AString tag = "csd-"; |
| 90 | tag.append(i); |
| 91 | sp<ABuffer> buffer; |
| 92 | if (!format->findBuffer(tag.c_str(), &buffer)) { |
| 93 | break; |
| 94 | } |
| 95 | mCSDsForCurrentFormat.push(buffer); |
| 96 | } |
| 97 | } |
| 98 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 99 | void NuPlayer::Decoder::onConfigure(const sp<AMessage> &format) { |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 100 | CHECK(mCodec == NULL); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 101 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 102 | ++mBufferGeneration; |
| 103 | |
Andreas Huber | 8406678 | 2011-08-16 09:34:26 -0700 | [diff] [blame] | 104 | AString mime; |
| 105 | CHECK(format->findString("mime", &mime)); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 106 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 107 | sp<Surface> surface = NULL; |
| 108 | if (mNativeWindow != NULL) { |
| 109 | surface = mNativeWindow->getSurfaceTextureClient(); |
Andreas Huber | 8406678 | 2011-08-16 09:34:26 -0700 | [diff] [blame] | 110 | } |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 111 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 112 | mComponentName = mime; |
| 113 | mComponentName.append(" decoder"); |
| 114 | ALOGV("[%s] onConfigure (surface=%p)", mComponentName.c_str(), surface.get()); |
| 115 | |
| 116 | mCodec = MediaCodec::CreateByType(mCodecLooper, mime.c_str(), false /* encoder */); |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 117 | int32_t secure = 0; |
| 118 | if (format->findInt32("secure", &secure) && secure != 0) { |
| 119 | if (mCodec != NULL) { |
| 120 | mCodec->getName(&mComponentName); |
| 121 | mComponentName.append(".secure"); |
| 122 | mCodec->release(); |
| 123 | ALOGI("[%s] creating", mComponentName.c_str()); |
| 124 | mCodec = MediaCodec::CreateByComponentName( |
| 125 | mCodecLooper, mComponentName.c_str()); |
| 126 | } |
| 127 | } |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 128 | if (mCodec == NULL) { |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 129 | ALOGE("Failed to create %s%s decoder", |
| 130 | (secure ? "secure " : ""), mime.c_str()); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 131 | handleError(UNKNOWN_ERROR); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | mCodec->getName(&mComponentName); |
| 136 | |
Lajos Molnar | 14986f6 | 2014-09-15 11:04:44 -0700 | [diff] [blame] | 137 | status_t err; |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 138 | if (mNativeWindow != NULL) { |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 139 | // disconnect from surface as MediaCodec will reconnect |
Lajos Molnar | 14986f6 | 2014-09-15 11:04:44 -0700 | [diff] [blame] | 140 | err = native_window_api_disconnect( |
| 141 | surface.get(), NATIVE_WINDOW_API_MEDIA); |
| 142 | // We treat this as a warning, as this is a preparatory step. |
| 143 | // Codec will try to connect to the surface, which is where |
| 144 | // any error signaling will occur. |
| 145 | ALOGW_IF(err != OK, "failed to disconnect from surface: %d", err); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 146 | } |
Lajos Molnar | 14986f6 | 2014-09-15 11:04:44 -0700 | [diff] [blame] | 147 | err = mCodec->configure( |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 148 | format, surface, NULL /* crypto */, 0 /* flags */); |
| 149 | if (err != OK) { |
| 150 | ALOGE("Failed to configure %s decoder (err=%d)", mComponentName.c_str(), err); |
Andy Hung | 2abde2c | 2014-09-30 14:40:32 -0700 | [diff] [blame] | 151 | mCodec->release(); |
| 152 | mCodec.clear(); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 153 | handleError(err); |
| 154 | return; |
| 155 | } |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 156 | rememberCodecSpecificData(format); |
| 157 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 158 | // the following should work in configured state |
| 159 | CHECK_EQ((status_t)OK, mCodec->getOutputFormat(&mOutputFormat)); |
| 160 | CHECK_EQ((status_t)OK, mCodec->getInputFormat(&mInputFormat)); |
| 161 | |
| 162 | err = mCodec->start(); |
| 163 | if (err != OK) { |
| 164 | ALOGE("Failed to start %s decoder (err=%d)", mComponentName.c_str(), err); |
Andy Hung | 2abde2c | 2014-09-30 14:40:32 -0700 | [diff] [blame] | 165 | mCodec->release(); |
| 166 | mCodec.clear(); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 167 | handleError(err); |
| 168 | return; |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 171 | // the following should work after start |
| 172 | CHECK_EQ((status_t)OK, mCodec->getInputBuffers(&mInputBuffers)); |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 173 | releaseAndResetMediaBuffers(); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 174 | CHECK_EQ((status_t)OK, mCodec->getOutputBuffers(&mOutputBuffers)); |
| 175 | ALOGV("[%s] got %zu input and %zu output buffers", |
| 176 | mComponentName.c_str(), |
| 177 | mInputBuffers.size(), |
| 178 | mOutputBuffers.size()); |
Andreas Huber | 078cfcf | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 179 | |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame^] | 180 | if (mRenderer != NULL) { |
| 181 | requestCodecNotification(); |
| 182 | } |
Wei Jia | 704e726 | 2014-06-04 16:21:56 -0700 | [diff] [blame] | 183 | mPaused = false; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 184 | } |
Andreas Huber | 078cfcf | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 185 | |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 186 | void NuPlayer::Decoder::releaseAndResetMediaBuffers() { |
| 187 | for (size_t i = 0; i < mMediaBuffers.size(); i++) { |
| 188 | if (mMediaBuffers[i] != NULL) { |
| 189 | mMediaBuffers[i]->release(); |
| 190 | mMediaBuffers.editItemAt(i) = NULL; |
| 191 | } |
| 192 | } |
| 193 | mMediaBuffers.resize(mInputBuffers.size()); |
Wei Jia | 81e50d0 | 2014-07-24 10:28:47 -0700 | [diff] [blame] | 194 | for (size_t i = 0; i < mMediaBuffers.size(); i++) { |
| 195 | mMediaBuffers.editItemAt(i) = NULL; |
| 196 | } |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 197 | mInputBufferIsDequeued.clear(); |
| 198 | mInputBufferIsDequeued.resize(mInputBuffers.size()); |
Wei Jia | 81e50d0 | 2014-07-24 10:28:47 -0700 | [diff] [blame] | 199 | for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) { |
| 200 | mInputBufferIsDequeued.editItemAt(i) = false; |
| 201 | } |
Wei Jia | 2245fc6 | 2014-10-02 15:12:25 -0700 | [diff] [blame] | 202 | |
| 203 | mPendingInputMessages.clear(); |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame^] | 204 | mSkipRenderingUntilMediaTimeUs = -1; |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 207 | void NuPlayer::Decoder::requestCodecNotification() { |
| 208 | if (mCodec != NULL) { |
| 209 | sp<AMessage> reply = new AMessage(kWhatCodecNotify, id()); |
| 210 | reply->setInt32("generation", mBufferGeneration); |
| 211 | mCodec->requestActivityNotification(reply); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) { |
| 216 | int32_t generation; |
| 217 | CHECK(msg->findInt32("generation", &generation)); |
| 218 | return generation != mBufferGeneration; |
| 219 | } |
| 220 | |
| 221 | void NuPlayer::Decoder::init() { |
| 222 | mDecoderLooper->registerHandler(this); |
| 223 | } |
| 224 | |
| 225 | void NuPlayer::Decoder::configure(const sp<AMessage> &format) { |
| 226 | sp<AMessage> msg = new AMessage(kWhatConfigure, id()); |
| 227 | msg->setMessage("format", format); |
| 228 | msg->post(); |
| 229 | } |
| 230 | |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame^] | 231 | void NuPlayer::Decoder::setRenderer(const sp<Renderer> &renderer) { |
| 232 | sp<AMessage> msg = new AMessage(kWhatSetRenderer, id()); |
| 233 | msg->setObject("renderer", renderer); |
| 234 | msg->post(); |
| 235 | } |
| 236 | |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 237 | void NuPlayer::Decoder::signalUpdateFormat(const sp<AMessage> &format) { |
| 238 | sp<AMessage> msg = new AMessage(kWhatUpdateFormat, id()); |
| 239 | msg->setMessage("format", format); |
| 240 | msg->post(); |
| 241 | } |
| 242 | |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 243 | status_t NuPlayer::Decoder::getInputBuffers(Vector<sp<ABuffer> > *buffers) const { |
| 244 | sp<AMessage> msg = new AMessage(kWhatGetInputBuffers, id()); |
| 245 | msg->setPointer("buffers", buffers); |
| 246 | |
| 247 | sp<AMessage> response; |
| 248 | return PostAndAwaitResponse(msg, &response); |
| 249 | } |
| 250 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 251 | void NuPlayer::Decoder::handleError(int32_t err) |
| 252 | { |
Andy Hung | cf31f1e | 2014-09-23 14:59:01 -0700 | [diff] [blame] | 253 | // We cannot immediately release the codec due to buffers still outstanding |
| 254 | // in the renderer. We signal to the player the error so it can shutdown/release the |
| 255 | // decoder after flushing and increment the generation to discard unnecessary messages. |
| 256 | |
| 257 | ++mBufferGeneration; |
Wei Jia | c22c695 | 2014-08-29 14:47:50 -0700 | [diff] [blame] | 258 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 259 | sp<AMessage> notify = mNotify->dup(); |
| 260 | notify->setInt32("what", kWhatError); |
| 261 | notify->setInt32("err", err); |
| 262 | notify->post(); |
| 263 | } |
| 264 | |
| 265 | bool NuPlayer::Decoder::handleAnInputBuffer() { |
| 266 | size_t bufferIx = -1; |
| 267 | status_t res = mCodec->dequeueInputBuffer(&bufferIx); |
| 268 | ALOGV("[%s] dequeued input: %d", |
| 269 | mComponentName.c_str(), res == OK ? (int)bufferIx : res); |
| 270 | if (res != OK) { |
| 271 | if (res != -EAGAIN) { |
Andy Hung | cf31f1e | 2014-09-23 14:59:01 -0700 | [diff] [blame] | 272 | ALOGE("Failed to dequeue input buffer for %s (err=%d)", |
| 273 | mComponentName.c_str(), res); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 274 | handleError(res); |
| 275 | } |
| 276 | return false; |
Andreas Huber | 078cfcf | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 279 | CHECK_LT(bufferIx, mInputBuffers.size()); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 280 | |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 281 | if (mMediaBuffers[bufferIx] != NULL) { |
| 282 | mMediaBuffers[bufferIx]->release(); |
| 283 | mMediaBuffers.editItemAt(bufferIx) = NULL; |
| 284 | } |
| 285 | mInputBufferIsDequeued.editItemAt(bufferIx) = true; |
| 286 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 287 | sp<AMessage> reply = new AMessage(kWhatInputBufferFilled, id()); |
| 288 | reply->setSize("buffer-ix", bufferIx); |
| 289 | reply->setInt32("generation", mBufferGeneration); |
| 290 | |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 291 | if (!mCSDsToSubmit.isEmpty()) { |
| 292 | sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0); |
| 293 | ALOGI("[%s] resubmitting CSD", mComponentName.c_str()); |
| 294 | reply->setBuffer("buffer", buffer); |
| 295 | mCSDsToSubmit.removeAt(0); |
Wei Jia | 2245fc6 | 2014-10-02 15:12:25 -0700 | [diff] [blame] | 296 | CHECK(onInputBufferFilled(reply)); |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | while (!mPendingInputMessages.empty()) { |
| 301 | sp<AMessage> msg = *mPendingInputMessages.begin(); |
| 302 | if (!onInputBufferFilled(msg)) { |
| 303 | break; |
| 304 | } |
| 305 | mPendingInputMessages.erase(mPendingInputMessages.begin()); |
| 306 | } |
| 307 | |
| 308 | if (!mInputBufferIsDequeued.editItemAt(bufferIx)) { |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 309 | return true; |
| 310 | } |
| 311 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 312 | sp<AMessage> notify = mNotify->dup(); |
| 313 | notify->setInt32("what", kWhatFillThisBuffer); |
| 314 | notify->setBuffer("buffer", mInputBuffers[bufferIx]); |
| 315 | notify->setMessage("reply", reply); |
| 316 | notify->post(); |
| 317 | return true; |
| 318 | } |
| 319 | |
Wei Jia | 2245fc6 | 2014-10-02 15:12:25 -0700 | [diff] [blame] | 320 | bool android::NuPlayer::Decoder::onInputBufferFilled(const sp<AMessage> &msg) { |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 321 | size_t bufferIx; |
| 322 | CHECK(msg->findSize("buffer-ix", &bufferIx)); |
| 323 | CHECK_LT(bufferIx, mInputBuffers.size()); |
| 324 | sp<ABuffer> codecBuffer = mInputBuffers[bufferIx]; |
| 325 | |
| 326 | sp<ABuffer> buffer; |
| 327 | bool hasBuffer = msg->findBuffer("buffer", &buffer); |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 328 | |
| 329 | // handle widevine classic source - that fills an arbitrary input buffer |
| 330 | MediaBuffer *mediaBuffer = NULL; |
Wei Jia | 96e92b5 | 2014-09-18 17:36:20 -0700 | [diff] [blame] | 331 | if (hasBuffer) { |
| 332 | mediaBuffer = (MediaBuffer *)(buffer->getMediaBufferBase()); |
| 333 | if (mediaBuffer != NULL) { |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 334 | // likely filled another buffer than we requested: adjust buffer index |
| 335 | size_t ix; |
| 336 | for (ix = 0; ix < mInputBuffers.size(); ix++) { |
| 337 | const sp<ABuffer> &buf = mInputBuffers[ix]; |
| 338 | if (buf->data() == mediaBuffer->data()) { |
| 339 | // all input buffers are dequeued on start, hence the check |
Wei Jia | 2245fc6 | 2014-10-02 15:12:25 -0700 | [diff] [blame] | 340 | if (!mInputBufferIsDequeued[ix]) { |
| 341 | ALOGV("[%s] received MediaBuffer for #%zu instead of #%zu", |
| 342 | mComponentName.c_str(), ix, bufferIx); |
| 343 | mediaBuffer->release(); |
| 344 | return false; |
| 345 | } |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 346 | |
| 347 | // TRICKY: need buffer for the metadata, so instead, set |
| 348 | // codecBuffer to the same (though incorrect) buffer to |
| 349 | // avoid a memcpy into the codecBuffer |
| 350 | codecBuffer = buffer; |
| 351 | codecBuffer->setRange( |
| 352 | mediaBuffer->range_offset(), |
| 353 | mediaBuffer->range_length()); |
| 354 | bufferIx = ix; |
| 355 | break; |
| 356 | } |
| 357 | } |
| 358 | CHECK(ix < mInputBuffers.size()); |
| 359 | } |
| 360 | } |
| 361 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 362 | if (buffer == NULL /* includes !hasBuffer */) { |
| 363 | int32_t streamErr = ERROR_END_OF_STREAM; |
| 364 | CHECK(msg->findInt32("err", &streamErr) || !hasBuffer); |
| 365 | |
| 366 | if (streamErr == OK) { |
| 367 | /* buffers are returned to hold on to */ |
Wei Jia | 2245fc6 | 2014-10-02 15:12:25 -0700 | [diff] [blame] | 368 | return true; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | // attempt to queue EOS |
| 372 | status_t err = mCodec->queueInputBuffer( |
| 373 | bufferIx, |
| 374 | 0, |
| 375 | 0, |
| 376 | 0, |
| 377 | MediaCodec::BUFFER_FLAG_EOS); |
Andy Hung | cf31f1e | 2014-09-23 14:59:01 -0700 | [diff] [blame] | 378 | if (err == OK) { |
| 379 | mInputBufferIsDequeued.editItemAt(bufferIx) = false; |
| 380 | } else if (streamErr == ERROR_END_OF_STREAM) { |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 381 | streamErr = err; |
| 382 | // err will not be ERROR_END_OF_STREAM |
| 383 | } |
| 384 | |
| 385 | if (streamErr != ERROR_END_OF_STREAM) { |
Andy Hung | cf31f1e | 2014-09-23 14:59:01 -0700 | [diff] [blame] | 386 | ALOGE("Stream error for %s (err=%d), EOS %s queued", |
| 387 | mComponentName.c_str(), |
| 388 | streamErr, |
| 389 | err == OK ? "successfully" : "unsuccessfully"); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 390 | handleError(streamErr); |
| 391 | } |
| 392 | } else { |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame^] | 393 | sp<AMessage> extra; |
| 394 | if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) { |
| 395 | int64_t resumeAtMediaTimeUs; |
| 396 | if (extra->findInt64( |
| 397 | "resume-at-mediaTimeUs", &resumeAtMediaTimeUs)) { |
| 398 | ALOGI("[%s] suppressing rendering until %lld us", |
| 399 | mComponentName.c_str(), (long long)resumeAtMediaTimeUs); |
| 400 | mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs; |
| 401 | } |
| 402 | } |
| 403 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 404 | int64_t timeUs = 0; |
| 405 | uint32_t flags = 0; |
| 406 | CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); |
| 407 | |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 408 | int32_t eos, csd; |
| 409 | // we do not expect SYNCFRAME for decoder |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 410 | if (buffer->meta()->findInt32("eos", &eos) && eos) { |
| 411 | flags |= MediaCodec::BUFFER_FLAG_EOS; |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 412 | } else if (buffer->meta()->findInt32("csd", &csd) && csd) { |
| 413 | flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | // copy into codec buffer |
| 417 | if (buffer != codecBuffer) { |
| 418 | CHECK_LE(buffer->size(), codecBuffer->capacity()); |
| 419 | codecBuffer->setRange(0, buffer->size()); |
| 420 | memcpy(codecBuffer->data(), buffer->data(), buffer->size()); |
| 421 | } |
| 422 | |
| 423 | status_t err = mCodec->queueInputBuffer( |
| 424 | bufferIx, |
| 425 | codecBuffer->offset(), |
| 426 | codecBuffer->size(), |
| 427 | timeUs, |
| 428 | flags); |
| 429 | if (err != OK) { |
Andy Hung | cf31f1e | 2014-09-23 14:59:01 -0700 | [diff] [blame] | 430 | if (mediaBuffer != NULL) { |
| 431 | mediaBuffer->release(); |
| 432 | } |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 433 | ALOGE("Failed to queue input buffer for %s (err=%d)", |
| 434 | mComponentName.c_str(), err); |
| 435 | handleError(err); |
Andy Hung | cf31f1e | 2014-09-23 14:59:01 -0700 | [diff] [blame] | 436 | } else { |
| 437 | mInputBufferIsDequeued.editItemAt(bufferIx) = false; |
| 438 | if (mediaBuffer != NULL) { |
| 439 | CHECK(mMediaBuffers[bufferIx] == NULL); |
| 440 | mMediaBuffers.editItemAt(bufferIx) = mediaBuffer; |
| 441 | } |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 442 | } |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 443 | } |
Wei Jia | 2245fc6 | 2014-10-02 15:12:25 -0700 | [diff] [blame] | 444 | return true; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | bool NuPlayer::Decoder::handleAnOutputBuffer() { |
| 448 | size_t bufferIx = -1; |
| 449 | size_t offset; |
| 450 | size_t size; |
| 451 | int64_t timeUs; |
| 452 | uint32_t flags; |
| 453 | status_t res = mCodec->dequeueOutputBuffer( |
| 454 | &bufferIx, &offset, &size, &timeUs, &flags); |
| 455 | |
| 456 | if (res != OK) { |
| 457 | ALOGV("[%s] dequeued output: %d", mComponentName.c_str(), res); |
| 458 | } else { |
| 459 | ALOGV("[%s] dequeued output: %d (time=%lld flags=%" PRIu32 ")", |
| 460 | mComponentName.c_str(), (int)bufferIx, timeUs, flags); |
| 461 | } |
| 462 | |
| 463 | if (res == INFO_OUTPUT_BUFFERS_CHANGED) { |
| 464 | res = mCodec->getOutputBuffers(&mOutputBuffers); |
| 465 | if (res != OK) { |
| 466 | ALOGE("Failed to get output buffers for %s after INFO event (err=%d)", |
| 467 | mComponentName.c_str(), res); |
| 468 | handleError(res); |
| 469 | return false; |
| 470 | } |
| 471 | // NuPlayer ignores this |
| 472 | return true; |
| 473 | } else if (res == INFO_FORMAT_CHANGED) { |
| 474 | sp<AMessage> format = new AMessage(); |
| 475 | res = mCodec->getOutputFormat(&format); |
| 476 | if (res != OK) { |
| 477 | ALOGE("Failed to get output format for %s after INFO event (err=%d)", |
| 478 | mComponentName.c_str(), res); |
| 479 | handleError(res); |
| 480 | return false; |
| 481 | } |
| 482 | |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame^] | 483 | if (isVideo()) { |
| 484 | sp<AMessage> notify = mNotify->dup(); |
| 485 | notify->setInt32("what", kWhatVideoSizeChanged); |
| 486 | notify->setMessage("format", format); |
| 487 | notify->post(); |
| 488 | } else if (mRenderer != NULL) { |
| 489 | uint32_t flags; |
| 490 | int64_t durationUs; |
| 491 | bool hasVideo = (mSource->getFormat(false /* audio */) != NULL); |
| 492 | if (!hasVideo && |
| 493 | mSource->getDuration(&durationUs) == OK && |
| 494 | durationUs |
| 495 | > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) { |
| 496 | flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
| 497 | } else { |
| 498 | flags = AUDIO_OUTPUT_FLAG_NONE; |
| 499 | } |
| 500 | |
| 501 | mRenderer->openAudioSink( |
| 502 | format, false /* offloadOnly */, hasVideo, flags); |
| 503 | } |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 504 | return true; |
| 505 | } else if (res == INFO_DISCONTINUITY) { |
| 506 | // nothing to do |
| 507 | return true; |
| 508 | } else if (res != OK) { |
| 509 | if (res != -EAGAIN) { |
Andy Hung | cf31f1e | 2014-09-23 14:59:01 -0700 | [diff] [blame] | 510 | ALOGE("Failed to dequeue output buffer for %s (err=%d)", |
| 511 | mComponentName.c_str(), res); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 512 | handleError(res); |
| 513 | } |
| 514 | return false; |
| 515 | } |
| 516 | |
| 517 | CHECK_LT(bufferIx, mOutputBuffers.size()); |
| 518 | sp<ABuffer> buffer = mOutputBuffers[bufferIx]; |
| 519 | buffer->setRange(offset, size); |
| 520 | buffer->meta()->clear(); |
| 521 | buffer->meta()->setInt64("timeUs", timeUs); |
| 522 | if (flags & MediaCodec::BUFFER_FLAG_EOS) { |
| 523 | buffer->meta()->setInt32("eos", true); |
| 524 | } |
| 525 | // we do not expect CODECCONFIG or SYNCFRAME for decoder |
| 526 | |
| 527 | sp<AMessage> reply = new AMessage(kWhatRenderBuffer, id()); |
| 528 | reply->setSize("buffer-ix", bufferIx); |
| 529 | reply->setInt32("generation", mBufferGeneration); |
| 530 | |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame^] | 531 | if (mSkipRenderingUntilMediaTimeUs >= 0) { |
| 532 | if (timeUs < mSkipRenderingUntilMediaTimeUs) { |
| 533 | ALOGV("[%s] dropping buffer at time %lld as requested.", |
| 534 | mComponentName.c_str(), (long long)timeUs); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 535 | |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame^] | 536 | reply->post(); |
| 537 | return true; |
| 538 | } |
| 539 | |
| 540 | mSkipRenderingUntilMediaTimeUs = -1; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 541 | } |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame^] | 542 | |
| 543 | if (mRenderer != NULL) { |
| 544 | // send the buffer to renderer. |
| 545 | mRenderer->queueBuffer(!isVideo(), buffer, reply); |
| 546 | if (flags & MediaCodec::BUFFER_FLAG_EOS) { |
| 547 | mRenderer->queueEOS(!isVideo(), ERROR_END_OF_STREAM); |
| 548 | } |
| 549 | } |
| 550 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 551 | return true; |
| 552 | } |
| 553 | |
| 554 | void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) { |
| 555 | status_t err; |
| 556 | int32_t render; |
| 557 | size_t bufferIx; |
| 558 | CHECK(msg->findSize("buffer-ix", &bufferIx)); |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame^] | 559 | |
| 560 | if (isVideo()) { |
| 561 | int64_t timeUs; |
| 562 | sp<ABuffer> buffer = mOutputBuffers[bufferIx]; |
| 563 | buffer->meta()->findInt64("timeUs", &timeUs); |
| 564 | sp<AMessage> notify = mNotify->dup(); |
| 565 | notify->setInt32("what", kWhatRenderBufferTime); |
| 566 | notify->setInt64("timeUs", timeUs); |
| 567 | notify->post(); |
| 568 | } |
| 569 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 570 | if (msg->findInt32("render", &render) && render) { |
Lajos Molnar | dc43dfa | 2014-05-07 15:33:04 -0700 | [diff] [blame] | 571 | int64_t timestampNs; |
| 572 | CHECK(msg->findInt64("timestampNs", ×tampNs)); |
| 573 | err = mCodec->renderOutputBufferAndRelease(bufferIx, timestampNs); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 574 | } else { |
| 575 | err = mCodec->releaseOutputBuffer(bufferIx); |
| 576 | } |
| 577 | if (err != OK) { |
| 578 | ALOGE("failed to release output buffer for %s (err=%d)", |
| 579 | mComponentName.c_str(), err); |
| 580 | handleError(err); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | void NuPlayer::Decoder::onFlush() { |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame^] | 585 | if (mRenderer != NULL) { |
| 586 | mRenderer->flush(!isVideo()); |
| 587 | } |
| 588 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 589 | status_t err = OK; |
| 590 | if (mCodec != NULL) { |
| 591 | err = mCodec->flush(); |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 592 | mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 593 | ++mBufferGeneration; |
| 594 | } |
| 595 | |
| 596 | if (err != OK) { |
| 597 | ALOGE("failed to flush %s (err=%d)", mComponentName.c_str(), err); |
| 598 | handleError(err); |
Andy Hung | 2abde2c | 2014-09-30 14:40:32 -0700 | [diff] [blame] | 599 | // finish with posting kWhatFlushCompleted. |
| 600 | // we attempt to release the buffers even if flush fails. |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 601 | } |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 602 | releaseAndResetMediaBuffers(); |
| 603 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 604 | sp<AMessage> notify = mNotify->dup(); |
| 605 | notify->setInt32("what", kWhatFlushCompleted); |
| 606 | notify->post(); |
Wei Jia | 704e726 | 2014-06-04 16:21:56 -0700 | [diff] [blame] | 607 | mPaused = true; |
| 608 | } |
| 609 | |
| 610 | void NuPlayer::Decoder::onResume() { |
| 611 | mPaused = false; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | void NuPlayer::Decoder::onShutdown() { |
| 615 | status_t err = OK; |
| 616 | if (mCodec != NULL) { |
| 617 | err = mCodec->release(); |
| 618 | mCodec = NULL; |
| 619 | ++mBufferGeneration; |
| 620 | |
| 621 | if (mNativeWindow != NULL) { |
| 622 | // reconnect to surface as MediaCodec disconnected from it |
Wei Jia | 3fb9f68 | 2014-08-20 14:30:09 -0700 | [diff] [blame] | 623 | status_t error = |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 624 | native_window_api_connect( |
| 625 | mNativeWindow->getNativeWindow().get(), |
Wei Jia | 3fb9f68 | 2014-08-20 14:30:09 -0700 | [diff] [blame] | 626 | NATIVE_WINDOW_API_MEDIA); |
| 627 | ALOGW_IF(error != NO_ERROR, |
| 628 | "[%s] failed to connect to native window, error=%d", |
| 629 | mComponentName.c_str(), error); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 630 | } |
| 631 | mComponentName = "decoder"; |
| 632 | } |
| 633 | |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 634 | releaseAndResetMediaBuffers(); |
| 635 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 636 | if (err != OK) { |
| 637 | ALOGE("failed to release %s (err=%d)", mComponentName.c_str(), err); |
| 638 | handleError(err); |
Andy Hung | 2abde2c | 2014-09-30 14:40:32 -0700 | [diff] [blame] | 639 | // finish with posting kWhatShutdownCompleted. |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | sp<AMessage> notify = mNotify->dup(); |
| 643 | notify->setInt32("what", kWhatShutdownCompleted); |
| 644 | notify->post(); |
Wei Jia | 704e726 | 2014-06-04 16:21:56 -0700 | [diff] [blame] | 645 | mPaused = true; |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) { |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 649 | ALOGV("[%s] onMessage: %s", mComponentName.c_str(), msg->debugString().c_str()); |
| 650 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 651 | switch (msg->what()) { |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 652 | case kWhatConfigure: |
| 653 | { |
| 654 | sp<AMessage> format; |
| 655 | CHECK(msg->findMessage("format", &format)); |
| 656 | onConfigure(format); |
| 657 | break; |
| 658 | } |
| 659 | |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame^] | 660 | case kWhatSetRenderer: |
| 661 | { |
| 662 | bool hadNoRenderer = (mRenderer == NULL); |
| 663 | sp<RefBase> obj; |
| 664 | CHECK(msg->findObject("renderer", &obj)); |
| 665 | mRenderer = static_cast<Renderer *>(obj.get()); |
| 666 | if (hadNoRenderer && mRenderer != NULL) { |
| 667 | requestCodecNotification(); |
| 668 | } |
| 669 | break; |
| 670 | } |
| 671 | |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 672 | case kWhatUpdateFormat: |
| 673 | { |
| 674 | sp<AMessage> format; |
| 675 | CHECK(msg->findMessage("format", &format)); |
| 676 | rememberCodecSpecificData(format); |
| 677 | break; |
| 678 | } |
| 679 | |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 680 | case kWhatGetInputBuffers: |
| 681 | { |
| 682 | uint32_t replyID; |
| 683 | CHECK(msg->senderAwaitsResponse(&replyID)); |
| 684 | |
| 685 | Vector<sp<ABuffer> > *dstBuffers; |
| 686 | CHECK(msg->findPointer("buffers", (void **)&dstBuffers)); |
| 687 | |
| 688 | dstBuffers->clear(); |
| 689 | for (size_t i = 0; i < mInputBuffers.size(); i++) { |
| 690 | dstBuffers->push(mInputBuffers[i]); |
| 691 | } |
| 692 | |
| 693 | (new AMessage)->postReply(replyID); |
| 694 | break; |
| 695 | } |
| 696 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 697 | case kWhatCodecNotify: |
| 698 | { |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 699 | if (!isStaleReply(msg)) { |
Chong Zhang | e47d444 | 2014-09-30 13:12:22 -0700 | [diff] [blame] | 700 | int32_t numInput, numOutput; |
| 701 | |
| 702 | if (!msg->findInt32("input-buffers", &numInput)) { |
| 703 | numInput = INT32_MAX; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 704 | } |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 705 | |
Chong Zhang | e47d444 | 2014-09-30 13:12:22 -0700 | [diff] [blame] | 706 | if (!msg->findInt32("output-buffers", &numOutput)) { |
| 707 | numOutput = INT32_MAX; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 708 | } |
Chong Zhang | e47d444 | 2014-09-30 13:12:22 -0700 | [diff] [blame] | 709 | |
| 710 | if (!mPaused) { |
| 711 | while (numInput-- > 0 && handleAnInputBuffer()) {} |
| 712 | } |
| 713 | |
| 714 | while (numOutput-- > 0 && handleAnOutputBuffer()) {} |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 715 | } |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 716 | |
| 717 | requestCodecNotification(); |
| 718 | break; |
| 719 | } |
| 720 | |
| 721 | case kWhatInputBufferFilled: |
| 722 | { |
| 723 | if (!isStaleReply(msg)) { |
Wei Jia | 2245fc6 | 2014-10-02 15:12:25 -0700 | [diff] [blame] | 724 | if (!mPendingInputMessages.empty() |
| 725 | || !onInputBufferFilled(msg)) { |
| 726 | mPendingInputMessages.push_back(msg); |
| 727 | } |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 728 | } |
Lajos Molnar | b9b87fe | 2014-09-10 13:53:21 -0700 | [diff] [blame] | 729 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 730 | break; |
| 731 | } |
| 732 | |
| 733 | case kWhatRenderBuffer: |
| 734 | { |
| 735 | if (!isStaleReply(msg)) { |
| 736 | onRenderBuffer(msg); |
| 737 | } |
| 738 | break; |
| 739 | } |
| 740 | |
| 741 | case kWhatFlush: |
| 742 | { |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 743 | sp<AMessage> format; |
| 744 | if (msg->findMessage("new-format", &format)) { |
| 745 | rememberCodecSpecificData(format); |
| 746 | } |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 747 | onFlush(); |
| 748 | break; |
| 749 | } |
| 750 | |
Wei Jia | 704e726 | 2014-06-04 16:21:56 -0700 | [diff] [blame] | 751 | case kWhatResume: |
| 752 | { |
| 753 | onResume(); |
| 754 | break; |
| 755 | } |
| 756 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 757 | case kWhatShutdown: |
| 758 | { |
| 759 | onShutdown(); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 760 | break; |
| 761 | } |
| 762 | |
| 763 | default: |
| 764 | TRESPASS(); |
| 765 | break; |
| 766 | } |
| 767 | } |
| 768 | |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 769 | void NuPlayer::Decoder::signalFlush(const sp<AMessage> &format) { |
| 770 | sp<AMessage> msg = new AMessage(kWhatFlush, id()); |
| 771 | if (format != NULL) { |
| 772 | msg->setMessage("new-format", format); |
| 773 | } |
| 774 | msg->post(); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | void NuPlayer::Decoder::signalResume() { |
Wei Jia | 704e726 | 2014-06-04 16:21:56 -0700 | [diff] [blame] | 778 | (new AMessage(kWhatResume, id()))->post(); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 779 | } |
| 780 | |
Andreas Huber | 3831a06 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 781 | void NuPlayer::Decoder::initiateShutdown() { |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 782 | (new AMessage(kWhatShutdown, id()))->post(); |
Andreas Huber | 3831a06 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 783 | } |
| 784 | |
Robert Shih | 6d0a94e | 2014-01-23 16:18:22 -0800 | [diff] [blame] | 785 | bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange(const sp<AMessage> &targetFormat) const { |
| 786 | if (targetFormat == NULL) { |
| 787 | return true; |
| 788 | } |
| 789 | |
| 790 | AString mime; |
| 791 | if (!targetFormat->findString("mime", &mime)) { |
| 792 | return false; |
| 793 | } |
| 794 | |
| 795 | if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) { |
| 796 | // field-by-field comparison |
| 797 | const char * keys[] = { "channel-count", "sample-rate", "is-adts" }; |
| 798 | for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) { |
| 799 | int32_t oldVal, newVal; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 800 | if (!mOutputFormat->findInt32(keys[i], &oldVal) || |
| 801 | !targetFormat->findInt32(keys[i], &newVal) || |
| 802 | oldVal != newVal) { |
Robert Shih | 6d0a94e | 2014-01-23 16:18:22 -0800 | [diff] [blame] | 803 | return false; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | sp<ABuffer> oldBuf, newBuf; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 808 | if (mOutputFormat->findBuffer("csd-0", &oldBuf) && |
| 809 | targetFormat->findBuffer("csd-0", &newBuf)) { |
Robert Shih | 6d0a94e | 2014-01-23 16:18:22 -0800 | [diff] [blame] | 810 | if (oldBuf->size() != newBuf->size()) { |
| 811 | return false; |
| 812 | } |
| 813 | return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size()); |
| 814 | } |
| 815 | } |
| 816 | return false; |
| 817 | } |
| 818 | |
| 819 | bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const { |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 820 | if (mOutputFormat == NULL) { |
Robert Shih | 6d0a94e | 2014-01-23 16:18:22 -0800 | [diff] [blame] | 821 | return false; |
| 822 | } |
| 823 | |
| 824 | if (targetFormat == NULL) { |
| 825 | return true; |
| 826 | } |
| 827 | |
| 828 | AString oldMime, newMime; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 829 | if (!mOutputFormat->findString("mime", &oldMime) |
Robert Shih | 6d0a94e | 2014-01-23 16:18:22 -0800 | [diff] [blame] | 830 | || !targetFormat->findString("mime", &newMime) |
| 831 | || !(oldMime == newMime)) { |
| 832 | return false; |
| 833 | } |
| 834 | |
| 835 | bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/")); |
| 836 | bool seamless; |
| 837 | if (audio) { |
| 838 | seamless = supportsSeamlessAudioFormatChange(targetFormat); |
| 839 | } else { |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 840 | int32_t isAdaptive; |
| 841 | seamless = (mCodec != NULL && |
| 842 | mInputFormat->findInt32("adaptive-playback", &isAdaptive) && |
| 843 | isAdaptive); |
Robert Shih | 6d0a94e | 2014-01-23 16:18:22 -0800 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str()); |
| 847 | return seamless; |
| 848 | } |
| 849 | |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame^] | 850 | bool NuPlayer::Decoder::isVideo() { |
| 851 | return mNativeWindow != NULL; |
| 852 | } |
| 853 | |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 854 | struct CCData { |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 855 | CCData(uint8_t type, uint8_t data1, uint8_t data2) |
| 856 | : mType(type), mData1(data1), mData2(data2) { |
| 857 | } |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 858 | bool getChannel(size_t *channel) const { |
| 859 | if (mData1 >= 0x10 && mData1 <= 0x1f) { |
| 860 | *channel = (mData1 >= 0x18 ? 1 : 0) + (mType ? 2 : 0); |
| 861 | return true; |
| 862 | } |
| 863 | return false; |
| 864 | } |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 865 | |
| 866 | uint8_t mType; |
| 867 | uint8_t mData1; |
| 868 | uint8_t mData2; |
| 869 | }; |
| 870 | |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 871 | static bool isNullPad(CCData *cc) { |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 872 | return cc->mData1 < 0x10 && cc->mData2 < 0x10; |
| 873 | } |
| 874 | |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 875 | static void dumpBytePair(const sp<ABuffer> &ccBuf) { |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 876 | size_t offset = 0; |
| 877 | AString out; |
| 878 | |
| 879 | while (offset < ccBuf->size()) { |
| 880 | char tmp[128]; |
| 881 | |
| 882 | CCData *cc = (CCData *) (ccBuf->data() + offset); |
| 883 | |
| 884 | if (isNullPad(cc)) { |
| 885 | // 1 null pad or XDS metadata, ignore |
| 886 | offset += sizeof(CCData); |
| 887 | continue; |
| 888 | } |
| 889 | |
| 890 | if (cc->mData1 >= 0x20 && cc->mData1 <= 0x7f) { |
| 891 | // 2 basic chars |
| 892 | sprintf(tmp, "[%d]Basic: %c %c", cc->mType, cc->mData1, cc->mData2); |
| 893 | } else if ((cc->mData1 == 0x11 || cc->mData1 == 0x19) |
| 894 | && cc->mData2 >= 0x30 && cc->mData2 <= 0x3f) { |
| 895 | // 1 special char |
| 896 | sprintf(tmp, "[%d]Special: %02x %02x", cc->mType, cc->mData1, cc->mData2); |
| 897 | } else if ((cc->mData1 == 0x12 || cc->mData1 == 0x1A) |
| 898 | && cc->mData2 >= 0x20 && cc->mData2 <= 0x3f){ |
| 899 | // 1 Spanish/French char |
| 900 | sprintf(tmp, "[%d]Spanish: %02x %02x", cc->mType, cc->mData1, cc->mData2); |
| 901 | } else if ((cc->mData1 == 0x13 || cc->mData1 == 0x1B) |
| 902 | && cc->mData2 >= 0x20 && cc->mData2 <= 0x3f){ |
| 903 | // 1 Portuguese/German/Danish char |
| 904 | sprintf(tmp, "[%d]German: %02x %02x", cc->mType, cc->mData1, cc->mData2); |
| 905 | } else if ((cc->mData1 == 0x11 || cc->mData1 == 0x19) |
| 906 | && cc->mData2 >= 0x20 && cc->mData2 <= 0x2f){ |
| 907 | // Mid-Row Codes (Table 69) |
| 908 | sprintf(tmp, "[%d]Mid-row: %02x %02x", cc->mType, cc->mData1, cc->mData2); |
| 909 | } else if (((cc->mData1 == 0x14 || cc->mData1 == 0x1c) |
| 910 | && cc->mData2 >= 0x20 && cc->mData2 <= 0x2f) |
| 911 | || |
| 912 | ((cc->mData1 == 0x17 || cc->mData1 == 0x1f) |
| 913 | && cc->mData2 >= 0x21 && cc->mData2 <= 0x23)){ |
| 914 | // Misc Control Codes (Table 70) |
| 915 | sprintf(tmp, "[%d]Ctrl: %02x %02x", cc->mType, cc->mData1, cc->mData2); |
| 916 | } else if ((cc->mData1 & 0x70) == 0x10 |
| 917 | && (cc->mData2 & 0x40) == 0x40 |
| 918 | && ((cc->mData1 & 0x07) || !(cc->mData2 & 0x20)) ) { |
| 919 | // Preamble Address Codes (Table 71) |
| 920 | sprintf(tmp, "[%d]PAC: %02x %02x", cc->mType, cc->mData1, cc->mData2); |
| 921 | } else { |
| 922 | sprintf(tmp, "[%d]Invalid: %02x %02x", cc->mType, cc->mData1, cc->mData2); |
| 923 | } |
| 924 | |
| 925 | if (out.size() > 0) { |
| 926 | out.append(", "); |
| 927 | } |
| 928 | |
| 929 | out.append(tmp); |
| 930 | |
| 931 | offset += sizeof(CCData); |
| 932 | } |
| 933 | |
| 934 | ALOGI("%s", out.c_str()); |
| 935 | } |
| 936 | |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 937 | NuPlayer::CCDecoder::CCDecoder(const sp<AMessage> ¬ify) |
| 938 | : mNotify(notify), |
| 939 | mCurrentChannel(0), |
| 940 | mSelectedTrack(-1) { |
| 941 | for (size_t i = 0; i < sizeof(mTrackIndices)/sizeof(mTrackIndices[0]); ++i) { |
| 942 | mTrackIndices[i] = -1; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | size_t NuPlayer::CCDecoder::getTrackCount() const { |
| 947 | return mFoundChannels.size(); |
| 948 | } |
| 949 | |
| 950 | sp<AMessage> NuPlayer::CCDecoder::getTrackInfo(size_t index) const { |
| 951 | if (!isTrackValid(index)) { |
| 952 | return NULL; |
| 953 | } |
| 954 | |
| 955 | sp<AMessage> format = new AMessage(); |
| 956 | |
| 957 | format->setInt32("type", MEDIA_TRACK_TYPE_SUBTITLE); |
| 958 | format->setString("language", "und"); |
| 959 | format->setString("mime", MEDIA_MIMETYPE_TEXT_CEA_608); |
| 960 | //CC1, field 0 channel 0 |
| 961 | bool isDefaultAuto = (mFoundChannels[index] == 0); |
| 962 | format->setInt32("auto", isDefaultAuto); |
| 963 | format->setInt32("default", isDefaultAuto); |
| 964 | format->setInt32("forced", 0); |
| 965 | |
| 966 | return format; |
| 967 | } |
| 968 | |
| 969 | status_t NuPlayer::CCDecoder::selectTrack(size_t index, bool select) { |
| 970 | if (!isTrackValid(index)) { |
| 971 | return BAD_VALUE; |
| 972 | } |
| 973 | |
| 974 | if (select) { |
| 975 | if (mSelectedTrack == (ssize_t)index) { |
| 976 | ALOGE("track %zu already selected", index); |
| 977 | return BAD_VALUE; |
| 978 | } |
| 979 | ALOGV("selected track %zu", index); |
| 980 | mSelectedTrack = index; |
| 981 | } else { |
| 982 | if (mSelectedTrack != (ssize_t)index) { |
| 983 | ALOGE("track %zu is not selected", index); |
| 984 | return BAD_VALUE; |
| 985 | } |
| 986 | ALOGV("unselected track %zu", index); |
| 987 | mSelectedTrack = -1; |
| 988 | } |
| 989 | |
| 990 | return OK; |
| 991 | } |
| 992 | |
| 993 | bool NuPlayer::CCDecoder::isSelected() const { |
| 994 | return mSelectedTrack >= 0 && mSelectedTrack < (int32_t) getTrackCount(); |
| 995 | } |
| 996 | |
| 997 | bool NuPlayer::CCDecoder::isTrackValid(size_t index) const { |
| 998 | return index < getTrackCount(); |
| 999 | } |
| 1000 | |
| 1001 | int32_t NuPlayer::CCDecoder::getTrackIndex(size_t channel) const { |
| 1002 | if (channel < sizeof(mTrackIndices)/sizeof(mTrackIndices[0])) { |
| 1003 | return mTrackIndices[channel]; |
| 1004 | } |
| 1005 | return -1; |
| 1006 | } |
| 1007 | |
| 1008 | // returns true if a new CC track is found |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 1009 | bool NuPlayer::CCDecoder::extractFromSEI(const sp<ABuffer> &accessUnit) { |
| 1010 | int64_t timeUs; |
| 1011 | CHECK(accessUnit->meta()->findInt64("timeUs", &timeUs)); |
| 1012 | |
| 1013 | sp<ABuffer> sei; |
| 1014 | if (!accessUnit->meta()->findBuffer("sei", &sei) || sei == NULL) { |
| 1015 | return false; |
| 1016 | } |
| 1017 | |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 1018 | bool trackAdded = false; |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 1019 | |
Chong Zhang | 862f845 | 2014-06-26 19:55:23 -0700 | [diff] [blame] | 1020 | NALBitReader br(sei->data() + 1, sei->size() - 1); |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 1021 | // sei_message() |
Chong Zhang | 862f845 | 2014-06-26 19:55:23 -0700 | [diff] [blame] | 1022 | while (br.atLeastNumBitsLeft(16)) { // at least 16-bit for sei_message() |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 1023 | uint32_t payload_type = 0; |
| 1024 | size_t payload_size = 0; |
| 1025 | uint8_t last_byte; |
| 1026 | |
| 1027 | do { |
| 1028 | last_byte = br.getBits(8); |
| 1029 | payload_type += last_byte; |
| 1030 | } while (last_byte == 0xFF); |
| 1031 | |
| 1032 | do { |
| 1033 | last_byte = br.getBits(8); |
| 1034 | payload_size += last_byte; |
| 1035 | } while (last_byte == 0xFF); |
| 1036 | |
| 1037 | // sei_payload() |
| 1038 | if (payload_type == 4) { |
| 1039 | // user_data_registered_itu_t_t35() |
| 1040 | |
| 1041 | // ATSC A/72: 6.4.2 |
| 1042 | uint8_t itu_t_t35_country_code = br.getBits(8); |
| 1043 | uint16_t itu_t_t35_provider_code = br.getBits(16); |
| 1044 | uint32_t user_identifier = br.getBits(32); |
| 1045 | uint8_t user_data_type_code = br.getBits(8); |
| 1046 | |
| 1047 | payload_size -= 1 + 2 + 4 + 1; |
| 1048 | |
| 1049 | if (itu_t_t35_country_code == 0xB5 |
| 1050 | && itu_t_t35_provider_code == 0x0031 |
| 1051 | && user_identifier == 'GA94' |
| 1052 | && user_data_type_code == 0x3) { |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 1053 | // MPEG_cc_data() |
| 1054 | // ATSC A/53 Part 4: 6.2.3.1 |
| 1055 | br.skipBits(1); //process_em_data_flag |
| 1056 | bool process_cc_data_flag = br.getBits(1); |
| 1057 | br.skipBits(1); //additional_data_flag |
| 1058 | size_t cc_count = br.getBits(5); |
| 1059 | br.skipBits(8); // em_data; |
| 1060 | payload_size -= 2; |
| 1061 | |
| 1062 | if (process_cc_data_flag) { |
| 1063 | AString out; |
| 1064 | |
| 1065 | sp<ABuffer> ccBuf = new ABuffer(cc_count * sizeof(CCData)); |
| 1066 | ccBuf->setRange(0, 0); |
| 1067 | |
| 1068 | for (size_t i = 0; i < cc_count; i++) { |
| 1069 | uint8_t marker = br.getBits(5); |
| 1070 | CHECK_EQ(marker, 0x1f); |
| 1071 | |
| 1072 | bool cc_valid = br.getBits(1); |
| 1073 | uint8_t cc_type = br.getBits(2); |
| 1074 | // remove odd parity bit |
| 1075 | uint8_t cc_data_1 = br.getBits(8) & 0x7f; |
| 1076 | uint8_t cc_data_2 = br.getBits(8) & 0x7f; |
| 1077 | |
| 1078 | if (cc_valid |
| 1079 | && (cc_type == 0 || cc_type == 1)) { |
| 1080 | CCData cc(cc_type, cc_data_1, cc_data_2); |
| 1081 | if (!isNullPad(&cc)) { |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 1082 | size_t channel; |
| 1083 | if (cc.getChannel(&channel) && getTrackIndex(channel) < 0) { |
| 1084 | mTrackIndices[channel] = mFoundChannels.size(); |
| 1085 | mFoundChannels.push_back(channel); |
| 1086 | trackAdded = true; |
| 1087 | } |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 1088 | memcpy(ccBuf->data() + ccBuf->size(), |
| 1089 | (void *)&cc, sizeof(cc)); |
| 1090 | ccBuf->setRange(0, ccBuf->size() + sizeof(CCData)); |
| 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | payload_size -= cc_count * 3; |
| 1095 | |
| 1096 | mCCMap.add(timeUs, ccBuf); |
| 1097 | break; |
| 1098 | } |
| 1099 | } else { |
| 1100 | ALOGV("Malformed SEI payload type 4"); |
| 1101 | } |
| 1102 | } else { |
| 1103 | ALOGV("Unsupported SEI payload type %d", payload_type); |
| 1104 | } |
| 1105 | |
| 1106 | // skipping remaining bits of this payload |
| 1107 | br.skipBits(payload_size * 8); |
| 1108 | } |
| 1109 | |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 1110 | return trackAdded; |
| 1111 | } |
| 1112 | |
| 1113 | sp<ABuffer> NuPlayer::CCDecoder::filterCCBuf( |
| 1114 | const sp<ABuffer> &ccBuf, size_t index) { |
| 1115 | sp<ABuffer> filteredCCBuf = new ABuffer(ccBuf->size()); |
| 1116 | filteredCCBuf->setRange(0, 0); |
| 1117 | |
| 1118 | size_t cc_count = ccBuf->size() / sizeof(CCData); |
| 1119 | const CCData* cc_data = (const CCData*)ccBuf->data(); |
| 1120 | for (size_t i = 0; i < cc_count; ++i) { |
| 1121 | size_t channel; |
| 1122 | if (cc_data[i].getChannel(&channel)) { |
| 1123 | mCurrentChannel = channel; |
| 1124 | } |
| 1125 | if (mCurrentChannel == mFoundChannels[index]) { |
| 1126 | memcpy(filteredCCBuf->data() + filteredCCBuf->size(), |
| 1127 | (void *)&cc_data[i], sizeof(CCData)); |
| 1128 | filteredCCBuf->setRange(0, filteredCCBuf->size() + sizeof(CCData)); |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | return filteredCCBuf; |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | void NuPlayer::CCDecoder::decode(const sp<ABuffer> &accessUnit) { |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 1136 | if (extractFromSEI(accessUnit)) { |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 1137 | ALOGI("Found CEA-608 track"); |
| 1138 | sp<AMessage> msg = mNotify->dup(); |
| 1139 | msg->setInt32("what", kWhatTrackAdded); |
| 1140 | msg->post(); |
| 1141 | } |
| 1142 | // TODO: extract CC from other sources |
| 1143 | } |
| 1144 | |
| 1145 | void NuPlayer::CCDecoder::display(int64_t timeUs) { |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 1146 | if (!isTrackValid(mSelectedTrack)) { |
| 1147 | ALOGE("Could not find current track(index=%d)", mSelectedTrack); |
| 1148 | return; |
| 1149 | } |
| 1150 | |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 1151 | ssize_t index = mCCMap.indexOfKey(timeUs); |
| 1152 | if (index < 0) { |
| 1153 | ALOGV("cc for timestamp %" PRId64 " not found", timeUs); |
| 1154 | return; |
| 1155 | } |
| 1156 | |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 1157 | sp<ABuffer> ccBuf = filterCCBuf(mCCMap.valueAt(index), mSelectedTrack); |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 1158 | |
| 1159 | if (ccBuf->size() > 0) { |
| 1160 | #if 0 |
| 1161 | dumpBytePair(ccBuf); |
| 1162 | #endif |
| 1163 | |
| 1164 | ccBuf->meta()->setInt32("trackIndex", mSelectedTrack); |
| 1165 | ccBuf->meta()->setInt64("timeUs", timeUs); |
| 1166 | ccBuf->meta()->setInt64("durationUs", 0ll); |
| 1167 | |
| 1168 | sp<AMessage> msg = mNotify->dup(); |
| 1169 | msg->setInt32("what", kWhatClosedCaptionData); |
| 1170 | msg->setBuffer("buffer", ccBuf); |
| 1171 | msg->post(); |
| 1172 | } |
| 1173 | |
| 1174 | // remove all entries before timeUs |
| 1175 | mCCMap.removeItemsAt(0, index + 1); |
| 1176 | } |
| 1177 | |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 1178 | void NuPlayer::CCDecoder::flush() { |
| 1179 | mCCMap.clear(); |
| 1180 | } |
| 1181 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1182 | } // namespace android |
| 1183 | |