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