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