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