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