AudioPlayer_to_android.cpp revision 377aa54ce344adcbc8bac731c6db9e7e39b432c5
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 //SL_LOGV("received event %d, data %d from SfAudioPlayer", event, data1); 652 switch(event) { 653 654 case android::GenericPlayer::kEventPrepared: { 655 656 if (PLAYER_SUCCESS != data1) { 657 object_lock_exclusive(&ap->mObject); 658 659 // already initialized at object creation, and can only prepare once so never reset 660 assert(ap->mAudioTrack == 0); 661 assert(ap->mNumChannels == UNKNOWN_NUMCHANNELS); 662 assert(ap->mSampleRateMilliHz == UNKNOWN_SAMPLERATE); 663 assert(ap->mAndroidObjState == ANDROID_UNINITIALIZED); 664 665 object_unlock_exclusive(&ap->mObject); 666 667 // SfPlayer prepare() failed prefetching, there is no event in SLPrefetchStatus to 668 // indicate a prefetch error, so we signal it by sending simulataneously two events: 669 // - SL_PREFETCHEVENT_FILLLEVELCHANGE with a level of 0 670 // - SL_PREFETCHEVENT_STATUSCHANGE with a status of SL_PREFETCHSTATUS_UNDERFLOW 671 SL_LOGE(ERROR_PLAYER_PREFETCH_d, data1); 672 if (!IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) { 673 break; 674 } 675 676 slPrefetchCallback callback = NULL; 677 void* callbackPContext = NULL; 678 679 interface_lock_exclusive(&ap->mPrefetchStatus); 680 ap->mPrefetchStatus.mLevel = 0; 681 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW; 682 if ((ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_FILLLEVELCHANGE) 683 && (ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_STATUSCHANGE)) { 684 callback = ap->mPrefetchStatus.mCallback; 685 callbackPContext = ap->mPrefetchStatus.mContext; 686 } 687 interface_unlock_exclusive(&ap->mPrefetchStatus); 688 689 // callback with no lock held 690 if (NULL != callback) { 691 (*callback)(&ap->mPrefetchStatus.mItf, callbackPContext, 692 SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE); 693 } 694 695 696 } else { 697 698 object_lock_exclusive(&ap->mObject); 699 700 if (AUDIOPLAYER_FROM_URIFD == ap->mAndroidObjType) { 701 //************************************** 702 // FIXME move under GenericMediaPlayer 703#if 0 704 ap->mAudioTrack = ap->mSfPlayer->getAudioTrack(); 705 ap->mNumChannels = ap->mSfPlayer->getNumChannels(); 706 ap->mSampleRateMilliHz = 707 android_to_sles_sampleRate(ap->mSfPlayer->getSampleRateHz()); 708 ap->mSfPlayer->startPrefetch_async(); 709 // update the new track with the current settings 710 audioPlayer_auxEffectUpdate(ap); 711 android_audioPlayer_useEventMask(ap); 712 android_audioPlayer_volumeUpdate(ap); 713 android_audioPlayer_setPlayRate(ap, ap->mPlaybackRate.mRate, false /*lockAP*/); 714#endif 715 } else if (AUDIOPLAYER_FROM_PCM_BUFFERQUEUE == ap->mAndroidObjType) { 716 if (ap->mAPlayer != 0) { 717 ((android::AudioToCbRenderer*)ap->mAPlayer.get())->startPrefetch_async(); 718 } 719 } else if (AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE == ap->mAndroidObjType) { 720 SL_LOGD("Received SfPlayer::kEventPrepared from AVPlayer for CAudioPlayer %p", ap); 721 } 722 723 ap->mAndroidObjState = ANDROID_READY; 724 725 object_unlock_exclusive(&ap->mObject); 726 } 727 728 } 729 break; 730 731 case android::GenericPlayer::kEventPrefetchFillLevelUpdate : { 732 if (!IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) { 733 break; 734 } 735 slPrefetchCallback callback = NULL; 736 void* callbackPContext = NULL; 737 738 // SLPrefetchStatusItf callback or no callback? 739 interface_lock_exclusive(&ap->mPrefetchStatus); 740 if (ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_FILLLEVELCHANGE) { 741 callback = ap->mPrefetchStatus.mCallback; 742 callbackPContext = ap->mPrefetchStatus.mContext; 743 } 744 ap->mPrefetchStatus.mLevel = (SLpermille)data1; 745 interface_unlock_exclusive(&ap->mPrefetchStatus); 746 747 // callback with no lock held 748 if (NULL != callback) { 749 (*callback)(&ap->mPrefetchStatus.mItf, callbackPContext, 750 SL_PREFETCHEVENT_FILLLEVELCHANGE); 751 } 752 } 753 break; 754 755 case android::GenericPlayer::kEventPrefetchStatusChange: { 756 if (!IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) { 757 break; 758 } 759 slPrefetchCallback callback = NULL; 760 void* callbackPContext = NULL; 761 762 // SLPrefetchStatusItf callback or no callback? 763 object_lock_exclusive(&ap->mObject); 764 if (ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_STATUSCHANGE) { 765 callback = ap->mPrefetchStatus.mCallback; 766 callbackPContext = ap->mPrefetchStatus.mContext; 767 } 768 if (data1 >= android::kStatusIntermediate) { 769 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_SUFFICIENTDATA; 770 ap->mAndroidObjState = ANDROID_READY; 771 } else if (data1 < android::kStatusIntermediate) { 772 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW; 773 } 774 object_unlock_exclusive(&ap->mObject); 775 776 // callback with no lock held 777 if (NULL != callback) { 778 (*callback)(&ap->mPrefetchStatus.mItf, callbackPContext, SL_PREFETCHEVENT_STATUSCHANGE); 779 } 780 } 781 break; 782 783 case android::GenericPlayer::kEventEndOfStream: { 784 audioPlayer_dispatch_headAtEnd_lockPlay(ap, true /*set state to paused?*/, true); 785 if ((ap->mAudioTrack != 0) && (!ap->mSeek.mLoopEnabled)) { 786 ap->mAudioTrack->stop(); 787 } 788 } 789 break; 790 791 default: 792 break; 793 } 794 795 ap->mCallbackProtector->exitCb(); 796} 797 798 799//----------------------------------------------------------------------------- 800SLresult android_audioPlayer_checkSourceSink(CAudioPlayer *pAudioPlayer) 801{ 802 // verify that the locator types for the source / sink combination is supported 803 pAudioPlayer->mAndroidObjType = audioPlayer_getAndroidObjectTypeForSourceSink(pAudioPlayer); 804 if (INVALID_TYPE == pAudioPlayer->mAndroidObjType) { 805 return SL_RESULT_PARAMETER_INVALID; 806 } 807 808 const SLDataSource *pAudioSrc = &pAudioPlayer->mDataSource.u.mSource; 809 const SLDataSink *pAudioSnk = &pAudioPlayer->mDataSink.u.mSink; 810 811 // format check: 812 const SLuint32 sourceLocatorType = *(SLuint32 *)pAudioSrc->pLocator; 813 const SLuint32 sinkLocatorType = *(SLuint32 *)pAudioSnk->pLocator; 814 const SLuint32 sourceFormatType = *(SLuint32 *)pAudioSrc->pFormat; 815 const SLuint32 sinkFormatType = *(SLuint32 *)pAudioSnk->pFormat; 816 817 switch (sourceLocatorType) { 818 //------------------ 819 // Buffer Queues 820 case SL_DATALOCATOR_BUFFERQUEUE: 821 case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE: 822 { 823 SLDataLocator_BufferQueue *dl_bq = (SLDataLocator_BufferQueue *) pAudioSrc->pLocator; 824 825 // Buffer format 826 switch (sourceFormatType) { 827 // currently only PCM buffer queues are supported, 828 case SL_DATAFORMAT_PCM: { 829 SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *) pAudioSrc->pFormat; 830 switch (df_pcm->numChannels) { 831 case 1: 832 case 2: 833 break; 834 default: 835 // this should have already been rejected by checkDataFormat 836 SL_LOGE("Cannot create audio player: unsupported " \ 837 "PCM data source with %u channels", (unsigned) df_pcm->numChannels); 838 return SL_RESULT_CONTENT_UNSUPPORTED; 839 } 840 switch (df_pcm->samplesPerSec) { 841 case SL_SAMPLINGRATE_8: 842 case SL_SAMPLINGRATE_11_025: 843 case SL_SAMPLINGRATE_12: 844 case SL_SAMPLINGRATE_16: 845 case SL_SAMPLINGRATE_22_05: 846 case SL_SAMPLINGRATE_24: 847 case SL_SAMPLINGRATE_32: 848 case SL_SAMPLINGRATE_44_1: 849 case SL_SAMPLINGRATE_48: 850 break; 851 case SL_SAMPLINGRATE_64: 852 case SL_SAMPLINGRATE_88_2: 853 case SL_SAMPLINGRATE_96: 854 case SL_SAMPLINGRATE_192: 855 default: 856 SL_LOGE("Cannot create audio player: unsupported sample rate %u milliHz", 857 (unsigned) df_pcm->samplesPerSec); 858 return SL_RESULT_CONTENT_UNSUPPORTED; 859 } 860 switch (df_pcm->bitsPerSample) { 861 case SL_PCMSAMPLEFORMAT_FIXED_8: 862 // FIXME We should support this 863 //SL_LOGE("Cannot create audio player: unsupported 8-bit data"); 864 //return SL_RESULT_CONTENT_UNSUPPORTED; 865 case SL_PCMSAMPLEFORMAT_FIXED_16: 866 break; 867 // others 868 default: 869 // this should have already been rejected by checkDataFormat 870 SL_LOGE("Cannot create audio player: unsupported sample bit depth %u", 871 (SLuint32)df_pcm->bitsPerSample); 872 return SL_RESULT_CONTENT_UNSUPPORTED; 873 } 874 switch (df_pcm->containerSize) { 875 case 8: 876 case 16: 877 break; 878 // others 879 default: 880 SL_LOGE("Cannot create audio player: unsupported container size %u", 881 (unsigned) df_pcm->containerSize); 882 return SL_RESULT_CONTENT_UNSUPPORTED; 883 } 884 switch (df_pcm->channelMask) { 885 // FIXME needs work 886 default: 887 break; 888 } 889 switch (df_pcm->endianness) { 890 case SL_BYTEORDER_LITTLEENDIAN: 891 break; 892 case SL_BYTEORDER_BIGENDIAN: 893 SL_LOGE("Cannot create audio player: unsupported big-endian byte order"); 894 return SL_RESULT_CONTENT_UNSUPPORTED; 895 // native is proposed but not yet in spec 896 default: 897 SL_LOGE("Cannot create audio player: unsupported byte order %u", 898 (unsigned) df_pcm->endianness); 899 return SL_RESULT_CONTENT_UNSUPPORTED; 900 } 901 } //case SL_DATAFORMAT_PCM 902 break; 903 case SL_DATAFORMAT_MIME: 904 case XA_DATAFORMAT_RAWIMAGE: 905 SL_LOGE("Cannot create audio player with buffer queue data source " 906 "without SL_DATAFORMAT_PCM format"); 907 return SL_RESULT_CONTENT_UNSUPPORTED; 908 default: 909 // invalid data format is detected earlier 910 assert(false); 911 return SL_RESULT_INTERNAL_ERROR; 912 } // switch (sourceFormatType) 913 } // case SL_DATALOCATOR_BUFFERQUEUE or SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE 914 break; 915 //------------------ 916 // URI 917 case SL_DATALOCATOR_URI: 918 { 919 SLDataLocator_URI *dl_uri = (SLDataLocator_URI *) pAudioSrc->pLocator; 920 if (NULL == dl_uri->URI) { 921 return SL_RESULT_PARAMETER_INVALID; 922 } 923 // URI format 924 switch (sourceFormatType) { 925 case SL_DATAFORMAT_MIME: 926 break; 927 case SL_DATAFORMAT_PCM: 928 case XA_DATAFORMAT_RAWIMAGE: 929 SL_LOGE("Cannot create audio player with SL_DATALOCATOR_URI data source without " 930 "SL_DATAFORMAT_MIME format"); 931 return SL_RESULT_CONTENT_UNSUPPORTED; 932 } // switch (sourceFormatType) 933 // decoding format check 934 if ((sinkLocatorType != SL_DATALOCATOR_OUTPUTMIX) && 935 !audioPlayer_isSupportedNonOutputMixSink(pAudioSnk)) { 936 return SL_RESULT_CONTENT_UNSUPPORTED; 937 } 938 } // case SL_DATALOCATOR_URI 939 break; 940 //------------------ 941 // File Descriptor 942 case SL_DATALOCATOR_ANDROIDFD: 943 { 944 // fd is already non null 945 switch (sourceFormatType) { 946 case SL_DATAFORMAT_MIME: 947 break; 948 case SL_DATAFORMAT_PCM: 949 // FIXME implement 950 SL_LOGD("[ FIXME implement PCM FD data sources ]"); 951 break; 952 case XA_DATAFORMAT_RAWIMAGE: 953 SL_LOGE("Cannot create audio player with SL_DATALOCATOR_ANDROIDFD data source " 954 "without SL_DATAFORMAT_MIME or SL_DATAFORMAT_PCM format"); 955 return SL_RESULT_CONTENT_UNSUPPORTED; 956 default: 957 // invalid data format is detected earlier 958 assert(false); 959 return SL_RESULT_INTERNAL_ERROR; 960 } // switch (sourceFormatType) 961 if ((sinkLocatorType != SL_DATALOCATOR_OUTPUTMIX) && 962 !audioPlayer_isSupportedNonOutputMixSink(pAudioSnk)) { 963 return SL_RESULT_CONTENT_UNSUPPORTED; 964 } 965 } // case SL_DATALOCATOR_ANDROIDFD 966 break; 967 //------------------ 968 // Stream 969 case SL_DATALOCATOR_ANDROIDBUFFERQUEUE: 970 { 971 switch (sourceFormatType) { 972 case SL_DATAFORMAT_MIME: 973 { 974 SLDataFormat_MIME *df_mime = (SLDataFormat_MIME *) pAudioSrc->pFormat; 975 if (SL_CONTAINERTYPE_MPEG_TS != df_mime->containerType) { 976 SL_LOGE("Cannot create player with SL_DATALOCATOR_ANDROIDBUFFERQUEUE data source " 977 "that is not fed MPEG-2 TS data"); 978 return SL_RESULT_CONTENT_UNSUPPORTED; 979 } 980 } 981 break; 982 default: 983 SL_LOGE("Cannot create player with SL_DATALOCATOR_ANDROIDBUFFERQUEUE data source " 984 "without SL_DATAFORMAT_MIME format"); 985 return SL_RESULT_CONTENT_UNSUPPORTED; 986 } 987 } 988 break; // case SL_DATALOCATOR_ANDROIDBUFFERQUEUE 989 //------------------ 990 // Address 991 case SL_DATALOCATOR_ADDRESS: 992 case SL_DATALOCATOR_IODEVICE: 993 case SL_DATALOCATOR_OUTPUTMIX: 994 case XA_DATALOCATOR_NATIVEDISPLAY: 995 case SL_DATALOCATOR_MIDIBUFFERQUEUE: 996 SL_LOGE("Cannot create audio player with data locator type 0x%x", 997 (unsigned) sourceLocatorType); 998 return SL_RESULT_CONTENT_UNSUPPORTED; 999 default: 1000 SL_LOGE("Cannot create audio player with invalid data locator type 0x%x", 1001 (unsigned) sourceLocatorType); 1002 return SL_RESULT_PARAMETER_INVALID; 1003 }// switch (locatorType) 1004 1005 return SL_RESULT_SUCCESS; 1006} 1007 1008 1009 1010//----------------------------------------------------------------------------- 1011static void audioTrack_callBack_uri(int event, void* user, void *info) { 1012 // EVENT_MORE_DATA needs to be handled with priority over the other events 1013 // because it will be called the most often during playback 1014 1015 if (event == android::AudioTrack::EVENT_MORE_DATA) { 1016 //SL_LOGV("received event EVENT_MORE_DATA from AudioTrack"); 1017 // set size to 0 to signal we're not using the callback to write more data 1018 android::AudioTrack::Buffer* pBuff = (android::AudioTrack::Buffer*)info; 1019 pBuff->size = 0; 1020 } else if (NULL != user) { 1021 CAudioPlayer *ap = (CAudioPlayer *)user; 1022 if (!android::CallbackProtector::enterCbIfOk(ap->mCallbackProtector)) { 1023 // it is not safe to enter the callback (the track is about to go away) 1024 return; 1025 } 1026 switch (event) { 1027 case android::AudioTrack::EVENT_MARKER : 1028 audioTrack_handleMarker_lockPlay(ap); 1029 break; 1030 case android::AudioTrack::EVENT_NEW_POS : 1031 audioTrack_handleNewPos_lockPlay(ap); 1032 break; 1033 case android::AudioTrack::EVENT_UNDERRUN : 1034 audioTrack_handleUnderrun_lockPlay(ap); 1035 break; 1036 case android::AudioTrack::EVENT_BUFFER_END : 1037 case android::AudioTrack::EVENT_LOOP_END : 1038 break; 1039 default: 1040 SL_LOGE("Encountered unknown AudioTrack event %d for CAudioPlayer %p", event, 1041 ap); 1042 break; 1043 } 1044 ap->mCallbackProtector->exitCb(); 1045 } 1046} 1047 1048//----------------------------------------------------------------------------- 1049// Callback associated with an AudioTrack of an SL ES AudioPlayer that gets its data 1050// from a buffer queue. This will not be called once the AudioTrack has been destroyed. 1051static void audioTrack_callBack_pullFromBuffQueue(int event, void* user, void *info) { 1052 CAudioPlayer *ap = (CAudioPlayer *)user; 1053 1054 if (!android::CallbackProtector::enterCbIfOk(ap->mCallbackProtector)) { 1055 // it is not safe to enter the callback (the track is about to go away) 1056 return; 1057 } 1058 1059 void * callbackPContext = NULL; 1060 switch(event) { 1061 1062 case android::AudioTrack::EVENT_MORE_DATA: { 1063 //SL_LOGV("received event EVENT_MORE_DATA from AudioTrack TID=%d", gettid()); 1064 slBufferQueueCallback callback = NULL; 1065 android::AudioTrack::Buffer* pBuff = (android::AudioTrack::Buffer*)info; 1066 1067 // retrieve data from the buffer queue 1068 interface_lock_exclusive(&ap->mBufferQueue); 1069 1070 if (ap->mBufferQueue.mState.count != 0) { 1071 //SL_LOGV("nbBuffers in queue = %u",ap->mBufferQueue.mState.count); 1072 assert(ap->mBufferQueue.mFront != ap->mBufferQueue.mRear); 1073 1074 BufferHeader *oldFront = ap->mBufferQueue.mFront; 1075 BufferHeader *newFront = &oldFront[1]; 1076 1077 // FIXME handle 8bit based on buffer format 1078 short *pSrc = (short*)((char *)oldFront->mBuffer 1079 + ap->mBufferQueue.mSizeConsumed); 1080 if (ap->mBufferQueue.mSizeConsumed + pBuff->size < oldFront->mSize) { 1081 // can't consume the whole or rest of the buffer in one shot 1082 ap->mBufferQueue.mSizeConsumed += pBuff->size; 1083 // leave pBuff->size untouched 1084 // consume data 1085 // FIXME can we avoid holding the lock during the copy? 1086 memcpy (pBuff->i16, pSrc, pBuff->size); 1087 } else { 1088 // finish consuming the buffer or consume the buffer in one shot 1089 pBuff->size = oldFront->mSize - ap->mBufferQueue.mSizeConsumed; 1090 ap->mBufferQueue.mSizeConsumed = 0; 1091 1092 if (newFront == 1093 &ap->mBufferQueue.mArray 1094 [ap->mBufferQueue.mNumBuffers + 1]) 1095 { 1096 newFront = ap->mBufferQueue.mArray; 1097 } 1098 ap->mBufferQueue.mFront = newFront; 1099 1100 ap->mBufferQueue.mState.count--; 1101 ap->mBufferQueue.mState.playIndex++; 1102 1103 // consume data 1104 // FIXME can we avoid holding the lock during the copy? 1105 memcpy (pBuff->i16, pSrc, pBuff->size); 1106 1107 // data has been consumed, and the buffer queue state has been updated 1108 // we will notify the client if applicable 1109 callback = ap->mBufferQueue.mCallback; 1110 // save callback data 1111 callbackPContext = ap->mBufferQueue.mContext; 1112 } 1113 } else { // empty queue 1114 // signal no data available 1115 pBuff->size = 0; 1116 1117 // signal we're at the end of the content, but don't pause (see note in function) 1118 audioPlayer_dispatch_headAtEnd_lockPlay(ap, false /*set state to paused?*/, false); 1119 1120 // signal underflow to prefetch status itf 1121 if (IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) { 1122 audioPlayer_dispatch_prefetchStatus_lockPrefetch(ap, SL_PREFETCHSTATUS_UNDERFLOW, 1123 false); 1124 } 1125 1126 // stop the track so it restarts playing faster when new data is enqueued 1127 ap->mAudioTrack->stop(); 1128 } 1129 interface_unlock_exclusive(&ap->mBufferQueue); 1130 1131 // notify client 1132 if (NULL != callback) { 1133 (*callback)(&ap->mBufferQueue.mItf, callbackPContext); 1134 } 1135 } 1136 break; 1137 1138 case android::AudioTrack::EVENT_MARKER: 1139 //SL_LOGI("received event EVENT_MARKER from AudioTrack"); 1140 audioTrack_handleMarker_lockPlay(ap); 1141 break; 1142 1143 case android::AudioTrack::EVENT_NEW_POS: 1144 //SL_LOGI("received event EVENT_NEW_POS from AudioTrack"); 1145 audioTrack_handleNewPos_lockPlay(ap); 1146 break; 1147 1148 case android::AudioTrack::EVENT_UNDERRUN: 1149 //SL_LOGI("received event EVENT_UNDERRUN from AudioTrack"); 1150 audioTrack_handleUnderrun_lockPlay(ap); 1151 break; 1152 1153 default: 1154 // FIXME where does the notification of SL_PLAYEVENT_HEADMOVING fit? 1155 SL_LOGE("Encountered unknown AudioTrack event %d for CAudioPlayer %p", event, 1156 (CAudioPlayer *)user); 1157 break; 1158 } 1159 1160 ap->mCallbackProtector->exitCb(); 1161} 1162 1163 1164//----------------------------------------------------------------------------- 1165SLresult android_audioPlayer_create(CAudioPlayer *pAudioPlayer) { 1166 1167 SLresult result = SL_RESULT_SUCCESS; 1168 // pAudioPlayer->mAndroidObjType has been set in audioPlayer_getAndroidObjectTypeForSourceSink() 1169 if (INVALID_TYPE == pAudioPlayer->mAndroidObjType) { 1170 audioPlayer_setInvalid(pAudioPlayer); 1171 result = SL_RESULT_PARAMETER_INVALID; 1172 } else { 1173 1174 // These initializations are in the same order as the field declarations in classes.h 1175 1176 // FIXME Consolidate initializations (many of these already in IEngine_CreateAudioPlayer) 1177 pAudioPlayer->mpLock = new android::Mutex(); 1178 // mAndroidObjType: see above comment 1179 pAudioPlayer->mAndroidObjState = ANDROID_UNINITIALIZED; 1180 pAudioPlayer->mSessionId = android::AudioSystem::newAudioSessionId(); 1181 pAudioPlayer->mStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE; 1182 1183 // mAudioTrack 1184 pAudioPlayer->mCallbackProtector = new android::CallbackProtector(); 1185 // mAPLayer 1186 // mAuxEffect 1187 1188 pAudioPlayer->mAuxSendLevel = 0; 1189 pAudioPlayer->mAmplFromVolLevel = 1.0f; 1190 pAudioPlayer->mAmplFromStereoPos[0] = 1.0f; 1191 pAudioPlayer->mAmplFromStereoPos[1] = 1.0f; 1192 pAudioPlayer->mAmplFromDirectLevel = 1.0f; // matches initial mDirectLevel value 1193 pAudioPlayer->mDeferredStart = false; 1194 // Already initialized in IEngine_CreateAudioPlayer, to be consolidated 1195 pAudioPlayer->mDirectLevel = 0; // no attenuation 1196 1197 // initialize interface-specific fields that can be used regardless of whether the 1198 // interface is exposed on the AudioPlayer or not 1199 // (empty section, as all initializations are the same as the defaults) 1200 } 1201 1202 return result; 1203} 1204 1205 1206//----------------------------------------------------------------------------- 1207SLresult android_audioPlayer_setConfig(CAudioPlayer *ap, const SLchar *configKey, 1208 const void *pConfigValue, SLuint32 valueSize) { 1209 1210 SLresult result = SL_RESULT_SUCCESS; 1211 1212 if (NULL == ap) { 1213 result = SL_RESULT_INTERNAL_ERROR; 1214 } else if (NULL == pConfigValue) { 1215 SL_LOGE(ERROR_CONFIG_NULL_PARAM); 1216 result = SL_RESULT_PARAMETER_INVALID; 1217 1218 } else if(strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_STREAM_TYPE) == 0) { 1219 1220 // stream type 1221 if (KEY_STREAM_TYPE_PARAMSIZE > valueSize) { 1222 SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW); 1223 result = SL_RESULT_PARAMETER_INVALID; 1224 } else { 1225 result = audioPlayer_setStreamType(ap, *(SLuint32*)pConfigValue); 1226 } 1227 1228 } else { 1229 SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY); 1230 result = SL_RESULT_PARAMETER_INVALID; 1231 } 1232 1233 return result; 1234} 1235 1236 1237//----------------------------------------------------------------------------- 1238SLresult android_audioPlayer_getConfig(CAudioPlayer* ap, const SLchar *configKey, 1239 SLuint32* pValueSize, void *pConfigValue) { 1240 1241 SLresult result = SL_RESULT_SUCCESS; 1242 1243 if (NULL == ap) { 1244 return SL_RESULT_INTERNAL_ERROR; 1245 } else if (NULL == pValueSize) { 1246 SL_LOGE(ERROR_CONFIG_NULL_PARAM); 1247 result = SL_RESULT_PARAMETER_INVALID; 1248 1249 } else if(strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_STREAM_TYPE) == 0) { 1250 1251 // stream type 1252 if (KEY_STREAM_TYPE_PARAMSIZE > *pValueSize) { 1253 SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW); 1254 result = SL_RESULT_PARAMETER_INVALID; 1255 } else { 1256 *pValueSize = KEY_STREAM_TYPE_PARAMSIZE; 1257 if (NULL != pConfigValue) { 1258 result = audioPlayer_getStreamType(ap, (SLint32*)pConfigValue); 1259 } 1260 } 1261 1262 } else { 1263 SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY); 1264 result = SL_RESULT_PARAMETER_INVALID; 1265 } 1266 1267 return result; 1268} 1269 1270 1271//----------------------------------------------------------------------------- 1272SLresult android_audioPlayer_realize(CAudioPlayer *pAudioPlayer, SLboolean async) { 1273 1274 SLresult result = SL_RESULT_SUCCESS; 1275 SL_LOGV("Realize pAudioPlayer=%p", pAudioPlayer); 1276 1277 switch (pAudioPlayer->mAndroidObjType) { 1278 //----------------------------------- 1279 // AudioTrack 1280 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 1281 { 1282 // initialize platform-specific CAudioPlayer fields 1283 1284 SLDataLocator_BufferQueue *dl_bq = (SLDataLocator_BufferQueue *) 1285 pAudioPlayer->mDynamicSource.mDataSource; 1286 SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *) 1287 pAudioPlayer->mDynamicSource.mDataSource->pFormat; 1288 1289 uint32_t sampleRate = sles_to_android_sampleRate(df_pcm->samplesPerSec); 1290 1291 pAudioPlayer->mAudioTrack = new android::AudioTrackProxy(new android::AudioTrack( 1292 pAudioPlayer->mStreamType, // streamType 1293 sampleRate, // sampleRate 1294 sles_to_android_sampleFormat(df_pcm->bitsPerSample), // format 1295 sles_to_android_channelMaskOut(df_pcm->numChannels, df_pcm->channelMask), 1296 //channel mask 1297 0, // frameCount (here min) 1298 0, // flags 1299 audioTrack_callBack_pullFromBuffQueue, // callback 1300 (void *) pAudioPlayer, // user 1301 0 // FIXME find appropriate frame count // notificationFrame 1302 , pAudioPlayer->mSessionId 1303 )); 1304 android::status_t status = pAudioPlayer->mAudioTrack->initCheck(); 1305 if (status != android::NO_ERROR) { 1306 SL_LOGE("AudioTrack::initCheck status %u", status); 1307 result = SL_RESULT_CONTENT_UNSUPPORTED; 1308 pAudioPlayer->mAudioTrack.clear(); 1309 return result; 1310 } 1311 1312 // initialize platform-independent CAudioPlayer fields 1313 1314 pAudioPlayer->mNumChannels = df_pcm->numChannels; 1315 pAudioPlayer->mSampleRateMilliHz = df_pcm->samplesPerSec; // Note: bad field name in SL ES 1316 1317 pAudioPlayer->mAndroidObjState = ANDROID_READY; 1318 } 1319 break; 1320 //----------------------------------- 1321 // MediaPlayer 1322 case AUDIOPLAYER_FROM_URIFD: { 1323 object_lock_exclusive(&pAudioPlayer->mObject); 1324 1325 assert(pAudioPlayer->mAndroidObjState == ANDROID_UNINITIALIZED); 1326 assert(pAudioPlayer->mNumChannels == UNKNOWN_NUMCHANNELS); 1327 assert(pAudioPlayer->mSampleRateMilliHz == UNKNOWN_SAMPLERATE); 1328 assert(pAudioPlayer->mAudioTrack == 0); 1329 1330 AudioPlayback_Parameters app; 1331 app.sessionId = pAudioPlayer->mSessionId; 1332 app.streamType = pAudioPlayer->mStreamType; 1333 app.trackcb = audioTrack_callBack_uri; 1334 app.trackcbUser = (void *) pAudioPlayer; 1335 1336 pAudioPlayer->mAPlayer = new android::LocAVPlayer(&app, false /*hasVideo*/); 1337 pAudioPlayer->mAPlayer->init(sfplayer_handlePrefetchEvent, 1338 (void*)pAudioPlayer /*notifUSer*/); 1339 1340 object_unlock_exclusive(&pAudioPlayer->mObject); 1341 1342 switch (pAudioPlayer->mDataSource.mLocator.mLocatorType) { 1343 case SL_DATALOCATOR_URI: 1344 pAudioPlayer->mAPlayer->setDataSource( 1345 (const char*)pAudioPlayer->mDataSource.mLocator.mURI.URI); 1346 break; 1347 case SL_DATALOCATOR_ANDROIDFD: { 1348 int64_t offset = (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.offset; 1349 pAudioPlayer->mAPlayer->setDataSource( 1350 (int)pAudioPlayer->mDataSource.mLocator.mFD.fd, 1351 offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ? 1352 (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset, 1353 (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.length); 1354 } 1355 break; 1356 default: 1357 SL_LOGE(ERROR_PLAYERREALIZE_UNKNOWN_DATASOURCE_LOCATOR); 1358 break; 1359 } 1360 1361 } 1362 break; 1363 //----------------------------------- 1364 // StreamPlayer 1365 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: { 1366 object_lock_exclusive(&pAudioPlayer->mObject); 1367 1368 android_StreamPlayer_realize_l(pAudioPlayer, sfplayer_handlePrefetchEvent, 1369 (void*)pAudioPlayer); 1370 1371 object_unlock_exclusive(&pAudioPlayer->mObject); 1372 } 1373 break; 1374 //----------------------------------- 1375 // AudioToCbRenderer 1376 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: { 1377 object_lock_exclusive(&pAudioPlayer->mObject); 1378 1379 AudioPlayback_Parameters app; 1380 app.sessionId = pAudioPlayer->mSessionId; 1381 app.streamType = pAudioPlayer->mStreamType; 1382 1383 android::AudioToCbRenderer* decoder = new android::AudioToCbRenderer(&app); 1384 pAudioPlayer->mAPlayer = decoder; 1385 decoder->setDataPushListener(adecoder_writeToBufferQueue, (void*)pAudioPlayer); 1386 decoder->init(sfplayer_handlePrefetchEvent, (void*)pAudioPlayer); 1387 1388 switch (pAudioPlayer->mDataSource.mLocator.mLocatorType) { 1389 case SL_DATALOCATOR_URI: 1390 decoder->setDataSource( 1391 (const char*)pAudioPlayer->mDataSource.mLocator.mURI.URI); 1392 break; 1393 case SL_DATALOCATOR_ANDROIDFD: { 1394 int64_t offset = (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.offset; 1395 decoder->setDataSource( 1396 (int)pAudioPlayer->mDataSource.mLocator.mFD.fd, 1397 offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ? 1398 (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset, 1399 (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.length); 1400 } 1401 break; 1402 default: 1403 SL_LOGE(ERROR_PLAYERREALIZE_UNKNOWN_DATASOURCE_LOCATOR); 1404 break; 1405 } 1406 1407 object_unlock_exclusive(&pAudioPlayer->mObject); 1408 } 1409 break; 1410 //----------------------------------- 1411 default: 1412 SL_LOGE(ERROR_PLAYERREALIZE_UNEXPECTED_OBJECT_TYPE_D, pAudioPlayer->mAndroidObjType); 1413 result = SL_RESULT_INTERNAL_ERROR; 1414 break; 1415 } 1416 1417 1418 // proceed with effect initialization 1419 // initialize EQ 1420 // FIXME use a table of effect descriptors when adding support for more effects 1421 if (memcmp(SL_IID_EQUALIZER, &pAudioPlayer->mEqualizer.mEqDescriptor.type, 1422 sizeof(effect_uuid_t)) == 0) { 1423 SL_LOGV("Need to initialize EQ for AudioPlayer=%p", pAudioPlayer); 1424 android_eq_init(pAudioPlayer->mSessionId, &pAudioPlayer->mEqualizer); 1425 } 1426 // initialize BassBoost 1427 if (memcmp(SL_IID_BASSBOOST, &pAudioPlayer->mBassBoost.mBassBoostDescriptor.type, 1428 sizeof(effect_uuid_t)) == 0) { 1429 SL_LOGV("Need to initialize BassBoost for AudioPlayer=%p", pAudioPlayer); 1430 android_bb_init(pAudioPlayer->mSessionId, &pAudioPlayer->mBassBoost); 1431 } 1432 // initialize Virtualizer 1433 if (memcmp(SL_IID_VIRTUALIZER, &pAudioPlayer->mVirtualizer.mVirtualizerDescriptor.type, 1434 sizeof(effect_uuid_t)) == 0) { 1435 SL_LOGV("Need to initialize Virtualizer for AudioPlayer=%p", pAudioPlayer); 1436 android_virt_init(pAudioPlayer->mSessionId, &pAudioPlayer->mVirtualizer); 1437 } 1438 1439 // initialize EffectSend 1440 // FIXME initialize EffectSend 1441 1442 return result; 1443} 1444 1445 1446//----------------------------------------------------------------------------- 1447/** 1448 * Called with a lock on AudioPlayer 1449 */ 1450SLresult android_audioPlayer_preDestroy(CAudioPlayer *pAudioPlayer) { 1451 SL_LOGD("android_audioPlayer_preDestroy(%p)", pAudioPlayer); 1452 SLresult result = SL_RESULT_SUCCESS; 1453 1454 if (pAudioPlayer->mAPlayer != 0) { 1455 pAudioPlayer->mAPlayer->preDestroy(); 1456 } 1457 SL_LOGD("android_audioPlayer_preDestroy(%p) after mAPlayer->preDestroy()", pAudioPlayer); 1458 1459 object_unlock_exclusive(&pAudioPlayer->mObject); 1460 if (pAudioPlayer->mCallbackProtector != 0) { 1461 pAudioPlayer->mCallbackProtector->requestCbExitAndWait(); 1462 } 1463 object_lock_exclusive(&pAudioPlayer->mObject); 1464 1465 return result; 1466} 1467 1468 1469//----------------------------------------------------------------------------- 1470SLresult android_audioPlayer_destroy(CAudioPlayer *pAudioPlayer) { 1471 SLresult result = SL_RESULT_SUCCESS; 1472 SL_LOGV("android_audioPlayer_destroy(%p)", pAudioPlayer); 1473 switch (pAudioPlayer->mAndroidObjType) { 1474 1475 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 1476 // We own the audio track for PCM buffer queue players 1477 if (pAudioPlayer->mAudioTrack != 0) { 1478 pAudioPlayer->mAudioTrack->stop(); 1479 // Note that there may still be another reference in post-unlock phase of SetPlayState 1480 pAudioPlayer->mAudioTrack.clear(); 1481 } 1482 break; 1483 1484 case AUDIOPLAYER_FROM_URIFD: // intended fall-through 1485 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 1486 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 1487 pAudioPlayer->mAPlayer.clear(); 1488 break; 1489 //----------------------------------- 1490 default: 1491 SL_LOGE(ERROR_PLAYERDESTROY_UNEXPECTED_OBJECT_TYPE_D, pAudioPlayer->mAndroidObjType); 1492 result = SL_RESULT_INTERNAL_ERROR; 1493 break; 1494 } 1495 1496 pAudioPlayer->mCallbackProtector.clear(); 1497 1498 // FIXME might not be needed 1499 pAudioPlayer->mAndroidObjType = INVALID_TYPE; 1500 1501 // explicit destructor 1502 pAudioPlayer->mAudioTrack.~sp(); 1503 // note that SetPlayState(PLAYING) may still hold a reference 1504 pAudioPlayer->mCallbackProtector.~sp(); 1505 pAudioPlayer->mAuxEffect.~sp(); 1506 pAudioPlayer->mAPlayer.~sp(); 1507 1508 if (pAudioPlayer->mpLock != NULL) { 1509 delete pAudioPlayer->mpLock; 1510 pAudioPlayer->mpLock = NULL; 1511 } 1512 1513 return result; 1514} 1515 1516 1517//----------------------------------------------------------------------------- 1518SLresult android_audioPlayer_setPlayRate(CAudioPlayer *ap, SLpermille rate, bool lockAP) { 1519 SLresult result = SL_RESULT_SUCCESS; 1520 uint32_t contentRate = 0; 1521 switch(ap->mAndroidObjType) { 1522 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 1523 case AUDIOPLAYER_FROM_URIFD: { 1524 // get the content sample rate 1525 if (lockAP) { object_lock_shared(&ap->mObject); } 1526 uint32_t contentRate = sles_to_android_sampleRate(ap->mSampleRateMilliHz); 1527 if (lockAP) { object_unlock_shared(&ap->mObject); } 1528 // apply the SL ES playback rate on the AudioTrack as a factor of its content sample rate 1529 if (ap->mAudioTrack != 0) { 1530 ap->mAudioTrack->setSampleRate(contentRate * (rate/1000.0f)); 1531 } 1532 } 1533 break; 1534 1535 default: 1536 SL_LOGE("Unexpected object type %d", ap->mAndroidObjType); 1537 result = SL_RESULT_INTERNAL_ERROR; 1538 break; 1539 } 1540 return result; 1541} 1542 1543 1544//----------------------------------------------------------------------------- 1545// called with no lock held 1546SLresult android_audioPlayer_setPlaybackRateBehavior(CAudioPlayer *ap, 1547 SLuint32 constraints) { 1548 SLresult result = SL_RESULT_SUCCESS; 1549 switch(ap->mAndroidObjType) { 1550 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 1551 case AUDIOPLAYER_FROM_URIFD: 1552 if (constraints != (constraints & SL_RATEPROP_NOPITCHCORAUDIO)) { 1553 result = SL_RESULT_FEATURE_UNSUPPORTED; 1554 } 1555 break; 1556 default: 1557 SL_LOGE("Unexpected object type %d", ap->mAndroidObjType); 1558 result = SL_RESULT_INTERNAL_ERROR; 1559 break; 1560 } 1561 return result; 1562} 1563 1564 1565//----------------------------------------------------------------------------- 1566// called with no lock held 1567SLresult android_audioPlayer_getCapabilitiesOfRate(CAudioPlayer *ap, 1568 SLuint32 *pCapabilities) { 1569 switch(ap->mAndroidObjType) { 1570 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 1571 case AUDIOPLAYER_FROM_URIFD: 1572 *pCapabilities = SL_RATEPROP_NOPITCHCORAUDIO; 1573 break; 1574 default: 1575 *pCapabilities = 0; 1576 break; 1577 } 1578 return SL_RESULT_SUCCESS; 1579} 1580 1581 1582//----------------------------------------------------------------------------- 1583// precondition 1584// called with no lock held 1585// ap != NULL 1586// pItemCount != NULL 1587SLresult android_audioPlayer_metadata_getItemCount(CAudioPlayer *ap, SLuint32 *pItemCount) { 1588 if (ap->mAPlayer == 0) { 1589 return SL_RESULT_PARAMETER_INVALID; 1590 } 1591 switch(ap->mAndroidObjType) { 1592 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 1593 { 1594 android::AudioSfDecoder* decoder = 1595 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get()); 1596 *pItemCount = decoder->getPcmFormatKeyCount(); 1597 } 1598 break; 1599 default: 1600 *pItemCount = 0; 1601 break; 1602 } 1603 return SL_RESULT_SUCCESS; 1604} 1605 1606 1607//----------------------------------------------------------------------------- 1608// precondition 1609// called with no lock held 1610// ap != NULL 1611// pKeySize != NULL 1612SLresult android_audioPlayer_metadata_getKeySize(CAudioPlayer *ap, 1613 SLuint32 index, SLuint32 *pKeySize) { 1614 if (ap->mAPlayer == 0) { 1615 return SL_RESULT_PARAMETER_INVALID; 1616 } 1617 SLresult res = SL_RESULT_SUCCESS; 1618 switch(ap->mAndroidObjType) { 1619 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 1620 { 1621 android::AudioSfDecoder* decoder = 1622 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get()); 1623 SLuint32 keyNameSize = 0; 1624 if (!decoder->getPcmFormatKeySize(index, &keyNameSize)) { 1625 res = SL_RESULT_PARAMETER_INVALID; 1626 } else { 1627 // *pKeySize is the size of the region used to store the key name AND 1628 // the information about the key (size, lang, encoding) 1629 *pKeySize = keyNameSize + sizeof(SLMetadataInfo); 1630 } 1631 } 1632 break; 1633 default: 1634 *pKeySize = 0; 1635 res = SL_RESULT_PARAMETER_INVALID; 1636 break; 1637 } 1638 return res; 1639} 1640 1641 1642//----------------------------------------------------------------------------- 1643// precondition 1644// called with no lock held 1645// ap != NULL 1646// pKey != NULL 1647SLresult android_audioPlayer_metadata_getKey(CAudioPlayer *ap, 1648 SLuint32 index, SLuint32 size, SLMetadataInfo *pKey) { 1649 if (ap->mAPlayer == 0) { 1650 return SL_RESULT_PARAMETER_INVALID; 1651 } 1652 SLresult res = SL_RESULT_SUCCESS; 1653 switch(ap->mAndroidObjType) { 1654 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 1655 { 1656 android::AudioSfDecoder* decoder = 1657 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get()); 1658 if ((size < sizeof(SLMetadataInfo) || 1659 (!decoder->getPcmFormatKeyName(index, size - sizeof(SLMetadataInfo), 1660 (char*)pKey->data)))) { 1661 res = SL_RESULT_PARAMETER_INVALID; 1662 } else { 1663 // successfully retrieved the key value, update the other fields 1664 pKey->encoding = SL_CHARACTERENCODING_UTF8; 1665 memcpy((char *) pKey->langCountry, "en", 3); 1666 pKey->size = strlen((char*)pKey->data) + 1; 1667 } 1668 } 1669 break; 1670 default: 1671 res = SL_RESULT_PARAMETER_INVALID; 1672 break; 1673 } 1674 return res; 1675} 1676 1677 1678//----------------------------------------------------------------------------- 1679// precondition 1680// called with no lock held 1681// ap != NULL 1682// pValueSize != NULL 1683SLresult android_audioPlayer_metadata_getValueSize(CAudioPlayer *ap, 1684 SLuint32 index, SLuint32 *pValueSize) { 1685 if (ap->mAPlayer == 0) { 1686 return SL_RESULT_PARAMETER_INVALID; 1687 } 1688 SLresult res = SL_RESULT_SUCCESS; 1689 switch(ap->mAndroidObjType) { 1690 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 1691 { 1692 android::AudioSfDecoder* decoder = 1693 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get()); 1694 SLuint32 valueSize = 0; 1695 if (!decoder->getPcmFormatValueSize(index, &valueSize)) { 1696 res = SL_RESULT_PARAMETER_INVALID; 1697 } else { 1698 // *pValueSize is the size of the region used to store the key value AND 1699 // the information about the value (size, lang, encoding) 1700 *pValueSize = valueSize + sizeof(SLMetadataInfo); 1701 } 1702 } 1703 break; 1704 default: 1705 *pValueSize = 0; 1706 res = SL_RESULT_PARAMETER_INVALID; 1707 break; 1708 } 1709 return res; 1710} 1711 1712 1713//----------------------------------------------------------------------------- 1714// precondition 1715// called with no lock held 1716// ap != NULL 1717// pValue != NULL 1718SLresult android_audioPlayer_metadata_getValue(CAudioPlayer *ap, 1719 SLuint32 index, SLuint32 size, SLMetadataInfo *pValue) { 1720 if (ap->mAPlayer == 0) { 1721 return SL_RESULT_PARAMETER_INVALID; 1722 } 1723 SLresult res = SL_RESULT_SUCCESS; 1724 switch(ap->mAndroidObjType) { 1725 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 1726 { 1727 android::AudioSfDecoder* decoder = 1728 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get()); 1729 pValue->encoding = SL_CHARACTERENCODING_BINARY; 1730 memcpy((char *) pValue->langCountry, "en", 3); // applicable here? 1731 SLuint32 valueSize = 0; 1732 if ((size < sizeof(SLMetadataInfo) 1733 || (!decoder->getPcmFormatValueSize(index, &valueSize)) 1734 || (!decoder->getPcmFormatKeyValue(index, size - sizeof(SLMetadataInfo), 1735 (SLuint32*)pValue->data)))) { 1736 res = SL_RESULT_PARAMETER_INVALID; 1737 } else { 1738 pValue->size = valueSize; 1739 } 1740 } 1741 break; 1742 default: 1743 res = SL_RESULT_PARAMETER_INVALID; 1744 break; 1745 } 1746 return res; 1747} 1748 1749//----------------------------------------------------------------------------- 1750// preconditions 1751// ap != NULL 1752// mutex is locked 1753// play state has changed 1754void android_audioPlayer_setPlayState(CAudioPlayer *ap) { 1755 1756 SLuint32 playState = ap->mPlay.mState; 1757 AndroidObjectState objState = ap->mAndroidObjState; 1758 1759 switch(ap->mAndroidObjType) { 1760 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 1761 switch (playState) { 1762 case SL_PLAYSTATE_STOPPED: 1763 SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_STOPPED"); 1764 if (ap->mAudioTrack != 0) { 1765 ap->mAudioTrack->stop(); 1766 } 1767 break; 1768 case SL_PLAYSTATE_PAUSED: 1769 SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PAUSED"); 1770 if (ap->mAudioTrack != 0) { 1771 ap->mAudioTrack->pause(); 1772 } 1773 break; 1774 case SL_PLAYSTATE_PLAYING: 1775 SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PLAYING"); 1776 if (ap->mAudioTrack != 0) { 1777 // instead of ap->mAudioTrack->start(); 1778 ap->mDeferredStart = true; 1779 } 1780 break; 1781 default: 1782 // checked by caller, should not happen 1783 break; 1784 } 1785 break; 1786 1787 case AUDIOPLAYER_FROM_URIFD: // intended fall-through 1788 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 1789 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 1790 // FIXME report and use the return code to the lock mechanism, which is where play state 1791 // changes are updated (see object_unlock_exclusive_attributes()) 1792 aplayer_setPlayState(ap->mAPlayer, playState, &(ap->mAndroidObjState)); 1793 break; 1794 default: 1795 SL_LOGE(ERROR_PLAYERSETPLAYSTATE_UNEXPECTED_OBJECT_TYPE_D, ap->mAndroidObjType); 1796 break; 1797 } 1798} 1799 1800 1801//----------------------------------------------------------------------------- 1802void android_audioPlayer_useEventMask(CAudioPlayer *ap) { 1803 IPlay *pPlayItf = &ap->mPlay; 1804 SLuint32 eventFlags = pPlayItf->mEventFlags; 1805 /*switch(ap->mAndroidObjType) { 1806 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:*/ 1807 1808 if (ap->mAudioTrack == 0) { 1809 return; 1810 } 1811 1812 if (eventFlags & SL_PLAYEVENT_HEADATMARKER) { 1813 ap->mAudioTrack->setMarkerPosition((uint32_t)((((int64_t)pPlayItf->mMarkerPosition 1814 * sles_to_android_sampleRate(ap->mSampleRateMilliHz)))/1000)); 1815 } else { 1816 // clear marker 1817 ap->mAudioTrack->setMarkerPosition(0); 1818 } 1819 1820 if (eventFlags & SL_PLAYEVENT_HEADATNEWPOS) { 1821 ap->mAudioTrack->setPositionUpdatePeriod( 1822 (uint32_t)((((int64_t)pPlayItf->mPositionUpdatePeriod 1823 * sles_to_android_sampleRate(ap->mSampleRateMilliHz)))/1000)); 1824 } else { 1825 // clear periodic update 1826 ap->mAudioTrack->setPositionUpdatePeriod(0); 1827 } 1828 1829 if (eventFlags & SL_PLAYEVENT_HEADATEND) { 1830 // nothing to do for SL_PLAYEVENT_HEADATEND, callback event will be checked against mask 1831 } 1832 1833 if (eventFlags & SL_PLAYEVENT_HEADMOVING) { 1834 // FIXME support SL_PLAYEVENT_HEADMOVING 1835 SL_LOGD("[ FIXME: IPlay_SetCallbackEventsMask(SL_PLAYEVENT_HEADMOVING) on an " 1836 "SL_OBJECTID_AUDIOPLAYER to be implemented ]"); 1837 } 1838 if (eventFlags & SL_PLAYEVENT_HEADSTALLED) { 1839 // nothing to do for SL_PLAYEVENT_HEADSTALLED, callback event will be checked against mask 1840 } 1841 1842} 1843 1844 1845//----------------------------------------------------------------------------- 1846SLresult android_audioPlayer_getDuration(IPlay *pPlayItf, SLmillisecond *pDurMsec) { 1847 CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis; 1848 switch(ap->mAndroidObjType) { 1849 1850 case AUDIOPLAYER_FROM_URIFD: // intended fall-through 1851 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: { 1852 int32_t durationMsec = ANDROID_UNKNOWN_TIME; 1853 if (ap->mAPlayer != 0) { 1854 ap->mAPlayer->getDurationMsec(&durationMsec); 1855 } 1856 *pDurMsec = durationMsec == ANDROID_UNKNOWN_TIME ? SL_TIME_UNKNOWN : durationMsec; 1857 break; 1858 } 1859 1860 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 1861 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: // intended fall-through 1862 default: { 1863 *pDurMsec = SL_TIME_UNKNOWN; 1864 } 1865 } 1866 return SL_RESULT_SUCCESS; 1867} 1868 1869 1870//----------------------------------------------------------------------------- 1871void android_audioPlayer_getPosition(IPlay *pPlayItf, SLmillisecond *pPosMsec) { 1872 CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis; 1873 switch(ap->mAndroidObjType) { 1874 1875 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 1876 if ((ap->mSampleRateMilliHz == UNKNOWN_SAMPLERATE) || (ap->mAudioTrack == 0)) { 1877 *pPosMsec = 0; 1878 } else { 1879 uint32_t positionInFrames; 1880 ap->mAudioTrack->getPosition(&positionInFrames); 1881 *pPosMsec = ((int64_t)positionInFrames * 1000) / 1882 sles_to_android_sampleRate(ap->mSampleRateMilliHz); 1883 } 1884 break; 1885 1886 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 1887 case AUDIOPLAYER_FROM_URIFD: // intended fall-through 1888 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: { 1889 int32_t posMsec = ANDROID_UNKNOWN_TIME; 1890 if (ap->mAPlayer != 0) { 1891 ap->mAPlayer->getPositionMsec(&posMsec); 1892 } 1893 *pPosMsec = posMsec == ANDROID_UNKNOWN_TIME ? 0 : posMsec; 1894 break; 1895 } 1896 1897 default: 1898 *pPosMsec = 0; 1899 } 1900} 1901 1902 1903//----------------------------------------------------------------------------- 1904void android_audioPlayer_seek(CAudioPlayer *ap, SLmillisecond posMsec) { 1905 1906 switch(ap->mAndroidObjType) { 1907 1908 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: // intended fall-through 1909 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: 1910 break; 1911 1912 case AUDIOPLAYER_FROM_URIFD: // intended fall-through 1913 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 1914 if (ap->mAPlayer != 0) { 1915 ap->mAPlayer->seek(posMsec); 1916 } 1917 break; 1918 1919 default: 1920 break; 1921 } 1922} 1923 1924 1925//----------------------------------------------------------------------------- 1926void android_audioPlayer_loop(CAudioPlayer *ap, SLboolean loopEnable) { 1927 1928 if ((AUDIOPLAYER_FROM_URIFD == ap->mAndroidObjType) && (ap->mAPlayer != 0)) { 1929 ap->mAPlayer->loop((bool)loopEnable); 1930 } 1931} 1932 1933 1934//----------------------------------------------------------------------------- 1935/* 1936 * Mutes or unmutes the Android media framework object associated with the CAudioPlayer that carries 1937 * the IVolume interface. 1938 * Pre-condition: 1939 * if ap->mMute is SL_BOOLEAN_FALSE, a call to this function was preceded by a call 1940 * to android_audioPlayer_volumeUpdate() 1941 */ 1942static void android_audioPlayer_setMute(CAudioPlayer* ap) { 1943 switch(ap->mAndroidObjType) { 1944 1945 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 1946 if (ap->mAudioTrack != 0) { 1947 // when unmuting: volume levels have already been updated in IVolume_SetMute 1948 ap->mAudioTrack->mute(ap->mMute); 1949 } 1950 break; 1951 1952 case AUDIOPLAYER_FROM_URIFD: // intended fall-through 1953 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: 1954 if ( (ap->mAPlayer != 0) && (NULL != &ap->mVolume) ) { 1955 android_Player_volumeUpdate(ap->mAPlayer, &ap->mVolume); 1956 } 1957 break; 1958 1959 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: // intented fall-through 1960 default: 1961 break; 1962 } 1963} 1964 1965 1966//----------------------------------------------------------------------------- 1967SLresult android_audioPlayer_volumeUpdate(CAudioPlayer* ap) { 1968 android_audioPlayer_updateStereoVolume(ap); 1969 android_audioPlayer_setMute(ap); 1970 return SL_RESULT_SUCCESS; 1971} 1972 1973 1974//----------------------------------------------------------------------------- 1975SLresult android_audioPlayer_setBufferingUpdateThresholdPerMille(CAudioPlayer *ap, 1976 SLpermille threshold) { 1977 SLresult result = SL_RESULT_SUCCESS; 1978 1979 switch (ap->mAndroidObjType) { 1980 case AUDIOPLAYER_FROM_URIFD: 1981 if (ap->mAPlayer != 0) { 1982 ap->mAPlayer->setBufferingUpdateThreshold(threshold / 10); 1983 } 1984 break; 1985 1986 default: {} 1987 } 1988 1989 return result; 1990} 1991 1992 1993//----------------------------------------------------------------------------- 1994void android_audioPlayer_bufferQueue_onRefilled_l(CAudioPlayer *ap) { 1995 // the AudioTrack associated with the AudioPlayer receiving audio from a PCM buffer 1996 // queue was stopped when the queue become empty, we restart as soon as a new buffer 1997 // has been enqueued since we're in playing state 1998 if (ap->mAudioTrack != 0) { 1999 // instead of ap->mAudioTrack->start(); 2000 ap->mDeferredStart = true; 2001 } 2002 2003 // when the queue became empty, an underflow on the prefetch status itf was sent. Now the queue 2004 // has received new data, signal it has sufficient data 2005 if (IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) { 2006 audioPlayer_dispatch_prefetchStatus_lockPrefetch(ap, SL_PREFETCHSTATUS_SUFFICIENTDATA, 2007 true); 2008 } 2009} 2010 2011 2012//----------------------------------------------------------------------------- 2013/* 2014 * BufferQueue::Clear 2015 */ 2016SLresult android_audioPlayer_bufferQueue_onClear(CAudioPlayer *ap) { 2017 SLresult result = SL_RESULT_SUCCESS; 2018 2019 switch (ap->mAndroidObjType) { 2020 //----------------------------------- 2021 // AudioTrack 2022 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 2023 if (ap->mAudioTrack != 0) { 2024 ap->mAudioTrack->flush(); 2025 } 2026 break; 2027 default: 2028 result = SL_RESULT_INTERNAL_ERROR; 2029 break; 2030 } 2031 2032 return result; 2033} 2034 2035 2036//----------------------------------------------------------------------------- 2037void android_audioPlayer_androidBufferQueue_registerCallback_l(CAudioPlayer *ap) { 2038 if ((ap->mAndroidObjType == AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) && (ap->mAPlayer != 0)) { 2039 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(ap->mAPlayer.get()); 2040 splr->registerQueueCallback( 2041 (const void*)ap, true /*userIsAudioPlayer*/, 2042 ap->mAndroidBufferQueue.mContext, 2043 (const void*)&(ap->mAndroidBufferQueue.mItf)); 2044 } 2045} 2046 2047//----------------------------------------------------------------------------- 2048void android_audioPlayer_androidBufferQueue_clear_l(CAudioPlayer *ap) { 2049 if ((ap->mAndroidObjType == AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) && (ap->mAPlayer != 0)) { 2050 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(ap->mAPlayer.get()); 2051 splr->appClear_l(); 2052 } 2053} 2054 2055void android_audioPlayer_androidBufferQueue_onRefilled_l(CAudioPlayer *ap) { 2056 if ((ap->mAndroidObjType == AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) && (ap->mAPlayer != 0)) { 2057 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(ap->mAPlayer.get()); 2058 splr->queueRefilled_l(); 2059 } 2060} 2061