android_AudioSfDecoder.cpp revision 4ee246c55533bdab8ab5fa0f0581744fe58e7c91
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//-------------------------------------------------------------------------------------------------- 32AudioSfDecoder::AudioSfDecoder(const AudioPlayback_Parameters* params) : GenericPlayer(params), 33 mBitrate(-1), 34 mNumChannels(1), 35 mSampleRateHz(0), 36 mDurationUsec(-1), 37 mDecodeBuffer(NULL), 38 mTimeDelta(-1), 39 mSeekTimeMsec(0), 40 mLastDecodedPositionUs(-1) 41{ 42 SL_LOGV("AudioSfDecoder::AudioSfDecoder()"); 43 44} 45 46 47AudioSfDecoder::~AudioSfDecoder() { 48 SL_LOGV("AudioSfDecoder::~AudioSfDecoder()"); 49 50} 51 52 53//-------------------------------------------------- 54void AudioSfDecoder::play() { 55 SL_LOGV("AudioSfDecoder::play"); 56 57 GenericPlayer::play(); 58 (new AMessage(kWhatDecode, id()))->post(); 59} 60 61 62void AudioSfDecoder::startPrefetch_async() { 63 SL_LOGV("AudioSfDecoder::startPrefetch_async()"); 64 65 if (wantPrefetch()) { 66 SL_LOGV("AudioSfDecoder::startPrefetch_async(): sending check cache msg"); 67 68 mStateFlags |= kFlagPreparing | kFlagBuffering; 69 70 (new AMessage(kWhatCheckCache, id()))->post(); 71 } 72} 73 74 75//-------------------------------------------------- 76// Event handlers 77void AudioSfDecoder::onPrepare() { 78 SL_LOGD("AudioSfDecoder::onPrepare()"); 79 80 sp<DataSource> dataSource; 81 82 switch (mDataLocatorType) { 83 84 case kDataLocatorNone: 85 SL_LOGE("AudioSfDecoder::onPrepare: no data locator set"); 86 notifyPrepared(MEDIA_ERROR_BASE); 87 return; 88 89 case kDataLocatorUri: 90 dataSource = DataSource::CreateFromURI(mDataLocator.uriRef); 91 if (dataSource == NULL) { 92 SL_LOGE("AudioSfDecoder::onPrepare(): Error opening %s", mDataLocator.uriRef); 93 notifyPrepared(MEDIA_ERROR_BASE); 94 return; 95 } 96 break; 97 98 case kDataLocatorFd: 99 { 100 dataSource = new FileSource( 101 mDataLocator.fdi.fd, mDataLocator.fdi.offset, mDataLocator.fdi.length); 102 status_t err = dataSource->initCheck(); 103 if (err != OK) { 104 notifyPrepared(err); 105 return; 106 } 107 break; 108 } 109 110 default: 111 TRESPASS(); 112 } 113 114 sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource); 115 if (extractor == NULL) { 116 SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate extractor."); 117 notifyPrepared(ERROR_UNSUPPORTED); 118 return; 119 } 120 121 ssize_t audioTrackIndex = -1; 122 bool isRawAudio = false; 123 for (size_t i = 0; i < extractor->countTracks(); ++i) { 124 sp<MetaData> meta = extractor->getTrackMetaData(i); 125 126 const char *mime; 127 CHECK(meta->findCString(kKeyMIMEType, &mime)); 128 129 if (!strncasecmp("audio/", mime, 6)) { 130 audioTrackIndex = i; 131 132 if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_RAW, mime)) { 133 isRawAudio = true; 134 } 135 break; 136 } 137 } 138 139 if (audioTrackIndex < 0) { 140 SL_LOGE("AudioSfDecoder::onPrepare: Could not find a supported audio track."); 141 notifyPrepared(ERROR_UNSUPPORTED); 142 return; 143 } 144 145 sp<MediaSource> source = extractor->getTrack(audioTrackIndex); 146 sp<MetaData> meta = source->getFormat(); 147 148 off64_t size; 149 int64_t durationUs; 150 if (dataSource->getSize(&size) == OK 151 && meta->findInt64(kKeyDuration, &durationUs)) { 152 mBitrate = size * 8000000ll / durationUs; // in bits/sec 153 mDurationUsec = durationUs; 154 } else { 155 mBitrate = -1; 156 mDurationUsec = -1; 157 } 158 159 if (!isRawAudio) { 160 OMXClient client; 161 CHECK_EQ(client.connect(), (status_t)OK); 162 163 source = OMXCodec::Create( 164 client.interface(), meta, false /* createEncoder */, 165 source); 166 167 if (source == NULL) { 168 SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate decoder."); 169 notifyPrepared(ERROR_UNSUPPORTED); 170 return; 171 } 172 173 meta = source->getFormat(); 174 } 175 176 177 if (source->start() != OK) { 178 SL_LOGE("AudioSfDecoder::onPrepare: Failed to start source/decoder."); 179 notifyPrepared(MEDIA_ERROR_BASE); 180 return; 181 } 182 183 mDataSource = dataSource; 184 mAudioSource = source; 185 186 CHECK(meta->findInt32(kKeyChannelCount, &mNumChannels)); 187 CHECK(meta->findInt32(kKeySampleRate, &mSampleRateHz)); 188 189 if (!wantPrefetch()) { 190 SL_LOGV("AudioSfDecoder::onPrepare: no need to prefetch"); 191 // doesn't need prefetching, notify good to go 192 mCacheStatus = kStatusHigh; 193 mCacheFill = 1000; 194 notifyStatus(); 195 notifyCacheFill(); 196 } 197 198 // at this point we have enough information about the source to create the sink that 199 // will consume the data 200 createAudioSink(); 201 202 GenericPlayer::onPrepare(); 203 SL_LOGD("AudioSfDecoder::onPrepare() done, mStateFlags=0x%x", mStateFlags); 204} 205 206 207void AudioSfDecoder::onPause() { 208 SL_LOGD("AudioSfDecoder::onPause()"); 209 GenericPlayer::onPause(); 210 pauseAudioSink(); 211} 212 213 214void AudioSfDecoder::onPlay() { 215 SL_LOGD("AudioSfDecoder::onPlay()"); 216 GenericPlayer::onPlay(); 217 startAudioSink(); 218} 219 220 221void AudioSfDecoder::onSeek(const sp<AMessage> &msg) { 222 SL_LOGV("AudioSfDecoder::onSeek"); 223 int64_t timeMsec; 224 CHECK(msg->findInt64(WHATPARAM_SEEK_SEEKTIME_MS, &timeMsec)); 225 226 Mutex::Autolock _l(mSeekLock); 227 mStateFlags |= kFlagSeeking; 228 mSeekTimeMsec = timeMsec; 229 mTimeDelta = -1; 230 mLastDecodedPositionUs = -1; 231} 232 233 234void AudioSfDecoder::onLoop(const sp<AMessage> &msg) { 235 SL_LOGV("AudioSfDecoder::onLoop"); 236 int32_t loop; 237 CHECK(msg->findInt32(WHATPARAM_LOOP_LOOPING, &loop)); 238 239 if (loop) { 240 //SL_LOGV("AudioSfDecoder::onLoop start looping"); 241 mStateFlags |= kFlagLooping; 242 } else { 243 //SL_LOGV("AudioSfDecoder::onLoop stop looping"); 244 mStateFlags &= ~kFlagLooping; 245 } 246} 247 248 249void AudioSfDecoder::onCheckCache(const sp<AMessage> &msg) { 250 //SL_LOGV("AudioSfDecoder::onCheckCache"); 251 bool eos; 252 CacheStatus_t status = getCacheRemaining(&eos); 253 254 if (eos || status == kStatusHigh 255 || ((mStateFlags & kFlagPreparing) && (status >= kStatusEnough))) { 256 if (mStateFlags & kFlagPlaying) { 257 startAudioSink(); 258 } 259 mStateFlags &= ~kFlagBuffering; 260 261 SL_LOGV("AudioSfDecoder::onCheckCache: buffering done."); 262 263 if (mStateFlags & kFlagPreparing) { 264 //SL_LOGV("AudioSfDecoder::onCheckCache: preparation done."); 265 mStateFlags &= ~kFlagPreparing; 266 } 267 268 mTimeDelta = -1; 269 if (mStateFlags & kFlagPlaying) { 270 (new AMessage(kWhatDecode, id()))->post(); 271 } 272 return; 273 } 274 275 msg->post(100000); 276} 277 278 279void AudioSfDecoder::onDecode() { 280 SL_LOGV("AudioSfDecoder::onDecode"); 281 282 //-------------------------------- Need to buffer some more before decoding? 283 bool eos; 284 if (mDataSource == 0) { 285 // application set play state to paused which failed, then set play state to playing 286 return; 287 } 288 if (wantPrefetch() 289 && (getCacheRemaining(&eos) == kStatusLow) 290 && !eos) { 291 SL_LOGV("buffering more."); 292 293 if (mStateFlags & kFlagPlaying) { 294 pauseAudioSink(); 295 } 296 mStateFlags |= kFlagBuffering; 297 (new AMessage(kWhatCheckCache, id()))->post(100000); 298 return; 299 } 300 301 if (!(mStateFlags & (kFlagPlaying | kFlagBuffering | kFlagPreparing))) { 302 // don't decode if we're not buffering, prefetching or playing 303 //SL_LOGV("don't decode: not buffering, prefetching or playing"); 304 return; 305 } 306 307 //-------------------------------- Decode 308 status_t err; 309 MediaSource::ReadOptions readOptions; 310 if (mStateFlags & kFlagSeeking) { 311 readOptions.setSeekTo(mSeekTimeMsec * 1000); 312 } 313 314 { 315 Mutex::Autolock _l(mDecodeBufferLock); 316 if (NULL != mDecodeBuffer) { 317 // the current decoded buffer hasn't been rendered, drop it 318 mDecodeBuffer->release(); 319 mDecodeBuffer = NULL; 320 } 321 err = mAudioSource->read(&mDecodeBuffer, &readOptions); 322 if (err == OK) { 323 CHECK(mDecodeBuffer->meta_data()->findInt64(kKeyTime, &mLastDecodedPositionUs)); 324 } 325 } 326 327 { 328 Mutex::Autolock _l(mSeekLock); 329 if (mStateFlags & kFlagSeeking) { 330 mStateFlags &= ~kFlagSeeking; 331 } 332 } 333 334 //-------------------------------- Handle return of decode 335 if (err != OK) { 336 bool continueDecoding = false; 337 switch(err) { 338 case ERROR_END_OF_STREAM: 339 if (0 < mDurationUsec) { 340 mLastDecodedPositionUs = mDurationUsec; 341 } 342 // handle notification and looping at end of stream 343 if (mStateFlags & kFlagPlaying) { 344 notify(PLAYEREVENT_ENDOFSTREAM, 1, true); 345 } 346 if (mStateFlags & kFlagLooping) { 347 seek(0); 348 // kick-off decoding again 349 continueDecoding = true; 350 } 351 break; 352 case INFO_FORMAT_CHANGED: 353 SL_LOGD("MediaSource::read encountered INFO_FORMAT_CHANGED"); 354 // reconfigure output 355 updateAudioSink(); 356 continueDecoding = true; 357 break; 358 case INFO_DISCONTINUITY: 359 SL_LOGD("MediaSource::read encountered INFO_DISCONTINUITY"); 360 continueDecoding = true; 361 break; 362 default: 363 SL_LOGE("MediaSource::read returned error %d", err); 364 break; 365 } 366 if (continueDecoding) { 367 if (NULL == mDecodeBuffer) { 368 (new AMessage(kWhatDecode, id()))->post(); 369 return; 370 } 371 } else { 372 return; 373 } 374 } 375 376 //-------------------------------- Render 377 sp<AMessage> msg = new AMessage(kWhatRender, id()); 378 msg->post(); 379} 380 381 382void AudioSfDecoder::onRender() { 383 //SL_LOGV("AudioSfDecoder::onRender"); 384 385 Mutex::Autolock _l(mDecodeBufferLock); 386 387 if (NULL == mDecodeBuffer) { 388 // nothing to render, move along 389 SL_LOGV("AudioSfDecoder::onRender NULL buffer, exiting"); 390 return; 391 } 392 393 mDecodeBuffer->release(); 394 mDecodeBuffer = NULL; 395 396} 397 398 399void AudioSfDecoder::onMessageReceived(const sp<AMessage> &msg) { 400 switch (msg->what()) { 401 case kWhatPrepare: 402 onPrepare(); 403 break; 404 405 case kWhatDecode: 406 onDecode(); 407 break; 408 409 case kWhatRender: 410 onRender(); 411 break; 412 413 case kWhatCheckCache: 414 onCheckCache(msg); 415 break; 416 417 case kWhatNotif: 418 onNotify(msg); 419 break; 420 421 case kWhatPlay: 422 onPlay(); 423 break; 424 425 case kWhatPause: 426 onPause(); 427 break; 428/* 429 case kWhatSeek: 430 onSeek(msg); 431 break; 432 433 case kWhatLoop: 434 onLoop(msg); 435 break; 436*/ 437 default: 438 GenericPlayer::onMessageReceived(msg); 439 break; 440 } 441} 442 443//-------------------------------------------------- 444// Prepared state, prefetch status notifications 445void AudioSfDecoder::notifyPrepared(status_t prepareRes) { 446 notify(PLAYEREVENT_PREPARED, (int32_t)prepareRes, true); 447 448} 449 450 451void AudioSfDecoder::onNotify(const sp<AMessage> &msg) { 452 if (NULL == mNotifyClient) { 453 return; 454 } 455 int32_t val; 456 if (msg->findInt32(PLAYEREVENT_PREFETCHSTATUSCHANGE, &val)) { 457 SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_PREFETCHSTATUSCHANGE, val); 458 mNotifyClient(kEventPrefetchStatusChange, val, 0, mNotifyUser); 459 } 460 else if (msg->findInt32(PLAYEREVENT_PREFETCHFILLLEVELUPDATE, &val)) { 461 SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_PREFETCHFILLLEVELUPDATE, val); 462 mNotifyClient(kEventPrefetchFillLevelUpdate, val, 0, mNotifyUser); 463 } 464 else if (msg->findInt32(PLAYEREVENT_ENDOFSTREAM, &val)) { 465 SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_ENDOFSTREAM, val); 466 mNotifyClient(kEventEndOfStream, val, 0, mNotifyUser); 467 } 468 else { 469 GenericPlayer::onNotify(msg); 470 } 471} 472 473 474//-------------------------------------------------- 475// Private utility functions 476 477bool AudioSfDecoder::wantPrefetch() { 478 return (mDataSource->flags() & DataSource::kWantsPrefetching); 479} 480 481 482int64_t AudioSfDecoder::getPositionUsec() { 483 Mutex::Autolock _l(mSeekLock); 484 if (mStateFlags & kFlagSeeking) { 485 return mSeekTimeMsec * 1000; 486 } else { 487 if (mLastDecodedPositionUs < 0) { 488 return 0; 489 } else { 490 return mLastDecodedPositionUs; 491 } 492 } 493} 494 495 496CacheStatus_t AudioSfDecoder::getCacheRemaining(bool *eos) { 497 sp<NuCachedSource2> cachedSource = 498 static_cast<NuCachedSource2 *>(mDataSource.get()); 499 500 CacheStatus_t oldStatus = mCacheStatus; 501 502 status_t finalStatus; 503 size_t dataRemaining = cachedSource->approxDataRemaining(&finalStatus); 504 *eos = (finalStatus != OK); 505 506 CHECK_GE(mBitrate, 0); 507 508 int64_t dataRemainingUs = dataRemaining * 8000000ll / mBitrate; 509 //SL_LOGV("AudioSfDecoder::getCacheRemaining: approx %.2f secs remaining (eos=%d)", 510 // dataRemainingUs / 1E6, *eos); 511 512 if (*eos) { 513 // data is buffered up to the end of the stream, it can't get any better than this 514 mCacheStatus = kStatusHigh; 515 mCacheFill = 1000; 516 517 } else { 518 if (mDurationUsec > 0) { 519 // known duration: 520 521 // fill level is ratio of how much has been played + how much is 522 // cached, divided by total duration 523 uint32_t currentPositionUsec = getPositionUsec(); 524 mCacheFill = (int16_t) ((1000.0 525 * (double)(currentPositionUsec + dataRemainingUs) / mDurationUsec)); 526 //SL_LOGV("cacheFill = %d", mCacheFill); 527 528 // cache status is evaluated against duration thresholds 529 if (dataRemainingUs > DURATION_CACHED_HIGH_MS*1000) { 530 mCacheStatus = kStatusHigh; 531 //LOGV("high"); 532 } else if (dataRemainingUs > DURATION_CACHED_MED_MS*1000) { 533 //LOGV("enough"); 534 mCacheStatus = kStatusEnough; 535 } else if (dataRemainingUs < DURATION_CACHED_LOW_MS*1000) { 536 //LOGV("low"); 537 mCacheStatus = kStatusLow; 538 } else { 539 mCacheStatus = kStatusIntermediate; 540 } 541 542 } else { 543 // unknown duration: 544 545 // cache status is evaluated against cache amount thresholds 546 // (no duration so we don't have the bitrate either, could be derived from format?) 547 if (dataRemaining > SIZE_CACHED_HIGH_BYTES) { 548 mCacheStatus = kStatusHigh; 549 } else if (dataRemaining > SIZE_CACHED_MED_BYTES) { 550 mCacheStatus = kStatusEnough; 551 } else if (dataRemaining < SIZE_CACHED_LOW_BYTES) { 552 mCacheStatus = kStatusLow; 553 } else { 554 mCacheStatus = kStatusIntermediate; 555 } 556 } 557 558 } 559 560 if (oldStatus != mCacheStatus) { 561 notifyStatus(); 562 } 563 564 if (abs(mCacheFill - mLastNotifiedCacheFill) > mCacheFillNotifThreshold) { 565 notifyCacheFill(); 566 } 567 568 return mCacheStatus; 569} 570 571} // namespace android 572