Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 "NuPlayerRenderer" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include "NuPlayerRenderer.h" |
| 22 | |
| 23 | #include <media/stagefright/foundation/ABuffer.h> |
| 24 | #include <media/stagefright/foundation/ADebug.h> |
Andreas Huber | 54e6649 | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 25 | #include <media/stagefright/foundation/AMessage.h> |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
Andreas Huber | fa6dfdc | 2011-09-13 08:28:38 -0700 | [diff] [blame] | 29 | // static |
| 30 | const int64_t NuPlayer::Renderer::kMinPositionUpdateDelayUs = 100000ll; |
| 31 | |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 32 | NuPlayer::Renderer::Renderer( |
| 33 | const sp<MediaPlayerBase::AudioSink> &sink, |
| 34 | const sp<AMessage> ¬ify) |
| 35 | : mAudioSink(sink), |
| 36 | mNotify(notify), |
| 37 | mNumFramesWritten(0), |
| 38 | mDrainAudioQueuePending(false), |
| 39 | mDrainVideoQueuePending(false), |
| 40 | mAudioQueueGeneration(0), |
| 41 | mVideoQueueGeneration(0), |
| 42 | mAnchorTimeMediaUs(-1), |
| 43 | mAnchorTimeRealUs(-1), |
| 44 | mFlushingAudio(false), |
| 45 | mFlushingVideo(false), |
Andreas Huber | 19922ad | 2011-01-21 10:15:23 -0800 | [diff] [blame] | 46 | mHasAudio(false), |
| 47 | mHasVideo(false), |
| 48 | mSyncQueues(false), |
Andreas Huber | fa6dfdc | 2011-09-13 08:28:38 -0700 | [diff] [blame] | 49 | mPaused(false), |
Andreas Huber | 950fc9d | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 50 | mLastPositionUpdateUs(-1ll), |
| 51 | mVideoLateByUs(0ll) { |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | NuPlayer::Renderer::~Renderer() { |
| 55 | } |
| 56 | |
| 57 | void NuPlayer::Renderer::queueBuffer( |
| 58 | bool audio, |
| 59 | const sp<ABuffer> &buffer, |
| 60 | const sp<AMessage> ¬ifyConsumed) { |
| 61 | sp<AMessage> msg = new AMessage(kWhatQueueBuffer, id()); |
| 62 | msg->setInt32("audio", static_cast<int32_t>(audio)); |
| 63 | msg->setObject("buffer", buffer); |
| 64 | msg->setMessage("notifyConsumed", notifyConsumed); |
| 65 | msg->post(); |
| 66 | } |
| 67 | |
| 68 | void NuPlayer::Renderer::queueEOS(bool audio, status_t finalResult) { |
| 69 | CHECK_NE(finalResult, (status_t)OK); |
| 70 | |
| 71 | sp<AMessage> msg = new AMessage(kWhatQueueEOS, id()); |
| 72 | msg->setInt32("audio", static_cast<int32_t>(audio)); |
| 73 | msg->setInt32("finalResult", finalResult); |
| 74 | msg->post(); |
| 75 | } |
| 76 | |
| 77 | void NuPlayer::Renderer::flush(bool audio) { |
| 78 | { |
| 79 | Mutex::Autolock autoLock(mFlushLock); |
| 80 | if (audio) { |
| 81 | CHECK(!mFlushingAudio); |
| 82 | mFlushingAudio = true; |
| 83 | } else { |
| 84 | CHECK(!mFlushingVideo); |
| 85 | mFlushingVideo = true; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | sp<AMessage> msg = new AMessage(kWhatFlush, id()); |
| 90 | msg->setInt32("audio", static_cast<int32_t>(audio)); |
| 91 | msg->post(); |
| 92 | } |
| 93 | |
| 94 | void NuPlayer::Renderer::signalTimeDiscontinuity() { |
| 95 | CHECK(mAudioQueue.empty()); |
| 96 | CHECK(mVideoQueue.empty()); |
| 97 | mAnchorTimeMediaUs = -1; |
| 98 | mAnchorTimeRealUs = -1; |
Andreas Huber | 41c3f74 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 99 | mSyncQueues = mHasAudio && mHasVideo; |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Andreas Huber | 601fe0e | 2011-01-20 15:23:04 -0800 | [diff] [blame] | 102 | void NuPlayer::Renderer::pause() { |
| 103 | (new AMessage(kWhatPause, id()))->post(); |
| 104 | } |
| 105 | |
| 106 | void NuPlayer::Renderer::resume() { |
| 107 | (new AMessage(kWhatResume, id()))->post(); |
| 108 | } |
| 109 | |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 110 | void NuPlayer::Renderer::onMessageReceived(const sp<AMessage> &msg) { |
| 111 | switch (msg->what()) { |
| 112 | case kWhatDrainAudioQueue: |
| 113 | { |
| 114 | int32_t generation; |
| 115 | CHECK(msg->findInt32("generation", &generation)); |
| 116 | if (generation != mAudioQueueGeneration) { |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | mDrainAudioQueuePending = false; |
| 121 | |
Andreas Huber | 115cac8 | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 122 | if (onDrainAudioQueue()) { |
| 123 | uint32_t numFramesPlayed; |
| 124 | CHECK_EQ(mAudioSink->getPosition(&numFramesPlayed), |
| 125 | (status_t)OK); |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 126 | |
Andreas Huber | 115cac8 | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 127 | uint32_t numFramesPendingPlayout = |
| 128 | mNumFramesWritten - numFramesPlayed; |
| 129 | |
| 130 | // This is how long the audio sink will have data to |
| 131 | // play back. |
| 132 | int64_t delayUs = |
| 133 | mAudioSink->msecsPerFrame() |
| 134 | * numFramesPendingPlayout * 1000ll; |
| 135 | |
| 136 | // Let's give it more data after about half that time |
| 137 | // has elapsed. |
| 138 | postDrainAudioQueue(delayUs / 2); |
| 139 | } |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 140 | break; |
| 141 | } |
| 142 | |
| 143 | case kWhatDrainVideoQueue: |
| 144 | { |
| 145 | int32_t generation; |
| 146 | CHECK(msg->findInt32("generation", &generation)); |
| 147 | if (generation != mVideoQueueGeneration) { |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | mDrainVideoQueuePending = false; |
| 152 | |
| 153 | onDrainVideoQueue(); |
| 154 | |
| 155 | postDrainVideoQueue(); |
| 156 | break; |
| 157 | } |
| 158 | |
| 159 | case kWhatQueueBuffer: |
| 160 | { |
| 161 | onQueueBuffer(msg); |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | case kWhatQueueEOS: |
| 166 | { |
| 167 | onQueueEOS(msg); |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | case kWhatFlush: |
| 172 | { |
| 173 | onFlush(msg); |
| 174 | break; |
| 175 | } |
| 176 | |
Andreas Huber | 41c3f74 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 177 | case kWhatAudioSinkChanged: |
| 178 | { |
| 179 | onAudioSinkChanged(); |
| 180 | break; |
| 181 | } |
| 182 | |
Andreas Huber | 601fe0e | 2011-01-20 15:23:04 -0800 | [diff] [blame] | 183 | case kWhatPause: |
| 184 | { |
| 185 | onPause(); |
| 186 | break; |
| 187 | } |
| 188 | |
| 189 | case kWhatResume: |
| 190 | { |
| 191 | onResume(); |
| 192 | break; |
| 193 | } |
| 194 | |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 195 | default: |
| 196 | TRESPASS(); |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
Andreas Huber | 115cac8 | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 201 | void NuPlayer::Renderer::postDrainAudioQueue(int64_t delayUs) { |
Andreas Huber | 601fe0e | 2011-01-20 15:23:04 -0800 | [diff] [blame] | 202 | if (mDrainAudioQueuePending || mSyncQueues || mPaused) { |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | |
| 206 | if (mAudioQueue.empty()) { |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | mDrainAudioQueuePending = true; |
| 211 | sp<AMessage> msg = new AMessage(kWhatDrainAudioQueue, id()); |
| 212 | msg->setInt32("generation", mAudioQueueGeneration); |
Andreas Huber | 115cac8 | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 213 | msg->post(delayUs); |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Andreas Huber | 41c3f74 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 216 | void NuPlayer::Renderer::signalAudioSinkChanged() { |
| 217 | (new AMessage(kWhatAudioSinkChanged, id()))->post(); |
| 218 | } |
| 219 | |
Andreas Huber | 115cac8 | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 220 | bool NuPlayer::Renderer::onDrainAudioQueue() { |
| 221 | uint32_t numFramesPlayed; |
Andreas Huber | a6be6dc | 2011-10-11 15:24:07 -0700 | [diff] [blame] | 222 | if (mAudioSink->getPosition(&numFramesPlayed) != OK) { |
| 223 | return false; |
| 224 | } |
Andreas Huber | d84fd79 | 2011-08-16 13:48:44 -0700 | [diff] [blame] | 225 | |
Andreas Huber | 115cac8 | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 226 | ssize_t numFramesAvailableToWrite = |
| 227 | mAudioSink->frameCount() - (mNumFramesWritten - numFramesPlayed); |
| 228 | |
| 229 | #if 0 |
| 230 | if (numFramesAvailableToWrite == mAudioSink->frameCount()) { |
| 231 | LOGI("audio sink underrun"); |
| 232 | } else { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame^] | 233 | ALOGV("audio queue has %d frames left to play", |
Andreas Huber | 115cac8 | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 234 | mAudioSink->frameCount() - numFramesAvailableToWrite); |
| 235 | } |
| 236 | #endif |
| 237 | |
| 238 | size_t numBytesAvailableToWrite = |
| 239 | numFramesAvailableToWrite * mAudioSink->frameSize(); |
| 240 | |
| 241 | while (numBytesAvailableToWrite > 0 && !mAudioQueue.empty()) { |
Andreas Huber | d84fd79 | 2011-08-16 13:48:44 -0700 | [diff] [blame] | 242 | QueueEntry *entry = &*mAudioQueue.begin(); |
| 243 | |
| 244 | if (entry->mBuffer == NULL) { |
| 245 | // EOS |
| 246 | |
| 247 | notifyEOS(true /* audio */, entry->mFinalResult); |
| 248 | |
| 249 | mAudioQueue.erase(mAudioQueue.begin()); |
| 250 | entry = NULL; |
Andreas Huber | 115cac8 | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 251 | return false; |
Eric Laurent | 6667ac3 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 254 | if (entry->mOffset == 0) { |
| 255 | int64_t mediaTimeUs; |
| 256 | CHECK(entry->mBuffer->meta()->findInt64("timeUs", &mediaTimeUs)); |
| 257 | |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame^] | 258 | ALOGV("rendering audio at media time %.2f secs", mediaTimeUs / 1E6); |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 259 | |
| 260 | mAnchorTimeMediaUs = mediaTimeUs; |
| 261 | |
| 262 | uint32_t numFramesPlayed; |
| 263 | CHECK_EQ(mAudioSink->getPosition(&numFramesPlayed), (status_t)OK); |
| 264 | |
| 265 | uint32_t numFramesPendingPlayout = |
| 266 | mNumFramesWritten - numFramesPlayed; |
| 267 | |
| 268 | int64_t realTimeOffsetUs = |
| 269 | (mAudioSink->latency() / 2 /* XXX */ |
| 270 | + numFramesPendingPlayout |
| 271 | * mAudioSink->msecsPerFrame()) * 1000ll; |
| 272 | |
| 273 | // LOGI("realTimeOffsetUs = %lld us", realTimeOffsetUs); |
| 274 | |
| 275 | mAnchorTimeRealUs = |
| 276 | ALooper::GetNowUs() + realTimeOffsetUs; |
| 277 | } |
| 278 | |
| 279 | size_t copy = entry->mBuffer->size() - entry->mOffset; |
| 280 | if (copy > numBytesAvailableToWrite) { |
| 281 | copy = numBytesAvailableToWrite; |
| 282 | } |
| 283 | |
| 284 | CHECK_EQ(mAudioSink->write( |
| 285 | entry->mBuffer->data() + entry->mOffset, copy), |
| 286 | (ssize_t)copy); |
| 287 | |
| 288 | entry->mOffset += copy; |
| 289 | if (entry->mOffset == entry->mBuffer->size()) { |
| 290 | entry->mNotifyConsumed->post(); |
| 291 | mAudioQueue.erase(mAudioQueue.begin()); |
Eric Laurent | 6667ac3 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 292 | |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 293 | entry = NULL; |
| 294 | } |
| 295 | |
Andreas Huber | 115cac8 | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 296 | numBytesAvailableToWrite -= copy; |
| 297 | size_t copiedFrames = copy / mAudioSink->frameSize(); |
| 298 | mNumFramesWritten += copiedFrames; |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 299 | } |
Andreas Huber | 08e10cb | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 300 | |
| 301 | notifyPosition(); |
Andreas Huber | 115cac8 | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 302 | |
| 303 | return !mAudioQueue.empty(); |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void NuPlayer::Renderer::postDrainVideoQueue() { |
Andreas Huber | 601fe0e | 2011-01-20 15:23:04 -0800 | [diff] [blame] | 307 | if (mDrainVideoQueuePending || mSyncQueues || mPaused) { |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 308 | return; |
| 309 | } |
| 310 | |
| 311 | if (mVideoQueue.empty()) { |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | QueueEntry &entry = *mVideoQueue.begin(); |
| 316 | |
| 317 | sp<AMessage> msg = new AMessage(kWhatDrainVideoQueue, id()); |
| 318 | msg->setInt32("generation", mVideoQueueGeneration); |
| 319 | |
| 320 | int64_t delayUs; |
| 321 | |
| 322 | if (entry.mBuffer == NULL) { |
| 323 | // EOS doesn't carry a timestamp. |
| 324 | delayUs = 0; |
| 325 | } else { |
| 326 | int64_t mediaTimeUs; |
| 327 | CHECK(entry.mBuffer->meta()->findInt64("timeUs", &mediaTimeUs)); |
| 328 | |
| 329 | if (mAnchorTimeMediaUs < 0) { |
| 330 | delayUs = 0; |
| 331 | |
Andreas Huber | 41c3f74 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 332 | if (!mHasAudio) { |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 333 | mAnchorTimeMediaUs = mediaTimeUs; |
| 334 | mAnchorTimeRealUs = ALooper::GetNowUs(); |
| 335 | } |
| 336 | } else { |
| 337 | int64_t realTimeUs = |
| 338 | (mediaTimeUs - mAnchorTimeMediaUs) + mAnchorTimeRealUs; |
| 339 | |
| 340 | delayUs = realTimeUs - ALooper::GetNowUs(); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | msg->post(delayUs); |
| 345 | |
| 346 | mDrainVideoQueuePending = true; |
| 347 | } |
| 348 | |
| 349 | void NuPlayer::Renderer::onDrainVideoQueue() { |
| 350 | if (mVideoQueue.empty()) { |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | QueueEntry *entry = &*mVideoQueue.begin(); |
| 355 | |
| 356 | if (entry->mBuffer == NULL) { |
| 357 | // EOS |
| 358 | |
Andreas Huber | d84fd79 | 2011-08-16 13:48:44 -0700 | [diff] [blame] | 359 | notifyEOS(false /* audio */, entry->mFinalResult); |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 360 | |
| 361 | mVideoQueue.erase(mVideoQueue.begin()); |
| 362 | entry = NULL; |
Andreas Huber | 950fc9d | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 363 | |
| 364 | mVideoLateByUs = 0ll; |
| 365 | |
| 366 | notifyPosition(); |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 367 | return; |
| 368 | } |
| 369 | |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 370 | int64_t mediaTimeUs; |
| 371 | CHECK(entry->mBuffer->meta()->findInt64("timeUs", &mediaTimeUs)); |
| 372 | |
Andreas Huber | 115cac8 | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 373 | int64_t realTimeUs = mediaTimeUs - mAnchorTimeMediaUs + mAnchorTimeRealUs; |
Andreas Huber | 950fc9d | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 374 | mVideoLateByUs = ALooper::GetNowUs() - realTimeUs; |
Andreas Huber | 115cac8 | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 375 | |
Andreas Huber | 950fc9d | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 376 | bool tooLate = (mVideoLateByUs > 40000); |
| 377 | |
| 378 | if (tooLate) { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame^] | 379 | ALOGV("video late by %lld us (%.2f secs)", lateByUs, lateByUs / 1E6); |
Andreas Huber | 115cac8 | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 380 | } else { |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame^] | 381 | ALOGV("rendering video at media time %.2f secs", mediaTimeUs / 1E6); |
Andreas Huber | 115cac8 | 2011-09-15 12:25:04 -0700 | [diff] [blame] | 382 | } |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 383 | |
Glenn Kasten | f330986 | 2011-11-04 18:05:35 -0700 | [diff] [blame] | 384 | entry->mNotifyConsumed->setInt32("render", !tooLate); |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 385 | entry->mNotifyConsumed->post(); |
| 386 | mVideoQueue.erase(mVideoQueue.begin()); |
| 387 | entry = NULL; |
Andreas Huber | 08e10cb | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 388 | |
| 389 | notifyPosition(); |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Andreas Huber | d84fd79 | 2011-08-16 13:48:44 -0700 | [diff] [blame] | 392 | void NuPlayer::Renderer::notifyEOS(bool audio, status_t finalResult) { |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 393 | sp<AMessage> notify = mNotify->dup(); |
| 394 | notify->setInt32("what", kWhatEOS); |
| 395 | notify->setInt32("audio", static_cast<int32_t>(audio)); |
Andreas Huber | d84fd79 | 2011-08-16 13:48:44 -0700 | [diff] [blame] | 396 | notify->setInt32("finalResult", finalResult); |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 397 | notify->post(); |
| 398 | } |
| 399 | |
| 400 | void NuPlayer::Renderer::onQueueBuffer(const sp<AMessage> &msg) { |
| 401 | int32_t audio; |
| 402 | CHECK(msg->findInt32("audio", &audio)); |
| 403 | |
Andreas Huber | 19922ad | 2011-01-21 10:15:23 -0800 | [diff] [blame] | 404 | if (audio) { |
| 405 | mHasAudio = true; |
| 406 | } else { |
| 407 | mHasVideo = true; |
| 408 | } |
| 409 | |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 410 | if (dropBufferWhileFlushing(audio, msg)) { |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | sp<RefBase> obj; |
| 415 | CHECK(msg->findObject("buffer", &obj)); |
| 416 | sp<ABuffer> buffer = static_cast<ABuffer *>(obj.get()); |
| 417 | |
| 418 | sp<AMessage> notifyConsumed; |
| 419 | CHECK(msg->findMessage("notifyConsumed", ¬ifyConsumed)); |
| 420 | |
| 421 | QueueEntry entry; |
| 422 | entry.mBuffer = buffer; |
| 423 | entry.mNotifyConsumed = notifyConsumed; |
| 424 | entry.mOffset = 0; |
| 425 | entry.mFinalResult = OK; |
| 426 | |
| 427 | if (audio) { |
| 428 | mAudioQueue.push_back(entry); |
| 429 | postDrainAudioQueue(); |
| 430 | } else { |
| 431 | mVideoQueue.push_back(entry); |
| 432 | postDrainVideoQueue(); |
| 433 | } |
| 434 | |
Andreas Huber | 6032a60 | 2011-08-26 16:02:19 -0700 | [diff] [blame] | 435 | if (!mSyncQueues || mAudioQueue.empty() || mVideoQueue.empty()) { |
| 436 | return; |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 437 | } |
Andreas Huber | 6032a60 | 2011-08-26 16:02:19 -0700 | [diff] [blame] | 438 | |
| 439 | sp<ABuffer> firstAudioBuffer = (*mAudioQueue.begin()).mBuffer; |
| 440 | sp<ABuffer> firstVideoBuffer = (*mVideoQueue.begin()).mBuffer; |
| 441 | |
| 442 | if (firstAudioBuffer == NULL || firstVideoBuffer == NULL) { |
| 443 | // EOS signalled on either queue. |
| 444 | syncQueuesDone(); |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | int64_t firstAudioTimeUs; |
| 449 | int64_t firstVideoTimeUs; |
| 450 | CHECK(firstAudioBuffer->meta() |
| 451 | ->findInt64("timeUs", &firstAudioTimeUs)); |
| 452 | CHECK(firstVideoBuffer->meta() |
| 453 | ->findInt64("timeUs", &firstVideoTimeUs)); |
| 454 | |
| 455 | int64_t diff = firstVideoTimeUs - firstAudioTimeUs; |
| 456 | |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame^] | 457 | ALOGV("queueDiff = %.2f secs", diff / 1E6); |
Andreas Huber | 6032a60 | 2011-08-26 16:02:19 -0700 | [diff] [blame] | 458 | |
| 459 | if (diff > 100000ll) { |
| 460 | // Audio data starts More than 0.1 secs before video. |
| 461 | // Drop some audio. |
| 462 | |
| 463 | (*mAudioQueue.begin()).mNotifyConsumed->post(); |
| 464 | mAudioQueue.erase(mAudioQueue.begin()); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | syncQueuesDone(); |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void NuPlayer::Renderer::syncQueuesDone() { |
| 472 | if (!mSyncQueues) { |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | mSyncQueues = false; |
| 477 | |
| 478 | if (!mAudioQueue.empty()) { |
| 479 | postDrainAudioQueue(); |
| 480 | } |
| 481 | |
| 482 | if (!mVideoQueue.empty()) { |
| 483 | postDrainVideoQueue(); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | void NuPlayer::Renderer::onQueueEOS(const sp<AMessage> &msg) { |
| 488 | int32_t audio; |
| 489 | CHECK(msg->findInt32("audio", &audio)); |
| 490 | |
| 491 | if (dropBufferWhileFlushing(audio, msg)) { |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | int32_t finalResult; |
| 496 | CHECK(msg->findInt32("finalResult", &finalResult)); |
| 497 | |
| 498 | QueueEntry entry; |
| 499 | entry.mOffset = 0; |
| 500 | entry.mFinalResult = finalResult; |
| 501 | |
| 502 | if (audio) { |
| 503 | mAudioQueue.push_back(entry); |
| 504 | postDrainAudioQueue(); |
| 505 | } else { |
| 506 | mVideoQueue.push_back(entry); |
| 507 | postDrainVideoQueue(); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | void NuPlayer::Renderer::onFlush(const sp<AMessage> &msg) { |
| 512 | int32_t audio; |
| 513 | CHECK(msg->findInt32("audio", &audio)); |
| 514 | |
| 515 | // If we're currently syncing the queues, i.e. dropping audio while |
| 516 | // aligning the first audio/video buffer times and only one of the |
| 517 | // two queues has data, we may starve that queue by not requesting |
| 518 | // more buffers from the decoder. If the other source then encounters |
| 519 | // a discontinuity that leads to flushing, we'll never find the |
| 520 | // corresponding discontinuity on the other queue. |
| 521 | // Therefore we'll stop syncing the queues if at least one of them |
| 522 | // is flushed. |
| 523 | syncQueuesDone(); |
| 524 | |
| 525 | if (audio) { |
| 526 | flushQueue(&mAudioQueue); |
| 527 | |
| 528 | Mutex::Autolock autoLock(mFlushLock); |
| 529 | mFlushingAudio = false; |
| 530 | |
| 531 | mDrainAudioQueuePending = false; |
| 532 | ++mAudioQueueGeneration; |
| 533 | } else { |
| 534 | flushQueue(&mVideoQueue); |
| 535 | |
| 536 | Mutex::Autolock autoLock(mFlushLock); |
| 537 | mFlushingVideo = false; |
| 538 | |
| 539 | mDrainVideoQueuePending = false; |
| 540 | ++mVideoQueueGeneration; |
| 541 | } |
| 542 | |
| 543 | notifyFlushComplete(audio); |
| 544 | } |
| 545 | |
| 546 | void NuPlayer::Renderer::flushQueue(List<QueueEntry> *queue) { |
| 547 | while (!queue->empty()) { |
| 548 | QueueEntry *entry = &*queue->begin(); |
| 549 | |
| 550 | if (entry->mBuffer != NULL) { |
| 551 | entry->mNotifyConsumed->post(); |
| 552 | } |
| 553 | |
| 554 | queue->erase(queue->begin()); |
| 555 | entry = NULL; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | void NuPlayer::Renderer::notifyFlushComplete(bool audio) { |
| 560 | sp<AMessage> notify = mNotify->dup(); |
| 561 | notify->setInt32("what", kWhatFlushComplete); |
| 562 | notify->setInt32("audio", static_cast<int32_t>(audio)); |
| 563 | notify->post(); |
| 564 | } |
| 565 | |
| 566 | bool NuPlayer::Renderer::dropBufferWhileFlushing( |
| 567 | bool audio, const sp<AMessage> &msg) { |
| 568 | bool flushing = false; |
| 569 | |
| 570 | { |
| 571 | Mutex::Autolock autoLock(mFlushLock); |
| 572 | if (audio) { |
| 573 | flushing = mFlushingAudio; |
| 574 | } else { |
| 575 | flushing = mFlushingVideo; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | if (!flushing) { |
| 580 | return false; |
| 581 | } |
| 582 | |
| 583 | sp<AMessage> notifyConsumed; |
| 584 | if (msg->findMessage("notifyConsumed", ¬ifyConsumed)) { |
| 585 | notifyConsumed->post(); |
| 586 | } |
| 587 | |
| 588 | return true; |
| 589 | } |
| 590 | |
Andreas Huber | 41c3f74 | 2010-12-21 10:22:33 -0800 | [diff] [blame] | 591 | void NuPlayer::Renderer::onAudioSinkChanged() { |
| 592 | CHECK(!mDrainAudioQueuePending); |
| 593 | mNumFramesWritten = 0; |
| 594 | } |
| 595 | |
Andreas Huber | 08e10cb | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 596 | void NuPlayer::Renderer::notifyPosition() { |
| 597 | if (mAnchorTimeRealUs < 0 || mAnchorTimeMediaUs < 0) { |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | int64_t nowUs = ALooper::GetNowUs(); |
Andreas Huber | fa6dfdc | 2011-09-13 08:28:38 -0700 | [diff] [blame] | 602 | |
| 603 | if (mLastPositionUpdateUs >= 0 |
| 604 | && nowUs < mLastPositionUpdateUs + kMinPositionUpdateDelayUs) { |
| 605 | return; |
| 606 | } |
| 607 | mLastPositionUpdateUs = nowUs; |
| 608 | |
Andreas Huber | 08e10cb | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 609 | int64_t positionUs = (nowUs - mAnchorTimeRealUs) + mAnchorTimeMediaUs; |
| 610 | |
| 611 | sp<AMessage> notify = mNotify->dup(); |
| 612 | notify->setInt32("what", kWhatPosition); |
| 613 | notify->setInt64("positionUs", positionUs); |
Andreas Huber | 950fc9d | 2011-09-16 15:09:22 -0700 | [diff] [blame] | 614 | notify->setInt64("videoLateByUs", mVideoLateByUs); |
Andreas Huber | 08e10cb | 2011-01-05 12:17:08 -0800 | [diff] [blame] | 615 | notify->post(); |
| 616 | } |
| 617 | |
Andreas Huber | 601fe0e | 2011-01-20 15:23:04 -0800 | [diff] [blame] | 618 | void NuPlayer::Renderer::onPause() { |
| 619 | CHECK(!mPaused); |
| 620 | |
| 621 | mDrainAudioQueuePending = false; |
| 622 | ++mAudioQueueGeneration; |
| 623 | |
| 624 | mDrainVideoQueuePending = false; |
| 625 | ++mVideoQueueGeneration; |
| 626 | |
| 627 | if (mHasAudio) { |
| 628 | mAudioSink->pause(); |
| 629 | } |
| 630 | |
Steve Block | 06ade6a | 2011-10-20 11:56:00 +0100 | [diff] [blame^] | 631 | ALOGV("now paused audio queue has %d entries, video has %d entries", |
Andreas Huber | 551aeac | 2011-11-28 16:27:35 -0800 | [diff] [blame] | 632 | mAudioQueue.size(), mVideoQueue.size()); |
| 633 | |
Andreas Huber | 601fe0e | 2011-01-20 15:23:04 -0800 | [diff] [blame] | 634 | mPaused = true; |
| 635 | } |
| 636 | |
| 637 | void NuPlayer::Renderer::onResume() { |
Andreas Huber | 551aeac | 2011-11-28 16:27:35 -0800 | [diff] [blame] | 638 | if (!mPaused) { |
| 639 | return; |
| 640 | } |
Andreas Huber | 601fe0e | 2011-01-20 15:23:04 -0800 | [diff] [blame] | 641 | |
| 642 | if (mHasAudio) { |
| 643 | mAudioSink->start(); |
| 644 | } |
| 645 | |
| 646 | mPaused = false; |
| 647 | |
| 648 | if (!mAudioQueue.empty()) { |
| 649 | postDrainAudioQueue(); |
| 650 | } |
| 651 | |
| 652 | if (!mVideoQueue.empty()) { |
| 653 | postDrainVideoQueue(); |
| 654 | } |
| 655 | } |
| 656 | |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 657 | } // namespace android |
| 658 | |