android_AudioSfDecoder.cpp revision 1209c173a13a62d0f7d9f1d605e3665a5818fa90
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#include "android/android_AudioSfDecoder.h" 21 22#include <media/stagefright/foundation/ADebug.h> 23 24 25#define SIZE_CACHED_HIGH_BYTES 1000000 26#define SIZE_CACHED_MED_BYTES 700000 27#define SIZE_CACHED_LOW_BYTES 400000 28 29namespace android { 30 31// keep in sync with the entries of kPcmDecodeMetadataKeys[] defined in android_AudioSfDecoder.h 32#define ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS 0 33#define ANDROID_KEY_INDEX_PCMFORMAT_SAMPLESPERSEC 1 34#define ANDROID_KEY_INDEX_PCMFORMAT_BITSPERSAMPLE 2 35#define ANDROID_KEY_INDEX_PCMFORMAT_CONTAINERSIZE 3 36#define ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK 4 37#define ANDROID_KEY_INDEX_PCMFORMAT_ENDIANNESS 5 38 39//-------------------------------------------------------------------------------------------------- 40AudioSfDecoder::AudioSfDecoder(const AudioPlayback_Parameters* params) : GenericPlayer(params), 41 mDataSource(0), 42 mAudioSource(0), 43 mAudioSourceStarted(false), 44 mBitrate(-1), 45 mChannelMask(ANDROID_UNKNOWN_CHANNELMASK), 46 mDurationUsec(-1), 47 mDecodeBuffer(NULL), 48 mTimeDelta(-1), 49 mSeekTimeMsec(0), 50 mLastDecodedPositionUs(-1), 51 mPcmFormatKeyCount(0), 52 mGetPcmFormatKeyCount(false) 53{ 54 SL_LOGV("AudioSfDecoder::AudioSfDecoder()"); 55} 56 57 58AudioSfDecoder::~AudioSfDecoder() { 59 SL_LOGV("AudioSfDecoder::~AudioSfDecoder()"); 60} 61 62 63void AudioSfDecoder::preDestroy() { 64 GenericPlayer::preDestroy(); 65 SL_LOGD("AudioSfDecoder::preDestroy()"); 66 { 67 Mutex::Autolock _l(mBufferSourceLock); 68 69 if (NULL != mDecodeBuffer) { 70 mDecodeBuffer->release(); 71 mDecodeBuffer = NULL; 72 } 73 74 if ((mAudioSource != 0) && mAudioSourceStarted) { 75 mAudioSource->stop(); 76 mAudioSourceStarted = false; 77 } 78 } 79} 80 81 82//-------------------------------------------------- 83void AudioSfDecoder::play() { 84 SL_LOGV("AudioSfDecoder::play"); 85 86 GenericPlayer::play(); 87 (new AMessage(kWhatDecode, id()))->post(); 88} 89 90 91void AudioSfDecoder::startPrefetch_async() { 92 SL_LOGV("AudioSfDecoder::startPrefetch_async()"); 93 94 if (wantPrefetch()) { 95 SL_LOGV("AudioSfDecoder::startPrefetch_async(): sending check cache msg"); 96 97 mStateFlags |= kFlagPreparing | kFlagBuffering; 98 99 (new AMessage(kWhatCheckCache, id()))->post(); 100 } 101} 102 103 104//-------------------------------------------------- 105uint32_t AudioSfDecoder::getPcmFormatKeyCount() { 106 android::Mutex::Autolock autoLock(mGetPcmFormatLockSingleton); 107 mGetPcmFormatKeyCount = false; 108 (new AMessage(kWhatGetPcmFormat, id()))->post(); 109 { 110 android::Mutex::Autolock autoLock(mGetPcmFormatLock); 111 while (!mGetPcmFormatKeyCount) { 112 mGetPcmFormatCondition.wait(mGetPcmFormatLock); 113 } 114 } 115 mGetPcmFormatKeyCount = false; 116 return mPcmFormatKeyCount; 117} 118 119 120//-------------------------------------------------- 121bool AudioSfDecoder::getPcmFormatKeySize(uint32_t index, uint32_t* pKeySize) { 122 uint32_t keyCount = getPcmFormatKeyCount(); 123 if (index >= keyCount) { 124 return false; 125 } else { 126 *pKeySize = strlen(kPcmDecodeMetadataKeys[index]) +1; 127 return true; 128 } 129} 130 131 132//-------------------------------------------------- 133bool AudioSfDecoder::getPcmFormatKeyName(uint32_t index, uint32_t keySize, char* keyName) { 134 uint32_t actualKeySize; 135 if (!getPcmFormatKeySize(index, &actualKeySize)) { 136 return false; 137 } 138 if (keySize < actualKeySize) { 139 return false; 140 } 141 strncpy(keyName, kPcmDecodeMetadataKeys[index], actualKeySize); 142 return true; 143} 144 145 146//-------------------------------------------------- 147bool AudioSfDecoder::getPcmFormatValueSize(uint32_t index, uint32_t* pValueSize) { 148 uint32_t keyCount = getPcmFormatKeyCount(); 149 if (index >= keyCount) { 150 *pValueSize = 0; 151 return false; 152 } else { 153 *pValueSize = sizeof(uint32_t); 154 return true; 155 } 156} 157 158 159//-------------------------------------------------- 160bool AudioSfDecoder::getPcmFormatKeyValue(uint32_t index, uint32_t size, uint32_t* pValue) { 161 uint32_t valueSize = 0; 162 if (!getPcmFormatValueSize(index, &valueSize)) { 163 return false; 164 } else if (size == valueSize) { 165 // this ensures we are accessing mPcmFormatValues with a valid size for that index 166 return false; 167 } else { 168 *pValue = mPcmFormatValues[index]; 169 return true; 170 } 171} 172 173 174//-------------------------------------------------- 175// Event handlers 176// it is strictly verboten to call those methods outside of the event loop 177 178// Initializes the data and audio sources, and update the PCM format info 179// post-condition: upon successful initialization based on the player data locator 180// GenericPlayer::onPrepare() was called 181// mDataSource != 0 182// mAudioSource != 0 183// mAudioSourceStarted == true 184void AudioSfDecoder::onPrepare() { 185 SL_LOGD("AudioSfDecoder::onPrepare()"); 186 Mutex::Autolock _l(mBufferSourceLock); 187 188 // Initialize the PCM format info with the known parameters before the start of the decode 189 mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_BITSPERSAMPLE] = SL_PCMSAMPLEFORMAT_FIXED_16; 190 mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CONTAINERSIZE] = 16; 191 mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_ENDIANNESS] = SL_BYTEORDER_LITTLEENDIAN; 192 // initialization with the default values: they will be replaced by the actual values 193 // once the decoder has figured them out 194 mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = mChannelCount; 195 mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLESPERSEC] = mSampleRateHz; 196 mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] = mChannelMask; 197 198 //--------------------------------- 199 // Instantiate and initialize the data source for the decoder 200 sp<DataSource> dataSource; 201 202 switch (mDataLocatorType) { 203 204 case kDataLocatorNone: 205 SL_LOGE("AudioSfDecoder::onPrepare: no data locator set"); 206 notifyPrepared(MEDIA_ERROR_BASE); 207 return; 208 209 case kDataLocatorUri: 210 dataSource = DataSource::CreateFromURI(mDataLocator.uriRef); 211 if (dataSource == NULL) { 212 SL_LOGE("AudioSfDecoder::onPrepare(): Error opening %s", mDataLocator.uriRef); 213 notifyPrepared(MEDIA_ERROR_BASE); 214 return; 215 } 216 break; 217 218 case kDataLocatorFd: 219 { 220 dataSource = new FileSource( 221 mDataLocator.fdi.fd, mDataLocator.fdi.offset, mDataLocator.fdi.length); 222 status_t err = dataSource->initCheck(); 223 if (err != OK) { 224 notifyPrepared(err); 225 return; 226 } 227 break; 228 } 229 230 default: 231 TRESPASS(); 232 } 233 234 //--------------------------------- 235 // Instanciate and initialize the decoder attached to the data source 236 sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource); 237 if (extractor == NULL) { 238 SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate extractor."); 239 notifyPrepared(ERROR_UNSUPPORTED); 240 return; 241 } 242 243 ssize_t audioTrackIndex = -1; 244 bool isRawAudio = false; 245 for (size_t i = 0; i < extractor->countTracks(); ++i) { 246 sp<MetaData> meta = extractor->getTrackMetaData(i); 247 248 const char *mime; 249 CHECK(meta->findCString(kKeyMIMEType, &mime)); 250 251 if (!strncasecmp("audio/", mime, 6)) { 252 audioTrackIndex = i; 253 254 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_RAW, mime)) { 255 isRawAudio = true; 256 } 257 break; 258 } 259 } 260 261 if (audioTrackIndex < 0) { 262 SL_LOGE("AudioSfDecoder::onPrepare: Could not find a supported audio track."); 263 notifyPrepared(ERROR_UNSUPPORTED); 264 return; 265 } 266 267 sp<MediaSource> source = extractor->getTrack(audioTrackIndex); 268 sp<MetaData> meta = source->getFormat(); 269 270 bool hasChannelCount = meta->findInt32(kKeyChannelCount, &mChannelCount); 271 if (hasChannelCount) { 272 mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = mChannelCount; 273 } 274 275 off64_t size; 276 int64_t durationUs; 277 if (dataSource->getSize(&size) == OK 278 && meta->findInt64(kKeyDuration, &durationUs)) { 279 mBitrate = size * 8000000ll / durationUs; // in bits/sec 280 mDurationUsec = durationUs; 281 mDurationMsec = durationUs / 1000; 282 } else { 283 mBitrate = -1; 284 mDurationUsec = -1; 285 } 286 287 // the audio content is not raw PCM, so we need a decoder 288 if (!isRawAudio) { 289 OMXClient client; 290 CHECK_EQ(client.connect(), (status_t)OK); 291 292 source = OMXCodec::Create( 293 client.interface(), meta, false /* createEncoder */, 294 source); 295 296 if (source == NULL) { 297 SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate decoder."); 298 notifyPrepared(ERROR_UNSUPPORTED); 299 return; 300 } 301 302 meta = source->getFormat(); 303 } 304 305 306 if (source->start() != OK) { 307 SL_LOGE("AudioSfDecoder::onPrepare: Failed to start source/decoder."); 308 notifyPrepared(MEDIA_ERROR_BASE); 309 return; 310 } 311 312 //--------------------------------- 313 // The data source, and audio source (a decoder if required) are ready to be used 314 mDataSource = dataSource; 315 mAudioSource = source; 316 mAudioSourceStarted = true; 317 318 if (!hasChannelCount) { 319 // even though the channel count was already queried above, there are issues with some 320 // OMXCodecs (e.g. MP3 software decoder) not reporting the right count, 321 // we trust the first reported value. 322 CHECK(meta->findInt32(kKeyChannelCount, &mChannelCount)); 323 mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = mChannelCount; 324 } 325 int32_t sr; 326 CHECK(meta->findInt32(kKeySampleRate, &sr)); 327 mSampleRateHz = (uint32_t) sr; 328 mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLESPERSEC] = mSampleRateHz; 329 // FIXME add code below once channel mask support is in, currently initialized to default 330 // if (meta->findInt32(kKeyChannelMask, &mChannelMask)) { 331 // mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] = mChannelMask; 332 // } 333 334 if (!wantPrefetch()) { 335 SL_LOGV("AudioSfDecoder::onPrepare: no need to prefetch"); 336 // doesn't need prefetching, notify good to go 337 mCacheStatus = kStatusHigh; 338 mCacheFill = 1000; 339 notifyStatus(); 340 notifyCacheFill(); 341 } 342 343 // at this point we have enough information about the source to create the sink that 344 // will consume the data 345 createAudioSink(); 346 347 GenericPlayer::onPrepare(); 348 SL_LOGD("AudioSfDecoder::onPrepare() done, mStateFlags=0x%x", mStateFlags); 349} 350 351 352void AudioSfDecoder::onPause() { 353 SL_LOGD("AudioSfDecoder::onPause()"); 354 GenericPlayer::onPause(); 355 pauseAudioSink(); 356} 357 358 359void AudioSfDecoder::onPlay() { 360 SL_LOGD("AudioSfDecoder::onPlay()"); 361 GenericPlayer::onPlay(); 362 startAudioSink(); 363} 364 365 366void AudioSfDecoder::onSeek(const sp<AMessage> &msg) { 367 SL_LOGV("AudioSfDecoder::onSeek"); 368 int64_t timeMsec; 369 CHECK(msg->findInt64(WHATPARAM_SEEK_SEEKTIME_MS, &timeMsec)); 370 371 Mutex::Autolock _l(mSeekLock); 372 mStateFlags |= kFlagSeeking; 373 mSeekTimeMsec = timeMsec; 374 mTimeDelta = -1; 375 mLastDecodedPositionUs = -1; 376} 377 378 379void AudioSfDecoder::onLoop(const sp<AMessage> &msg) { 380 SL_LOGV("AudioSfDecoder::onLoop"); 381 int32_t loop; 382 CHECK(msg->findInt32(WHATPARAM_LOOP_LOOPING, &loop)); 383 384 if (loop) { 385 //SL_LOGV("AudioSfDecoder::onLoop start looping"); 386 mStateFlags |= kFlagLooping; 387 } else { 388 //SL_LOGV("AudioSfDecoder::onLoop stop looping"); 389 mStateFlags &= ~kFlagLooping; 390 } 391} 392 393 394void AudioSfDecoder::onGetPcmFormatKeyCount() { 395 SL_LOGV("AudioSfDecoder::onGetPcmFormatKeyCount"); 396 { 397 android::Mutex::Autolock autoLock(mGetPcmFormatLock); 398 399 if (!(mStateFlags & kFlagPrepared)) { 400 mPcmFormatKeyCount = 0; 401 } else { 402 mPcmFormatKeyCount = NB_PCMMETADATA_KEYS; 403 } 404 405 mGetPcmFormatKeyCount = true; 406 mGetPcmFormatCondition.signal(); 407 } 408 409} 410 411 412void AudioSfDecoder::onCheckCache(const sp<AMessage> &msg) { 413 //SL_LOGV("AudioSfDecoder::onCheckCache"); 414 bool eos; 415 CacheStatus_t status = getCacheRemaining(&eos); 416 417 if (eos || status == kStatusHigh 418 || ((mStateFlags & kFlagPreparing) && (status >= kStatusEnough))) { 419 if (mStateFlags & kFlagPlaying) { 420 startAudioSink(); 421 } 422 mStateFlags &= ~kFlagBuffering; 423 424 SL_LOGV("AudioSfDecoder::onCheckCache: buffering done."); 425 426 if (mStateFlags & kFlagPreparing) { 427 //SL_LOGV("AudioSfDecoder::onCheckCache: preparation done."); 428 mStateFlags &= ~kFlagPreparing; 429 } 430 431 mTimeDelta = -1; 432 if (mStateFlags & kFlagPlaying) { 433 (new AMessage(kWhatDecode, id()))->post(); 434 } 435 return; 436 } 437 438 msg->post(100000); 439} 440 441 442void AudioSfDecoder::onDecode() { 443 SL_LOGV("AudioSfDecoder::onDecode"); 444 445 //-------------------------------- Need to buffer some more before decoding? 446 bool eos; 447 if (mDataSource == 0) { 448 // application set play state to paused which failed, then set play state to playing 449 return; 450 } 451 452 if (wantPrefetch() 453 && (getCacheRemaining(&eos) == kStatusLow) 454 && !eos) { 455 SL_LOGV("buffering more."); 456 457 if (mStateFlags & kFlagPlaying) { 458 pauseAudioSink(); 459 } 460 mStateFlags |= kFlagBuffering; 461 (new AMessage(kWhatCheckCache, id()))->post(100000); 462 return; 463 } 464 465 if (!(mStateFlags & (kFlagPlaying | kFlagBuffering | kFlagPreparing))) { 466 // don't decode if we're not buffering, prefetching or playing 467 //SL_LOGV("don't decode: not buffering, prefetching or playing"); 468 return; 469 } 470 471 //-------------------------------- Decode 472 status_t err; 473 MediaSource::ReadOptions readOptions; 474 if (mStateFlags & kFlagSeeking) { 475 readOptions.setSeekTo(mSeekTimeMsec * 1000); 476 } 477 478 { 479 Mutex::Autolock _l(mBufferSourceLock); 480 481 if (NULL != mDecodeBuffer) { 482 // the current decoded buffer hasn't been rendered, drop it 483 mDecodeBuffer->release(); 484 mDecodeBuffer = NULL; 485 } 486 if(!mAudioSourceStarted) { 487 return; 488 } 489 err = mAudioSource->read(&mDecodeBuffer, &readOptions); 490 if (err == OK) { 491 CHECK(mDecodeBuffer->meta_data()->findInt64(kKeyTime, &mLastDecodedPositionUs)); 492 } 493 } 494 495 { 496 Mutex::Autolock _l(mSeekLock); 497 if (mStateFlags & kFlagSeeking) { 498 mStateFlags &= ~kFlagSeeking; 499 } 500 } 501 502 //-------------------------------- Handle return of decode 503 if (err != OK) { 504 bool continueDecoding = false; 505 switch(err) { 506 case ERROR_END_OF_STREAM: 507 if (0 < mDurationUsec) { 508 mLastDecodedPositionUs = mDurationUsec; 509 } 510 // handle notification and looping at end of stream 511 if (mStateFlags & kFlagPlaying) { 512 notify(PLAYEREVENT_ENDOFSTREAM, 1, true); 513 } 514 if (mStateFlags & kFlagLooping) { 515 seek(0); 516 // kick-off decoding again 517 continueDecoding = true; 518 } 519 break; 520 case INFO_FORMAT_CHANGED: 521 SL_LOGD("MediaSource::read encountered INFO_FORMAT_CHANGED"); 522 // reconfigure output 523 updateAudioSink(); 524 continueDecoding = true; 525 break; 526 case INFO_DISCONTINUITY: 527 SL_LOGD("MediaSource::read encountered INFO_DISCONTINUITY"); 528 continueDecoding = true; 529 break; 530 default: 531 SL_LOGE("MediaSource::read returned error %d", err); 532 break; 533 } 534 if (continueDecoding) { 535 if (NULL == mDecodeBuffer) { 536 (new AMessage(kWhatDecode, id()))->post(); 537 return; 538 } 539 } else { 540 return; 541 } 542 } 543 544 //-------------------------------- Render 545 sp<AMessage> msg = new AMessage(kWhatRender, id()); 546 msg->post(); 547} 548 549 550void AudioSfDecoder::onRender() { 551 //SL_LOGV("AudioSfDecoder::onRender"); 552 553 Mutex::Autolock _l(mBufferSourceLock); 554 555 if (NULL == mDecodeBuffer) { 556 // nothing to render, move along 557 SL_LOGV("AudioSfDecoder::onRender NULL buffer, exiting"); 558 return; 559 } 560 561 mDecodeBuffer->release(); 562 mDecodeBuffer = NULL; 563 564} 565 566 567void AudioSfDecoder::onMessageReceived(const sp<AMessage> &msg) { 568 switch (msg->what()) { 569 case kWhatPrepare: 570 onPrepare(); 571 break; 572 573 case kWhatDecode: 574 onDecode(); 575 break; 576 577 case kWhatRender: 578 onRender(); 579 break; 580 581 case kWhatCheckCache: 582 onCheckCache(msg); 583 break; 584 585 case kWhatNotif: 586 onNotify(msg); 587 break; 588 589 case kWhatPlay: 590 onPlay(); 591 break; 592 593 case kWhatPause: 594 onPause(); 595 break; 596 597 case kWhatGetPcmFormat: 598 onGetPcmFormatKeyCount(); 599 break; 600/* 601 case kWhatSeek: 602 onSeek(msg); 603 break; 604 605 case kWhatLoop: 606 onLoop(msg); 607 break; 608*/ 609 default: 610 GenericPlayer::onMessageReceived(msg); 611 break; 612 } 613} 614 615//-------------------------------------------------- 616// Prepared state, prefetch status notifications 617void AudioSfDecoder::notifyPrepared(status_t prepareRes) { 618 notify(PLAYEREVENT_PREPARED, (int32_t)prepareRes, true); 619 620} 621 622 623void AudioSfDecoder::onNotify(const sp<AMessage> &msg) { 624 notif_cbf_t notifyClient; 625 void* notifyUser; 626 { 627 android::Mutex::Autolock autoLock(mNotifyClientLock); 628 if (NULL == mNotifyClient) { 629 return; 630 } else { 631 notifyClient = mNotifyClient; 632 notifyUser = mNotifyUser; 633 } 634 } 635 int32_t val; 636 if (msg->findInt32(PLAYEREVENT_PREFETCHSTATUSCHANGE, &val)) { 637 SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_PREFETCHSTATUSCHANGE, val); 638 notifyClient(kEventPrefetchStatusChange, val, 0, notifyUser); 639 } 640 else if (msg->findInt32(PLAYEREVENT_PREFETCHFILLLEVELUPDATE, &val)) { 641 SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_PREFETCHFILLLEVELUPDATE, val); 642 notifyClient(kEventPrefetchFillLevelUpdate, val, 0, notifyUser); 643 } 644 else if (msg->findInt32(PLAYEREVENT_ENDOFSTREAM, &val)) { 645 SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_ENDOFSTREAM, val); 646 notifyClient(kEventEndOfStream, val, 0, notifyUser); 647 } 648 else { 649 GenericPlayer::onNotify(msg); 650 } 651} 652 653 654//-------------------------------------------------- 655// Private utility functions 656 657bool AudioSfDecoder::wantPrefetch() { 658 if (mDataSource != 0) { 659 return (mDataSource->flags() & DataSource::kWantsPrefetching); 660 } else { 661 // happens if an improper data locator was passed, if the media extractor couldn't be 662 // initialized, if there is no audio track in the media, if the OMX decoder couldn't be 663 // instantiated, if the source couldn't be opened, or if the MediaSource 664 // couldn't be started 665 SL_LOGV("AudioSfDecoder::wantPrefetch() tries to access NULL mDataSource"); 666 return false; 667 } 668} 669 670 671int64_t AudioSfDecoder::getPositionUsec() { 672 Mutex::Autolock _l(mSeekLock); 673 if (mStateFlags & kFlagSeeking) { 674 return mSeekTimeMsec * 1000; 675 } else { 676 if (mLastDecodedPositionUs < 0) { 677 return 0; 678 } else { 679 return mLastDecodedPositionUs; 680 } 681 } 682} 683 684 685CacheStatus_t AudioSfDecoder::getCacheRemaining(bool *eos) { 686 sp<NuCachedSource2> cachedSource = 687 static_cast<NuCachedSource2 *>(mDataSource.get()); 688 689 CacheStatus_t oldStatus = mCacheStatus; 690 691 status_t finalStatus; 692 size_t dataRemaining = cachedSource->approxDataRemaining(&finalStatus); 693 *eos = (finalStatus != OK); 694 695 CHECK_GE(mBitrate, 0); 696 697 int64_t dataRemainingUs = dataRemaining * 8000000ll / mBitrate; 698 //SL_LOGV("AudioSfDecoder::getCacheRemaining: approx %.2f secs remaining (eos=%d)", 699 // dataRemainingUs / 1E6, *eos); 700 701 if (*eos) { 702 // data is buffered up to the end of the stream, it can't get any better than this 703 mCacheStatus = kStatusHigh; 704 mCacheFill = 1000; 705 706 } else { 707 if (mDurationUsec > 0) { 708 // known duration: 709 710 // fill level is ratio of how much has been played + how much is 711 // cached, divided by total duration 712 uint32_t currentPositionUsec = getPositionUsec(); 713 mCacheFill = (int16_t) ((1000.0 714 * (double)(currentPositionUsec + dataRemainingUs) / mDurationUsec)); 715 //SL_LOGV("cacheFill = %d", mCacheFill); 716 717 // cache status is evaluated against duration thresholds 718 if (dataRemainingUs > DURATION_CACHED_HIGH_MS*1000) { 719 mCacheStatus = kStatusHigh; 720 //LOGV("high"); 721 } else if (dataRemainingUs > DURATION_CACHED_MED_MS*1000) { 722 //LOGV("enough"); 723 mCacheStatus = kStatusEnough; 724 } else if (dataRemainingUs < DURATION_CACHED_LOW_MS*1000) { 725 //LOGV("low"); 726 mCacheStatus = kStatusLow; 727 } else { 728 mCacheStatus = kStatusIntermediate; 729 } 730 731 } else { 732 // unknown duration: 733 734 // cache status is evaluated against cache amount thresholds 735 // (no duration so we don't have the bitrate either, could be derived from format?) 736 if (dataRemaining > SIZE_CACHED_HIGH_BYTES) { 737 mCacheStatus = kStatusHigh; 738 } else if (dataRemaining > SIZE_CACHED_MED_BYTES) { 739 mCacheStatus = kStatusEnough; 740 } else if (dataRemaining < SIZE_CACHED_LOW_BYTES) { 741 mCacheStatus = kStatusLow; 742 } else { 743 mCacheStatus = kStatusIntermediate; 744 } 745 } 746 747 } 748 749 if (oldStatus != mCacheStatus) { 750 notifyStatus(); 751 } 752 753 if (abs(mCacheFill - mLastNotifiedCacheFill) > mCacheFillNotifThreshold) { 754 notifyCacheFill(); 755 } 756 757 return mCacheStatus; 758} 759 760} // namespace android 761