Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 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 "NuPlayerDecoderPassThrough" |
| 19 | #include <utils/Log.h> |
| 20 | #include <inttypes.h> |
| 21 | |
| 22 | #include "NuPlayerDecoderPassThrough.h" |
| 23 | |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 24 | #include "NuPlayerRenderer.h" |
| 25 | #include "NuPlayerSource.h" |
| 26 | |
Marco Nelissen | 13aa1a4 | 2019-09-27 10:21:55 -0700 | [diff] [blame^] | 27 | #include <mediadrm/ICrypto.h> |
Wonsik Kim | 7e34bf5 | 2016-08-23 00:09:18 +0900 | [diff] [blame] | 28 | #include <media/MediaCodecBuffer.h> |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 29 | #include <media/stagefright/foundation/ABuffer.h> |
| 30 | #include <media/stagefright/foundation/ADebug.h> |
| 31 | #include <media/stagefright/foundation/AMessage.h> |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 32 | #include <media/stagefright/MediaErrors.h> |
| 33 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 34 | #include "ATSParser.h" |
| 35 | |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 36 | namespace android { |
| 37 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 38 | // TODO optimize buffer size for power consumption |
| 39 | // The offload read buffer size is 32 KB but 24 KB uses less power. |
| 40 | static const size_t kAggregateBufferSizeBytes = 24 * 1024; |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 41 | static const size_t kMaxCachedBytes = 200000; |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 42 | |
| 43 | NuPlayer::DecoderPassThrough::DecoderPassThrough( |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 44 | const sp<AMessage> ¬ify, |
| 45 | const sp<Source> &source, |
| 46 | const sp<Renderer> &renderer) |
Andy Hung | 202bce1 | 2014-12-03 11:47:36 -0800 | [diff] [blame] | 47 | : DecoderBase(notify), |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 48 | mSource(source), |
| 49 | mRenderer(renderer), |
Chih-Hung Hsieh | 62309d5 | 2018-12-11 13:54:02 -0800 | [diff] [blame] | 50 | mSkipRenderingUntilMediaTimeUs(-1LL), |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 51 | mReachedEOS(true), |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 52 | mPendingAudioErr(OK), |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 53 | mPendingBuffersToDrain(0), |
Chong Zhang | de01afb | 2014-08-13 13:48:10 -0700 | [diff] [blame] | 54 | mCachedBytes(0), |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 55 | mComponentName("pass through decoder") { |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 56 | ALOGW_IF(renderer == NULL, "expect a non-NULL renderer"); |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | NuPlayer::DecoderPassThrough::~DecoderPassThrough() { |
| 60 | } |
| 61 | |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 62 | void NuPlayer::DecoderPassThrough::onConfigure(const sp<AMessage> &format) { |
| 63 | ALOGV("[%s] onConfigure", mComponentName.c_str()); |
Chong Zhang | de01afb | 2014-08-13 13:48:10 -0700 | [diff] [blame] | 64 | mCachedBytes = 0; |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 65 | mPendingBuffersToDrain = 0; |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 66 | mReachedEOS = false; |
| 67 | ++mBufferGeneration; |
| 68 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 69 | onRequestInputBuffers(); |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 70 | |
Haynes Mathew George | 8b63533 | 2015-03-30 17:59:47 -0700 | [diff] [blame] | 71 | int32_t hasVideo = 0; |
| 72 | format->findInt32("has-video", &hasVideo); |
| 73 | |
Andy Hung | 202bce1 | 2014-12-03 11:47:36 -0800 | [diff] [blame] | 74 | // The audio sink is already opened before the PassThrough decoder is created. |
| 75 | // Opening again might be relevant if decoder is instantiated after shutdown and |
| 76 | // format is different. |
| 77 | status_t err = mRenderer->openAudioSink( |
Haynes Mathew George | 8b63533 | 2015-03-30 17:59:47 -0700 | [diff] [blame] | 78 | format, true /* offloadOnly */, hasVideo, |
Dhananjay Kumar | c387f2b | 2015-08-06 10:43:16 +0530 | [diff] [blame] | 79 | AUDIO_OUTPUT_FLAG_NONE /* flags */, NULL /* isOffloaded */, mSource->isStreaming()); |
Andy Hung | 202bce1 | 2014-12-03 11:47:36 -0800 | [diff] [blame] | 80 | if (err != OK) { |
| 81 | handleError(err); |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 82 | } |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Ronghua Wu | 8db8813 | 2015-04-22 13:51:35 -0700 | [diff] [blame] | 85 | void NuPlayer::DecoderPassThrough::onSetParameters(const sp<AMessage> &/*params*/) { |
| 86 | ALOGW("onSetParameters() called unexpectedly"); |
| 87 | } |
| 88 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 89 | void NuPlayer::DecoderPassThrough::onSetRenderer( |
| 90 | const sp<Renderer> &renderer) { |
| 91 | // renderer can't be changed during offloading |
| 92 | ALOGW_IF(renderer != mRenderer, |
| 93 | "ignoring request to change renderer"); |
| 94 | } |
| 95 | |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 96 | bool NuPlayer::DecoderPassThrough::isStaleReply(const sp<AMessage> &msg) { |
| 97 | int32_t generation; |
| 98 | CHECK(msg->findInt32("generation", &generation)); |
| 99 | return generation != mBufferGeneration; |
| 100 | } |
| 101 | |
Ronghua Wu | f182891 | 2014-12-01 15:33:36 -0800 | [diff] [blame] | 102 | bool NuPlayer::DecoderPassThrough::isDoneFetching() const { |
| 103 | ALOGV("[%s] mCachedBytes = %zu, mReachedEOS = %d mPaused = %d", |
| 104 | mComponentName.c_str(), mCachedBytes, mReachedEOS, mPaused); |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 105 | |
Ronghua Wu | f182891 | 2014-12-01 15:33:36 -0800 | [diff] [blame] | 106 | return mCachedBytes >= kMaxCachedBytes || mReachedEOS || mPaused; |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Chong Zhang | 3b032b3 | 2015-04-17 15:49:06 -0700 | [diff] [blame] | 109 | /* |
| 110 | * returns true if we should request more data |
| 111 | */ |
| 112 | bool NuPlayer::DecoderPassThrough::doRequestBuffers() { |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 113 | status_t err = OK; |
Ronghua Wu | f182891 | 2014-12-01 15:33:36 -0800 | [diff] [blame] | 114 | while (!isDoneFetching()) { |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 115 | sp<AMessage> msg = new AMessage(); |
| 116 | |
| 117 | err = fetchInputData(msg); |
| 118 | if (err != OK) { |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | onInputBufferFetched(msg); |
| 123 | } |
| 124 | |
Chong Zhang | 3b032b3 | 2015-04-17 15:49:06 -0700 | [diff] [blame] | 125 | return err == -EWOULDBLOCK |
| 126 | && mSource->feedMoreTSData() == OK; |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | status_t NuPlayer::DecoderPassThrough::dequeueAccessUnit(sp<ABuffer> *accessUnit) { |
| 130 | status_t err; |
| 131 | |
| 132 | // Did we save an accessUnit earlier because of a discontinuity? |
| 133 | if (mPendingAudioAccessUnit != NULL) { |
| 134 | *accessUnit = mPendingAudioAccessUnit; |
| 135 | mPendingAudioAccessUnit.clear(); |
| 136 | err = mPendingAudioErr; |
| 137 | ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit"); |
| 138 | } else { |
| 139 | err = mSource->dequeueAccessUnit(true /* audio */, accessUnit); |
| 140 | } |
| 141 | |
| 142 | if (err == INFO_DISCONTINUITY || err == ERROR_END_OF_STREAM) { |
| 143 | if (mAggregateBuffer != NULL) { |
| 144 | // We already have some data so save this for later. |
| 145 | mPendingAudioErr = err; |
| 146 | mPendingAudioAccessUnit = *accessUnit; |
| 147 | (*accessUnit).clear(); |
| 148 | ALOGD("return aggregated buffer and save err(=%d) for later", err); |
| 149 | err = OK; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return err; |
| 154 | } |
| 155 | |
| 156 | sp<ABuffer> NuPlayer::DecoderPassThrough::aggregateBuffer( |
| 157 | const sp<ABuffer> &accessUnit) { |
| 158 | sp<ABuffer> aggregate; |
| 159 | |
| 160 | if (accessUnit == NULL) { |
| 161 | // accessUnit is saved to mPendingAudioAccessUnit |
| 162 | // return current mAggregateBuffer |
| 163 | aggregate = mAggregateBuffer; |
| 164 | mAggregateBuffer.clear(); |
| 165 | return aggregate; |
| 166 | } |
| 167 | |
| 168 | size_t smallSize = accessUnit->size(); |
| 169 | if ((mAggregateBuffer == NULL) |
| 170 | // Don't bother if only room for a few small buffers. |
| 171 | && (smallSize < (kAggregateBufferSizeBytes / 3))) { |
| 172 | // Create a larger buffer for combining smaller buffers from the extractor. |
| 173 | mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes); |
| 174 | mAggregateBuffer->setRange(0, 0); // start empty |
| 175 | } |
| 176 | |
| 177 | if (mAggregateBuffer != NULL) { |
| 178 | int64_t timeUs; |
| 179 | int64_t dummy; |
| 180 | bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs); |
| 181 | bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy); |
| 182 | // Will the smaller buffer fit? |
| 183 | size_t bigSize = mAggregateBuffer->size(); |
| 184 | size_t roomLeft = mAggregateBuffer->capacity() - bigSize; |
| 185 | // Should we save this small buffer for the next big buffer? |
| 186 | // If the first small buffer did not have a timestamp then save |
| 187 | // any buffer that does have a timestamp until the next big buffer. |
| 188 | if ((smallSize > roomLeft) |
| 189 | || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) { |
| 190 | mPendingAudioErr = OK; |
| 191 | mPendingAudioAccessUnit = accessUnit; |
| 192 | aggregate = mAggregateBuffer; |
| 193 | mAggregateBuffer.clear(); |
| 194 | } else { |
| 195 | // Grab time from first small buffer if available. |
| 196 | if ((bigSize == 0) && smallTimestampValid) { |
| 197 | mAggregateBuffer->meta()->setInt64("timeUs", timeUs); |
| 198 | } |
| 199 | // Append small buffer to the bigger buffer. |
| 200 | memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize); |
| 201 | bigSize += smallSize; |
| 202 | mAggregateBuffer->setRange(0, bigSize); |
| 203 | |
| 204 | ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu", |
| 205 | smallSize, bigSize, mAggregateBuffer->capacity()); |
| 206 | } |
| 207 | } else { |
| 208 | // decided not to aggregate |
| 209 | aggregate = accessUnit; |
| 210 | } |
| 211 | |
| 212 | return aggregate; |
| 213 | } |
| 214 | |
| 215 | status_t NuPlayer::DecoderPassThrough::fetchInputData(sp<AMessage> &reply) { |
| 216 | sp<ABuffer> accessUnit; |
| 217 | |
| 218 | do { |
| 219 | status_t err = dequeueAccessUnit(&accessUnit); |
| 220 | |
| 221 | if (err == -EWOULDBLOCK) { |
Wei Jia | 14532f2 | 2015-12-29 11:28:15 -0800 | [diff] [blame] | 222 | // Flush out the aggregate buffer to try to avoid underrun. |
| 223 | accessUnit = aggregateBuffer(NULL /* accessUnit */); |
| 224 | if (accessUnit != NULL) { |
| 225 | break; |
| 226 | } |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 227 | return err; |
| 228 | } else if (err != OK) { |
| 229 | if (err == INFO_DISCONTINUITY) { |
| 230 | int32_t type; |
| 231 | CHECK(accessUnit->meta()->findInt32("discontinuity", &type)); |
| 232 | |
| 233 | bool formatChange = |
| 234 | (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT) != 0; |
| 235 | |
| 236 | bool timeChange = |
| 237 | (type & ATSParser::DISCONTINUITY_TIME) != 0; |
| 238 | |
| 239 | ALOGI("audio discontinuity (formatChange=%d, time=%d)", |
| 240 | formatChange, timeChange); |
| 241 | |
| 242 | if (formatChange || timeChange) { |
| 243 | sp<AMessage> msg = mNotify->dup(); |
| 244 | msg->setInt32("what", kWhatInputDiscontinuity); |
| 245 | // will perform seamless format change, |
| 246 | // only notify NuPlayer to scan sources |
| 247 | msg->setInt32("formatChange", false); |
| 248 | msg->post(); |
| 249 | } |
| 250 | |
| 251 | if (timeChange) { |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 252 | doFlush(false /* notifyComplete */); |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 253 | err = OK; |
| 254 | } else if (formatChange) { |
| 255 | // do seamless format change |
| 256 | err = OK; |
| 257 | } else { |
| 258 | // This stream is unaffected by the discontinuity |
| 259 | return -EWOULDBLOCK; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | reply->setInt32("err", err); |
| 264 | return OK; |
| 265 | } |
| 266 | |
| 267 | accessUnit = aggregateBuffer(accessUnit); |
| 268 | } while (accessUnit == NULL); |
| 269 | |
| 270 | #if 0 |
| 271 | int64_t mediaTimeUs; |
| 272 | CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs)); |
| 273 | ALOGV("feeding audio input buffer at media time %.2f secs", |
| 274 | mediaTimeUs / 1E6); |
| 275 | #endif |
| 276 | |
| 277 | reply->setBuffer("buffer", accessUnit); |
| 278 | |
| 279 | return OK; |
| 280 | } |
| 281 | |
| 282 | void NuPlayer::DecoderPassThrough::onInputBufferFetched( |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 283 | const sp<AMessage> &msg) { |
| 284 | if (mReachedEOS) { |
| 285 | return; |
| 286 | } |
| 287 | |
Wonsik Kim | c8f7c35 | 2016-10-11 17:10:36 +0900 | [diff] [blame] | 288 | sp<ABuffer> buffer; |
| 289 | bool hasBuffer = msg->findBuffer("buffer", &buffer); |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 290 | if (buffer == NULL) { |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 291 | int32_t streamErr = ERROR_END_OF_STREAM; |
| 292 | CHECK(msg->findInt32("err", &streamErr) || !hasBuffer); |
| 293 | if (streamErr == OK) { |
| 294 | return; |
| 295 | } |
| 296 | |
Wei Jia | 1cbe62c | 2017-03-30 17:57:02 -0700 | [diff] [blame] | 297 | if (streamErr != ERROR_END_OF_STREAM) { |
| 298 | handleError(streamErr); |
| 299 | } |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 300 | mReachedEOS = true; |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 301 | if (mRenderer != NULL) { |
| 302 | mRenderer->queueEOS(true /* audio */, ERROR_END_OF_STREAM); |
| 303 | } |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 304 | return; |
| 305 | } |
| 306 | |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 307 | sp<AMessage> extra; |
| 308 | if (buffer->meta()->findMessage("extra", &extra) && extra != NULL) { |
| 309 | int64_t resumeAtMediaTimeUs; |
| 310 | if (extra->findInt64( |
| 311 | "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) { |
| 312 | ALOGI("[%s] suppressing rendering until %lld us", |
| 313 | mComponentName.c_str(), (long long)resumeAtMediaTimeUs); |
| 314 | mSkipRenderingUntilMediaTimeUs = resumeAtMediaTimeUs; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | int32_t bufferSize = buffer->size(); |
| 319 | mCachedBytes += bufferSize; |
| 320 | |
Wonsik Kim | c8f7c35 | 2016-10-11 17:10:36 +0900 | [diff] [blame] | 321 | int64_t timeUs = 0; |
| 322 | CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 323 | if (mSkipRenderingUntilMediaTimeUs >= 0) { |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 324 | if (timeUs < mSkipRenderingUntilMediaTimeUs) { |
| 325 | ALOGV("[%s] dropping buffer at time %lld as requested.", |
| 326 | mComponentName.c_str(), (long long)timeUs); |
| 327 | |
| 328 | onBufferConsumed(bufferSize); |
| 329 | return; |
| 330 | } |
| 331 | |
| 332 | mSkipRenderingUntilMediaTimeUs = -1; |
| 333 | } |
| 334 | |
| 335 | if (mRenderer == NULL) { |
| 336 | onBufferConsumed(bufferSize); |
| 337 | return; |
| 338 | } |
Chong Zhang | de01afb | 2014-08-13 13:48:10 -0700 | [diff] [blame] | 339 | |
Lajos Molnar | 1d15ab5 | 2015-03-04 16:46:34 -0800 | [diff] [blame] | 340 | sp<AMessage> reply = new AMessage(kWhatBufferConsumed, this); |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 341 | reply->setInt32("generation", mBufferGeneration); |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 342 | reply->setInt32("size", bufferSize); |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 343 | |
Wonsik Kim | c8f7c35 | 2016-10-11 17:10:36 +0900 | [diff] [blame] | 344 | sp<MediaCodecBuffer> mcBuffer = new MediaCodecBuffer(nullptr, buffer); |
| 345 | mcBuffer->meta()->setInt64("timeUs", timeUs); |
| 346 | |
| 347 | mRenderer->queueBuffer(true /* audio */, mcBuffer, reply); |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 348 | |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 349 | ++mPendingBuffersToDrain; |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 350 | ALOGV("onInputBufferFilled: #ToDrain = %zu, cachedBytes = %zu", |
| 351 | mPendingBuffersToDrain, mCachedBytes); |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Chong Zhang | de01afb | 2014-08-13 13:48:10 -0700 | [diff] [blame] | 354 | void NuPlayer::DecoderPassThrough::onBufferConsumed(int32_t size) { |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 355 | --mPendingBuffersToDrain; |
Chong Zhang | de01afb | 2014-08-13 13:48:10 -0700 | [diff] [blame] | 356 | mCachedBytes -= size; |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 357 | ALOGV("onBufferConsumed: #ToDrain = %zu, cachedBytes = %zu", |
| 358 | mPendingBuffersToDrain, mCachedBytes); |
| 359 | onRequestInputBuffers(); |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Chong Zhang | f8d7177 | 2014-11-26 15:08:34 -0800 | [diff] [blame] | 362 | void NuPlayer::DecoderPassThrough::onResume(bool notifyComplete) { |
Ronghua Wu | f182891 | 2014-12-01 15:33:36 -0800 | [diff] [blame] | 363 | mPaused = false; |
| 364 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 365 | onRequestInputBuffers(); |
Chong Zhang | f8d7177 | 2014-11-26 15:08:34 -0800 | [diff] [blame] | 366 | |
| 367 | if (notifyComplete) { |
| 368 | sp<AMessage> notify = mNotify->dup(); |
| 369 | notify->setInt32("what", kWhatResumeCompleted); |
| 370 | notify->post(); |
| 371 | } |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 374 | void NuPlayer::DecoderPassThrough::doFlush(bool notifyComplete) { |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 375 | ++mBufferGeneration; |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 376 | mSkipRenderingUntilMediaTimeUs = -1; |
Ronghua Wu | f182891 | 2014-12-01 15:33:36 -0800 | [diff] [blame] | 377 | mPendingAudioAccessUnit.clear(); |
| 378 | mPendingAudioErr = OK; |
| 379 | mAggregateBuffer.clear(); |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 380 | |
| 381 | if (mRenderer != NULL) { |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 382 | mRenderer->flush(true /* audio */, notifyComplete); |
| 383 | mRenderer->signalTimeDiscontinuity(); |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 384 | } |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 385 | |
Phil Burk | c5cc2e2 | 2014-09-09 20:08:39 -0700 | [diff] [blame] | 386 | mPendingBuffersToDrain = 0; |
Chong Zhang | de01afb | 2014-08-13 13:48:10 -0700 | [diff] [blame] | 387 | mCachedBytes = 0; |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 388 | mReachedEOS = false; |
| 389 | } |
| 390 | |
Chong Zhang | 66704af | 2015-03-03 19:32:35 -0800 | [diff] [blame] | 391 | void NuPlayer::DecoderPassThrough::onFlush() { |
| 392 | doFlush(true /* notifyComplete */); |
| 393 | |
| 394 | mPaused = true; |
| 395 | sp<AMessage> notify = mNotify->dup(); |
| 396 | notify->setInt32("what", kWhatFlushCompleted); |
| 397 | notify->post(); |
| 398 | |
| 399 | } |
| 400 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 401 | void NuPlayer::DecoderPassThrough::onShutdown(bool notifyComplete) { |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 402 | ++mBufferGeneration; |
Wei Jia | c6cfd70 | 2014-11-11 16:33:20 -0800 | [diff] [blame] | 403 | mSkipRenderingUntilMediaTimeUs = -1; |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 404 | |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 405 | if (notifyComplete) { |
| 406 | sp<AMessage> notify = mNotify->dup(); |
| 407 | notify->setInt32("what", kWhatShutdownCompleted); |
| 408 | notify->post(); |
| 409 | } |
| 410 | |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 411 | mReachedEOS = true; |
| 412 | } |
| 413 | |
| 414 | void NuPlayer::DecoderPassThrough::onMessageReceived(const sp<AMessage> &msg) { |
| 415 | ALOGV("[%s] onMessage: %s", mComponentName.c_str(), |
| 416 | msg->debugString().c_str()); |
| 417 | |
| 418 | switch (msg->what()) { |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 419 | case kWhatBufferConsumed: |
| 420 | { |
| 421 | if (!isStaleReply(msg)) { |
Chong Zhang | de01afb | 2014-08-13 13:48:10 -0700 | [diff] [blame] | 422 | int32_t size; |
| 423 | CHECK(msg->findInt32("size", &size)); |
| 424 | onBufferConsumed(size); |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 425 | } |
| 426 | break; |
| 427 | } |
| 428 | |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 429 | default: |
Chong Zhang | 7137ec7 | 2014-11-12 16:41:05 -0800 | [diff] [blame] | 430 | DecoderBase::onMessageReceived(msg); |
Wei Jia | bc2fb72 | 2014-07-08 16:37:57 -0700 | [diff] [blame] | 431 | break; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | } // namespace android |