MediaPlayer_to_android.cpp revision f731cc1f3cd5911b0aff730bd504ee47a3ba3c6c
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 case android::GenericPlayer::kEventChannelCount: { 127 SL_LOGV("kEventChannelCount channels = %d", data1); 128 object_lock_exclusive(&mp->mObject); 129 if (UNKNOWN_NUMCHANNELS == mp->mNumChannels && UNKNOWN_NUMCHANNELS != data1) { 130 mp->mNumChannels = data1; 131 android_Player_volumeUpdate(mp); 132 } 133 object_unlock_exclusive(&mp->mObject); 134 } 135 break; 136 137 case android::GenericPlayer::kEventPrefetchFillLevelUpdate: { 138 SL_LOGV("kEventPrefetchFillLevelUpdate"); 139 } 140 break; 141 142 case android::GenericPlayer::kEventPrefetchStatusChange: { 143 SL_LOGV("kEventPrefetchStatusChange"); 144 } 145 break; 146 147 148 default: { 149 SL_LOGE("Received unknown event %d, data %d from AVPlayer", event, data1); 150 } 151 } 152} 153 154 155//----------------------------------------------------------------------------- 156XAresult android_Player_checkSourceSink(CMediaPlayer *mp) { 157 158 XAresult result = XA_RESULT_SUCCESS; 159 160 const SLDataSource *pSrc = &mp->mDataSource.u.mSource; 161 const SLDataSink *pAudioSnk = &mp->mAudioSink.u.mSink; 162 163 // format check: 164 const SLuint32 sourceLocatorType = *(SLuint32 *)pSrc->pLocator; 165 const SLuint32 sourceFormatType = *(SLuint32 *)pSrc->pFormat; 166 const SLuint32 audioSinkLocatorType = *(SLuint32 *)pAudioSnk->pLocator; 167 //const SLuint32 sinkFormatType = *(SLuint32 *)pAudioSnk->pFormat; 168 169 // Source check 170 switch(sourceLocatorType) { 171 172 case XA_DATALOCATOR_ANDROIDBUFFERQUEUE: { 173 switch (sourceFormatType) { 174 case XA_DATAFORMAT_MIME: { 175 SLDataFormat_MIME *df_mime = (SLDataFormat_MIME *) pSrc->pFormat; 176 if (SL_CONTAINERTYPE_MPEG_TS != df_mime->containerType) { 177 SL_LOGE("Cannot create player with XA_DATALOCATOR_ANDROIDBUFFERQUEUE data source " 178 "that is not fed MPEG-2 TS data"); 179 return SL_RESULT_CONTENT_UNSUPPORTED; 180 } 181 } break; 182 default: 183 SL_LOGE("Cannot create player with XA_DATALOCATOR_ANDROIDBUFFERQUEUE data source " 184 "without SL_DATAFORMAT_MIME format"); 185 return XA_RESULT_CONTENT_UNSUPPORTED; 186 } 187 } break; 188 189 case XA_DATALOCATOR_URI: // intended fall-through 190 case XA_DATALOCATOR_ANDROIDFD: 191 break; 192 193 default: 194 SL_LOGE("Cannot create media player with data locator type 0x%x", 195 (unsigned) sourceLocatorType); 196 return SL_RESULT_PARAMETER_INVALID; 197 }// switch (locatorType) 198 199 // Audio sink check: only playback is supported here 200 switch(audioSinkLocatorType) { 201 202 case XA_DATALOCATOR_OUTPUTMIX: 203 break; 204 205 default: 206 SL_LOGE("Cannot create media player with audio sink data locator of type 0x%x", 207 (unsigned) audioSinkLocatorType); 208 return XA_RESULT_PARAMETER_INVALID; 209 }// switch (locaaudioSinkLocatorTypeorType) 210 211 return result; 212} 213 214 215//----------------------------------------------------------------------------- 216XAresult android_Player_create(CMediaPlayer *mp) { 217 218 XAresult result = XA_RESULT_SUCCESS; 219 220 // FIXME verify data source 221 const SLDataSource *pDataSrc = &mp->mDataSource.u.mSource; 222 // FIXME verify audio data sink 223 const SLDataSink *pAudioSnk = &mp->mAudioSink.u.mSink; 224 // FIXME verify image data sink 225 const SLDataSink *pVideoSnk = &mp->mImageVideoSink.u.mSink; 226 227 XAuint32 sourceLocator = *(XAuint32 *)pDataSrc->pLocator; 228 switch(sourceLocator) { 229 // FIXME support Android simple buffer queue as well 230 case XA_DATALOCATOR_ANDROIDBUFFERQUEUE: 231 mp->mAndroidObjType = AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE; 232 break; 233 case XA_DATALOCATOR_URI: // intended fall-through 234 case SL_DATALOCATOR_ANDROIDFD: 235 mp->mAndroidObjType = AUDIOVIDEOPLAYER_FROM_URIFD; 236 break; 237 case XA_DATALOCATOR_ADDRESS: // intended fall-through 238 default: 239 SL_LOGE("Unable to create MediaPlayer for data source locator 0x%x", sourceLocator); 240 result = XA_RESULT_PARAMETER_INVALID; 241 break; 242 } 243 244 // FIXME duplicates an initialization also done by higher level 245 mp->mAndroidObjState = ANDROID_UNINITIALIZED; 246 mp->mStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE; 247 mp->mSessionId = android::AudioSystem::newAudioSessionId(); 248 249 return result; 250} 251 252 253//----------------------------------------------------------------------------- 254// FIXME abstract out the diff between CMediaPlayer and CAudioPlayer 255XAresult android_Player_realize(CMediaPlayer *mp, SLboolean async) { 256 SL_LOGV("android_Player_realize_l(%p)", mp); 257 XAresult result = XA_RESULT_SUCCESS; 258 259 const SLDataSource *pDataSrc = &mp->mDataSource.u.mSource; 260 const SLuint32 sourceLocator = *(SLuint32 *)pDataSrc->pLocator; 261 262 AudioPlayback_Parameters ap_params; 263 ap_params.sessionId = mp->mSessionId; 264 ap_params.streamType = mp->mStreamType; 265 ap_params.trackcb = NULL; 266 ap_params.trackcbUser = NULL; 267 268 switch(mp->mAndroidObjType) { 269 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: { 270 mp->mAVPlayer = new android::StreamPlayer(&ap_params, true /*hasVideo*/); 271 mp->mAVPlayer->init(player_handleMediaPlayerEventNotifications, (void*)mp); 272 } 273 break; 274 case AUDIOVIDEOPLAYER_FROM_URIFD: { 275 mp->mAVPlayer = new android::LocAVPlayer(&ap_params, true /*hasVideo*/); 276 mp->mAVPlayer->init(player_handleMediaPlayerEventNotifications, (void*)mp); 277 switch (mp->mDataSource.mLocator.mLocatorType) { 278 case XA_DATALOCATOR_URI: 279 ((android::LocAVPlayer*)mp->mAVPlayer.get())->setDataSource( 280 (const char*)mp->mDataSource.mLocator.mURI.URI); 281 break; 282 case XA_DATALOCATOR_ANDROIDFD: { 283 int64_t offset = (int64_t)mp->mDataSource.mLocator.mFD.offset; 284 ((android::LocAVPlayer*)mp->mAVPlayer.get())->setDataSource( 285 (int)mp->mDataSource.mLocator.mFD.fd, 286 offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ? 287 (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset, 288 (int64_t)mp->mDataSource.mLocator.mFD.length); 289 } 290 break; 291 default: 292 SL_LOGE("Invalid or unsupported data locator type %u for data source", 293 mp->mDataSource.mLocator.mLocatorType); 294 result = XA_RESULT_PARAMETER_INVALID; 295 } 296 } 297 break; 298 case INVALID_TYPE: // intended fall-through 299 default: 300 SL_LOGE("Unable to realize MediaPlayer, invalid internal Android object type"); 301 result = XA_RESULT_PARAMETER_INVALID; 302 break; 303 } 304 305 return result; 306} 307 308//----------------------------------------------------------------------------- 309XAresult android_Player_destroy(CMediaPlayer *mp) { 310 SL_LOGV("android_Player_destroy(%p)", mp); 311 XAresult result = XA_RESULT_SUCCESS; 312 313 if (mp->mAVPlayer != 0) { 314 mp->mAVPlayer.clear(); 315 } 316 317 return result; 318} 319 320//----------------------------------------------------------------------------- 321/** 322 * pre-conditions: gp != 0, surface != 0 323 */ 324XAresult android_Player_setVideoSurface(const android::sp<android::GenericPlayer> &gp, 325 const android::sp<android::Surface> &surface) { 326 XAresult result = XA_RESULT_SUCCESS; 327 328 android::GenericMediaPlayer* gmp = static_cast<android::GenericMediaPlayer*>(gp.get()); 329 gmp->setVideoSurface(surface); 330 331 return result; 332} 333 334 335/** 336 * pre-conditions: gp != 0, surfaceTexture != 0 337 */ 338XAresult android_Player_setVideoSurfaceTexture(const android::sp<android::GenericPlayer> &gp, 339 const android::sp<android::ISurfaceTexture> &surfaceTexture) { 340 XAresult result = XA_RESULT_SUCCESS; 341 342 android::GenericMediaPlayer* gmp = static_cast<android::GenericMediaPlayer*>(gp.get()); 343 gmp->setVideoSurfaceTexture(surfaceTexture); 344 345 return result; 346} 347 348 349XAresult android_Player_getDuration(IPlay *pPlayItf, XAmillisecond *pDurMsec) { 350 XAresult result = XA_RESULT_SUCCESS; 351 CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis; 352 353 switch (avp->mAndroidObjType) { 354 355 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 356 case AUDIOVIDEOPLAYER_FROM_URIFD: { 357 int dur = -1; 358 if (avp->mAVPlayer != 0) { 359 avp->mAVPlayer->getDurationMsec(&dur); 360 } 361 if (dur == ANDROID_UNKNOWN_TIME) { 362 *pDurMsec = XA_TIME_UNKNOWN; 363 } else { 364 *pDurMsec = (XAmillisecond)dur; 365 } 366 } break; 367 368 default: 369 // we shouldn't be here 370 assert(false); 371 break; 372 } 373 374 return result; 375} 376 377 378XAresult android_Player_getPosition(IPlay *pPlayItf, XAmillisecond *pPosMsec) { 379 SL_LOGD("android_Player_getPosition()"); 380 XAresult result = XA_RESULT_SUCCESS; 381 CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis; 382 383 switch (avp->mAndroidObjType) { 384 385 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 386 case AUDIOVIDEOPLAYER_FROM_URIFD: { 387 int pos = -1; 388 if (avp->mAVPlayer != 0) { 389 avp->mAVPlayer->getPositionMsec(&pos); 390 } 391 if (pos == ANDROID_UNKNOWN_TIME) { 392 *pPosMsec = XA_TIME_UNKNOWN; 393 } else { 394 *pPosMsec = (XAmillisecond)pos; 395 } 396 } break; 397 398 default: 399 // we shouldn't be here 400 assert(false); 401 break; 402 } 403 404 return result; 405} 406 407 408//----------------------------------------------------------------------------- 409/** 410 * pre-condition: mp != NULL 411 */ 412void android_Player_volumeUpdate(CMediaPlayer* mp) 413{ 414 android::GenericPlayer* avp = mp->mAVPlayer.get(); 415 if (avp != NULL) { 416 float volumes[2]; 417 // MediaPlayer does not currently support EffectSend or MuteSolo 418 android_player_volumeUpdate(volumes, &mp->mVolume, mp->mNumChannels, 1.0f, NULL); 419 float leftVol = volumes[0], rightVol = volumes[1]; 420 avp->setVolume(leftVol, rightVol); 421 } 422} 423 424//----------------------------------------------------------------------------- 425/** 426 * pre-condition: gp != 0 427 */ 428XAresult android_Player_setPlayState(const android::sp<android::GenericPlayer> &gp, 429 SLuint32 playState, 430 AndroidObjectState* pObjState) 431{ 432 XAresult result = XA_RESULT_SUCCESS; 433 AndroidObjectState objState = *pObjState; 434 435 switch (playState) { 436 case SL_PLAYSTATE_STOPPED: { 437 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_STOPPED"); 438 gp->stop(); 439 } 440 break; 441 case SL_PLAYSTATE_PAUSED: { 442 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PAUSED"); 443 switch(objState) { 444 case ANDROID_UNINITIALIZED: 445 *pObjState = ANDROID_PREPARING; 446 gp->prepare(); 447 break; 448 case ANDROID_PREPARING: 449 break; 450 case ANDROID_READY: 451 gp->pause(); 452 break; 453 default: 454 SL_LOGE("Android object in invalid state"); 455 break; 456 } 457 } 458 break; 459 case SL_PLAYSTATE_PLAYING: { 460 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PLAYING"); 461 switch(objState) { 462 case ANDROID_UNINITIALIZED: 463 *pObjState = ANDROID_PREPARING; 464 gp->prepare(); 465 // intended fall through 466 case ANDROID_PREPARING: 467 // intended fall through 468 case ANDROID_READY: 469 gp->play(); 470 break; 471 default: 472 SL_LOGE("Android object in invalid state"); 473 break; 474 } 475 } 476 break; 477 default: 478 // checked by caller, should not happen 479 break; 480 } 481 482 return result; 483} 484 485 486/** 487 * pre-condition: mp != NULL 488 */ 489XAresult android_Player_seek(CMediaPlayer *mp, SLmillisecond posMsec) { 490 XAresult result = XA_RESULT_SUCCESS; 491 switch (mp->mAndroidObjType) { 492 case AUDIOVIDEOPLAYER_FROM_URIFD: 493 if (mp->mAVPlayer !=0) { 494 mp->mAVPlayer->seek(posMsec); 495 } 496 break; 497 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 498 default: { 499 result = XA_RESULT_PARAMETER_INVALID; 500 } 501 } 502 return result; 503} 504 505 506/** 507 * pre-condition: mp != NULL 508 */ 509XAresult android_Player_loop(CMediaPlayer *mp, SLboolean loopEnable) { 510 XAresult result = XA_RESULT_SUCCESS; 511 switch (mp->mAndroidObjType) { 512 case AUDIOVIDEOPLAYER_FROM_URIFD: 513 if (mp->mAVPlayer !=0) { 514 mp->mAVPlayer->loop(loopEnable); 515 } 516 break; 517 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 518 default: { 519 result = XA_RESULT_PARAMETER_INVALID; 520 } 521 } 522 return result; 523} 524 525 526//----------------------------------------------------------------------------- 527void android_Player_androidBufferQueue_registerCallback_l(CMediaPlayer *mp) { 528 if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) 529 && (mp->mAVPlayer != 0)) { 530 SL_LOGD("android_Player_androidBufferQueue_registerCallback_l"); 531 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get()); 532 splr->registerQueueCallback( 533 (const void*)mp, false /*userIsAudioPlayer*/, 534 mp->mAndroidBufferQueue.mContext, (const void*)&(mp->mAndroidBufferQueue.mItf)); 535 536 } 537} 538 539 540void android_Player_androidBufferQueue_clear_l(CMediaPlayer *mp) { 541 if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) 542 && (mp->mAVPlayer != 0)) { 543 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get()); 544 splr->appClear_l(); 545 } 546} 547 548 549void android_Player_androidBufferQueue_onRefilled_l(CMediaPlayer *mp) { 550 if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) 551 && (mp->mAVPlayer != 0)) { 552 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get()); 553 splr->queueRefilled_l(); 554 } 555} 556 557 558 559