MediaPlayer_to_android.cpp revision 8478d83d1e42e2b4e252cc7e7135fd872bb36982
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
30    // FIXME This code is derived from similar code in sfplayer_handlePrefetchEvent.  The two
31    // versions are quite similar, but still different enough that they need to be separate.
32    // At some point they should be re-factored and merged if feasible.
33    // As with other OpenMAX AL implementation code, this copy mostly uses SL_ symbols
34    // rather than XA_ unless the difference is significant.
35
36    if (NULL == user) {
37        return;
38    }
39
40    CMediaPlayer* mp = (CMediaPlayer*) user;
41    union {
42        char c[sizeof(int)];
43        int i;
44    } u;
45    u.i = event;
46    SL_LOGV("player_handleMediaPlayerEventNotifications(event='%c%c%c%c' (%d), data1=%d, data2=%d, "
47            "user=%p) from AVPlayer", u.c[3], u.c[2], u.c[1], u.c[0], event, data1, data2, user);
48    switch(event) {
49
50      case android::GenericPlayer::kEventPrepared: {
51
52        SL_LOGV("Received AVPlayer::kEventPrepared for CMediaPlayer %p", mp);
53
54        // assume no callback
55        slPrefetchCallback callback = NULL;
56        void* callbackPContext = NULL;
57
58        object_lock_exclusive(&mp->mObject);
59        // mark object as prepared; same state is used for successfully or unsuccessful prepare
60        mp->mAndroidObjState = ANDROID_READY;
61
62        // AVPlayer prepare() failed prefetching, there is no event in XAPrefetchStatus to
63        //  indicate a prefetch error, so we signal it by sending simulataneously two events:
64        //  - SL_PREFETCHEVENT_FILLLEVELCHANGE with a level of 0
65        //  - SL_PREFETCHEVENT_STATUSCHANGE with a status of SL_PREFETCHSTATUS_UNDERFLOW
66        if (PLAYER_SUCCESS != data1 && IsInterfaceInitialized(&mp->mObject, MPH_XAPREFETCHSTATUS)) {
67            mp->mPrefetchStatus.mLevel = 0;
68            mp->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW;
69            if (!(~mp->mPrefetchStatus.mCallbackEventsMask &
70                    (SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE))) {
71                callback = mp->mPrefetchStatus.mCallback;
72                callbackPContext = mp->mPrefetchStatus.mContext;
73            }
74        }
75        object_unlock_exclusive(&mp->mObject);
76
77        // callback with no lock held
78        if (NULL != callback) {
79            (*callback)(&mp->mPrefetchStatus.mItf, callbackPContext,
80                    SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE);
81        }
82
83        break;
84      }
85
86      case android::GenericPlayer::kEventHasVideoSize: {
87        SL_LOGV("Received AVPlayer::kEventHasVideoSize (%d,%d) for CMediaPlayer %p",
88                data1, data2, mp);
89
90        object_lock_exclusive(&mp->mObject);
91
92        // remove an existing video info entry (here we only have one video stream)
93        for(size_t i=0 ; i < mp->mStreamInfo.mStreamInfoTable.size() ; i++) {
94            if (XA_DOMAINTYPE_VIDEO == mp->mStreamInfo.mStreamInfoTable.itemAt(i).domain) {
95                mp->mStreamInfo.mStreamInfoTable.removeAt(i);
96                break;
97            }
98        }
99        // update the stream information with a new video info entry
100        StreamInfo streamInfo;
101        streamInfo.domain = XA_DOMAINTYPE_VIDEO;
102        streamInfo.videoInfo.codecId = 0;// unknown, we don't have that info FIXME
103        streamInfo.videoInfo.width = (XAuint32)data1;
104        streamInfo.videoInfo.height = (XAuint32)data2;
105        streamInfo.videoInfo.bitRate = 0;// unknown, we don't have that info FIXME
106        streamInfo.videoInfo.frameRate = 0;
107        streamInfo.videoInfo.duration = XA_TIME_UNKNOWN;
108        StreamInfo &contInfo = mp->mStreamInfo.mStreamInfoTable.editItemAt(0);
109        contInfo.containerInfo.numStreams = 1;
110        ssize_t index = mp->mStreamInfo.mStreamInfoTable.add(streamInfo);
111
112        xaStreamEventChangeCallback callback = mp->mStreamInfo.mCallback;
113        void* callbackPContext = mp->mStreamInfo.mContext;
114
115        object_unlock_exclusive(&mp->mObject);
116
117        // enqueue notification (outside of lock) that the stream information has been updated
118        if ((NULL != callback) && (index >= 0)) {
119#ifdef XA_SYNCHRONOUS_STREAMCBEVENT_PROPERTYCHANGE
120            (*callback)(&mp->mStreamInfo.mItf, XA_STREAMCBEVENT_PROPERTYCHANGE /*eventId*/,
121                    1 /*streamIndex, only one stream supported here, 0 is reserved*/,
122                    NULL /*pEventData, always NULL in OpenMAX AL 1.0.1*/,
123                    callbackPContext /*pContext*/);
124#else
125            SLresult res = EnqueueAsyncCallback_piipp(mp, callback,
126                    /*p1*/ &mp->mStreamInfo.mItf,
127                    /*i1*/ XA_STREAMCBEVENT_PROPERTYCHANGE /*eventId*/,
128                    /*i2*/ 1 /*streamIndex, only one stream supported here, 0 is reserved*/,
129                    /*p2*/ NULL /*pEventData, always NULL in OpenMAX AL 1.0.1*/,
130                    /*p3*/ callbackPContext /*pContext*/);
131#endif
132        }
133        break;
134      }
135
136      case android::GenericPlayer::kEventEndOfStream: {
137        SL_LOGV("Received AVPlayer::kEventEndOfStream for CMediaPlayer %p", mp);
138
139        object_lock_exclusive(&mp->mObject);
140        // should be xaPlayCallback but we're sharing the itf between SL and AL
141        slPlayCallback playCallback = NULL;
142        void * playContext = NULL;
143        // XAPlayItf callback or no callback?
144        if (mp->mPlay.mEventFlags & XA_PLAYEVENT_HEADATEND) {
145            playCallback = mp->mPlay.mCallback;
146            playContext = mp->mPlay.mContext;
147        }
148        mp->mPlay.mState = XA_PLAYSTATE_PAUSED;
149        object_unlock_exclusive(&mp->mObject);
150
151        // enqueue callback with no lock held
152        if (NULL != playCallback) {
153#ifdef USE_SYNCHRONOUS_PLAY_CALLBACK
154            (*playCallback)(&mp->mPlay.mItf, playContext, XA_PLAYEVENT_HEADATEND);
155#else
156            SLresult res = EnqueueAsyncCallback_ppi(mp, playCallback, &mp->mPlay.mItf, playContext,
157                    XA_PLAYEVENT_HEADATEND);
158            LOGW_IF(SL_RESULT_SUCCESS != res,
159                    "Callback %p(%p, %p, SL_PLAYEVENT_HEADATEND) dropped", playCallback,
160                    &mp->mPlay.mItf, playContext);
161#endif
162        }
163        break;
164      }
165
166      case android::GenericPlayer::kEventChannelCount: {
167        SL_LOGV("kEventChannelCount channels = %d", data1);
168        object_lock_exclusive(&mp->mObject);
169        if (UNKNOWN_NUMCHANNELS == mp->mNumChannels && UNKNOWN_NUMCHANNELS != data1) {
170            mp->mNumChannels = data1;
171            android_Player_volumeUpdate(mp);
172        }
173        object_unlock_exclusive(&mp->mObject);
174      }
175      break;
176
177      case android::GenericPlayer::kEventPrefetchFillLevelUpdate: {
178        SL_LOGV("kEventPrefetchFillLevelUpdate");
179        if (!IsInterfaceInitialized(&mp->mObject, MPH_XAPREFETCHSTATUS)) {
180            break;
181        }
182        slPrefetchCallback callback = NULL;
183        void* callbackPContext = NULL;
184
185        // SLPrefetchStatusItf callback or no callback?
186        interface_lock_exclusive(&mp->mPrefetchStatus);
187        if (mp->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_FILLLEVELCHANGE) {
188            callback = mp->mPrefetchStatus.mCallback;
189            callbackPContext = mp->mPrefetchStatus.mContext;
190        }
191        mp->mPrefetchStatus.mLevel = (SLpermille)data1;
192        interface_unlock_exclusive(&mp->mPrefetchStatus);
193
194        // callback with no lock held
195        if (NULL != callback) {
196            (*callback)(&mp->mPrefetchStatus.mItf, callbackPContext,
197                    SL_PREFETCHEVENT_FILLLEVELCHANGE);
198        }
199      }
200      break;
201
202      case android::GenericPlayer::kEventPrefetchStatusChange: {
203        SL_LOGV("kEventPrefetchStatusChange");
204        if (!IsInterfaceInitialized(&mp->mObject, MPH_XAPREFETCHSTATUS)) {
205            break;
206        }
207        slPrefetchCallback callback = NULL;
208        void* callbackPContext = NULL;
209
210        // SLPrefetchStatusItf callback or no callback?
211        object_lock_exclusive(&mp->mObject);
212        if (mp->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_STATUSCHANGE) {
213            callback = mp->mPrefetchStatus.mCallback;
214            callbackPContext = mp->mPrefetchStatus.mContext;
215        }
216        if (data1 >= android::kStatusIntermediate) {
217            mp->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_SUFFICIENTDATA;
218            // FIXME copied from AudioPlayer, but probably wrong
219            mp->mAndroidObjState = ANDROID_READY;
220        } else if (data1 < android::kStatusIntermediate) {
221            mp->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW;
222        }
223        object_unlock_exclusive(&mp->mObject);
224
225        // callback with no lock held
226        if (NULL != callback) {
227            (*callback)(&mp->mPrefetchStatus.mItf, callbackPContext, SL_PREFETCHEVENT_STATUSCHANGE);
228        }
229      }
230      break;
231
232      case android::GenericPlayer::kEventPlay: {
233        SL_LOGV("kEventPlay");
234
235        interface_lock_shared(&mp->mPlay);
236        slPlayCallback callback = mp->mPlay.mCallback;
237        void* callbackPContext = mp->mPlay.mContext;
238        interface_unlock_shared(&mp->mPlay);
239
240        if (NULL != callback) {
241            (*callback)(&mp->mPlay.mItf, callbackPContext, (SLuint32) data1); // SL_PLAYEVENT_HEAD*
242        }
243      }
244      break;
245
246      default: {
247        SL_LOGE("Received unknown event %d, data %d from AVPlayer", event, data1);
248      }
249    }
250}
251
252
253//-----------------------------------------------------------------------------
254XAresult android_Player_checkSourceSink(CMediaPlayer *mp) {
255
256    XAresult result = XA_RESULT_SUCCESS;
257
258    const SLDataSource *pSrc    = &mp->mDataSource.u.mSource;
259    const SLDataSink *pAudioSnk = &mp->mAudioSink.u.mSink;
260
261    // format check:
262    const SLuint32 sourceLocatorType = *(SLuint32 *)pSrc->pLocator;
263    const SLuint32 sourceFormatType  = *(SLuint32 *)pSrc->pFormat;
264    const SLuint32 audioSinkLocatorType = *(SLuint32 *)pAudioSnk->pLocator;
265    //const SLuint32 sinkFormatType = *(SLuint32 *)pAudioSnk->pFormat;
266
267    // Source check
268    switch(sourceLocatorType) {
269
270    case XA_DATALOCATOR_ANDROIDBUFFERQUEUE: {
271        switch (sourceFormatType) {
272        case XA_DATAFORMAT_MIME: {
273            SLDataFormat_MIME *df_mime = (SLDataFormat_MIME *) pSrc->pFormat;
274            if (SL_CONTAINERTYPE_MPEG_TS != df_mime->containerType) {
275                SL_LOGE("Cannot create player with XA_DATALOCATOR_ANDROIDBUFFERQUEUE data source "
276                        "that is not fed MPEG-2 TS data");
277                return SL_RESULT_CONTENT_UNSUPPORTED;
278            }
279        } break;
280        default:
281            SL_LOGE("Cannot create player with XA_DATALOCATOR_ANDROIDBUFFERQUEUE data source "
282                    "without SL_DATAFORMAT_MIME format");
283            return XA_RESULT_CONTENT_UNSUPPORTED;
284        }
285    } break;
286
287    case XA_DATALOCATOR_URI: // intended fall-through
288    case XA_DATALOCATOR_ANDROIDFD:
289        break;
290
291    default:
292        SL_LOGE("Cannot create media player with data locator type 0x%x",
293                (unsigned) sourceLocatorType);
294        return SL_RESULT_PARAMETER_INVALID;
295    }// switch (locatorType)
296
297    // Audio sink check: only playback is supported here
298    switch(audioSinkLocatorType) {
299
300    case XA_DATALOCATOR_OUTPUTMIX:
301        break;
302
303    default:
304        SL_LOGE("Cannot create media player with audio sink data locator of type 0x%x",
305                (unsigned) audioSinkLocatorType);
306        return XA_RESULT_PARAMETER_INVALID;
307    }// switch (locaaudioSinkLocatorTypeorType)
308
309    return result;
310}
311
312
313//-----------------------------------------------------------------------------
314XAresult android_Player_create(CMediaPlayer *mp) {
315
316    XAresult result = XA_RESULT_SUCCESS;
317
318    // FIXME verify data source
319    const SLDataSource *pDataSrc = &mp->mDataSource.u.mSource;
320    // FIXME verify audio data sink
321    const SLDataSink *pAudioSnk = &mp->mAudioSink.u.mSink;
322    // FIXME verify image data sink
323    const SLDataSink *pVideoSnk = &mp->mImageVideoSink.u.mSink;
324
325    XAuint32 sourceLocator = *(XAuint32 *)pDataSrc->pLocator;
326    switch(sourceLocator) {
327    // FIXME support Android simple buffer queue as well
328    case XA_DATALOCATOR_ANDROIDBUFFERQUEUE:
329        mp->mAndroidObjType = AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE;
330        break;
331    case XA_DATALOCATOR_URI: // intended fall-through
332    case SL_DATALOCATOR_ANDROIDFD:
333        mp->mAndroidObjType = AUDIOVIDEOPLAYER_FROM_URIFD;
334        break;
335    case XA_DATALOCATOR_ADDRESS: // intended fall-through
336    default:
337        SL_LOGE("Unable to create MediaPlayer for data source locator 0x%x", sourceLocator);
338        result = XA_RESULT_PARAMETER_INVALID;
339        break;
340    }
341
342    // FIXME duplicates an initialization also done by higher level
343    mp->mAndroidObjState = ANDROID_UNINITIALIZED;
344    mp->mStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE;
345    mp->mSessionId = android::AudioSystem::newAudioSessionId();
346
347    return result;
348}
349
350
351//-----------------------------------------------------------------------------
352// FIXME abstract out the diff between CMediaPlayer and CAudioPlayer
353XAresult android_Player_realize(CMediaPlayer *mp, SLboolean async) {
354    SL_LOGV("android_Player_realize_l(%p)", mp);
355    XAresult result = XA_RESULT_SUCCESS;
356
357    const SLDataSource *pDataSrc = &mp->mDataSource.u.mSource;
358    const SLuint32 sourceLocator = *(SLuint32 *)pDataSrc->pLocator;
359
360    AudioPlayback_Parameters ap_params;
361    ap_params.sessionId = mp->mSessionId;
362    ap_params.streamType = mp->mStreamType;
363    ap_params.trackcb = NULL;
364    ap_params.trackcbUser = NULL;
365
366    switch(mp->mAndroidObjType) {
367    case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: {
368        mp->mAVPlayer = new android::StreamPlayer(&ap_params, true /*hasVideo*/);
369        mp->mAVPlayer->init(player_handleMediaPlayerEventNotifications, (void*)mp);
370        }
371        break;
372    case AUDIOVIDEOPLAYER_FROM_URIFD: {
373        mp->mAVPlayer = new android::LocAVPlayer(&ap_params, true /*hasVideo*/);
374        mp->mAVPlayer->init(player_handleMediaPlayerEventNotifications, (void*)mp);
375        switch (mp->mDataSource.mLocator.mLocatorType) {
376        case XA_DATALOCATOR_URI:
377            ((android::LocAVPlayer*)mp->mAVPlayer.get())->setDataSource(
378                    (const char*)mp->mDataSource.mLocator.mURI.URI);
379            break;
380        case XA_DATALOCATOR_ANDROIDFD: {
381            int64_t offset = (int64_t)mp->mDataSource.mLocator.mFD.offset;
382            ((android::LocAVPlayer*)mp->mAVPlayer.get())->setDataSource(
383                    (int)mp->mDataSource.mLocator.mFD.fd,
384                    offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ?
385                            (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset,
386                    (int64_t)mp->mDataSource.mLocator.mFD.length);
387            }
388            break;
389        default:
390            SL_LOGE("Invalid or unsupported data locator type %u for data source",
391                    mp->mDataSource.mLocator.mLocatorType);
392            result = XA_RESULT_PARAMETER_INVALID;
393        }
394        }
395        break;
396    case INVALID_TYPE: // intended fall-through
397    default:
398        SL_LOGE("Unable to realize MediaPlayer, invalid internal Android object type");
399        result = XA_RESULT_PARAMETER_INVALID;
400        break;
401    }
402
403    if (XA_RESULT_SUCCESS == result) {
404
405        // if there is a video sink
406        if (XA_DATALOCATOR_NATIVEDISPLAY ==
407                mp->mImageVideoSink.mLocator.mLocatorType) {
408            ANativeWindow *nativeWindow = (ANativeWindow *)
409                    mp->mImageVideoSink.mLocator.mNativeDisplay.hWindow;
410            // we already verified earlier that hWindow is non-NULL
411            assert(nativeWindow != NULL);
412            result = android_Player_setNativeWindow(mp, nativeWindow);
413        }
414
415    }
416
417    return result;
418}
419
420//-----------------------------------------------------------------------------
421XAresult android_Player_destroy(CMediaPlayer *mp) {
422    SL_LOGV("android_Player_destroy(%p)", mp);
423    XAresult result = XA_RESULT_SUCCESS;
424
425    if (mp->mAVPlayer != 0) {
426        mp->mAVPlayer.clear();
427    }
428
429    return result;
430}
431
432
433void android_Player_usePlayEventMask(CMediaPlayer *mp) {
434    if (mp->mAVPlayer != 0) {
435        IPlay *pPlayItf = &mp->mPlay;
436        mp->mAVPlayer->setPlayEvents((int32_t) pPlayItf->mEventFlags,
437                (int32_t) pPlayItf->mMarkerPosition, (int32_t) pPlayItf->mPositionUpdatePeriod);
438    }
439}
440
441
442XAresult android_Player_getDuration(IPlay *pPlayItf, XAmillisecond *pDurMsec) {
443    CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis;
444
445    switch (avp->mAndroidObjType) {
446
447    case AUDIOVIDEOPLAYER_FROM_URIFD: {
448        int dur = ANDROID_UNKNOWN_TIME;
449        if (avp->mAVPlayer != 0) {
450            avp->mAVPlayer->getDurationMsec(&dur);
451        }
452        if (dur == ANDROID_UNKNOWN_TIME) {
453            *pDurMsec = XA_TIME_UNKNOWN;
454        } else {
455            *pDurMsec = (XAmillisecond)dur;
456        }
457    } break;
458
459    case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
460    default:
461        *pDurMsec = XA_TIME_UNKNOWN;
462        break;
463    }
464
465    return XA_RESULT_SUCCESS;
466}
467
468
469XAresult android_Player_getPosition(IPlay *pPlayItf, XAmillisecond *pPosMsec) {
470    SL_LOGD("android_Player_getPosition()");
471    XAresult result = XA_RESULT_SUCCESS;
472    CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis;
473
474    switch (avp->mAndroidObjType) {
475
476    case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
477    case AUDIOVIDEOPLAYER_FROM_URIFD: {
478        int pos = -1;
479        if (avp->mAVPlayer != 0) {
480            avp->mAVPlayer->getPositionMsec(&pos);
481        }
482        if (pos == ANDROID_UNKNOWN_TIME) {
483            *pPosMsec = XA_TIME_UNKNOWN;
484        } else {
485            *pPosMsec = (XAmillisecond)pos;
486        }
487    } break;
488
489    default:
490        // we shouldn't be here
491        assert(false);
492        break;
493    }
494
495    return result;
496}
497
498
499//-----------------------------------------------------------------------------
500/**
501 * pre-condition: mp != NULL
502 */
503void android_Player_volumeUpdate(CMediaPlayer* mp)
504{
505    android::GenericPlayer* avp = mp->mAVPlayer.get();
506    if (avp != NULL) {
507        float volumes[2];
508        // MediaPlayer does not currently support EffectSend or MuteSolo
509        android_player_volumeUpdate(volumes, &mp->mVolume, mp->mNumChannels, 1.0f, NULL);
510        float leftVol = volumes[0], rightVol = volumes[1];
511        avp->setVolume(leftVol, rightVol);
512    }
513}
514
515//-----------------------------------------------------------------------------
516/**
517 * pre-condition: gp != 0
518 */
519XAresult android_Player_setPlayState(const android::sp<android::GenericPlayer> &gp,
520        SLuint32 playState,
521        AndroidObjectState* pObjState)
522{
523    XAresult result = XA_RESULT_SUCCESS;
524    AndroidObjectState objState = *pObjState;
525
526    switch (playState) {
527     case SL_PLAYSTATE_STOPPED: {
528         SL_LOGV("setting AVPlayer to SL_PLAYSTATE_STOPPED");
529         gp->stop();
530         }
531         break;
532     case SL_PLAYSTATE_PAUSED: {
533         SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PAUSED");
534         switch(objState) {
535         case ANDROID_UNINITIALIZED:
536             *pObjState = ANDROID_PREPARING;
537             gp->prepare();
538             break;
539         case ANDROID_PREPARING:
540             break;
541         case ANDROID_READY:
542             gp->pause();
543             break;
544         default:
545             SL_LOGE("Android object in invalid state");
546             break;
547         }
548         }
549         break;
550     case SL_PLAYSTATE_PLAYING: {
551         SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PLAYING");
552         switch(objState) {
553         case ANDROID_UNINITIALIZED:
554             *pObjState = ANDROID_PREPARING;
555             gp->prepare();
556             // intended fall through
557         case ANDROID_PREPARING:
558             // intended fall through
559         case ANDROID_READY:
560             gp->play();
561             break;
562         default:
563             SL_LOGE("Android object in invalid state");
564             break;
565         }
566         }
567         break;
568     default:
569         // checked by caller, should not happen
570         break;
571     }
572
573    return result;
574}
575
576
577/**
578 * pre-condition: mp != NULL
579 */
580XAresult android_Player_seek(CMediaPlayer *mp, SLmillisecond posMsec) {
581    XAresult result = XA_RESULT_SUCCESS;
582    switch (mp->mAndroidObjType) {
583      case AUDIOVIDEOPLAYER_FROM_URIFD:
584        if (mp->mAVPlayer !=0) {
585            mp->mAVPlayer->seek(posMsec);
586        }
587        break;
588      case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
589      default: {
590          result = XA_RESULT_PARAMETER_INVALID;
591      }
592    }
593    return result;
594}
595
596
597/**
598 * pre-condition: mp != NULL
599 */
600XAresult android_Player_loop(CMediaPlayer *mp, SLboolean loopEnable) {
601    XAresult result = XA_RESULT_SUCCESS;
602    switch (mp->mAndroidObjType) {
603      case AUDIOVIDEOPLAYER_FROM_URIFD:
604        if (mp->mAVPlayer !=0) {
605            mp->mAVPlayer->loop(loopEnable);
606        }
607        break;
608      case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
609      default: {
610          result = XA_RESULT_PARAMETER_INVALID;
611      }
612    }
613    return result;
614}
615
616
617//-----------------------------------------------------------------------------
618void android_Player_androidBufferQueue_registerCallback_l(CMediaPlayer *mp) {
619    if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE)
620            && (mp->mAVPlayer != 0)) {
621        SL_LOGD("android_Player_androidBufferQueue_registerCallback_l");
622        android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get());
623        splr->registerQueueCallback(
624                (const void*)mp, false /*userIsAudioPlayer*/,
625                mp->mAndroidBufferQueue.mContext, (const void*)&(mp->mAndroidBufferQueue.mItf));
626
627    }
628}
629
630
631void android_Player_androidBufferQueue_clear_l(CMediaPlayer *mp) {
632    if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE)
633            && (mp->mAVPlayer != 0)) {
634        android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get());
635        splr->appClear_l();
636    }
637}
638
639
640void android_Player_androidBufferQueue_onRefilled_l(CMediaPlayer *mp) {
641    if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE)
642            && (mp->mAVPlayer != 0)) {
643        android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get());
644        splr->queueRefilled_l();
645    }
646}
647
648
649/*
650 *  pre-conditions:
651 *      mp != NULL
652 *      mp->mAVPlayer != 0 (player is realized)
653 *      nativeWindow can be NULL, but if NULL it is treated as an error
654 */
655SLresult android_Player_setNativeWindow(CMediaPlayer *mp, ANativeWindow *nativeWindow)
656{
657    assert(mp != NULL);
658    assert(mp->mAVPlayer != 0);
659    if (nativeWindow == NULL) {
660        SL_LOGE("ANativeWindow is NULL");
661        return SL_RESULT_PARAMETER_INVALID;
662    }
663    SLresult result;
664    int err;
665    int value;
666    // this could crash if app passes in a bad parameter, but that's OK
667    err = (*nativeWindow->query)(nativeWindow, NATIVE_WINDOW_CONCRETE_TYPE, &value);
668    if (0 != err) {
669        SL_LOGE("Query NATIVE_WINDOW_CONCRETE_TYPE on ANativeWindow * %p failed; "
670                "errno %d", nativeWindow, err);
671        result = SL_RESULT_PARAMETER_INVALID;
672    } else {
673        switch (value) {
674        case NATIVE_WINDOW_SURFACE: {                // Surface
675            SL_LOGV("Displaying on ANativeWindow of type NATIVE_WINDOW_SURFACE");
676            android::sp<android::Surface> nativeSurface(
677                    static_cast<android::Surface *>(nativeWindow));
678            mp->mAVPlayer->setVideoSurface(nativeSurface);
679            result = SL_RESULT_SUCCESS;
680            } break;
681        case NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT: { // SurfaceTextureClient
682            SL_LOGV("Displaying on ANativeWindow of type NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT");
683            android::sp<android::SurfaceTextureClient> surfaceTextureClient(
684                    static_cast<android::SurfaceTextureClient *>(nativeWindow));
685            android::sp<android::ISurfaceTexture> nativeSurfaceTexture(
686                    surfaceTextureClient->getISurfaceTexture());
687            mp->mAVPlayer->setVideoSurfaceTexture(nativeSurfaceTexture);
688            result = SL_RESULT_SUCCESS;
689            } break;
690        case NATIVE_WINDOW_FRAMEBUFFER:              // FramebufferNativeWindow
691            // fall through
692        default:
693            SL_LOGE("ANativeWindow * %p has unknown or unsupported concrete type %d",
694                    nativeWindow, value);
695            result = SL_RESULT_PARAMETER_INVALID;
696            break;
697        }
698    }
699    return result;
700}
701