AudioPlayer_to_android.cpp revision 7133228a478e16458b659946f2180ecddd13fda7
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#include "sles_allinclusive.h" 18#include "android_prompts.h" 19#include "android/android_AudioToCbRenderer.h" 20#include "android/android_StreamPlayer.h" 21#include "android/android_LocAVPlayer.h" 22#include "android/include/AacBqToPcmCbRenderer.h" 23 24#include <fcntl.h> 25#include <sys/stat.h> 26 27#include <system/audio.h> 28 29template class android::KeyedVector<SLuint32, android::AudioEffect* > ; 30 31#define KEY_STREAM_TYPE_PARAMSIZE sizeof(SLint32) 32 33#define AUDIOTRACK_MIN_PLAYBACKRATE_PERMILLE 500 34#define AUDIOTRACK_MAX_PLAYBACKRATE_PERMILLE 2000 35 36//----------------------------------------------------------------------------- 37// FIXME this method will be absorbed into android_audioPlayer_setPlayState() once 38// bufferqueue and uri/fd playback are moved under the GenericPlayer C++ object 39SLresult aplayer_setPlayState(const android::sp<android::GenericPlayer> &ap, SLuint32 playState, 40 AndroidObjectState* pObjState) { 41 SLresult result = SL_RESULT_SUCCESS; 42 AndroidObjectState objState = *pObjState; 43 44 switch (playState) { 45 case SL_PLAYSTATE_STOPPED: 46 SL_LOGV("setting GenericPlayer to SL_PLAYSTATE_STOPPED"); 47 ap->stop(); 48 break; 49 case SL_PLAYSTATE_PAUSED: 50 SL_LOGV("setting GenericPlayer to SL_PLAYSTATE_PAUSED"); 51 switch(objState) { 52 case ANDROID_UNINITIALIZED: 53 *pObjState = ANDROID_PREPARING; 54 ap->prepare(); 55 break; 56 case ANDROID_PREPARING: 57 break; 58 case ANDROID_READY: 59 ap->pause(); 60 break; 61 default: 62 SL_LOGE(ERROR_PLAYERSETPLAYSTATE_INVALID_OBJECT_STATE_D, playState); 63 result = SL_RESULT_INTERNAL_ERROR; 64 break; 65 } 66 break; 67 case SL_PLAYSTATE_PLAYING: { 68 SL_LOGV("setting GenericPlayer to SL_PLAYSTATE_PLAYING"); 69 switch(objState) { 70 case ANDROID_UNINITIALIZED: 71 *pObjState = ANDROID_PREPARING; 72 ap->prepare(); 73 // intended fall through 74 case ANDROID_PREPARING: 75 // intended fall through 76 case ANDROID_READY: 77 ap->play(); 78 break; 79 default: 80 SL_LOGE(ERROR_PLAYERSETPLAYSTATE_INVALID_OBJECT_STATE_D, playState); 81 result = SL_RESULT_INTERNAL_ERROR; 82 break; 83 } 84 } 85 break; 86 default: 87 // checked by caller, should not happen 88 SL_LOGE(ERROR_SHOULDNT_BE_HERE_S, "aplayer_setPlayState"); 89 result = SL_RESULT_INTERNAL_ERROR; 90 break; 91 } 92 93 return result; 94} 95 96 97//----------------------------------------------------------------------------- 98// Callback associated with a AudioToCbRenderer of an SL ES AudioPlayer that gets its data 99// from a URI or FD, to write the decoded audio data to a buffer queue 100static size_t adecoder_writeToBufferQueue(const uint8_t *data, size_t size, void* user) { 101 size_t sizeConsumed = 0; 102 if (NULL == user) { 103 return sizeConsumed; 104 } 105 SL_LOGD("received %d bytes from decoder", size); 106 CAudioPlayer *ap = (CAudioPlayer *)user; 107 slBufferQueueCallback callback = NULL; 108 void * callbackPContext = NULL; 109 110 // push decoded data to the buffer queue 111 object_lock_exclusive(&ap->mObject); 112 113 if (ap->mBufferQueue.mState.count != 0) { 114 assert(ap->mBufferQueue.mFront != ap->mBufferQueue.mRear); 115 116 BufferHeader *oldFront = ap->mBufferQueue.mFront; 117 BufferHeader *newFront = &oldFront[1]; 118 119 uint8_t *pDest = (uint8_t *)oldFront->mBuffer + ap->mBufferQueue.mSizeConsumed; 120 if (ap->mBufferQueue.mSizeConsumed + size < oldFront->mSize) { 121 // room to consume the whole or rest of the decoded data in one shot 122 ap->mBufferQueue.mSizeConsumed += size; 123 // consume data but no callback to the BufferQueue interface here 124 memcpy (pDest, data, size); 125 sizeConsumed = size; 126 } else { 127 // push as much as possible of the decoded data into the buffer queue 128 sizeConsumed = oldFront->mSize - ap->mBufferQueue.mSizeConsumed; 129 130 // the buffer at the head of the buffer queue is full, update the state 131 ap->mBufferQueue.mSizeConsumed = 0; 132 if (newFront == &ap->mBufferQueue.mArray[ap->mBufferQueue.mNumBuffers + 1]) { 133 newFront = ap->mBufferQueue.mArray; 134 } 135 ap->mBufferQueue.mFront = newFront; 136 137 ap->mBufferQueue.mState.count--; 138 ap->mBufferQueue.mState.playIndex++; 139 // consume data 140 memcpy (pDest, data, sizeConsumed); 141 // data has been copied to the buffer, and the buffer queue state has been updated 142 // we will notify the client if applicable 143 callback = ap->mBufferQueue.mCallback; 144 // save callback data 145 callbackPContext = ap->mBufferQueue.mContext; 146 } 147 148 } else { 149 // no available buffers in the queue to write the decoded data 150 sizeConsumed = 0; 151 } 152 153 object_unlock_exclusive(&ap->mObject); 154 // notify client 155 if (NULL != callback) { 156 (*callback)(&ap->mBufferQueue.mItf, callbackPContext); 157 } 158 159 return sizeConsumed; 160} 161 162//----------------------------------------------------------------------------- 163int android_getMinFrameCount(uint32_t sampleRate) { 164 int afSampleRate; 165 if (android::AudioSystem::getOutputSamplingRate(&afSampleRate, 166 ANDROID_DEFAULT_OUTPUT_STREAM_TYPE) != android::NO_ERROR) { 167 return ANDROID_DEFAULT_AUDIOTRACK_BUFFER_SIZE; 168 } 169 int afFrameCount; 170 if (android::AudioSystem::getOutputFrameCount(&afFrameCount, 171 ANDROID_DEFAULT_OUTPUT_STREAM_TYPE) != android::NO_ERROR) { 172 return ANDROID_DEFAULT_AUDIOTRACK_BUFFER_SIZE; 173 } 174 uint32_t afLatency; 175 if (android::AudioSystem::getOutputLatency(&afLatency, 176 ANDROID_DEFAULT_OUTPUT_STREAM_TYPE) != android::NO_ERROR) { 177 return ANDROID_DEFAULT_AUDIOTRACK_BUFFER_SIZE; 178 } 179 // minimum nb of buffers to cover output latency, given the size of each hardware audio buffer 180 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate); 181 if (minBufCount < 2) minBufCount = 2; 182 // minimum number of frames to cover output latency at the sample rate of the content 183 return (afFrameCount*sampleRate*minBufCount)/afSampleRate; 184} 185 186 187//----------------------------------------------------------------------------- 188#define LEFT_CHANNEL_MASK 0x1 << 0 189#define RIGHT_CHANNEL_MASK 0x1 << 1 190 191void android_audioPlayer_volumeUpdate(CAudioPlayer* ap) 192{ 193 assert(ap != NULL); 194 195 // the source's channel count, where zero means unknown 196 SLuint8 channelCount = ap->mNumChannels; 197 198 // whether each channel is audible 199 bool leftAudibilityFactor, rightAudibilityFactor; 200 201 // mute has priority over solo 202 if (channelCount >= STEREO_CHANNELS) { 203 if (ap->mMuteMask & LEFT_CHANNEL_MASK) { 204 // left muted 205 leftAudibilityFactor = false; 206 } else { 207 // left not muted 208 if (ap->mSoloMask & LEFT_CHANNEL_MASK) { 209 // left soloed 210 leftAudibilityFactor = true; 211 } else { 212 // left not soloed 213 if (ap->mSoloMask & RIGHT_CHANNEL_MASK) { 214 // right solo silences left 215 leftAudibilityFactor = false; 216 } else { 217 // left and right are not soloed, and left is not muted 218 leftAudibilityFactor = true; 219 } 220 } 221 } 222 223 if (ap->mMuteMask & RIGHT_CHANNEL_MASK) { 224 // right muted 225 rightAudibilityFactor = false; 226 } else { 227 // right not muted 228 if (ap->mSoloMask & RIGHT_CHANNEL_MASK) { 229 // right soloed 230 rightAudibilityFactor = true; 231 } else { 232 // right not soloed 233 if (ap->mSoloMask & LEFT_CHANNEL_MASK) { 234 // left solo silences right 235 rightAudibilityFactor = false; 236 } else { 237 // left and right are not soloed, and right is not muted 238 rightAudibilityFactor = true; 239 } 240 } 241 } 242 243 // channel mute and solo are ignored for mono and unknown channel count sources 244 } else { 245 leftAudibilityFactor = true; 246 rightAudibilityFactor = true; 247 } 248 249 // compute volumes without setting 250 const bool audibilityFactors[2] = {leftAudibilityFactor, rightAudibilityFactor}; 251 float volumes[2]; 252 android_player_volumeUpdate(volumes, &ap->mVolume, channelCount, ap->mAmplFromDirectLevel, 253 audibilityFactors); 254 float leftVol = volumes[0], rightVol = volumes[1]; 255 256 // set volume on the underlying media player or audio track 257 if (ap->mAPlayer != 0) { 258 ap->mAPlayer->setVolume(leftVol, rightVol); 259 } else if (ap->mAudioTrack != 0) { 260 ap->mAudioTrack->setVolume(leftVol, rightVol); 261 } 262 263 // changes in the AudioPlayer volume must be reflected in the send level: 264 // in SLEffectSendItf or in SLAndroidEffectSendItf? 265 // FIXME replace interface test by an internal API once we have one. 266 if (NULL != ap->mEffectSend.mItf) { 267 for (unsigned int i=0 ; i<AUX_MAX ; i++) { 268 if (ap->mEffectSend.mEnableLevels[i].mEnable) { 269 android_fxSend_setSendLevel(ap, 270 ap->mEffectSend.mEnableLevels[i].mSendLevel + ap->mVolume.mLevel); 271 // there's a single aux bus on Android, so we can stop looking once the first 272 // aux effect is found. 273 break; 274 } 275 } 276 } else if (NULL != ap->mAndroidEffectSend.mItf) { 277 android_fxSend_setSendLevel(ap, ap->mAndroidEffectSend.mSendLevel + ap->mVolume.mLevel); 278 } 279} 280 281// Called by android_audioPlayer_volumeUpdate and android_mediaPlayer_volumeUpdate to compute 282// volumes, but setting volumes is handled by the caller. 283 284void android_player_volumeUpdate(float *pVolumes /*[2]*/, const IVolume *volumeItf, unsigned 285channelCount, float amplFromDirectLevel, const bool *audibilityFactors /*[2]*/) 286{ 287 assert(pVolumes != NULL); 288 assert(volumeItf != NULL); 289 // OK for audibilityFactors to be NULL 290 291 bool leftAudibilityFactor, rightAudibilityFactor; 292 293 // apply player mute factor 294 // note that AudioTrack has mute() but not MediaPlayer, so it's easier to use volume 295 // to mute for both rather than calling mute() for AudioTrack 296 297 // player is muted 298 if (volumeItf->mMute) { 299 leftAudibilityFactor = false; 300 rightAudibilityFactor = false; 301 // player isn't muted, and channel mute/solo audibility factors are available (AudioPlayer) 302 } else if (audibilityFactors != NULL) { 303 leftAudibilityFactor = audibilityFactors[0]; 304 rightAudibilityFactor = audibilityFactors[1]; 305 // player isn't muted, and channel mute/solo audibility factors aren't available (MediaPlayer) 306 } else { 307 leftAudibilityFactor = true; 308 rightAudibilityFactor = true; 309 } 310 311 // compute amplification as the combination of volume level and stereo position 312 // amplification (or attenuation) from volume level 313 float amplFromVolLevel = sles_to_android_amplification(volumeItf->mLevel); 314 // amplification from direct level (changed in SLEffectSendtItf and SLAndroidEffectSendItf) 315 float leftVol = amplFromVolLevel * amplFromDirectLevel; 316 float rightVol = leftVol; 317 318 // amplification from stereo position 319 if (volumeItf->mEnableStereoPosition) { 320 // Left/right amplification (can be attenuations) factors derived for the StereoPosition 321 float amplFromStereoPos[STEREO_CHANNELS]; 322 // panning law depends on content channel count: mono to stereo panning vs stereo balance 323 if (1 == channelCount) { 324 // mono to stereo panning 325 double theta = (1000+volumeItf->mStereoPosition)*M_PI_4/1000.0f; // 0 <= theta <= Pi/2 326 amplFromStereoPos[0] = cos(theta); 327 amplFromStereoPos[1] = sin(theta); 328 // channel count is 0 (unknown), 2 (stereo), or > 2 (multi-channel) 329 } else { 330 // stereo balance 331 if (volumeItf->mStereoPosition > 0) { 332 amplFromStereoPos[0] = (1000-volumeItf->mStereoPosition)/1000.0f; 333 amplFromStereoPos[1] = 1.0f; 334 } else { 335 amplFromStereoPos[0] = 1.0f; 336 amplFromStereoPos[1] = (1000+volumeItf->mStereoPosition)/1000.0f; 337 } 338 } 339 leftVol *= amplFromStereoPos[0]; 340 rightVol *= amplFromStereoPos[1]; 341 } 342 343 // apply audibility factors 344 if (!leftAudibilityFactor) { 345 leftVol = 0.0; 346 } 347 if (!rightAudibilityFactor) { 348 rightVol = 0.0; 349 } 350 351 // return the computed volumes 352 pVolumes[0] = leftVol; 353 pVolumes[1] = rightVol; 354} 355 356//----------------------------------------------------------------------------- 357void audioTrack_handleMarker_lockPlay(CAudioPlayer* ap) { 358 //SL_LOGV("received event EVENT_MARKER from AudioTrack"); 359 slPlayCallback callback = NULL; 360 void* callbackPContext = NULL; 361 362 interface_lock_shared(&ap->mPlay); 363 callback = ap->mPlay.mCallback; 364 callbackPContext = ap->mPlay.mContext; 365 interface_unlock_shared(&ap->mPlay); 366 367 if (NULL != callback) { 368 // getting this event implies SL_PLAYEVENT_HEADATMARKER was set in the event mask 369 (*callback)(&ap->mPlay.mItf, callbackPContext, SL_PLAYEVENT_HEADATMARKER); 370 } 371} 372 373//----------------------------------------------------------------------------- 374void audioTrack_handleNewPos_lockPlay(CAudioPlayer* ap) { 375 //SL_LOGV("received event EVENT_NEW_POS from AudioTrack"); 376 slPlayCallback callback = NULL; 377 void* callbackPContext = NULL; 378 379 interface_lock_shared(&ap->mPlay); 380 callback = ap->mPlay.mCallback; 381 callbackPContext = ap->mPlay.mContext; 382 interface_unlock_shared(&ap->mPlay); 383 384 if (NULL != callback) { 385 // getting this event implies SL_PLAYEVENT_HEADATNEWPOS was set in the event mask 386 (*callback)(&ap->mPlay.mItf, callbackPContext, SL_PLAYEVENT_HEADATNEWPOS); 387 } 388} 389 390 391//----------------------------------------------------------------------------- 392void audioTrack_handleUnderrun_lockPlay(CAudioPlayer* ap) { 393 slPlayCallback callback = NULL; 394 void* callbackPContext = NULL; 395 396 interface_lock_shared(&ap->mPlay); 397 callback = ap->mPlay.mCallback; 398 callbackPContext = ap->mPlay.mContext; 399 bool headStalled = (ap->mPlay.mEventFlags & SL_PLAYEVENT_HEADSTALLED) != 0; 400 interface_unlock_shared(&ap->mPlay); 401 402 if ((NULL != callback) && headStalled) { 403 (*callback)(&ap->mPlay.mItf, callbackPContext, SL_PLAYEVENT_HEADSTALLED); 404 } 405} 406 407 408//----------------------------------------------------------------------------- 409/** 410 * post-condition: play state of AudioPlayer is SL_PLAYSTATE_PAUSED if setPlayStateToPaused is true 411 * 412 * note: a conditional flag, setPlayStateToPaused, is used here to specify whether the play state 413 * needs to be changed when the player reaches the end of the content to play. This is 414 * relative to what the specification describes for buffer queues vs the 415 * SL_PLAYEVENT_HEADATEND event. In the OpenSL ES specification 1.0.1: 416 * - section 8.12 SLBufferQueueItf states "In the case of starvation due to insufficient 417 * buffers in the queue, the playing of audio data stops. The player remains in the 418 * SL_PLAYSTATE_PLAYING state." 419 * - section 9.2.31 SL_PLAYEVENT states "SL_PLAYEVENT_HEADATEND Playback head is at the end 420 * of the current content and the player has paused." 421 */ 422void audioPlayer_dispatch_headAtEnd_lockPlay(CAudioPlayer *ap, bool setPlayStateToPaused, 423 bool needToLock) { 424 //SL_LOGV("ap=%p, setPlayStateToPaused=%d, needToLock=%d", ap, setPlayStateToPaused, 425 // needToLock); 426 slPlayCallback playCallback = NULL; 427 void * playContext = NULL; 428 // SLPlayItf callback or no callback? 429 if (needToLock) { 430 interface_lock_exclusive(&ap->mPlay); 431 } 432 if (ap->mPlay.mEventFlags & SL_PLAYEVENT_HEADATEND) { 433 playCallback = ap->mPlay.mCallback; 434 playContext = ap->mPlay.mContext; 435 } 436 if (setPlayStateToPaused) { 437 ap->mPlay.mState = SL_PLAYSTATE_PAUSED; 438 } 439 if (needToLock) { 440 interface_unlock_exclusive(&ap->mPlay); 441 } 442 // enqueue callback with no lock held 443 if (NULL != playCallback) { 444#ifndef USE_ASYNCHRONOUS_PLAY_CALLBACK 445 (*playCallback)(&ap->mPlay.mItf, playContext, SL_PLAYEVENT_HEADATEND); 446#else 447 SLresult result = EnqueueAsyncCallback_ppi(ap, playCallback, &ap->mPlay.mItf, playContext, 448 SL_PLAYEVENT_HEADATEND); 449 if (SL_RESULT_SUCCESS != result) { 450 LOGW("Callback %p(%p, %p, SL_PLAYEVENT_HEADATEND) dropped", playCallback, 451 &ap->mPlay.mItf, playContext); 452 } 453#endif 454 } 455 456} 457 458 459//----------------------------------------------------------------------------- 460/** 461 * pre-condition: AudioPlayer has SLPrefetchStatusItf initialized 462 * post-condition: 463 * - ap->mPrefetchStatus.mStatus == status 464 * - the prefetch status callback, if any, has been notified if a change occurred 465 * 466 */ 467void audioPlayer_dispatch_prefetchStatus_lockPrefetch(CAudioPlayer *ap, SLuint32 status, 468 bool needToLock) { 469 slPrefetchCallback prefetchCallback = NULL; 470 void * prefetchContext = NULL; 471 472 if (needToLock) { 473 interface_lock_exclusive(&ap->mPrefetchStatus); 474 } 475 // status change? 476 if (ap->mPrefetchStatus.mStatus != status) { 477 ap->mPrefetchStatus.mStatus = status; 478 // callback or no callback? 479 if (ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_STATUSCHANGE) { 480 prefetchCallback = ap->mPrefetchStatus.mCallback; 481 prefetchContext = ap->mPrefetchStatus.mContext; 482 } 483 } 484 if (needToLock) { 485 interface_unlock_exclusive(&ap->mPrefetchStatus); 486 } 487 488 // callback with no lock held 489 if (NULL != prefetchCallback) { 490 (*prefetchCallback)(&ap->mPrefetchStatus.mItf, prefetchContext, status); 491 } 492} 493 494 495//----------------------------------------------------------------------------- 496SLresult audioPlayer_setStreamType(CAudioPlayer* ap, SLint32 type) { 497 SLresult result = SL_RESULT_SUCCESS; 498 SL_LOGV("type %d", type); 499 500 int newStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE; 501 switch(type) { 502 case SL_ANDROID_STREAM_VOICE: 503 newStreamType = AUDIO_STREAM_VOICE_CALL; 504 break; 505 case SL_ANDROID_STREAM_SYSTEM: 506 newStreamType = AUDIO_STREAM_SYSTEM; 507 break; 508 case SL_ANDROID_STREAM_RING: 509 newStreamType = AUDIO_STREAM_RING; 510 break; 511 case SL_ANDROID_STREAM_MEDIA: 512 newStreamType = AUDIO_STREAM_MUSIC; 513 break; 514 case SL_ANDROID_STREAM_ALARM: 515 newStreamType = AUDIO_STREAM_ALARM; 516 break; 517 case SL_ANDROID_STREAM_NOTIFICATION: 518 newStreamType = AUDIO_STREAM_NOTIFICATION; 519 break; 520 default: 521 SL_LOGE(ERROR_PLAYERSTREAMTYPE_SET_UNKNOWN_TYPE); 522 result = SL_RESULT_PARAMETER_INVALID; 523 break; 524 } 525 526 // stream type needs to be set before the object is realized 527 // (ap->mAudioTrack is supposed to be NULL until then) 528 if (SL_OBJECT_STATE_UNREALIZED != ap->mObject.mState) { 529 SL_LOGE(ERROR_PLAYERSTREAMTYPE_REALIZED); 530 result = SL_RESULT_PRECONDITIONS_VIOLATED; 531 } else { 532 ap->mStreamType = newStreamType; 533 } 534 535 return result; 536} 537 538 539//----------------------------------------------------------------------------- 540SLresult audioPlayer_getStreamType(CAudioPlayer* ap, SLint32 *pType) { 541 SLresult result = SL_RESULT_SUCCESS; 542 543 switch(ap->mStreamType) { 544 case AUDIO_STREAM_VOICE_CALL: 545 *pType = SL_ANDROID_STREAM_VOICE; 546 break; 547 case AUDIO_STREAM_SYSTEM: 548 *pType = SL_ANDROID_STREAM_SYSTEM; 549 break; 550 case AUDIO_STREAM_RING: 551 *pType = SL_ANDROID_STREAM_RING; 552 break; 553 case AUDIO_STREAM_DEFAULT: 554 case AUDIO_STREAM_MUSIC: 555 *pType = SL_ANDROID_STREAM_MEDIA; 556 break; 557 case AUDIO_STREAM_ALARM: 558 *pType = SL_ANDROID_STREAM_ALARM; 559 break; 560 case AUDIO_STREAM_NOTIFICATION: 561 *pType = SL_ANDROID_STREAM_NOTIFICATION; 562 break; 563 default: 564 result = SL_RESULT_INTERNAL_ERROR; 565 *pType = SL_ANDROID_STREAM_MEDIA; 566 break; 567 } 568 569 return result; 570} 571 572 573//----------------------------------------------------------------------------- 574void audioPlayer_auxEffectUpdate(CAudioPlayer* ap) { 575 if ((ap->mAudioTrack != 0) && (ap->mAuxEffect != 0)) { 576 android_fxSend_attach(ap, true, ap->mAuxEffect, ap->mVolume.mLevel + ap->mAuxSendLevel); 577 } 578} 579 580 581//----------------------------------------------------------------------------- 582void audioPlayer_setInvalid(CAudioPlayer* ap) { 583 ap->mAndroidObjType = INVALID_TYPE; 584 ap->mpLock = NULL; 585} 586 587 588//----------------------------------------------------------------------------- 589/* 590 * returns true if the given data sink is supported by AudioPlayer that doesn't 591 * play to an OutputMix object, false otherwise 592 * 593 * pre-condition: the locator of the audio sink is not SL_DATALOCATOR_OUTPUTMIX 594 */ 595bool audioPlayer_isSupportedNonOutputMixSink(const SLDataSink* pAudioSink) { 596 bool result = true; 597 const SLuint32 sinkLocatorType = *(SLuint32 *)pAudioSink->pLocator; 598 const SLuint32 sinkFormatType = *(SLuint32 *)pAudioSink->pFormat; 599 600 switch (sinkLocatorType) { 601 602 case SL_DATALOCATOR_BUFFERQUEUE: 603 case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE: 604 if (SL_DATAFORMAT_PCM != sinkFormatType) { 605 SL_LOGE("Unsupported sink format 0x%x, expected SL_DATAFORMAT_PCM", 606 (unsigned)sinkFormatType); 607 result = false; 608 } 609 // it's no use checking the PCM format fields because additional characteristics 610 // such as the number of channels, or sample size are unknown to the player at this stage 611 break; 612 613 default: 614 SL_LOGE("Unsupported sink locator type 0x%x", (unsigned)sinkLocatorType); 615 result = false; 616 break; 617 } 618 619 return result; 620} 621 622 623//----------------------------------------------------------------------------- 624/* 625 * returns the Android object type if the locator type combinations for the source and sinks 626 * are supported by this implementation, INVALID_TYPE otherwise 627 */ 628AndroidObjectType audioPlayer_getAndroidObjectTypeForSourceSink(CAudioPlayer *ap) { 629 630 const SLDataSource *pAudioSrc = &ap->mDataSource.u.mSource; 631 const SLDataSink *pAudioSnk = &ap->mDataSink.u.mSink; 632 const SLuint32 sourceLocatorType = *(SLuint32 *)pAudioSrc->pLocator; 633 const SLuint32 sinkLocatorType = *(SLuint32 *)pAudioSnk->pLocator; 634 AndroidObjectType type = INVALID_TYPE; 635 636 //-------------------------------------- 637 // Sink / source matching check: 638 // the following source / sink combinations are supported 639 // SL_DATALOCATOR_BUFFERQUEUE / SL_DATALOCATOR_OUTPUTMIX 640 // SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE / SL_DATALOCATOR_OUTPUTMIX 641 // SL_DATALOCATOR_URI / SL_DATALOCATOR_OUTPUTMIX 642 // SL_DATALOCATOR_ANDROIDFD / SL_DATALOCATOR_OUTPUTMIX 643 // SL_DATALOCATOR_ANDROIDBUFFERQUEUE / SL_DATALOCATOR_OUTPUTMIX 644 // SL_DATALOCATOR_ANDROIDBUFFERQUEUE / SL_DATALOCATOR_BUFFERQUEUE 645 // SL_DATALOCATOR_URI / SL_DATALOCATOR_BUFFERQUEUE 646 // SL_DATALOCATOR_ANDROIDFD / SL_DATALOCATOR_BUFFERQUEUE 647 // SL_DATALOCATOR_URI / SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE 648 // SL_DATALOCATOR_ANDROIDFD / SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE 649 switch (sinkLocatorType) { 650 651 case SL_DATALOCATOR_OUTPUTMIX: { 652 switch (sourceLocatorType) { 653 654 // Buffer Queue to AudioTrack 655 case SL_DATALOCATOR_BUFFERQUEUE: 656 case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE: 657 type = AUDIOPLAYER_FROM_PCM_BUFFERQUEUE; 658 break; 659 660 // URI or FD to MediaPlayer 661 case SL_DATALOCATOR_URI: 662 case SL_DATALOCATOR_ANDROIDFD: 663 type = AUDIOPLAYER_FROM_URIFD; 664 break; 665 666 // Android BufferQueue to MediaPlayer (shared memory streaming) 667 case SL_DATALOCATOR_ANDROIDBUFFERQUEUE: 668 type = AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE; 669 break; 670 671 default: 672 SL_LOGE("Source data locator 0x%x not supported with SL_DATALOCATOR_OUTPUTMIX sink", 673 (unsigned)sourceLocatorType); 674 break; 675 } 676 } 677 break; 678 679 case SL_DATALOCATOR_BUFFERQUEUE: 680 case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE: 681 switch (sourceLocatorType) { 682 683 // URI or FD decoded to PCM in a buffer queue 684 case SL_DATALOCATOR_URI: 685 case SL_DATALOCATOR_ANDROIDFD: 686 type = AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE; 687 break; 688 689 // AAC ADTS Android buffer queue decoded to PCM in a buffer queue 690 case SL_DATALOCATOR_ANDROIDBUFFERQUEUE: 691 type = AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE; 692 break; 693 694 default: 695 SL_LOGE("Source data locator 0x%x not supported with SL_DATALOCATOR_BUFFERQUEUE sink", 696 (unsigned)sourceLocatorType); 697 break; 698 } 699 break; 700 701 default: 702 SL_LOGE("Sink data locator 0x%x not supported", (unsigned)sinkLocatorType); 703 break; 704 } 705 706 return type; 707} 708 709 710//----------------------------------------------------------------------------- 711/* 712 * Callback associated with an SfPlayer of an SL ES AudioPlayer that gets its data 713 * from a URI or FD, for prepare, prefetch, and play events 714 */ 715static void sfplayer_handlePrefetchEvent(int event, int data1, int data2, void* user) { 716 717 // FIXME see similar code and comment in player_handleMediaPlayerEventNotifications 718 719 if (NULL == user) { 720 return; 721 } 722 723 CAudioPlayer *ap = (CAudioPlayer *)user; 724 if (!android::CallbackProtector::enterCbIfOk(ap->mCallbackProtector)) { 725 // it is not safe to enter the callback (the track is about to go away) 726 return; 727 } 728 union { 729 char c[sizeof(int)]; 730 int i; 731 } u; 732 u.i = event; 733 SL_LOGV("sfplayer_handlePrefetchEvent(event='%c%c%c%c' (%d), data1=%d, data2=%d, user=%p) from " 734 "SfAudioPlayer", u.c[3], u.c[2], u.c[1], u.c[0], event, data1, data2, user); 735 switch(event) { 736 737 case android::GenericPlayer::kEventPrepared: { 738 739 if (PLAYER_SUCCESS != data1) { 740 object_lock_exclusive(&ap->mObject); 741 742 // already initialized at object creation, and can only prepare once so never reset 743 assert(ap->mAudioTrack == 0); 744 assert(ap->mNumChannels == UNKNOWN_NUMCHANNELS); 745 assert(ap->mSampleRateMilliHz == UNKNOWN_SAMPLERATE); 746 assert(ap->mAndroidObjState == ANDROID_PREPARING); 747 ap->mAndroidObjState = ANDROID_READY; 748 749 object_unlock_exclusive(&ap->mObject); 750 751 // SfPlayer prepare() failed prefetching, there is no event in SLPrefetchStatus to 752 // indicate a prefetch error, so we signal it by sending simulataneously two events: 753 // - SL_PREFETCHEVENT_FILLLEVELCHANGE with a level of 0 754 // - SL_PREFETCHEVENT_STATUSCHANGE with a status of SL_PREFETCHSTATUS_UNDERFLOW 755 SL_LOGE(ERROR_PLAYER_PREFETCH_d, data1); 756 if (!IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) { 757 break; 758 } 759 760 slPrefetchCallback callback = NULL; 761 void* callbackPContext = NULL; 762 763 interface_lock_exclusive(&ap->mPrefetchStatus); 764 ap->mPrefetchStatus.mLevel = 0; 765 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW; 766 if ((ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_FILLLEVELCHANGE) 767 && (ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_STATUSCHANGE)) { 768 callback = ap->mPrefetchStatus.mCallback; 769 callbackPContext = ap->mPrefetchStatus.mContext; 770 } 771 interface_unlock_exclusive(&ap->mPrefetchStatus); 772 773 // callback with no lock held 774 if (NULL != callback) { 775 (*callback)(&ap->mPrefetchStatus.mItf, callbackPContext, 776 SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE); 777 } 778 779 780 } else { 781 782 object_lock_exclusive(&ap->mObject); 783 784 if (AUDIOPLAYER_FROM_URIFD == ap->mAndroidObjType) { 785 //************************************** 786 // FIXME move under GenericMediaPlayer 787#if 0 788 ap->mAudioTrack = ap->mSfPlayer->getAudioTrack(); 789 ap->mNumChannels = ap->mSfPlayer->getNumChannels(); 790 ap->mSampleRateMilliHz = 791 android_to_sles_sampleRate(ap->mSfPlayer->getSampleRateHz()); 792 ap->mSfPlayer->startPrefetch_async(); 793 // update the new track with the current settings 794 audioPlayer_auxEffectUpdate(ap); 795 android_audioPlayer_useEventMask(ap); 796 android_audioPlayer_volumeUpdate(ap); 797 android_audioPlayer_setPlayRate(ap, ap->mPlaybackRate.mRate, false /*lockAP*/); 798#endif 799 } else if (AUDIOPLAYER_FROM_PCM_BUFFERQUEUE == ap->mAndroidObjType) { 800 if (ap->mAPlayer != 0) { 801 ((android::AudioToCbRenderer*)ap->mAPlayer.get())->startPrefetch_async(); 802 } 803 } else if (AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE == ap->mAndroidObjType) { 804 SL_LOGD("Received SfPlayer::kEventPrepared from AVPlayer for CAudioPlayer %p", ap); 805 } 806 807 ap->mAndroidObjState = ANDROID_READY; 808 809 object_unlock_exclusive(&ap->mObject); 810 } 811 812 } 813 break; 814 815 case android::GenericPlayer::kEventPrefetchFillLevelUpdate : { 816 if (!IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) { 817 break; 818 } 819 slPrefetchCallback callback = NULL; 820 void* callbackPContext = NULL; 821 822 // SLPrefetchStatusItf callback or no callback? 823 interface_lock_exclusive(&ap->mPrefetchStatus); 824 if (ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_FILLLEVELCHANGE) { 825 callback = ap->mPrefetchStatus.mCallback; 826 callbackPContext = ap->mPrefetchStatus.mContext; 827 } 828 ap->mPrefetchStatus.mLevel = (SLpermille)data1; 829 interface_unlock_exclusive(&ap->mPrefetchStatus); 830 831 // callback with no lock held 832 if (NULL != callback) { 833 (*callback)(&ap->mPrefetchStatus.mItf, callbackPContext, 834 SL_PREFETCHEVENT_FILLLEVELCHANGE); 835 } 836 } 837 break; 838 839 case android::GenericPlayer::kEventPrefetchStatusChange: { 840 if (!IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) { 841 break; 842 } 843 slPrefetchCallback callback = NULL; 844 void* callbackPContext = NULL; 845 846 // SLPrefetchStatusItf callback or no callback? 847 object_lock_exclusive(&ap->mObject); 848 if (ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_STATUSCHANGE) { 849 callback = ap->mPrefetchStatus.mCallback; 850 callbackPContext = ap->mPrefetchStatus.mContext; 851 } 852 if (data1 >= android::kStatusIntermediate) { 853 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_SUFFICIENTDATA; 854 ap->mAndroidObjState = ANDROID_READY; 855 } else if (data1 < android::kStatusIntermediate) { 856 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW; 857 } 858 object_unlock_exclusive(&ap->mObject); 859 860 // callback with no lock held 861 if (NULL != callback) { 862 (*callback)(&ap->mPrefetchStatus.mItf, callbackPContext, SL_PREFETCHEVENT_STATUSCHANGE); 863 } 864 } 865 break; 866 867 case android::GenericPlayer::kEventEndOfStream: { 868 audioPlayer_dispatch_headAtEnd_lockPlay(ap, true /*set state to paused?*/, true); 869 if ((ap->mAudioTrack != 0) && (!ap->mSeek.mLoopEnabled)) { 870 ap->mAudioTrack->stop(); 871 } 872 } 873 break; 874 875 case android::GenericPlayer::kEventChannelCount: { 876 object_lock_exclusive(&ap->mObject); 877 if (UNKNOWN_NUMCHANNELS == ap->mNumChannels && UNKNOWN_NUMCHANNELS != data1) { 878 ap->mNumChannels = data1; 879 android_audioPlayer_volumeUpdate(ap); 880 } 881 object_unlock_exclusive(&ap->mObject); 882 } 883 break; 884 885 case android::GenericPlayer::kEventPlay: { 886 slPlayCallback callback = NULL; 887 void* callbackPContext = NULL; 888 889 interface_lock_shared(&ap->mPlay); 890 callback = ap->mPlay.mCallback; 891 callbackPContext = ap->mPlay.mContext; 892 interface_unlock_shared(&ap->mPlay); 893 894 if (NULL != callback) { 895 SLuint32 event = (SLuint32) data1; // SL_PLAYEVENT_HEAD* 896#ifndef USE_ASYNCHRONOUS_PLAY_CALLBACK 897 // synchronous callback requires a synchronous GetPosition implementation 898 (*callback)(&ap->mPlay.mItf, callbackPContext, event); 899#else 900 // asynchronous callback works with any GetPosition implementation 901 SLresult result = EnqueueAsyncCallback_ppi(ap, callback, &ap->mPlay.mItf, 902 callbackPContext, event); 903 if (SL_RESULT_SUCCESS != result) { 904 LOGW("Callback %p(%p, %p, 0x%x) dropped", callback, 905 &ap->mPlay.mItf, callbackPContext, event); 906 } 907#endif 908 } 909 } 910 break; 911 912 default: 913 break; 914 } 915 916 ap->mCallbackProtector->exitCb(); 917} 918 919 920//----------------------------------------------------------------------------- 921SLresult android_audioPlayer_checkSourceSink(CAudioPlayer *pAudioPlayer) 922{ 923 // verify that the locator types for the source / sink combination is supported 924 pAudioPlayer->mAndroidObjType = audioPlayer_getAndroidObjectTypeForSourceSink(pAudioPlayer); 925 if (INVALID_TYPE == pAudioPlayer->mAndroidObjType) { 926 return SL_RESULT_PARAMETER_INVALID; 927 } 928 929 const SLDataSource *pAudioSrc = &pAudioPlayer->mDataSource.u.mSource; 930 const SLDataSink *pAudioSnk = &pAudioPlayer->mDataSink.u.mSink; 931 932 // format check: 933 const SLuint32 sourceLocatorType = *(SLuint32 *)pAudioSrc->pLocator; 934 const SLuint32 sinkLocatorType = *(SLuint32 *)pAudioSnk->pLocator; 935 const SLuint32 sourceFormatType = *(SLuint32 *)pAudioSrc->pFormat; 936 const SLuint32 sinkFormatType = *(SLuint32 *)pAudioSnk->pFormat; 937 938 switch (sourceLocatorType) { 939 //------------------ 940 // Buffer Queues 941 case SL_DATALOCATOR_BUFFERQUEUE: 942 case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE: 943 { 944 SLDataLocator_BufferQueue *dl_bq = (SLDataLocator_BufferQueue *) pAudioSrc->pLocator; 945 946 // Buffer format 947 switch (sourceFormatType) { 948 // currently only PCM buffer queues are supported, 949 case SL_DATAFORMAT_PCM: { 950 SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *) pAudioSrc->pFormat; 951 switch (df_pcm->numChannels) { 952 case 1: 953 case 2: 954 break; 955 default: 956 // this should have already been rejected by checkDataFormat 957 SL_LOGE("Cannot create audio player: unsupported " \ 958 "PCM data source with %u channels", (unsigned) df_pcm->numChannels); 959 return SL_RESULT_CONTENT_UNSUPPORTED; 960 } 961 switch (df_pcm->samplesPerSec) { 962 case SL_SAMPLINGRATE_8: 963 case SL_SAMPLINGRATE_11_025: 964 case SL_SAMPLINGRATE_12: 965 case SL_SAMPLINGRATE_16: 966 case SL_SAMPLINGRATE_22_05: 967 case SL_SAMPLINGRATE_24: 968 case SL_SAMPLINGRATE_32: 969 case SL_SAMPLINGRATE_44_1: 970 case SL_SAMPLINGRATE_48: 971 break; 972 case SL_SAMPLINGRATE_64: 973 case SL_SAMPLINGRATE_88_2: 974 case SL_SAMPLINGRATE_96: 975 case SL_SAMPLINGRATE_192: 976 default: 977 SL_LOGE("Cannot create audio player: unsupported sample rate %u milliHz", 978 (unsigned) df_pcm->samplesPerSec); 979 return SL_RESULT_CONTENT_UNSUPPORTED; 980 } 981 switch (df_pcm->bitsPerSample) { 982 case SL_PCMSAMPLEFORMAT_FIXED_8: 983 case SL_PCMSAMPLEFORMAT_FIXED_16: 984 break; 985 // others 986 default: 987 // this should have already been rejected by checkDataFormat 988 SL_LOGE("Cannot create audio player: unsupported sample bit depth %u", 989 (SLuint32)df_pcm->bitsPerSample); 990 return SL_RESULT_CONTENT_UNSUPPORTED; 991 } 992 switch (df_pcm->containerSize) { 993 case 8: 994 case 16: 995 break; 996 // others 997 default: 998 SL_LOGE("Cannot create audio player: unsupported container size %u", 999 (unsigned) df_pcm->containerSize); 1000 return SL_RESULT_CONTENT_UNSUPPORTED; 1001 } 1002 // df_pcm->channelMask: the earlier platform-independent check and the 1003 // upcoming check by sles_to_android_channelMaskOut are sufficient 1004 switch (df_pcm->endianness) { 1005 case SL_BYTEORDER_LITTLEENDIAN: 1006 break; 1007 case SL_BYTEORDER_BIGENDIAN: 1008 SL_LOGE("Cannot create audio player: unsupported big-endian byte order"); 1009 return SL_RESULT_CONTENT_UNSUPPORTED; 1010 // native is proposed but not yet in spec 1011 default: 1012 SL_LOGE("Cannot create audio player: unsupported byte order %u", 1013 (unsigned) df_pcm->endianness); 1014 return SL_RESULT_CONTENT_UNSUPPORTED; 1015 } 1016 } //case SL_DATAFORMAT_PCM 1017 break; 1018 case SL_DATAFORMAT_MIME: 1019 case XA_DATAFORMAT_RAWIMAGE: 1020 SL_LOGE("Cannot create audio player with buffer queue data source " 1021 "without SL_DATAFORMAT_PCM format"); 1022 return SL_RESULT_CONTENT_UNSUPPORTED; 1023 default: 1024 // invalid data format is detected earlier 1025 assert(false); 1026 return SL_RESULT_INTERNAL_ERROR; 1027 } // switch (sourceFormatType) 1028 } // case SL_DATALOCATOR_BUFFERQUEUE or SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE 1029 break; 1030 //------------------ 1031 // URI 1032 case SL_DATALOCATOR_URI: 1033 { 1034 SLDataLocator_URI *dl_uri = (SLDataLocator_URI *) pAudioSrc->pLocator; 1035 if (NULL == dl_uri->URI) { 1036 return SL_RESULT_PARAMETER_INVALID; 1037 } 1038 // URI format 1039 switch (sourceFormatType) { 1040 case SL_DATAFORMAT_MIME: 1041 break; 1042 case SL_DATAFORMAT_PCM: 1043 case XA_DATAFORMAT_RAWIMAGE: 1044 SL_LOGE("Cannot create audio player with SL_DATALOCATOR_URI data source without " 1045 "SL_DATAFORMAT_MIME format"); 1046 return SL_RESULT_CONTENT_UNSUPPORTED; 1047 } // switch (sourceFormatType) 1048 // decoding format check 1049 if ((sinkLocatorType != SL_DATALOCATOR_OUTPUTMIX) && 1050 !audioPlayer_isSupportedNonOutputMixSink(pAudioSnk)) { 1051 return SL_RESULT_CONTENT_UNSUPPORTED; 1052 } 1053 } // case SL_DATALOCATOR_URI 1054 break; 1055 //------------------ 1056 // File Descriptor 1057 case SL_DATALOCATOR_ANDROIDFD: 1058 { 1059 // fd is already non null 1060 switch (sourceFormatType) { 1061 case SL_DATAFORMAT_MIME: 1062 break; 1063 case SL_DATAFORMAT_PCM: 1064 // FIXME implement 1065 SL_LOGD("[ FIXME implement PCM FD data sources ]"); 1066 break; 1067 case XA_DATAFORMAT_RAWIMAGE: 1068 SL_LOGE("Cannot create audio player with SL_DATALOCATOR_ANDROIDFD data source " 1069 "without SL_DATAFORMAT_MIME or SL_DATAFORMAT_PCM format"); 1070 return SL_RESULT_CONTENT_UNSUPPORTED; 1071 default: 1072 // invalid data format is detected earlier 1073 assert(false); 1074 return SL_RESULT_INTERNAL_ERROR; 1075 } // switch (sourceFormatType) 1076 if ((sinkLocatorType != SL_DATALOCATOR_OUTPUTMIX) && 1077 !audioPlayer_isSupportedNonOutputMixSink(pAudioSnk)) { 1078 return SL_RESULT_CONTENT_UNSUPPORTED; 1079 } 1080 } // case SL_DATALOCATOR_ANDROIDFD 1081 break; 1082 //------------------ 1083 // Stream 1084 case SL_DATALOCATOR_ANDROIDBUFFERQUEUE: 1085 { 1086 switch (sourceFormatType) { 1087 case SL_DATAFORMAT_MIME: 1088 { 1089 SLDataFormat_MIME *df_mime = (SLDataFormat_MIME *) pAudioSrc->pFormat; 1090 if (NULL == df_mime) { 1091 SL_LOGE("MIME type null invalid"); 1092 return SL_RESULT_CONTENT_UNSUPPORTED; 1093 } 1094 SL_LOGD("source MIME is %s", (char*)df_mime->mimeType); 1095 switch(df_mime->containerType) { 1096 case SL_CONTAINERTYPE_MPEG_TS: 1097 if (strcasecmp((char*)df_mime->mimeType, ANDROID_MIME_MP2TS)) { 1098 SL_LOGE("Invalid MIME (%s) for container SL_CONTAINERTYPE_MPEG_TS, expects %s", 1099 (char*)df_mime->mimeType, ANDROID_MIME_MP2TS); 1100 return SL_RESULT_CONTENT_UNSUPPORTED; 1101 } 1102 break; 1103 case SL_CONTAINERTYPE_RAW: 1104 case SL_CONTAINERTYPE_AAC: 1105 if (strcasecmp((char*)df_mime->mimeType, ANDROID_MIME_AACADTS) && 1106 strcasecmp((char*)df_mime->mimeType, 1107 ANDROID_MIME_AACADTS_ANDROID_FRAMEWORK)) { 1108 SL_LOGE("Invalid MIME (%s) for container type %d, expects %s", 1109 (char*)df_mime->mimeType, df_mime->containerType, 1110 ANDROID_MIME_AACADTS); 1111 return SL_RESULT_CONTENT_UNSUPPORTED; 1112 } 1113 break; 1114 default: 1115 SL_LOGE("Cannot create player with SL_DATALOCATOR_ANDROIDBUFFERQUEUE data source " 1116 "that is not fed MPEG-2 TS data or AAC ADTS data"); 1117 return SL_RESULT_CONTENT_UNSUPPORTED; 1118 } 1119 } 1120 break; 1121 default: 1122 SL_LOGE("Cannot create player with SL_DATALOCATOR_ANDROIDBUFFERQUEUE data source " 1123 "without SL_DATAFORMAT_MIME format"); 1124 return SL_RESULT_CONTENT_UNSUPPORTED; 1125 } 1126 } 1127 break; // case SL_DATALOCATOR_ANDROIDBUFFERQUEUE 1128 //------------------ 1129 // Address 1130 case SL_DATALOCATOR_ADDRESS: 1131 case SL_DATALOCATOR_IODEVICE: 1132 case SL_DATALOCATOR_OUTPUTMIX: 1133 case XA_DATALOCATOR_NATIVEDISPLAY: 1134 case SL_DATALOCATOR_MIDIBUFFERQUEUE: 1135 SL_LOGE("Cannot create audio player with data locator type 0x%x", 1136 (unsigned) sourceLocatorType); 1137 return SL_RESULT_CONTENT_UNSUPPORTED; 1138 default: 1139 SL_LOGE("Cannot create audio player with invalid data locator type 0x%x", 1140 (unsigned) sourceLocatorType); 1141 return SL_RESULT_PARAMETER_INVALID; 1142 }// switch (locatorType) 1143 1144 return SL_RESULT_SUCCESS; 1145} 1146 1147 1148 1149//----------------------------------------------------------------------------- 1150static void audioTrack_callBack_uri(int event, void* user, void *info) { 1151 // EVENT_MORE_DATA needs to be handled with priority over the other events 1152 // because it will be called the most often during playback 1153 1154 if (event == android::AudioTrack::EVENT_MORE_DATA) { 1155 //SL_LOGV("received event EVENT_MORE_DATA from AudioTrack"); 1156 // set size to 0 to signal we're not using the callback to write more data 1157 android::AudioTrack::Buffer* pBuff = (android::AudioTrack::Buffer*)info; 1158 pBuff->size = 0; 1159 } else if (NULL != user) { 1160 CAudioPlayer *ap = (CAudioPlayer *)user; 1161 if (!android::CallbackProtector::enterCbIfOk(ap->mCallbackProtector)) { 1162 // it is not safe to enter the callback (the track is about to go away) 1163 return; 1164 } 1165 switch (event) { 1166 case android::AudioTrack::EVENT_MARKER : 1167 audioTrack_handleMarker_lockPlay(ap); 1168 break; 1169 case android::AudioTrack::EVENT_NEW_POS : 1170 audioTrack_handleNewPos_lockPlay(ap); 1171 break; 1172 case android::AudioTrack::EVENT_UNDERRUN : 1173 audioTrack_handleUnderrun_lockPlay(ap); 1174 break; 1175 case android::AudioTrack::EVENT_BUFFER_END : 1176 case android::AudioTrack::EVENT_LOOP_END : 1177 break; 1178 default: 1179 SL_LOGE("Encountered unknown AudioTrack event %d for CAudioPlayer %p", event, 1180 ap); 1181 break; 1182 } 1183 ap->mCallbackProtector->exitCb(); 1184 } 1185} 1186 1187//----------------------------------------------------------------------------- 1188// Callback associated with an AudioTrack of an SL ES AudioPlayer that gets its data 1189// from a buffer queue. This will not be called once the AudioTrack has been destroyed. 1190static void audioTrack_callBack_pullFromBuffQueue(int event, void* user, void *info) { 1191 CAudioPlayer *ap = (CAudioPlayer *)user; 1192 1193 if (!android::CallbackProtector::enterCbIfOk(ap->mCallbackProtector)) { 1194 // it is not safe to enter the callback (the track is about to go away) 1195 return; 1196 } 1197 1198 void * callbackPContext = NULL; 1199 switch(event) { 1200 1201 case android::AudioTrack::EVENT_MORE_DATA: { 1202 //SL_LOGV("received event EVENT_MORE_DATA from AudioTrack TID=%d", gettid()); 1203 slBufferQueueCallback callback = NULL; 1204 slPrefetchCallback prefetchCallback = NULL; 1205 void *prefetchContext = NULL; 1206 SLuint32 prefetchEvents = SL_PREFETCHEVENT_NONE; 1207 android::AudioTrack::Buffer* pBuff = (android::AudioTrack::Buffer*)info; 1208 1209 // retrieve data from the buffer queue 1210 interface_lock_exclusive(&ap->mBufferQueue); 1211 1212 if (ap->mBufferQueue.mState.count != 0) { 1213 //SL_LOGV("nbBuffers in queue = %u",ap->mBufferQueue.mState.count); 1214 assert(ap->mBufferQueue.mFront != ap->mBufferQueue.mRear); 1215 1216 BufferHeader *oldFront = ap->mBufferQueue.mFront; 1217 BufferHeader *newFront = &oldFront[1]; 1218 1219 // declared as void * because this code supports both 8-bit and 16-bit PCM data 1220 void *pSrc = (char *)oldFront->mBuffer + ap->mBufferQueue.mSizeConsumed; 1221 if (ap->mBufferQueue.mSizeConsumed + pBuff->size < oldFront->mSize) { 1222 // can't consume the whole or rest of the buffer in one shot 1223 ap->mBufferQueue.mSizeConsumed += pBuff->size; 1224 // leave pBuff->size untouched 1225 // consume data 1226 // FIXME can we avoid holding the lock during the copy? 1227 memcpy (pBuff->raw, pSrc, pBuff->size); 1228 } else { 1229 // finish consuming the buffer or consume the buffer in one shot 1230 pBuff->size = oldFront->mSize - ap->mBufferQueue.mSizeConsumed; 1231 ap->mBufferQueue.mSizeConsumed = 0; 1232 1233 if (newFront == 1234 &ap->mBufferQueue.mArray 1235 [ap->mBufferQueue.mNumBuffers + 1]) 1236 { 1237 newFront = ap->mBufferQueue.mArray; 1238 } 1239 ap->mBufferQueue.mFront = newFront; 1240 1241 ap->mBufferQueue.mState.count--; 1242 ap->mBufferQueue.mState.playIndex++; 1243 1244 // consume data 1245 // FIXME can we avoid holding the lock during the copy? 1246 memcpy (pBuff->raw, pSrc, pBuff->size); 1247 1248 // data has been consumed, and the buffer queue state has been updated 1249 // we will notify the client if applicable 1250 callback = ap->mBufferQueue.mCallback; 1251 // save callback data 1252 callbackPContext = ap->mBufferQueue.mContext; 1253 } 1254 } else { // empty queue 1255 // signal no data available 1256 pBuff->size = 0; 1257 1258 // signal we're at the end of the content, but don't pause (see note in function) 1259 audioPlayer_dispatch_headAtEnd_lockPlay(ap, false /*set state to paused?*/, false); 1260 1261 // signal underflow to prefetch status itf 1262 if (IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) { 1263 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW; 1264 ap->mPrefetchStatus.mLevel = 0; 1265 // callback or no callback? 1266 prefetchEvents = ap->mPrefetchStatus.mCallbackEventsMask & 1267 (SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE); 1268 if (SL_PREFETCHEVENT_NONE != prefetchEvents) { 1269 prefetchCallback = ap->mPrefetchStatus.mCallback; 1270 prefetchContext = ap->mPrefetchStatus.mContext; 1271 } 1272 } 1273 1274 // stop the track so it restarts playing faster when new data is enqueued 1275 ap->mAudioTrack->stop(); 1276 } 1277 interface_unlock_exclusive(&ap->mBufferQueue); 1278 1279 // notify client 1280 if (NULL != prefetchCallback) { 1281 assert(SL_PREFETCHEVENT_NONE != prefetchEvents); 1282 // spec requires separate callbacks for each event 1283 if (prefetchEvents & SL_PREFETCHEVENT_STATUSCHANGE) { 1284 (*prefetchCallback)(&ap->mPrefetchStatus.mItf, prefetchContext, 1285 SL_PREFETCHEVENT_STATUSCHANGE); 1286 } 1287 if (prefetchEvents & SL_PREFETCHEVENT_FILLLEVELCHANGE) { 1288 (*prefetchCallback)(&ap->mPrefetchStatus.mItf, prefetchContext, 1289 SL_PREFETCHEVENT_FILLLEVELCHANGE); 1290 } 1291 } 1292 if (NULL != callback) { 1293 (*callback)(&ap->mBufferQueue.mItf, callbackPContext); 1294 } 1295 } 1296 break; 1297 1298 case android::AudioTrack::EVENT_MARKER: 1299 //SL_LOGI("received event EVENT_MARKER from AudioTrack"); 1300 audioTrack_handleMarker_lockPlay(ap); 1301 break; 1302 1303 case android::AudioTrack::EVENT_NEW_POS: 1304 //SL_LOGI("received event EVENT_NEW_POS from AudioTrack"); 1305 audioTrack_handleNewPos_lockPlay(ap); 1306 break; 1307 1308 case android::AudioTrack::EVENT_UNDERRUN: 1309 //SL_LOGI("received event EVENT_UNDERRUN from AudioTrack"); 1310 audioTrack_handleUnderrun_lockPlay(ap); 1311 break; 1312 1313 default: 1314 // FIXME where does the notification of SL_PLAYEVENT_HEADMOVING fit? 1315 SL_LOGE("Encountered unknown AudioTrack event %d for CAudioPlayer %p", event, 1316 (CAudioPlayer *)user); 1317 break; 1318 } 1319 1320 ap->mCallbackProtector->exitCb(); 1321} 1322 1323 1324//----------------------------------------------------------------------------- 1325SLresult android_audioPlayer_create(CAudioPlayer *pAudioPlayer) { 1326 1327 SLresult result = SL_RESULT_SUCCESS; 1328 // pAudioPlayer->mAndroidObjType has been set in audioPlayer_getAndroidObjectTypeForSourceSink() 1329 if (INVALID_TYPE == pAudioPlayer->mAndroidObjType) { 1330 audioPlayer_setInvalid(pAudioPlayer); 1331 result = SL_RESULT_PARAMETER_INVALID; 1332 } else { 1333 1334 // These initializations are in the same order as the field declarations in classes.h 1335 1336 // FIXME Consolidate initializations (many of these already in IEngine_CreateAudioPlayer) 1337 pAudioPlayer->mpLock = new android::Mutex(); 1338 // mAndroidObjType: see above comment 1339 pAudioPlayer->mAndroidObjState = ANDROID_UNINITIALIZED; 1340 pAudioPlayer->mSessionId = android::AudioSystem::newAudioSessionId(); 1341 pAudioPlayer->mStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE; 1342 1343 // mAudioTrack 1344 pAudioPlayer->mCallbackProtector = new android::CallbackProtector(); 1345 // mAPLayer 1346 // mAuxEffect 1347 1348 pAudioPlayer->mAuxSendLevel = 0; 1349 pAudioPlayer->mAmplFromDirectLevel = 1.0f; // matches initial mDirectLevel value 1350 pAudioPlayer->mDeferredStart = false; 1351 1352 // Already initialized in IEngine_CreateAudioPlayer, to be consolidated 1353 pAudioPlayer->mDirectLevel = 0; // no attenuation 1354 1355 // This section re-initializes interface-specific fields that 1356 // can be set or used regardless of whether the interface is 1357 // exposed on the AudioPlayer or not 1358 1359 // Only AudioTrack supports a non-trivial playback rate 1360 switch (pAudioPlayer->mAndroidObjType) { 1361 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 1362 pAudioPlayer->mPlaybackRate.mMinRate = AUDIOTRACK_MIN_PLAYBACKRATE_PERMILLE; 1363 pAudioPlayer->mPlaybackRate.mMaxRate = AUDIOTRACK_MAX_PLAYBACKRATE_PERMILLE; 1364 break; 1365 default: 1366 // use the default range 1367 break; 1368 } 1369 1370 } 1371 1372 return result; 1373} 1374 1375 1376//----------------------------------------------------------------------------- 1377SLresult android_audioPlayer_setConfig(CAudioPlayer *ap, const SLchar *configKey, 1378 const void *pConfigValue, SLuint32 valueSize) { 1379 1380 SLresult result; 1381 1382 assert(NULL != ap && NULL != configKey && NULL != pConfigValue); 1383 if(strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_STREAM_TYPE) == 0) { 1384 1385 // stream type 1386 if (KEY_STREAM_TYPE_PARAMSIZE > valueSize) { 1387 SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW); 1388 result = SL_RESULT_BUFFER_INSUFFICIENT; 1389 } else { 1390 result = audioPlayer_setStreamType(ap, *(SLuint32*)pConfigValue); 1391 } 1392 1393 } else { 1394 SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY); 1395 result = SL_RESULT_PARAMETER_INVALID; 1396 } 1397 1398 return result; 1399} 1400 1401 1402//----------------------------------------------------------------------------- 1403SLresult android_audioPlayer_getConfig(CAudioPlayer* ap, const SLchar *configKey, 1404 SLuint32* pValueSize, void *pConfigValue) { 1405 1406 SLresult result; 1407 1408 assert(NULL != ap && NULL != configKey && NULL != pValueSize); 1409 if(strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_STREAM_TYPE) == 0) { 1410 1411 // stream type 1412 if (NULL == pConfigValue) { 1413 result = SL_RESULT_SUCCESS; 1414 } else if (KEY_STREAM_TYPE_PARAMSIZE > *pValueSize) { 1415 SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW); 1416 result = SL_RESULT_BUFFER_INSUFFICIENT; 1417 } else { 1418 result = audioPlayer_getStreamType(ap, (SLint32*)pConfigValue); 1419 } 1420 *pValueSize = KEY_STREAM_TYPE_PARAMSIZE; 1421 1422 } else { 1423 SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY); 1424 result = SL_RESULT_PARAMETER_INVALID; 1425 } 1426 1427 return result; 1428} 1429 1430 1431//----------------------------------------------------------------------------- 1432// FIXME abstract out the diff between CMediaPlayer and CAudioPlayer 1433SLresult android_audioPlayer_realize(CAudioPlayer *pAudioPlayer, SLboolean async) { 1434 1435 SLresult result = SL_RESULT_SUCCESS; 1436 SL_LOGV("Realize pAudioPlayer=%p", pAudioPlayer); 1437 1438 switch (pAudioPlayer->mAndroidObjType) { 1439 //----------------------------------- 1440 // AudioTrack 1441 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 1442 { 1443 // initialize platform-specific CAudioPlayer fields 1444 1445 SLDataLocator_BufferQueue *dl_bq = (SLDataLocator_BufferQueue *) 1446 pAudioPlayer->mDynamicSource.mDataSource; 1447 SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *) 1448 pAudioPlayer->mDynamicSource.mDataSource->pFormat; 1449 1450 uint32_t sampleRate = sles_to_android_sampleRate(df_pcm->samplesPerSec); 1451 1452 pAudioPlayer->mAudioTrack = new android::AudioTrackProxy(new android::AudioTrack( 1453 pAudioPlayer->mStreamType, // streamType 1454 sampleRate, // sampleRate 1455 sles_to_android_sampleFormat(df_pcm->bitsPerSample), // format 1456 sles_to_android_channelMaskOut(df_pcm->numChannels, df_pcm->channelMask), 1457 //channel mask 1458 0, // frameCount (here min) 1459 0, // flags 1460 audioTrack_callBack_pullFromBuffQueue, // callback 1461 (void *) pAudioPlayer, // user 1462 0 // FIXME find appropriate frame count // notificationFrame 1463 , pAudioPlayer->mSessionId 1464 )); 1465 android::status_t status = pAudioPlayer->mAudioTrack->initCheck(); 1466 if (status != android::NO_ERROR) { 1467 SL_LOGE("AudioTrack::initCheck status %u", status); 1468 result = SL_RESULT_CONTENT_UNSUPPORTED; 1469 pAudioPlayer->mAudioTrack.clear(); 1470 return result; 1471 } 1472 1473 // initialize platform-independent CAudioPlayer fields 1474 1475 pAudioPlayer->mNumChannels = df_pcm->numChannels; 1476 pAudioPlayer->mSampleRateMilliHz = df_pcm->samplesPerSec; // Note: bad field name in SL ES 1477 1478 pAudioPlayer->mAndroidObjState = ANDROID_READY; 1479 } 1480 break; 1481 //----------------------------------- 1482 // MediaPlayer 1483 case AUDIOPLAYER_FROM_URIFD: { 1484 assert(pAudioPlayer->mAndroidObjState == ANDROID_UNINITIALIZED); 1485 assert(pAudioPlayer->mNumChannels == UNKNOWN_NUMCHANNELS); 1486 assert(pAudioPlayer->mSampleRateMilliHz == UNKNOWN_SAMPLERATE); 1487 assert(pAudioPlayer->mAudioTrack == 0); 1488 1489 AudioPlayback_Parameters app; 1490 app.sessionId = pAudioPlayer->mSessionId; 1491 app.streamType = pAudioPlayer->mStreamType; 1492 app.trackcb = audioTrack_callBack_uri; 1493 app.trackcbUser = (void *) pAudioPlayer; 1494 1495 pAudioPlayer->mAPlayer = new android::LocAVPlayer(&app, false /*hasVideo*/); 1496 pAudioPlayer->mAPlayer->init(sfplayer_handlePrefetchEvent, 1497 (void*)pAudioPlayer /*notifUSer*/); 1498 1499 switch (pAudioPlayer->mDataSource.mLocator.mLocatorType) { 1500 case SL_DATALOCATOR_URI: { 1501 // The legacy implementation ran Stagefright within the application process, and 1502 // so allowed local pathnames specified by URI that were openable by 1503 // the application but were not openable by mediaserver. 1504 // The current implementation runs Stagefright (mostly) within mediaserver, 1505 // which runs as a different UID and likely a different current working directory. 1506 // For backwards compatibility with any applications which may have relied on the 1507 // previous behavior, we convert an openable file URI into an FD. 1508 // Note that unlike SL_DATALOCATOR_ANDROIDFD, this FD is owned by us 1509 // and so we close it as soon as we've passed it (via Binder dup) to mediaserver. 1510 const char *uri = (const char *)pAudioPlayer->mDataSource.mLocator.mURI.URI; 1511 if (!isDistantProtocol(uri)) { 1512 // don't touch the original uri, we may need it later 1513 const char *pathname = uri; 1514 // skip over an optional leading file:// prefix 1515 if (!strncasecmp(pathname, "file://", 7)) { 1516 pathname += 7; 1517 } 1518 // attempt to open it as a file using the application's credentials 1519 int fd = ::open(pathname, O_RDONLY); 1520 if (fd >= 0) { 1521 // if open is successful, then check to see if it's a regular file 1522 struct stat statbuf; 1523 if (!::fstat(fd, &statbuf) && S_ISREG(statbuf.st_mode)) { 1524 // treat similarly to an FD data locator, but 1525 // let setDataSource take responsibility for closing fd 1526 pAudioPlayer->mAPlayer->setDataSource(fd, 0, statbuf.st_size, true); 1527 break; 1528 } 1529 // we were able to open it, but it's not a file, so let mediaserver try 1530 (void) ::close(fd); 1531 } 1532 } 1533 // if either the URI didn't look like a file, or open failed, or not a file 1534 pAudioPlayer->mAPlayer->setDataSource(uri); 1535 } break; 1536 case SL_DATALOCATOR_ANDROIDFD: { 1537 int64_t offset = (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.offset; 1538 pAudioPlayer->mAPlayer->setDataSource( 1539 (int)pAudioPlayer->mDataSource.mLocator.mFD.fd, 1540 offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ? 1541 (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset, 1542 (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.length); 1543 } 1544 break; 1545 default: 1546 SL_LOGE(ERROR_PLAYERREALIZE_UNKNOWN_DATASOURCE_LOCATOR); 1547 break; 1548 } 1549 1550 } 1551 break; 1552 //----------------------------------- 1553 // StreamPlayer 1554 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: { 1555 object_lock_exclusive(&pAudioPlayer->mObject); 1556 AudioPlayback_Parameters ap_params; 1557 ap_params.sessionId = pAudioPlayer->mSessionId; 1558 ap_params.streamType = pAudioPlayer->mStreamType; 1559 ap_params.trackcb = NULL; 1560 ap_params.trackcbUser = NULL; 1561 android::StreamPlayer* splr = new android::StreamPlayer(&ap_params, false /*hasVideo*/, 1562 &pAudioPlayer->mAndroidBufferQueue, pAudioPlayer->mCallbackProtector); 1563 pAudioPlayer->mAPlayer = splr; 1564 splr->init(sfplayer_handlePrefetchEvent, (void*)pAudioPlayer); 1565 object_unlock_exclusive(&pAudioPlayer->mObject); 1566 } 1567 break; 1568 //----------------------------------- 1569 // AudioToCbRenderer 1570 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: { 1571 AudioPlayback_Parameters app; 1572 app.sessionId = pAudioPlayer->mSessionId; 1573 app.streamType = pAudioPlayer->mStreamType; 1574 1575 android::AudioToCbRenderer* decoder = new android::AudioToCbRenderer(&app); 1576 pAudioPlayer->mAPlayer = decoder; 1577 // configures the callback for the sink buffer queue 1578 decoder->setDataPushListener(adecoder_writeToBufferQueue, (void*)pAudioPlayer); 1579 // configures the callback for the notifications coming from the SF code 1580 decoder->init(sfplayer_handlePrefetchEvent, (void*)pAudioPlayer); 1581 1582 switch (pAudioPlayer->mDataSource.mLocator.mLocatorType) { 1583 case SL_DATALOCATOR_URI: 1584 decoder->setDataSource( 1585 (const char*)pAudioPlayer->mDataSource.mLocator.mURI.URI); 1586 break; 1587 case SL_DATALOCATOR_ANDROIDFD: { 1588 int64_t offset = (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.offset; 1589 decoder->setDataSource( 1590 (int)pAudioPlayer->mDataSource.mLocator.mFD.fd, 1591 offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ? 1592 (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset, 1593 (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.length); 1594 } 1595 break; 1596 default: 1597 SL_LOGE(ERROR_PLAYERREALIZE_UNKNOWN_DATASOURCE_LOCATOR); 1598 break; 1599 } 1600 1601 } 1602 break; 1603 //----------------------------------- 1604 // AacBqToPcmCbRenderer 1605 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: { 1606 AudioPlayback_Parameters app; 1607 app.sessionId = pAudioPlayer->mSessionId; 1608 app.streamType = pAudioPlayer->mStreamType; 1609 app.trackcb = NULL; 1610 app.trackcbUser = NULL; 1611 android::AacBqToPcmCbRenderer* bqtobq = new android::AacBqToPcmCbRenderer(&app); 1612 // configures the callback for the sink buffer queue 1613 bqtobq->setDataPushListener(adecoder_writeToBufferQueue, (void*)pAudioPlayer); 1614 pAudioPlayer->mAPlayer = bqtobq; 1615 // configures the callback for the notifications coming from the SF code, 1616 // but also implicitly configures the AndroidBufferQueue from which ADTS data is read 1617 pAudioPlayer->mAPlayer->init(sfplayer_handlePrefetchEvent, (void*)pAudioPlayer); 1618 } 1619 break; 1620 //----------------------------------- 1621 default: 1622 SL_LOGE(ERROR_PLAYERREALIZE_UNEXPECTED_OBJECT_TYPE_D, pAudioPlayer->mAndroidObjType); 1623 result = SL_RESULT_INTERNAL_ERROR; 1624 break; 1625 } 1626 1627 1628 // proceed with effect initialization 1629 // initialize EQ 1630 // FIXME use a table of effect descriptors when adding support for more effects 1631 if (memcmp(SL_IID_EQUALIZER, &pAudioPlayer->mEqualizer.mEqDescriptor.type, 1632 sizeof(effect_uuid_t)) == 0) { 1633 SL_LOGV("Need to initialize EQ for AudioPlayer=%p", pAudioPlayer); 1634 android_eq_init(pAudioPlayer->mSessionId, &pAudioPlayer->mEqualizer); 1635 } 1636 // initialize BassBoost 1637 if (memcmp(SL_IID_BASSBOOST, &pAudioPlayer->mBassBoost.mBassBoostDescriptor.type, 1638 sizeof(effect_uuid_t)) == 0) { 1639 SL_LOGV("Need to initialize BassBoost for AudioPlayer=%p", pAudioPlayer); 1640 android_bb_init(pAudioPlayer->mSessionId, &pAudioPlayer->mBassBoost); 1641 } 1642 // initialize Virtualizer 1643 if (memcmp(SL_IID_VIRTUALIZER, &pAudioPlayer->mVirtualizer.mVirtualizerDescriptor.type, 1644 sizeof(effect_uuid_t)) == 0) { 1645 SL_LOGV("Need to initialize Virtualizer for AudioPlayer=%p", pAudioPlayer); 1646 android_virt_init(pAudioPlayer->mSessionId, &pAudioPlayer->mVirtualizer); 1647 } 1648 1649 // initialize EffectSend 1650 // FIXME initialize EffectSend 1651 1652 return result; 1653} 1654 1655 1656//----------------------------------------------------------------------------- 1657/** 1658 * Called with a lock on AudioPlayer, and blocks until safe to destroy 1659 */ 1660SLresult android_audioPlayer_preDestroy(CAudioPlayer *pAudioPlayer) { 1661 SL_LOGD("android_audioPlayer_preDestroy(%p)", pAudioPlayer); 1662 SLresult result = SL_RESULT_SUCCESS; 1663 1664 if (pAudioPlayer->mAPlayer != 0) { 1665 pAudioPlayer->mAPlayer->preDestroy(); 1666 } 1667 SL_LOGD("android_audioPlayer_preDestroy(%p) after mAPlayer->preDestroy()", pAudioPlayer); 1668 1669 object_unlock_exclusive(&pAudioPlayer->mObject); 1670 if (pAudioPlayer->mCallbackProtector != 0) { 1671 pAudioPlayer->mCallbackProtector->requestCbExitAndWait(); 1672 } 1673 object_lock_exclusive(&pAudioPlayer->mObject); 1674 1675 return result; 1676} 1677 1678 1679//----------------------------------------------------------------------------- 1680SLresult android_audioPlayer_destroy(CAudioPlayer *pAudioPlayer) { 1681 SLresult result = SL_RESULT_SUCCESS; 1682 SL_LOGV("android_audioPlayer_destroy(%p)", pAudioPlayer); 1683 switch (pAudioPlayer->mAndroidObjType) { 1684 1685 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 1686 // We own the audio track for PCM buffer queue players 1687 if (pAudioPlayer->mAudioTrack != 0) { 1688 pAudioPlayer->mAudioTrack->stop(); 1689 // Note that there may still be another reference in post-unlock phase of SetPlayState 1690 pAudioPlayer->mAudioTrack.clear(); 1691 } 1692 break; 1693 1694 case AUDIOPLAYER_FROM_URIFD: // intended fall-through 1695 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 1696 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: // intended fall-through 1697 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: 1698 pAudioPlayer->mAPlayer.clear(); 1699 break; 1700 //----------------------------------- 1701 default: 1702 SL_LOGE(ERROR_PLAYERDESTROY_UNEXPECTED_OBJECT_TYPE_D, pAudioPlayer->mAndroidObjType); 1703 result = SL_RESULT_INTERNAL_ERROR; 1704 break; 1705 } 1706 1707 pAudioPlayer->mCallbackProtector.clear(); 1708 1709 // FIXME might not be needed 1710 pAudioPlayer->mAndroidObjType = INVALID_TYPE; 1711 1712 // explicit destructor 1713 pAudioPlayer->mAudioTrack.~sp(); 1714 // note that SetPlayState(PLAYING) may still hold a reference 1715 pAudioPlayer->mCallbackProtector.~sp(); 1716 pAudioPlayer->mAuxEffect.~sp(); 1717 pAudioPlayer->mAPlayer.~sp(); 1718 1719 if (pAudioPlayer->mpLock != NULL) { 1720 delete pAudioPlayer->mpLock; 1721 pAudioPlayer->mpLock = NULL; 1722 } 1723 1724 return result; 1725} 1726 1727 1728//----------------------------------------------------------------------------- 1729SLresult android_audioPlayer_setPlaybackRateAndConstraints(CAudioPlayer *ap, SLpermille rate, 1730 SLuint32 constraints) { 1731 SLresult result = SL_RESULT_SUCCESS; 1732 switch(ap->mAndroidObjType) { 1733 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: { 1734 // these asserts were already checked by the platform-independent layer 1735 assert((AUDIOTRACK_MIN_PLAYBACKRATE_PERMILLE <= rate) && 1736 (rate <= AUDIOTRACK_MAX_PLAYBACKRATE_PERMILLE)); 1737 assert(constraints & SL_RATEPROP_NOPITCHCORAUDIO); 1738 // get the content sample rate 1739 uint32_t contentRate = sles_to_android_sampleRate(ap->mSampleRateMilliHz); 1740 // apply the SL ES playback rate on the AudioTrack as a factor of its content sample rate 1741 if (ap->mAudioTrack != 0) { 1742 ap->mAudioTrack->setSampleRate(contentRate * (rate/1000.0f)); 1743 } 1744 } 1745 break; 1746 case AUDIOPLAYER_FROM_URIFD: 1747 assert(rate == 1000); 1748 assert(constraints & SL_RATEPROP_NOPITCHCORAUDIO); 1749 // that was easy 1750 break; 1751 1752 default: 1753 SL_LOGE("Unexpected object type %d", ap->mAndroidObjType); 1754 result = SL_RESULT_FEATURE_UNSUPPORTED; 1755 break; 1756 } 1757 return result; 1758} 1759 1760 1761//----------------------------------------------------------------------------- 1762// precondition 1763// called with no lock held 1764// ap != NULL 1765// pItemCount != NULL 1766SLresult android_audioPlayer_metadata_getItemCount(CAudioPlayer *ap, SLuint32 *pItemCount) { 1767 if (ap->mAPlayer == 0) { 1768 return SL_RESULT_PARAMETER_INVALID; 1769 } 1770 switch(ap->mAndroidObjType) { 1771 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 1772 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: 1773 { 1774 android::AudioSfDecoder* decoder = 1775 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get()); 1776 *pItemCount = decoder->getPcmFormatKeyCount(); 1777 } 1778 break; 1779 default: 1780 *pItemCount = 0; 1781 break; 1782 } 1783 return SL_RESULT_SUCCESS; 1784} 1785 1786 1787//----------------------------------------------------------------------------- 1788// precondition 1789// called with no lock held 1790// ap != NULL 1791// pKeySize != NULL 1792SLresult android_audioPlayer_metadata_getKeySize(CAudioPlayer *ap, 1793 SLuint32 index, SLuint32 *pKeySize) { 1794 if (ap->mAPlayer == 0) { 1795 return SL_RESULT_PARAMETER_INVALID; 1796 } 1797 SLresult res = SL_RESULT_SUCCESS; 1798 switch(ap->mAndroidObjType) { 1799 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 1800 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: 1801 { 1802 android::AudioSfDecoder* decoder = 1803 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get()); 1804 SLuint32 keyNameSize = 0; 1805 if (!decoder->getPcmFormatKeySize(index, &keyNameSize)) { 1806 res = SL_RESULT_PARAMETER_INVALID; 1807 } else { 1808 // *pKeySize is the size of the region used to store the key name AND 1809 // the information about the key (size, lang, encoding) 1810 *pKeySize = keyNameSize + sizeof(SLMetadataInfo); 1811 } 1812 } 1813 break; 1814 default: 1815 *pKeySize = 0; 1816 res = SL_RESULT_PARAMETER_INVALID; 1817 break; 1818 } 1819 return res; 1820} 1821 1822 1823//----------------------------------------------------------------------------- 1824// precondition 1825// called with no lock held 1826// ap != NULL 1827// pKey != NULL 1828SLresult android_audioPlayer_metadata_getKey(CAudioPlayer *ap, 1829 SLuint32 index, SLuint32 size, SLMetadataInfo *pKey) { 1830 if (ap->mAPlayer == 0) { 1831 return SL_RESULT_PARAMETER_INVALID; 1832 } 1833 SLresult res = SL_RESULT_SUCCESS; 1834 switch(ap->mAndroidObjType) { 1835 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 1836 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: 1837 { 1838 android::AudioSfDecoder* decoder = 1839 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get()); 1840 if ((size < sizeof(SLMetadataInfo) || 1841 (!decoder->getPcmFormatKeyName(index, size - sizeof(SLMetadataInfo), 1842 (char*)pKey->data)))) { 1843 res = SL_RESULT_PARAMETER_INVALID; 1844 } else { 1845 // successfully retrieved the key value, update the other fields 1846 pKey->encoding = SL_CHARACTERENCODING_UTF8; 1847 memcpy((char *) pKey->langCountry, "en", 3); 1848 pKey->size = strlen((char*)pKey->data) + 1; 1849 } 1850 } 1851 break; 1852 default: 1853 res = SL_RESULT_PARAMETER_INVALID; 1854 break; 1855 } 1856 return res; 1857} 1858 1859 1860//----------------------------------------------------------------------------- 1861// precondition 1862// called with no lock held 1863// ap != NULL 1864// pValueSize != NULL 1865SLresult android_audioPlayer_metadata_getValueSize(CAudioPlayer *ap, 1866 SLuint32 index, SLuint32 *pValueSize) { 1867 if (ap->mAPlayer == 0) { 1868 return SL_RESULT_PARAMETER_INVALID; 1869 } 1870 SLresult res = SL_RESULT_SUCCESS; 1871 switch(ap->mAndroidObjType) { 1872 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 1873 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: 1874 { 1875 android::AudioSfDecoder* decoder = 1876 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get()); 1877 SLuint32 valueSize = 0; 1878 if (!decoder->getPcmFormatValueSize(index, &valueSize)) { 1879 res = SL_RESULT_PARAMETER_INVALID; 1880 } else { 1881 // *pValueSize is the size of the region used to store the key value AND 1882 // the information about the value (size, lang, encoding) 1883 *pValueSize = valueSize + sizeof(SLMetadataInfo); 1884 } 1885 } 1886 break; 1887 default: 1888 *pValueSize = 0; 1889 res = SL_RESULT_PARAMETER_INVALID; 1890 break; 1891 } 1892 return res; 1893} 1894 1895 1896//----------------------------------------------------------------------------- 1897// precondition 1898// called with no lock held 1899// ap != NULL 1900// pValue != NULL 1901SLresult android_audioPlayer_metadata_getValue(CAudioPlayer *ap, 1902 SLuint32 index, SLuint32 size, SLMetadataInfo *pValue) { 1903 if (ap->mAPlayer == 0) { 1904 return SL_RESULT_PARAMETER_INVALID; 1905 } 1906 SLresult res = SL_RESULT_SUCCESS; 1907 switch(ap->mAndroidObjType) { 1908 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 1909 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: 1910 { 1911 android::AudioSfDecoder* decoder = 1912 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get()); 1913 pValue->encoding = SL_CHARACTERENCODING_BINARY; 1914 memcpy((char *) pValue->langCountry, "en", 3); // applicable here? 1915 SLuint32 valueSize = 0; 1916 if ((size < sizeof(SLMetadataInfo) 1917 || (!decoder->getPcmFormatValueSize(index, &valueSize)) 1918 || (!decoder->getPcmFormatKeyValue(index, size - sizeof(SLMetadataInfo), 1919 (SLuint32*)pValue->data)))) { 1920 res = SL_RESULT_PARAMETER_INVALID; 1921 } else { 1922 pValue->size = valueSize; 1923 } 1924 } 1925 break; 1926 default: 1927 res = SL_RESULT_PARAMETER_INVALID; 1928 break; 1929 } 1930 return res; 1931} 1932 1933//----------------------------------------------------------------------------- 1934// preconditions 1935// ap != NULL 1936// mutex is locked 1937// play state has changed 1938void android_audioPlayer_setPlayState(CAudioPlayer *ap) { 1939 1940 SLuint32 playState = ap->mPlay.mState; 1941 AndroidObjectState objState = ap->mAndroidObjState; 1942 1943 switch(ap->mAndroidObjType) { 1944 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 1945 switch (playState) { 1946 case SL_PLAYSTATE_STOPPED: 1947 SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_STOPPED"); 1948 if (ap->mAudioTrack != 0) { 1949 ap->mAudioTrack->stop(); 1950 } 1951 break; 1952 case SL_PLAYSTATE_PAUSED: 1953 SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PAUSED"); 1954 if (ap->mAudioTrack != 0) { 1955 ap->mAudioTrack->pause(); 1956 } 1957 break; 1958 case SL_PLAYSTATE_PLAYING: 1959 SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PLAYING"); 1960 if (ap->mAudioTrack != 0) { 1961 // instead of ap->mAudioTrack->start(); 1962 ap->mDeferredStart = true; 1963 } 1964 break; 1965 default: 1966 // checked by caller, should not happen 1967 break; 1968 } 1969 break; 1970 1971 case AUDIOPLAYER_FROM_URIFD: // intended fall-through 1972 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 1973 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: // intended fall-through 1974 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: 1975 // FIXME report and use the return code to the lock mechanism, which is where play state 1976 // changes are updated (see object_unlock_exclusive_attributes()) 1977 aplayer_setPlayState(ap->mAPlayer, playState, &(ap->mAndroidObjState)); 1978 break; 1979 default: 1980 SL_LOGE(ERROR_PLAYERSETPLAYSTATE_UNEXPECTED_OBJECT_TYPE_D, ap->mAndroidObjType); 1981 break; 1982 } 1983} 1984 1985 1986//----------------------------------------------------------------------------- 1987// call when either player event flags, marker position, or position update period changes 1988void android_audioPlayer_usePlayEventMask(CAudioPlayer *ap) { 1989 IPlay *pPlayItf = &ap->mPlay; 1990 SLuint32 eventFlags = pPlayItf->mEventFlags; 1991 /*switch(ap->mAndroidObjType) { 1992 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:*/ 1993 1994 if (ap->mAPlayer != 0) { 1995 assert(ap->mAudioTrack == 0); 1996 ap->mAPlayer->setPlayEvents((int32_t) eventFlags, (int32_t) pPlayItf->mMarkerPosition, 1997 (int32_t) pPlayItf->mPositionUpdatePeriod); 1998 return; 1999 } 2000 2001 if (ap->mAudioTrack == 0) { 2002 return; 2003 } 2004 2005 if (eventFlags & SL_PLAYEVENT_HEADATMARKER) { 2006 ap->mAudioTrack->setMarkerPosition((uint32_t)((((int64_t)pPlayItf->mMarkerPosition 2007 * sles_to_android_sampleRate(ap->mSampleRateMilliHz)))/1000)); 2008 } else { 2009 // clear marker 2010 ap->mAudioTrack->setMarkerPosition(0); 2011 } 2012 2013 if (eventFlags & SL_PLAYEVENT_HEADATNEWPOS) { 2014 ap->mAudioTrack->setPositionUpdatePeriod( 2015 (uint32_t)((((int64_t)pPlayItf->mPositionUpdatePeriod 2016 * sles_to_android_sampleRate(ap->mSampleRateMilliHz)))/1000)); 2017 } else { 2018 // clear periodic update 2019 ap->mAudioTrack->setPositionUpdatePeriod(0); 2020 } 2021 2022 if (eventFlags & SL_PLAYEVENT_HEADATEND) { 2023 // nothing to do for SL_PLAYEVENT_HEADATEND, callback event will be checked against mask 2024 } 2025 2026 if (eventFlags & SL_PLAYEVENT_HEADMOVING) { 2027 // FIXME support SL_PLAYEVENT_HEADMOVING 2028 SL_LOGD("[ FIXME: IPlay_SetCallbackEventsMask(SL_PLAYEVENT_HEADMOVING) on an " 2029 "SL_OBJECTID_AUDIOPLAYER to be implemented ]"); 2030 } 2031 if (eventFlags & SL_PLAYEVENT_HEADSTALLED) { 2032 // nothing to do for SL_PLAYEVENT_HEADSTALLED, callback event will be checked against mask 2033 } 2034 2035} 2036 2037 2038//----------------------------------------------------------------------------- 2039SLresult android_audioPlayer_getDuration(IPlay *pPlayItf, SLmillisecond *pDurMsec) { 2040 CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis; 2041 switch(ap->mAndroidObjType) { 2042 2043 case AUDIOPLAYER_FROM_URIFD: // intended fall-through 2044 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: { 2045 int32_t durationMsec = ANDROID_UNKNOWN_TIME; 2046 if (ap->mAPlayer != 0) { 2047 ap->mAPlayer->getDurationMsec(&durationMsec); 2048 } 2049 *pDurMsec = durationMsec == ANDROID_UNKNOWN_TIME ? SL_TIME_UNKNOWN : durationMsec; 2050 break; 2051 } 2052 2053 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 2054 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 2055 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: 2056 default: { 2057 *pDurMsec = SL_TIME_UNKNOWN; 2058 } 2059 } 2060 return SL_RESULT_SUCCESS; 2061} 2062 2063 2064//----------------------------------------------------------------------------- 2065void android_audioPlayer_getPosition(IPlay *pPlayItf, SLmillisecond *pPosMsec) { 2066 CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis; 2067 switch(ap->mAndroidObjType) { 2068 2069 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 2070 if ((ap->mSampleRateMilliHz == UNKNOWN_SAMPLERATE) || (ap->mAudioTrack == 0)) { 2071 *pPosMsec = 0; 2072 } else { 2073 uint32_t positionInFrames; 2074 ap->mAudioTrack->getPosition(&positionInFrames); 2075 *pPosMsec = ((int64_t)positionInFrames * 1000) / 2076 sles_to_android_sampleRate(ap->mSampleRateMilliHz); 2077 } 2078 break; 2079 2080 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 2081 case AUDIOPLAYER_FROM_URIFD: 2082 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 2083 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: { 2084 int32_t posMsec = ANDROID_UNKNOWN_TIME; 2085 if (ap->mAPlayer != 0) { 2086 ap->mAPlayer->getPositionMsec(&posMsec); 2087 } 2088 *pPosMsec = posMsec == ANDROID_UNKNOWN_TIME ? 0 : posMsec; 2089 break; 2090 } 2091 2092 default: 2093 *pPosMsec = 0; 2094 } 2095} 2096 2097 2098//----------------------------------------------------------------------------- 2099SLresult android_audioPlayer_seek(CAudioPlayer *ap, SLmillisecond posMsec) { 2100 SLresult result = SL_RESULT_SUCCESS; 2101 2102 switch(ap->mAndroidObjType) { 2103 2104 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: // intended fall-through 2105 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: 2106 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: 2107 result = SL_RESULT_FEATURE_UNSUPPORTED; 2108 break; 2109 2110 case AUDIOPLAYER_FROM_URIFD: // intended fall-through 2111 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 2112 if (ap->mAPlayer != 0) { 2113 ap->mAPlayer->seek(posMsec); 2114 } 2115 break; 2116 2117 default: 2118 break; 2119 } 2120 return result; 2121} 2122 2123 2124//----------------------------------------------------------------------------- 2125SLresult android_audioPlayer_loop(CAudioPlayer *ap, SLboolean loopEnable) { 2126 SLresult result = SL_RESULT_SUCCESS; 2127 2128 switch (ap->mAndroidObjType) { 2129 case AUDIOPLAYER_FROM_URIFD: 2130 // case AUDIOPLAY_FROM_URIFD_TO_PCM_BUFFERQUEUE: 2131 // would actually work, but what's the point? 2132 if (ap->mAPlayer != 0) { 2133 ap->mAPlayer->loop((bool)loopEnable); 2134 } 2135 break; 2136 default: 2137 result = SL_RESULT_FEATURE_UNSUPPORTED; 2138 break; 2139 } 2140 return result; 2141} 2142 2143 2144//----------------------------------------------------------------------------- 2145SLresult android_audioPlayer_setBufferingUpdateThresholdPerMille(CAudioPlayer *ap, 2146 SLpermille threshold) { 2147 SLresult result = SL_RESULT_SUCCESS; 2148 2149 switch (ap->mAndroidObjType) { 2150 case AUDIOPLAYER_FROM_URIFD: 2151 if (ap->mAPlayer != 0) { 2152 ap->mAPlayer->setBufferingUpdateThreshold(threshold / 10); 2153 } 2154 break; 2155 2156 default: {} 2157 } 2158 2159 return result; 2160} 2161 2162 2163//----------------------------------------------------------------------------- 2164void android_audioPlayer_bufferQueue_onRefilled_l(CAudioPlayer *ap) { 2165 // the AudioTrack associated with the AudioPlayer receiving audio from a PCM buffer 2166 // queue was stopped when the queue become empty, we restart as soon as a new buffer 2167 // has been enqueued since we're in playing state 2168 if (ap->mAudioTrack != 0) { 2169 // instead of ap->mAudioTrack->start(); 2170 ap->mDeferredStart = true; 2171 } 2172 2173 // when the queue became empty, an underflow on the prefetch status itf was sent. Now the queue 2174 // has received new data, signal it has sufficient data 2175 if (IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) { 2176 // we wouldn't have been called unless we were previously in the underflow state 2177 assert(SL_PREFETCHSTATUS_UNDERFLOW == ap->mPrefetchStatus.mStatus); 2178 assert(0 == ap->mPrefetchStatus.mLevel); 2179 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_SUFFICIENTDATA; 2180 ap->mPrefetchStatus.mLevel = 1000; 2181 // callback or no callback? 2182 SLuint32 prefetchEvents = ap->mPrefetchStatus.mCallbackEventsMask & 2183 (SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE); 2184 if (SL_PREFETCHEVENT_NONE != prefetchEvents) { 2185 ap->mPrefetchStatus.mDeferredPrefetchCallback = ap->mPrefetchStatus.mCallback; 2186 ap->mPrefetchStatus.mDeferredPrefetchContext = ap->mPrefetchStatus.mContext; 2187 ap->mPrefetchStatus.mDeferredPrefetchEvents = prefetchEvents; 2188 } 2189 } 2190} 2191 2192 2193//----------------------------------------------------------------------------- 2194/* 2195 * BufferQueue::Clear 2196 */ 2197SLresult android_audioPlayer_bufferQueue_onClear(CAudioPlayer *ap) { 2198 SLresult result = SL_RESULT_SUCCESS; 2199 2200 switch (ap->mAndroidObjType) { 2201 //----------------------------------- 2202 // AudioTrack 2203 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 2204 if (ap->mAudioTrack != 0) { 2205 ap->mAudioTrack->flush(); 2206 } 2207 break; 2208 default: 2209 result = SL_RESULT_INTERNAL_ERROR; 2210 break; 2211 } 2212 2213 return result; 2214} 2215 2216 2217//----------------------------------------------------------------------------- 2218SLresult android_audioPlayer_androidBufferQueue_registerCallback_l(CAudioPlayer *ap) { 2219 SLresult result = SL_RESULT_SUCCESS; 2220 assert(ap->mAPlayer != 0); 2221 switch (ap->mAndroidObjType) { 2222 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: { 2223 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(ap->mAPlayer.get()); 2224 splr->registerQueueCallback(&ap->mAndroidBufferQueue); 2225 } break; 2226 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: { 2227 android::AacBqToPcmCbRenderer* dec = 2228 static_cast<android::AacBqToPcmCbRenderer*>(ap->mAPlayer.get()); 2229 dec->registerSourceQueueCallback((const void*)ap /*user*/, 2230 ap->mAndroidBufferQueue.mContext /*context*/, 2231 (const void*)&(ap->mAndroidBufferQueue.mItf) /*caller*/); 2232 } break; 2233 default: 2234 SL_LOGE("Error registering AndroidBufferQueue callback: unexpected object type %d", 2235 ap->mAndroidObjType); 2236 result = SL_RESULT_INTERNAL_ERROR; 2237 break; 2238 } 2239 return result; 2240} 2241 2242//----------------------------------------------------------------------------- 2243void android_audioPlayer_androidBufferQueue_clear_l(CAudioPlayer *ap) { 2244 switch (ap->mAndroidObjType) { 2245 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: 2246 if (ap->mAPlayer != 0) { 2247 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(ap->mAPlayer.get()); 2248 splr->appClear_l(); 2249 } break; 2250 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: 2251 // nothing to do here, fall through 2252 default: 2253 break; 2254 } 2255} 2256 2257void android_audioPlayer_androidBufferQueue_onRefilled_l(CAudioPlayer *ap) { 2258 switch (ap->mAndroidObjType) { 2259 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: 2260 if (ap->mAPlayer != 0) { 2261 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(ap->mAPlayer.get()); 2262 splr->queueRefilled(); 2263 } break; 2264 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: 2265 // FIXME this may require waking up the decoder if it is currently starved and isn't polling 2266 default: 2267 break; 2268 } 2269} 2270