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 // 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*/);
88 case android::GenericPlayer::kEventEndOfStream: {
89 SL_LOGV("Received AVPlayer::kEventEndOfStream for CMediaPlayer %p", mp);
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;
100 object_unlock_exclusive(&mp->mObject);
102 // callback with no lock held
103 if (NULL != playCallback) {
104 (*playCallback)(&mp->mPlay.mItf, playContext, XA_PLAYEVENT_HEADATEND);
110 SL_LOGE("Received unknown event %d, data %d from AVPlayer", event, data1);
116 //-----------------------------------------------------------------------------
117 XAresult android_Player_checkSourceSink(CMediaPlayer *mp) {
119 XAresult result = XA_RESULT_SUCCESS;
121 const SLDataSource *pSrc = &mp->mDataSource.u.mSource;
122 const SLDataSink *pAudioSnk = &mp->mAudioSink.u.mSink;
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;
131 switch(sourceLocatorType) {
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;
144 SL_LOGE("Cannot create player with XA_DATALOCATOR_ANDROIDBUFFERQUEUE data source "
145 "without SL_DATAFORMAT_MIME format");
146 return XA_RESULT_CONTENT_UNSUPPORTED;
150 case XA_DATALOCATOR_URI: // intended fall-through
151 case XA_DATALOCATOR_ANDROIDFD:
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)
160 // Audio sink check: only playback is supported here
161 switch(audioSinkLocatorType) {
163 case XA_DATALOCATOR_OUTPUTMIX:
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)
176 //-----------------------------------------------------------------------------
177 XAresult android_Player_create(CMediaPlayer *mp) {
179 XAresult result = XA_RESULT_SUCCESS;
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;
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;
194 case XA_DATALOCATOR_URI: // intended fall-through
195 case SL_DATALOCATOR_ANDROIDFD:
196 mp->mAndroidObjType = AUDIOVIDEOPLAYER_FROM_URIFD;
198 case XA_DATALOCATOR_ADDRESS: // intended fall-through
200 SL_LOGE("Unable to create MediaPlayer for data source locator 0x%x", sourceLocator);
201 result = XA_RESULT_PARAMETER_INVALID;
205 mp->mAndroidObjState = ANDROID_UNINITIALIZED;
206 mp->mStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE;
207 mp->mSessionId = android::AudioSystem::newAudioSessionId();
209 mp->mDirectLevel = 0; // no attenuation
215 //-----------------------------------------------------------------------------
216 // FIXME abstract out the diff between CMediaPlayer and CAudioPlayer
217 XAresult android_Player_realize(CMediaPlayer *mp, SLboolean async) {
218 SL_LOGI("android_Player_realize_l(%p)", mp);
219 XAresult result = XA_RESULT_SUCCESS;
221 const SLDataSource *pDataSrc = &mp->mDataSource.u.mSource;
222 const SLuint32 sourceLocator = *(SLuint32 *)pDataSrc->pLocator;
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;
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);
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);
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);
254 SL_LOGE("Invalid or unsupported data locator type %u for data source",
255 mp->mDataSource.mLocator.mLocatorType);
256 result = XA_RESULT_PARAMETER_INVALID;
260 case INVALID_TYPE: // intended fall-through
262 SL_LOGE("Unable to realize MediaPlayer, invalid internal Android object type");
263 result = XA_RESULT_PARAMETER_INVALID;
270 //-----------------------------------------------------------------------------
271 XAresult android_Player_destroy(CMediaPlayer *mp) {
272 SL_LOGI("android_Player_destroy(%p)", mp);
273 XAresult result = XA_RESULT_SUCCESS;
275 if (mp->mAVPlayer != 0) {
276 mp->mAVPlayer.clear();
282 //-----------------------------------------------------------------------------
284 * pre-conditions: gp != 0, surface != 0
286 XAresult android_Player_setVideoSurface(const android::sp<android::GenericPlayer> &gp,
287 const android::sp<android::Surface> &surface) {
288 XAresult result = XA_RESULT_SUCCESS;
290 android::GenericMediaPlayer* gmp = static_cast<android::GenericMediaPlayer*>(gp.get());
291 gmp->setVideoSurface(surface);
298 * pre-conditions: gp != 0, surfaceTexture != 0
300 XAresult android_Player_setVideoSurfaceTexture(const android::sp<android::GenericPlayer> &gp,
301 const android::sp<android::ISurfaceTexture> &surfaceTexture) {
302 XAresult result = XA_RESULT_SUCCESS;
304 android::GenericMediaPlayer* gmp = static_cast<android::GenericMediaPlayer*>(gp.get());
305 gmp->setVideoSurfaceTexture(surfaceTexture);
311 XAresult android_Player_getDuration(IPlay *pPlayItf, XAmillisecond *pDurMsec) {
312 XAresult result = XA_RESULT_SUCCESS;
313 CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis;
315 switch (avp->mAndroidObjType) {
317 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
318 case AUDIOVIDEOPLAYER_FROM_URIFD: {
320 if (avp->mAVPlayer != 0) {
321 avp->mAVPlayer->getDurationMsec(&dur);
323 if (dur == ANDROID_UNKNOWN_TIME) {
324 *pDurMsec = XA_TIME_UNKNOWN;
326 *pDurMsec = (XAmillisecond)dur;
331 // we shouldn't be here
340 XAresult 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;
345 switch (avp->mAndroidObjType) {
347 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
348 case AUDIOVIDEOPLAYER_FROM_URIFD: {
350 if (avp->mAVPlayer != 0) {
351 avp->mAVPlayer->getPositionMsec(&pos);
353 if (pos == ANDROID_UNKNOWN_TIME) {
354 *pPosMsec = XA_TIME_UNKNOWN;
356 *pPosMsec = (XAmillisecond)pos;
361 // we shouldn't be here
370 //-----------------------------------------------------------------------------
372 * pre-condition: avp != 0, pVolItf != NULL
374 XAresult android_Player_volumeUpdate(const android::sp<android::GenericPlayer> &gp,
377 XAresult result = XA_RESULT_SUCCESS;
379 gp->setVolume((bool)pVolItf->mMute, (bool)pVolItf->mEnableStereoPosition,
380 pVolItf->mStereoPosition, pVolItf->mLevel);
385 //-----------------------------------------------------------------------------
387 * pre-condition: gp != 0
389 XAresult android_Player_setPlayState(const android::sp<android::GenericPlayer> &gp,
391 AndroidObjectState* pObjState)
393 XAresult result = XA_RESULT_SUCCESS;
394 AndroidObjectState objState = *pObjState;
397 case SL_PLAYSTATE_STOPPED: {
398 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_STOPPED");
402 case SL_PLAYSTATE_PAUSED: {
403 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PAUSED");
405 case ANDROID_UNINITIALIZED:
406 *pObjState = ANDROID_PREPARING;
409 case ANDROID_PREPARING:
415 SL_LOGE("Android object in invalid state");
420 case SL_PLAYSTATE_PLAYING: {
421 SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PLAYING");
423 case ANDROID_UNINITIALIZED:
424 *pObjState = ANDROID_PREPARING;
426 // intended fall through
427 case ANDROID_PREPARING:
428 // intended fall through
433 SL_LOGE("Android object in invalid state");
439 // checked by caller, should not happen
448 * pre-condition: mp != NULL
450 XAresult 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);
458 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
460 result = XA_RESULT_PARAMETER_INVALID;
468 * pre-condition: mp != NULL
470 XAresult 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);
478 case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
480 result = XA_RESULT_PARAMETER_INVALID;
487 //-----------------------------------------------------------------------------
488 void 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));
501 void 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());
510 void 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();