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