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