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