MediaPlayer_to_android.cpp revision cb441acdda6f8e81d44fcdaadd4ff7ab3d3e367b
1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/*
2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * Copyright (C) 2010 The Android Open Source Project
3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) *
4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * Licensed under the Apache License, Version 2.0 (the "License");
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * you may not use this file except in compliance with the License.
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * You may obtain a copy of the License at
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) *
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) *      http://www.apache.org/licenses/LICENSE-2.0
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) *
10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * Unless required by applicable law or agreed to in writing, software
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * distributed under the License is distributed on an "AS IS" BASIS,
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * See the License for the specific language governing permissions and
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) * limitations under the License.
15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) */
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "sles_allinclusive.h"
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "utils/RefBase.h"
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "android_prompts.h"
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// LocAVPlayer and StreamPlayer derive from GenericMediaPlayer,
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//    so no need to #include "android_GenericMediaPlayer.h"
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "android_LocAVPlayer.h"
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "android_StreamPlayer.h"
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//-----------------------------------------------------------------------------
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)static void player_handleMediaPlayerEventNotifications(int event, int data1, int data2, void* user)
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles){
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (NULL == user) {
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        return;
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    CMediaPlayer* mp = (CMediaPlayer*) user;
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    SL_LOGV("received event %d, data %d from AVPlayer", event, data1);
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    switch(event) {
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      case android::GenericPlayer::kEventPrepared: {
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        if (PLAYER_SUCCESS == data1) {
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            object_lock_exclusive(&mp->mObject);
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            SL_LOGV("Received AVPlayer::kEventPrepared from AVPlayer for CMediaPlayer %p", mp);
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            mp->mAndroidObjState = ANDROID_READY;
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            object_unlock_exclusive(&mp->mObject);
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        }
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        break;
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      }
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      case android::GenericPlayer::kEventHasVideoSize: {
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        SL_LOGV("Received AVPlayer::kEventHasVideoSize (%d,%d) for CMediaPlayer %p",
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                data1, data2, mp);
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        object_lock_exclusive(&mp->mObject);
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
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.frameRate = 0;
69        streamInfo.videoInfo.duration = XA_TIME_UNKNOWN;
70        StreamInfo &contInfo = mp->mStreamInfo.mStreamInfoTable.editItemAt(0);
71        contInfo.containerInfo.numStreams = 1;
72        ssize_t index = mp->mStreamInfo.mStreamInfoTable.add(streamInfo);
73
74        xaStreamEventChangeCallback callback = mp->mStreamInfo.mCallback;
75        void* callbackPContext = mp->mStreamInfo.mContext;
76
77        object_unlock_exclusive(&mp->mObject);
78
79        // enqueue notification (outside of lock) that the stream information has been updated
80        if ((NULL != callback) && (index >= 0)) {
81#ifdef XA_SYNCHRONOUS_STREAMCBEVENT_PROPERTYCHANGE
82            (*callback)(&mp->mStreamInfo.mItf, XA_STREAMCBEVENT_PROPERTYCHANGE /*eventId*/,
83                    1 /*streamIndex, only one stream supported here, 0 is reserved*/,
84                    NULL /*pEventData, always NULL in OpenMAX AL 1.0.1*/,
85                    callbackPContext /*pContext*/);
86#else
87            SLresult res = EnqueueAsyncCallback_piipp(mp, callback,
88                    /*p1*/ &mp->mStreamInfo.mItf,
89                    /*i1*/ XA_STREAMCBEVENT_PROPERTYCHANGE /*eventId*/,
90                    /*i2*/ 1 /*streamIndex, only one stream supported here, 0 is reserved*/,
91                    /*p2*/ NULL /*pEventData, always NULL in OpenMAX AL 1.0.1*/,
92                    /*p3*/ callbackPContext /*pContext*/);
93#endif
94        }
95        break;
96      }
97
98      case android::GenericPlayer::kEventEndOfStream: {
99        SL_LOGV("Received AVPlayer::kEventEndOfStream for CMediaPlayer %p", mp);
100
101        object_lock_exclusive(&mp->mObject);
102        // should be xaPlayCallback but we're sharing the itf between SL and AL
103        slPlayCallback playCallback = NULL;
104        void * playContext = NULL;
105        // XAPlayItf callback or no callback?
106        if (mp->mPlay.mEventFlags & XA_PLAYEVENT_HEADATEND) {
107            playCallback = mp->mPlay.mCallback;
108            playContext = mp->mPlay.mContext;
109        }
110        object_unlock_exclusive(&mp->mObject);
111
112        // enqueue callback with no lock held
113        if (NULL != playCallback) {
114#ifdef XA_SYNCHRONOUS_PLAYEVENT_HEADATEND
115            (*playCallback)(&mp->mPlay.mItf, playContext, XA_PLAYEVENT_HEADATEND);
116#else
117            SLresult res = EnqueueAsyncCallback_ppi(mp, playCallback, &mp->mPlay.mItf, playContext,
118                    XA_PLAYEVENT_HEADATEND);
119            LOGW_IF(SL_RESULT_SUCCESS != res,
120                    "Callback %p(%p, %p, XA_PLAYEVENT_HEADATEND) dropped", playCallback,
121                    &mp->mPlay.mItf, playContext);
122#endif
123        }
124        break;
125      }
126
127      case android::GenericPlayer::kEventChannelCount: {
128        SL_LOGV("kEventChannelCount channels = %d", data1);
129        object_lock_exclusive(&mp->mObject);
130        if (UNKNOWN_NUMCHANNELS == mp->mNumChannels && UNKNOWN_NUMCHANNELS != data1) {
131            mp->mNumChannels = data1;
132            android_Player_volumeUpdate(mp);
133        }
134        object_unlock_exclusive(&mp->mObject);
135      }
136      break;
137
138      case android::GenericPlayer::kEventPrefetchFillLevelUpdate: {
139        SL_LOGV("kEventPrefetchFillLevelUpdate");
140      }
141      break;
142
143      case android::GenericPlayer::kEventPrefetchStatusChange: {
144        SL_LOGV("kEventPrefetchStatusChange");
145      }
146      break;
147
148
149      default: {
150        SL_LOGE("Received unknown event %d, data %d from AVPlayer", event, data1);
151      }
152    }
153}
154
155
156//-----------------------------------------------------------------------------
157XAresult android_Player_checkSourceSink(CMediaPlayer *mp) {
158
159    XAresult result = XA_RESULT_SUCCESS;
160
161    const SLDataSource *pSrc    = &mp->mDataSource.u.mSource;
162    const SLDataSink *pAudioSnk = &mp->mAudioSink.u.mSink;
163
164    // format check:
165    const SLuint32 sourceLocatorType = *(SLuint32 *)pSrc->pLocator;
166    const SLuint32 sourceFormatType  = *(SLuint32 *)pSrc->pFormat;
167    const SLuint32 audioSinkLocatorType = *(SLuint32 *)pAudioSnk->pLocator;
168    //const SLuint32 sinkFormatType = *(SLuint32 *)pAudioSnk->pFormat;
169
170    // Source check
171    switch(sourceLocatorType) {
172
173    case XA_DATALOCATOR_ANDROIDBUFFERQUEUE: {
174        switch (sourceFormatType) {
175        case XA_DATAFORMAT_MIME: {
176            SLDataFormat_MIME *df_mime = (SLDataFormat_MIME *) pSrc->pFormat;
177            if (SL_CONTAINERTYPE_MPEG_TS != df_mime->containerType) {
178                SL_LOGE("Cannot create player with XA_DATALOCATOR_ANDROIDBUFFERQUEUE data source "
179                        "that is not fed MPEG-2 TS data");
180                return SL_RESULT_CONTENT_UNSUPPORTED;
181            }
182        } break;
183        default:
184            SL_LOGE("Cannot create player with XA_DATALOCATOR_ANDROIDBUFFERQUEUE data source "
185                    "without SL_DATAFORMAT_MIME format");
186            return XA_RESULT_CONTENT_UNSUPPORTED;
187        }
188    } break;
189
190    case XA_DATALOCATOR_URI: // intended fall-through
191    case XA_DATALOCATOR_ANDROIDFD:
192        break;
193
194    default:
195        SL_LOGE("Cannot create media player with data locator type 0x%x",
196                (unsigned) sourceLocatorType);
197        return SL_RESULT_PARAMETER_INVALID;
198    }// switch (locatorType)
199
200    // Audio sink check: only playback is supported here
201    switch(audioSinkLocatorType) {
202
203    case XA_DATALOCATOR_OUTPUTMIX:
204        break;
205
206    default:
207        SL_LOGE("Cannot create media player with audio sink data locator of type 0x%x",
208                (unsigned) audioSinkLocatorType);
209        return XA_RESULT_PARAMETER_INVALID;
210    }// switch (locaaudioSinkLocatorTypeorType)
211
212    return result;
213}
214
215
216//-----------------------------------------------------------------------------
217XAresult android_Player_create(CMediaPlayer *mp) {
218
219    XAresult result = XA_RESULT_SUCCESS;
220
221    // FIXME verify data source
222    const SLDataSource *pDataSrc = &mp->mDataSource.u.mSource;
223    // FIXME verify audio data sink
224    const SLDataSink *pAudioSnk = &mp->mAudioSink.u.mSink;
225    // FIXME verify image data sink
226    const SLDataSink *pVideoSnk = &mp->mImageVideoSink.u.mSink;
227
228    XAuint32 sourceLocator = *(XAuint32 *)pDataSrc->pLocator;
229    switch(sourceLocator) {
230    // FIXME support Android simple buffer queue as well
231    case XA_DATALOCATOR_ANDROIDBUFFERQUEUE:
232        mp->mAndroidObjType = AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE;
233        break;
234    case XA_DATALOCATOR_URI: // intended fall-through
235    case SL_DATALOCATOR_ANDROIDFD:
236        mp->mAndroidObjType = AUDIOVIDEOPLAYER_FROM_URIFD;
237        break;
238    case XA_DATALOCATOR_ADDRESS: // intended fall-through
239    default:
240        SL_LOGE("Unable to create MediaPlayer for data source locator 0x%x", sourceLocator);
241        result = XA_RESULT_PARAMETER_INVALID;
242        break;
243    }
244
245    // FIXME duplicates an initialization also done by higher level
246    mp->mAndroidObjState = ANDROID_UNINITIALIZED;
247    mp->mStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE;
248    mp->mSessionId = android::AudioSystem::newAudioSessionId();
249
250    return result;
251}
252
253
254//-----------------------------------------------------------------------------
255// FIXME abstract out the diff between CMediaPlayer and CAudioPlayer
256XAresult android_Player_realize(CMediaPlayer *mp, SLboolean async) {
257    SL_LOGV("android_Player_realize_l(%p)", mp);
258    XAresult result = XA_RESULT_SUCCESS;
259
260    const SLDataSource *pDataSrc = &mp->mDataSource.u.mSource;
261    const SLuint32 sourceLocator = *(SLuint32 *)pDataSrc->pLocator;
262
263    AudioPlayback_Parameters ap_params;
264    ap_params.sessionId = mp->mSessionId;
265    ap_params.streamType = mp->mStreamType;
266    ap_params.trackcb = NULL;
267    ap_params.trackcbUser = NULL;
268
269    switch(mp->mAndroidObjType) {
270    case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: {
271        mp->mAVPlayer = new android::StreamPlayer(&ap_params, true /*hasVideo*/);
272        mp->mAVPlayer->init(player_handleMediaPlayerEventNotifications, (void*)mp);
273        }
274        break;
275    case AUDIOVIDEOPLAYER_FROM_URIFD: {
276        mp->mAVPlayer = new android::LocAVPlayer(&ap_params, true /*hasVideo*/);
277        mp->mAVPlayer->init(player_handleMediaPlayerEventNotifications, (void*)mp);
278        switch (mp->mDataSource.mLocator.mLocatorType) {
279        case XA_DATALOCATOR_URI:
280            ((android::LocAVPlayer*)mp->mAVPlayer.get())->setDataSource(
281                    (const char*)mp->mDataSource.mLocator.mURI.URI);
282            break;
283        case XA_DATALOCATOR_ANDROIDFD: {
284            int64_t offset = (int64_t)mp->mDataSource.mLocator.mFD.offset;
285            ((android::LocAVPlayer*)mp->mAVPlayer.get())->setDataSource(
286                    (int)mp->mDataSource.mLocator.mFD.fd,
287                    offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ?
288                            (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset,
289                    (int64_t)mp->mDataSource.mLocator.mFD.length);
290            }
291            break;
292        default:
293            SL_LOGE("Invalid or unsupported data locator type %u for data source",
294                    mp->mDataSource.mLocator.mLocatorType);
295            result = XA_RESULT_PARAMETER_INVALID;
296        }
297        }
298        break;
299    case INVALID_TYPE: // intended fall-through
300    default:
301        SL_LOGE("Unable to realize MediaPlayer, invalid internal Android object type");
302        result = XA_RESULT_PARAMETER_INVALID;
303        break;
304    }
305
306    if (XA_RESULT_SUCCESS == result) {
307
308        // if there is a video sink
309        if (XA_DATALOCATOR_NATIVEDISPLAY ==
310                mp->mImageVideoSink.mLocator.mLocatorType) {
311            ANativeWindow *nativeWindow = (ANativeWindow *)
312                    mp->mImageVideoSink.mLocator.mNativeDisplay.hWindow;
313            // we already verified earlier that hWindow is non-NULL
314            assert(nativeWindow != NULL);
315            result = android_Player_setNativeWindow(mp, nativeWindow);
316        }
317
318    }
319
320    return result;
321}
322
323//-----------------------------------------------------------------------------
324XAresult android_Player_destroy(CMediaPlayer *mp) {
325    SL_LOGV("android_Player_destroy(%p)", mp);
326    XAresult result = XA_RESULT_SUCCESS;
327
328    if (mp->mAVPlayer != 0) {
329        mp->mAVPlayer.clear();
330    }
331
332    return result;
333}
334
335
336XAresult android_Player_getDuration(IPlay *pPlayItf, XAmillisecond *pDurMsec) {
337    XAresult result = XA_RESULT_SUCCESS;
338    CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis;
339
340    switch (avp->mAndroidObjType) {
341
342    case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
343    case AUDIOVIDEOPLAYER_FROM_URIFD: {
344        int dur = -1;
345        if (avp->mAVPlayer != 0) {
346            avp->mAVPlayer->getDurationMsec(&dur);
347        }
348        if (dur == ANDROID_UNKNOWN_TIME) {
349            *pDurMsec = XA_TIME_UNKNOWN;
350        } else {
351            *pDurMsec = (XAmillisecond)dur;
352        }
353    } break;
354
355    default:
356        // we shouldn't be here
357        assert(false);
358        break;
359    }
360
361    return result;
362}
363
364
365XAresult android_Player_getPosition(IPlay *pPlayItf, XAmillisecond *pPosMsec) {
366    SL_LOGD("android_Player_getPosition()");
367    XAresult result = XA_RESULT_SUCCESS;
368    CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis;
369
370    switch (avp->mAndroidObjType) {
371
372    case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
373    case AUDIOVIDEOPLAYER_FROM_URIFD: {
374        int pos = -1;
375        if (avp->mAVPlayer != 0) {
376            avp->mAVPlayer->getPositionMsec(&pos);
377        }
378        if (pos == ANDROID_UNKNOWN_TIME) {
379            *pPosMsec = XA_TIME_UNKNOWN;
380        } else {
381            *pPosMsec = (XAmillisecond)pos;
382        }
383    } break;
384
385    default:
386        // we shouldn't be here
387        assert(false);
388        break;
389    }
390
391    return result;
392}
393
394
395//-----------------------------------------------------------------------------
396/**
397 * pre-condition: mp != NULL
398 */
399void android_Player_volumeUpdate(CMediaPlayer* mp)
400{
401    android::GenericPlayer* avp = mp->mAVPlayer.get();
402    if (avp != NULL) {
403        float volumes[2];
404        // MediaPlayer does not currently support EffectSend or MuteSolo
405        android_player_volumeUpdate(volumes, &mp->mVolume, mp->mNumChannels, 1.0f, NULL);
406        float leftVol = volumes[0], rightVol = volumes[1];
407        avp->setVolume(leftVol, rightVol);
408    }
409}
410
411//-----------------------------------------------------------------------------
412/**
413 * pre-condition: gp != 0
414 */
415XAresult android_Player_setPlayState(const android::sp<android::GenericPlayer> &gp,
416        SLuint32 playState,
417        AndroidObjectState* pObjState)
418{
419    XAresult result = XA_RESULT_SUCCESS;
420    AndroidObjectState objState = *pObjState;
421
422    switch (playState) {
423     case SL_PLAYSTATE_STOPPED: {
424         SL_LOGV("setting AVPlayer to SL_PLAYSTATE_STOPPED");
425         gp->stop();
426         }
427         break;
428     case SL_PLAYSTATE_PAUSED: {
429         SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PAUSED");
430         switch(objState) {
431         case ANDROID_UNINITIALIZED:
432             *pObjState = ANDROID_PREPARING;
433             gp->prepare();
434             break;
435         case ANDROID_PREPARING:
436             break;
437         case ANDROID_READY:
438             gp->pause();
439             break;
440         default:
441             SL_LOGE("Android object in invalid state");
442             break;
443         }
444         }
445         break;
446     case SL_PLAYSTATE_PLAYING: {
447         SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PLAYING");
448         switch(objState) {
449         case ANDROID_UNINITIALIZED:
450             *pObjState = ANDROID_PREPARING;
451             gp->prepare();
452             // intended fall through
453         case ANDROID_PREPARING:
454             // intended fall through
455         case ANDROID_READY:
456             gp->play();
457             break;
458         default:
459             SL_LOGE("Android object in invalid state");
460             break;
461         }
462         }
463         break;
464     default:
465         // checked by caller, should not happen
466         break;
467     }
468
469    return result;
470}
471
472
473/**
474 * pre-condition: mp != NULL
475 */
476XAresult android_Player_seek(CMediaPlayer *mp, SLmillisecond posMsec) {
477    XAresult result = XA_RESULT_SUCCESS;
478    switch (mp->mAndroidObjType) {
479      case AUDIOVIDEOPLAYER_FROM_URIFD:
480        if (mp->mAVPlayer !=0) {
481            mp->mAVPlayer->seek(posMsec);
482        }
483        break;
484      case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
485      default: {
486          result = XA_RESULT_PARAMETER_INVALID;
487      }
488    }
489    return result;
490}
491
492
493/**
494 * pre-condition: mp != NULL
495 */
496XAresult android_Player_loop(CMediaPlayer *mp, SLboolean loopEnable) {
497    XAresult result = XA_RESULT_SUCCESS;
498    switch (mp->mAndroidObjType) {
499      case AUDIOVIDEOPLAYER_FROM_URIFD:
500        if (mp->mAVPlayer !=0) {
501            mp->mAVPlayer->loop(loopEnable);
502        }
503        break;
504      case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
505      default: {
506          result = XA_RESULT_PARAMETER_INVALID;
507      }
508    }
509    return result;
510}
511
512
513//-----------------------------------------------------------------------------
514void android_Player_androidBufferQueue_registerCallback_l(CMediaPlayer *mp) {
515    if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE)
516            && (mp->mAVPlayer != 0)) {
517        SL_LOGD("android_Player_androidBufferQueue_registerCallback_l");
518        android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get());
519        splr->registerQueueCallback(
520                (const void*)mp, false /*userIsAudioPlayer*/,
521                mp->mAndroidBufferQueue.mContext, (const void*)&(mp->mAndroidBufferQueue.mItf));
522
523    }
524}
525
526
527void android_Player_androidBufferQueue_clear_l(CMediaPlayer *mp) {
528    if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE)
529            && (mp->mAVPlayer != 0)) {
530        android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get());
531        splr->appClear_l();
532    }
533}
534
535
536void android_Player_androidBufferQueue_onRefilled_l(CMediaPlayer *mp) {
537    if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE)
538            && (mp->mAVPlayer != 0)) {
539        android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get());
540        splr->queueRefilled_l();
541    }
542}
543
544
545/*
546 *  pre-conditions:
547 *      mp != NULL
548 *      mp->mAVPlayer != 0 (player is realized)
549 *      nativeWindow can be NULL, but if NULL it is treated as an error
550 */
551SLresult android_Player_setNativeWindow(CMediaPlayer *mp, ANativeWindow *nativeWindow)
552{
553    assert(mp != NULL);
554    assert(mp->mAVPlayer != 0);
555    if (nativeWindow == NULL) {
556        SL_LOGE("ANativeWindow is NULL");
557        return SL_RESULT_PARAMETER_INVALID;
558    }
559    SLresult result;
560    int err;
561    int value;
562    // this could crash if app passes in a bad parameter, but that's OK
563    err = (*nativeWindow->query)(nativeWindow, NATIVE_WINDOW_CONCRETE_TYPE, &value);
564    if (0 != err) {
565        SL_LOGE("Query NATIVE_WINDOW_CONCRETE_TYPE on ANativeWindow * %p failed; "
566                "errno %d", nativeWindow, err);
567        result = SL_RESULT_PARAMETER_INVALID;
568    } else {
569        switch (value) {
570        case NATIVE_WINDOW_SURFACE: {                // Surface
571            SL_LOGV("Displaying on ANativeWindow of type NATIVE_WINDOW_SURFACE");
572            android::sp<android::Surface> nativeSurface(
573                    static_cast<android::Surface *>(nativeWindow));
574            mp->mAVPlayer->setVideoSurface(nativeSurface);
575            result = SL_RESULT_SUCCESS;
576            } break;
577        case NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT: { // SurfaceTextureClient
578            SL_LOGV("Displaying on ANativeWindow of type NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT");
579            android::sp<android::SurfaceTextureClient> surfaceTextureClient(
580                    static_cast<android::SurfaceTextureClient *>(nativeWindow));
581            android::sp<android::ISurfaceTexture> nativeSurfaceTexture(
582                    surfaceTextureClient->getISurfaceTexture());
583            mp->mAVPlayer->setVideoSurfaceTexture(nativeSurfaceTexture);
584            result = SL_RESULT_SUCCESS;
585            } break;
586        case NATIVE_WINDOW_FRAMEBUFFER:              // FramebufferNativeWindow
587            // fall through
588        default:
589            SL_LOGE("ANativeWindow * %p has unknown or unsupported concrete type %d",
590                    nativeWindow, value);
591            result = SL_RESULT_PARAMETER_INVALID;
592            break;
593        }
594    }
595    return result;
596}
597