MediaPlayer_to_android.cpp revision 85133c817f6f387cd7d072988a8818f18bb53702
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 mp->mAndroidObjState = ANDROID_UNINITIALIZED; 223 mp->mStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE; 224 mp->mSessionId = android::AudioSystem::newAudioSessionId(); 225 226 mp->mDirectLevel = 0; // no attenuation 227 228 return result; 229} 230 231 232//----------------------------------------------------------------------------- 233// FIXME abstract out the diff between CMediaPlayer and CAudioPlayer 234XAresult android_Player_realize(CMediaPlayer *mp, SLboolean async) { 235 SL_LOGI("android_Player_realize_l(%p)", mp); 236 XAresult result = XA_RESULT_SUCCESS; 237 238 const SLDataSource *pDataSrc = &mp->mDataSource.u.mSource; 239 const SLuint32 sourceLocator = *(SLuint32 *)pDataSrc->pLocator; 240 241 AudioPlayback_Parameters ap_params; 242 ap_params.sessionId = mp->mSessionId; 243 ap_params.streamType = mp->mStreamType; 244 ap_params.trackcb = NULL; 245 ap_params.trackcbUser = NULL; 246 247 switch(mp->mAndroidObjType) { 248 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: { 249 mp->mAVPlayer = new android::StreamPlayer(&ap_params, true /*hasVideo*/); 250 mp->mAVPlayer->init(player_handleMediaPlayerEventNotifications, (void*)mp); 251 } 252 break; 253 case AUDIOVIDEOPLAYER_FROM_URIFD: { 254 mp->mAVPlayer = new android::LocAVPlayer(&ap_params, true /*hasVideo*/); 255 mp->mAVPlayer->init(player_handleMediaPlayerEventNotifications, (void*)mp); 256 switch (mp->mDataSource.mLocator.mLocatorType) { 257 case XA_DATALOCATOR_URI: 258 ((android::LocAVPlayer*)mp->mAVPlayer.get())->setDataSource( 259 (const char*)mp->mDataSource.mLocator.mURI.URI); 260 break; 261 case XA_DATALOCATOR_ANDROIDFD: { 262 int64_t offset = (int64_t)mp->mDataSource.mLocator.mFD.offset; 263 ((android::LocAVPlayer*)mp->mAVPlayer.get())->setDataSource( 264 (int)mp->mDataSource.mLocator.mFD.fd, 265 offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ? 266 (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset, 267 (int64_t)mp->mDataSource.mLocator.mFD.length); 268 } 269 break; 270 default: 271 SL_LOGE("Invalid or unsupported data locator type %u for data source", 272 mp->mDataSource.mLocator.mLocatorType); 273 result = XA_RESULT_PARAMETER_INVALID; 274 } 275 } 276 break; 277 case INVALID_TYPE: // intended fall-through 278 default: 279 SL_LOGE("Unable to realize MediaPlayer, invalid internal Android object type"); 280 result = XA_RESULT_PARAMETER_INVALID; 281 break; 282 } 283 284 return result; 285} 286 287//----------------------------------------------------------------------------- 288XAresult android_Player_destroy(CMediaPlayer *mp) { 289 SL_LOGI("android_Player_destroy(%p)", mp); 290 XAresult result = XA_RESULT_SUCCESS; 291 292 if (mp->mAVPlayer != 0) { 293 mp->mAVPlayer.clear(); 294 } 295 296 return result; 297} 298 299//----------------------------------------------------------------------------- 300/** 301 * pre-conditions: gp != 0, surface != 0 302 */ 303XAresult android_Player_setVideoSurface(const android::sp<android::GenericPlayer> &gp, 304 const android::sp<android::Surface> &surface) { 305 XAresult result = XA_RESULT_SUCCESS; 306 307 android::GenericMediaPlayer* gmp = static_cast<android::GenericMediaPlayer*>(gp.get()); 308 gmp->setVideoSurface(surface); 309 310 return result; 311} 312 313 314/** 315 * pre-conditions: gp != 0, surfaceTexture != 0 316 */ 317XAresult android_Player_setVideoSurfaceTexture(const android::sp<android::GenericPlayer> &gp, 318 const android::sp<android::ISurfaceTexture> &surfaceTexture) { 319 XAresult result = XA_RESULT_SUCCESS; 320 321 android::GenericMediaPlayer* gmp = static_cast<android::GenericMediaPlayer*>(gp.get()); 322 gmp->setVideoSurfaceTexture(surfaceTexture); 323 324 return result; 325} 326 327 328XAresult android_Player_getDuration(IPlay *pPlayItf, XAmillisecond *pDurMsec) { 329 XAresult result = XA_RESULT_SUCCESS; 330 CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis; 331 332 switch (avp->mAndroidObjType) { 333 334 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 335 case AUDIOVIDEOPLAYER_FROM_URIFD: { 336 int dur = -1; 337 if (avp->mAVPlayer != 0) { 338 avp->mAVPlayer->getDurationMsec(&dur); 339 } 340 if (dur == ANDROID_UNKNOWN_TIME) { 341 *pDurMsec = XA_TIME_UNKNOWN; 342 } else { 343 *pDurMsec = (XAmillisecond)dur; 344 } 345 } break; 346 347 default: 348 // we shouldn't be here 349 assert(false); 350 break; 351 } 352 353 return result; 354} 355 356 357XAresult android_Player_getPosition(IPlay *pPlayItf, XAmillisecond *pPosMsec) { 358 SL_LOGD("android_Player_getPosition()"); 359 XAresult result = XA_RESULT_SUCCESS; 360 CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis; 361 362 switch (avp->mAndroidObjType) { 363 364 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 365 case AUDIOVIDEOPLAYER_FROM_URIFD: { 366 int pos = -1; 367 if (avp->mAVPlayer != 0) { 368 avp->mAVPlayer->getPositionMsec(&pos); 369 } 370 if (pos == ANDROID_UNKNOWN_TIME) { 371 *pPosMsec = XA_TIME_UNKNOWN; 372 } else { 373 *pPosMsec = (XAmillisecond)pos; 374 } 375 } break; 376 377 default: 378 // we shouldn't be here 379 assert(false); 380 break; 381 } 382 383 return result; 384} 385 386 387//----------------------------------------------------------------------------- 388/** 389 * pre-condition: avp != 0, pVolItf != NULL 390 */ 391XAresult android_Player_volumeUpdate(const android::sp<android::GenericPlayer> &gp, 392 IVolume *pVolItf) 393{ 394 XAresult result = XA_RESULT_SUCCESS; 395 396 gp->setVolume((bool)pVolItf->mMute, (bool)pVolItf->mEnableStereoPosition, 397 pVolItf->mStereoPosition, pVolItf->mLevel); 398 399 return result; 400} 401 402//----------------------------------------------------------------------------- 403/** 404 * pre-condition: gp != 0 405 */ 406XAresult android_Player_setPlayState(const android::sp<android::GenericPlayer> &gp, 407 SLuint32 playState, 408 AndroidObjectState* pObjState) 409{ 410 XAresult result = XA_RESULT_SUCCESS; 411 AndroidObjectState objState = *pObjState; 412 413 switch (playState) { 414 case SL_PLAYSTATE_STOPPED: { 415 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_STOPPED"); 416 gp->stop(); 417 } 418 break; 419 case SL_PLAYSTATE_PAUSED: { 420 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PAUSED"); 421 switch(objState) { 422 case ANDROID_UNINITIALIZED: 423 *pObjState = ANDROID_PREPARING; 424 gp->prepare(); 425 break; 426 case ANDROID_PREPARING: 427 break; 428 case ANDROID_READY: 429 gp->pause(); 430 break; 431 default: 432 SL_LOGE("Android object in invalid state"); 433 break; 434 } 435 } 436 break; 437 case SL_PLAYSTATE_PLAYING: { 438 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PLAYING"); 439 switch(objState) { 440 case ANDROID_UNINITIALIZED: 441 *pObjState = ANDROID_PREPARING; 442 gp->prepare(); 443 // intended fall through 444 case ANDROID_PREPARING: 445 // intended fall through 446 case ANDROID_READY: 447 gp->play(); 448 break; 449 default: 450 SL_LOGE("Android object in invalid state"); 451 break; 452 } 453 } 454 break; 455 default: 456 // checked by caller, should not happen 457 break; 458 } 459 460 return result; 461} 462 463 464/** 465 * pre-condition: mp != NULL 466 */ 467XAresult android_Player_seek(CMediaPlayer *mp, SLmillisecond posMsec) { 468 XAresult result = XA_RESULT_SUCCESS; 469 switch (mp->mAndroidObjType) { 470 case AUDIOVIDEOPLAYER_FROM_URIFD: 471 if (mp->mAVPlayer !=0) { 472 mp->mAVPlayer->seek(posMsec); 473 } 474 break; 475 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 476 default: { 477 result = XA_RESULT_PARAMETER_INVALID; 478 } 479 } 480 return result; 481} 482 483 484/** 485 * pre-condition: mp != NULL 486 */ 487XAresult android_Player_loop(CMediaPlayer *mp, SLboolean loopEnable) { 488 XAresult result = XA_RESULT_SUCCESS; 489 switch (mp->mAndroidObjType) { 490 case AUDIOVIDEOPLAYER_FROM_URIFD: 491 if (mp->mAVPlayer !=0) { 492 mp->mAVPlayer->loop(loopEnable); 493 } 494 break; 495 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 496 default: { 497 result = XA_RESULT_PARAMETER_INVALID; 498 } 499 } 500 return result; 501} 502 503 504//----------------------------------------------------------------------------- 505void android_Player_androidBufferQueue_registerCallback_l(CMediaPlayer *mp) { 506 if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) 507 && (mp->mAVPlayer != 0)) { 508 SL_LOGD("android_Player_androidBufferQueue_registerCallback_l"); 509 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get()); 510 splr->registerQueueCallback( 511 (const void*)mp, false /*userIsAudioPlayer*/, 512 mp->mAndroidBufferQueue.mContext, (const void*)&(mp->mAndroidBufferQueue.mItf)); 513 514 } 515} 516 517 518void android_Player_androidBufferQueue_clear_l(CMediaPlayer *mp) { 519 if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) 520 && (mp->mAVPlayer != 0)) { 521 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get()); 522 splr->appClear_l(); 523 } 524} 525 526 527void android_Player_androidBufferQueue_onRefilled_l(CMediaPlayer *mp) { 528 if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) 529 && (mp->mAVPlayer != 0)) { 530 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get()); 531 splr->queueRefilled_l(); 532 } 533} 534 535 536 537