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