OSDN Git Service

Merge "Fix 5249076 Don't let the shared memory buffer drain"
[android-x86/system-media.git] / wilhelm / src / android / MediaPlayer_to_android.cpp
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 //-----------------------------------------------------------------------------
27 static 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 //-----------------------------------------------------------------------------
257 XAresult 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 //-----------------------------------------------------------------------------
317 XAresult 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     mp->mCallbackProtector = new android::CallbackProtector();
351
352     return result;
353 }
354
355
356 //-----------------------------------------------------------------------------
357 // FIXME abstract out the diff between CMediaPlayer and CAudioPlayer
358 XAresult android_Player_realize(CMediaPlayer *mp, SLboolean async) {
359     SL_LOGV("android_Player_realize_l(%p)", mp);
360     XAresult result = XA_RESULT_SUCCESS;
361
362     const SLDataSource *pDataSrc = &mp->mDataSource.u.mSource;
363     const SLuint32 sourceLocator = *(SLuint32 *)pDataSrc->pLocator;
364
365     AudioPlayback_Parameters ap_params;
366     ap_params.sessionId = mp->mSessionId;
367     ap_params.streamType = mp->mStreamType;
368     ap_params.trackcb = NULL;
369     ap_params.trackcbUser = NULL;
370
371     switch(mp->mAndroidObjType) {
372     case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: {
373         mp->mAVPlayer = new android::StreamPlayer(&ap_params, true /*hasVideo*/);
374         mp->mAVPlayer->init(player_handleMediaPlayerEventNotifications, (void*)mp);
375         }
376         break;
377     case AUDIOVIDEOPLAYER_FROM_URIFD: {
378         mp->mAVPlayer = new android::LocAVPlayer(&ap_params, true /*hasVideo*/);
379         mp->mAVPlayer->init(player_handleMediaPlayerEventNotifications, (void*)mp);
380         switch (mp->mDataSource.mLocator.mLocatorType) {
381         case XA_DATALOCATOR_URI:
382             ((android::LocAVPlayer*)mp->mAVPlayer.get())->setDataSource(
383                     (const char*)mp->mDataSource.mLocator.mURI.URI);
384             break;
385         case XA_DATALOCATOR_ANDROIDFD: {
386             int64_t offset = (int64_t)mp->mDataSource.mLocator.mFD.offset;
387             ((android::LocAVPlayer*)mp->mAVPlayer.get())->setDataSource(
388                     (int)mp->mDataSource.mLocator.mFD.fd,
389                     offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ?
390                             (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset,
391                     (int64_t)mp->mDataSource.mLocator.mFD.length);
392             }
393             break;
394         default:
395             SL_LOGE("Invalid or unsupported data locator type %u for data source",
396                     mp->mDataSource.mLocator.mLocatorType);
397             result = XA_RESULT_PARAMETER_INVALID;
398         }
399         }
400         break;
401     case INVALID_TYPE: // intended fall-through
402     default:
403         SL_LOGE("Unable to realize MediaPlayer, invalid internal Android object type");
404         result = XA_RESULT_PARAMETER_INVALID;
405         break;
406     }
407
408     if (XA_RESULT_SUCCESS == result) {
409
410         // inform GenericPlayer of the associated callback protector
411         mp->mAVPlayer->setCallbackProtector(mp->mCallbackProtector);
412
413         // if there is a video sink
414         if (XA_DATALOCATOR_NATIVEDISPLAY ==
415                 mp->mImageVideoSink.mLocator.mLocatorType) {
416             ANativeWindow *nativeWindow = (ANativeWindow *)
417                     mp->mImageVideoSink.mLocator.mNativeDisplay.hWindow;
418             // we already verified earlier that hWindow is non-NULL
419             assert(nativeWindow != NULL);
420             result = android_Player_setNativeWindow(mp, nativeWindow);
421         }
422
423     }
424
425     return result;
426 }
427
428 // Called with a lock on MediaPlayer, and blocks until safe to destroy
429 XAresult android_Player_preDestroy(CMediaPlayer *mp) {
430     SL_LOGV("android_Player_preDestroy(%p)", mp);
431     if (mp->mAVPlayer != 0) {
432         mp->mAVPlayer->preDestroy();
433     }
434     SL_LOGV("android_Player_preDestroy(%p) after mAVPlayer->preDestroy()", mp);
435
436     object_unlock_exclusive(&mp->mObject);
437     if (mp->mCallbackProtector != 0) {
438         mp->mCallbackProtector->requestCbExitAndWait();
439     }
440     object_lock_exclusive(&mp->mObject);
441
442     return XA_RESULT_SUCCESS;
443 }
444
445 //-----------------------------------------------------------------------------
446 XAresult android_Player_destroy(CMediaPlayer *mp) {
447     SL_LOGV("android_Player_destroy(%p)", mp);
448
449     mp->mAVPlayer.clear();
450     mp->mCallbackProtector.clear();
451
452     // explicit destructor
453     mp->mAVPlayer.~sp();
454     mp->mCallbackProtector.~sp();
455
456     return XA_RESULT_SUCCESS;
457 }
458
459
460 void android_Player_usePlayEventMask(CMediaPlayer *mp) {
461     if (mp->mAVPlayer != 0) {
462         IPlay *pPlayItf = &mp->mPlay;
463         mp->mAVPlayer->setPlayEvents((int32_t) pPlayItf->mEventFlags,
464                 (int32_t) pPlayItf->mMarkerPosition, (int32_t) pPlayItf->mPositionUpdatePeriod);
465     }
466 }
467
468
469 XAresult android_Player_getDuration(IPlay *pPlayItf, XAmillisecond *pDurMsec) {
470     CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis;
471
472     switch (avp->mAndroidObjType) {
473
474     case AUDIOVIDEOPLAYER_FROM_URIFD: {
475         int dur = ANDROID_UNKNOWN_TIME;
476         if (avp->mAVPlayer != 0) {
477             avp->mAVPlayer->getDurationMsec(&dur);
478         }
479         if (dur == ANDROID_UNKNOWN_TIME) {
480             *pDurMsec = XA_TIME_UNKNOWN;
481         } else {
482             *pDurMsec = (XAmillisecond)dur;
483         }
484     } break;
485
486     case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
487     default:
488         *pDurMsec = XA_TIME_UNKNOWN;
489         break;
490     }
491
492     return XA_RESULT_SUCCESS;
493 }
494
495
496 XAresult android_Player_getPosition(IPlay *pPlayItf, XAmillisecond *pPosMsec) {
497     SL_LOGD("android_Player_getPosition()");
498     XAresult result = XA_RESULT_SUCCESS;
499     CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis;
500
501     switch (avp->mAndroidObjType) {
502
503     case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
504     case AUDIOVIDEOPLAYER_FROM_URIFD: {
505         int pos = ANDROID_UNKNOWN_TIME;
506         if (avp->mAVPlayer != 0) {
507             avp->mAVPlayer->getPositionMsec(&pos);
508         }
509         if (pos == ANDROID_UNKNOWN_TIME) {
510             *pPosMsec = 0;
511         } else {
512             *pPosMsec = (XAmillisecond)pos;
513         }
514     } break;
515
516     default:
517         // we shouldn't be here
518         assert(false);
519         break;
520     }
521
522     return result;
523 }
524
525
526 //-----------------------------------------------------------------------------
527 /**
528  * pre-condition: mp != NULL
529  */
530 void android_Player_volumeUpdate(CMediaPlayer* mp)
531 {
532     android::GenericPlayer* avp = mp->mAVPlayer.get();
533     if (avp != NULL) {
534         float volumes[2];
535         // MediaPlayer does not currently support EffectSend or MuteSolo
536         android_player_volumeUpdate(volumes, &mp->mVolume, mp->mNumChannels, 1.0f, NULL);
537         float leftVol = volumes[0], rightVol = volumes[1];
538         avp->setVolume(leftVol, rightVol);
539     }
540 }
541
542 //-----------------------------------------------------------------------------
543 /**
544  * pre-condition: gp != 0
545  */
546 XAresult android_Player_setPlayState(const android::sp<android::GenericPlayer> &gp,
547         SLuint32 playState,
548         AndroidObjectState* pObjState)
549 {
550     XAresult result = XA_RESULT_SUCCESS;
551     AndroidObjectState objState = *pObjState;
552
553     switch (playState) {
554      case SL_PLAYSTATE_STOPPED: {
555          SL_LOGV("setting AVPlayer to SL_PLAYSTATE_STOPPED");
556          gp->stop();
557          }
558          break;
559      case SL_PLAYSTATE_PAUSED: {
560          SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PAUSED");
561          switch(objState) {
562          case ANDROID_UNINITIALIZED:
563              *pObjState = ANDROID_PREPARING;
564              gp->prepare();
565              break;
566          case ANDROID_PREPARING:
567              break;
568          case ANDROID_READY:
569              gp->pause();
570              break;
571          default:
572              SL_LOGE("Android object in invalid state");
573              break;
574          }
575          }
576          break;
577      case SL_PLAYSTATE_PLAYING: {
578          SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PLAYING");
579          switch(objState) {
580          case ANDROID_UNINITIALIZED:
581              *pObjState = ANDROID_PREPARING;
582              gp->prepare();
583              // intended fall through
584          case ANDROID_PREPARING:
585              // intended fall through
586          case ANDROID_READY:
587              gp->play();
588              break;
589          default:
590              SL_LOGE("Android object in invalid state");
591              break;
592          }
593          }
594          break;
595      default:
596          // checked by caller, should not happen
597          break;
598      }
599
600     return result;
601 }
602
603
604 /**
605  * pre-condition: mp != NULL
606  */
607 XAresult android_Player_seek(CMediaPlayer *mp, SLmillisecond posMsec) {
608     XAresult result = XA_RESULT_SUCCESS;
609     switch (mp->mAndroidObjType) {
610       case AUDIOVIDEOPLAYER_FROM_URIFD:
611         if (mp->mAVPlayer !=0) {
612             mp->mAVPlayer->seek(posMsec);
613         }
614         break;
615       case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
616       default: {
617           result = XA_RESULT_FEATURE_UNSUPPORTED;
618       }
619     }
620     return result;
621 }
622
623
624 /**
625  * pre-condition: mp != NULL
626  */
627 XAresult android_Player_loop(CMediaPlayer *mp, SLboolean loopEnable) {
628     XAresult result = XA_RESULT_SUCCESS;
629     switch (mp->mAndroidObjType) {
630       case AUDIOVIDEOPLAYER_FROM_URIFD:
631         if (mp->mAVPlayer !=0) {
632             mp->mAVPlayer->loop(loopEnable);
633         }
634         break;
635       case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
636       default: {
637           result = XA_RESULT_FEATURE_UNSUPPORTED;
638       }
639     }
640     return result;
641 }
642
643
644 //-----------------------------------------------------------------------------
645 void android_Player_androidBufferQueue_registerCallback_l(CMediaPlayer *mp) {
646     if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE)
647             && (mp->mAVPlayer != 0)) {
648         SL_LOGD("android_Player_androidBufferQueue_registerCallback_l");
649         android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get());
650         splr->registerQueueCallback(
651                 (const void*)mp, false /*userIsAudioPlayer*/,
652                 mp->mAndroidBufferQueue.mContext, (const void*)&(mp->mAndroidBufferQueue.mItf));
653
654     }
655 }
656
657
658 void android_Player_androidBufferQueue_clear_l(CMediaPlayer *mp) {
659     if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE)
660             && (mp->mAVPlayer != 0)) {
661         android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get());
662         splr->appClear_l();
663     }
664 }
665
666
667 void android_Player_androidBufferQueue_onRefilled_l(CMediaPlayer *mp) {
668     if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE)
669             && (mp->mAVPlayer != 0)) {
670         android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get());
671         splr->queueRefilled();
672     }
673 }
674
675
676 /*
677  *  pre-conditions:
678  *      mp != NULL
679  *      mp->mAVPlayer != 0 (player is realized)
680  *      nativeWindow can be NULL, but if NULL it is treated as an error
681  */
682 SLresult android_Player_setNativeWindow(CMediaPlayer *mp, ANativeWindow *nativeWindow)
683 {
684     assert(mp != NULL);
685     assert(mp->mAVPlayer != 0);
686     if (nativeWindow == NULL) {
687         SL_LOGE("ANativeWindow is NULL");
688         return SL_RESULT_PARAMETER_INVALID;
689     }
690     SLresult result;
691     int err;
692     int value;
693     // this could crash if app passes in a bad parameter, but that's OK
694     err = (*nativeWindow->query)(nativeWindow, NATIVE_WINDOW_CONCRETE_TYPE, &value);
695     if (0 != err) {
696         SL_LOGE("Query NATIVE_WINDOW_CONCRETE_TYPE on ANativeWindow * %p failed; "
697                 "errno %d", nativeWindow, err);
698         result = SL_RESULT_PARAMETER_INVALID;
699     } else {
700         switch (value) {
701         case NATIVE_WINDOW_SURFACE: {                // Surface
702             SL_LOGV("Displaying on ANativeWindow of type NATIVE_WINDOW_SURFACE");
703             android::sp<android::Surface> nativeSurface(
704                     static_cast<android::Surface *>(nativeWindow));
705             mp->mAVPlayer->setVideoSurface(nativeSurface);
706             result = SL_RESULT_SUCCESS;
707             } break;
708         case NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT: { // SurfaceTextureClient
709             SL_LOGV("Displaying on ANativeWindow of type NATIVE_WINDOW_SURFACE_TEXTURE_CLIENT");
710             android::sp<android::SurfaceTextureClient> surfaceTextureClient(
711                     static_cast<android::SurfaceTextureClient *>(nativeWindow));
712             android::sp<android::ISurfaceTexture> nativeSurfaceTexture(
713                     surfaceTextureClient->getISurfaceTexture());
714             mp->mAVPlayer->setVideoSurfaceTexture(nativeSurfaceTexture);
715             result = SL_RESULT_SUCCESS;
716             } break;
717         case NATIVE_WINDOW_FRAMEBUFFER:              // FramebufferNativeWindow
718             // fall through
719         default:
720             SL_LOGE("ANativeWindow * %p has unknown or unsupported concrete type %d",
721                     nativeWindow, value);
722             result = SL_RESULT_PARAMETER_INVALID;
723             break;
724         }
725     }
726     return result;
727 }