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