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