AudioPlayer_to_android.cpp revision be59fc5cfd9354d70d4b0e28bb2bca24a6ca6f22
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 21template class android::KeyedVector<SLuint32, android::AudioEffect* > ; 22 23#define KEY_STREAM_TYPE_PARAMSIZE sizeof(SLint32) 24 25//----------------------------------------------------------------------------- 26int 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 54static 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//----------------------------------------------------------------------------- 162void 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//----------------------------------------------------------------------------- 179void 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//----------------------------------------------------------------------------- 197void 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 */ 227void 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 */ 263void 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//----------------------------------------------------------------------------- 292SLresult 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//----------------------------------------------------------------------------- 336SLresult 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//----------------------------------------------------------------------------- 370void 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 379static 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 395static 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//----------------------------------------------------------------------------- 562SLresult 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 XA_DATAFORMAT_RAWIMAGE: 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 // invalid data format is detected earlier 672 assert(false); 673 return SL_RESULT_INTERNAL_ERROR; 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 XA_DATAFORMAT_RAWIMAGE: 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 XA_DATAFORMAT_RAWIMAGE: 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 default: 714 // invalid data format is detected earlier 715 assert(false); 716 return SL_RESULT_INTERNAL_ERROR; 717 } // switch (formatType) 718 } // case SL_DATALOCATOR_ANDROIDFD 719 break; 720 //------------------ 721 // Stream 722 case SL_DATALOCATOR_ANDROIDBUFFERQUEUE: 723 { 724 } // case SL_DATALOCATOR_ANDROIDBUFFERQUEUE 725 break; 726 //------------------ 727 // Address 728 case SL_DATALOCATOR_ADDRESS: 729 case SL_DATALOCATOR_IODEVICE: 730 case SL_DATALOCATOR_OUTPUTMIX: 731 case XA_DATALOCATOR_NATIVEDISPLAY: 732 case SL_DATALOCATOR_MIDIBUFFERQUEUE: 733 SL_LOGE("Cannot create audio player with data locator type 0x%x", (unsigned) locatorType); 734 return SL_RESULT_CONTENT_UNSUPPORTED; 735 default: 736 SL_LOGE("Cannot create audio player with invalid data locator type 0x%x", 737 (unsigned) locatorType); 738 return SL_RESULT_PARAMETER_INVALID; 739 }// switch (locatorType) 740 741 return SL_RESULT_SUCCESS; 742} 743 744 745 746//----------------------------------------------------------------------------- 747static void audioTrack_callBack_uri(int event, void* user, void *info) { 748 // EVENT_MORE_DATA needs to be handled with priority over the other events 749 // because it will be called the most often during playback 750 if (event == android::AudioTrack::EVENT_MORE_DATA) { 751 //SL_LOGV("received event EVENT_MORE_DATA from AudioTrack"); 752 // set size to 0 to signal we're not using the callback to write more data 753 android::AudioTrack::Buffer* pBuff = (android::AudioTrack::Buffer*)info; 754 pBuff->size = 0; 755 } else if (NULL != user) { 756 switch (event) { 757 case (android::AudioTrack::EVENT_MARKER) : 758 audioTrack_handleMarker_lockPlay((CAudioPlayer *)user); 759 break; 760 case (android::AudioTrack::EVENT_NEW_POS) : 761 audioTrack_handleNewPos_lockPlay((CAudioPlayer *)user); 762 break; 763 case (android::AudioTrack::EVENT_UNDERRUN) : 764 audioTrack_handleUnderrun_lockPlay((CAudioPlayer *)user); 765 break; 766 case (android::AudioTrack::EVENT_BUFFER_END) : 767 case (android::AudioTrack::EVENT_LOOP_END) : 768 break; 769 default: 770 SL_LOGE("Encountered unknown AudioTrack event %d for CAudioPlayer %p", event, 771 (CAudioPlayer *)user); 772 break; 773 } 774 } 775} 776 777//----------------------------------------------------------------------------- 778// Callback associated with an AudioTrack of an SL ES AudioPlayer that gets its data 779// from a buffer queue. 780static void audioTrack_callBack_pullFromBuffQueue(int event, void* user, void *info) { 781 CAudioPlayer *ap = (CAudioPlayer *)user; 782 void * callbackPContext = NULL; 783 switch(event) { 784 785 case (android::AudioTrack::EVENT_MORE_DATA) : { 786 //SL_LOGV("received event EVENT_MORE_DATA from AudioTrack"); 787 slBufferQueueCallback callback = NULL; 788 android::AudioTrack::Buffer* pBuff = (android::AudioTrack::Buffer*)info; 789 // retrieve data from the buffer queue 790 interface_lock_exclusive(&ap->mBufferQueue); 791 if (ap->mBufferQueue.mState.count != 0) { 792 //SL_LOGV("nbBuffers in queue = %lu",ap->mBufferQueue.mState.count); 793 assert(ap->mBufferQueue.mFront != ap->mBufferQueue.mRear); 794 795 BufferHeader *oldFront = ap->mBufferQueue.mFront; 796 BufferHeader *newFront = &oldFront[1]; 797 798 // FIXME handle 8bit based on buffer format 799 short *pSrc = (short*)((char *)oldFront->mBuffer 800 + ap->mBufferQueue.mSizeConsumed); 801 if (ap->mBufferQueue.mSizeConsumed + pBuff->size < oldFront->mSize) { 802 // can't consume the whole or rest of the buffer in one shot 803 ap->mBufferQueue.mSizeConsumed += pBuff->size; 804 // leave pBuff->size untouched 805 // consume data 806 // FIXME can we avoid holding the lock during the copy? 807 memcpy (pBuff->i16, pSrc, pBuff->size); 808 } else { 809 // finish consuming the buffer or consume the buffer in one shot 810 pBuff->size = oldFront->mSize - ap->mBufferQueue.mSizeConsumed; 811 ap->mBufferQueue.mSizeConsumed = 0; 812 813 if (newFront == 814 &ap->mBufferQueue.mArray 815 [ap->mBufferQueue.mNumBuffers + 1]) 816 { 817 newFront = ap->mBufferQueue.mArray; 818 } 819 ap->mBufferQueue.mFront = newFront; 820 821 ap->mBufferQueue.mState.count--; 822 ap->mBufferQueue.mState.playIndex++; 823 824 // consume data 825 // FIXME can we avoid holding the lock during the copy? 826 memcpy (pBuff->i16, pSrc, pBuff->size); 827 828 // data has been consumed, and the buffer queue state has been updated 829 // we will notify the client if applicable 830 callback = ap->mBufferQueue.mCallback; 831 // save callback data 832 callbackPContext = ap->mBufferQueue.mContext; 833 } 834 } else { // empty queue 835 // signal no data available 836 pBuff->size = 0; 837 838 // signal we're at the end of the content, but don't pause (see note in function) 839 audioPlayer_dispatch_headAtEnd_lockPlay(ap, false /*set state to paused?*/, false); 840 841 // signal underflow to prefetch status itf 842 if (IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) { 843 audioPlayer_dispatch_prefetchStatus_lockPrefetch(ap, SL_PREFETCHSTATUS_UNDERFLOW, 844 false); 845 } 846 847 // stop the track so it restarts playing faster when new data is enqueued 848 ap->mAudioTrack->stop(); 849 } 850 interface_unlock_exclusive(&ap->mBufferQueue); 851 // notify client 852 if (NULL != callback) { 853 (*callback)(&ap->mBufferQueue.mItf, callbackPContext); 854 } 855 } 856 break; 857 858 case (android::AudioTrack::EVENT_MARKER) : 859 audioTrack_handleMarker_lockPlay(ap); 860 break; 861 862 case (android::AudioTrack::EVENT_NEW_POS) : 863 audioTrack_handleNewPos_lockPlay(ap); 864 break; 865 866 case (android::AudioTrack::EVENT_UNDERRUN) : 867 audioTrack_handleUnderrun_lockPlay(ap); 868 break; 869 870 default: 871 // FIXME where does the notification of SL_PLAYEVENT_HEADMOVING fit? 872 SL_LOGE("Encountered unknown AudioTrack event %d for CAudioPlayer %p", event, 873 (CAudioPlayer *)user); 874 break; 875 } 876} 877 878 879//----------------------------------------------------------------------------- 880SLresult android_audioPlayer_create( 881 CAudioPlayer *pAudioPlayer) { 882 883 const SLDataSource *pAudioSrc = &pAudioPlayer->mDataSource.u.mSource; 884 const SLDataSink *pAudioSnk = &pAudioPlayer->mDataSink.u.mSink; 885 SLresult result = SL_RESULT_SUCCESS; 886 887 //-------------------------------------- 888 // Sink check: 889 // currently only OutputMix sinks are supported 890 // this has already been verified in sles_to_android_CheckAudioPlayerSourceSink 891 // SLuint32 locatorType = *(SLuint32 *)pAudioSnk->pLocator; 892 // if (SL_DATALOCATOR_OUTPUTMIX == locatorType) { 893 // } 894 895 //-------------------------------------- 896 // Source check: 897 SLuint32 locatorType = *(SLuint32 *)pAudioSrc->pLocator; 898 switch (locatorType) { 899 // ----------------------------------- 900 // Buffer Queue to AudioTrack 901 case SL_DATALOCATOR_BUFFERQUEUE: 902 case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE: 903 pAudioPlayer->mAndroidObjType = AUDIOTRACK_PULL; 904 pAudioPlayer->mpLock = new android::Mutex(); 905 pAudioPlayer->mPlaybackRate.mCapabilities = SL_RATEPROP_NOPITCHCORAUDIO; 906 break; 907 // ----------------------------------- 908 // URI or FD to MediaPlayer 909 case SL_DATALOCATOR_URI: 910 case SL_DATALOCATOR_ANDROIDFD: 911 pAudioPlayer->mAndroidObjType = MEDIAPLAYER; 912 pAudioPlayer->mpLock = new android::Mutex(); 913 pAudioPlayer->mPlaybackRate.mCapabilities = SL_RATEPROP_NOPITCHCORAUDIO; 914 break; 915 case SL_DATALOCATOR_ANDROIDBUFFERQUEUE: 916 pAudioPlayer->mAndroidObjType = STREAM_SOURCE; 917 pAudioPlayer->mpLock = new android::Mutex(); 918 pAudioPlayer->mPlaybackRate.mCapabilities = SL_RATEPROP_NOPITCHCORAUDIO; 919 break; 920 default: 921 pAudioPlayer->mAndroidObjType = INVALID_TYPE; 922 pAudioPlayer->mpLock = NULL; 923 pAudioPlayer->mPlaybackRate.mCapabilities = 0; 924 result = SL_RESULT_PARAMETER_INVALID; 925 break; 926 } 927 928 pAudioPlayer->mAndroidObjState = ANDROID_UNINITIALIZED; 929 pAudioPlayer->mStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE; 930 pAudioPlayer->mAudioTrack = NULL; 931#ifndef USE_BACKPORT 932 // no longer needed, as placement new (explicit constructor) already does this 933 // pAudioPlayer->mSfPlayer.clear(); 934#endif 935 936#ifndef USE_BACKPORT 937 pAudioPlayer->mSessionId = android::AudioSystem::newAudioSessionId(); 938#endif 939 940 pAudioPlayer->mAmplFromVolLevel = 1.0f; 941 pAudioPlayer->mAmplFromStereoPos[0] = 1.0f; 942 pAudioPlayer->mAmplFromStereoPos[1] = 1.0f; 943 pAudioPlayer->mDirectLevel = 0; // no attenuation 944 pAudioPlayer->mAmplFromDirectLevel = 1.0f; // matches initial mDirectLevel value 945 pAudioPlayer->mAuxSendLevel = 0; 946 947 // initialize interface-specific fields that can be used regardless of whether the interface 948 // is exposed on the AudioPlayer or not 949 // (section no longer applicable, as all previous initializations were the same as the defaults) 950 951 return result; 952 953} 954 955 956//----------------------------------------------------------------------------- 957SLresult android_audioPlayer_setConfig(CAudioPlayer *ap, const SLchar *configKey, 958 const void *pConfigValue, SLuint32 valueSize) { 959 960 SLresult result = SL_RESULT_SUCCESS; 961 962 if (NULL == ap) { 963 result = SL_RESULT_INTERNAL_ERROR; 964 } else if (NULL == pConfigValue) { 965 SL_LOGE(ERROR_CONFIG_NULL_PARAM); 966 result = SL_RESULT_PARAMETER_INVALID; 967 968 } else if(strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_STREAM_TYPE) == 0) { 969 970 // stream type 971 if (KEY_STREAM_TYPE_PARAMSIZE > valueSize) { 972 SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW); 973 result = SL_RESULT_PARAMETER_INVALID; 974 } else { 975 result = audioPlayer_setStreamType(ap, *(SLuint32*)pConfigValue); 976 } 977 978 } else { 979 SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY); 980 result = SL_RESULT_PARAMETER_INVALID; 981 } 982 983 return result; 984} 985 986 987//----------------------------------------------------------------------------- 988SLresult android_audioPlayer_getConfig(CAudioPlayer* ap, const SLchar *configKey, 989 SLuint32* pValueSize, void *pConfigValue) { 990 991 SLresult result = SL_RESULT_SUCCESS; 992 993 if (NULL == ap) { 994 return SL_RESULT_INTERNAL_ERROR; 995 } else if (NULL == pValueSize) { 996 SL_LOGE(ERROR_CONFIG_NULL_PARAM); 997 result = SL_RESULT_PARAMETER_INVALID; 998 999 } else if(strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_STREAM_TYPE) == 0) { 1000 1001 // stream type 1002 if (KEY_STREAM_TYPE_PARAMSIZE > *pValueSize) { 1003 SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW); 1004 result = SL_RESULT_PARAMETER_INVALID; 1005 } else { 1006 *pValueSize = KEY_STREAM_TYPE_PARAMSIZE; 1007 if (NULL != pConfigValue) { 1008 result = audioPlayer_getStreamType(ap, (SLint32*)pConfigValue); 1009 } 1010 } 1011 1012 } else { 1013 SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY); 1014 result = SL_RESULT_PARAMETER_INVALID; 1015 } 1016 1017 return result; 1018} 1019 1020 1021//----------------------------------------------------------------------------- 1022SLresult android_audioPlayer_realize(CAudioPlayer *pAudioPlayer, SLboolean async) { 1023 1024 SLresult result = SL_RESULT_SUCCESS; 1025 SL_LOGV("Realize pAudioPlayer=%p", pAudioPlayer); 1026 1027 switch (pAudioPlayer->mAndroidObjType) { 1028 //----------------------------------- 1029 // AudioTrack 1030 case AUDIOTRACK_PULL: 1031 { 1032 // initialize platform-specific CAudioPlayer fields 1033 1034 SLDataLocator_BufferQueue *dl_bq = (SLDataLocator_BufferQueue *) 1035 pAudioPlayer->mDynamicSource.mDataSource; 1036 SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *) 1037 pAudioPlayer->mDynamicSource.mDataSource->pFormat; 1038 1039 uint32_t sampleRate = sles_to_android_sampleRate(df_pcm->samplesPerSec); 1040 1041 pAudioPlayer->mAudioTrack = new android::AudioTrack( 1042 pAudioPlayer->mStreamType, // streamType 1043 sampleRate, // sampleRate 1044 sles_to_android_sampleFormat(df_pcm->bitsPerSample), // format 1045 sles_to_android_channelMask(df_pcm->numChannels, df_pcm->channelMask),//channel mask 1046 0, // frameCount (here min) 1047 0, // flags 1048 audioTrack_callBack_pullFromBuffQueue, // callback 1049 (void *) pAudioPlayer, // user 1050 0 // FIXME find appropriate frame count // notificationFrame 1051 , pAudioPlayer->mSessionId 1052 ); 1053 android::status_t status = pAudioPlayer->mAudioTrack->initCheck(); 1054 if (status != android::NO_ERROR) { 1055 SL_LOGE("AudioTrack::initCheck status %u", status); 1056 result = SL_RESULT_CONTENT_UNSUPPORTED; 1057 } 1058 1059 // initialize platform-independent CAudioPlayer fields 1060 1061 pAudioPlayer->mNumChannels = df_pcm->numChannels; 1062 pAudioPlayer->mSampleRateMilliHz = df_pcm->samplesPerSec; // Note: bad field name in SL ES 1063 1064 pAudioPlayer->mAndroidObjState = ANDROID_READY; 1065 } break; 1066 //----------------------------------- 1067 // MediaPlayer 1068 case MEDIAPLAYER: { 1069 object_lock_exclusive(&pAudioPlayer->mObject); 1070 1071 pAudioPlayer->mAndroidObjState = ANDROID_UNINITIALIZED; 1072 pAudioPlayer->mNumChannels = 0; 1073 pAudioPlayer->mSampleRateMilliHz = 0; 1074 pAudioPlayer->mAudioTrack = NULL; 1075 1076 AudioPlayback_Parameters app; 1077 app.sessionId = pAudioPlayer->mSessionId; 1078 app.streamType = pAudioPlayer->mStreamType; 1079 app.trackcb = audioTrack_callBack_uri; 1080 app.trackcbUser = (void *) pAudioPlayer; 1081 1082 pAudioPlayer->mSfPlayer = new android::SfPlayer(&app); 1083 pAudioPlayer->mSfPlayer->setNotifListener(sfplayer_handlePrefetchEvent, 1084 (void*)pAudioPlayer /*notifUSer*/); 1085 pAudioPlayer->mSfPlayer->armLooper(); 1086 1087 object_unlock_exclusive(&pAudioPlayer->mObject); 1088 1089 switch (pAudioPlayer->mDataSource.mLocator.mLocatorType) { 1090 case SL_DATALOCATOR_URI: 1091 pAudioPlayer->mSfPlayer->setDataSource( 1092 (const char*)pAudioPlayer->mDataSource.mLocator.mURI.URI); 1093 break; 1094 case SL_DATALOCATOR_ANDROIDFD: { 1095 int64_t offset = (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.offset; 1096 pAudioPlayer->mSfPlayer->setDataSource( 1097 (int)pAudioPlayer->mDataSource.mLocator.mFD.fd, 1098 offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ? 1099 (int64_t)SFPLAYER_FD_FIND_FILE_SIZE : offset, 1100 (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.length); 1101 } break; 1102 default: 1103 SL_LOGE(ERROR_PLAYERREALIZE_UNKNOWN_DATASOURCE_LOCATOR); 1104 break; 1105 } 1106 1107 } break; 1108 //----------------------------------- 1109 // StreamPlayer 1110 case STREAM_SOURCE: { 1111 object_lock_exclusive(&pAudioPlayer->mObject); 1112 1113 android_StreamPlayer_realize_l(pAudioPlayer); 1114 1115 object_unlock_exclusive(&pAudioPlayer->mObject); 1116 } break; 1117 //----------------------------------- 1118 default: 1119 SL_LOGE("Unexpected object type %d", pAudioPlayer->mAndroidObjType); 1120 result = SL_RESULT_INTERNAL_ERROR; 1121 break; 1122 } 1123 1124 1125 // proceed with effect initialization 1126 // initialize EQ 1127 // FIXME use a table of effect descriptors when adding support for more effects 1128 if (memcmp(SL_IID_EQUALIZER, &pAudioPlayer->mEqualizer.mEqDescriptor.type, 1129 sizeof(effect_uuid_t)) == 0) { 1130 SL_LOGV("Need to initialize EQ for AudioPlayer=%p", pAudioPlayer); 1131 android_eq_init(pAudioPlayer->mSessionId, &pAudioPlayer->mEqualizer); 1132 } 1133 // initialize BassBoost 1134 if (memcmp(SL_IID_BASSBOOST, &pAudioPlayer->mBassBoost.mBassBoostDescriptor.type, 1135 sizeof(effect_uuid_t)) == 0) { 1136 SL_LOGV("Need to initialize BassBoost for AudioPlayer=%p", pAudioPlayer); 1137 android_bb_init(pAudioPlayer->mSessionId, &pAudioPlayer->mBassBoost); 1138 } 1139 // initialize Virtualizer 1140 if (memcmp(SL_IID_VIRTUALIZER, &pAudioPlayer->mVirtualizer.mVirtualizerDescriptor.type, 1141 sizeof(effect_uuid_t)) == 0) { 1142 SL_LOGV("Need to initialize Virtualizer for AudioPlayer=%p", pAudioPlayer); 1143 android_virt_init(pAudioPlayer->mSessionId, &pAudioPlayer->mVirtualizer); 1144 } 1145 1146 // initialize EffectSend 1147 // FIXME initialize EffectSend 1148 1149 return result; 1150} 1151 1152 1153//----------------------------------------------------------------------------- 1154SLresult android_audioPlayer_destroy(CAudioPlayer *pAudioPlayer) { 1155 SLresult result = SL_RESULT_SUCCESS; 1156 SL_LOGV("android_audioPlayer_destroy(%p)", pAudioPlayer); 1157 switch (pAudioPlayer->mAndroidObjType) { 1158 //----------------------------------- 1159 // AudioTrack 1160 case AUDIOTRACK_PULL: 1161 // We own the audio track for PCM buffer queue players 1162 if (pAudioPlayer->mAudioTrack != NULL) { 1163 pAudioPlayer->mAudioTrack->stop(); 1164 delete pAudioPlayer->mAudioTrack; 1165 pAudioPlayer->mAudioTrack = NULL; 1166 } 1167 break; 1168 //----------------------------------- 1169 // MediaPlayer 1170 case MEDIAPLAYER: 1171 // We don't own this audio track, SfPlayer does 1172 pAudioPlayer->mAudioTrack = NULL; 1173 // FIXME might no longer be needed since we call explicit destructor 1174 if (pAudioPlayer->mSfPlayer != 0) { 1175 pAudioPlayer->mSfPlayer.clear(); 1176 } 1177 break; 1178 //----------------------------------- 1179 // StreamPlayer 1180 case STREAM_SOURCE: 1181 android_StreamPlayer_destroy(pAudioPlayer); 1182 break; 1183 //----------------------------------- 1184 default: 1185 SL_LOGE("Unexpected object type %d", pAudioPlayer->mAndroidObjType); 1186 result = SL_RESULT_INTERNAL_ERROR; 1187 break; 1188 } 1189 1190 // FIXME might not be needed 1191 pAudioPlayer->mAndroidObjType = INVALID_TYPE; 1192 1193 // explicit destructor 1194 pAudioPlayer->mSfPlayer.~sp(); 1195 pAudioPlayer->mAuxEffect.~sp(); 1196 1197 if (pAudioPlayer->mpLock != NULL) { 1198 delete pAudioPlayer->mpLock; 1199 pAudioPlayer->mpLock = NULL; 1200 } 1201 1202 return result; 1203} 1204 1205 1206//----------------------------------------------------------------------------- 1207SLresult android_audioPlayer_setPlayRate(CAudioPlayer *ap, SLpermille rate, bool lockAP) { 1208 SLresult result = SL_RESULT_SUCCESS; 1209 uint32_t contentRate = 0; 1210 switch(ap->mAndroidObjType) { 1211 case AUDIOTRACK_PULL: 1212 case MEDIAPLAYER: { 1213 // get the content sample rate 1214 if (lockAP) { object_lock_shared(&ap->mObject); } 1215 uint32_t contentRate = sles_to_android_sampleRate(ap->mSampleRateMilliHz); 1216 if (lockAP) { object_unlock_shared(&ap->mObject); } 1217 // apply the SL ES playback rate on the AudioTrack as a factor of its content sample rate 1218 if (ap->mAudioTrack != NULL) { 1219 ap->mAudioTrack->setSampleRate(contentRate * (rate/1000.0f)); 1220 } 1221 } 1222 break; 1223 1224 default: 1225 SL_LOGE("Unexpected object type %d", ap->mAndroidObjType); 1226 result = SL_RESULT_INTERNAL_ERROR; 1227 break; 1228 } 1229 return result; 1230} 1231 1232 1233//----------------------------------------------------------------------------- 1234// called with no lock held 1235SLresult android_audioPlayer_setPlaybackRateBehavior(CAudioPlayer *ap, 1236 SLuint32 constraints) { 1237 SLresult result = SL_RESULT_SUCCESS; 1238 switch(ap->mAndroidObjType) { 1239 case AUDIOTRACK_PULL: 1240 case MEDIAPLAYER: 1241 if (constraints != (constraints & SL_RATEPROP_NOPITCHCORAUDIO)) { 1242 result = SL_RESULT_FEATURE_UNSUPPORTED; 1243 } 1244 break; 1245 default: 1246 SL_LOGE("Unexpected object type %d", ap->mAndroidObjType); 1247 result = SL_RESULT_INTERNAL_ERROR; 1248 break; 1249 } 1250 return result; 1251} 1252 1253 1254//----------------------------------------------------------------------------- 1255// called with no lock held 1256SLresult android_audioPlayer_getCapabilitiesOfRate(CAudioPlayer *ap, 1257 SLuint32 *pCapabilities) { 1258 switch(ap->mAndroidObjType) { 1259 case AUDIOTRACK_PULL: 1260 case MEDIAPLAYER: 1261 *pCapabilities = SL_RATEPROP_NOPITCHCORAUDIO; 1262 break; 1263 default: 1264 *pCapabilities = 0; 1265 break; 1266 } 1267 return SL_RESULT_SUCCESS; 1268} 1269 1270 1271//----------------------------------------------------------------------------- 1272void android_audioPlayer_setPlayState(CAudioPlayer *ap, bool lockAP) { 1273 1274 if (lockAP) { object_lock_shared(&ap->mObject); } 1275 SLuint32 playState = ap->mPlay.mState; 1276 AndroidObject_state objState = ap->mAndroidObjState; 1277 if (lockAP) { object_unlock_shared(&ap->mObject); } 1278 1279 switch(ap->mAndroidObjType) { 1280 case AUDIOTRACK_PULL: 1281 switch (playState) { 1282 case SL_PLAYSTATE_STOPPED: 1283 SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_STOPPED"); 1284 if (NULL != ap->mAudioTrack) { 1285 ap->mAudioTrack->stop(); 1286 } 1287 break; 1288 case SL_PLAYSTATE_PAUSED: 1289 SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PAUSED"); 1290 if (NULL != ap->mAudioTrack) { 1291 ap->mAudioTrack->pause(); 1292 } 1293 break; 1294 case SL_PLAYSTATE_PLAYING: 1295 SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PLAYING"); 1296 if (NULL != ap->mAudioTrack) { 1297 ap->mAudioTrack->start(); 1298 } 1299 break; 1300 default: 1301 // checked by caller, should not happen 1302 break; 1303 } 1304 break; 1305 1306 case MEDIAPLAYER: 1307 switch (playState) { 1308 case SL_PLAYSTATE_STOPPED: { 1309 SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_STOPPED"); 1310 if (ap->mSfPlayer != 0) { 1311 ap->mSfPlayer->stop(); 1312 } 1313 } break; 1314 case SL_PLAYSTATE_PAUSED: { 1315 SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PAUSED"); 1316 switch(objState) { 1317 case(ANDROID_UNINITIALIZED): 1318 sfplayer_prepare(ap, lockAP); 1319 break; 1320 case(ANDROID_PREPARING): 1321 break; 1322 case(ANDROID_READY): 1323 if (ap->mSfPlayer != 0) { 1324 ap->mSfPlayer->pause(); 1325 } 1326 break; 1327 default: 1328 break; 1329 } 1330 } break; 1331 case SL_PLAYSTATE_PLAYING: { 1332 SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PLAYING"); 1333 switch(objState) { 1334 case(ANDROID_UNINITIALIZED): 1335 sfplayer_prepare(ap, lockAP); 1336 // fall through 1337 case(ANDROID_PREPARING): 1338 case(ANDROID_READY): 1339 if (ap->mSfPlayer != 0) { 1340 ap->mSfPlayer->play(); 1341 } 1342 break; 1343 default: 1344 break; 1345 } 1346 } break; 1347 1348 default: 1349 // checked by caller, should not happen 1350 break; 1351 } 1352 break; 1353 1354 case STREAM_SOURCE: 1355 android_StreamPlayer_setPlayState(ap, playState, objState); 1356 break; 1357 1358 default: 1359 break; 1360 } 1361} 1362 1363 1364//----------------------------------------------------------------------------- 1365void android_audioPlayer_useEventMask(CAudioPlayer *ap) { 1366 IPlay *pPlayItf = &ap->mPlay; 1367 SLuint32 eventFlags = pPlayItf->mEventFlags; 1368 /*switch(ap->mAndroidObjType) { 1369 case AUDIOTRACK_PULL:*/ 1370 1371 if (NULL == ap->mAudioTrack) { 1372 return; 1373 } 1374 1375 if (eventFlags & SL_PLAYEVENT_HEADATMARKER) { 1376 ap->mAudioTrack->setMarkerPosition((uint32_t)((((int64_t)pPlayItf->mMarkerPosition 1377 * sles_to_android_sampleRate(ap->mSampleRateMilliHz)))/1000)); 1378 } else { 1379 // clear marker 1380 ap->mAudioTrack->setMarkerPosition(0); 1381 } 1382 1383 if (eventFlags & SL_PLAYEVENT_HEADATNEWPOS) { 1384 ap->mAudioTrack->setPositionUpdatePeriod( 1385 (uint32_t)((((int64_t)pPlayItf->mPositionUpdatePeriod 1386 * sles_to_android_sampleRate(ap->mSampleRateMilliHz)))/1000)); 1387 } else { 1388 // clear periodic update 1389 ap->mAudioTrack->setPositionUpdatePeriod(0); 1390 } 1391 1392 if (eventFlags & SL_PLAYEVENT_HEADATEND) { 1393 // nothing to do for SL_PLAYEVENT_HEADATEND, callback event will be checked against mask 1394 } 1395 1396 if (eventFlags & SL_PLAYEVENT_HEADMOVING) { 1397 // FIXME support SL_PLAYEVENT_HEADMOVING 1398 SL_LOGD("[ FIXME: IPlay_SetCallbackEventsMask(SL_PLAYEVENT_HEADMOVING) on an " 1399 "SL_OBJECTID_AUDIOPLAYER to be implemented ]"); 1400 } 1401 if (eventFlags & SL_PLAYEVENT_HEADSTALLED) { 1402 // nothing to do for SL_PLAYEVENT_HEADSTALLED, callback event will be checked against mask 1403 } 1404 1405} 1406 1407 1408//----------------------------------------------------------------------------- 1409SLresult android_audioPlayer_getDuration(IPlay *pPlayItf, SLmillisecond *pDurMsec) { 1410 CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis; 1411 switch(ap->mAndroidObjType) { 1412 case AUDIOTRACK_PULL: 1413 *pDurMsec = SL_TIME_UNKNOWN; 1414 // FIXME if the data source is not a buffer queue, and the audio data is saved in 1415 // shared memory with the mixer process, the duration is the size of the buffer 1416 SL_LOGD("FIXME: android_audioPlayer_getDuration() verify if duration can be retrieved"); 1417 break; 1418#ifndef USE_BACKPORT 1419 case MEDIAPLAYER: { 1420 int64_t durationUsec = SL_TIME_UNKNOWN; 1421 if (ap->mSfPlayer != 0) { 1422 durationUsec = ap->mSfPlayer->getDurationUsec(); 1423 *pDurMsec = durationUsec == -1 ? SL_TIME_UNKNOWN : durationUsec / 1000; 1424 } 1425 } break; 1426#endif 1427 default: 1428 break; 1429 } 1430 return SL_RESULT_SUCCESS; 1431} 1432 1433 1434//----------------------------------------------------------------------------- 1435void android_audioPlayer_getPosition(IPlay *pPlayItf, SLmillisecond *pPosMsec) { 1436 CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis; 1437 switch(ap->mAndroidObjType) { 1438 case AUDIOTRACK_PULL: 1439 if ((ap->mSampleRateMilliHz == 0) || (NULL == ap->mAudioTrack)) { 1440 *pPosMsec = 0; 1441 } else { 1442 uint32_t positionInFrames; 1443 ap->mAudioTrack->getPosition(&positionInFrames); 1444 *pPosMsec = ((int64_t)positionInFrames * 1000) / 1445 sles_to_android_sampleRate(ap->mSampleRateMilliHz); 1446 } 1447 break; 1448 case MEDIAPLAYER: 1449 if (ap->mSfPlayer != 0) { 1450 *pPosMsec = ap->mSfPlayer->getPositionMsec(); 1451 } else { 1452 *pPosMsec = 0; 1453 } 1454 break; 1455 default: 1456 break; 1457 } 1458} 1459 1460 1461//----------------------------------------------------------------------------- 1462void android_audioPlayer_seek(CAudioPlayer *ap, SLmillisecond posMsec) { 1463 1464 switch(ap->mAndroidObjType) { 1465 case AUDIOTRACK_PULL: 1466 break; 1467#ifndef USE_BACKPORT 1468 case MEDIAPLAYER: 1469 if (ap->mSfPlayer != 0) { 1470 ap->mSfPlayer->seek(posMsec); 1471 } 1472 break; 1473#endif 1474 default: 1475 break; 1476 } 1477} 1478 1479 1480//----------------------------------------------------------------------------- 1481void android_audioPlayer_loop(CAudioPlayer *ap, SLboolean loopEnable) { 1482 1483 if ((MEDIAPLAYER == ap->mAndroidObjType) && (ap->mSfPlayer != 0)) { 1484 ap->mSfPlayer->loop((bool)loopEnable); 1485 } 1486} 1487 1488 1489//----------------------------------------------------------------------------- 1490/* 1491 * Mutes or unmutes the Android media framework object associated with the CAudioPlayer that carries 1492 * the IVolume interface. 1493 * Pre-condition: 1494 * if ap->mMute is SL_BOOLEAN_FALSE, a call to this function was preceded by a call 1495 * to android_audioPlayer_volumeUpdate() 1496 */ 1497static void android_audioPlayer_setMute(CAudioPlayer* ap) { 1498 android::AudioTrack *t = NULL; 1499 switch(ap->mAndroidObjType) { 1500 case AUDIOTRACK_PULL: 1501 case MEDIAPLAYER: 1502 t = ap->mAudioTrack; 1503 break; 1504 default: 1505 break; 1506 } 1507 // when unmuting: volume levels have already been updated in IVolume_SetMute 1508 if (NULL != t) { 1509 t->mute(ap->mMute); 1510 } 1511} 1512 1513 1514//----------------------------------------------------------------------------- 1515SLresult android_audioPlayer_volumeUpdate(CAudioPlayer* ap) { 1516 android_audioPlayer_updateStereoVolume(ap); 1517 android_audioPlayer_setMute(ap); 1518 return SL_RESULT_SUCCESS; 1519} 1520 1521 1522//----------------------------------------------------------------------------- 1523void android_audioPlayer_bufferQueue_onRefilled(CAudioPlayer *ap) { 1524 // the AudioTrack associated with the AudioPlayer receiving audio from a PCM buffer 1525 // queue was stopped when the queue become empty, we restart as soon as a new buffer 1526 // has been enqueued since we're in playing state 1527 if (NULL != ap->mAudioTrack) { 1528 ap->mAudioTrack->start(); 1529 } 1530 1531 // when the queue became empty, an underflow on the prefetch status itf was sent. Now the queue 1532 // has received new data, signal it has sufficient data 1533 if (IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) { 1534 audioPlayer_dispatch_prefetchStatus_lockPrefetch(ap, SL_PREFETCHSTATUS_SUFFICIENTDATA, 1535 true); 1536 } 1537} 1538 1539 1540//----------------------------------------------------------------------------- 1541/* 1542 * BufferQueue::Clear 1543 */ 1544SLresult android_audioPlayer_bufferQueue_onClear(CAudioPlayer *ap) { 1545 SLresult result = SL_RESULT_SUCCESS; 1546 1547 switch (ap->mAndroidObjType) { 1548 //----------------------------------- 1549 // AudioTrack 1550 case AUDIOTRACK_PULL: 1551 if (NULL != ap->mAudioTrack) { 1552 ap->mAudioTrack->flush(); 1553 } 1554 break; 1555 default: 1556 result = SL_RESULT_INTERNAL_ERROR; 1557 break; 1558 } 1559 1560 return result; 1561} 1562 1563 1564//----------------------------------------------------------------------------- 1565void android_audioPlayer_androidBufferQueue_registerCallback_l(CAudioPlayer *ap) { 1566 if (ap->mAndroidObjType == STREAM_SOURCE) { 1567 android_StreamPlayer_registerCallback_l(ap); 1568 } 1569} 1570 1571//----------------------------------------------------------------------------- 1572void android_audioPlayer_androidBufferQueue_clear_l(CAudioPlayer *ap) { 1573 if (ap->mAndroidObjType == STREAM_SOURCE) { 1574 android_StreamPlayer_clear_l(ap); 1575 } 1576} 1577 1578void android_audioPlayer_androidBufferQueue_enqueue_l(CAudioPlayer *ap, 1579 SLuint32 bufferId, SLuint32 length, SLAbufferQueueEvent event, void *pData) { 1580 if (ap->mAndroidObjType == STREAM_SOURCE) { 1581 android_StreamPlayer_enqueue_l(ap, bufferId, length, event, pData); 1582 } 1583} 1584 1585