AudioPlayer_to_android.cpp revision 833251ab9e5e59a6ea5ac325122cf3abdf7cd944
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 SLresult result = EnqueueAsyncCallback_ppi(ap, playCallback, &ap->mPlay.mItf, playContext, 445 SL_PLAYEVENT_HEADATEND); 446 if (SL_RESULT_SUCCESS != result) { 447 LOGW("Callback %p(%p, %p, SL_PLAYEVENT_HEADATEND) dropped", playCallback, 448 &ap->mPlay.mItf, playContext); 449 } 450 } 451 452} 453 454 455//----------------------------------------------------------------------------- 456/** 457 * pre-condition: AudioPlayer has SLPrefetchStatusItf initialized 458 * post-condition: 459 * - ap->mPrefetchStatus.mStatus == status 460 * - the prefetch status callback, if any, has been notified if a change occurred 461 * 462 */ 463void audioPlayer_dispatch_prefetchStatus_lockPrefetch(CAudioPlayer *ap, SLuint32 status, 464 bool needToLock) { 465 slPrefetchCallback prefetchCallback = NULL; 466 void * prefetchContext = NULL; 467 468 if (needToLock) { 469 interface_lock_exclusive(&ap->mPrefetchStatus); 470 } 471 // status change? 472 if (ap->mPrefetchStatus.mStatus != status) { 473 ap->mPrefetchStatus.mStatus = status; 474 // callback or no callback? 475 if (ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_STATUSCHANGE) { 476 prefetchCallback = ap->mPrefetchStatus.mCallback; 477 prefetchContext = ap->mPrefetchStatus.mContext; 478 } 479 } 480 if (needToLock) { 481 interface_unlock_exclusive(&ap->mPrefetchStatus); 482 } 483 484 // callback with no lock held 485 if (NULL != prefetchCallback) { 486 (*prefetchCallback)(&ap->mPrefetchStatus.mItf, prefetchContext, status); 487 } 488} 489 490 491//----------------------------------------------------------------------------- 492SLresult audioPlayer_setStreamType(CAudioPlayer* ap, SLint32 type) { 493 SLresult result = SL_RESULT_SUCCESS; 494 SL_LOGV("type %d", type); 495 496 int newStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE; 497 switch(type) { 498 case SL_ANDROID_STREAM_VOICE: 499 newStreamType = AUDIO_STREAM_VOICE_CALL; 500 break; 501 case SL_ANDROID_STREAM_SYSTEM: 502 newStreamType = AUDIO_STREAM_SYSTEM; 503 break; 504 case SL_ANDROID_STREAM_RING: 505 newStreamType = AUDIO_STREAM_RING; 506 break; 507 case SL_ANDROID_STREAM_MEDIA: 508 newStreamType = AUDIO_STREAM_MUSIC; 509 break; 510 case SL_ANDROID_STREAM_ALARM: 511 newStreamType = AUDIO_STREAM_ALARM; 512 break; 513 case SL_ANDROID_STREAM_NOTIFICATION: 514 newStreamType = AUDIO_STREAM_NOTIFICATION; 515 break; 516 default: 517 SL_LOGE(ERROR_PLAYERSTREAMTYPE_SET_UNKNOWN_TYPE); 518 result = SL_RESULT_PARAMETER_INVALID; 519 break; 520 } 521 522 // stream type needs to be set before the object is realized 523 // (ap->mAudioTrack is supposed to be NULL until then) 524 if (SL_OBJECT_STATE_UNREALIZED != ap->mObject.mState) { 525 SL_LOGE(ERROR_PLAYERSTREAMTYPE_REALIZED); 526 result = SL_RESULT_PRECONDITIONS_VIOLATED; 527 } else { 528 ap->mStreamType = newStreamType; 529 } 530 531 return result; 532} 533 534 535//----------------------------------------------------------------------------- 536SLresult audioPlayer_getStreamType(CAudioPlayer* ap, SLint32 *pType) { 537 SLresult result = SL_RESULT_SUCCESS; 538 539 switch(ap->mStreamType) { 540 case AUDIO_STREAM_VOICE_CALL: 541 *pType = SL_ANDROID_STREAM_VOICE; 542 break; 543 case AUDIO_STREAM_SYSTEM: 544 *pType = SL_ANDROID_STREAM_SYSTEM; 545 break; 546 case AUDIO_STREAM_RING: 547 *pType = SL_ANDROID_STREAM_RING; 548 break; 549 case AUDIO_STREAM_DEFAULT: 550 case AUDIO_STREAM_MUSIC: 551 *pType = SL_ANDROID_STREAM_MEDIA; 552 break; 553 case AUDIO_STREAM_ALARM: 554 *pType = SL_ANDROID_STREAM_ALARM; 555 break; 556 case AUDIO_STREAM_NOTIFICATION: 557 *pType = SL_ANDROID_STREAM_NOTIFICATION; 558 break; 559 default: 560 result = SL_RESULT_INTERNAL_ERROR; 561 *pType = SL_ANDROID_STREAM_MEDIA; 562 break; 563 } 564 565 return result; 566} 567 568 569//----------------------------------------------------------------------------- 570void audioPlayer_auxEffectUpdate(CAudioPlayer* ap) { 571 if ((ap->mAudioTrack != 0) && (ap->mAuxEffect != 0)) { 572 android_fxSend_attach(ap, true, ap->mAuxEffect, ap->mVolume.mLevel + ap->mAuxSendLevel); 573 } 574} 575 576 577//----------------------------------------------------------------------------- 578void audioPlayer_setInvalid(CAudioPlayer* ap) { 579 ap->mAndroidObjType = INVALID_TYPE; 580 ap->mpLock = NULL; 581} 582 583 584//----------------------------------------------------------------------------- 585/* 586 * returns true if the given data sink is supported by AudioPlayer that doesn't 587 * play to an OutputMix object, false otherwise 588 * 589 * pre-condition: the locator of the audio sink is not SL_DATALOCATOR_OUTPUTMIX 590 */ 591bool audioPlayer_isSupportedNonOutputMixSink(const SLDataSink* pAudioSink) { 592 bool result = true; 593 const SLuint32 sinkLocatorType = *(SLuint32 *)pAudioSink->pLocator; 594 const SLuint32 sinkFormatType = *(SLuint32 *)pAudioSink->pFormat; 595 596 switch (sinkLocatorType) { 597 598 case SL_DATALOCATOR_BUFFERQUEUE: 599 case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE: 600 if (SL_DATAFORMAT_PCM != sinkFormatType) { 601 SL_LOGE("Unsupported sink format 0x%x, expected SL_DATAFORMAT_PCM", 602 (unsigned)sinkFormatType); 603 result = false; 604 } 605 // it's no use checking the PCM format fields because additional characteristics 606 // such as the number of channels, or sample size are unknown to the player at this stage 607 break; 608 609 default: 610 SL_LOGE("Unsupported sink locator type 0x%x", (unsigned)sinkLocatorType); 611 result = false; 612 break; 613 } 614 615 return result; 616} 617 618 619//----------------------------------------------------------------------------- 620/* 621 * returns the Android object type if the locator type combinations for the source and sinks 622 * are supported by this implementation, INVALID_TYPE otherwise 623 */ 624AndroidObjectType audioPlayer_getAndroidObjectTypeForSourceSink(CAudioPlayer *ap) { 625 626 const SLDataSource *pAudioSrc = &ap->mDataSource.u.mSource; 627 const SLDataSink *pAudioSnk = &ap->mDataSink.u.mSink; 628 const SLuint32 sourceLocatorType = *(SLuint32 *)pAudioSrc->pLocator; 629 const SLuint32 sinkLocatorType = *(SLuint32 *)pAudioSnk->pLocator; 630 AndroidObjectType type = INVALID_TYPE; 631 632 //-------------------------------------- 633 // Sink / source matching check: 634 // the following source / sink combinations are supported 635 // SL_DATALOCATOR_BUFFERQUEUE / SL_DATALOCATOR_OUTPUTMIX 636 // SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE / SL_DATALOCATOR_OUTPUTMIX 637 // SL_DATALOCATOR_URI / SL_DATALOCATOR_OUTPUTMIX 638 // SL_DATALOCATOR_ANDROIDFD / SL_DATALOCATOR_OUTPUTMIX 639 // SL_DATALOCATOR_ANDROIDBUFFERQUEUE / SL_DATALOCATOR_OUTPUTMIX 640 // SL_DATALOCATOR_ANDROIDBUFFERQUEUE / SL_DATALOCATOR_BUFFERQUEUE 641 // SL_DATALOCATOR_URI / SL_DATALOCATOR_BUFFERQUEUE 642 // SL_DATALOCATOR_ANDROIDFD / SL_DATALOCATOR_BUFFERQUEUE 643 // SL_DATALOCATOR_URI / SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE 644 // SL_DATALOCATOR_ANDROIDFD / SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE 645 switch (sinkLocatorType) { 646 647 case SL_DATALOCATOR_OUTPUTMIX: { 648 switch (sourceLocatorType) { 649 650 // Buffer Queue to AudioTrack 651 case SL_DATALOCATOR_BUFFERQUEUE: 652 case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE: 653 type = AUDIOPLAYER_FROM_PCM_BUFFERQUEUE; 654 break; 655 656 // URI or FD to MediaPlayer 657 case SL_DATALOCATOR_URI: 658 case SL_DATALOCATOR_ANDROIDFD: 659 type = AUDIOPLAYER_FROM_URIFD; 660 break; 661 662 // Android BufferQueue to MediaPlayer (shared memory streaming) 663 case SL_DATALOCATOR_ANDROIDBUFFERQUEUE: 664 type = AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE; 665 break; 666 667 default: 668 SL_LOGE("Source data locator 0x%x not supported with SL_DATALOCATOR_OUTPUTMIX sink", 669 (unsigned)sourceLocatorType); 670 break; 671 } 672 } 673 break; 674 675 case SL_DATALOCATOR_BUFFERQUEUE: 676 case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE: 677 switch (sourceLocatorType) { 678 679 // URI or FD decoded to PCM in a buffer queue 680 case SL_DATALOCATOR_URI: 681 case SL_DATALOCATOR_ANDROIDFD: 682 type = AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE; 683 break; 684 685 // AAC ADTS Android buffer queue decoded to PCM in a buffer queue 686 case SL_DATALOCATOR_ANDROIDBUFFERQUEUE: 687 type = AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE; 688 break; 689 690 default: 691 SL_LOGE("Source data locator 0x%x not supported with SL_DATALOCATOR_BUFFERQUEUE sink", 692 (unsigned)sourceLocatorType); 693 break; 694 } 695 break; 696 697 default: 698 SL_LOGE("Sink data locator 0x%x not supported", (unsigned)sinkLocatorType); 699 break; 700 } 701 702 return type; 703} 704 705 706//----------------------------------------------------------------------------- 707/* 708 * Callback associated with an SfPlayer of an SL ES AudioPlayer that gets its data 709 * from a URI or FD, for prepare, prefetch, and play events 710 */ 711static void sfplayer_handlePrefetchEvent(int event, int data1, int data2, void* user) { 712 713 // FIXME see similar code and comment in player_handleMediaPlayerEventNotifications 714 715 if (NULL == user) { 716 return; 717 } 718 719 CAudioPlayer *ap = (CAudioPlayer *)user; 720 if (!android::CallbackProtector::enterCbIfOk(ap->mCallbackProtector)) { 721 // it is not safe to enter the callback (the track is about to go away) 722 return; 723 } 724 union { 725 char c[sizeof(int)]; 726 int i; 727 } u; 728 u.i = event; 729 SL_LOGV("sfplayer_handlePrefetchEvent(event='%c%c%c%c' (%d), data1=%d, data2=%d, user=%p) from " 730 "SfAudioPlayer", u.c[3], u.c[2], u.c[1], u.c[0], event, data1, data2, user); 731 switch(event) { 732 733 case android::GenericPlayer::kEventPrepared: { 734 735 if (PLAYER_SUCCESS != data1) { 736 object_lock_exclusive(&ap->mObject); 737 738 // already initialized at object creation, and can only prepare once so never reset 739 assert(ap->mAudioTrack == 0); 740 assert(ap->mNumChannels == UNKNOWN_NUMCHANNELS); 741 assert(ap->mSampleRateMilliHz == UNKNOWN_SAMPLERATE); 742 assert(ap->mAndroidObjState == ANDROID_PREPARING); 743 ap->mAndroidObjState = ANDROID_READY; 744 745 object_unlock_exclusive(&ap->mObject); 746 747 // SfPlayer prepare() failed prefetching, there is no event in SLPrefetchStatus to 748 // indicate a prefetch error, so we signal it by sending simulataneously two events: 749 // - SL_PREFETCHEVENT_FILLLEVELCHANGE with a level of 0 750 // - SL_PREFETCHEVENT_STATUSCHANGE with a status of SL_PREFETCHSTATUS_UNDERFLOW 751 SL_LOGE(ERROR_PLAYER_PREFETCH_d, data1); 752 if (!IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) { 753 break; 754 } 755 756 slPrefetchCallback callback = NULL; 757 void* callbackPContext = NULL; 758 759 interface_lock_exclusive(&ap->mPrefetchStatus); 760 ap->mPrefetchStatus.mLevel = 0; 761 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW; 762 if ((ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_FILLLEVELCHANGE) 763 && (ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_STATUSCHANGE)) { 764 callback = ap->mPrefetchStatus.mCallback; 765 callbackPContext = ap->mPrefetchStatus.mContext; 766 } 767 interface_unlock_exclusive(&ap->mPrefetchStatus); 768 769 // callback with no lock held 770 if (NULL != callback) { 771 (*callback)(&ap->mPrefetchStatus.mItf, callbackPContext, 772 SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE); 773 } 774 775 776 } else { 777 778 object_lock_exclusive(&ap->mObject); 779 780 if (AUDIOPLAYER_FROM_URIFD == ap->mAndroidObjType) { 781 //************************************** 782 // FIXME move under GenericMediaPlayer 783#if 0 784 ap->mAudioTrack = ap->mSfPlayer->getAudioTrack(); 785 ap->mNumChannels = ap->mSfPlayer->getNumChannels(); 786 ap->mSampleRateMilliHz = 787 android_to_sles_sampleRate(ap->mSfPlayer->getSampleRateHz()); 788 ap->mSfPlayer->startPrefetch_async(); 789 // update the new track with the current settings 790 audioPlayer_auxEffectUpdate(ap); 791 android_audioPlayer_useEventMask(ap); 792 android_audioPlayer_volumeUpdate(ap); 793 android_audioPlayer_setPlayRate(ap, ap->mPlaybackRate.mRate, false /*lockAP*/); 794#endif 795 } else if (AUDIOPLAYER_FROM_PCM_BUFFERQUEUE == ap->mAndroidObjType) { 796 if (ap->mAPlayer != 0) { 797 ((android::AudioToCbRenderer*)ap->mAPlayer.get())->startPrefetch_async(); 798 } 799 } else if (AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE == ap->mAndroidObjType) { 800 SL_LOGD("Received SfPlayer::kEventPrepared from AVPlayer for CAudioPlayer %p", ap); 801 } 802 803 ap->mAndroidObjState = ANDROID_READY; 804 805 object_unlock_exclusive(&ap->mObject); 806 } 807 808 } 809 break; 810 811 case android::GenericPlayer::kEventPrefetchFillLevelUpdate : { 812 if (!IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) { 813 break; 814 } 815 slPrefetchCallback callback = NULL; 816 void* callbackPContext = NULL; 817 818 // SLPrefetchStatusItf callback or no callback? 819 interface_lock_exclusive(&ap->mPrefetchStatus); 820 if (ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_FILLLEVELCHANGE) { 821 callback = ap->mPrefetchStatus.mCallback; 822 callbackPContext = ap->mPrefetchStatus.mContext; 823 } 824 ap->mPrefetchStatus.mLevel = (SLpermille)data1; 825 interface_unlock_exclusive(&ap->mPrefetchStatus); 826 827 // callback with no lock held 828 if (NULL != callback) { 829 (*callback)(&ap->mPrefetchStatus.mItf, callbackPContext, 830 SL_PREFETCHEVENT_FILLLEVELCHANGE); 831 } 832 } 833 break; 834 835 case android::GenericPlayer::kEventPrefetchStatusChange: { 836 if (!IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) { 837 break; 838 } 839 slPrefetchCallback callback = NULL; 840 void* callbackPContext = NULL; 841 842 // SLPrefetchStatusItf callback or no callback? 843 object_lock_exclusive(&ap->mObject); 844 if (ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_STATUSCHANGE) { 845 callback = ap->mPrefetchStatus.mCallback; 846 callbackPContext = ap->mPrefetchStatus.mContext; 847 } 848 if (data1 >= android::kStatusIntermediate) { 849 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_SUFFICIENTDATA; 850 ap->mAndroidObjState = ANDROID_READY; 851 } else if (data1 < android::kStatusIntermediate) { 852 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW; 853 } 854 object_unlock_exclusive(&ap->mObject); 855 856 // callback with no lock held 857 if (NULL != callback) { 858 (*callback)(&ap->mPrefetchStatus.mItf, callbackPContext, SL_PREFETCHEVENT_STATUSCHANGE); 859 } 860 } 861 break; 862 863 case android::GenericPlayer::kEventEndOfStream: { 864 audioPlayer_dispatch_headAtEnd_lockPlay(ap, true /*set state to paused?*/, true); 865 if ((ap->mAudioTrack != 0) && (!ap->mSeek.mLoopEnabled)) { 866 ap->mAudioTrack->stop(); 867 } 868 } 869 break; 870 871 case android::GenericPlayer::kEventChannelCount: { 872 object_lock_exclusive(&ap->mObject); 873 if (UNKNOWN_NUMCHANNELS == ap->mNumChannels && UNKNOWN_NUMCHANNELS != data1) { 874 ap->mNumChannels = data1; 875 android_audioPlayer_volumeUpdate(ap); 876 } 877 object_unlock_exclusive(&ap->mObject); 878 } 879 break; 880 881 case android::GenericPlayer::kEventPlay: { 882 slPlayCallback callback = NULL; 883 void* callbackPContext = NULL; 884 885 interface_lock_shared(&ap->mPlay); 886 callback = ap->mPlay.mCallback; 887 callbackPContext = ap->mPlay.mContext; 888 interface_unlock_shared(&ap->mPlay); 889 890 if (NULL != callback) { 891 SLuint32 event = (SLuint32) data1; // SL_PLAYEVENT_HEAD* 892#ifndef USE_ASYNCHRONOUS_PLAY_CALLBACK 893 // synchronous callback requires a synchronous GetPosition implementation 894 (*callback)(&ap->mPlay.mItf, callbackPContext, event); 895#else 896 // asynchronous callback works with any GetPosition implementation 897 SLresult result = EnqueueAsyncCallback_ppi(ap, callback, &ap->mPlay.mItf, 898 callbackPContext, event); 899 if (SL_RESULT_SUCCESS != result) { 900 LOGW("Callback %p(%p, %p, 0x%x) dropped", callback, 901 &ap->mPlay.mItf, callbackPContext, event); 902 } 903#endif 904 } 905 } 906 break; 907 908 default: 909 break; 910 } 911 912 ap->mCallbackProtector->exitCb(); 913} 914 915 916//----------------------------------------------------------------------------- 917SLresult android_audioPlayer_checkSourceSink(CAudioPlayer *pAudioPlayer) 918{ 919 // verify that the locator types for the source / sink combination is supported 920 pAudioPlayer->mAndroidObjType = audioPlayer_getAndroidObjectTypeForSourceSink(pAudioPlayer); 921 if (INVALID_TYPE == pAudioPlayer->mAndroidObjType) { 922 return SL_RESULT_PARAMETER_INVALID; 923 } 924 925 const SLDataSource *pAudioSrc = &pAudioPlayer->mDataSource.u.mSource; 926 const SLDataSink *pAudioSnk = &pAudioPlayer->mDataSink.u.mSink; 927 928 // format check: 929 const SLuint32 sourceLocatorType = *(SLuint32 *)pAudioSrc->pLocator; 930 const SLuint32 sinkLocatorType = *(SLuint32 *)pAudioSnk->pLocator; 931 const SLuint32 sourceFormatType = *(SLuint32 *)pAudioSrc->pFormat; 932 const SLuint32 sinkFormatType = *(SLuint32 *)pAudioSnk->pFormat; 933 934 switch (sourceLocatorType) { 935 //------------------ 936 // Buffer Queues 937 case SL_DATALOCATOR_BUFFERQUEUE: 938 case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE: 939 { 940 SLDataLocator_BufferQueue *dl_bq = (SLDataLocator_BufferQueue *) pAudioSrc->pLocator; 941 942 // Buffer format 943 switch (sourceFormatType) { 944 // currently only PCM buffer queues are supported, 945 case SL_DATAFORMAT_PCM: { 946 SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *) pAudioSrc->pFormat; 947 switch (df_pcm->numChannels) { 948 case 1: 949 case 2: 950 break; 951 default: 952 // this should have already been rejected by checkDataFormat 953 SL_LOGE("Cannot create audio player: unsupported " \ 954 "PCM data source with %u channels", (unsigned) df_pcm->numChannels); 955 return SL_RESULT_CONTENT_UNSUPPORTED; 956 } 957 switch (df_pcm->samplesPerSec) { 958 case SL_SAMPLINGRATE_8: 959 case SL_SAMPLINGRATE_11_025: 960 case SL_SAMPLINGRATE_12: 961 case SL_SAMPLINGRATE_16: 962 case SL_SAMPLINGRATE_22_05: 963 case SL_SAMPLINGRATE_24: 964 case SL_SAMPLINGRATE_32: 965 case SL_SAMPLINGRATE_44_1: 966 case SL_SAMPLINGRATE_48: 967 break; 968 case SL_SAMPLINGRATE_64: 969 case SL_SAMPLINGRATE_88_2: 970 case SL_SAMPLINGRATE_96: 971 case SL_SAMPLINGRATE_192: 972 default: 973 SL_LOGE("Cannot create audio player: unsupported sample rate %u milliHz", 974 (unsigned) df_pcm->samplesPerSec); 975 return SL_RESULT_CONTENT_UNSUPPORTED; 976 } 977 switch (df_pcm->bitsPerSample) { 978 case SL_PCMSAMPLEFORMAT_FIXED_8: 979 case SL_PCMSAMPLEFORMAT_FIXED_16: 980 break; 981 // others 982 default: 983 // this should have already been rejected by checkDataFormat 984 SL_LOGE("Cannot create audio player: unsupported sample bit depth %u", 985 (SLuint32)df_pcm->bitsPerSample); 986 return SL_RESULT_CONTENT_UNSUPPORTED; 987 } 988 switch (df_pcm->containerSize) { 989 case 8: 990 case 16: 991 break; 992 // others 993 default: 994 SL_LOGE("Cannot create audio player: unsupported container size %u", 995 (unsigned) df_pcm->containerSize); 996 return SL_RESULT_CONTENT_UNSUPPORTED; 997 } 998 // df_pcm->channelMask: the earlier platform-independent check and the 999 // upcoming check by sles_to_android_channelMaskOut are sufficient 1000 switch (df_pcm->endianness) { 1001 case SL_BYTEORDER_LITTLEENDIAN: 1002 break; 1003 case SL_BYTEORDER_BIGENDIAN: 1004 SL_LOGE("Cannot create audio player: unsupported big-endian byte order"); 1005 return SL_RESULT_CONTENT_UNSUPPORTED; 1006 // native is proposed but not yet in spec 1007 default: 1008 SL_LOGE("Cannot create audio player: unsupported byte order %u", 1009 (unsigned) df_pcm->endianness); 1010 return SL_RESULT_CONTENT_UNSUPPORTED; 1011 } 1012 } //case SL_DATAFORMAT_PCM 1013 break; 1014 case SL_DATAFORMAT_MIME: 1015 case XA_DATAFORMAT_RAWIMAGE: 1016 SL_LOGE("Cannot create audio player with buffer queue data source " 1017 "without SL_DATAFORMAT_PCM format"); 1018 return SL_RESULT_CONTENT_UNSUPPORTED; 1019 default: 1020 // invalid data format is detected earlier 1021 assert(false); 1022 return SL_RESULT_INTERNAL_ERROR; 1023 } // switch (sourceFormatType) 1024 } // case SL_DATALOCATOR_BUFFERQUEUE or SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE 1025 break; 1026 //------------------ 1027 // URI 1028 case SL_DATALOCATOR_URI: 1029 { 1030 SLDataLocator_URI *dl_uri = (SLDataLocator_URI *) pAudioSrc->pLocator; 1031 if (NULL == dl_uri->URI) { 1032 return SL_RESULT_PARAMETER_INVALID; 1033 } 1034 // URI format 1035 switch (sourceFormatType) { 1036 case SL_DATAFORMAT_MIME: 1037 break; 1038 case SL_DATAFORMAT_PCM: 1039 case XA_DATAFORMAT_RAWIMAGE: 1040 SL_LOGE("Cannot create audio player with SL_DATALOCATOR_URI data source without " 1041 "SL_DATAFORMAT_MIME format"); 1042 return SL_RESULT_CONTENT_UNSUPPORTED; 1043 } // switch (sourceFormatType) 1044 // decoding format check 1045 if ((sinkLocatorType != SL_DATALOCATOR_OUTPUTMIX) && 1046 !audioPlayer_isSupportedNonOutputMixSink(pAudioSnk)) { 1047 return SL_RESULT_CONTENT_UNSUPPORTED; 1048 } 1049 } // case SL_DATALOCATOR_URI 1050 break; 1051 //------------------ 1052 // File Descriptor 1053 case SL_DATALOCATOR_ANDROIDFD: 1054 { 1055 // fd is already non null 1056 switch (sourceFormatType) { 1057 case SL_DATAFORMAT_MIME: 1058 break; 1059 case SL_DATAFORMAT_PCM: 1060 // FIXME implement 1061 SL_LOGD("[ FIXME implement PCM FD data sources ]"); 1062 break; 1063 case XA_DATAFORMAT_RAWIMAGE: 1064 SL_LOGE("Cannot create audio player with SL_DATALOCATOR_ANDROIDFD data source " 1065 "without SL_DATAFORMAT_MIME or SL_DATAFORMAT_PCM format"); 1066 return SL_RESULT_CONTENT_UNSUPPORTED; 1067 default: 1068 // invalid data format is detected earlier 1069 assert(false); 1070 return SL_RESULT_INTERNAL_ERROR; 1071 } // switch (sourceFormatType) 1072 if ((sinkLocatorType != SL_DATALOCATOR_OUTPUTMIX) && 1073 !audioPlayer_isSupportedNonOutputMixSink(pAudioSnk)) { 1074 return SL_RESULT_CONTENT_UNSUPPORTED; 1075 } 1076 } // case SL_DATALOCATOR_ANDROIDFD 1077 break; 1078 //------------------ 1079 // Stream 1080 case SL_DATALOCATOR_ANDROIDBUFFERQUEUE: 1081 { 1082 switch (sourceFormatType) { 1083 case SL_DATAFORMAT_MIME: 1084 { 1085 SLDataFormat_MIME *df_mime = (SLDataFormat_MIME *) pAudioSrc->pFormat; 1086 if (NULL == df_mime) { 1087 SL_LOGE("MIME type null invalid"); 1088 return SL_RESULT_CONTENT_UNSUPPORTED; 1089 } 1090 SL_LOGD("source MIME is %s", (char*)df_mime->mimeType); 1091 switch(df_mime->containerType) { 1092 case SL_CONTAINERTYPE_MPEG_TS: 1093 if (strcasecmp((char*)df_mime->mimeType, ANDROID_MIME_MP2TS)) { 1094 SL_LOGE("Invalid MIME (%s) for container SL_CONTAINERTYPE_MPEG_TS, expects %s", 1095 (char*)df_mime->mimeType, ANDROID_MIME_MP2TS); 1096 return SL_RESULT_CONTENT_UNSUPPORTED; 1097 } 1098 break; 1099 case SL_CONTAINERTYPE_RAW: 1100 case SL_CONTAINERTYPE_AAC: 1101 if (strcasecmp((char*)df_mime->mimeType, ANDROID_MIME_AACADTS) && 1102 strcasecmp((char*)df_mime->mimeType, 1103 ANDROID_MIME_AACADTS_ANDROID_FRAMEWORK)) { 1104 SL_LOGE("Invalid MIME (%s) for container type %d, expects %s", 1105 (char*)df_mime->mimeType, df_mime->containerType, 1106 ANDROID_MIME_AACADTS); 1107 return SL_RESULT_CONTENT_UNSUPPORTED; 1108 } 1109 break; 1110 default: 1111 SL_LOGE("Cannot create player with SL_DATALOCATOR_ANDROIDBUFFERQUEUE data source " 1112 "that is not fed MPEG-2 TS data or AAC ADTS data"); 1113 return SL_RESULT_CONTENT_UNSUPPORTED; 1114 } 1115 } 1116 break; 1117 default: 1118 SL_LOGE("Cannot create player with SL_DATALOCATOR_ANDROIDBUFFERQUEUE data source " 1119 "without SL_DATAFORMAT_MIME format"); 1120 return SL_RESULT_CONTENT_UNSUPPORTED; 1121 } 1122 } 1123 break; // case SL_DATALOCATOR_ANDROIDBUFFERQUEUE 1124 //------------------ 1125 // Address 1126 case SL_DATALOCATOR_ADDRESS: 1127 case SL_DATALOCATOR_IODEVICE: 1128 case SL_DATALOCATOR_OUTPUTMIX: 1129 case XA_DATALOCATOR_NATIVEDISPLAY: 1130 case SL_DATALOCATOR_MIDIBUFFERQUEUE: 1131 SL_LOGE("Cannot create audio player with data locator type 0x%x", 1132 (unsigned) sourceLocatorType); 1133 return SL_RESULT_CONTENT_UNSUPPORTED; 1134 default: 1135 SL_LOGE("Cannot create audio player with invalid data locator type 0x%x", 1136 (unsigned) sourceLocatorType); 1137 return SL_RESULT_PARAMETER_INVALID; 1138 }// switch (locatorType) 1139 1140 return SL_RESULT_SUCCESS; 1141} 1142 1143 1144 1145//----------------------------------------------------------------------------- 1146static void audioTrack_callBack_uri(int event, void* user, void *info) { 1147 // EVENT_MORE_DATA needs to be handled with priority over the other events 1148 // because it will be called the most often during playback 1149 1150 if (event == android::AudioTrack::EVENT_MORE_DATA) { 1151 //SL_LOGV("received event EVENT_MORE_DATA from AudioTrack"); 1152 // set size to 0 to signal we're not using the callback to write more data 1153 android::AudioTrack::Buffer* pBuff = (android::AudioTrack::Buffer*)info; 1154 pBuff->size = 0; 1155 } else if (NULL != user) { 1156 CAudioPlayer *ap = (CAudioPlayer *)user; 1157 if (!android::CallbackProtector::enterCbIfOk(ap->mCallbackProtector)) { 1158 // it is not safe to enter the callback (the track is about to go away) 1159 return; 1160 } 1161 switch (event) { 1162 case android::AudioTrack::EVENT_MARKER : 1163 audioTrack_handleMarker_lockPlay(ap); 1164 break; 1165 case android::AudioTrack::EVENT_NEW_POS : 1166 audioTrack_handleNewPos_lockPlay(ap); 1167 break; 1168 case android::AudioTrack::EVENT_UNDERRUN : 1169 audioTrack_handleUnderrun_lockPlay(ap); 1170 break; 1171 case android::AudioTrack::EVENT_BUFFER_END : 1172 case android::AudioTrack::EVENT_LOOP_END : 1173 break; 1174 default: 1175 SL_LOGE("Encountered unknown AudioTrack event %d for CAudioPlayer %p", event, 1176 ap); 1177 break; 1178 } 1179 ap->mCallbackProtector->exitCb(); 1180 } 1181} 1182 1183//----------------------------------------------------------------------------- 1184// Callback associated with an AudioTrack of an SL ES AudioPlayer that gets its data 1185// from a buffer queue. This will not be called once the AudioTrack has been destroyed. 1186static void audioTrack_callBack_pullFromBuffQueue(int event, void* user, void *info) { 1187 CAudioPlayer *ap = (CAudioPlayer *)user; 1188 1189 if (!android::CallbackProtector::enterCbIfOk(ap->mCallbackProtector)) { 1190 // it is not safe to enter the callback (the track is about to go away) 1191 return; 1192 } 1193 1194 void * callbackPContext = NULL; 1195 switch(event) { 1196 1197 case android::AudioTrack::EVENT_MORE_DATA: { 1198 //SL_LOGV("received event EVENT_MORE_DATA from AudioTrack TID=%d", gettid()); 1199 slBufferQueueCallback callback = NULL; 1200 slPrefetchCallback prefetchCallback = NULL; 1201 void *prefetchContext = NULL; 1202 SLuint32 prefetchEvents = SL_PREFETCHEVENT_NONE; 1203 android::AudioTrack::Buffer* pBuff = (android::AudioTrack::Buffer*)info; 1204 1205 // retrieve data from the buffer queue 1206 interface_lock_exclusive(&ap->mBufferQueue); 1207 1208 if (ap->mBufferQueue.mState.count != 0) { 1209 //SL_LOGV("nbBuffers in queue = %u",ap->mBufferQueue.mState.count); 1210 assert(ap->mBufferQueue.mFront != ap->mBufferQueue.mRear); 1211 1212 BufferHeader *oldFront = ap->mBufferQueue.mFront; 1213 BufferHeader *newFront = &oldFront[1]; 1214 1215 // declared as void * because this code supports both 8-bit and 16-bit PCM data 1216 void *pSrc = (char *)oldFront->mBuffer + ap->mBufferQueue.mSizeConsumed; 1217 if (ap->mBufferQueue.mSizeConsumed + pBuff->size < oldFront->mSize) { 1218 // can't consume the whole or rest of the buffer in one shot 1219 ap->mBufferQueue.mSizeConsumed += pBuff->size; 1220 // leave pBuff->size untouched 1221 // consume data 1222 // FIXME can we avoid holding the lock during the copy? 1223 memcpy (pBuff->raw, pSrc, pBuff->size); 1224 } else { 1225 // finish consuming the buffer or consume the buffer in one shot 1226 pBuff->size = oldFront->mSize - ap->mBufferQueue.mSizeConsumed; 1227 ap->mBufferQueue.mSizeConsumed = 0; 1228 1229 if (newFront == 1230 &ap->mBufferQueue.mArray 1231 [ap->mBufferQueue.mNumBuffers + 1]) 1232 { 1233 newFront = ap->mBufferQueue.mArray; 1234 } 1235 ap->mBufferQueue.mFront = newFront; 1236 1237 ap->mBufferQueue.mState.count--; 1238 ap->mBufferQueue.mState.playIndex++; 1239 1240 // consume data 1241 // FIXME can we avoid holding the lock during the copy? 1242 memcpy (pBuff->raw, pSrc, pBuff->size); 1243 1244 // data has been consumed, and the buffer queue state has been updated 1245 // we will notify the client if applicable 1246 callback = ap->mBufferQueue.mCallback; 1247 // save callback data 1248 callbackPContext = ap->mBufferQueue.mContext; 1249 } 1250 } else { // empty queue 1251 // signal no data available 1252 pBuff->size = 0; 1253 1254 // signal we're at the end of the content, but don't pause (see note in function) 1255 audioPlayer_dispatch_headAtEnd_lockPlay(ap, false /*set state to paused?*/, false); 1256 1257 // signal underflow to prefetch status itf 1258 if (IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) { 1259 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW; 1260 ap->mPrefetchStatus.mLevel = 0; 1261 // callback or no callback? 1262 prefetchEvents = ap->mPrefetchStatus.mCallbackEventsMask & 1263 (SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE); 1264 if (SL_PREFETCHEVENT_NONE != prefetchEvents) { 1265 prefetchCallback = ap->mPrefetchStatus.mCallback; 1266 prefetchContext = ap->mPrefetchStatus.mContext; 1267 } 1268 } 1269 1270 // stop the track so it restarts playing faster when new data is enqueued 1271 ap->mAudioTrack->stop(); 1272 } 1273 interface_unlock_exclusive(&ap->mBufferQueue); 1274 1275 // notify client 1276 if (NULL != prefetchCallback) { 1277 assert(SL_PREFETCHEVENT_NONE != prefetchEvents); 1278 // spec requires separate callbacks for each event 1279 if (prefetchEvents & SL_PREFETCHEVENT_STATUSCHANGE) { 1280 (*prefetchCallback)(&ap->mPrefetchStatus.mItf, prefetchContext, 1281 SL_PREFETCHEVENT_STATUSCHANGE); 1282 } 1283 if (prefetchEvents & SL_PREFETCHEVENT_FILLLEVELCHANGE) { 1284 (*prefetchCallback)(&ap->mPrefetchStatus.mItf, prefetchContext, 1285 SL_PREFETCHEVENT_FILLLEVELCHANGE); 1286 } 1287 } 1288 if (NULL != callback) { 1289 (*callback)(&ap->mBufferQueue.mItf, callbackPContext); 1290 } 1291 } 1292 break; 1293 1294 case android::AudioTrack::EVENT_MARKER: 1295 //SL_LOGI("received event EVENT_MARKER from AudioTrack"); 1296 audioTrack_handleMarker_lockPlay(ap); 1297 break; 1298 1299 case android::AudioTrack::EVENT_NEW_POS: 1300 //SL_LOGI("received event EVENT_NEW_POS from AudioTrack"); 1301 audioTrack_handleNewPos_lockPlay(ap); 1302 break; 1303 1304 case android::AudioTrack::EVENT_UNDERRUN: 1305 //SL_LOGI("received event EVENT_UNDERRUN from AudioTrack"); 1306 audioTrack_handleUnderrun_lockPlay(ap); 1307 break; 1308 1309 default: 1310 // FIXME where does the notification of SL_PLAYEVENT_HEADMOVING fit? 1311 SL_LOGE("Encountered unknown AudioTrack event %d for CAudioPlayer %p", event, 1312 (CAudioPlayer *)user); 1313 break; 1314 } 1315 1316 ap->mCallbackProtector->exitCb(); 1317} 1318 1319 1320//----------------------------------------------------------------------------- 1321SLresult android_audioPlayer_create(CAudioPlayer *pAudioPlayer) { 1322 1323 SLresult result = SL_RESULT_SUCCESS; 1324 // pAudioPlayer->mAndroidObjType has been set in audioPlayer_getAndroidObjectTypeForSourceSink() 1325 if (INVALID_TYPE == pAudioPlayer->mAndroidObjType) { 1326 audioPlayer_setInvalid(pAudioPlayer); 1327 result = SL_RESULT_PARAMETER_INVALID; 1328 } else { 1329 1330 // These initializations are in the same order as the field declarations in classes.h 1331 1332 // FIXME Consolidate initializations (many of these already in IEngine_CreateAudioPlayer) 1333 pAudioPlayer->mpLock = new android::Mutex(); 1334 // mAndroidObjType: see above comment 1335 pAudioPlayer->mAndroidObjState = ANDROID_UNINITIALIZED; 1336 pAudioPlayer->mSessionId = android::AudioSystem::newAudioSessionId(); 1337 pAudioPlayer->mStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE; 1338 1339 // mAudioTrack 1340 pAudioPlayer->mCallbackProtector = new android::CallbackProtector(); 1341 // mAPLayer 1342 // mAuxEffect 1343 1344 pAudioPlayer->mAuxSendLevel = 0; 1345 pAudioPlayer->mAmplFromDirectLevel = 1.0f; // matches initial mDirectLevel value 1346 pAudioPlayer->mDeferredStart = false; 1347 1348 // Already initialized in IEngine_CreateAudioPlayer, to be consolidated 1349 pAudioPlayer->mDirectLevel = 0; // no attenuation 1350 1351 // This section re-initializes interface-specific fields that 1352 // can be set or used regardless of whether the interface is 1353 // exposed on the AudioPlayer or not 1354 1355 // Only AudioTrack supports a non-trivial playback rate 1356 switch (pAudioPlayer->mAndroidObjType) { 1357 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 1358 pAudioPlayer->mPlaybackRate.mMinRate = AUDIOTRACK_MIN_PLAYBACKRATE_PERMILLE; 1359 pAudioPlayer->mPlaybackRate.mMaxRate = AUDIOTRACK_MAX_PLAYBACKRATE_PERMILLE; 1360 break; 1361 default: 1362 // use the default range 1363 break; 1364 } 1365 1366 } 1367 1368 return result; 1369} 1370 1371 1372//----------------------------------------------------------------------------- 1373SLresult android_audioPlayer_setConfig(CAudioPlayer *ap, const SLchar *configKey, 1374 const void *pConfigValue, SLuint32 valueSize) { 1375 1376 SLresult result; 1377 1378 assert(NULL != ap && NULL != configKey && NULL != pConfigValue); 1379 if(strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_STREAM_TYPE) == 0) { 1380 1381 // stream type 1382 if (KEY_STREAM_TYPE_PARAMSIZE > valueSize) { 1383 SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW); 1384 result = SL_RESULT_BUFFER_INSUFFICIENT; 1385 } else { 1386 result = audioPlayer_setStreamType(ap, *(SLuint32*)pConfigValue); 1387 } 1388 1389 } else { 1390 SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY); 1391 result = SL_RESULT_PARAMETER_INVALID; 1392 } 1393 1394 return result; 1395} 1396 1397 1398//----------------------------------------------------------------------------- 1399SLresult android_audioPlayer_getConfig(CAudioPlayer* ap, const SLchar *configKey, 1400 SLuint32* pValueSize, void *pConfigValue) { 1401 1402 SLresult result; 1403 1404 assert(NULL != ap && NULL != configKey && NULL != pValueSize); 1405 if(strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_STREAM_TYPE) == 0) { 1406 1407 // stream type 1408 if (NULL == pConfigValue) { 1409 result = SL_RESULT_SUCCESS; 1410 } else if (KEY_STREAM_TYPE_PARAMSIZE > *pValueSize) { 1411 SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW); 1412 result = SL_RESULT_BUFFER_INSUFFICIENT; 1413 } else { 1414 result = audioPlayer_getStreamType(ap, (SLint32*)pConfigValue); 1415 } 1416 *pValueSize = KEY_STREAM_TYPE_PARAMSIZE; 1417 1418 } else { 1419 SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY); 1420 result = SL_RESULT_PARAMETER_INVALID; 1421 } 1422 1423 return result; 1424} 1425 1426 1427//----------------------------------------------------------------------------- 1428SLresult android_audioPlayer_realize(CAudioPlayer *pAudioPlayer, SLboolean async) { 1429 1430 SLresult result = SL_RESULT_SUCCESS; 1431 SL_LOGV("Realize pAudioPlayer=%p", pAudioPlayer); 1432 1433 switch (pAudioPlayer->mAndroidObjType) { 1434 //----------------------------------- 1435 // AudioTrack 1436 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 1437 { 1438 // initialize platform-specific CAudioPlayer fields 1439 1440 SLDataLocator_BufferQueue *dl_bq = (SLDataLocator_BufferQueue *) 1441 pAudioPlayer->mDynamicSource.mDataSource; 1442 SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *) 1443 pAudioPlayer->mDynamicSource.mDataSource->pFormat; 1444 1445 uint32_t sampleRate = sles_to_android_sampleRate(df_pcm->samplesPerSec); 1446 1447 pAudioPlayer->mAudioTrack = new android::AudioTrackProxy(new android::AudioTrack( 1448 pAudioPlayer->mStreamType, // streamType 1449 sampleRate, // sampleRate 1450 sles_to_android_sampleFormat(df_pcm->bitsPerSample), // format 1451 sles_to_android_channelMaskOut(df_pcm->numChannels, df_pcm->channelMask), 1452 //channel mask 1453 0, // frameCount (here min) 1454 0, // flags 1455 audioTrack_callBack_pullFromBuffQueue, // callback 1456 (void *) pAudioPlayer, // user 1457 0 // FIXME find appropriate frame count // notificationFrame 1458 , pAudioPlayer->mSessionId 1459 )); 1460 android::status_t status = pAudioPlayer->mAudioTrack->initCheck(); 1461 if (status != android::NO_ERROR) { 1462 SL_LOGE("AudioTrack::initCheck status %u", status); 1463 result = SL_RESULT_CONTENT_UNSUPPORTED; 1464 pAudioPlayer->mAudioTrack.clear(); 1465 return result; 1466 } 1467 1468 // initialize platform-independent CAudioPlayer fields 1469 1470 pAudioPlayer->mNumChannels = df_pcm->numChannels; 1471 pAudioPlayer->mSampleRateMilliHz = df_pcm->samplesPerSec; // Note: bad field name in SL ES 1472 1473 pAudioPlayer->mAndroidObjState = ANDROID_READY; 1474 } 1475 break; 1476 //----------------------------------- 1477 // MediaPlayer 1478 case AUDIOPLAYER_FROM_URIFD: { 1479 object_lock_exclusive(&pAudioPlayer->mObject); 1480 1481 assert(pAudioPlayer->mAndroidObjState == ANDROID_UNINITIALIZED); 1482 assert(pAudioPlayer->mNumChannels == UNKNOWN_NUMCHANNELS); 1483 assert(pAudioPlayer->mSampleRateMilliHz == UNKNOWN_SAMPLERATE); 1484 assert(pAudioPlayer->mAudioTrack == 0); 1485 1486 AudioPlayback_Parameters app; 1487 app.sessionId = pAudioPlayer->mSessionId; 1488 app.streamType = pAudioPlayer->mStreamType; 1489 app.trackcb = audioTrack_callBack_uri; 1490 app.trackcbUser = (void *) pAudioPlayer; 1491 1492 pAudioPlayer->mAPlayer = new android::LocAVPlayer(&app, false /*hasVideo*/); 1493 pAudioPlayer->mAPlayer->init(sfplayer_handlePrefetchEvent, 1494 (void*)pAudioPlayer /*notifUSer*/); 1495 1496 object_unlock_exclusive(&pAudioPlayer->mObject); 1497 1498 switch (pAudioPlayer->mDataSource.mLocator.mLocatorType) { 1499 case SL_DATALOCATOR_URI: { 1500 // The legacy implementation ran Stagefright within the application process, and 1501 // so allowed local pathnames specified by URI that were openable by 1502 // the application but were not openable by mediaserver. 1503 // The current implementation runs Stagefright (mostly) within mediaserver, 1504 // which runs as a different UID and likely a different current working directory. 1505 // For backwards compatibility with any applications which may have relied on the 1506 // previous behavior, we convert an openable file URI into an FD. 1507 // Note that unlike SL_DATALOCATOR_ANDROIDFD, this FD is owned by us 1508 // and so we close it as soon as we've passed it (via Binder dup) to mediaserver. 1509 const char *uri = (const char *)pAudioPlayer->mDataSource.mLocator.mURI.URI; 1510 if (!isDistantProtocol(uri)) { 1511 // don't touch the original uri, we may need it later 1512 const char *pathname = uri; 1513 // skip over an optional leading file:// prefix 1514 if (!strncasecmp(pathname, "file://", 7)) { 1515 pathname += 7; 1516 } 1517 // attempt to open it as a file using the application's credentials 1518 int fd = ::open(pathname, O_RDONLY); 1519 if (fd >= 0) { 1520 // if open is successful, then check to see if it's a regular file 1521 struct stat statbuf; 1522 if (!::fstat(fd, &statbuf) && S_ISREG(statbuf.st_mode)) { 1523 // treat similarly to an FD data locator, but 1524 // let setDataSource take responsibility for closing fd 1525 pAudioPlayer->mAPlayer->setDataSource(fd, 0, statbuf.st_size, true); 1526 break; 1527 } 1528 // we were able to open it, but it's not a file, so let mediaserver try 1529 (void) ::close(fd); 1530 } 1531 } 1532 // if either the URI didn't look like a file, or open failed, or not a file 1533 pAudioPlayer->mAPlayer->setDataSource(uri); 1534 } break; 1535 case SL_DATALOCATOR_ANDROIDFD: { 1536 int64_t offset = (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.offset; 1537 pAudioPlayer->mAPlayer->setDataSource( 1538 (int)pAudioPlayer->mDataSource.mLocator.mFD.fd, 1539 offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ? 1540 (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset, 1541 (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.length); 1542 } 1543 break; 1544 default: 1545 SL_LOGE(ERROR_PLAYERREALIZE_UNKNOWN_DATASOURCE_LOCATOR); 1546 break; 1547 } 1548 1549 } 1550 break; 1551 //----------------------------------- 1552 // StreamPlayer 1553 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: { 1554 object_lock_exclusive(&pAudioPlayer->mObject); 1555 1556 android_StreamPlayer_realize_l(pAudioPlayer, sfplayer_handlePrefetchEvent, 1557 (void*)pAudioPlayer); 1558 1559 object_unlock_exclusive(&pAudioPlayer->mObject); 1560 } 1561 break; 1562 //----------------------------------- 1563 // AudioToCbRenderer 1564 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: { 1565 object_lock_exclusive(&pAudioPlayer->mObject); 1566 1567 AudioPlayback_Parameters app; 1568 app.sessionId = pAudioPlayer->mSessionId; 1569 app.streamType = pAudioPlayer->mStreamType; 1570 1571 android::AudioToCbRenderer* decoder = new android::AudioToCbRenderer(&app); 1572 pAudioPlayer->mAPlayer = decoder; 1573 // configures the callback for the sink buffer queue 1574 decoder->setDataPushListener(adecoder_writeToBufferQueue, (void*)pAudioPlayer); 1575 // configures the callback for the notifications coming from the SF code 1576 decoder->init(sfplayer_handlePrefetchEvent, (void*)pAudioPlayer); 1577 1578 switch (pAudioPlayer->mDataSource.mLocator.mLocatorType) { 1579 case SL_DATALOCATOR_URI: 1580 decoder->setDataSource( 1581 (const char*)pAudioPlayer->mDataSource.mLocator.mURI.URI); 1582 break; 1583 case SL_DATALOCATOR_ANDROIDFD: { 1584 int64_t offset = (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.offset; 1585 decoder->setDataSource( 1586 (int)pAudioPlayer->mDataSource.mLocator.mFD.fd, 1587 offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ? 1588 (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset, 1589 (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.length); 1590 } 1591 break; 1592 default: 1593 SL_LOGE(ERROR_PLAYERREALIZE_UNKNOWN_DATASOURCE_LOCATOR); 1594 break; 1595 } 1596 1597 object_unlock_exclusive(&pAudioPlayer->mObject); 1598 } 1599 break; 1600 //----------------------------------- 1601 // AacBqToPcmCbRenderer 1602 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: { 1603 object_lock_exclusive(&pAudioPlayer->mObject); 1604 1605 AudioPlayback_Parameters app; 1606 app.sessionId = pAudioPlayer->mSessionId; 1607 app.streamType = pAudioPlayer->mStreamType; 1608 app.trackcb = NULL; 1609 app.trackcbUser = NULL; 1610 android::AacBqToPcmCbRenderer* bqtobq = new android::AacBqToPcmCbRenderer(&app); 1611 // configures the callback for the sink buffer queue 1612 bqtobq->setDataPushListener(adecoder_writeToBufferQueue, (void*)pAudioPlayer); 1613 pAudioPlayer->mAPlayer = bqtobq; 1614 // configures the callback for the notifications coming from the SF code, 1615 // but also implicitly configures the AndroidBufferQueue from which ADTS data is read 1616 pAudioPlayer->mAPlayer->init(sfplayer_handlePrefetchEvent, (void*)pAudioPlayer); 1617 1618 object_unlock_exclusive(&pAudioPlayer->mObject); 1619 } 1620 break; 1621 //----------------------------------- 1622 default: 1623 SL_LOGE(ERROR_PLAYERREALIZE_UNEXPECTED_OBJECT_TYPE_D, pAudioPlayer->mAndroidObjType); 1624 result = SL_RESULT_INTERNAL_ERROR; 1625 break; 1626 } 1627 1628 1629 // proceed with effect initialization 1630 // initialize EQ 1631 // FIXME use a table of effect descriptors when adding support for more effects 1632 if (memcmp(SL_IID_EQUALIZER, &pAudioPlayer->mEqualizer.mEqDescriptor.type, 1633 sizeof(effect_uuid_t)) == 0) { 1634 SL_LOGV("Need to initialize EQ for AudioPlayer=%p", pAudioPlayer); 1635 android_eq_init(pAudioPlayer->mSessionId, &pAudioPlayer->mEqualizer); 1636 } 1637 // initialize BassBoost 1638 if (memcmp(SL_IID_BASSBOOST, &pAudioPlayer->mBassBoost.mBassBoostDescriptor.type, 1639 sizeof(effect_uuid_t)) == 0) { 1640 SL_LOGV("Need to initialize BassBoost for AudioPlayer=%p", pAudioPlayer); 1641 android_bb_init(pAudioPlayer->mSessionId, &pAudioPlayer->mBassBoost); 1642 } 1643 // initialize Virtualizer 1644 if (memcmp(SL_IID_VIRTUALIZER, &pAudioPlayer->mVirtualizer.mVirtualizerDescriptor.type, 1645 sizeof(effect_uuid_t)) == 0) { 1646 SL_LOGV("Need to initialize Virtualizer for AudioPlayer=%p", pAudioPlayer); 1647 android_virt_init(pAudioPlayer->mSessionId, &pAudioPlayer->mVirtualizer); 1648 } 1649 1650 // initialize EffectSend 1651 // FIXME initialize EffectSend 1652 1653 return result; 1654} 1655 1656 1657//----------------------------------------------------------------------------- 1658/** 1659 * Called with a lock on AudioPlayer 1660 */ 1661SLresult android_audioPlayer_preDestroy(CAudioPlayer *pAudioPlayer) { 1662 SL_LOGD("android_audioPlayer_preDestroy(%p)", pAudioPlayer); 1663 SLresult result = SL_RESULT_SUCCESS; 1664 1665 if (pAudioPlayer->mAPlayer != 0) { 1666 pAudioPlayer->mAPlayer->preDestroy(); 1667 } 1668 SL_LOGD("android_audioPlayer_preDestroy(%p) after mAPlayer->preDestroy()", pAudioPlayer); 1669 1670 object_unlock_exclusive(&pAudioPlayer->mObject); 1671 if (pAudioPlayer->mCallbackProtector != 0) { 1672 pAudioPlayer->mCallbackProtector->requestCbExitAndWait(); 1673 } 1674 object_lock_exclusive(&pAudioPlayer->mObject); 1675 1676 return result; 1677} 1678 1679 1680//----------------------------------------------------------------------------- 1681SLresult android_audioPlayer_destroy(CAudioPlayer *pAudioPlayer) { 1682 SLresult result = SL_RESULT_SUCCESS; 1683 SL_LOGV("android_audioPlayer_destroy(%p)", pAudioPlayer); 1684 switch (pAudioPlayer->mAndroidObjType) { 1685 1686 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 1687 // We own the audio track for PCM buffer queue players 1688 if (pAudioPlayer->mAudioTrack != 0) { 1689 pAudioPlayer->mAudioTrack->stop(); 1690 // Note that there may still be another reference in post-unlock phase of SetPlayState 1691 pAudioPlayer->mAudioTrack.clear(); 1692 } 1693 break; 1694 1695 case AUDIOPLAYER_FROM_URIFD: // intended fall-through 1696 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 1697 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: // intended fall-through 1698 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: 1699 pAudioPlayer->mAPlayer.clear(); 1700 break; 1701 //----------------------------------- 1702 default: 1703 SL_LOGE(ERROR_PLAYERDESTROY_UNEXPECTED_OBJECT_TYPE_D, pAudioPlayer->mAndroidObjType); 1704 result = SL_RESULT_INTERNAL_ERROR; 1705 break; 1706 } 1707 1708 pAudioPlayer->mCallbackProtector.clear(); 1709 1710 // FIXME might not be needed 1711 pAudioPlayer->mAndroidObjType = INVALID_TYPE; 1712 1713 // explicit destructor 1714 pAudioPlayer->mAudioTrack.~sp(); 1715 // note that SetPlayState(PLAYING) may still hold a reference 1716 pAudioPlayer->mCallbackProtector.~sp(); 1717 pAudioPlayer->mAuxEffect.~sp(); 1718 pAudioPlayer->mAPlayer.~sp(); 1719 1720 if (pAudioPlayer->mpLock != NULL) { 1721 delete pAudioPlayer->mpLock; 1722 pAudioPlayer->mpLock = NULL; 1723 } 1724 1725 return result; 1726} 1727 1728 1729//----------------------------------------------------------------------------- 1730SLresult android_audioPlayer_setPlaybackRateAndConstraints(CAudioPlayer *ap, SLpermille rate, 1731 SLuint32 constraints) { 1732 SLresult result = SL_RESULT_SUCCESS; 1733 switch(ap->mAndroidObjType) { 1734 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: { 1735 // these asserts were already checked by the platform-independent layer 1736 assert((AUDIOTRACK_MIN_PLAYBACKRATE_PERMILLE <= rate) && 1737 (rate <= AUDIOTRACK_MAX_PLAYBACKRATE_PERMILLE)); 1738 assert(constraints & SL_RATEPROP_NOPITCHCORAUDIO); 1739 // get the content sample rate 1740 uint32_t contentRate = sles_to_android_sampleRate(ap->mSampleRateMilliHz); 1741 // apply the SL ES playback rate on the AudioTrack as a factor of its content sample rate 1742 if (ap->mAudioTrack != 0) { 1743 ap->mAudioTrack->setSampleRate(contentRate * (rate/1000.0f)); 1744 } 1745 } 1746 break; 1747 case AUDIOPLAYER_FROM_URIFD: 1748 assert(rate == 1000); 1749 assert(constraints & SL_RATEPROP_NOPITCHCORAUDIO); 1750 // that was easy 1751 break; 1752 1753 default: 1754 SL_LOGE("Unexpected object type %d", ap->mAndroidObjType); 1755 result = SL_RESULT_FEATURE_UNSUPPORTED; 1756 break; 1757 } 1758 return result; 1759} 1760 1761 1762//----------------------------------------------------------------------------- 1763// precondition 1764// called with no lock held 1765// ap != NULL 1766// pItemCount != NULL 1767SLresult android_audioPlayer_metadata_getItemCount(CAudioPlayer *ap, SLuint32 *pItemCount) { 1768 if (ap->mAPlayer == 0) { 1769 return SL_RESULT_PARAMETER_INVALID; 1770 } 1771 switch(ap->mAndroidObjType) { 1772 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 1773 { 1774 android::AudioSfDecoder* decoder = 1775 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get()); 1776 *pItemCount = decoder->getPcmFormatKeyCount(); 1777 } 1778 break; 1779 default: 1780 *pItemCount = 0; 1781 break; 1782 } 1783 return SL_RESULT_SUCCESS; 1784} 1785 1786 1787//----------------------------------------------------------------------------- 1788// precondition 1789// called with no lock held 1790// ap != NULL 1791// pKeySize != NULL 1792SLresult android_audioPlayer_metadata_getKeySize(CAudioPlayer *ap, 1793 SLuint32 index, SLuint32 *pKeySize) { 1794 if (ap->mAPlayer == 0) { 1795 return SL_RESULT_PARAMETER_INVALID; 1796 } 1797 SLresult res = SL_RESULT_SUCCESS; 1798 switch(ap->mAndroidObjType) { 1799 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 1800 { 1801 android::AudioSfDecoder* decoder = 1802 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get()); 1803 SLuint32 keyNameSize = 0; 1804 if (!decoder->getPcmFormatKeySize(index, &keyNameSize)) { 1805 res = SL_RESULT_PARAMETER_INVALID; 1806 } else { 1807 // *pKeySize is the size of the region used to store the key name AND 1808 // the information about the key (size, lang, encoding) 1809 *pKeySize = keyNameSize + sizeof(SLMetadataInfo); 1810 } 1811 } 1812 break; 1813 default: 1814 *pKeySize = 0; 1815 res = SL_RESULT_PARAMETER_INVALID; 1816 break; 1817 } 1818 return res; 1819} 1820 1821 1822//----------------------------------------------------------------------------- 1823// precondition 1824// called with no lock held 1825// ap != NULL 1826// pKey != NULL 1827SLresult android_audioPlayer_metadata_getKey(CAudioPlayer *ap, 1828 SLuint32 index, SLuint32 size, SLMetadataInfo *pKey) { 1829 if (ap->mAPlayer == 0) { 1830 return SL_RESULT_PARAMETER_INVALID; 1831 } 1832 SLresult res = SL_RESULT_SUCCESS; 1833 switch(ap->mAndroidObjType) { 1834 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 1835 { 1836 android::AudioSfDecoder* decoder = 1837 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get()); 1838 if ((size < sizeof(SLMetadataInfo) || 1839 (!decoder->getPcmFormatKeyName(index, size - sizeof(SLMetadataInfo), 1840 (char*)pKey->data)))) { 1841 res = SL_RESULT_PARAMETER_INVALID; 1842 } else { 1843 // successfully retrieved the key value, update the other fields 1844 pKey->encoding = SL_CHARACTERENCODING_UTF8; 1845 memcpy((char *) pKey->langCountry, "en", 3); 1846 pKey->size = strlen((char*)pKey->data) + 1; 1847 } 1848 } 1849 break; 1850 default: 1851 res = SL_RESULT_PARAMETER_INVALID; 1852 break; 1853 } 1854 return res; 1855} 1856 1857 1858//----------------------------------------------------------------------------- 1859// precondition 1860// called with no lock held 1861// ap != NULL 1862// pValueSize != NULL 1863SLresult android_audioPlayer_metadata_getValueSize(CAudioPlayer *ap, 1864 SLuint32 index, SLuint32 *pValueSize) { 1865 if (ap->mAPlayer == 0) { 1866 return SL_RESULT_PARAMETER_INVALID; 1867 } 1868 SLresult res = SL_RESULT_SUCCESS; 1869 switch(ap->mAndroidObjType) { 1870 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 1871 { 1872 android::AudioSfDecoder* decoder = 1873 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get()); 1874 SLuint32 valueSize = 0; 1875 if (!decoder->getPcmFormatValueSize(index, &valueSize)) { 1876 res = SL_RESULT_PARAMETER_INVALID; 1877 } else { 1878 // *pValueSize is the size of the region used to store the key value AND 1879 // the information about the value (size, lang, encoding) 1880 *pValueSize = valueSize + sizeof(SLMetadataInfo); 1881 } 1882 } 1883 break; 1884 default: 1885 *pValueSize = 0; 1886 res = SL_RESULT_PARAMETER_INVALID; 1887 break; 1888 } 1889 return res; 1890} 1891 1892 1893//----------------------------------------------------------------------------- 1894// precondition 1895// called with no lock held 1896// ap != NULL 1897// pValue != NULL 1898SLresult android_audioPlayer_metadata_getValue(CAudioPlayer *ap, 1899 SLuint32 index, SLuint32 size, SLMetadataInfo *pValue) { 1900 if (ap->mAPlayer == 0) { 1901 return SL_RESULT_PARAMETER_INVALID; 1902 } 1903 SLresult res = SL_RESULT_SUCCESS; 1904 switch(ap->mAndroidObjType) { 1905 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 1906 { 1907 android::AudioSfDecoder* decoder = 1908 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get()); 1909 pValue->encoding = SL_CHARACTERENCODING_BINARY; 1910 memcpy((char *) pValue->langCountry, "en", 3); // applicable here? 1911 SLuint32 valueSize = 0; 1912 if ((size < sizeof(SLMetadataInfo) 1913 || (!decoder->getPcmFormatValueSize(index, &valueSize)) 1914 || (!decoder->getPcmFormatKeyValue(index, size - sizeof(SLMetadataInfo), 1915 (SLuint32*)pValue->data)))) { 1916 res = SL_RESULT_PARAMETER_INVALID; 1917 } else { 1918 pValue->size = valueSize; 1919 } 1920 } 1921 break; 1922 default: 1923 res = SL_RESULT_PARAMETER_INVALID; 1924 break; 1925 } 1926 return res; 1927} 1928 1929//----------------------------------------------------------------------------- 1930// preconditions 1931// ap != NULL 1932// mutex is locked 1933// play state has changed 1934void android_audioPlayer_setPlayState(CAudioPlayer *ap) { 1935 1936 SLuint32 playState = ap->mPlay.mState; 1937 AndroidObjectState objState = ap->mAndroidObjState; 1938 1939 switch(ap->mAndroidObjType) { 1940 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 1941 switch (playState) { 1942 case SL_PLAYSTATE_STOPPED: 1943 SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_STOPPED"); 1944 if (ap->mAudioTrack != 0) { 1945 ap->mAudioTrack->stop(); 1946 } 1947 break; 1948 case SL_PLAYSTATE_PAUSED: 1949 SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PAUSED"); 1950 if (ap->mAudioTrack != 0) { 1951 ap->mAudioTrack->pause(); 1952 } 1953 break; 1954 case SL_PLAYSTATE_PLAYING: 1955 SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PLAYING"); 1956 if (ap->mAudioTrack != 0) { 1957 // instead of ap->mAudioTrack->start(); 1958 ap->mDeferredStart = true; 1959 } 1960 break; 1961 default: 1962 // checked by caller, should not happen 1963 break; 1964 } 1965 break; 1966 1967 case AUDIOPLAYER_FROM_URIFD: // intended fall-through 1968 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 1969 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: // intended fall-through 1970 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: 1971 // FIXME report and use the return code to the lock mechanism, which is where play state 1972 // changes are updated (see object_unlock_exclusive_attributes()) 1973 aplayer_setPlayState(ap->mAPlayer, playState, &(ap->mAndroidObjState)); 1974 break; 1975 default: 1976 SL_LOGE(ERROR_PLAYERSETPLAYSTATE_UNEXPECTED_OBJECT_TYPE_D, ap->mAndroidObjType); 1977 break; 1978 } 1979} 1980 1981 1982//----------------------------------------------------------------------------- 1983// call when either player event flags, marker position, or position update period changes 1984void android_audioPlayer_usePlayEventMask(CAudioPlayer *ap) { 1985 IPlay *pPlayItf = &ap->mPlay; 1986 SLuint32 eventFlags = pPlayItf->mEventFlags; 1987 /*switch(ap->mAndroidObjType) { 1988 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:*/ 1989 1990 if (ap->mAPlayer != 0) { 1991 assert(ap->mAudioTrack == 0); 1992 ap->mAPlayer->setPlayEvents((int32_t) eventFlags, (int32_t) pPlayItf->mMarkerPosition, 1993 (int32_t) pPlayItf->mPositionUpdatePeriod); 1994 return; 1995 } 1996 1997 if (ap->mAudioTrack == 0) { 1998 return; 1999 } 2000 2001 if (eventFlags & SL_PLAYEVENT_HEADATMARKER) { 2002 ap->mAudioTrack->setMarkerPosition((uint32_t)((((int64_t)pPlayItf->mMarkerPosition 2003 * sles_to_android_sampleRate(ap->mSampleRateMilliHz)))/1000)); 2004 } else { 2005 // clear marker 2006 ap->mAudioTrack->setMarkerPosition(0); 2007 } 2008 2009 if (eventFlags & SL_PLAYEVENT_HEADATNEWPOS) { 2010 ap->mAudioTrack->setPositionUpdatePeriod( 2011 (uint32_t)((((int64_t)pPlayItf->mPositionUpdatePeriod 2012 * sles_to_android_sampleRate(ap->mSampleRateMilliHz)))/1000)); 2013 } else { 2014 // clear periodic update 2015 ap->mAudioTrack->setPositionUpdatePeriod(0); 2016 } 2017 2018 if (eventFlags & SL_PLAYEVENT_HEADATEND) { 2019 // nothing to do for SL_PLAYEVENT_HEADATEND, callback event will be checked against mask 2020 } 2021 2022 if (eventFlags & SL_PLAYEVENT_HEADMOVING) { 2023 // FIXME support SL_PLAYEVENT_HEADMOVING 2024 SL_LOGD("[ FIXME: IPlay_SetCallbackEventsMask(SL_PLAYEVENT_HEADMOVING) on an " 2025 "SL_OBJECTID_AUDIOPLAYER to be implemented ]"); 2026 } 2027 if (eventFlags & SL_PLAYEVENT_HEADSTALLED) { 2028 // nothing to do for SL_PLAYEVENT_HEADSTALLED, callback event will be checked against mask 2029 } 2030 2031} 2032 2033 2034//----------------------------------------------------------------------------- 2035SLresult android_audioPlayer_getDuration(IPlay *pPlayItf, SLmillisecond *pDurMsec) { 2036 CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis; 2037 switch(ap->mAndroidObjType) { 2038 2039 case AUDIOPLAYER_FROM_URIFD: // intended fall-through 2040 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: { 2041 int32_t durationMsec = ANDROID_UNKNOWN_TIME; 2042 if (ap->mAPlayer != 0) { 2043 ap->mAPlayer->getDurationMsec(&durationMsec); 2044 } 2045 *pDurMsec = durationMsec == ANDROID_UNKNOWN_TIME ? SL_TIME_UNKNOWN : durationMsec; 2046 break; 2047 } 2048 2049 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 2050 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: // intended fall-through 2051 default: { 2052 *pDurMsec = SL_TIME_UNKNOWN; 2053 } 2054 } 2055 return SL_RESULT_SUCCESS; 2056} 2057 2058 2059//----------------------------------------------------------------------------- 2060void android_audioPlayer_getPosition(IPlay *pPlayItf, SLmillisecond *pPosMsec) { 2061 CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis; 2062 switch(ap->mAndroidObjType) { 2063 2064 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 2065 if ((ap->mSampleRateMilliHz == UNKNOWN_SAMPLERATE) || (ap->mAudioTrack == 0)) { 2066 *pPosMsec = 0; 2067 } else { 2068 uint32_t positionInFrames; 2069 ap->mAudioTrack->getPosition(&positionInFrames); 2070 *pPosMsec = ((int64_t)positionInFrames * 1000) / 2071 sles_to_android_sampleRate(ap->mSampleRateMilliHz); 2072 } 2073 break; 2074 2075 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 2076 case AUDIOPLAYER_FROM_URIFD: // intended fall-through 2077 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: { 2078 int32_t posMsec = ANDROID_UNKNOWN_TIME; 2079 if (ap->mAPlayer != 0) { 2080 ap->mAPlayer->getPositionMsec(&posMsec); 2081 } 2082 *pPosMsec = posMsec == ANDROID_UNKNOWN_TIME ? 0 : posMsec; 2083 break; 2084 } 2085 2086 default: 2087 *pPosMsec = 0; 2088 } 2089} 2090 2091 2092//----------------------------------------------------------------------------- 2093void android_audioPlayer_seek(CAudioPlayer *ap, SLmillisecond posMsec) { 2094 2095 switch(ap->mAndroidObjType) { 2096 2097 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: // intended fall-through 2098 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: 2099 break; 2100 2101 case AUDIOPLAYER_FROM_URIFD: // intended fall-through 2102 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: 2103 if (ap->mAPlayer != 0) { 2104 ap->mAPlayer->seek(posMsec); 2105 } 2106 break; 2107 2108 default: 2109 break; 2110 } 2111} 2112 2113 2114//----------------------------------------------------------------------------- 2115void android_audioPlayer_loop(CAudioPlayer *ap, SLboolean loopEnable) { 2116 2117 if ((AUDIOPLAYER_FROM_URIFD == ap->mAndroidObjType) && (ap->mAPlayer != 0)) { 2118 ap->mAPlayer->loop((bool)loopEnable); 2119 } 2120} 2121 2122 2123//----------------------------------------------------------------------------- 2124SLresult android_audioPlayer_setBufferingUpdateThresholdPerMille(CAudioPlayer *ap, 2125 SLpermille threshold) { 2126 SLresult result = SL_RESULT_SUCCESS; 2127 2128 switch (ap->mAndroidObjType) { 2129 case AUDIOPLAYER_FROM_URIFD: 2130 if (ap->mAPlayer != 0) { 2131 ap->mAPlayer->setBufferingUpdateThreshold(threshold / 10); 2132 } 2133 break; 2134 2135 default: {} 2136 } 2137 2138 return result; 2139} 2140 2141 2142//----------------------------------------------------------------------------- 2143void android_audioPlayer_bufferQueue_onRefilled_l(CAudioPlayer *ap) { 2144 // the AudioTrack associated with the AudioPlayer receiving audio from a PCM buffer 2145 // queue was stopped when the queue become empty, we restart as soon as a new buffer 2146 // has been enqueued since we're in playing state 2147 if (ap->mAudioTrack != 0) { 2148 // instead of ap->mAudioTrack->start(); 2149 ap->mDeferredStart = true; 2150 } 2151 2152 // when the queue became empty, an underflow on the prefetch status itf was sent. Now the queue 2153 // has received new data, signal it has sufficient data 2154 if (IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) { 2155 // we wouldn't have been called unless we were previously in the underflow state 2156 assert(SL_PREFETCHSTATUS_UNDERFLOW == ap->mPrefetchStatus.mStatus); 2157 assert(0 == ap->mPrefetchStatus.mLevel); 2158 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_SUFFICIENTDATA; 2159 ap->mPrefetchStatus.mLevel = 1000; 2160 // callback or no callback? 2161 SLuint32 prefetchEvents = ap->mPrefetchStatus.mCallbackEventsMask & 2162 (SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE); 2163 if (SL_PREFETCHEVENT_NONE != prefetchEvents) { 2164 ap->mPrefetchStatus.mDeferredPrefetchCallback = ap->mPrefetchStatus.mCallback; 2165 ap->mPrefetchStatus.mDeferredPrefetchContext = ap->mPrefetchStatus.mContext; 2166 ap->mPrefetchStatus.mDeferredPrefetchEvents = prefetchEvents; 2167 } 2168 } 2169} 2170 2171 2172//----------------------------------------------------------------------------- 2173/* 2174 * BufferQueue::Clear 2175 */ 2176SLresult android_audioPlayer_bufferQueue_onClear(CAudioPlayer *ap) { 2177 SLresult result = SL_RESULT_SUCCESS; 2178 2179 switch (ap->mAndroidObjType) { 2180 //----------------------------------- 2181 // AudioTrack 2182 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: 2183 if (ap->mAudioTrack != 0) { 2184 ap->mAudioTrack->flush(); 2185 } 2186 break; 2187 default: 2188 result = SL_RESULT_INTERNAL_ERROR; 2189 break; 2190 } 2191 2192 return result; 2193} 2194 2195 2196//----------------------------------------------------------------------------- 2197SLresult android_audioPlayer_androidBufferQueue_registerCallback_l(CAudioPlayer *ap) { 2198 SLresult result = SL_RESULT_SUCCESS; 2199 assert(ap->mAPlayer != 0); 2200 switch (ap->mAndroidObjType) { 2201 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: { 2202 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(ap->mAPlayer.get()); 2203 splr->registerQueueCallback( 2204 (const void*)ap /*user*/, true /*userIsAudioPlayer*/, 2205 ap->mAndroidBufferQueue.mContext /*context*/, 2206 (const void*)&(ap->mAndroidBufferQueue.mItf) /*caller*/); 2207 } break; 2208 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: { 2209 android::AacBqToPcmCbRenderer* dec = 2210 static_cast<android::AacBqToPcmCbRenderer*>(ap->mAPlayer.get()); 2211 dec->registerSourceQueueCallback((const void*)ap /*user*/, 2212 ap->mAndroidBufferQueue.mContext /*context*/, 2213 (const void*)&(ap->mAndroidBufferQueue.mItf) /*caller*/); 2214 } break; 2215 default: 2216 SL_LOGE("Error registering AndroidBufferQueue callback: unexpected object type %d", 2217 ap->mAndroidObjType); 2218 result = SL_RESULT_INTERNAL_ERROR; 2219 break; 2220 } 2221 return result; 2222} 2223 2224//----------------------------------------------------------------------------- 2225void android_audioPlayer_androidBufferQueue_clear_l(CAudioPlayer *ap) { 2226 if ((ap->mAndroidObjType == AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) && (ap->mAPlayer != 0)) { 2227 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(ap->mAPlayer.get()); 2228 splr->appClear_l(); 2229 } 2230} 2231 2232void android_audioPlayer_androidBufferQueue_onRefilled_l(CAudioPlayer *ap) { 2233 if ((ap->mAndroidObjType == AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) && (ap->mAPlayer != 0)) { 2234 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(ap->mAPlayer.get()); 2235 splr->queueRefilled_l(); 2236 } 2237} 2238