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