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