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