Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017 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 "NuPlayer2Decoder" |
| 19 | #include <utils/Log.h> |
| 20 | #include <inttypes.h> |
| 21 | |
| 22 | #include <algorithm> |
| 23 | |
| 24 | #include "NdkWrapper.h" |
| 25 | #include "NuPlayer2CCDecoder.h" |
| 26 | #include "NuPlayer2Decoder.h" |
| 27 | #include "NuPlayer2Drm.h" |
| 28 | #include "NuPlayer2Renderer.h" |
| 29 | #include "NuPlayer2Source.h" |
| 30 | |
| 31 | #include <cutils/properties.h> |
| 32 | #include <media/MediaCodecBuffer.h> |
| 33 | #include <media/NdkMediaCodec.h> |
| 34 | #include <media/stagefright/foundation/ABuffer.h> |
| 35 | #include <media/stagefright/foundation/ADebug.h> |
| 36 | #include <media/stagefright/foundation/AMessage.h> |
| 37 | #include <media/stagefright/foundation/avc_utils.h> |
| 38 | #include <media/stagefright/MediaBuffer.h> |
| 39 | #include <media/stagefright/MediaDefs.h> |
| 40 | #include <media/stagefright/MediaErrors.h> |
| 41 | #include <media/stagefright/SurfaceUtils.h> |
| 42 | #include <gui/Surface.h> |
| 43 | |
| 44 | #include "ATSParser.h" |
| 45 | |
| 46 | namespace android { |
| 47 | |
| 48 | static float kDisplayRefreshingRate = 60.f; // TODO: get this from the display |
| 49 | |
| 50 | // The default total video frame rate of a stream when that info is not available from |
| 51 | // the source. |
| 52 | static float kDefaultVideoFrameRateTotal = 30.f; |
| 53 | |
| 54 | static inline bool getAudioDeepBufferSetting() { |
| 55 | return property_get_bool("media.stagefright.audio.deep", false /* default_value */); |
| 56 | } |
| 57 | |
| 58 | NuPlayer2::Decoder::Decoder( |
| 59 | const sp<AMessage> ¬ify, |
| 60 | const sp<Source> &source, |
| 61 | pid_t pid, |
| 62 | uid_t uid, |
| 63 | const sp<Renderer> &renderer, |
| 64 | const sp<Surface> &surface, |
| 65 | const sp<CCDecoder> &ccDecoder) |
| 66 | : DecoderBase(notify), |
| 67 | mSurface(surface), |
| 68 | mSource(source), |
| 69 | mRenderer(renderer), |
| 70 | mCCDecoder(ccDecoder), |
| 71 | mPid(pid), |
| 72 | mUid(uid), |
| 73 | mSkipRenderingUntilMediaTimeUs(-1ll), |
| 74 | mNumFramesTotal(0ll), |
| 75 | mNumInputFramesDropped(0ll), |
| 76 | mNumOutputFramesDropped(0ll), |
| 77 | mVideoWidth(0), |
| 78 | mVideoHeight(0), |
| 79 | mIsAudio(true), |
| 80 | mIsVideoAVC(false), |
| 81 | mIsSecure(false), |
| 82 | mIsEncrypted(false), |
| 83 | mIsEncryptedObservedEarlier(false), |
| 84 | mFormatChangePending(false), |
| 85 | mTimeChangePending(false), |
| 86 | mFrameRateTotal(kDefaultVideoFrameRateTotal), |
| 87 | mPlaybackSpeed(1.0f), |
| 88 | mNumVideoTemporalLayerTotal(1), // decode all layers |
| 89 | mNumVideoTemporalLayerAllowed(1), |
| 90 | mCurrentMaxVideoTemporalLayerId(0), |
| 91 | mResumePending(false), |
| 92 | mComponentName("decoder") { |
| 93 | mVideoTemporalLayerAggregateFps[0] = mFrameRateTotal; |
| 94 | } |
| 95 | |
| 96 | NuPlayer2::Decoder::~Decoder() { |
| 97 | // Need to stop looper first since mCodec could be accessed on the mDecoderLooper. |
| 98 | stopLooper(); |
| 99 | if (mCodec != NULL) { |
| 100 | mCodec->release(); |
| 101 | } |
| 102 | releaseAndResetMediaBuffers(); |
| 103 | } |
| 104 | |
| 105 | sp<AMessage> NuPlayer2::Decoder::getStats() const { |
| 106 | mStats->setInt64("frames-total", mNumFramesTotal); |
| 107 | mStats->setInt64("frames-dropped-input", mNumInputFramesDropped); |
| 108 | mStats->setInt64("frames-dropped-output", mNumOutputFramesDropped); |
| 109 | return mStats; |
| 110 | } |
| 111 | |
| 112 | status_t NuPlayer2::Decoder::setVideoSurface(const sp<Surface> &surface) { |
| 113 | if (surface == NULL || ADebug::isExperimentEnabled("legacy-setsurface")) { |
| 114 | return BAD_VALUE; |
| 115 | } |
| 116 | |
| 117 | sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this); |
| 118 | |
| 119 | msg->setObject("surface", surface); |
| 120 | sp<AMessage> response; |
| 121 | status_t err = msg->postAndAwaitResponse(&response); |
| 122 | if (err == OK && response != NULL) { |
| 123 | CHECK(response->findInt32("err", &err)); |
| 124 | } |
| 125 | return err; |
| 126 | } |
| 127 | |
| 128 | void NuPlayer2::Decoder::onMessageReceived(const sp<AMessage> &msg) { |
| 129 | ALOGV("[%s] onMessage: %s", mComponentName.c_str(), msg->debugString().c_str()); |
| 130 | |
| 131 | switch (msg->what()) { |
| 132 | case kWhatCodecNotify: |
| 133 | { |
| 134 | int32_t cbID; |
| 135 | CHECK(msg->findInt32("callbackID", &cbID)); |
| 136 | |
| 137 | ALOGV("[%s] kWhatCodecNotify: cbID = %d, paused = %d", |
| 138 | mIsAudio ? "audio" : "video", cbID, mPaused); |
| 139 | |
| 140 | if (mPaused) { |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | switch (cbID) { |
| 145 | case AMediaCodecWrapper::CB_INPUT_AVAILABLE: |
| 146 | { |
| 147 | int32_t index; |
| 148 | CHECK(msg->findInt32("index", &index)); |
| 149 | |
| 150 | handleAnInputBuffer(index); |
| 151 | break; |
| 152 | } |
| 153 | |
| 154 | case AMediaCodecWrapper::CB_OUTPUT_AVAILABLE: |
| 155 | { |
| 156 | int32_t index; |
| 157 | size_t offset; |
| 158 | size_t size; |
| 159 | int64_t timeUs; |
| 160 | int32_t flags; |
| 161 | |
| 162 | CHECK(msg->findInt32("index", &index)); |
| 163 | CHECK(msg->findSize("offset", &offset)); |
| 164 | CHECK(msg->findSize("size", &size)); |
| 165 | CHECK(msg->findInt64("timeUs", &timeUs)); |
| 166 | CHECK(msg->findInt32("flags", &flags)); |
| 167 | |
| 168 | handleAnOutputBuffer(index, offset, size, timeUs, flags); |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | case AMediaCodecWrapper::CB_OUTPUT_FORMAT_CHANGED: |
| 173 | { |
| 174 | sp<AMessage> format; |
| 175 | CHECK(msg->findMessage("format", &format)); |
| 176 | |
| 177 | handleOutputFormatChange(format); |
| 178 | break; |
| 179 | } |
| 180 | |
| 181 | case AMediaCodecWrapper::CB_ERROR: |
| 182 | { |
| 183 | status_t err; |
| 184 | CHECK(msg->findInt32("err", &err)); |
| 185 | ALOGE("Decoder (%s) reported error : 0x%x", |
| 186 | mIsAudio ? "audio" : "video", err); |
| 187 | |
| 188 | handleError(err); |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | default: |
| 193 | { |
| 194 | TRESPASS(); |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | case kWhatRenderBuffer: |
| 203 | { |
| 204 | if (!isStaleReply(msg)) { |
| 205 | onRenderBuffer(msg); |
| 206 | } |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | case kWhatAudioOutputFormatChanged: |
| 211 | { |
| 212 | if (!isStaleReply(msg)) { |
| 213 | status_t err; |
| 214 | if (msg->findInt32("err", &err) && err != OK) { |
| 215 | ALOGE("Renderer reported 0x%x when changing audio output format", err); |
| 216 | handleError(err); |
| 217 | } |
| 218 | } |
| 219 | break; |
| 220 | } |
| 221 | |
| 222 | case kWhatSetVideoSurface: |
| 223 | { |
| 224 | sp<AReplyToken> replyID; |
| 225 | CHECK(msg->senderAwaitsResponse(&replyID)); |
| 226 | |
| 227 | sp<RefBase> obj; |
| 228 | CHECK(msg->findObject("surface", &obj)); |
| 229 | sp<Surface> surface = static_cast<Surface *>(obj.get()); // non-null |
| 230 | int32_t err = INVALID_OPERATION; |
| 231 | // NOTE: in practice mSurface is always non-null, but checking here for completeness |
| 232 | if (mCodec != NULL && mSurface != NULL) { |
| 233 | // TODO: once AwesomePlayer is removed, remove this automatic connecting |
| 234 | // to the surface by MediaPlayerService. |
| 235 | // |
| 236 | // at this point MediaPlayerService::client has already connected to the |
| 237 | // surface, which MediaCodec does not expect |
| 238 | err = nativeWindowDisconnect(surface.get(), "kWhatSetVideoSurface(surface)"); |
| 239 | if (err == OK) { |
| 240 | err = mCodec->setOutputSurface(surface); |
| 241 | ALOGI_IF(err, "codec setOutputSurface returned: %d", err); |
| 242 | if (err == OK) { |
| 243 | // reconnect to the old surface as MPS::Client will expect to |
| 244 | // be able to disconnect from it. |
| 245 | (void)nativeWindowConnect(mSurface.get(), "kWhatSetVideoSurface(mSurface)"); |
| 246 | mSurface = surface; |
| 247 | } |
| 248 | } |
| 249 | if (err != OK) { |
| 250 | // reconnect to the new surface on error as MPS::Client will expect to |
| 251 | // be able to disconnect from it. |
| 252 | (void)nativeWindowConnect(surface.get(), "kWhatSetVideoSurface(err)"); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | sp<AMessage> response = new AMessage; |
| 257 | response->setInt32("err", err); |
| 258 | response->postReply(replyID); |
| 259 | break; |
| 260 | } |
| 261 | |
| 262 | case kWhatDrmReleaseCrypto: |
| 263 | { |
| 264 | ALOGV("kWhatDrmReleaseCrypto"); |
| 265 | onReleaseCrypto(msg); |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | default: |
| 270 | DecoderBase::onMessageReceived(msg); |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void NuPlayer2::Decoder::onConfigure(const sp<AMessage> &format) { |
| 276 | ALOGV("[%s] onConfigure (format=%s)", mComponentName.c_str(), format->debugString().c_str()); |
| 277 | CHECK(mCodec == NULL); |
| 278 | |
| 279 | mFormatChangePending = false; |
| 280 | mTimeChangePending = false; |
| 281 | |
| 282 | ++mBufferGeneration; |
| 283 | |
| 284 | AString mime; |
| 285 | CHECK(format->findString("mime", &mime)); |
| 286 | |
| 287 | mIsAudio = !strncasecmp("audio/", mime.c_str(), 6); |
| 288 | mIsVideoAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str()); |
| 289 | |
| 290 | mComponentName = mime; |
| 291 | mComponentName.append(" decoder"); |
| 292 | ALOGV("[%s] onConfigure (surface=%p)", mComponentName.c_str(), mSurface.get()); |
| 293 | |
| 294 | mCodec = AMediaCodecWrapper::CreateDecoderByType(mime); |
| 295 | int32_t secure = 0; |
| 296 | if (format->findInt32("secure", &secure) && secure != 0) { |
| 297 | if (mCodec != NULL) { |
| 298 | if (mCodec->getName(&mComponentName) == OK) { |
| 299 | mComponentName.append(".secure"); |
| 300 | mCodec->release(); |
| 301 | ALOGI("[%s] creating", mComponentName.c_str()); |
| 302 | mCodec = AMediaCodecWrapper::CreateCodecByName(mComponentName); |
| 303 | } else { |
| 304 | mCodec = NULL; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | if (mCodec == NULL) { |
| 309 | ALOGE("Failed to create %s%s decoder", |
| 310 | (secure ? "secure " : ""), mime.c_str()); |
| 311 | handleError(NO_INIT); |
| 312 | return; |
| 313 | } |
| 314 | mIsSecure = secure; |
| 315 | |
| 316 | mCodec->getName(&mComponentName); |
| 317 | |
| 318 | status_t err; |
| 319 | if (mSurface != NULL) { |
| 320 | // disconnect from surface as MediaCodec will reconnect |
| 321 | err = nativeWindowDisconnect(mSurface.get(), "onConfigure"); |
| 322 | // We treat this as a warning, as this is a preparatory step. |
| 323 | // Codec will try to connect to the surface, which is where |
| 324 | // any error signaling will occur. |
| 325 | ALOGW_IF(err != OK, "failed to disconnect from surface: %d", err); |
| 326 | } |
| 327 | |
| 328 | // Modular DRM |
| 329 | sp<RefBase> objCrypto; |
| 330 | format->findObject("crypto", &objCrypto); |
| 331 | sp<AMediaCryptoWrapper> crypto = static_cast<AMediaCryptoWrapper *>(objCrypto.get()); |
| 332 | // non-encrypted source won't have a crypto |
| 333 | mIsEncrypted = (crypto != NULL); |
| 334 | // configure is called once; still using OR in case the behavior changes. |
| 335 | mIsEncryptedObservedEarlier = mIsEncryptedObservedEarlier || mIsEncrypted; |
| 336 | ALOGV("onConfigure mCrypto: %p, mIsSecure: %d", crypto.get(), mIsSecure); |
| 337 | |
| 338 | err = mCodec->configure( |
| 339 | AMediaFormatWrapper::Create(format), |
| 340 | mSurface, |
| 341 | crypto, |
| 342 | 0 /* flags */); |
| 343 | |
| 344 | if (err != OK) { |
| 345 | ALOGE("Failed to configure [%s] decoder (err=%d)", mComponentName.c_str(), err); |
| 346 | mCodec->release(); |
| 347 | mCodec.clear(); |
| 348 | handleError(err); |
| 349 | return; |
| 350 | } |
| 351 | rememberCodecSpecificData(format); |
| 352 | |
| 353 | // the following should work in configured state |
| 354 | sp<AMediaFormatWrapper> outputFormat = mCodec->getOutputFormat(); |
| 355 | if (outputFormat == NULL) { |
| 356 | handleError(INVALID_OPERATION); |
| 357 | return; |
| 358 | } |
| 359 | mInputFormat = mCodec->getInputFormat(); |
| 360 | if (mInputFormat == NULL) { |
| 361 | handleError(INVALID_OPERATION); |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | mStats->setString("mime", mime.c_str()); |
| 366 | mStats->setString("component-name", mComponentName.c_str()); |
| 367 | |
| 368 | if (!mIsAudio) { |
| 369 | int32_t width, height; |
| 370 | if (outputFormat->getInt32("width", &width) |
| 371 | && outputFormat->getInt32("height", &height)) { |
| 372 | mStats->setInt32("width", width); |
| 373 | mStats->setInt32("height", height); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | sp<AMessage> reply = new AMessage(kWhatCodecNotify, this); |
| 378 | mCodec->setCallback(reply); |
| 379 | |
| 380 | err = mCodec->start(); |
| 381 | if (err != OK) { |
| 382 | ALOGE("Failed to start [%s] decoder (err=%d)", mComponentName.c_str(), err); |
| 383 | mCodec->release(); |
| 384 | mCodec.clear(); |
| 385 | handleError(err); |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | releaseAndResetMediaBuffers(); |
| 390 | |
| 391 | mPaused = false; |
| 392 | mResumePending = false; |
| 393 | } |
| 394 | |
| 395 | void NuPlayer2::Decoder::onSetParameters(const sp<AMessage> ¶ms) { |
| 396 | bool needAdjustLayers = false; |
| 397 | float frameRateTotal; |
| 398 | if (params->findFloat("frame-rate-total", &frameRateTotal) |
| 399 | && mFrameRateTotal != frameRateTotal) { |
| 400 | needAdjustLayers = true; |
| 401 | mFrameRateTotal = frameRateTotal; |
| 402 | } |
| 403 | |
| 404 | int32_t numVideoTemporalLayerTotal; |
| 405 | if (params->findInt32("temporal-layer-count", &numVideoTemporalLayerTotal) |
| 406 | && numVideoTemporalLayerTotal >= 0 |
| 407 | && numVideoTemporalLayerTotal <= kMaxNumVideoTemporalLayers |
| 408 | && mNumVideoTemporalLayerTotal != numVideoTemporalLayerTotal) { |
| 409 | needAdjustLayers = true; |
| 410 | mNumVideoTemporalLayerTotal = std::max(numVideoTemporalLayerTotal, 1); |
| 411 | } |
| 412 | |
| 413 | if (needAdjustLayers && mNumVideoTemporalLayerTotal > 1) { |
| 414 | // TODO: For now, layer fps is calculated for some specific architectures. |
| 415 | // But it really should be extracted from the stream. |
| 416 | mVideoTemporalLayerAggregateFps[0] = |
| 417 | mFrameRateTotal / (float)(1ll << (mNumVideoTemporalLayerTotal - 1)); |
| 418 | for (int32_t i = 1; i < mNumVideoTemporalLayerTotal; ++i) { |
| 419 | mVideoTemporalLayerAggregateFps[i] = |
| 420 | mFrameRateTotal / (float)(1ll << (mNumVideoTemporalLayerTotal - i)) |
| 421 | + mVideoTemporalLayerAggregateFps[i - 1]; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | float playbackSpeed; |
| 426 | if (params->findFloat("playback-speed", &playbackSpeed) |
| 427 | && mPlaybackSpeed != playbackSpeed) { |
| 428 | needAdjustLayers = true; |
| 429 | mPlaybackSpeed = playbackSpeed; |
| 430 | } |
| 431 | |
| 432 | if (needAdjustLayers) { |
| 433 | float decodeFrameRate = mFrameRateTotal; |
| 434 | // enable temporal layering optimization only if we know the layering depth |
| 435 | if (mNumVideoTemporalLayerTotal > 1) { |
| 436 | int32_t layerId; |
| 437 | for (layerId = 0; layerId < mNumVideoTemporalLayerTotal - 1; ++layerId) { |
| 438 | if (mVideoTemporalLayerAggregateFps[layerId] * mPlaybackSpeed |
| 439 | >= kDisplayRefreshingRate * 0.9) { |
| 440 | break; |
| 441 | } |
| 442 | } |
| 443 | mNumVideoTemporalLayerAllowed = layerId + 1; |
| 444 | decodeFrameRate = mVideoTemporalLayerAggregateFps[layerId]; |
| 445 | } |
| 446 | ALOGV("onSetParameters: allowed layers=%d, decodeFps=%g", |
| 447 | mNumVideoTemporalLayerAllowed, decodeFrameRate); |
| 448 | |
| 449 | if (mCodec == NULL) { |
| 450 | ALOGW("onSetParameters called before codec is created."); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | sp<AMediaFormatWrapper> codecParams = new AMediaFormatWrapper(); |
| 455 | codecParams->setFloat("operating-rate", decodeFrameRate * mPlaybackSpeed); |
| 456 | mCodec->setParameters(codecParams); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | void NuPlayer2::Decoder::onSetRenderer(const sp<Renderer> &renderer) { |
| 461 | mRenderer = renderer; |
| 462 | } |
| 463 | |
| 464 | void NuPlayer2::Decoder::onResume(bool notifyComplete) { |
| 465 | mPaused = false; |
| 466 | |
| 467 | if (notifyComplete) { |
| 468 | mResumePending = true; |
| 469 | } |
| 470 | |
| 471 | if (mCodec == NULL) { |
| 472 | ALOGE("[%s] onResume without a valid codec", mComponentName.c_str()); |
| 473 | handleError(NO_INIT); |
| 474 | return; |
| 475 | } |
| 476 | mCodec->start(); |
| 477 | } |
| 478 | |
| 479 | void NuPlayer2::Decoder::doFlush(bool notifyComplete) { |
| 480 | if (mCCDecoder != NULL) { |
| 481 | mCCDecoder->flush(); |
| 482 | } |
| 483 | |
| 484 | if (mRenderer != NULL) { |
| 485 | mRenderer->flush(mIsAudio, notifyComplete); |
| 486 | mRenderer->signalTimeDiscontinuity(); |
| 487 | } |
| 488 | |
| 489 | status_t err = OK; |
| 490 | if (mCodec != NULL) { |
| 491 | err = mCodec->flush(); |
| 492 | mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator |
| 493 | ++mBufferGeneration; |
| 494 | } |
| 495 | |
| 496 | if (err != OK) { |
| 497 | ALOGE("failed to flush [%s] (err=%d)", mComponentName.c_str(), err); |
| 498 | handleError(err); |
| 499 | // finish with posting kWhatFlushCompleted. |
| 500 | // we attempt to release the buffers even if flush fails. |
| 501 | } |
| 502 | releaseAndResetMediaBuffers(); |
| 503 | mPaused = true; |
| 504 | } |
| 505 | |
| 506 | |
| 507 | void NuPlayer2::Decoder::onFlush() { |
| 508 | doFlush(true); |
| 509 | |
| 510 | if (isDiscontinuityPending()) { |
| 511 | // This could happen if the client starts seeking/shutdown |
| 512 | // after we queued an EOS for discontinuities. |
| 513 | // We can consider discontinuity handled. |
| 514 | finishHandleDiscontinuity(false /* flushOnTimeChange */); |
| 515 | } |
| 516 | |
| 517 | sp<AMessage> notify = mNotify->dup(); |
| 518 | notify->setInt32("what", kWhatFlushCompleted); |
| 519 | notify->post(); |
| 520 | } |
| 521 | |
| 522 | void NuPlayer2::Decoder::onShutdown(bool notifyComplete) { |
| 523 | status_t err = OK; |
| 524 | |
| 525 | // if there is a pending resume request, notify complete now |
| 526 | notifyResumeCompleteIfNecessary(); |
| 527 | |
| 528 | if (mCodec != NULL) { |
| 529 | err = mCodec->release(); |
| 530 | mCodec = NULL; |
| 531 | ++mBufferGeneration; |
| 532 | |
| 533 | if (mSurface != NULL) { |
| 534 | // reconnect to surface as MediaCodec disconnected from it |
| 535 | status_t error = nativeWindowConnect(mSurface.get(), "onShutdown"); |
| 536 | ALOGW_IF(error != NO_ERROR, |
| 537 | "[%s] failed to connect to native window, error=%d", |
| 538 | mComponentName.c_str(), error); |
| 539 | } |
| 540 | mComponentName = "decoder"; |
| 541 | } |
| 542 | |
| 543 | releaseAndResetMediaBuffers(); |
| 544 | |
| 545 | if (err != OK) { |
| 546 | ALOGE("failed to release [%s] (err=%d)", mComponentName.c_str(), err); |
| 547 | handleError(err); |
| 548 | // finish with posting kWhatShutdownCompleted. |
| 549 | } |
| 550 | |
| 551 | if (notifyComplete) { |
| 552 | sp<AMessage> notify = mNotify->dup(); |
| 553 | notify->setInt32("what", kWhatShutdownCompleted); |
| 554 | notify->post(); |
| 555 | mPaused = true; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * returns true if we should request more data |
| 561 | */ |
| 562 | bool NuPlayer2::Decoder::doRequestBuffers() { |
| 563 | if (isDiscontinuityPending()) { |
| 564 | return false; |
| 565 | } |
| 566 | status_t err = OK; |
| 567 | while (err == OK && !mDequeuedInputBuffers.empty()) { |
| 568 | size_t bufferIx = *mDequeuedInputBuffers.begin(); |
| 569 | sp<AMessage> msg = new AMessage(); |
| 570 | msg->setSize("buffer-ix", bufferIx); |
| 571 | err = fetchInputData(msg); |
| 572 | if (err != OK && err != ERROR_END_OF_STREAM) { |
| 573 | // if EOS, need to queue EOS buffer |
| 574 | break; |
| 575 | } |
| 576 | mDequeuedInputBuffers.erase(mDequeuedInputBuffers.begin()); |
| 577 | |
| 578 | if (!mPendingInputMessages.empty() |
| 579 | || !onInputBufferFetched(msg)) { |
| 580 | mPendingInputMessages.push_back(msg); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | return err == -EWOULDBLOCK |
| 585 | && mSource->feedMoreTSData() == OK; |
| 586 | } |
| 587 | |
| 588 | void NuPlayer2::Decoder::handleError(int32_t err) |
| 589 | { |
| 590 | // We cannot immediately release the codec due to buffers still outstanding |
| 591 | // in the renderer. We signal to the player the error so it can shutdown/release the |
| 592 | // decoder after flushing and increment the generation to discard unnecessary messages. |
| 593 | |
| 594 | ++mBufferGeneration; |
| 595 | |
| 596 | sp<AMessage> notify = mNotify->dup(); |
| 597 | notify->setInt32("what", kWhatError); |
| 598 | notify->setInt32("err", err); |
| 599 | notify->post(); |
| 600 | } |
| 601 | |
| 602 | status_t NuPlayer2::Decoder::releaseCrypto() |
| 603 | { |
| 604 | ALOGV("releaseCrypto"); |
| 605 | |
| 606 | sp<AMessage> msg = new AMessage(kWhatDrmReleaseCrypto, this); |
| 607 | |
| 608 | sp<AMessage> response; |
| 609 | status_t status = msg->postAndAwaitResponse(&response); |
| 610 | if (status == OK && response != NULL) { |
| 611 | CHECK(response->findInt32("status", &status)); |
| 612 | ALOGV("releaseCrypto ret: %d ", status); |
| 613 | } else { |
| 614 | ALOGE("releaseCrypto err: %d", status); |
| 615 | } |
| 616 | |
| 617 | return status; |
| 618 | } |
| 619 | |
| 620 | void NuPlayer2::Decoder::onReleaseCrypto(const sp<AMessage>& msg) |
| 621 | { |
| 622 | status_t status = INVALID_OPERATION; |
| 623 | if (mCodec != NULL) { |
| 624 | status = mCodec->releaseCrypto(); |
| 625 | } else { |
| 626 | // returning OK if the codec has been already released |
| 627 | status = OK; |
| 628 | ALOGE("onReleaseCrypto No mCodec. err: %d", status); |
| 629 | } |
| 630 | |
| 631 | sp<AMessage> response = new AMessage; |
| 632 | response->setInt32("status", status); |
| 633 | // Clearing the state as it's tied to crypto. mIsEncryptedObservedEarlier is sticky though |
| 634 | // and lasts for the lifetime of this codec. See its use in fetchInputData. |
| 635 | mIsEncrypted = false; |
| 636 | |
| 637 | sp<AReplyToken> replyID; |
| 638 | CHECK(msg->senderAwaitsResponse(&replyID)); |
| 639 | response->postReply(replyID); |
| 640 | } |
| 641 | |
| 642 | bool NuPlayer2::Decoder::handleAnInputBuffer(size_t index) { |
| 643 | if (isDiscontinuityPending()) { |
| 644 | return false; |
| 645 | } |
| 646 | |
| 647 | if (mCodec == NULL) { |
| 648 | ALOGE("[%s] handleAnInputBuffer without a valid codec", mComponentName.c_str()); |
| 649 | handleError(NO_INIT); |
| 650 | return false; |
| 651 | } |
| 652 | |
| 653 | size_t bufferSize = 0; |
| 654 | uint8_t *bufferBase = mCodec->getInputBuffer(index, &bufferSize); |
| 655 | |
| 656 | if (bufferBase == NULL) { |
| 657 | ALOGE("[%s] handleAnInputBuffer, failed to get input buffer", mComponentName.c_str()); |
| 658 | handleError(UNKNOWN_ERROR); |
| 659 | return false; |
| 660 | } |
| 661 | |
| 662 | sp<MediaCodecBuffer> buffer = |
| 663 | new MediaCodecBuffer(NULL /* format */, new ABuffer(bufferBase, bufferSize)); |
| 664 | |
| 665 | if (index >= mInputBuffers.size()) { |
| 666 | for (size_t i = mInputBuffers.size(); i <= index; ++i) { |
| 667 | mInputBuffers.add(); |
| 668 | mMediaBuffers.add(); |
| 669 | mInputBufferIsDequeued.add(); |
| 670 | mMediaBuffers.editItemAt(i) = NULL; |
| 671 | mInputBufferIsDequeued.editItemAt(i) = false; |
| 672 | } |
| 673 | } |
| 674 | mInputBuffers.editItemAt(index) = buffer; |
| 675 | |
| 676 | //CHECK_LT(bufferIx, mInputBuffers.size()); |
| 677 | |
| 678 | if (mMediaBuffers[index] != NULL) { |
| 679 | mMediaBuffers[index]->release(); |
| 680 | mMediaBuffers.editItemAt(index) = NULL; |
| 681 | } |
| 682 | mInputBufferIsDequeued.editItemAt(index) = true; |
| 683 | |
| 684 | if (!mCSDsToSubmit.isEmpty()) { |
| 685 | sp<AMessage> msg = new AMessage(); |
| 686 | msg->setSize("buffer-ix", index); |
| 687 | |
| 688 | sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0); |
| 689 | ALOGI("[%s] resubmitting CSD", mComponentName.c_str()); |
| 690 | msg->setBuffer("buffer", buffer); |
| 691 | mCSDsToSubmit.removeAt(0); |
| 692 | if (!onInputBufferFetched(msg)) { |
| 693 | handleError(UNKNOWN_ERROR); |
| 694 | return false; |
| 695 | } |
| 696 | return true; |
| 697 | } |
| 698 | |
| 699 | while (!mPendingInputMessages.empty()) { |
| 700 | sp<AMessage> msg = *mPendingInputMessages.begin(); |
| 701 | if (!onInputBufferFetched(msg)) { |
| 702 | break; |
| 703 | } |
| 704 | mPendingInputMessages.erase(mPendingInputMessages.begin()); |
| 705 | } |
| 706 | |
| 707 | if (!mInputBufferIsDequeued.editItemAt(index)) { |
| 708 | return true; |
| 709 | } |
| 710 | |
| 711 | mDequeuedInputBuffers.push_back(index); |
| 712 | |
| 713 | onRequestInputBuffers(); |
| 714 | return true; |
| 715 | } |
| 716 | |
| 717 | bool NuPlayer2::Decoder::handleAnOutputBuffer( |
| 718 | size_t index, |
| 719 | size_t offset, |
| 720 | size_t size, |
| 721 | int64_t timeUs, |
| 722 | int32_t flags) { |
| 723 | if (mCodec == NULL) { |
| 724 | ALOGE("[%s] handleAnOutputBuffer without a valid codec", mComponentName.c_str()); |
| 725 | handleError(NO_INIT); |
| 726 | return false; |
| 727 | } |
| 728 | |
| 729 | // CHECK_LT(bufferIx, mOutputBuffers.size()); |
| 730 | |
| 731 | size_t bufferSize = 0; |
| 732 | uint8_t *bufferBase = mCodec->getOutputBuffer(index, &bufferSize); |
| 733 | |
| 734 | if (bufferBase == NULL) { |
| 735 | ALOGE("[%s] handleAnOutputBuffer, failed to get output buffer", mComponentName.c_str()); |
| 736 | handleError(UNKNOWN_ERROR); |
| 737 | return false; |
| 738 | } |
| 739 | |
| 740 | sp<MediaCodecBuffer> buffer = |
| 741 | new MediaCodecBuffer(NULL /* format */, new ABuffer(bufferBase, bufferSize)); |
| 742 | |
| 743 | if (index >= mOutputBuffers.size()) { |
| 744 | for (size_t i = mOutputBuffers.size(); i <= index; ++i) { |
| 745 | mOutputBuffers.add(); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | mOutputBuffers.editItemAt(index) = buffer; |
| 750 | |
| 751 | buffer->setRange(offset, size); |
| 752 | buffer->meta()->clear(); |
| 753 | buffer->meta()->setInt64("timeUs", timeUs); |
| 754 | |
| 755 | bool eos = flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM; |
| 756 | // we do not expect CODECCONFIG or SYNCFRAME for decoder |
| 757 | |
| 758 | sp<AMessage> reply = new AMessage(kWhatRenderBuffer, this); |
| 759 | reply->setSize("buffer-ix", index); |
| 760 | reply->setInt32("generation", mBufferGeneration); |
| 761 | |
| 762 | if (eos) { |
| 763 | ALOGI("[%s] saw output EOS", mIsAudio ? "audio" : "video"); |
| 764 | |
| 765 | buffer->meta()->setInt32("eos", true); |
| 766 | reply->setInt32("eos", true); |
| 767 | } |
| 768 | |
| 769 | if (mSkipRenderingUntilMediaTimeUs >= 0) { |
| 770 | if (timeUs < mSkipRenderingUntilMediaTimeUs) { |
| 771 | ALOGV("[%s] dropping buffer at time %lld as requested.", |
| 772 | mComponentName.c_str(), (long long)timeUs); |
| 773 | |
| 774 | reply->post(); |
| 775 | if (eos) { |
| 776 | notifyResumeCompleteIfNecessary(); |
| 777 | if (mRenderer != NULL && !isDiscontinuityPending()) { |
| 778 | mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM); |
| 779 | } |
| 780 | } |
| 781 | return true; |
| 782 | } |
| 783 | |
| 784 | mSkipRenderingUntilMediaTimeUs = -1; |
| 785 | } |
| 786 | |
| 787 | mNumFramesTotal += !mIsAudio; |
| 788 | |
| 789 | // wait until 1st frame comes out to signal resume complete |
| 790 | notifyResumeCompleteIfNecessary(); |
| 791 | |
| 792 | if (mRenderer != NULL) { |
| 793 | // send the buffer to renderer. |
| 794 | mRenderer->queueBuffer(mIsAudio, buffer, reply); |
| 795 | if (eos && !isDiscontinuityPending()) { |
| 796 | mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | return true; |
| 801 | } |
| 802 | |
| 803 | void NuPlayer2::Decoder::handleOutputFormatChange(const sp<AMessage> &format) { |
| 804 | if (!mIsAudio) { |
| 805 | int32_t width, height; |
| 806 | if (format->findInt32("width", &width) |
| 807 | && format->findInt32("height", &height)) { |
| 808 | mStats->setInt32("width", width); |
| 809 | mStats->setInt32("height", height); |
| 810 | } |
| 811 | sp<AMessage> notify = mNotify->dup(); |
| 812 | notify->setInt32("what", kWhatVideoSizeChanged); |
| 813 | notify->setMessage("format", format); |
| 814 | notify->post(); |
| 815 | } else if (mRenderer != NULL) { |
| 816 | uint32_t flags; |
| 817 | int64_t durationUs; |
| 818 | bool hasVideo = (mSource->getFormat(false /* audio */) != NULL); |
| 819 | if (getAudioDeepBufferSetting() // override regardless of source duration |
| 820 | || (mSource->getDuration(&durationUs) == OK |
| 821 | && durationUs > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US)) { |
| 822 | flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
| 823 | } else { |
| 824 | flags = AUDIO_OUTPUT_FLAG_NONE; |
| 825 | } |
| 826 | |
| 827 | sp<AMessage> reply = new AMessage(kWhatAudioOutputFormatChanged, this); |
| 828 | reply->setInt32("generation", mBufferGeneration); |
| 829 | mRenderer->changeAudioFormat( |
| 830 | format, false /* offloadOnly */, hasVideo, |
| 831 | flags, mSource->isStreaming(), reply); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | void NuPlayer2::Decoder::releaseAndResetMediaBuffers() { |
| 836 | for (size_t i = 0; i < mMediaBuffers.size(); i++) { |
| 837 | if (mMediaBuffers[i] != NULL) { |
| 838 | mMediaBuffers[i]->release(); |
| 839 | mMediaBuffers.editItemAt(i) = NULL; |
| 840 | } |
| 841 | } |
| 842 | mMediaBuffers.resize(mInputBuffers.size()); |
| 843 | for (size_t i = 0; i < mMediaBuffers.size(); i++) { |
| 844 | mMediaBuffers.editItemAt(i) = NULL; |
| 845 | } |
| 846 | mInputBufferIsDequeued.clear(); |
| 847 | mInputBufferIsDequeued.resize(mInputBuffers.size()); |
| 848 | for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) { |
| 849 | mInputBufferIsDequeued.editItemAt(i) = false; |
| 850 | } |
| 851 | |
| 852 | mPendingInputMessages.clear(); |
| 853 | mDequeuedInputBuffers.clear(); |
| 854 | mSkipRenderingUntilMediaTimeUs = -1; |
| 855 | } |
| 856 | |
| 857 | bool NuPlayer2::Decoder::isStaleReply(const sp<AMessage> &msg) { |
| 858 | int32_t generation; |
| 859 | CHECK(msg->findInt32("generation", &generation)); |
| 860 | return generation != mBufferGeneration; |
| 861 | } |
| 862 | |
| 863 | status_t NuPlayer2::Decoder::fetchInputData(sp<AMessage> &reply) { |
| 864 | sp<ABuffer> accessUnit; |
| 865 | bool dropAccessUnit = true; |
| 866 | do { |
| 867 | status_t err = mSource->dequeueAccessUnit(mIsAudio, &accessUnit); |
| 868 | |
| 869 | if (err == -EWOULDBLOCK) { |
| 870 | return err; |
| 871 | } else if (err != OK) { |
| 872 | if (err == INFO_DISCONTINUITY) { |
| 873 | int32_t type; |
| 874 | CHECK(accessUnit->meta()->findInt32("discontinuity", &type)); |
| 875 | |
| 876 | bool formatChange = |
| 877 | (mIsAudio && |
| 878 | (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT)) |
| 879 | || (!mIsAudio && |
| 880 | (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT)); |
| 881 | |
| 882 | bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0; |
| 883 | |
| 884 | ALOGI("%s discontinuity (format=%d, time=%d)", |
| 885 | mIsAudio ? "audio" : "video", formatChange, timeChange); |
| 886 | |
| 887 | bool seamlessFormatChange = false; |
| 888 | sp<AMessage> newFormat = mSource->getFormat(mIsAudio); |
| 889 | if (formatChange) { |
| 890 | seamlessFormatChange = |
| 891 | supportsSeamlessFormatChange(newFormat); |
| 892 | // treat seamless format change separately |
| 893 | formatChange = !seamlessFormatChange; |
| 894 | } |
| 895 | |
| 896 | // For format or time change, return EOS to queue EOS input, |
| 897 | // then wait for EOS on output. |
| 898 | if (formatChange /* not seamless */) { |
| 899 | mFormatChangePending = true; |
| 900 | err = ERROR_END_OF_STREAM; |
| 901 | } else if (timeChange) { |
| 902 | rememberCodecSpecificData(newFormat); |
| 903 | mTimeChangePending = true; |
| 904 | err = ERROR_END_OF_STREAM; |
| 905 | } else if (seamlessFormatChange) { |
| 906 | // reuse existing decoder and don't flush |
| 907 | rememberCodecSpecificData(newFormat); |
| 908 | continue; |
| 909 | } else { |
| 910 | // This stream is unaffected by the discontinuity |
| 911 | return -EWOULDBLOCK; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | // reply should only be returned without a buffer set |
| 916 | // when there is an error (including EOS) |
| 917 | CHECK(err != OK); |
| 918 | |
| 919 | reply->setInt32("err", err); |
| 920 | return ERROR_END_OF_STREAM; |
| 921 | } |
| 922 | |
| 923 | dropAccessUnit = false; |
| 924 | if (!mIsAudio && !mIsEncrypted) { |
| 925 | // Extra safeguard if higher-level behavior changes. Otherwise, not required now. |
| 926 | // Preventing the buffer from being processed (and sent to codec) if this is a later |
| 927 | // round of playback but this time without prepareDrm. Or if there is a race between |
| 928 | // stop (which is not blocking) and releaseDrm allowing buffers being processed after |
| 929 | // Crypto has been released (GenericSource currently prevents this race though). |
| 930 | // Particularly doing this check before IsAVCReferenceFrame call to prevent parsing |
| 931 | // of encrypted data. |
| 932 | if (mIsEncryptedObservedEarlier) { |
| 933 | ALOGE("fetchInputData: mismatched mIsEncrypted/mIsEncryptedObservedEarlier (0/1)"); |
| 934 | |
| 935 | return INVALID_OPERATION; |
| 936 | } |
| 937 | |
| 938 | int32_t layerId = 0; |
| 939 | bool haveLayerId = accessUnit->meta()->findInt32("temporal-layer-id", &layerId); |
| 940 | if (mRenderer->getVideoLateByUs() > 100000ll |
| 941 | && mIsVideoAVC |
| 942 | && !IsAVCReferenceFrame(accessUnit)) { |
| 943 | dropAccessUnit = true; |
| 944 | } else if (haveLayerId && mNumVideoTemporalLayerTotal > 1) { |
| 945 | // Add only one layer each time. |
| 946 | if (layerId > mCurrentMaxVideoTemporalLayerId + 1 |
| 947 | || layerId >= mNumVideoTemporalLayerAllowed) { |
| 948 | dropAccessUnit = true; |
| 949 | ALOGV("dropping layer(%d), speed=%g, allowed layer count=%d, max layerId=%d", |
| 950 | layerId, mPlaybackSpeed, mNumVideoTemporalLayerAllowed, |
| 951 | mCurrentMaxVideoTemporalLayerId); |
| 952 | } else if (layerId > mCurrentMaxVideoTemporalLayerId) { |
| 953 | mCurrentMaxVideoTemporalLayerId = layerId; |
| 954 | } else if (layerId == 0 && mNumVideoTemporalLayerTotal > 1 |
| 955 | && IsIDR(accessUnit->data(), accessUnit->size())) { |
| 956 | mCurrentMaxVideoTemporalLayerId = mNumVideoTemporalLayerTotal - 1; |
| 957 | } |
| 958 | } |
| 959 | if (dropAccessUnit) { |
| 960 | if (layerId <= mCurrentMaxVideoTemporalLayerId && layerId > 0) { |
| 961 | mCurrentMaxVideoTemporalLayerId = layerId - 1; |
| 962 | } |
| 963 | ++mNumInputFramesDropped; |
| 964 | } |
| 965 | } |
| 966 | } while (dropAccessUnit); |
| 967 | |
| 968 | // ALOGV("returned a valid buffer of %s data", mIsAudio ? "mIsAudio" : "video"); |
| 969 | #if 0 |
| 970 | int64_t mediaTimeUs; |
| 971 | CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs)); |
| 972 | ALOGV("[%s] feeding input buffer at media time %.3f", |
| 973 | mIsAudio ? "audio" : "video", |
| 974 | mediaTimeUs / 1E6); |
| 975 | #endif |
| 976 | |
| 977 | if (mCCDecoder != NULL) { |
| 978 | mCCDecoder->decode(accessUnit); |
| 979 | } |
| 980 | |
| 981 | reply->setBuffer("buffer", accessUnit); |
| 982 | |
| 983 | return OK; |
| 984 | } |
| 985 | |
| 986 | bool NuPlayer2::Decoder::onInputBufferFetched(const sp<AMessage> &msg) { |
| 987 | if (mCodec == NULL) { |
| 988 | ALOGE("[%s] onInputBufferFetched without a valid codec", mComponentName.c_str()); |
| 989 | handleError(NO_INIT); |
| 990 | return false; |
| 991 | } |
| 992 | |
| 993 | size_t bufferIx; |
| 994 | CHECK(msg->findSize("buffer-ix", &bufferIx)); |
| 995 | CHECK_LT(bufferIx, mInputBuffers.size()); |
| 996 | sp<MediaCodecBuffer> codecBuffer = mInputBuffers[bufferIx]; |
| 997 | |
| 998 | sp<ABuffer> buffer; |
| 999 | bool hasBuffer = msg->findBuffer("buffer", &buffer); |
| 1000 | bool needsCopy = true; |
| 1001 | |
| 1002 | if (buffer == NULL /* includes !hasBuffer */) { |
| 1003 | int32_t streamErr = ERROR_END_OF_STREAM; |
| 1004 | CHECK(msg->findInt32("err", &streamErr) || !hasBuffer); |
| 1005 | |
| 1006 | CHECK(streamErr != OK); |
| 1007 | |
| 1008 | // attempt to queue EOS |
| 1009 | status_t err = mCodec->queueInputBuffer( |
| 1010 | bufferIx, |
| 1011 | 0, |
| 1012 | 0, |
| 1013 | 0, |
| 1014 | AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM); |
| 1015 | if (err == OK) { |
| 1016 | mInputBufferIsDequeued.editItemAt(bufferIx) = false; |
| 1017 | } else if (streamErr == ERROR_END_OF_STREAM) { |
| 1018 | streamErr = err; |
| 1019 | // err will not be ERROR_END_OF_STREAM |
| 1020 | } |
| 1021 | |
| 1022 | if (streamErr != ERROR_END_OF_STREAM) { |
| 1023 | ALOGE("Stream error for [%s] (err=%d), EOS %s queued", |
| 1024 | mComponentName.c_str(), |
| 1025 | streamErr, |
| 1026 | err == OK ? "successfully" : "unsuccessfully"); |
| 1027 | handleError(streamErr); |
| 1028 | } |
| 1029 | } else { |
| 1030 | sp<AMessage> extra; |
| 1031 | if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) { |
| 1032 | int64_t resumeAtMediaTimeUs; |
| 1033 | if (extra->findInt64( |
| 1034 | "resume-at-mediaTimeUs", &resumeAtMediaTimeUs)) { |
| 1035 | ALOGI("[%s] suppressing rendering until %lld us", |
| 1036 | mComponentName.c_str(), (long long)resumeAtMediaTimeUs); |
| 1037 | mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs; |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | int64_t timeUs = 0; |
| 1042 | uint32_t flags = 0; |
| 1043 | CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); |
| 1044 | |
| 1045 | int32_t eos, csd; |
| 1046 | // we do not expect SYNCFRAME for decoder |
| 1047 | if (buffer->meta()->findInt32("eos", &eos) && eos) { |
| 1048 | flags |= AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM; |
| 1049 | } else if (buffer->meta()->findInt32("csd", &csd) && csd) { |
| 1050 | flags |= AMEDIACODEC_BUFFER_FLAG_CODEC_CONFIG; |
| 1051 | } |
| 1052 | |
| 1053 | // Modular DRM |
| 1054 | MediaBuffer *mediaBuf = NULL; |
| 1055 | sp<AMediaCodecCryptoInfoWrapper> cryptInfo; |
| 1056 | |
| 1057 | // copy into codec buffer |
| 1058 | if (needsCopy) { |
| 1059 | if (buffer->size() > codecBuffer->capacity()) { |
| 1060 | handleError(ERROR_BUFFER_TOO_SMALL); |
| 1061 | mDequeuedInputBuffers.push_back(bufferIx); |
| 1062 | return false; |
| 1063 | } |
| 1064 | |
| 1065 | if (buffer->data() != NULL) { |
| 1066 | codecBuffer->setRange(0, buffer->size()); |
| 1067 | memcpy(codecBuffer->data(), buffer->data(), buffer->size()); |
| 1068 | } else { // No buffer->data() |
| 1069 | //Modular DRM |
| 1070 | mediaBuf = (MediaBuffer*)buffer->getMediaBufferBase(); |
| 1071 | if (mediaBuf != NULL) { |
| 1072 | codecBuffer->setRange(0, mediaBuf->size()); |
| 1073 | memcpy(codecBuffer->data(), mediaBuf->data(), mediaBuf->size()); |
| 1074 | |
| 1075 | sp<MetaData> meta_data = mediaBuf->meta_data(); |
| 1076 | cryptInfo = AMediaCodecCryptoInfoWrapper::Create(meta_data); |
| 1077 | |
| 1078 | // since getMediaBuffer() has incremented the refCount |
| 1079 | mediaBuf->release(); |
| 1080 | } else { // No mediaBuf |
| 1081 | ALOGE("onInputBufferFetched: buffer->data()/mediaBuf are NULL for %p", |
| 1082 | buffer.get()); |
| 1083 | handleError(UNKNOWN_ERROR); |
| 1084 | return false; |
| 1085 | } |
| 1086 | } // buffer->data() |
| 1087 | } // needsCopy |
| 1088 | |
| 1089 | status_t err; |
| 1090 | if (cryptInfo != NULL) { |
| 1091 | err = mCodec->queueSecureInputBuffer( |
| 1092 | bufferIx, |
| 1093 | codecBuffer->offset(), |
| 1094 | cryptInfo, |
| 1095 | timeUs, |
| 1096 | flags); |
| 1097 | // synchronous call so done with cryptInfo here |
| 1098 | } else { |
| 1099 | err = mCodec->queueInputBuffer( |
| 1100 | bufferIx, |
| 1101 | codecBuffer->offset(), |
| 1102 | codecBuffer->size(), |
| 1103 | timeUs, |
| 1104 | flags); |
| 1105 | } // no cryptInfo |
| 1106 | |
| 1107 | if (err != OK) { |
| 1108 | ALOGE("onInputBufferFetched: queue%sInputBuffer failed for [%s] (err=%d)", |
| 1109 | (cryptInfo != NULL ? "Secure" : ""), |
| 1110 | mComponentName.c_str(), err); |
| 1111 | handleError(err); |
| 1112 | } else { |
| 1113 | mInputBufferIsDequeued.editItemAt(bufferIx) = false; |
| 1114 | } |
| 1115 | |
| 1116 | } // buffer != NULL |
| 1117 | return true; |
| 1118 | } |
| 1119 | |
| 1120 | void NuPlayer2::Decoder::onRenderBuffer(const sp<AMessage> &msg) { |
| 1121 | status_t err; |
| 1122 | int32_t render; |
| 1123 | size_t bufferIx; |
| 1124 | int32_t eos; |
| 1125 | CHECK(msg->findSize("buffer-ix", &bufferIx)); |
| 1126 | |
| 1127 | if (!mIsAudio) { |
| 1128 | int64_t timeUs; |
| 1129 | sp<MediaCodecBuffer> buffer = mOutputBuffers[bufferIx]; |
| 1130 | buffer->meta()->findInt64("timeUs", &timeUs); |
| 1131 | |
| 1132 | if (mCCDecoder != NULL && mCCDecoder->isSelected()) { |
| 1133 | mCCDecoder->display(timeUs); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | if (mCodec == NULL) { |
| 1138 | err = NO_INIT; |
| 1139 | } else if (msg->findInt32("render", &render) && render) { |
| 1140 | int64_t timestampNs; |
| 1141 | CHECK(msg->findInt64("timestampNs", ×tampNs)); |
| 1142 | err = mCodec->releaseOutputBufferAtTime(bufferIx, timestampNs); |
| 1143 | } else { |
| 1144 | mNumOutputFramesDropped += !mIsAudio; |
| 1145 | err = mCodec->releaseOutputBuffer(bufferIx, false /* render */); |
| 1146 | } |
| 1147 | if (err != OK) { |
| 1148 | ALOGE("failed to release output buffer for [%s] (err=%d)", |
| 1149 | mComponentName.c_str(), err); |
| 1150 | handleError(err); |
| 1151 | } |
| 1152 | if (msg->findInt32("eos", &eos) && eos |
| 1153 | && isDiscontinuityPending()) { |
| 1154 | finishHandleDiscontinuity(true /* flushOnTimeChange */); |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | bool NuPlayer2::Decoder::isDiscontinuityPending() const { |
| 1159 | return mFormatChangePending || mTimeChangePending; |
| 1160 | } |
| 1161 | |
| 1162 | void NuPlayer2::Decoder::finishHandleDiscontinuity(bool flushOnTimeChange) { |
| 1163 | ALOGV("finishHandleDiscontinuity: format %d, time %d, flush %d", |
| 1164 | mFormatChangePending, mTimeChangePending, flushOnTimeChange); |
| 1165 | |
| 1166 | // If we have format change, pause and wait to be killed; |
| 1167 | // If we have time change only, flush and restart fetching. |
| 1168 | |
| 1169 | if (mFormatChangePending) { |
| 1170 | mPaused = true; |
| 1171 | } else if (mTimeChangePending) { |
| 1172 | if (flushOnTimeChange) { |
| 1173 | doFlush(false /* notifyComplete */); |
| 1174 | signalResume(false /* notifyComplete */); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | // Notify NuPlayer2 to either shutdown decoder, or rescan sources |
| 1179 | sp<AMessage> msg = mNotify->dup(); |
| 1180 | msg->setInt32("what", kWhatInputDiscontinuity); |
| 1181 | msg->setInt32("formatChange", mFormatChangePending); |
| 1182 | msg->post(); |
| 1183 | |
| 1184 | mFormatChangePending = false; |
| 1185 | mTimeChangePending = false; |
| 1186 | } |
| 1187 | |
| 1188 | bool NuPlayer2::Decoder::supportsSeamlessAudioFormatChange( |
| 1189 | const sp<AMessage> &targetFormat) const { |
| 1190 | if (targetFormat == NULL) { |
| 1191 | return true; |
| 1192 | } |
| 1193 | |
| 1194 | AString mime; |
| 1195 | if (!targetFormat->findString("mime", &mime)) { |
| 1196 | return false; |
| 1197 | } |
| 1198 | |
| 1199 | if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) { |
| 1200 | // field-by-field comparison |
| 1201 | const char * keys[] = { "channel-count", "sample-rate", "is-adts" }; |
| 1202 | for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) { |
| 1203 | int32_t oldVal, newVal; |
| 1204 | if (!mInputFormat->getInt32(keys[i], &oldVal) || |
| 1205 | !targetFormat->findInt32(keys[i], &newVal) || |
| 1206 | oldVal != newVal) { |
| 1207 | return false; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | sp<ABuffer> newBuf; |
| 1212 | uint8_t *oldBufData = NULL; |
| 1213 | size_t oldBufSize = 0; |
| 1214 | if (mInputFormat->getBuffer("csd-0", (void**)&oldBufData, &oldBufSize) && |
| 1215 | targetFormat->findBuffer("csd-0", &newBuf)) { |
| 1216 | if (oldBufSize != newBuf->size()) { |
| 1217 | return false; |
| 1218 | } |
| 1219 | return !memcmp(oldBufData, newBuf->data(), oldBufSize); |
| 1220 | } |
| 1221 | } |
| 1222 | return false; |
| 1223 | } |
| 1224 | |
| 1225 | bool NuPlayer2::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const { |
| 1226 | if (mInputFormat == NULL) { |
| 1227 | return false; |
| 1228 | } |
| 1229 | |
| 1230 | if (targetFormat == NULL) { |
| 1231 | return true; |
| 1232 | } |
| 1233 | |
| 1234 | AString oldMime, newMime; |
| 1235 | if (!mInputFormat->getString("mime", &oldMime) |
| 1236 | || !targetFormat->findString("mime", &newMime) |
| 1237 | || !(oldMime == newMime)) { |
| 1238 | return false; |
| 1239 | } |
| 1240 | |
| 1241 | bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/")); |
| 1242 | bool seamless; |
| 1243 | if (audio) { |
| 1244 | seamless = supportsSeamlessAudioFormatChange(targetFormat); |
| 1245 | } else { |
| 1246 | int32_t isAdaptive; |
| 1247 | seamless = (mCodec != NULL && |
| 1248 | mInputFormat->getInt32("adaptive-playback", &isAdaptive) && |
| 1249 | isAdaptive); |
| 1250 | } |
| 1251 | |
| 1252 | ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str()); |
| 1253 | return seamless; |
| 1254 | } |
| 1255 | |
| 1256 | void NuPlayer2::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) { |
| 1257 | if (format == NULL) { |
| 1258 | return; |
| 1259 | } |
| 1260 | mCSDsForCurrentFormat.clear(); |
| 1261 | for (int32_t i = 0; ; ++i) { |
| 1262 | AString tag = "csd-"; |
| 1263 | tag.append(i); |
| 1264 | sp<ABuffer> buffer; |
| 1265 | if (!format->findBuffer(tag.c_str(), &buffer)) { |
| 1266 | break; |
| 1267 | } |
| 1268 | mCSDsForCurrentFormat.push(buffer); |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | void NuPlayer2::Decoder::notifyResumeCompleteIfNecessary() { |
| 1273 | if (mResumePending) { |
| 1274 | mResumePending = false; |
| 1275 | |
| 1276 | sp<AMessage> notify = mNotify->dup(); |
| 1277 | notify->setInt32("what", kWhatResumeCompleted); |
| 1278 | notify->post(); |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | } // namespace android |
| 1283 | |