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