MediaPlayer_to_android.cpp revision a8179ea15c4ff78db589d742b135649f0eda7ef2
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 // FIXME implement for a MediaPlayer playing on URI or FD (on LocAVPlayer, returns -1) 320 int dur = -1; 321 if (avp->mAVPlayer != 0) { 322 avp->mAVPlayer->getDurationMsec(&dur); 323 } 324 if (dur < 0) { 325 *pDurMsec = SL_TIME_UNKNOWN; 326 } else { 327 *pDurMsec = (XAmillisecond)dur; 328 } 329 } break; 330 331 default: 332 *pDurMsec = XA_TIME_UNKNOWN; 333 break; 334 } 335 336 return result; 337} 338 339 340//----------------------------------------------------------------------------- 341/** 342 * pre-condition: avp != 0, pVolItf != NULL 343 */ 344XAresult android_Player_volumeUpdate(const android::sp<android::GenericPlayer> &gp, 345 IVolume *pVolItf) 346{ 347 XAresult result = XA_RESULT_SUCCESS; 348 349 gp->setVolume((bool)pVolItf->mMute, (bool)pVolItf->mEnableStereoPosition, 350 pVolItf->mStereoPosition, pVolItf->mLevel); 351 352 return result; 353} 354 355//----------------------------------------------------------------------------- 356/** 357 * pre-condition: gp != 0 358 */ 359XAresult android_Player_setPlayState(const android::sp<android::GenericPlayer> &gp, 360 SLuint32 playState, 361 AndroidObjectState* pObjState) 362{ 363 XAresult result = XA_RESULT_SUCCESS; 364 AndroidObjectState objState = *pObjState; 365 366 switch (playState) { 367 case SL_PLAYSTATE_STOPPED: { 368 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_STOPPED"); 369 gp->stop(); 370 } 371 break; 372 case SL_PLAYSTATE_PAUSED: { 373 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PAUSED"); 374 switch(objState) { 375 case ANDROID_UNINITIALIZED: 376 *pObjState = ANDROID_PREPARING; 377 gp->prepare(); 378 break; 379 case ANDROID_PREPARING: 380 break; 381 case ANDROID_READY: 382 gp->pause(); 383 break; 384 default: 385 SL_LOGE("Android object in invalid state"); 386 break; 387 } 388 } 389 break; 390 case SL_PLAYSTATE_PLAYING: { 391 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PLAYING"); 392 switch(objState) { 393 case ANDROID_UNINITIALIZED: 394 *pObjState = ANDROID_PREPARING; 395 gp->prepare(); 396 // intended fall through 397 case ANDROID_PREPARING: 398 // intended fall through 399 case ANDROID_READY: 400 gp->play(); 401 break; 402 default: 403 SL_LOGE("Android object in invalid state"); 404 break; 405 } 406 } 407 break; 408 default: 409 // checked by caller, should not happen 410 break; 411 } 412 413 return result; 414} 415 416 417/** 418 * pre-condition: mp != NULL 419 */ 420XAresult android_Player_seek(CMediaPlayer *mp, SLmillisecond posMsec) { 421 XAresult result = XA_RESULT_SUCCESS; 422 switch (mp->mAndroidObjType) { 423 case AUDIOVIDEOPLAYER_FROM_URIFD: 424 if (mp->mAVPlayer !=0) { 425 mp->mAVPlayer->seek(posMsec); 426 } 427 break; 428 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 429 default: { 430 result = XA_RESULT_PARAMETER_INVALID; 431 } 432 } 433 return result; 434} 435 436 437/** 438 * pre-condition: mp != NULL 439 */ 440XAresult android_Player_loop(CMediaPlayer *mp, SLboolean loopEnable) { 441 XAresult result = XA_RESULT_SUCCESS; 442 switch (mp->mAndroidObjType) { 443 case AUDIOVIDEOPLAYER_FROM_URIFD: 444 if (mp->mAVPlayer !=0) { 445 mp->mAVPlayer->loop(loopEnable); 446 } 447 break; 448 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through 449 default: { 450 result = XA_RESULT_PARAMETER_INVALID; 451 } 452 } 453 return result; 454} 455 456 457//----------------------------------------------------------------------------- 458void android_Player_androidBufferQueue_registerCallback_l(CMediaPlayer *mp) { 459 if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) 460 && (mp->mAVPlayer != 0)) { 461 SL_LOGD("android_Player_androidBufferQueue_registerCallback_l"); 462 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get()); 463 splr->registerQueueCallback( 464 (const void*)mp, false /*userIsAudioPlayer*/, 465 mp->mAndroidBufferQueue.mContext, (const void*)&(mp->mAndroidBufferQueue.mItf)); 466 467 } 468} 469 470 471void android_Player_androidBufferQueue_clear_l(CMediaPlayer *mp) { 472 if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) 473 && (mp->mAVPlayer != 0)) { 474 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get()); 475 splr->appClear_l(); 476 } 477} 478 479 480void android_Player_androidBufferQueue_onRefilled_l(CMediaPlayer *mp) { 481 if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) 482 && (mp->mAVPlayer != 0)) { 483 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get()); 484 splr->queueRefilled_l(); 485 } 486} 487 488 489 490