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