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