2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
26 //-----------------------------------------------------------------------------
27 static void player_handleMediaPlayerEventNotifications(int event, int data1, int data2, void* user)
33 CMediaPlayer* mp = (CMediaPlayer*) user;
34 SL_LOGV("received event %d, data %d from AVPlayer", event, data1);
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);
48 case android::GenericPlayer::kEventHasVideoSize: {
49 SL_LOGV("Received AVPlayer::kEventHasVideoSize (%d,%d) for CMediaPlayer %p",
52 object_lock_exclusive(&mp->mObject);
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);
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);
73 xaStreamEventChangeCallback callback = mp->mStreamInfo.mCallback;
74 void* callbackPContext = mp->mStreamInfo.mContext;
76 object_unlock_exclusive(&mp->mObject);
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*/);
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*/);
97 case android::GenericPlayer::kEventEndOfStream: {
98 SL_LOGV("Received AVPlayer::kEventEndOfStream for CMediaPlayer %p", mp);
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;
109 object_unlock_exclusive(&mp->mObject);
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);
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);
127 SL_LOGE("Received unknown event %d, data %d from AVPlayer", event, data1);
133 //-----------------------------------------------------------------------------
134 XAresult android_Player_checkSourceSink(CMediaPlayer *mp) {
136 XAresult result = XA_RESULT_SUCCESS;
138 const SLDataSource *pSrc = &mp->mDataSource.u.mSource;
139 const SLDataSink *pAudioSnk = &mp->mAudioSink.u.mSink;
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;
148 switch(sourceLocatorType) {
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;
161 SL_LOGE("Cannot create player with XA_DATALOCATOR_ANDROIDBUFFERQUEUE data source "
162 "without SL_DATAFORMAT_MIME format");
163 return XA_RESULT_CONTENT_UNSUPPORTED;
167 case XA_DATALOCATOR_URI: // intended fall-through
168 case XA_DATALOCATOR_ANDROIDFD:
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)
177 // Audio sink check: only playback is supported here
178 switch(audioSinkLocatorType) {
180 case XA_DATALOCATOR_OUTPUTMIX:
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)
193 //-----------------------------------------------------------------------------
194 XAresult android_Player_create(CMediaPlayer *mp) {
196 XAresult result = XA_RESULT_SUCCESS;
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;
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;
211 case XA_DATALOCATOR_URI: // intended fall-through
212 case SL_DATALOCATOR_ANDROIDFD:
213 mp->mAndroidObjType = AUDIOVIDEOPLAYER_FROM_URIFD;
215 case XA_DATALOCATOR_ADDRESS: // intended fall-through
217 SL_LOGE("Unable to create MediaPlayer for data source locator 0x%x", sourceLocator);
218 result = XA_RESULT_PARAMETER_INVALID;
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();
227 mp->mDirectLevel = 0; // no attenuation
233 //-----------------------------------------------------------------------------
234 // FIXME abstract out the diff between CMediaPlayer and CAudioPlayer
235 XAresult android_Player_realize(CMediaPlayer *mp, SLboolean async) {
236 SL_LOGI("android_Player_realize_l(%p)", mp);
237 XAresult result = XA_RESULT_SUCCESS;
239 const SLDataSource *pDataSrc = &mp->mDataSource.u.mSource;
240 const SLuint32 sourceLocator = *(SLuint32 *)pDataSrc->pLocator;
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;
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);
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);
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);
272 SL_LOGE("Invalid or unsupported data locator type %u for data source",
273 mp->mDataSource.mLocator.mLocatorType);
274 result = XA_RESULT_PARAMETER_INVALID;
278 case INVALID_TYPE: // intended fall-through
280 SL_LOGE("Unable to realize MediaPlayer, invalid internal Android object type");
281 result = XA_RESULT_PARAMETER_INVALID;
288 //-----------------------------------------------------------------------------
289 XAresult android_Player_destroy(CMediaPlayer *mp) {
290 SL_LOGI("android_Player_destroy(%p)", mp);
291 XAresult result = XA_RESULT_SUCCESS;
293 if (mp->mAVPlayer != 0) {
294 mp->mAVPlayer.clear();
300 //-----------------------------------------------------------------------------
302 * pre-conditions: gp != 0, surface != 0
304 XAresult android_Player_setVideoSurface(const android::sp<android::GenericPlayer> &gp,
305 const android::sp<android::Surface> &surface) {
306 XAresult result = XA_RESULT_SUCCESS;
308 android::GenericMediaPlayer* gmp = static_cast<android::GenericMediaPlayer*>(gp.get());
309 gmp->setVideoSurface(surface);
316 * pre-conditions: gp != 0, surfaceTexture != 0
318 XAresult android_Player_setVideoSurfaceTexture(const android::sp<android::GenericPlayer> &gp,
319 const android::sp<android::ISurfaceTexture> &surfaceTexture) {
320 XAresult result = XA_RESULT_SUCCESS;
322 android::GenericMediaPlayer* gmp = static_cast<android::GenericMediaPlayer*>(gp.get());
323 gmp->setVideoSurfaceTexture(surfaceTexture);
329 XAresult android_Player_getDuration(IPlay *pPlayItf, XAmillisecond *pDurMsec) {
330 XAresult result = XA_RESULT_SUCCESS;
331 CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis;
333 switch (avp->mAndroidObjType) {
335 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
336 case AUDIOVIDEOPLAYER_FROM_URIFD: {
338 if (avp->mAVPlayer != 0) {
339 avp->mAVPlayer->getDurationMsec(&dur);
341 if (dur == ANDROID_UNKNOWN_TIME) {
342 *pDurMsec = XA_TIME_UNKNOWN;
344 *pDurMsec = (XAmillisecond)dur;
349 // we shouldn't be here
358 XAresult 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;
363 switch (avp->mAndroidObjType) {
365 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
366 case AUDIOVIDEOPLAYER_FROM_URIFD: {
368 if (avp->mAVPlayer != 0) {
369 avp->mAVPlayer->getPositionMsec(&pos);
371 if (pos == ANDROID_UNKNOWN_TIME) {
372 *pPosMsec = XA_TIME_UNKNOWN;
374 *pPosMsec = (XAmillisecond)pos;
379 // we shouldn't be here
388 //-----------------------------------------------------------------------------
390 * pre-condition: avp != 0, pVolItf != NULL
392 XAresult android_Player_volumeUpdate(const android::sp<android::GenericPlayer> &gp,
395 XAresult result = XA_RESULT_SUCCESS;
399 gp->setVolume((bool)pVolItf->mMute, (bool)pVolItf->mEnableStereoPosition,
400 pVolItf->mStereoPosition, pVolItf->mLevel);
406 //-----------------------------------------------------------------------------
408 * pre-condition: gp != 0
410 XAresult android_Player_setPlayState(const android::sp<android::GenericPlayer> &gp,
412 AndroidObjectState* pObjState)
414 XAresult result = XA_RESULT_SUCCESS;
415 AndroidObjectState objState = *pObjState;
418 case SL_PLAYSTATE_STOPPED: {
419 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_STOPPED");
423 case SL_PLAYSTATE_PAUSED: {
424 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PAUSED");
426 case ANDROID_UNINITIALIZED:
427 *pObjState = ANDROID_PREPARING;
430 case ANDROID_PREPARING:
436 SL_LOGE("Android object in invalid state");
441 case SL_PLAYSTATE_PLAYING: {
442 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PLAYING");
444 case ANDROID_UNINITIALIZED:
445 *pObjState = ANDROID_PREPARING;
447 // intended fall through
448 case ANDROID_PREPARING:
449 // intended fall through
454 SL_LOGE("Android object in invalid state");
460 // checked by caller, should not happen
469 * pre-condition: mp != NULL
471 XAresult android_Player_seek(CMediaPlayer *mp, SLmillisecond posMsec) {
472 XAresult result = XA_RESULT_SUCCESS;
473 switch (mp->mAndroidObjType) {
474 case AUDIOVIDEOPLAYER_FROM_URIFD:
475 if (mp->mAVPlayer !=0) {
476 mp->mAVPlayer->seek(posMsec);
479 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
481 result = XA_RESULT_PARAMETER_INVALID;
489 * pre-condition: mp != NULL
491 XAresult android_Player_loop(CMediaPlayer *mp, SLboolean loopEnable) {
492 XAresult result = XA_RESULT_SUCCESS;
493 switch (mp->mAndroidObjType) {
494 case AUDIOVIDEOPLAYER_FROM_URIFD:
495 if (mp->mAVPlayer !=0) {
496 mp->mAVPlayer->loop(loopEnable);
499 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
501 result = XA_RESULT_PARAMETER_INVALID;
508 //-----------------------------------------------------------------------------
509 void android_Player_androidBufferQueue_registerCallback_l(CMediaPlayer *mp) {
510 if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE)
511 && (mp->mAVPlayer != 0)) {
512 SL_LOGD("android_Player_androidBufferQueue_registerCallback_l");
513 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get());
514 splr->registerQueueCallback(
515 (const void*)mp, false /*userIsAudioPlayer*/,
516 mp->mAndroidBufferQueue.mContext, (const void*)&(mp->mAndroidBufferQueue.mItf));
522 void android_Player_androidBufferQueue_clear_l(CMediaPlayer *mp) {
523 if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE)
524 && (mp->mAVPlayer != 0)) {
525 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get());
531 void android_Player_androidBufferQueue_onRefilled_l(CMediaPlayer *mp) {
532 if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE)
533 && (mp->mAVPlayer != 0)) {
534 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get());
535 splr->queueRefilled_l();