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