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