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