MediaPlayer_to_android.cpp revision 64621eac543d714d4d3f7cb9c24205f2ddc59201
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 if (NULL == user) { 30 return; 31 } 32 33 CMediaPlayer* mp = (CMediaPlayer*) user; 34 SL_LOGV("received event %d, data %d from AVPlayer", event, data1); 35 36 switch(event) { 37 38 case android::GenericPlayer::kEventPrepared: { 39 if (PLAYER_SUCCESS == data1) { 40 object_lock_exclusive(&mp->mObject); 41 SL_LOGV("Received AVPlayer::kEventPrepared from AVPlayer for CMediaPlayer %p", mp); 42 mp->mAndroidObjState = ANDROID_READY; 43 object_unlock_exclusive(&mp->mObject); 44 } 45 break; 46 } 47 48 case android::GenericPlayer::kEventHasVideoSize: { 49 SL_LOGV("Received AVPlayer::kEventHasVideoSize (%d,%d) for CMediaPlayer %p", 50 data1, data2, mp); 51 52 object_lock_exclusive(&mp->mObject); 53 54 // remove an existing video info entry (here we only have one video stream) 55 for(size_t i=0 ; i < mp->mStreamInfo.mStreamInfoTable.size() ; i++) { 56 if (XA_DOMAINTYPE_VIDEO == mp->mStreamInfo.mStreamInfoTable.itemAt(i).domain) { 57 mp->mStreamInfo.mStreamInfoTable.removeAt(i); 58 break; 59 } 60 } 61 // update the stream information with a new video info entry 62 StreamInfo streamInfo; 63 streamInfo.domain = XA_DOMAINTYPE_VIDEO; 64 streamInfo.videoInfo.codecId = 0;// unknown, we don't have that info FIXME 65 streamInfo.videoInfo.width = (XAuint32)data1; 66 streamInfo.videoInfo.height = (XAuint32)data2; 67 streamInfo.videoInfo.bitRate = 0;// unknown, we don't have that info FIXME 68 streamInfo.videoInfo.duration = XA_TIME_UNKNOWN; 69 StreamInfo &contInfo = mp->mStreamInfo.mStreamInfoTable.editItemAt(0); 70 contInfo.containerInfo.numStreams = 1; 71 ssize_t index = mp->mStreamInfo.mStreamInfoTable.add(streamInfo); 72 73 xaStreamEventChangeCallback callback = mp->mStreamInfo.mCallback; 74 void* callbackPContext = mp->mStreamInfo.mContext; 75 76 object_unlock_exclusive(&mp->mObject); 77 78 // enqueue notification (outside of lock) that the stream information has been updated 79 if ((NULL != callback) && (index >= 0)) { 80#ifdef XA_SYNCHRONOUS_STREAMCBEVENT_PROPERTYCHANGE 81 (*callback)(&mp->mStreamInfo.mItf, XA_STREAMCBEVENT_PROPERTYCHANGE /*eventId*/, 82 1 /*streamIndex, only one stream supported here, 0 is reserved*/, 83 NULL /*pEventData, always NULL in OpenMAX AL 1.0.1*/, 84 callbackPContext /*pContext*/); 85#else 86 SLresult res = EnqueueAsyncCallback_piipp(mp, callback, 87 /*p1*/ &mp->mStreamInfo.mItf, 88 /*i1*/ XA_STREAMCBEVENT_PROPERTYCHANGE /*eventId*/, 89 /*i2*/ 1 /*streamIndex, only one stream supported here, 0 is reserved*/, 90 /*p2*/ NULL /*pEventData, always NULL in OpenMAX AL 1.0.1*/, 91 /*p3*/ callbackPContext /*pContext*/); 92#endif 93 } 94 break; 95 } 96 97 case android::GenericPlayer::kEventEndOfStream: { 98 SL_LOGV("Received AVPlayer::kEventEndOfStream for CMediaPlayer %p", mp); 99 100 object_lock_exclusive(&mp->mObject); 101 // should be xaPlayCallback but we're sharing the itf between SL and AL 102 slPlayCallback playCallback = NULL; 103 void * playContext = NULL; 104 // XAPlayItf callback or no callback? 105 if (mp->mPlay.mEventFlags & XA_PLAYEVENT_HEADATEND) { 106 playCallback = mp->mPlay.mCallback; 107 playContext = mp->mPlay.mContext; 108 } 109 object_unlock_exclusive(&mp->mObject); 110 111 // enqueue callback with no lock held 112 if (NULL != playCallback) { 113#ifdef XA_SYNCHRONOUS_PLAYEVENT_HEADATEND 114 (*playCallback)(&mp->mPlay.mItf, playContext, XA_PLAYEVENT_HEADATEND); 115#else 116 SLresult res = EnqueueAsyncCallback_ppi(mp, playCallback, &mp->mPlay.mItf, playContext, 117 XA_PLAYEVENT_HEADATEND); 118 LOGW_IF(SL_RESULT_SUCCESS != res, 119 "Callback %p(%p, %p, XA_PLAYEVENT_HEADATEND) dropped", playCallback, 120 &mp->mPlay.mItf, playContext); 121#endif 122 } 123 break; 124 } 125 126 default: { 127 SL_LOGE("Received unknown event %d, data %d from AVPlayer", event, data1); 128 } 129 } 130} 131 132 133//----------------------------------------------------------------------------- 134XAresult android_Player_checkSourceSink(CMediaPlayer *mp) { 135 136 XAresult result = XA_RESULT_SUCCESS; 137 138 const SLDataSource *pSrc = &mp->mDataSource.u.mSource; 139 const SLDataSink *pAudioSnk = &mp->mAudioSink.u.mSink; 140 141 // format check: 142 const SLuint32 sourceLocatorType = *(SLuint32 *)pSrc->pLocator; 143 const SLuint32 sourceFormatType = *(SLuint32 *)pSrc->pFormat; 144 const SLuint32 audioSinkLocatorType = *(SLuint32 *)pAudioSnk->pLocator; 145 //const SLuint32 sinkFormatType = *(SLuint32 *)pAudioSnk->pFormat; 146 147 // Source check 148 switch(sourceLocatorType) { 149 150 case XA_DATALOCATOR_ANDROIDBUFFERQUEUE: { 151 switch (sourceFormatType) { 152 case XA_DATAFORMAT_MIME: { 153 SLDataFormat_MIME *df_mime = (SLDataFormat_MIME *) pSrc->pFormat; 154 if (SL_CONTAINERTYPE_MPEG_TS != df_mime->containerType) { 155 SL_LOGE("Cannot create player with XA_DATALOCATOR_ANDROIDBUFFERQUEUE data source " 156 "that is not fed MPEG-2 TS data"); 157 return SL_RESULT_CONTENT_UNSUPPORTED; 158 } 159 } break; 160 default: 161 SL_LOGE("Cannot create player with XA_DATALOCATOR_ANDROIDBUFFERQUEUE data source " 162 "without SL_DATAFORMAT_MIME format"); 163 return XA_RESULT_CONTENT_UNSUPPORTED; 164 } 165 } break; 166 167 case XA_DATALOCATOR_URI: // intended fall-through 168 case XA_DATALOCATOR_ANDROIDFD: 169 break; 170 171 default: 172 SL_LOGE("Cannot create media player with data locator type 0x%x", 173 (unsigned) sourceLocatorType); 174 return SL_RESULT_PARAMETER_INVALID; 175 }// switch (locatorType) 176 177 // Audio sink check: only playback is supported here 178 switch(audioSinkLocatorType) { 179 180 case XA_DATALOCATOR_OUTPUTMIX: 181 break; 182 183 default: 184 SL_LOGE("Cannot create media player with audio sink data locator of type 0x%x", 185 (unsigned) audioSinkLocatorType); 186 return XA_RESULT_PARAMETER_INVALID; 187 }// switch (locaaudioSinkLocatorTypeorType) 188 189 return result; 190} 191 192 193//----------------------------------------------------------------------------- 194XAresult android_Player_create(CMediaPlayer *mp) { 195 196 XAresult result = XA_RESULT_SUCCESS; 197 198 // FIXME verify data source 199 const SLDataSource *pDataSrc = &mp->mDataSource.u.mSource; 200 // FIXME verify audio data sink 201 const SLDataSink *pAudioSnk = &mp->mAudioSink.u.mSink; 202 // FIXME verify image data sink 203 const SLDataSink *pVideoSnk = &mp->mImageVideoSink.u.mSink; 204 205 XAuint32 sourceLocator = *(XAuint32 *)pDataSrc->pLocator; 206 switch(sourceLocator) { 207 // FIXME support Android simple buffer queue as well 208 case XA_DATALOCATOR_ANDROIDBUFFERQUEUE: 209 mp->mAndroidObjType = AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE; 210 break; 211 case XA_DATALOCATOR_URI: // intended fall-through 212 case SL_DATALOCATOR_ANDROIDFD: 213 mp->mAndroidObjType = AUDIOVIDEOPLAYER_FROM_URIFD; 214 break; 215 case XA_DATALOCATOR_ADDRESS: // intended fall-through 216 default: 217 SL_LOGE("Unable to create MediaPlayer for data source locator 0x%x", sourceLocator); 218 result = XA_RESULT_PARAMETER_INVALID; 219 break; 220 } 221 222 // FIXME duplicates an initialization also done by higher level 223 mp->mAndroidObjState = ANDROID_UNINITIALIZED; 224 mp->mStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE; 225 mp->mSessionId = android::AudioSystem::newAudioSessionId(); 226 227 mp->mDirectLevel = 0; // no attenuation 228 229 return result; 230} 231 232 233//----------------------------------------------------------------------------- 234// FIXME abstract out the diff between CMediaPlayer and CAudioPlayer 235XAresult android_Player_realize(CMediaPlayer *mp, SLboolean async) { 236 SL_LOGI("android_Player_realize_l(%p)", mp); 237 XAresult result = XA_RESULT_SUCCESS; 238 239 const SLDataSource *pDataSrc = &mp->mDataSource.u.mSource; 240 const SLuint32 sourceLocator = *(SLuint32 *)pDataSrc->pLocator; 241 242 AudioPlayback_Parameters ap_params; 243 ap_params.sessionId = mp->mSessionId; 244 ap_params.streamType = mp->mStreamType; 245 ap_params.trackcb = NULL; 246 ap_params.trackcbUser = NULL; 247 248 switch(mp->mAndroidObjType) { 249 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: { 250 mp->mAVPlayer = new android::StreamPlayer(&ap_params, true /*hasVideo*/); 251 mp->mAVPlayer->init(player_handleMediaPlayerEventNotifications, (void*)mp); 252 } 253 break; 254 case AUDIOVIDEOPLAYER_FROM_URIFD: { 255 mp->mAVPlayer = new android::LocAVPlayer(&ap_params, true /*hasVideo*/); 256 mp->mAVPlayer->init(player_handleMediaPlayerEventNotifications, (void*)mp); 257 switch (mp->mDataSource.mLocator.mLocatorType) { 258 case XA_DATALOCATOR_URI: 259 ((android::LocAVPlayer*)mp->mAVPlayer.get())->setDataSource( 260 (const char*)mp->mDataSource.mLocator.mURI.URI); 261 break; 262 case XA_DATALOCATOR_ANDROIDFD: { 263 int64_t offset = (int64_t)mp->mDataSource.mLocator.mFD.offset; 264 ((android::LocAVPlayer*)mp->mAVPlayer.get())->setDataSource( 265 (int)mp->mDataSource.mLocator.mFD.fd, 266 offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ? 267 (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset, 268 (int64_t)mp->mDataSource.mLocator.mFD.length); 269 } 270 break; 271 default: 272 SL_LOGE("Invalid or unsupported data locator type %u for data source", 273 mp->mDataSource.mLocator.mLocatorType); 274 result = XA_RESULT_PARAMETER_INVALID; 275 } 276 } 277 break; 278 case INVALID_TYPE: // intended fall-through 279 default: 280 SL_LOGE("Unable to realize MediaPlayer, invalid internal Android object type"); 281 result = XA_RESULT_PARAMETER_INVALID; 282 break; 283 } 284 285 return result; 286} 287 288//----------------------------------------------------------------------------- 289XAresult android_Player_destroy(CMediaPlayer *mp) { 290 SL_LOGI("android_Player_destroy(%p)", mp); 291 XAresult result = XA_RESULT_SUCCESS; 292 293 if (mp->mAVPlayer != 0) { 294 mp->mAVPlayer.clear(); 295 } 296 297 return result; 298} 299 300//----------------------------------------------------------------------------- 301/** 302 * pre-conditions: gp != 0, surface != 0 303 */ 304XAresult android_Player_setVideoSurface(const android::sp<android::GenericPlayer> &gp, 305 const android::sp<android::Surface> &surface) { 306 XAresult result = XA_RESULT_SUCCESS; 307 308 android::GenericMediaPlayer* gmp = static_cast<android::GenericMediaPlayer*>(gp.get()); 309 gmp->setVideoSurface(surface); 310 311 return result; 312} 313 314 315/** 316 * pre-conditions: gp != 0, surfaceTexture != 0 317 */ 318XAresult android_Player_setVideoSurfaceTexture(const android::sp<android::GenericPlayer> &gp, 319 const android::sp<android::ISurfaceTexture> &surfaceTexture) { 320 XAresult result = XA_RESULT_SUCCESS; 321 322 android::GenericMediaPlayer* gmp = static_cast<android::GenericMediaPlayer*>(gp.get()); 323 gmp->setVideoSurfaceTexture(surfaceTexture); 324 325 return result; 326} 327 328 329XAresult android_Player_getDuration(IPlay *pPlayItf, XAmillisecond *pDurMsec) { 330 XAresult result = XA_RESULT_SUCCESS; 331 CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis; 332 333 switch (avp->mAndroidObjType) { 334 335 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 336 case AUDIOVIDEOPLAYER_FROM_URIFD: { 337 int dur = -1; 338 if (avp->mAVPlayer != 0) { 339 avp->mAVPlayer->getDurationMsec(&dur); 340 } 341 if (dur == ANDROID_UNKNOWN_TIME) { 342 *pDurMsec = XA_TIME_UNKNOWN; 343 } else { 344 *pDurMsec = (XAmillisecond)dur; 345 } 346 } break; 347 348 default: 349 // we shouldn't be here 350 assert(false); 351 break; 352 } 353 354 return result; 355} 356 357 358XAresult android_Player_getPosition(IPlay *pPlayItf, XAmillisecond *pPosMsec) { 359 SL_LOGD("android_Player_getPosition()"); 360 XAresult result = XA_RESULT_SUCCESS; 361 CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis; 362 363 switch (avp->mAndroidObjType) { 364 365 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 366 case AUDIOVIDEOPLAYER_FROM_URIFD: { 367 int pos = -1; 368 if (avp->mAVPlayer != 0) { 369 avp->mAVPlayer->getPositionMsec(&pos); 370 } 371 if (pos == ANDROID_UNKNOWN_TIME) { 372 *pPosMsec = XA_TIME_UNKNOWN; 373 } else { 374 *pPosMsec = (XAmillisecond)pos; 375 } 376 } break; 377 378 default: 379 // we shouldn't be here 380 assert(false); 381 break; 382 } 383 384 return result; 385} 386 387 388//----------------------------------------------------------------------------- 389/** 390 * pre-condition: avp != 0, pVolItf != NULL 391 */ 392XAresult android_Player_volumeUpdate(const android::sp<android::GenericPlayer> &gp, 393 IVolume *pVolItf) 394{ 395 XAresult result = XA_RESULT_SUCCESS; 396 397 gp->setVolume((bool)pVolItf->mMute, (bool)pVolItf->mEnableStereoPosition, 398 pVolItf->mStereoPosition, pVolItf->mLevel); 399 400 return result; 401} 402 403//----------------------------------------------------------------------------- 404/** 405 * pre-condition: gp != 0 406 */ 407XAresult android_Player_setPlayState(const android::sp<android::GenericPlayer> &gp, 408 SLuint32 playState, 409 AndroidObjectState* pObjState) 410{ 411 XAresult result = XA_RESULT_SUCCESS; 412 AndroidObjectState objState = *pObjState; 413 414 switch (playState) { 415 case SL_PLAYSTATE_STOPPED: { 416 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_STOPPED"); 417 gp->stop(); 418 } 419 break; 420 case SL_PLAYSTATE_PAUSED: { 421 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PAUSED"); 422 switch(objState) { 423 case ANDROID_UNINITIALIZED: 424 *pObjState = ANDROID_PREPARING; 425 gp->prepare(); 426 break; 427 case ANDROID_PREPARING: 428 break; 429 case ANDROID_READY: 430 gp->pause(); 431 break; 432 default: 433 SL_LOGE("Android object in invalid state"); 434 break; 435 } 436 } 437 break; 438 case SL_PLAYSTATE_PLAYING: { 439 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PLAYING"); 440 switch(objState) { 441 case ANDROID_UNINITIALIZED: 442 *pObjState = ANDROID_PREPARING; 443 gp->prepare(); 444 // intended fall through 445 case ANDROID_PREPARING: 446 // intended fall through 447 case ANDROID_READY: 448 gp->play(); 449 break; 450 default: 451 SL_LOGE("Android object in invalid state"); 452 break; 453 } 454 } 455 break; 456 default: 457 // checked by caller, should not happen 458 break; 459 } 460 461 return result; 462} 463 464 465/** 466 * pre-condition: mp != NULL 467 */ 468XAresult android_Player_seek(CMediaPlayer *mp, SLmillisecond posMsec) { 469 XAresult result = XA_RESULT_SUCCESS; 470 switch (mp->mAndroidObjType) { 471 case AUDIOVIDEOPLAYER_FROM_URIFD: 472 if (mp->mAVPlayer !=0) { 473 mp->mAVPlayer->seek(posMsec); 474 } 475 break; 476 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 477 default: { 478 result = XA_RESULT_PARAMETER_INVALID; 479 } 480 } 481 return result; 482} 483 484 485/** 486 * pre-condition: mp != NULL 487 */ 488XAresult android_Player_loop(CMediaPlayer *mp, SLboolean loopEnable) { 489 XAresult result = XA_RESULT_SUCCESS; 490 switch (mp->mAndroidObjType) { 491 case AUDIOVIDEOPLAYER_FROM_URIFD: 492 if (mp->mAVPlayer !=0) { 493 mp->mAVPlayer->loop(loopEnable); 494 } 495 break; 496 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 497 default: { 498 result = XA_RESULT_PARAMETER_INVALID; 499 } 500 } 501 return result; 502} 503 504 505//----------------------------------------------------------------------------- 506void android_Player_androidBufferQueue_registerCallback_l(CMediaPlayer *mp) { 507 if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) 508 && (mp->mAVPlayer != 0)) { 509 SL_LOGD("android_Player_androidBufferQueue_registerCallback_l"); 510 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get()); 511 splr->registerQueueCallback( 512 (const void*)mp, false /*userIsAudioPlayer*/, 513 mp->mAndroidBufferQueue.mContext, (const void*)&(mp->mAndroidBufferQueue.mItf)); 514 515 } 516} 517 518 519void android_Player_androidBufferQueue_clear_l(CMediaPlayer *mp) { 520 if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) 521 && (mp->mAVPlayer != 0)) { 522 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get()); 523 splr->appClear_l(); 524 } 525} 526 527 528void android_Player_androidBufferQueue_onRefilled_l(CMediaPlayer *mp) { 529 if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) 530 && (mp->mAVPlayer != 0)) { 531 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get()); 532 splr->queueRefilled_l(); 533 } 534} 535 536 537 538