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