android_GenericPlayer.cpp revision 1fa5c3206d06bbebdea2dc92f378ce6b8a211e23
1/* 2 * Copyright (C) 2011 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 USE_LOG SLAndroidLogLevel_Verbose 18 19#include "sles_allinclusive.h" 20 21#include <media/stagefright/foundation/ADebug.h> 22#include <sys/stat.h> 23 24namespace android { 25 26//-------------------------------------------------------------------------------------------------- 27GenericPlayer::GenericPlayer(const AudioPlayback_Parameters* params) : 28 mDataLocatorType(kDataLocatorNone), 29 mNotifyClient(NULL), 30 mNotifyUser(NULL), 31 mStateFlags(0), 32 mPlaybackParams(*params), 33 mDurationMsec(ANDROID_UNKNOWN_TIME), 34 mCacheStatus(kStatusEmpty), 35 mCacheFill(0), 36 mLastNotifiedCacheFill(0), 37 mCacheFillNotifThreshold(100), 38 mEventFlags(0), 39 mMarkerPositionMs(ANDROID_UNKNOWN_TIME), 40 mPositionUpdatePeriodMs(1000), // per spec 41 mOneShotGeneration(0), 42 mDeliveredNewPosMs(ANDROID_UNKNOWN_TIME), 43 mObservedPositionMs(ANDROID_UNKNOWN_TIME) 44{ 45 SL_LOGD("GenericPlayer::GenericPlayer()"); 46 47 mLooper = new android::ALooper(); 48 49 mAndroidAudioLevels.mFinalVolume[0] = 1.0f; 50 mAndroidAudioLevels.mFinalVolume[1] = 1.0f; 51} 52 53 54GenericPlayer::~GenericPlayer() { 55 SL_LOGV("GenericPlayer::~GenericPlayer()"); 56 57 resetDataLocator(); 58} 59 60 61void GenericPlayer::init(const notif_cbf_t cbf, void* notifUser) { 62 SL_LOGD("GenericPlayer::init()"); 63 64 { 65 android::Mutex::Autolock autoLock(mNotifyClientLock); 66 mNotifyClient = cbf; 67 mNotifyUser = notifUser; 68 } 69 70 mLooper->registerHandler(this); 71 mLooper->start(false /*runOnCallingThread*/, false /*canCallJava*/, PRIORITY_DEFAULT); 72} 73 74 75void GenericPlayer::preDestroy() { 76 SL_LOGD("GenericPlayer::preDestroy()"); 77 { 78 android::Mutex::Autolock autoLock(mNotifyClientLock); 79 mNotifyClient = NULL; 80 mNotifyUser = NULL; 81 } 82 83 mLooper->stop(); 84 mLooper->unregisterHandler(id()); 85} 86 87 88void GenericPlayer::setDataSource(const char *uri) { 89 SL_LOGV("GenericPlayer::setDataSource(uri=%s)", uri); 90 resetDataLocator(); 91 92 mDataLocator.uriRef = uri; 93 94 mDataLocatorType = kDataLocatorUri; 95} 96 97 98void GenericPlayer::setDataSource(int fd, int64_t offset, int64_t length, bool closeAfterUse) { 99 SL_LOGV("GenericPlayer::setDataSource(fd=%d, offset=%lld, length=%lld, closeAfterUse=%s)", fd, 100 offset, length, closeAfterUse ? "true" : "false"); 101 resetDataLocator(); 102 103 mDataLocator.fdi.fd = fd; 104 105 struct stat sb; 106 int ret = fstat(fd, &sb); 107 if (ret != 0) { 108 SL_LOGE("GenericPlayer::setDataSource: fstat(%d) failed: %d, %s", fd, ret, strerror(errno)); 109 return; 110 } 111 112 if (offset >= sb.st_size) { 113 SL_LOGE("SfPlayer::setDataSource: invalid offset"); 114 return; 115 } 116 mDataLocator.fdi.offset = offset; 117 118 if (PLAYER_FD_FIND_FILE_SIZE == length) { 119 mDataLocator.fdi.length = sb.st_size; 120 } else if (offset + length > sb.st_size) { 121 mDataLocator.fdi.length = sb.st_size - offset; 122 } else { 123 mDataLocator.fdi.length = length; 124 } 125 126 mDataLocator.fdi.mCloseAfterUse = closeAfterUse; 127 128 mDataLocatorType = kDataLocatorFd; 129} 130 131 132void GenericPlayer::prepare() { 133 SL_LOGD("GenericPlayer::prepare()"); 134 // do not attempt prepare more than once 135 if (!(mStateFlags & (kFlagPrepared | kFlagPreparedUnsuccessfully))) { 136 sp<AMessage> msg = new AMessage(kWhatPrepare, id()); 137 msg->post(); 138 } 139} 140 141 142void GenericPlayer::play() { 143 SL_LOGD("GenericPlayer::play()"); 144 sp<AMessage> msg = new AMessage(kWhatPlay, id()); 145 msg->post(); 146} 147 148 149void GenericPlayer::pause() { 150 SL_LOGD("GenericPlayer::pause()"); 151 sp<AMessage> msg = new AMessage(kWhatPause, id()); 152 msg->post(); 153} 154 155 156void GenericPlayer::stop() { 157 SL_LOGD("GenericPlayer::stop()"); 158 (new AMessage(kWhatPause, id()))->post(); 159 160 // after a stop, playback should resume from the start. 161 seek(0); 162} 163 164 165void GenericPlayer::seek(int64_t timeMsec) { 166 SL_LOGV("GenericPlayer::seek %lld", timeMsec); 167 if (timeMsec < 0 && timeMsec != ANDROID_UNKNOWN_TIME) { 168 SL_LOGE("GenericPlayer::seek error, can't seek to negative time %lldms", timeMsec); 169 return; 170 } 171 sp<AMessage> msg = new AMessage(kWhatSeek, id()); 172 msg->setInt64(WHATPARAM_SEEK_SEEKTIME_MS, timeMsec); 173 msg->post(); 174} 175 176 177void GenericPlayer::loop(bool loop) { 178 SL_LOGV("GenericPlayer::loop %s", loop ? "true" : "false"); 179 sp<AMessage> msg = new AMessage(kWhatLoop, id()); 180 msg->setInt32(WHATPARAM_LOOP_LOOPING, (int32_t)loop); 181 msg->post(); 182} 183 184 185void GenericPlayer::setBufferingUpdateThreshold(int16_t thresholdPercent) { 186 SL_LOGV("GenericPlayer::setBufferingUpdateThreshold %d", thresholdPercent); 187 sp<AMessage> msg = new AMessage(kWhatBuffUpdateThres, id()); 188 msg->setInt32(WHATPARAM_BUFFERING_UPDATETHRESHOLD_PERCENT, (int32_t)thresholdPercent); 189 msg->post(); 190} 191 192 193//-------------------------------------------------- 194void GenericPlayer::getDurationMsec(int* msec) { 195 Mutex::Autolock _l(mSettingsLock); 196 *msec = mDurationMsec; 197} 198 199//-------------------------------------------------- 200void GenericPlayer::setVolume(float leftVol, float rightVol) 201{ 202 { 203 Mutex::Autolock _l(mSettingsLock); 204 mAndroidAudioLevels.mFinalVolume[0] = leftVol; 205 mAndroidAudioLevels.mFinalVolume[1] = rightVol; 206 } 207 // send a message for the volume to be updated by the object which implements the volume 208 (new AMessage(kWhatVolumeUpdate, id()))->post(); 209} 210 211 212//-------------------------------------------------- 213void GenericPlayer::attachAuxEffect(int32_t effectId) 214{ 215 SL_LOGV("GenericPlayer::attachAuxEffect(id=%d)", effectId); 216 sp<AMessage> msg = new AMessage(kWhatAttachAuxEffect, id()); 217 msg->setInt32(WHATPARAM_ATTACHAUXEFFECT, effectId); 218 msg->post(); 219} 220 221 222//-------------------------------------------------- 223void GenericPlayer::setAuxEffectSendLevel(float level) 224{ 225 SL_LOGV("GenericPlayer::setAuxEffectSendLevel(level=%g)", level); 226 sp<AMessage> msg = new AMessage(kWhatSetAuxEffectSendLevel, id()); 227 msg->setFloat(WHATPARAM_SETAUXEFFECTSENDLEVEL, level); 228 msg->post(); 229} 230 231 232//-------------------------------------------------- 233// Call after changing any of the IPlay settings related to SL_PLAYEVENT_* 234void GenericPlayer::setPlayEvents(int32_t eventFlags, int32_t markerPositionMs, 235 int32_t positionUpdatePeriodMs) 236{ 237 // Normalize ms that are within the valid unsigned range, but not in the int32_t range 238 if (markerPositionMs < 0) { 239 markerPositionMs = ANDROID_UNKNOWN_TIME; 240 } 241 if (positionUpdatePeriodMs < 0) { 242 positionUpdatePeriodMs = ANDROID_UNKNOWN_TIME; 243 } 244 // markers are delivered accurately, but new position updates are limited to every 100 ms 245 if (positionUpdatePeriodMs < 100) { 246 positionUpdatePeriodMs = 100; 247 } 248 sp<AMessage> msg = new AMessage(kWhatSetPlayEvents, id()); 249 msg->setInt32(WHATPARAM_SETPLAYEVENTS_FLAGS, eventFlags); 250 msg->setInt32(WHATPARAM_SETPLAYEVENTS_MARKER, markerPositionMs); 251 msg->setInt32(WHATPARAM_SETPLAYEVENTS_UPDATE, positionUpdatePeriodMs); 252 msg->post(); 253} 254 255 256//-------------------------------------------------- 257/* 258 * post-condition: mDataLocatorType == kDataLocatorNone 259 * 260 */ 261void GenericPlayer::resetDataLocator() { 262 SL_LOGV("GenericPlayer::resetDataLocator()"); 263 if (mDataLocatorType == kDataLocatorFd && mDataLocator.fdi.mCloseAfterUse) { 264 (void) ::close(mDataLocator.fdi.fd); 265 // would be redundant, as we're about to invalidate the union mDataLocator 266 //mDataLocator.fdi.fd = -1; 267 //mDataLocator.fdi.mCloseAfterUse = false; 268 } 269 mDataLocatorType = kDataLocatorNone; 270} 271 272 273void GenericPlayer::notify(const char* event, int data, bool async) { 274 SL_LOGV("GenericPlayer::notify(event=%s, data=%d, async=%s)", event, data, 275 async ? "true" : "false"); 276 sp<AMessage> msg = new AMessage(kWhatNotif, id()); 277 msg->setInt32(event, (int32_t)data); 278 if (async) { 279 msg->post(); 280 } else { 281 this->onNotify(msg); 282 } 283} 284 285 286void GenericPlayer::notify(const char* event, int data1, int data2, bool async) { 287 SL_LOGV("GenericPlayer::notify(event=%s, data1=%d, data2=%d, async=%s)", event, data1, data2, 288 async ? "true" : "false"); 289 sp<AMessage> msg = new AMessage(kWhatNotif, id()); 290 msg->setRect(event, 0, 0, (int32_t)data1, (int32_t)data2); 291 if (async) { 292 msg->post(); 293 } else { 294 this->onNotify(msg); 295 } 296} 297 298 299//-------------------------------------------------- 300// AHandler implementation 301void GenericPlayer::onMessageReceived(const sp<AMessage> &msg) { 302 SL_LOGV("GenericPlayer::onMessageReceived()"); 303 switch (msg->what()) { 304 case kWhatPrepare: 305 SL_LOGV("kWhatPrepare"); 306 onPrepare(); 307 break; 308 309 case kWhatNotif: 310 SL_LOGV("kWhatNotif"); 311 onNotify(msg); 312 break; 313 314 case kWhatPlay: 315 SL_LOGV("kWhatPlay"); 316 onPlay(); 317 break; 318 319 case kWhatPause: 320 SL_LOGV("kWhatPause"); 321 onPause(); 322 break; 323 324 case kWhatSeek: 325 SL_LOGV("kWhatSeek"); 326 onSeek(msg); 327 break; 328 329 case kWhatLoop: 330 SL_LOGV("kWhatLoop"); 331 onLoop(msg); 332 break; 333 334 case kWhatVolumeUpdate: 335 SL_LOGV("kWhatVolumeUpdate"); 336 onVolumeUpdate(); 337 break; 338 339 case kWhatSeekComplete: 340 SL_LOGV("kWhatSeekComplete"); 341 onSeekComplete(); 342 break; 343 344 case kWhatBufferingUpdate: 345 SL_LOGV("kWhatBufferingUpdate"); 346 onBufferingUpdate(msg); 347 break; 348 349 case kWhatBuffUpdateThres: 350 SL_LOGV("kWhatBuffUpdateThres"); 351 onSetBufferingUpdateThreshold(msg); 352 break; 353 354 case kWhatAttachAuxEffect: 355 SL_LOGV("kWhatAttachAuxEffect"); 356 onAttachAuxEffect(msg); 357 break; 358 359 case kWhatSetAuxEffectSendLevel: 360 SL_LOGV("kWhatSetAuxEffectSendLevel"); 361 onSetAuxEffectSendLevel(msg); 362 break; 363 364 case kWhatSetPlayEvents: 365 SL_LOGV("kWhatSetPlayEvents"); 366 onSetPlayEvents(msg); 367 break; 368 369 case kWhatOneShot: 370 SL_LOGV("kWhatOneShot"); 371 onOneShot(msg); 372 break; 373 374 default: 375 SL_LOGE("GenericPlayer::onMessageReceived unknown message %d", msg->what()); 376 TRESPASS(); 377 } 378} 379 380 381//-------------------------------------------------- 382// Event handlers 383// it is strictly verboten to call those methods outside of the event loop 384 385void GenericPlayer::onPrepare() { 386 SL_LOGV("GenericPlayer::onPrepare()"); 387 // Subclass is responsible for indicating whether prepare was successful or unsuccessful 388 // by updating mStateFlags accordingly. It must set exactly one of these two flags. 389 assert(!(mStateFlags & kFlagPrepared) != !(mStateFlags & kFlagPreparedUnsuccessfully)); 390 notify(PLAYEREVENT_PREPARED, mStateFlags & kFlagPrepared ? PLAYER_SUCCESS : PLAYER_FAILURE, 391 true /*async*/); 392 SL_LOGD("GenericPlayer::onPrepare() done, mStateFlags=0x%x", mStateFlags); 393} 394 395 396void GenericPlayer::onNotify(const sp<AMessage> &msg) { 397 SL_LOGV("GenericPlayer::onNotify()"); 398 notif_cbf_t notifClient; 399 void* notifUser; 400 { 401 android::Mutex::Autolock autoLock(mNotifyClientLock); 402 if (NULL == mNotifyClient) { 403 return; 404 } else { 405 notifClient = mNotifyClient; 406 notifUser = mNotifyUser; 407 } 408 } 409 410 int32_t val1, val2; 411 if (msg->findInt32(PLAYEREVENT_PREFETCHSTATUSCHANGE, &val1)) { 412 SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_PREFETCHSTATUSCHANGE, val1); 413 notifClient(kEventPrefetchStatusChange, val1, 0, notifUser); 414 } else if (msg->findInt32(PLAYEREVENT_PREFETCHFILLLEVELUPDATE, &val1)) { 415 SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_PREFETCHFILLLEVELUPDATE, val1); 416 notifClient(kEventPrefetchFillLevelUpdate, val1, 0, notifUser); 417 } else if (msg->findInt32(PLAYEREVENT_ENDOFSTREAM, &val1)) { 418 SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_ENDOFSTREAM, val1); 419 notifClient(kEventEndOfStream, val1, 0, notifUser); 420 } else if (msg->findInt32(PLAYEREVENT_PREPARED, &val1)) { 421 SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_PREPARED, val1); 422 notifClient(kEventPrepared, val1, 0, notifUser); 423 } else if (msg->findInt32(PLAYEREVENT_CHANNEL_COUNT, &val1)) { 424 SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_CHANNEL_COUNT, val1); 425 notifClient(kEventChannelCount, val1, 0, notifUser); 426 } else if (msg->findRect(PLAYEREVENT_VIDEO_SIZE_UPDATE, &val1, &val2, &val1, &val2)) { 427 SL_LOGV("GenericPlayer notifying %s = %d, %d", PLAYEREVENT_VIDEO_SIZE_UPDATE, val1, val2); 428 notifClient(kEventHasVideoSize, val1, val2, notifUser); 429 } else if (msg->findInt32(PLAYEREVENT_PLAY, &val1)) { 430 SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_PLAY, val1); 431 notifClient(kEventPlay, val1, 0, notifUser); 432 } else { 433 SL_LOGV("GenericPlayer notifying unknown"); 434 } 435} 436 437 438void GenericPlayer::onPlay() { 439 SL_LOGD("GenericPlayer::onPlay()"); 440 if ((mStateFlags & (kFlagPrepared | kFlagPlaying)) == kFlagPrepared) { 441 SL_LOGD("starting player"); 442 mStateFlags |= kFlagPlaying; 443 updateOneShot(); 444 } 445} 446 447 448void GenericPlayer::onPause() { 449 SL_LOGD("GenericPlayer::onPause()"); 450 if (!(~mStateFlags & (kFlagPrepared | kFlagPlaying))) { 451 SL_LOGV("pausing player"); 452 mStateFlags &= ~kFlagPlaying; 453 updateOneShot(); 454 } 455} 456 457 458void GenericPlayer::onSeek(const sp<AMessage> &msg) { 459 SL_LOGV("GenericPlayer::onSeek"); 460} 461 462 463void GenericPlayer::onLoop(const sp<AMessage> &msg) { 464 SL_LOGV("GenericPlayer::onLoop"); 465} 466 467 468void GenericPlayer::onVolumeUpdate() { 469 SL_LOGV("GenericPlayer::onVolumeUpdate"); 470} 471 472 473void GenericPlayer::onSeekComplete() { 474 SL_LOGD("GenericPlayer::onSeekComplete()"); 475 mStateFlags &= ~kFlagSeeking; 476 // avoid spurious or lost events caused by seeking past a marker 477 mDeliveredNewPosMs = ANDROID_UNKNOWN_TIME; 478 mObservedPositionMs = ANDROID_UNKNOWN_TIME; 479 updateOneShot(); 480} 481 482 483void GenericPlayer::onBufferingUpdate(const sp<AMessage> &msg) { 484 SL_LOGV("GenericPlayer::onBufferingUpdate"); 485} 486 487 488void GenericPlayer::onSetBufferingUpdateThreshold(const sp<AMessage> &msg) { 489 SL_LOGV("GenericPlayer::onSetBufferingUpdateThreshold"); 490 int32_t thresholdPercent = 0; 491 if (msg->findInt32(WHATPARAM_BUFFERING_UPDATETHRESHOLD_PERCENT, &thresholdPercent)) { 492 Mutex::Autolock _l(mSettingsLock); 493 mCacheFillNotifThreshold = (int16_t)thresholdPercent; 494 } 495} 496 497 498void GenericPlayer::onAttachAuxEffect(const sp<AMessage> &msg) { 499 SL_LOGV("GenericPlayer::onAttachAuxEffect()"); 500} 501 502 503void GenericPlayer::onSetAuxEffectSendLevel(const sp<AMessage> &msg) { 504 SL_LOGV("GenericPlayer::onSetAuxEffectSendLevel()"); 505} 506 507 508void GenericPlayer::onSetPlayEvents(const sp<AMessage> &msg) { 509 SL_LOGV("GenericPlayer::onSetPlayEvents()"); 510 int32_t eventFlags, markerPositionMs, positionUpdatePeriodMs; 511 if (msg->findInt32(WHATPARAM_SETPLAYEVENTS_FLAGS, &eventFlags) && 512 msg->findInt32(WHATPARAM_SETPLAYEVENTS_MARKER, &markerPositionMs) && 513 msg->findInt32(WHATPARAM_SETPLAYEVENTS_UPDATE, &positionUpdatePeriodMs)) { 514 mEventFlags = eventFlags; 515 mMarkerPositionMs = markerPositionMs; 516 mPositionUpdatePeriodMs = positionUpdatePeriodMs; 517 updateOneShot(); 518 } 519} 520 521 522void GenericPlayer::onOneShot(const sp<AMessage> &msg) { 523 SL_LOGV("GenericPlayer::onOneShot()"); 524 int32_t generation; 525 if (msg->findInt32(WHATPARAM_ONESHOT_GENERATION, &generation)) { 526 if (generation != mOneShotGeneration) { 527 SL_LOGV("GenericPlayer::onOneShot() generation %d cancelled; latest is %d", 528 generation, mOneShotGeneration); 529 return; 530 } 531 updateOneShot(); 532 } 533} 534 535 536//------------------------------------------------- 537void GenericPlayer::notifyStatus() { 538 SL_LOGV("GenericPlayer::notifyStatus"); 539 notify(PLAYEREVENT_PREFETCHSTATUSCHANGE, (int32_t)mCacheStatus, true /*async*/); 540} 541 542 543void GenericPlayer::notifyCacheFill() { 544 SL_LOGV("GenericPlayer::notifyCacheFill"); 545 mLastNotifiedCacheFill = mCacheFill; 546 notify(PLAYEREVENT_PREFETCHFILLLEVELUPDATE, (int32_t)mLastNotifiedCacheFill, true/*async*/); 547} 548 549 550void GenericPlayer::seekComplete() { 551 SL_LOGV("GenericPlayer::seekComplete"); 552 sp<AMessage> msg = new AMessage(kWhatSeekComplete, id()); 553 msg->post(); 554} 555 556 557void GenericPlayer::bufferingUpdate(int16_t fillLevelPerMille) { 558 SL_LOGV("GenericPlayer::bufferingUpdate"); 559 sp<AMessage> msg = new AMessage(kWhatBufferingUpdate, id()); 560 msg->setInt32(WHATPARAM_BUFFERING_UPDATE, fillLevelPerMille); 561 msg->post(); 562} 563 564 565// For the meaning of positionMs, see comment in declaration at android_GenericPlayer.h 566void GenericPlayer::updateOneShot(int positionMs) 567{ 568 SL_LOGV("GenericPlayer::updateOneShot"); 569 570 // nop until prepared 571 if (!(mStateFlags & kFlagPrepared)) { 572 return; 573 } 574 575 // cancel any pending one-shot(s) 576 ++mOneShotGeneration; 577 578 // don't restart one-shot if player is paused or stopped 579 if (!(mStateFlags & kFlagPlaying)) { 580 return; 581 } 582 583 // get current player position in milliseconds 584 if (positionMs < 0) { 585 positionMs = ANDROID_UNKNOWN_TIME; 586 } 587 if (positionMs == ANDROID_UNKNOWN_TIME) { 588 getPositionMsec(&positionMs); 589 // normalize it 590 if (positionMs < 0) { 591 positionMs = ANDROID_UNKNOWN_TIME; 592 } 593 if (ANDROID_UNKNOWN_TIME == positionMs) { 594 // getPositionMsec is not working for some reason, give up 595 //LOGV("Does anyone really know what time it is?"); 596 return; 597 } 598 } 599 600 // delayUs is the expected delay between current position and marker; 601 // the default is infinity in case there are no upcoming marker(s) 602 int64_t delayUs = -1; 603 604 // is there a marker? 605 if ((mEventFlags & SL_PLAYEVENT_HEADATMARKER) && (mMarkerPositionMs != ANDROID_UNKNOWN_TIME)) { 606 // check to see if we have observed the position passing through the marker 607 if (mObservedPositionMs <= mMarkerPositionMs && mMarkerPositionMs <= positionMs) { 608 notify(PLAYEREVENT_PLAY, (int32_t) SL_PLAYEVENT_HEADATMARKER, true /*async*/); 609 } else if (positionMs < mMarkerPositionMs) { 610 delayUs = (mMarkerPositionMs - positionMs) * 1000LL; 611 } 612 } 613 614 // are periodic position updates needed? 615 if ((mEventFlags & SL_PLAYEVENT_HEADATNEWPOS) && 616 (mPositionUpdatePeriodMs != ANDROID_UNKNOWN_TIME)) { 617 // check to see if we have observed the position passing through a virtual marker, where the 618 // virtual marker is at the previously delivered new position plus position update period 619 int32_t virtualMarkerMs; 620 if (mDeliveredNewPosMs != ANDROID_UNKNOWN_TIME) { 621 virtualMarkerMs = mDeliveredNewPosMs + mPositionUpdatePeriodMs; 622 } else if (mObservedPositionMs != ANDROID_UNKNOWN_TIME) { 623 virtualMarkerMs = mObservedPositionMs + mPositionUpdatePeriodMs; 624 // pretend there has been an update in the past 625 mDeliveredNewPosMs = mObservedPositionMs; 626 } else { 627 virtualMarkerMs = positionMs + mPositionUpdatePeriodMs; 628 // pretend there has been an update in the past 629 mDeliveredNewPosMs = positionMs; 630 } 631 // nextVirtualMarkerMs will be set to the position of the next upcoming virtual marker 632 int32_t nextVirtualMarkerMs; 633 if (mObservedPositionMs <= virtualMarkerMs && virtualMarkerMs <= positionMs) { 634 // we did pass through the virtual marker, now compute the next virtual marker 635 mDeliveredNewPosMs = virtualMarkerMs; 636 nextVirtualMarkerMs = virtualMarkerMs + mPositionUpdatePeriodMs; 637 // re-synchronize if we missed an update 638 if (nextVirtualMarkerMs <= positionMs) { 639 SL_LOGW("Missed SL_PLAYEVENT_HEADATNEWPOS for position %d; current position %d", 640 nextVirtualMarkerMs, positionMs); 641 // try to catch up by setting next goal to current position plus update period 642 mDeliveredNewPosMs = positionMs; 643 nextVirtualMarkerMs = positionMs + mPositionUpdatePeriodMs; 644 } 645 notify(PLAYEREVENT_PLAY, (int32_t) SL_PLAYEVENT_HEADATNEWPOS, true /*async*/); 646 } else { 647 // we did not pass through the virtual marker yet, so use same marker again 648 nextVirtualMarkerMs = virtualMarkerMs; 649 } 650 // note that if arithmetic overflow occurred, nextVirtualMarkerMs will be negative 651 if (positionMs < nextVirtualMarkerMs) { 652 int64_t trialDelayUs; 653 trialDelayUs = (nextVirtualMarkerMs - positionMs) * 1000LL; 654 if (trialDelayUs > 0 && (delayUs == -1 || trialDelayUs < delayUs)) { 655 delayUs = trialDelayUs; 656 } 657 } 658 } 659 660 // we have a new observed position 661 mObservedPositionMs = positionMs; 662 663 // post the new one-shot message if needed 664 if (advancesPositionInRealTime() && delayUs >= 0) { 665 // 20 ms min delay to avoid near busy waiting 666 if (delayUs < 20000LL) { 667 delayUs = 20000LL; 668 } 669 // 1 minute max delay avoids indefinite memory leaks caused by cancelled one-shots 670 if (delayUs > 60000000LL) { 671 delayUs = 60000000LL; 672 } 673 //SL_LOGI("delayUs = %lld", delayUs); 674 sp<AMessage> msg = new AMessage(kWhatOneShot, id()); 675 msg->setInt32(WHATPARAM_ONESHOT_GENERATION, mOneShotGeneration); 676 msg->post(delayUs); 677 } 678 679} 680 681} // namespace android 682