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