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 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 22 | #include "NuPlayerCCDecoder.h" |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 23 | #include "NuPlayerDecoder.h" |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 24 | #include "NuPlayerRenderer.h" |
| 25 | #include "NuPlayerSource.h" |
| 26 | |
Andy Hung | 288da02 | 2015-05-31 22:55:59 -0700 | [diff] [blame] | 27 | #include <cutils/properties.h> |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 28 | #include <media/ICrypto.h> |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 29 | #include <media/stagefright/foundation/ABuffer.h> |
| 30 | #include <media/stagefright/foundation/ADebug.h> |
Andreas Huber | 5bc087c | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 31 | #include <media/stagefright/foundation/AMessage.h> |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 32 | #include <media/stagefright/MediaBuffer.h> |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 33 | #include <media/stagefright/MediaCodec.h> |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 34 | #include <media/stagefright/MediaDefs.h> |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 35 | #include <media/stagefright/MediaErrors.h> |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 36 | |
Lajos Molnar | 1de1e25 | 2015-04-30 18:18:34 -0700 | [diff] [blame] | 37 | #include <gui/Surface.h> |
| 38 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 39 | #include "avc_utils.h" |
| 40 | #include "ATSParser.h" |
| 41 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 42 | namespace android { |
| 43 | |
Andy Hung | 288da02 | 2015-05-31 22:55:59 -0700 | [diff] [blame] | 44 | static inline bool getAudioDeepBufferSetting() { |
| 45 | return property_get_bool("media.stagefright.audio.deep", false /* default_value */); |
| 46 | } |
| 47 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 48 | NuPlayer::Decoder::Decoder( |
Glenn Kasten | 1173118 | 2011-02-08 17:26:17 -0800 | [diff] [blame] | 49 | const sp<AMessage> ¬ify, |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 50 | const sp<Source> &source, |
Ronghua Wu | 68845c1 | 2015-07-21 09:50:48 -0700 | [diff] [blame] | 51 | pid_t pid, |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 52 | const sp<Renderer> &renderer, |
Lajos Molnar | 1de1e25 | 2015-04-30 18:18:34 -0700 | [diff] [blame] | 53 | const sp<Surface> &surface, |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 54 | const sp<CCDecoder> &ccDecoder) |
Andy Hung | 202bce1 | 2014-12-03 11:47:36 -0800 | [diff] [blame] | 55 | : DecoderBase(notify), |
Lajos Molnar | 1de1e25 | 2015-04-30 18:18:34 -0700 | [diff] [blame] | 56 | mSurface(surface), |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 57 | mSource(source), |
| 58 | mRenderer(renderer), |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 59 | mCCDecoder(ccDecoder), |
Ronghua Wu | 68845c1 | 2015-07-21 09:50:48 -0700 | [diff] [blame] | 60 | mPid(pid), |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 61 | mSkipRenderingUntilMediaTimeUs(-1ll), |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 62 | mNumFramesTotal(0ll), |
Praveen Chavan | e1e5d7a | 2015-05-19 19:09:48 -0700 | [diff] [blame] | 63 | mNumInputFramesDropped(0ll), |
| 64 | mNumOutputFramesDropped(0ll), |
| 65 | mVideoWidth(0), |
| 66 | mVideoHeight(0), |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 67 | mIsAudio(true), |
| 68 | mIsVideoAVC(false), |
| 69 | mIsSecure(false), |
| 70 | mFormatChangePending(false), |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 71 | mTimeChangePending(false), |
Chong Zhang | f8d7177 | 2014-11-26 15:08:34 -0800 | [diff] [blame] | 72 | mResumePending(false), |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 73 | mComponentName("decoder") { |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 74 | mCodecLooper = new ALooper; |
Marco Nelissen | 9e2b791 | 2014-08-18 16:13:03 -0700 | [diff] [blame] | 75 | mCodecLooper->setName("NPDecoder-CL"); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 76 | mCodecLooper->start(false, false, ANDROID_PRIORITY_AUDIO); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | NuPlayer::Decoder::~Decoder() { |
Ronghua Wu | faeb0f2 | 2015-05-21 12:20:21 -0700 | [diff] [blame] | 80 | mCodec->release(); |
Wei Jia | 4923cee | 2014-09-24 14:25:19 -0700 | [diff] [blame] | 81 | releaseAndResetMediaBuffers(); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Praveen Chavan | e1e5d7a | 2015-05-19 19:09:48 -0700 | [diff] [blame] | 84 | sp<AMessage> NuPlayer::Decoder::getStats() const { |
| 85 | mStats->setInt64("frames-total", mNumFramesTotal); |
| 86 | mStats->setInt64("frames-dropped-input", mNumInputFramesDropped); |
| 87 | mStats->setInt64("frames-dropped-output", mNumOutputFramesDropped); |
| 88 | return mStats; |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Lajos Molnar | a81c622 | 2015-07-10 19:17:45 -0700 | [diff] [blame] | 91 | status_t NuPlayer::Decoder::setVideoSurface(const sp<Surface> &surface) { |
| 92 | if (surface == NULL || ADebug::isExperimentEnabled("legacy-setsurface")) { |
| 93 | return BAD_VALUE; |
| 94 | } |
| 95 | |
| 96 | sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this); |
| 97 | |
| 98 | msg->setObject("surface", surface); |
| 99 | sp<AMessage> response; |
| 100 | status_t err = msg->postAndAwaitResponse(&response); |
| 101 | if (err == OK && response != NULL) { |
| 102 | CHECK(response->findInt32("err", &err)); |
| 103 | } |
| 104 | return err; |
| 105 | } |
| 106 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 107 | void NuPlayer::Decoder::onMessageReceived(const sp<AMessage> &msg) { |
| 108 | ALOGV("[%s] onMessage: %s", mComponentName.c_str(), msg->debugString().c_str()); |
| 109 | |
| 110 | switch (msg->what()) { |
| 111 | case kWhatCodecNotify: |
| 112 | { |
Chong Zhang | 3b032b3 | 2015-04-17 15:49:06 -0700 | [diff] [blame] | 113 | int32_t cbID; |
| 114 | CHECK(msg->findInt32("callbackID", &cbID)); |
| 115 | |
| 116 | ALOGV("[%s] kWhatCodecNotify: cbID = %d, paused = %d", |
| 117 | mIsAudio ? "audio" : "video", cbID, mPaused); |
| 118 | |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 119 | if (mPaused) { |
| 120 | break; |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 123 | switch (cbID) { |
| 124 | case MediaCodec::CB_INPUT_AVAILABLE: |
| 125 | { |
| 126 | int32_t index; |
| 127 | CHECK(msg->findInt32("index", &index)); |
| 128 | |
| 129 | handleAnInputBuffer(index); |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | case MediaCodec::CB_OUTPUT_AVAILABLE: |
| 134 | { |
| 135 | int32_t index; |
| 136 | size_t offset; |
| 137 | size_t size; |
| 138 | int64_t timeUs; |
| 139 | int32_t flags; |
| 140 | |
| 141 | CHECK(msg->findInt32("index", &index)); |
| 142 | CHECK(msg->findSize("offset", &offset)); |
| 143 | CHECK(msg->findSize("size", &size)); |
| 144 | CHECK(msg->findInt64("timeUs", &timeUs)); |
| 145 | CHECK(msg->findInt32("flags", &flags)); |
| 146 | |
| 147 | handleAnOutputBuffer(index, offset, size, timeUs, flags); |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | case MediaCodec::CB_OUTPUT_FORMAT_CHANGED: |
| 152 | { |
| 153 | sp<AMessage> format; |
| 154 | CHECK(msg->findMessage("format", &format)); |
| 155 | |
| 156 | handleOutputFormatChange(format); |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | case MediaCodec::CB_ERROR: |
| 161 | { |
| 162 | status_t err; |
| 163 | CHECK(msg->findInt32("err", &err)); |
| 164 | ALOGE("Decoder (%s) reported error : 0x%x", |
| 165 | mIsAudio ? "audio" : "video", err); |
| 166 | |
| 167 | handleError(err); |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | default: |
| 172 | { |
| 173 | TRESPASS(); |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 178 | break; |
| 179 | } |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 180 | |
| 181 | case kWhatRenderBuffer: |
| 182 | { |
| 183 | if (!isStaleReply(msg)) { |
| 184 | onRenderBuffer(msg); |
| 185 | } |
| 186 | break; |
| 187 | } |
| 188 | |
Lajos Molnar | a81c622 | 2015-07-10 19:17:45 -0700 | [diff] [blame] | 189 | case kWhatSetVideoSurface: |
| 190 | { |
| 191 | sp<AReplyToken> replyID; |
| 192 | CHECK(msg->senderAwaitsResponse(&replyID)); |
| 193 | |
| 194 | sp<RefBase> obj; |
| 195 | CHECK(msg->findObject("surface", &obj)); |
| 196 | sp<Surface> surface = static_cast<Surface *>(obj.get()); // non-null |
| 197 | int32_t err = INVALID_OPERATION; |
| 198 | // NOTE: in practice mSurface is always non-null, but checking here for completeness |
| 199 | if (mCodec != NULL && mSurface != NULL) { |
| 200 | // TODO: once AwesomePlayer is removed, remove this automatic connecting |
| 201 | // to the surface by MediaPlayerService. |
| 202 | // |
| 203 | // at this point MediaPlayerService::client has already connected to the |
| 204 | // surface, which MediaCodec does not expect |
| 205 | err = native_window_api_disconnect(surface.get(), NATIVE_WINDOW_API_MEDIA); |
| 206 | if (err == OK) { |
| 207 | err = mCodec->setSurface(surface); |
| 208 | ALOGI_IF(err, "codec setSurface returned: %d", err); |
| 209 | if (err == OK) { |
| 210 | // reconnect to the old surface as MPS::Client will expect to |
| 211 | // be able to disconnect from it. |
| 212 | (void)native_window_api_connect(mSurface.get(), NATIVE_WINDOW_API_MEDIA); |
| 213 | mSurface = surface; |
| 214 | } |
| 215 | } |
| 216 | if (err != OK) { |
| 217 | // reconnect to the new surface on error as MPS::Client will expect to |
| 218 | // be able to disconnect from it. |
| 219 | (void)native_window_api_connect(surface.get(), NATIVE_WINDOW_API_MEDIA); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | sp<AMessage> response = new AMessage; |
| 224 | response->setInt32("err", err); |
| 225 | response->postReply(replyID); |
| 226 | break; |
| 227 | } |
| 228 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 229 | default: |
| 230 | DecoderBase::onMessageReceived(msg); |
| 231 | break; |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 235 | void NuPlayer::Decoder::onConfigure(const sp<AMessage> &format) { |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 236 | CHECK(mCodec == NULL); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 237 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 238 | mFormatChangePending = false; |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 239 | mTimeChangePending = false; |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 240 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 241 | ++mBufferGeneration; |
| 242 | |
Andreas Huber | 8406678 | 2011-08-16 09:34:26 -0700 | [diff] [blame] | 243 | AString mime; |
| 244 | CHECK(format->findString("mime", &mime)); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 245 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 246 | mIsAudio = !strncasecmp("audio/", mime.c_str(), 6); |
| 247 | mIsVideoAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str()); |
| 248 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 249 | mComponentName = mime; |
| 250 | mComponentName.append(" decoder"); |
Lajos Molnar | 1de1e25 | 2015-04-30 18:18:34 -0700 | [diff] [blame] | 251 | ALOGV("[%s] onConfigure (surface=%p)", mComponentName.c_str(), mSurface.get()); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 252 | |
Ronghua Wu | 68845c1 | 2015-07-21 09:50:48 -0700 | [diff] [blame] | 253 | mCodec = MediaCodec::CreateByType( |
| 254 | mCodecLooper, mime.c_str(), false /* encoder */, NULL /* err */, mPid); |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 255 | int32_t secure = 0; |
| 256 | if (format->findInt32("secure", &secure) && secure != 0) { |
| 257 | if (mCodec != NULL) { |
| 258 | mCodec->getName(&mComponentName); |
| 259 | mComponentName.append(".secure"); |
| 260 | mCodec->release(); |
| 261 | ALOGI("[%s] creating", mComponentName.c_str()); |
| 262 | mCodec = MediaCodec::CreateByComponentName( |
Ronghua Wu | 68845c1 | 2015-07-21 09:50:48 -0700 | [diff] [blame] | 263 | mCodecLooper, mComponentName.c_str(), NULL /* err */, mPid); |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 266 | if (mCodec == NULL) { |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 267 | ALOGE("Failed to create %s%s decoder", |
| 268 | (secure ? "secure " : ""), mime.c_str()); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 269 | handleError(UNKNOWN_ERROR); |
| 270 | return; |
| 271 | } |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 272 | mIsSecure = secure; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 273 | |
| 274 | mCodec->getName(&mComponentName); |
| 275 | |
Lajos Molnar | 14986f6 | 2014-09-15 11:04:44 -0700 | [diff] [blame] | 276 | status_t err; |
Lajos Molnar | 1de1e25 | 2015-04-30 18:18:34 -0700 | [diff] [blame] | 277 | if (mSurface != NULL) { |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 278 | // disconnect from surface as MediaCodec will reconnect |
Lajos Molnar | 14986f6 | 2014-09-15 11:04:44 -0700 | [diff] [blame] | 279 | err = native_window_api_disconnect( |
Lajos Molnar | 1de1e25 | 2015-04-30 18:18:34 -0700 | [diff] [blame] | 280 | mSurface.get(), NATIVE_WINDOW_API_MEDIA); |
Lajos Molnar | 14986f6 | 2014-09-15 11:04:44 -0700 | [diff] [blame] | 281 | // We treat this as a warning, as this is a preparatory step. |
| 282 | // Codec will try to connect to the surface, which is where |
| 283 | // any error signaling will occur. |
| 284 | ALOGW_IF(err != OK, "failed to disconnect from surface: %d", err); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 285 | } |
Lajos Molnar | 14986f6 | 2014-09-15 11:04:44 -0700 | [diff] [blame] | 286 | err = mCodec->configure( |
Lajos Molnar | 1de1e25 | 2015-04-30 18:18:34 -0700 | [diff] [blame] | 287 | format, mSurface, NULL /* crypto */, 0 /* flags */); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 288 | if (err != OK) { |
| 289 | ALOGE("Failed to configure %s decoder (err=%d)", mComponentName.c_str(), err); |
Andy Hung | 2abde2c | 2014-09-30 14:40:32 -0700 | [diff] [blame] | 290 | mCodec->release(); |
| 291 | mCodec.clear(); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 292 | handleError(err); |
| 293 | return; |
| 294 | } |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 295 | rememberCodecSpecificData(format); |
| 296 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 297 | // the following should work in configured state |
| 298 | CHECK_EQ((status_t)OK, mCodec->getOutputFormat(&mOutputFormat)); |
| 299 | CHECK_EQ((status_t)OK, mCodec->getInputFormat(&mInputFormat)); |
| 300 | |
Praveen Chavan | e1e5d7a | 2015-05-19 19:09:48 -0700 | [diff] [blame] | 301 | mStats->setString("mime", mime.c_str()); |
| 302 | mStats->setString("component-name", mComponentName.c_str()); |
| 303 | |
| 304 | if (!mIsAudio) { |
| 305 | int32_t width, height; |
| 306 | if (mOutputFormat->findInt32("width", &width) |
| 307 | && mOutputFormat->findInt32("height", &height)) { |
| 308 | mStats->setInt32("width", width); |
| 309 | mStats->setInt32("height", height); |
| 310 | } |
| 311 | } |
| 312 | |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 313 | sp<AMessage> reply = new AMessage(kWhatCodecNotify, this); |
| 314 | mCodec->setCallback(reply); |
| 315 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 316 | err = mCodec->start(); |
| 317 | if (err != OK) { |
| 318 | ALOGE("Failed to start %s decoder (err=%d)", mComponentName.c_str(), err); |
Andy Hung | 2abde2c | 2014-09-30 14:40:32 -0700 | [diff] [blame] | 319 | mCodec->release(); |
| 320 | mCodec.clear(); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 321 | handleError(err); |
| 322 | return; |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 325 | releaseAndResetMediaBuffers(); |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 326 | |
Wei Jia | 704e726 | 2014-06-04 16:21:56 -0700 | [diff] [blame] | 327 | mPaused = false; |
Chong Zhang | f8d7177 | 2014-11-26 15:08:34 -0800 | [diff] [blame] | 328 | mResumePending = false; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 329 | } |
Andreas Huber | 078cfcf | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 330 | |
Ronghua Wu | 8db8813 | 2015-04-22 13:51:35 -0700 | [diff] [blame] | 331 | void NuPlayer::Decoder::onSetParameters(const sp<AMessage> ¶ms) { |
| 332 | if (mCodec == NULL) { |
| 333 | ALOGW("onSetParameters called before codec is created."); |
| 334 | return; |
| 335 | } |
| 336 | mCodec->setParameters(params); |
| 337 | } |
| 338 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 339 | void NuPlayer::Decoder::onSetRenderer(const sp<Renderer> &renderer) { |
| 340 | bool hadNoRenderer = (mRenderer == NULL); |
| 341 | mRenderer = renderer; |
| 342 | if (hadNoRenderer && mRenderer != NULL) { |
Lajos Molnar | e6109e2 | 2015-04-10 17:18:22 -0700 | [diff] [blame] | 343 | // this means that the widevine legacy source is ready |
| 344 | onRequestInputBuffers(); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
| 348 | void NuPlayer::Decoder::onGetInputBuffers( |
| 349 | Vector<sp<ABuffer> > *dstBuffers) { |
Lajos Molnar | e6109e2 | 2015-04-10 17:18:22 -0700 | [diff] [blame] | 350 | CHECK_EQ((status_t)OK, mCodec->getWidevineLegacyBuffers(dstBuffers)); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Chong Zhang | f8d7177 | 2014-11-26 15:08:34 -0800 | [diff] [blame] | 353 | void NuPlayer::Decoder::onResume(bool notifyComplete) { |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 354 | mPaused = false; |
Chong Zhang | f8d7177 | 2014-11-26 15:08:34 -0800 | [diff] [blame] | 355 | |
| 356 | if (notifyComplete) { |
| 357 | mResumePending = true; |
| 358 | } |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 359 | mCodec->start(); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 362 | void NuPlayer::Decoder::doFlush(bool notifyComplete) { |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 363 | if (mCCDecoder != NULL) { |
| 364 | mCCDecoder->flush(); |
| 365 | } |
| 366 | |
| 367 | if (mRenderer != NULL) { |
| 368 | mRenderer->flush(mIsAudio, notifyComplete); |
| 369 | mRenderer->signalTimeDiscontinuity(); |
| 370 | } |
| 371 | |
| 372 | status_t err = OK; |
| 373 | if (mCodec != NULL) { |
| 374 | err = mCodec->flush(); |
| 375 | mCSDsToSubmit = mCSDsForCurrentFormat; // copy operator |
| 376 | ++mBufferGeneration; |
| 377 | } |
| 378 | |
| 379 | if (err != OK) { |
| 380 | ALOGE("failed to flush %s (err=%d)", mComponentName.c_str(), err); |
| 381 | handleError(err); |
| 382 | // finish with posting kWhatFlushCompleted. |
| 383 | // we attempt to release the buffers even if flush fails. |
| 384 | } |
| 385 | releaseAndResetMediaBuffers(); |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 386 | mPaused = true; |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 387 | } |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 388 | |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 389 | |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 390 | void NuPlayer::Decoder::onFlush() { |
| 391 | doFlush(true); |
| 392 | |
| 393 | if (isDiscontinuityPending()) { |
| 394 | // This could happen if the client starts seeking/shutdown |
| 395 | // after we queued an EOS for discontinuities. |
| 396 | // We can consider discontinuity handled. |
| 397 | finishHandleDiscontinuity(false /* flushOnTimeChange */); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 398 | } |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 399 | |
| 400 | sp<AMessage> notify = mNotify->dup(); |
| 401 | notify->setInt32("what", kWhatFlushCompleted); |
| 402 | notify->post(); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | void NuPlayer::Decoder::onShutdown(bool notifyComplete) { |
| 406 | status_t err = OK; |
Chong Zhang | f8d7177 | 2014-11-26 15:08:34 -0800 | [diff] [blame] | 407 | |
| 408 | // if there is a pending resume request, notify complete now |
| 409 | notifyResumeCompleteIfNecessary(); |
| 410 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 411 | if (mCodec != NULL) { |
| 412 | err = mCodec->release(); |
| 413 | mCodec = NULL; |
| 414 | ++mBufferGeneration; |
| 415 | |
Lajos Molnar | 1de1e25 | 2015-04-30 18:18:34 -0700 | [diff] [blame] | 416 | if (mSurface != NULL) { |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 417 | // reconnect to surface as MediaCodec disconnected from it |
| 418 | status_t error = |
Lajos Molnar | 1de1e25 | 2015-04-30 18:18:34 -0700 | [diff] [blame] | 419 | native_window_api_connect(mSurface.get(), NATIVE_WINDOW_API_MEDIA); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 420 | ALOGW_IF(error != NO_ERROR, |
| 421 | "[%s] failed to connect to native window, error=%d", |
| 422 | mComponentName.c_str(), error); |
| 423 | } |
| 424 | mComponentName = "decoder"; |
| 425 | } |
| 426 | |
| 427 | releaseAndResetMediaBuffers(); |
| 428 | |
| 429 | if (err != OK) { |
| 430 | ALOGE("failed to release %s (err=%d)", mComponentName.c_str(), err); |
| 431 | handleError(err); |
| 432 | // finish with posting kWhatShutdownCompleted. |
| 433 | } |
| 434 | |
| 435 | if (notifyComplete) { |
| 436 | sp<AMessage> notify = mNotify->dup(); |
| 437 | notify->setInt32("what", kWhatShutdownCompleted); |
| 438 | notify->post(); |
| 439 | mPaused = true; |
| 440 | } |
| 441 | } |
| 442 | |
Chong Zhang | 3b032b3 | 2015-04-17 15:49:06 -0700 | [diff] [blame] | 443 | /* |
| 444 | * returns true if we should request more data |
| 445 | */ |
| 446 | bool NuPlayer::Decoder::doRequestBuffers() { |
Lajos Molnar | e6109e2 | 2015-04-10 17:18:22 -0700 | [diff] [blame] | 447 | // mRenderer is only NULL if we have a legacy widevine source that |
| 448 | // is not yet ready. In this case we must not fetch input. |
| 449 | if (isDiscontinuityPending() || mRenderer == NULL) { |
Chong Zhang | 3b032b3 | 2015-04-17 15:49:06 -0700 | [diff] [blame] | 450 | return false; |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 451 | } |
| 452 | status_t err = OK; |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 453 | while (err == OK && !mDequeuedInputBuffers.empty()) { |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 454 | size_t bufferIx = *mDequeuedInputBuffers.begin(); |
| 455 | sp<AMessage> msg = new AMessage(); |
| 456 | msg->setSize("buffer-ix", bufferIx); |
| 457 | err = fetchInputData(msg); |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 458 | if (err != OK && err != ERROR_END_OF_STREAM) { |
| 459 | // if EOS, need to queue EOS buffer |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 460 | break; |
| 461 | } |
| 462 | mDequeuedInputBuffers.erase(mDequeuedInputBuffers.begin()); |
| 463 | |
| 464 | if (!mPendingInputMessages.empty() |
| 465 | || !onInputBufferFetched(msg)) { |
| 466 | mPendingInputMessages.push_back(msg); |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 467 | } |
| 468 | } |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 469 | |
Chong Zhang | 3b032b3 | 2015-04-17 15:49:06 -0700 | [diff] [blame] | 470 | return err == -EWOULDBLOCK |
| 471 | && mSource->feedMoreTSData() == OK; |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 474 | void NuPlayer::Decoder::handleError(int32_t err) |
| 475 | { |
| 476 | // We cannot immediately release the codec due to buffers still outstanding |
| 477 | // in the renderer. We signal to the player the error so it can shutdown/release the |
| 478 | // decoder after flushing and increment the generation to discard unnecessary messages. |
| 479 | |
| 480 | ++mBufferGeneration; |
| 481 | |
| 482 | sp<AMessage> notify = mNotify->dup(); |
| 483 | notify->setInt32("what", kWhatError); |
| 484 | notify->setInt32("err", err); |
| 485 | notify->post(); |
| 486 | } |
| 487 | |
| 488 | bool NuPlayer::Decoder::handleAnInputBuffer(size_t index) { |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 489 | if (isDiscontinuityPending()) { |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 490 | return false; |
| 491 | } |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 492 | |
| 493 | sp<ABuffer> buffer; |
| 494 | mCodec->getInputBuffer(index, &buffer); |
| 495 | |
Wei Jia | 6301a5e | 2015-05-13 13:15:18 -0700 | [diff] [blame] | 496 | if (buffer == NULL) { |
| 497 | handleError(UNKNOWN_ERROR); |
| 498 | return false; |
| 499 | } |
| 500 | |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 501 | if (index >= mInputBuffers.size()) { |
| 502 | for (size_t i = mInputBuffers.size(); i <= index; ++i) { |
| 503 | mInputBuffers.add(); |
| 504 | mMediaBuffers.add(); |
| 505 | mInputBufferIsDequeued.add(); |
| 506 | mMediaBuffers.editItemAt(i) = NULL; |
| 507 | mInputBufferIsDequeued.editItemAt(i) = false; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 508 | } |
Andreas Huber | 078cfcf | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 509 | } |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 510 | mInputBuffers.editItemAt(index) = buffer; |
Andreas Huber | 078cfcf | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 511 | |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 512 | //CHECK_LT(bufferIx, mInputBuffers.size()); |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 513 | |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 514 | if (mMediaBuffers[index] != NULL) { |
| 515 | mMediaBuffers[index]->release(); |
| 516 | mMediaBuffers.editItemAt(index) = NULL; |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 517 | } |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 518 | mInputBufferIsDequeued.editItemAt(index) = true; |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 519 | |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 520 | if (!mCSDsToSubmit.isEmpty()) { |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 521 | sp<AMessage> msg = new AMessage(); |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 522 | msg->setSize("buffer-ix", index); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 523 | |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 524 | sp<ABuffer> buffer = mCSDsToSubmit.itemAt(0); |
| 525 | ALOGI("[%s] resubmitting CSD", mComponentName.c_str()); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 526 | msg->setBuffer("buffer", buffer); |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 527 | mCSDsToSubmit.removeAt(0); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 528 | CHECK(onInputBufferFetched(msg)); |
Wei Jia | 2245fc6 | 2014-10-02 15:12:25 -0700 | [diff] [blame] | 529 | return true; |
| 530 | } |
| 531 | |
| 532 | while (!mPendingInputMessages.empty()) { |
| 533 | sp<AMessage> msg = *mPendingInputMessages.begin(); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 534 | if (!onInputBufferFetched(msg)) { |
Wei Jia | 2245fc6 | 2014-10-02 15:12:25 -0700 | [diff] [blame] | 535 | break; |
| 536 | } |
| 537 | mPendingInputMessages.erase(mPendingInputMessages.begin()); |
| 538 | } |
| 539 | |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 540 | if (!mInputBufferIsDequeued.editItemAt(index)) { |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 541 | return true; |
| 542 | } |
| 543 | |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 544 | mDequeuedInputBuffers.push_back(index); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 545 | |
| 546 | onRequestInputBuffers(); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 547 | return true; |
| 548 | } |
| 549 | |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 550 | bool NuPlayer::Decoder::handleAnOutputBuffer( |
| 551 | size_t index, |
| 552 | size_t offset, |
| 553 | size_t size, |
| 554 | int64_t timeUs, |
| 555 | int32_t flags) { |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 556 | // CHECK_LT(bufferIx, mOutputBuffers.size()); |
| 557 | sp<ABuffer> buffer; |
| 558 | mCodec->getOutputBuffer(index, &buffer); |
| 559 | |
| 560 | if (index >= mOutputBuffers.size()) { |
| 561 | for (size_t i = mOutputBuffers.size(); i <= index; ++i) { |
| 562 | mOutputBuffers.add(); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | mOutputBuffers.editItemAt(index) = buffer; |
| 567 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 568 | buffer->setRange(offset, size); |
| 569 | buffer->meta()->clear(); |
| 570 | buffer->meta()->setInt64("timeUs", timeUs); |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 571 | |
| 572 | bool eos = flags & MediaCodec::BUFFER_FLAG_EOS; |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 573 | // we do not expect CODECCONFIG or SYNCFRAME for decoder |
| 574 | |
Lajos Molnar | 1d15ab5 | 2015-03-04 16:46:34 -0800 | [diff] [blame] | 575 | sp<AMessage> reply = new AMessage(kWhatRenderBuffer, this); |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 576 | reply->setSize("buffer-ix", index); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 577 | reply->setInt32("generation", mBufferGeneration); |
| 578 | |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 579 | if (eos) { |
| 580 | ALOGI("[%s] saw output EOS", mIsAudio ? "audio" : "video"); |
| 581 | |
| 582 | buffer->meta()->setInt32("eos", true); |
| 583 | reply->setInt32("eos", true); |
| 584 | } else if (mSkipRenderingUntilMediaTimeUs >= 0) { |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 585 | if (timeUs < mSkipRenderingUntilMediaTimeUs) { |
| 586 | ALOGV("[%s] dropping buffer at time %lld as requested.", |
| 587 | mComponentName.c_str(), (long long)timeUs); |
| 588 | |
| 589 | reply->post(); |
| 590 | return true; |
| 591 | } |
| 592 | |
| 593 | mSkipRenderingUntilMediaTimeUs = -1; |
| 594 | } |
| 595 | |
Praveen Chavan | e1e5d7a | 2015-05-19 19:09:48 -0700 | [diff] [blame] | 596 | mNumFramesTotal += !mIsAudio; |
| 597 | |
Chong Zhang | f8d7177 | 2014-11-26 15:08:34 -0800 | [diff] [blame] | 598 | // wait until 1st frame comes out to signal resume complete |
| 599 | notifyResumeCompleteIfNecessary(); |
| 600 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 601 | if (mRenderer != NULL) { |
| 602 | // send the buffer to renderer. |
| 603 | mRenderer->queueBuffer(mIsAudio, buffer, reply); |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 604 | if (eos && !isDiscontinuityPending()) { |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 605 | mRenderer->queueEOS(mIsAudio, ERROR_END_OF_STREAM); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | return true; |
| 610 | } |
| 611 | |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 612 | void NuPlayer::Decoder::handleOutputFormatChange(const sp<AMessage> &format) { |
| 613 | if (!mIsAudio) { |
Praveen Chavan | e1e5d7a | 2015-05-19 19:09:48 -0700 | [diff] [blame] | 614 | int32_t width, height; |
| 615 | if (format->findInt32("width", &width) |
| 616 | && format->findInt32("height", &height)) { |
| 617 | mStats->setInt32("width", width); |
| 618 | mStats->setInt32("height", height); |
| 619 | } |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 620 | sp<AMessage> notify = mNotify->dup(); |
| 621 | notify->setInt32("what", kWhatVideoSizeChanged); |
| 622 | notify->setMessage("format", format); |
| 623 | notify->post(); |
| 624 | } else if (mRenderer != NULL) { |
| 625 | uint32_t flags; |
| 626 | int64_t durationUs; |
| 627 | bool hasVideo = (mSource->getFormat(false /* audio */) != NULL); |
Andy Hung | 288da02 | 2015-05-31 22:55:59 -0700 | [diff] [blame] | 628 | if (getAudioDeepBufferSetting() // override regardless of source duration |
| 629 | || (!hasVideo |
| 630 | && mSource->getDuration(&durationUs) == OK |
| 631 | && durationUs > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US)) { |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 632 | flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
| 633 | } else { |
| 634 | flags = AUDIO_OUTPUT_FLAG_NONE; |
| 635 | } |
| 636 | |
Eric Laurent | 216f017 | 2015-08-20 18:40:24 -0700 | [diff] [blame] | 637 | status_t err = mRenderer->openAudioSink( |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 638 | format, false /* offloadOnly */, hasVideo, flags, NULL /* isOffloaed */); |
Eric Laurent | 216f017 | 2015-08-20 18:40:24 -0700 | [diff] [blame] | 639 | if (err != OK) { |
| 640 | handleError(err); |
| 641 | } |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 645 | void NuPlayer::Decoder::releaseAndResetMediaBuffers() { |
| 646 | for (size_t i = 0; i < mMediaBuffers.size(); i++) { |
| 647 | if (mMediaBuffers[i] != NULL) { |
| 648 | mMediaBuffers[i]->release(); |
| 649 | mMediaBuffers.editItemAt(i) = NULL; |
| 650 | } |
| 651 | } |
| 652 | mMediaBuffers.resize(mInputBuffers.size()); |
| 653 | for (size_t i = 0; i < mMediaBuffers.size(); i++) { |
| 654 | mMediaBuffers.editItemAt(i) = NULL; |
| 655 | } |
| 656 | mInputBufferIsDequeued.clear(); |
| 657 | mInputBufferIsDequeued.resize(mInputBuffers.size()); |
| 658 | for (size_t i = 0; i < mInputBufferIsDequeued.size(); i++) { |
| 659 | mInputBufferIsDequeued.editItemAt(i) = false; |
| 660 | } |
| 661 | |
| 662 | mPendingInputMessages.clear(); |
| 663 | mDequeuedInputBuffers.clear(); |
| 664 | mSkipRenderingUntilMediaTimeUs = -1; |
| 665 | } |
| 666 | |
| 667 | void NuPlayer::Decoder::requestCodecNotification() { |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 668 | if (mCodec != NULL) { |
Lajos Molnar | 1d15ab5 | 2015-03-04 16:46:34 -0800 | [diff] [blame] | 669 | sp<AMessage> reply = new AMessage(kWhatCodecNotify, this); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 670 | reply->setInt32("generation", mBufferGeneration); |
| 671 | mCodec->requestActivityNotification(reply); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | bool NuPlayer::Decoder::isStaleReply(const sp<AMessage> &msg) { |
| 676 | int32_t generation; |
| 677 | CHECK(msg->findInt32("generation", &generation)); |
| 678 | return generation != mBufferGeneration; |
| 679 | } |
| 680 | |
| 681 | status_t NuPlayer::Decoder::fetchInputData(sp<AMessage> &reply) { |
| 682 | sp<ABuffer> accessUnit; |
| 683 | bool dropAccessUnit; |
| 684 | do { |
| 685 | status_t err = mSource->dequeueAccessUnit(mIsAudio, &accessUnit); |
| 686 | |
| 687 | if (err == -EWOULDBLOCK) { |
| 688 | return err; |
| 689 | } else if (err != OK) { |
| 690 | if (err == INFO_DISCONTINUITY) { |
| 691 | int32_t type; |
| 692 | CHECK(accessUnit->meta()->findInt32("discontinuity", &type)); |
| 693 | |
| 694 | bool formatChange = |
| 695 | (mIsAudio && |
| 696 | (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT)) |
| 697 | || (!mIsAudio && |
| 698 | (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT)); |
| 699 | |
| 700 | bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0; |
| 701 | |
| 702 | ALOGI("%s discontinuity (format=%d, time=%d)", |
| 703 | mIsAudio ? "audio" : "video", formatChange, timeChange); |
| 704 | |
| 705 | bool seamlessFormatChange = false; |
| 706 | sp<AMessage> newFormat = mSource->getFormat(mIsAudio); |
| 707 | if (formatChange) { |
| 708 | seamlessFormatChange = |
| 709 | supportsSeamlessFormatChange(newFormat); |
| 710 | // treat seamless format change separately |
| 711 | formatChange = !seamlessFormatChange; |
| 712 | } |
| 713 | |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 714 | // For format or time change, return EOS to queue EOS input, |
| 715 | // then wait for EOS on output. |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 716 | if (formatChange /* not seamless */) { |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 717 | mFormatChangePending = true; |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 718 | err = ERROR_END_OF_STREAM; |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 719 | } else if (timeChange) { |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 720 | rememberCodecSpecificData(newFormat); |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 721 | mTimeChangePending = true; |
| 722 | err = ERROR_END_OF_STREAM; |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 723 | } else if (seamlessFormatChange) { |
| 724 | // reuse existing decoder and don't flush |
| 725 | rememberCodecSpecificData(newFormat); |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 726 | continue; |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 727 | } else { |
| 728 | // This stream is unaffected by the discontinuity |
| 729 | return -EWOULDBLOCK; |
| 730 | } |
| 731 | } |
| 732 | |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 733 | // reply should only be returned without a buffer set |
| 734 | // when there is an error (including EOS) |
| 735 | CHECK(err != OK); |
| 736 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 737 | reply->setInt32("err", err); |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 738 | return ERROR_END_OF_STREAM; |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 739 | } |
| 740 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 741 | dropAccessUnit = false; |
| 742 | if (!mIsAudio |
| 743 | && !mIsSecure |
| 744 | && mRenderer->getVideoLateByUs() > 100000ll |
| 745 | && mIsVideoAVC |
| 746 | && !IsAVCReferenceFrame(accessUnit)) { |
| 747 | dropAccessUnit = true; |
Praveen Chavan | e1e5d7a | 2015-05-19 19:09:48 -0700 | [diff] [blame] | 748 | ++mNumInputFramesDropped; |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 749 | } |
| 750 | } while (dropAccessUnit); |
| 751 | |
| 752 | // ALOGV("returned a valid buffer of %s data", mIsAudio ? "mIsAudio" : "video"); |
| 753 | #if 0 |
| 754 | int64_t mediaTimeUs; |
| 755 | CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs)); |
Chong Zhang | 5abbd3d | 2015-04-20 16:03:00 -0700 | [diff] [blame] | 756 | ALOGV("[%s] feeding input buffer at media time %.3f", |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 757 | mIsAudio ? "audio" : "video", |
| 758 | mediaTimeUs / 1E6); |
| 759 | #endif |
| 760 | |
| 761 | if (mCCDecoder != NULL) { |
| 762 | mCCDecoder->decode(accessUnit); |
| 763 | } |
| 764 | |
| 765 | reply->setBuffer("buffer", accessUnit); |
| 766 | |
| 767 | return OK; |
| 768 | } |
| 769 | |
| 770 | bool NuPlayer::Decoder::onInputBufferFetched(const sp<AMessage> &msg) { |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 771 | size_t bufferIx; |
| 772 | CHECK(msg->findSize("buffer-ix", &bufferIx)); |
| 773 | CHECK_LT(bufferIx, mInputBuffers.size()); |
| 774 | sp<ABuffer> codecBuffer = mInputBuffers[bufferIx]; |
| 775 | |
| 776 | sp<ABuffer> buffer; |
| 777 | bool hasBuffer = msg->findBuffer("buffer", &buffer); |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 778 | |
| 779 | // handle widevine classic source - that fills an arbitrary input buffer |
| 780 | MediaBuffer *mediaBuffer = NULL; |
Wei Jia | 96e92b5 | 2014-09-18 17:36:20 -0700 | [diff] [blame] | 781 | if (hasBuffer) { |
| 782 | mediaBuffer = (MediaBuffer *)(buffer->getMediaBufferBase()); |
| 783 | if (mediaBuffer != NULL) { |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 784 | // likely filled another buffer than we requested: adjust buffer index |
| 785 | size_t ix; |
| 786 | for (ix = 0; ix < mInputBuffers.size(); ix++) { |
| 787 | const sp<ABuffer> &buf = mInputBuffers[ix]; |
| 788 | if (buf->data() == mediaBuffer->data()) { |
| 789 | // all input buffers are dequeued on start, hence the check |
Wei Jia | 2245fc6 | 2014-10-02 15:12:25 -0700 | [diff] [blame] | 790 | if (!mInputBufferIsDequeued[ix]) { |
| 791 | ALOGV("[%s] received MediaBuffer for #%zu instead of #%zu", |
| 792 | mComponentName.c_str(), ix, bufferIx); |
| 793 | mediaBuffer->release(); |
| 794 | return false; |
| 795 | } |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 796 | |
| 797 | // TRICKY: need buffer for the metadata, so instead, set |
| 798 | // codecBuffer to the same (though incorrect) buffer to |
| 799 | // avoid a memcpy into the codecBuffer |
| 800 | codecBuffer = buffer; |
| 801 | codecBuffer->setRange( |
| 802 | mediaBuffer->range_offset(), |
| 803 | mediaBuffer->range_length()); |
| 804 | bufferIx = ix; |
| 805 | break; |
| 806 | } |
| 807 | } |
| 808 | CHECK(ix < mInputBuffers.size()); |
| 809 | } |
| 810 | } |
| 811 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 812 | if (buffer == NULL /* includes !hasBuffer */) { |
| 813 | int32_t streamErr = ERROR_END_OF_STREAM; |
| 814 | CHECK(msg->findInt32("err", &streamErr) || !hasBuffer); |
| 815 | |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 816 | CHECK(streamErr != OK); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 817 | |
| 818 | // attempt to queue EOS |
| 819 | status_t err = mCodec->queueInputBuffer( |
| 820 | bufferIx, |
| 821 | 0, |
| 822 | 0, |
| 823 | 0, |
| 824 | MediaCodec::BUFFER_FLAG_EOS); |
Andy Hung | cf31f1e | 2014-09-23 14:59:01 -0700 | [diff] [blame] | 825 | if (err == OK) { |
| 826 | mInputBufferIsDequeued.editItemAt(bufferIx) = false; |
| 827 | } else if (streamErr == ERROR_END_OF_STREAM) { |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 828 | streamErr = err; |
| 829 | // err will not be ERROR_END_OF_STREAM |
| 830 | } |
| 831 | |
| 832 | if (streamErr != ERROR_END_OF_STREAM) { |
Andy Hung | cf31f1e | 2014-09-23 14:59:01 -0700 | [diff] [blame] | 833 | ALOGE("Stream error for %s (err=%d), EOS %s queued", |
| 834 | mComponentName.c_str(), |
| 835 | streamErr, |
| 836 | err == OK ? "successfully" : "unsuccessfully"); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 837 | handleError(streamErr); |
| 838 | } |
| 839 | } else { |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 840 | sp<AMessage> extra; |
| 841 | if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) { |
| 842 | int64_t resumeAtMediaTimeUs; |
| 843 | if (extra->findInt64( |
| 844 | "resume-at-mediaTimeUs", &resumeAtMediaTimeUs)) { |
| 845 | ALOGI("[%s] suppressing rendering until %lld us", |
| 846 | mComponentName.c_str(), (long long)resumeAtMediaTimeUs); |
| 847 | mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs; |
| 848 | } |
| 849 | } |
| 850 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 851 | int64_t timeUs = 0; |
| 852 | uint32_t flags = 0; |
| 853 | CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); |
| 854 | |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 855 | int32_t eos, csd; |
| 856 | // we do not expect SYNCFRAME for decoder |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 857 | if (buffer->meta()->findInt32("eos", &eos) && eos) { |
| 858 | flags |= MediaCodec::BUFFER_FLAG_EOS; |
Lajos Molnar | 87603c0 | 2014-08-20 19:25:30 -0700 | [diff] [blame] | 859 | } else if (buffer->meta()->findInt32("csd", &csd) && csd) { |
| 860 | flags |= MediaCodec::BUFFER_FLAG_CODECCONFIG; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | // copy into codec buffer |
| 864 | if (buffer != codecBuffer) { |
| 865 | CHECK_LE(buffer->size(), codecBuffer->capacity()); |
| 866 | codecBuffer->setRange(0, buffer->size()); |
| 867 | memcpy(codecBuffer->data(), buffer->data(), buffer->size()); |
| 868 | } |
| 869 | |
| 870 | status_t err = mCodec->queueInputBuffer( |
| 871 | bufferIx, |
| 872 | codecBuffer->offset(), |
| 873 | codecBuffer->size(), |
| 874 | timeUs, |
| 875 | flags); |
| 876 | if (err != OK) { |
Andy Hung | cf31f1e | 2014-09-23 14:59:01 -0700 | [diff] [blame] | 877 | if (mediaBuffer != NULL) { |
| 878 | mediaBuffer->release(); |
| 879 | } |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 880 | ALOGE("Failed to queue input buffer for %s (err=%d)", |
| 881 | mComponentName.c_str(), err); |
| 882 | handleError(err); |
Andy Hung | cf31f1e | 2014-09-23 14:59:01 -0700 | [diff] [blame] | 883 | } else { |
| 884 | mInputBufferIsDequeued.editItemAt(bufferIx) = false; |
| 885 | if (mediaBuffer != NULL) { |
| 886 | CHECK(mMediaBuffers[bufferIx] == NULL); |
| 887 | mMediaBuffers.editItemAt(bufferIx) = mediaBuffer; |
| 888 | } |
Lajos Molnar | 0952483 | 2014-07-17 14:29:51 -0700 | [diff] [blame] | 889 | } |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 890 | } |
Wei Jia | 2245fc6 | 2014-10-02 15:12:25 -0700 | [diff] [blame] | 891 | return true; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 892 | } |
| 893 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 894 | void NuPlayer::Decoder::onRenderBuffer(const sp<AMessage> &msg) { |
| 895 | status_t err; |
| 896 | int32_t render; |
| 897 | size_t bufferIx; |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 898 | int32_t eos; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 899 | CHECK(msg->findSize("buffer-ix", &bufferIx)); |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 900 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 901 | if (!mIsAudio) { |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 902 | int64_t timeUs; |
| 903 | sp<ABuffer> buffer = mOutputBuffers[bufferIx]; |
| 904 | buffer->meta()->findInt64("timeUs", &timeUs); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 905 | |
| 906 | if (mCCDecoder != NULL && mCCDecoder->isSelected()) { |
| 907 | mCCDecoder->display(timeUs); |
| 908 | } |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 909 | } |
| 910 | |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 911 | if (msg->findInt32("render", &render) && render) { |
Lajos Molnar | dc43dfa | 2014-05-07 15:33:04 -0700 | [diff] [blame] | 912 | int64_t timestampNs; |
| 913 | CHECK(msg->findInt64("timestampNs", ×tampNs)); |
| 914 | err = mCodec->renderOutputBufferAndRelease(bufferIx, timestampNs); |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 915 | } else { |
Praveen Chavan | e1e5d7a | 2015-05-19 19:09:48 -0700 | [diff] [blame] | 916 | mNumOutputFramesDropped += !mIsAudio; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 917 | err = mCodec->releaseOutputBuffer(bufferIx); |
| 918 | } |
| 919 | if (err != OK) { |
| 920 | ALOGE("failed to release output buffer for %s (err=%d)", |
| 921 | mComponentName.c_str(), err); |
| 922 | handleError(err); |
| 923 | } |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 924 | if (msg->findInt32("eos", &eos) && eos |
| 925 | && isDiscontinuityPending()) { |
| 926 | finishHandleDiscontinuity(true /* flushOnTimeChange */); |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | bool NuPlayer::Decoder::isDiscontinuityPending() const { |
| 931 | return mFormatChangePending || mTimeChangePending; |
| 932 | } |
| 933 | |
| 934 | void NuPlayer::Decoder::finishHandleDiscontinuity(bool flushOnTimeChange) { |
| 935 | ALOGV("finishHandleDiscontinuity: format %d, time %d, flush %d", |
| 936 | mFormatChangePending, mTimeChangePending, flushOnTimeChange); |
| 937 | |
| 938 | // If we have format change, pause and wait to be killed; |
| 939 | // If we have time change only, flush and restart fetching. |
| 940 | |
| 941 | if (mFormatChangePending) { |
| 942 | mPaused = true; |
| 943 | } else if (mTimeChangePending) { |
| 944 | if (flushOnTimeChange) { |
Marco Nelissen | 421f47c | 2015-03-25 14:40:32 -0700 | [diff] [blame] | 945 | doFlush(false /* notifyComplete */); |
| 946 | signalResume(false /* notifyComplete */); |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 947 | } |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | // Notify NuPlayer to either shutdown decoder, or rescan sources |
| 951 | sp<AMessage> msg = mNotify->dup(); |
| 952 | msg->setInt32("what", kWhatInputDiscontinuity); |
| 953 | msg->setInt32("formatChange", mFormatChangePending); |
| 954 | msg->post(); |
| 955 | |
| 956 | mFormatChangePending = false; |
| 957 | mTimeChangePending = false; |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 958 | } |
| 959 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 960 | bool NuPlayer::Decoder::supportsSeamlessAudioFormatChange( |
| 961 | const sp<AMessage> &targetFormat) const { |
Robert Shih | 6d0a94e | 2014-01-23 16:18:22 -0800 | [diff] [blame] | 962 | if (targetFormat == NULL) { |
| 963 | return true; |
| 964 | } |
| 965 | |
| 966 | AString mime; |
| 967 | if (!targetFormat->findString("mime", &mime)) { |
| 968 | return false; |
| 969 | } |
| 970 | |
| 971 | if (!strcasecmp(mime.c_str(), MEDIA_MIMETYPE_AUDIO_AAC)) { |
| 972 | // field-by-field comparison |
| 973 | const char * keys[] = { "channel-count", "sample-rate", "is-adts" }; |
| 974 | for (unsigned int i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) { |
| 975 | int32_t oldVal, newVal; |
joakim johansson | 7abbd4c | 2015-01-30 14:16:03 +0100 | [diff] [blame] | 976 | if (!mInputFormat->findInt32(keys[i], &oldVal) || |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 977 | !targetFormat->findInt32(keys[i], &newVal) || |
| 978 | oldVal != newVal) { |
Robert Shih | 6d0a94e | 2014-01-23 16:18:22 -0800 | [diff] [blame] | 979 | return false; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | sp<ABuffer> oldBuf, newBuf; |
joakim johansson | 7abbd4c | 2015-01-30 14:16:03 +0100 | [diff] [blame] | 984 | if (mInputFormat->findBuffer("csd-0", &oldBuf) && |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 985 | targetFormat->findBuffer("csd-0", &newBuf)) { |
Robert Shih | 6d0a94e | 2014-01-23 16:18:22 -0800 | [diff] [blame] | 986 | if (oldBuf->size() != newBuf->size()) { |
| 987 | return false; |
| 988 | } |
| 989 | return !memcmp(oldBuf->data(), newBuf->data(), oldBuf->size()); |
| 990 | } |
| 991 | } |
| 992 | return false; |
| 993 | } |
| 994 | |
| 995 | bool NuPlayer::Decoder::supportsSeamlessFormatChange(const sp<AMessage> &targetFormat) const { |
joakim johansson | 7abbd4c | 2015-01-30 14:16:03 +0100 | [diff] [blame] | 996 | if (mInputFormat == NULL) { |
Robert Shih | 6d0a94e | 2014-01-23 16:18:22 -0800 | [diff] [blame] | 997 | return false; |
| 998 | } |
| 999 | |
| 1000 | if (targetFormat == NULL) { |
| 1001 | return true; |
| 1002 | } |
| 1003 | |
| 1004 | AString oldMime, newMime; |
joakim johansson | 7abbd4c | 2015-01-30 14:16:03 +0100 | [diff] [blame] | 1005 | if (!mInputFormat->findString("mime", &oldMime) |
Robert Shih | 6d0a94e | 2014-01-23 16:18:22 -0800 | [diff] [blame] | 1006 | || !targetFormat->findString("mime", &newMime) |
| 1007 | || !(oldMime == newMime)) { |
| 1008 | return false; |
| 1009 | } |
| 1010 | |
| 1011 | bool audio = !strncasecmp(oldMime.c_str(), "audio/", strlen("audio/")); |
| 1012 | bool seamless; |
| 1013 | if (audio) { |
| 1014 | seamless = supportsSeamlessAudioFormatChange(targetFormat); |
| 1015 | } else { |
Lajos Molnar | 1cd1398 | 2014-01-17 15:12:51 -0800 | [diff] [blame] | 1016 | int32_t isAdaptive; |
| 1017 | seamless = (mCodec != NULL && |
| 1018 | mInputFormat->findInt32("adaptive-playback", &isAdaptive) && |
| 1019 | isAdaptive); |
Robert Shih | 6d0a94e | 2014-01-23 16:18:22 -0800 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | ALOGV("%s seamless support for %s", seamless ? "yes" : "no", oldMime.c_str()); |
| 1023 | return seamless; |
| 1024 | } |
| 1025 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 1026 | void NuPlayer::Decoder::rememberCodecSpecificData(const sp<AMessage> &format) { |
| 1027 | if (format == NULL) { |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 1028 | return; |
| 1029 | } |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 1030 | mCSDsForCurrentFormat.clear(); |
| 1031 | for (int32_t i = 0; ; ++i) { |
| 1032 | AString tag = "csd-"; |
| 1033 | tag.append(i); |
| 1034 | sp<ABuffer> buffer; |
| 1035 | if (!format->findBuffer(tag.c_str(), &buffer)) { |
| 1036 | break; |
| 1037 | } |
| 1038 | mCSDsForCurrentFormat.push(buffer); |
Chong Zhang | a7fa1d9 | 2014-06-11 14:49:23 -0700 | [diff] [blame] | 1039 | } |
Chong Zhang | b86e68f | 2014-08-01 13:46:53 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
Chong Zhang | f8d7177 | 2014-11-26 15:08:34 -0800 | [diff] [blame] | 1042 | void NuPlayer::Decoder::notifyResumeCompleteIfNecessary() { |
| 1043 | if (mResumePending) { |
| 1044 | mResumePending = false; |
| 1045 | |
| 1046 | sp<AMessage> notify = mNotify->dup(); |
| 1047 | notify->setInt32("what", kWhatResumeCompleted); |
| 1048 | notify->post(); |
| 1049 | } |
| 1050 | } |
| 1051 | |
Andreas Huber | f933441 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1052 | } // namespace android |
| 1053 | |