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