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