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