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