MediaPlayer_to_android.cpp revision 9d8a98601357c0669dca4de63e43196c0a70553d
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "sles_allinclusive.h" 18#include "utils/RefBase.h" 19#include "android_prompts.h" 20// LocAVPlayer and StreamPlayer derive from GenericMediaPlayer, 21// so no need to #include "android_GenericMediaPlayer.h" 22#include "android_LocAVPlayer.h" 23#include "android_StreamPlayer.h" 24 25 26//----------------------------------------------------------------------------- 27static void player_handleMediaPlayerEventNotifications(int event, int data1, int data2, void* user) 28{ 29 30 // FIXME This code is derived from similar code in sfplayer_handlePrefetchEvent. The two 31 // versions are quite similar, but still different enough that they need to be separate. 32 // At some point they should be re-factored and merged if feasible. 33 // As with other OpenMAX AL implementation code, this copy mostly uses SL_ symbols 34 // rather than XA_ unless the difference is significant. 35 36 if (NULL == user) { 37 return; 38 } 39 40 CMediaPlayer* mp = (CMediaPlayer*) user; 41 if (!android::CallbackProtector::enterCbIfOk(mp->mCallbackProtector)) { 42 // it is not safe to enter the callback (the media player is about to go away) 43 return; 44 } 45 union { 46 char c[sizeof(int)]; 47 int i; 48 } u; 49 u.i = event; 50 SL_LOGV("player_handleMediaPlayerEventNotifications(event='%c%c%c%c' (%d), data1=%d, data2=%d, " 51 "user=%p) from AVPlayer", u.c[3], u.c[2], u.c[1], u.c[0], event, data1, data2, user); 52 switch(event) { 53 54 case android::GenericPlayer::kEventPrepared: { 55 56 SL_LOGV("Received AVPlayer::kEventPrepared for CMediaPlayer %p", mp); 57 58 // assume no callback 59 slPrefetchCallback callback = NULL; 60 void* callbackPContext = NULL; 61 62 object_lock_exclusive(&mp->mObject); 63 // mark object as prepared; same state is used for successfully or unsuccessful prepare 64 mp->mAndroidObjState = ANDROID_READY; 65 66 // AVPlayer prepare() failed prefetching, there is no event in XAPrefetchStatus to 67 // indicate a prefetch error, so we signal it by sending simulataneously two events: 68 // - SL_PREFETCHEVENT_FILLLEVELCHANGE with a level of 0 69 // - SL_PREFETCHEVENT_STATUSCHANGE with a status of SL_PREFETCHSTATUS_UNDERFLOW 70 if (PLAYER_SUCCESS != data1 && IsInterfaceInitialized(&mp->mObject, MPH_XAPREFETCHSTATUS)) { 71 mp->mPrefetchStatus.mLevel = 0; 72 mp->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW; 73 if (!(~mp->mPrefetchStatus.mCallbackEventsMask & 74 (SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE))) { 75 callback = mp->mPrefetchStatus.mCallback; 76 callbackPContext = mp->mPrefetchStatus.mContext; 77 } 78 } 79 object_unlock_exclusive(&mp->mObject); 80 81 // callback with no lock held 82 if (NULL != callback) { 83 (*callback)(&mp->mPrefetchStatus.mItf, callbackPContext, 84 SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE); 85 } 86 87 break; 88 } 89 90 case android::GenericPlayer::kEventHasVideoSize: { 91 SL_LOGV("Received AVPlayer::kEventHasVideoSize (%d,%d) for CMediaPlayer %p", 92 data1, data2, mp); 93 94 object_lock_exclusive(&mp->mObject); 95 96 // remove an existing video info entry (here we only have one video stream) 97 for(size_t i=0 ; i < mp->mStreamInfo.mStreamInfoTable.size() ; i++) { 98 if (XA_DOMAINTYPE_VIDEO == mp->mStreamInfo.mStreamInfoTable.itemAt(i).domain) { 99 mp->mStreamInfo.mStreamInfoTable.removeAt(i); 100 break; 101 } 102 } 103 // update the stream information with a new video info entry 104 StreamInfo streamInfo; 105 streamInfo.domain = XA_DOMAINTYPE_VIDEO; 106 streamInfo.videoInfo.codecId = 0;// unknown, we don't have that info FIXME 107 streamInfo.videoInfo.width = (XAuint32)data1; 108 streamInfo.videoInfo.height = (XAuint32)data2; 109 streamInfo.videoInfo.bitRate = 0;// unknown, we don't have that info FIXME 110 streamInfo.videoInfo.frameRate = 0; 111 streamInfo.videoInfo.duration = XA_TIME_UNKNOWN; 112 StreamInfo &contInfo = mp->mStreamInfo.mStreamInfoTable.editItemAt(0); 113 contInfo.containerInfo.numStreams = 1; 114 ssize_t index = mp->mStreamInfo.mStreamInfoTable.add(streamInfo); 115 116 xaStreamEventChangeCallback callback = mp->mStreamInfo.mCallback; 117 void* callbackPContext = mp->mStreamInfo.mContext; 118 119 object_unlock_exclusive(&mp->mObject); 120 121 // enqueue notification (outside of lock) that the stream information has been updated 122 if ((NULL != callback) && (index >= 0)) { 123#ifndef USE_ASYNCHRONOUS_STREAMCBEVENT_PROPERTYCHANGE_CALLBACK 124 (*callback)(&mp->mStreamInfo.mItf, XA_STREAMCBEVENT_PROPERTYCHANGE /*eventId*/, 125 1 /*streamIndex, only one stream supported here, 0 is reserved*/, 126 NULL /*pEventData, always NULL in OpenMAX AL 1.0.1*/, 127 callbackPContext /*pContext*/); 128#else 129 SLresult res = EnqueueAsyncCallback_piipp(mp, callback, 130 /*p1*/ &mp->mStreamInfo.mItf, 131 /*i1*/ XA_STREAMCBEVENT_PROPERTYCHANGE /*eventId*/, 132 /*i2*/ 1 /*streamIndex, only one stream supported here, 0 is reserved*/, 133 /*p2*/ NULL /*pEventData, always NULL in OpenMAX AL 1.0.1*/, 134 /*p3*/ callbackPContext /*pContext*/); 135 LOGW_IF(SL_RESULT_SUCCESS != res, 136 "Callback %p(%p, XA_STREAMCBEVENT_PROPERTYCHANGE, 1, NULL, %p) dropped", 137 callback, &mp->mStreamInfo.mItf, callbackPContext); 138#endif 139 } 140 break; 141 } 142 143 case android::GenericPlayer::kEventEndOfStream: { 144 SL_LOGV("Received AVPlayer::kEventEndOfStream for CMediaPlayer %p", mp); 145 146 object_lock_exclusive(&mp->mObject); 147 // should be xaPlayCallback but we're sharing the itf between SL and AL 148 slPlayCallback playCallback = NULL; 149 void * playContext = NULL; 150 // XAPlayItf callback or no callback? 151 if (mp->mPlay.mEventFlags & XA_PLAYEVENT_HEADATEND) { 152 playCallback = mp->mPlay.mCallback; 153 playContext = mp->mPlay.mContext; 154 } 155 mp->mPlay.mState = XA_PLAYSTATE_PAUSED; 156 object_unlock_exclusive(&mp->mObject); 157 158 // enqueue callback with no lock held 159 if (NULL != playCallback) { 160#ifndef USE_ASYNCHRONOUS_PLAY_CALLBACK 161 (*playCallback)(&mp->mPlay.mItf, playContext, XA_PLAYEVENT_HEADATEND); 162#else 163 SLresult res = EnqueueAsyncCallback_ppi(mp, playCallback, &mp->mPlay.mItf, playContext, 164 XA_PLAYEVENT_HEADATEND); 165 LOGW_IF(SL_RESULT_SUCCESS != res, 166 "Callback %p(%p, %p, SL_PLAYEVENT_HEADATEND) dropped", playCallback, 167 &mp->mPlay.mItf, playContext); 168#endif 169 } 170 break; 171 } 172 173 case android::GenericPlayer::kEventChannelCount: { 174 SL_LOGV("kEventChannelCount channels = %d", data1); 175 object_lock_exclusive(&mp->mObject); 176 if (UNKNOWN_NUMCHANNELS == mp->mNumChannels && UNKNOWN_NUMCHANNELS != data1) { 177 mp->mNumChannels = data1; 178 android_Player_volumeUpdate(mp); 179 } 180 object_unlock_exclusive(&mp->mObject); 181 } 182 break; 183 184 case android::GenericPlayer::kEventPrefetchFillLevelUpdate: { 185 SL_LOGV("kEventPrefetchFillLevelUpdate"); 186 if (!IsInterfaceInitialized(&mp->mObject, MPH_XAPREFETCHSTATUS)) { 187 break; 188 } 189 slPrefetchCallback callback = NULL; 190 void* callbackPContext = NULL; 191 192 // SLPrefetchStatusItf callback or no callback? 193 interface_lock_exclusive(&mp->mPrefetchStatus); 194 if (mp->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_FILLLEVELCHANGE) { 195 callback = mp->mPrefetchStatus.mCallback; 196 callbackPContext = mp->mPrefetchStatus.mContext; 197 } 198 mp->mPrefetchStatus.mLevel = (SLpermille)data1; 199 interface_unlock_exclusive(&mp->mPrefetchStatus); 200 201 // callback with no lock held 202 if (NULL != callback) { 203 (*callback)(&mp->mPrefetchStatus.mItf, callbackPContext, 204 SL_PREFETCHEVENT_FILLLEVELCHANGE); 205 } 206 } 207 break; 208 209 case android::GenericPlayer::kEventPrefetchStatusChange: { 210 SL_LOGV("kEventPrefetchStatusChange"); 211 if (!IsInterfaceInitialized(&mp->mObject, MPH_XAPREFETCHSTATUS)) { 212 break; 213 } 214 slPrefetchCallback callback = NULL; 215 void* callbackPContext = NULL; 216 217 // SLPrefetchStatusItf callback or no callback? 218 object_lock_exclusive(&mp->mObject); 219 if (mp->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_STATUSCHANGE) { 220 callback = mp->mPrefetchStatus.mCallback; 221 callbackPContext = mp->mPrefetchStatus.mContext; 222 } 223 if (data1 >= android::kStatusIntermediate) { 224 mp->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_SUFFICIENTDATA; 225 } else if (data1 < android::kStatusIntermediate) { 226 mp->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW; 227 } 228 object_unlock_exclusive(&mp->mObject); 229 230 // callback with no lock held 231 if (NULL != callback) { 232 (*callback)(&mp->mPrefetchStatus.mItf, callbackPContext, SL_PREFETCHEVENT_STATUSCHANGE); 233 } 234 } 235 break; 236 237 case android::GenericPlayer::kEventPlay: { 238 SL_LOGV("kEventPlay"); 239 240 interface_lock_shared(&mp->mPlay); 241 slPlayCallback callback = mp->mPlay.mCallback; 242 void* callbackPContext = mp->mPlay.mContext; 243 interface_unlock_shared(&mp->mPlay); 244 245 if (NULL != callback) { 246 (*callback)(&mp->mPlay.mItf, callbackPContext, (SLuint32) data1); // SL_PLAYEVENT_HEAD* 247 } 248 } 249 break; 250 251 default: { 252 SL_LOGE("Received unknown event %d, data %d from AVPlayer", event, data1); 253 } 254 } 255 256 mp->mCallbackProtector->exitCb(); 257} 258 259 260//----------------------------------------------------------------------------- 261XAresult android_Player_checkSourceSink(CMediaPlayer *mp) { 262 263 XAresult result = XA_RESULT_SUCCESS; 264 265 const SLDataSource *pSrc = &mp->mDataSource.u.mSource; 266 const SLDataSink *pAudioSnk = &mp->mAudioSink.u.mSink; 267 268 // format check: 269 const SLuint32 sourceLocatorType = *(SLuint32 *)pSrc->pLocator; 270 const SLuint32 sourceFormatType = *(SLuint32 *)pSrc->pFormat; 271 const SLuint32 audioSinkLocatorType = *(SLuint32 *)pAudioSnk->pLocator; 272 //const SLuint32 sinkFormatType = *(SLuint32 *)pAudioSnk->pFormat; 273 274 // Source check 275 switch(sourceLocatorType) { 276 277 case XA_DATALOCATOR_ANDROIDBUFFERQUEUE: { 278 switch (sourceFormatType) { 279 case XA_DATAFORMAT_MIME: { 280 SLDataFormat_MIME *df_mime = (SLDataFormat_MIME *) pSrc->pFormat; 281 if (SL_CONTAINERTYPE_MPEG_TS != df_mime->containerType) { 282 SL_LOGE("Cannot create player with XA_DATALOCATOR_ANDROIDBUFFERQUEUE data source " 283 "that is not fed MPEG-2 TS data"); 284 return SL_RESULT_CONTENT_UNSUPPORTED; 285 } 286 } break; 287 default: 288 SL_LOGE("Cannot create player with XA_DATALOCATOR_ANDROIDBUFFERQUEUE data source " 289 "without SL_DATAFORMAT_MIME format"); 290 return XA_RESULT_CONTENT_UNSUPPORTED; 291 } 292 } break; 293 294 case XA_DATALOCATOR_URI: // intended fall-through 295 case XA_DATALOCATOR_ANDROIDFD: 296 break; 297 298 default: 299 SL_LOGE("Cannot create media player with data locator type 0x%x", 300 (unsigned) sourceLocatorType); 301 return SL_RESULT_PARAMETER_INVALID; 302 }// switch (locatorType) 303 304 // Audio sink check: only playback is supported here 305 switch(audioSinkLocatorType) { 306 307 case XA_DATALOCATOR_OUTPUTMIX: 308 break; 309 310 default: 311 SL_LOGE("Cannot create media player with audio sink data locator of type 0x%x", 312 (unsigned) audioSinkLocatorType); 313 return XA_RESULT_PARAMETER_INVALID; 314 }// switch (locaaudioSinkLocatorTypeorType) 315 316 return result; 317} 318 319 320//----------------------------------------------------------------------------- 321XAresult android_Player_create(CMediaPlayer *mp) { 322 323 XAresult result = XA_RESULT_SUCCESS; 324 325 // FIXME verify data source 326 const SLDataSource *pDataSrc = &mp->mDataSource.u.mSource; 327 // FIXME verify audio data sink 328 const SLDataSink *pAudioSnk = &mp->mAudioSink.u.mSink; 329 // FIXME verify image data sink 330 const SLDataSink *pVideoSnk = &mp->mImageVideoSink.u.mSink; 331 332 XAuint32 sourceLocator = *(XAuint32 *)pDataSrc->pLocator; 333 switch(sourceLocator) { 334 // FIXME support Android simple buffer queue as well 335 case XA_DATALOCATOR_ANDROIDBUFFERQUEUE: 336 mp->mAndroidObjType = AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE; 337 break; 338 case XA_DATALOCATOR_URI: // intended fall-through 339 case SL_DATALOCATOR_ANDROIDFD: 340 mp->mAndroidObjType = AUDIOVIDEOPLAYER_FROM_URIFD; 341 break; 342 case XA_DATALOCATOR_ADDRESS: // intended fall-through 343 default: 344 SL_LOGE("Unable to create MediaPlayer for data source locator 0x%x", sourceLocator); 345 result = XA_RESULT_PARAMETER_INVALID; 346 break; 347 } 348 349 // FIXME duplicates an initialization also done by higher level 350 mp->mAndroidObjState = ANDROID_UNINITIALIZED; 351 mp->mStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE; 352 mp->mSessionId = android::AudioSystem::newAudioSessionId(); 353 354 mp->mCallbackProtector = new android::CallbackProtector(); 355 356 return result; 357} 358 359 360//----------------------------------------------------------------------------- 361// FIXME abstract out the diff between CMediaPlayer and CAudioPlayer 362XAresult android_Player_realize(CMediaPlayer *mp, SLboolean async) { 363 SL_LOGV("android_Player_realize_l(%p)", mp); 364 XAresult result = XA_RESULT_SUCCESS; 365 366 const SLDataSource *pDataSrc = &mp->mDataSource.u.mSource; 367 const SLuint32 sourceLocator = *(SLuint32 *)pDataSrc->pLocator; 368 369 AudioPlayback_Parameters ap_params; 370 ap_params.sessionId = mp->mSessionId; 371 ap_params.streamType = mp->mStreamType; 372 373 switch(mp->mAndroidObjType) { 374 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: { 375 mp->mAVPlayer = new android::StreamPlayer(&ap_params, true /*hasVideo*/, 376 &mp->mAndroidBufferQueue, mp->mCallbackProtector); 377 mp->mAVPlayer->init(player_handleMediaPlayerEventNotifications, (void*)mp); 378 } 379 break; 380 case AUDIOVIDEOPLAYER_FROM_URIFD: { 381 mp->mAVPlayer = new android::LocAVPlayer(&ap_params, true /*hasVideo*/); 382 mp->mAVPlayer->init(player_handleMediaPlayerEventNotifications, (void*)mp); 383 switch (mp->mDataSource.mLocator.mLocatorType) { 384 case XA_DATALOCATOR_URI: 385 ((android::LocAVPlayer*)mp->mAVPlayer.get())->setDataSource( 386 (const char*)mp->mDataSource.mLocator.mURI.URI); 387 break; 388 case XA_DATALOCATOR_ANDROIDFD: { 389 int64_t offset = (int64_t)mp->mDataSource.mLocator.mFD.offset; 390 ((android::LocAVPlayer*)mp->mAVPlayer.get())->setDataSource( 391 (int)mp->mDataSource.mLocator.mFD.fd, 392 offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ? 393 (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset, 394 (int64_t)mp->mDataSource.mLocator.mFD.length); 395 } 396 break; 397 default: 398 SL_LOGE("Invalid or unsupported data locator type %u for data source", 399 mp->mDataSource.mLocator.mLocatorType); 400 result = XA_RESULT_PARAMETER_INVALID; 401 } 402 } 403 break; 404 case INVALID_TYPE: // intended fall-through 405 default: 406 SL_LOGE("Unable to realize MediaPlayer, invalid internal Android object type"); 407 result = XA_RESULT_PARAMETER_INVALID; 408 break; 409 } 410 411 if (XA_RESULT_SUCCESS == result) { 412 413 // if there is a video sink 414 if (XA_DATALOCATOR_NATIVEDISPLAY == 415 mp->mImageVideoSink.mLocator.mLocatorType) { 416 ANativeWindow *nativeWindow = (ANativeWindow *) 417 mp->mImageVideoSink.mLocator.mNativeDisplay.hWindow; 418 // we already verified earlier that hWindow is non-NULL 419 assert(nativeWindow != NULL); 420 result = android_Player_setNativeWindow(mp, nativeWindow); 421 } 422 423 } 424 425 return result; 426} 427 428// Called with a lock on MediaPlayer, and blocks until safe to destroy 429XAresult android_Player_preDestroy(CMediaPlayer *mp) { 430 SL_LOGV("android_Player_preDestroy(%p)", mp); 431 432 // Not yet clear why this order is important, but it reduces detected deadlocks 433 object_unlock_exclusive(&mp->mObject); 434 if (mp->mCallbackProtector != 0) { 435 mp->mCallbackProtector->requestCbExitAndWait(); 436 } 437 object_lock_exclusive(&mp->mObject); 438 439 if (mp->mAVPlayer != 0) { 440 mp->mAVPlayer->preDestroy(); 441 } 442 SL_LOGV("android_Player_preDestroy(%p) after mAVPlayer->preDestroy()", mp); 443 444 return XA_RESULT_SUCCESS; 445} 446 447//----------------------------------------------------------------------------- 448XAresult android_Player_destroy(CMediaPlayer *mp) { 449 SL_LOGV("android_Player_destroy(%p)", mp); 450 451 mp->mAVPlayer.clear(); 452 mp->mCallbackProtector.clear(); 453 454 // explicit destructor 455 mp->mAVPlayer.~sp(); 456 mp->mCallbackProtector.~sp(); 457 458 return XA_RESULT_SUCCESS; 459} 460 461 462void android_Player_usePlayEventMask(CMediaPlayer *mp) { 463 if (mp->mAVPlayer != 0) { 464 IPlay *pPlayItf = &mp->mPlay; 465 mp->mAVPlayer->setPlayEvents((int32_t) pPlayItf->mEventFlags, 466 (int32_t) pPlayItf->mMarkerPosition, (int32_t) pPlayItf->mPositionUpdatePeriod); 467 } 468} 469 470 471XAresult android_Player_getDuration(IPlay *pPlayItf, XAmillisecond *pDurMsec) { 472 CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis; 473 474 switch (avp->mAndroidObjType) { 475 476 case AUDIOVIDEOPLAYER_FROM_URIFD: { 477 int dur = ANDROID_UNKNOWN_TIME; 478 if (avp->mAVPlayer != 0) { 479 avp->mAVPlayer->getDurationMsec(&dur); 480 } 481 if (dur == ANDROID_UNKNOWN_TIME) { 482 *pDurMsec = XA_TIME_UNKNOWN; 483 } else { 484 *pDurMsec = (XAmillisecond)dur; 485 } 486 } break; 487 488 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 489 default: 490 *pDurMsec = XA_TIME_UNKNOWN; 491 break; 492 } 493 494 return XA_RESULT_SUCCESS; 495} 496 497 498XAresult android_Player_getPosition(IPlay *pPlayItf, XAmillisecond *pPosMsec) { 499 SL_LOGD("android_Player_getPosition()"); 500 XAresult result = XA_RESULT_SUCCESS; 501 CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis; 502 503 switch (avp->mAndroidObjType) { 504 505 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 506 case AUDIOVIDEOPLAYER_FROM_URIFD: { 507 int pos = ANDROID_UNKNOWN_TIME; 508 if (avp->mAVPlayer != 0) { 509 avp->mAVPlayer->getPositionMsec(&pos); 510 } 511 if (pos == ANDROID_UNKNOWN_TIME) { 512 *pPosMsec = 0; 513 } else { 514 *pPosMsec = (XAmillisecond)pos; 515 } 516 } break; 517 518 default: 519 // we shouldn't be here 520 assert(false); 521 break; 522 } 523 524 return result; 525} 526 527 528//----------------------------------------------------------------------------- 529/** 530 * pre-condition: mp != NULL 531 */ 532void android_Player_volumeUpdate(CMediaPlayer* mp) 533{ 534 android::GenericPlayer* avp = mp->mAVPlayer.get(); 535 if (avp != NULL) { 536 float volumes[2]; 537 // MediaPlayer does not currently support EffectSend or MuteSolo 538 android_player_volumeUpdate(volumes, &mp->mVolume, mp->mNumChannels, 1.0f, NULL); 539 float leftVol = volumes[0], rightVol = volumes[1]; 540 avp->setVolume(leftVol, rightVol); 541 } 542} 543 544//----------------------------------------------------------------------------- 545/** 546 * pre-condition: gp != 0 547 */ 548XAresult android_Player_setPlayState(const android::sp<android::GenericPlayer> &gp, 549 SLuint32 playState, 550 AndroidObjectState* pObjState) 551{ 552 XAresult result = XA_RESULT_SUCCESS; 553 AndroidObjectState objState = *pObjState; 554 555 switch (playState) { 556 case SL_PLAYSTATE_STOPPED: { 557 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_STOPPED"); 558 gp->stop(); 559 } 560 break; 561 case SL_PLAYSTATE_PAUSED: { 562 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PAUSED"); 563 switch(objState) { 564 case ANDROID_UNINITIALIZED: 565 *pObjState = ANDROID_PREPARING; 566 gp->prepare(); 567 break; 568 case ANDROID_PREPARING: 569 break; 570 case ANDROID_READY: 571 gp->pause(); 572 break; 573 default: 574 SL_LOGE("Android object in invalid state"); 575 break; 576 } 577 } 578 break; 579 case SL_PLAYSTATE_PLAYING: { 580 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PLAYING"); 581 switch(objState) { 582 case ANDROID_UNINITIALIZED: 583 *pObjState = ANDROID_PREPARING; 584 gp->prepare(); 585 // intended fall through 586 case ANDROID_PREPARING: 587 // intended fall through 588 case ANDROID_READY: 589 gp->play(); 590 break; 591 default: 592 SL_LOGE("Android object in invalid state"); 593 break; 594 } 595 } 596 break; 597 default: 598 // checked by caller, should not happen 599 break; 600 } 601 602 return result; 603} 604 605 606/** 607 * pre-condition: mp != NULL 608 */ 609XAresult android_Player_seek(CMediaPlayer *mp, SLmillisecond posMsec) { 610 XAresult result = XA_RESULT_SUCCESS; 611 switch (mp->mAndroidObjType) { 612 case AUDIOVIDEOPLAYER_FROM_URIFD: 613 if (mp->mAVPlayer !=0) { 614 mp->mAVPlayer->seek(posMsec); 615 } 616 break; 617 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 618 default: { 619 result = XA_RESULT_FEATURE_UNSUPPORTED; 620 } 621 } 622 return result; 623} 624 625 626/** 627 * pre-condition: mp != NULL 628 */ 629XAresult android_Player_loop(CMediaPlayer *mp, SLboolean loopEnable) { 630 XAresult result = XA_RESULT_SUCCESS; 631 switch (mp->mAndroidObjType) { 632 case AUDIOVIDEOPLAYER_FROM_URIFD: 633 if (mp->mAVPlayer !=0) { 634 mp->mAVPlayer->loop(loopEnable); 635 } 636 break; 637 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 638 default: { 639 result = XA_RESULT_FEATURE_UNSUPPORTED; 640 } 641 } 642 return result; 643} 644 645 646//----------------------------------------------------------------------------- 647void android_Player_androidBufferQueue_clear_l(CMediaPlayer *mp) { 648 if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) 649 && (mp->mAVPlayer != 0)) { 650 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get()); 651 splr->appClear_l(); 652 } 653} 654 655 656void android_Player_androidBufferQueue_onRefilled_l(CMediaPlayer *mp) { 657 if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) 658 && (mp->mAVPlayer != 0)) { 659 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get()); 660 splr->queueRefilled(); 661 } 662} 663 664 665/* 666 * pre-conditions: 667 * mp != NULL 668 * mp->mAVPlayer != 0 (player is realized) 669 * nativeWindow can be NULL, but if NULL it is treated as an error 670 */ 671SLresult android_Player_setNativeWindow(CMediaPlayer *mp, ANativeWindow *nativeWindow) 672{ 673 assert(mp != NULL); 674 assert(mp->mAVPlayer != 0); 675 if (nativeWindow == NULL) { 676 SL_LOGE("ANativeWindow is NULL"); 677 return SL_RESULT_PARAMETER_INVALID; 678 } 679 SLresult result; 680 int err; 681 int value; 682 // this could crash if app passes in a bad parameter, but that's OK 683 err = (*nativeWindow->query)(nativeWindow, NATIVE_WINDOW_CONCRETE_TYPE, &value); 684 if (0 != err) { 685 SL_LOGE("Query NATIVE_WINDOW_CONCRETE_TYPE on ANativeWindow * %p failed; " 686 "errno %d", nativeWindow, err); 687 result = SL_RESULT_PARAMETER_INVALID; 688 } else { 689 switch (value) { 690 case NATIVE_WINDOW_SURFACE: { // Surface 691 SL_LOGV("Displaying on ANativeWindow of type NATIVE_WINDOW_SURFACE"); 692 android::sp<android::Surface> nativeSurface( 693 static_cast<android::Surface *>(nativeWindow)); 694 mp->mAVPlayer->setVideoSurface(nativeSurface); 695 result = SL_RESULT_SUCCESS; 696 } break; 697 case NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT: { // SurfaceTextureClient 698 SL_LOGV("Displaying on ANativeWindow of type NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT"); 699 android::sp<android::SurfaceTextureClient> surfaceTextureClient( 700 static_cast<android::SurfaceTextureClient *>(nativeWindow)); 701 android::sp<android::ISurfaceTexture> nativeSurfaceTexture( 702 surfaceTextureClient->getISurfaceTexture()); 703 mp->mAVPlayer->setVideoSurfaceTexture(nativeSurfaceTexture); 704 result = SL_RESULT_SUCCESS; 705 } break; 706 case NATIVE_WINDOW_FRAMEBUFFER: // FramebufferNativeWindow 707 // fall through 708 default: 709 SL_LOGE("ANativeWindow * %p has unknown or unsupported concrete type %d", 710 nativeWindow, value); 711 result = SL_RESULT_PARAMETER_INVALID; 712 break; 713 } 714 } 715 return result; 716} 717