OSDN Git Service

bbc60a6ad69c6b25d91c04d9a5059b13371fec01
[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     if (NULL == user) {
30         return;
31     }
32
33     CMediaPlayer* mp = (CMediaPlayer*) user;
34     SL_LOGV("received event %d, data %d from AVPlayer", event, data1);
35
36     switch(event) {
37
38       case android::GenericPlayer::kEventPrepared: {
39         if (PLAYER_SUCCESS == data1) {
40             object_lock_exclusive(&mp->mObject);
41             SL_LOGV("Received AVPlayer::kEventPrepared from AVPlayer for CMediaPlayer %p", mp);
42             mp->mAndroidObjState = ANDROID_READY;
43             object_unlock_exclusive(&mp->mObject);
44         }
45         break;
46       }
47
48       case android::GenericPlayer::kEventHasVideoSize: {
49         SL_LOGV("Received AVPlayer::kEventHasVideoSize (%d,%d) for CMediaPlayer %p",
50                 data1, data2, mp);
51
52         object_lock_exclusive(&mp->mObject);
53
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.duration = XA_TIME_UNKNOWN;
69         StreamInfo &contInfo = mp->mStreamInfo.mStreamInfoTable.editItemAt(0);
70         contInfo.containerInfo.numStreams = 1;
71         ssize_t index = mp->mStreamInfo.mStreamInfoTable.add(streamInfo);
72
73         xaStreamEventChangeCallback callback = mp->mStreamInfo.mCallback;
74         void* callbackPContext = mp->mStreamInfo.mContext;
75
76         object_unlock_exclusive(&mp->mObject);
77
78         // enqueue notification (outside of lock) that the stream information has been updated
79         if ((NULL != callback) && (index >= 0)) {
80 #ifdef XA_SYNCHRONOUS_STREAMCBEVENT_PROPERTYCHANGE
81             (*callback)(&mp->mStreamInfo.mItf, XA_STREAMCBEVENT_PROPERTYCHANGE /*eventId*/,
82                     1 /*streamIndex, only one stream supported here, 0 is reserved*/,
83                     NULL /*pEventData, always NULL in OpenMAX AL 1.0.1*/,
84                     callbackPContext /*pContext*/);
85 #else
86             SLresult res = EnqueueAsyncCallback_piipp(mp, callback,
87                     /*p1*/ &mp->mStreamInfo.mItf,
88                     /*i1*/ XA_STREAMCBEVENT_PROPERTYCHANGE /*eventId*/,
89                     /*i2*/ 1 /*streamIndex, only one stream supported here, 0 is reserved*/,
90                     /*p2*/ NULL /*pEventData, always NULL in OpenMAX AL 1.0.1*/,
91                     /*p3*/ callbackPContext /*pContext*/);
92 #endif
93         }
94         break;
95       }
96
97       case android::GenericPlayer::kEventEndOfStream: {
98         SL_LOGV("Received AVPlayer::kEventEndOfStream for CMediaPlayer %p", mp);
99
100         object_lock_exclusive(&mp->mObject);
101         // should be xaPlayCallback but we're sharing the itf between SL and AL
102         slPlayCallback playCallback = NULL;
103         void * playContext = NULL;
104         // XAPlayItf callback or no callback?
105         if (mp->mPlay.mEventFlags & XA_PLAYEVENT_HEADATEND) {
106             playCallback = mp->mPlay.mCallback;
107             playContext = mp->mPlay.mContext;
108         }
109         object_unlock_exclusive(&mp->mObject);
110
111         // enqueue callback with no lock held
112         if (NULL != playCallback) {
113 #ifdef XA_SYNCHRONOUS_PLAYEVENT_HEADATEND
114             (*playCallback)(&mp->mPlay.mItf, playContext, XA_PLAYEVENT_HEADATEND);
115 #else
116             SLresult res = EnqueueAsyncCallback_ppi(mp, playCallback, &mp->mPlay.mItf, playContext,
117                     XA_PLAYEVENT_HEADATEND);
118             LOGW_IF(SL_RESULT_SUCCESS != res,
119                     "Callback %p(%p, %p, XA_PLAYEVENT_HEADATEND) dropped", playCallback,
120                     &mp->mPlay.mItf, playContext);
121 #endif
122         }
123         break;
124       }
125
126       default: {
127         SL_LOGE("Received unknown event %d, data %d from AVPlayer", event, data1);
128       }
129     }
130 }
131
132
133 //-----------------------------------------------------------------------------
134 XAresult android_Player_checkSourceSink(CMediaPlayer *mp) {
135
136     XAresult result = XA_RESULT_SUCCESS;
137
138     const SLDataSource *pSrc    = &mp->mDataSource.u.mSource;
139     const SLDataSink *pAudioSnk = &mp->mAudioSink.u.mSink;
140
141     // format check:
142     const SLuint32 sourceLocatorType = *(SLuint32 *)pSrc->pLocator;
143     const SLuint32 sourceFormatType  = *(SLuint32 *)pSrc->pFormat;
144     const SLuint32 audioSinkLocatorType = *(SLuint32 *)pAudioSnk->pLocator;
145     //const SLuint32 sinkFormatType = *(SLuint32 *)pAudioSnk->pFormat;
146
147     // Source check
148     switch(sourceLocatorType) {
149
150     case XA_DATALOCATOR_ANDROIDBUFFERQUEUE: {
151         switch (sourceFormatType) {
152         case XA_DATAFORMAT_MIME: {
153             SLDataFormat_MIME *df_mime = (SLDataFormat_MIME *) pSrc->pFormat;
154             if (SL_CONTAINERTYPE_MPEG_TS != df_mime->containerType) {
155                 SL_LOGE("Cannot create player with XA_DATALOCATOR_ANDROIDBUFFERQUEUE data source "
156                         "that is not fed MPEG-2 TS data");
157                 return SL_RESULT_CONTENT_UNSUPPORTED;
158             }
159         } break;
160         default:
161             SL_LOGE("Cannot create player with XA_DATALOCATOR_ANDROIDBUFFERQUEUE data source "
162                     "without SL_DATAFORMAT_MIME format");
163             return XA_RESULT_CONTENT_UNSUPPORTED;
164         }
165     } break;
166
167     case XA_DATALOCATOR_URI: // intended fall-through
168     case XA_DATALOCATOR_ANDROIDFD:
169         break;
170
171     default:
172         SL_LOGE("Cannot create media player with data locator type 0x%x",
173                 (unsigned) sourceLocatorType);
174         return SL_RESULT_PARAMETER_INVALID;
175     }// switch (locatorType)
176
177     // Audio sink check: only playback is supported here
178     switch(audioSinkLocatorType) {
179
180     case XA_DATALOCATOR_OUTPUTMIX:
181         break;
182
183     default:
184         SL_LOGE("Cannot create media player with audio sink data locator of type 0x%x",
185                 (unsigned) audioSinkLocatorType);
186         return XA_RESULT_PARAMETER_INVALID;
187     }// switch (locaaudioSinkLocatorTypeorType)
188
189     return result;
190 }
191
192
193 //-----------------------------------------------------------------------------
194 XAresult android_Player_create(CMediaPlayer *mp) {
195
196     XAresult result = XA_RESULT_SUCCESS;
197
198     // FIXME verify data source
199     const SLDataSource *pDataSrc = &mp->mDataSource.u.mSource;
200     // FIXME verify audio data sink
201     const SLDataSink *pAudioSnk = &mp->mAudioSink.u.mSink;
202     // FIXME verify image data sink
203     const SLDataSink *pVideoSnk = &mp->mImageVideoSink.u.mSink;
204
205     XAuint32 sourceLocator = *(XAuint32 *)pDataSrc->pLocator;
206     switch(sourceLocator) {
207     // FIXME support Android simple buffer queue as well
208     case XA_DATALOCATOR_ANDROIDBUFFERQUEUE:
209         mp->mAndroidObjType = AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE;
210         break;
211     case XA_DATALOCATOR_URI: // intended fall-through
212     case SL_DATALOCATOR_ANDROIDFD:
213         mp->mAndroidObjType = AUDIOVIDEOPLAYER_FROM_URIFD;
214         break;
215     case XA_DATALOCATOR_ADDRESS: // intended fall-through
216     default:
217         SL_LOGE("Unable to create MediaPlayer for data source locator 0x%x", sourceLocator);
218         result = XA_RESULT_PARAMETER_INVALID;
219         break;
220     }
221
222     // FIXME duplicates an initialization also done by higher level
223     mp->mAndroidObjState = ANDROID_UNINITIALIZED;
224     mp->mStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE;
225     mp->mSessionId = android::AudioSystem::newAudioSessionId();
226
227     mp->mDirectLevel = 0; // no attenuation
228
229     return result;
230 }
231
232
233 //-----------------------------------------------------------------------------
234 // FIXME abstract out the diff between CMediaPlayer and CAudioPlayer
235 XAresult android_Player_realize(CMediaPlayer *mp, SLboolean async) {
236     SL_LOGI("android_Player_realize_l(%p)", mp);
237     XAresult result = XA_RESULT_SUCCESS;
238
239     const SLDataSource *pDataSrc = &mp->mDataSource.u.mSource;
240     const SLuint32 sourceLocator = *(SLuint32 *)pDataSrc->pLocator;
241
242     AudioPlayback_Parameters ap_params;
243     ap_params.sessionId = mp->mSessionId;
244     ap_params.streamType = mp->mStreamType;
245     ap_params.trackcb = NULL;
246     ap_params.trackcbUser = NULL;
247
248     switch(mp->mAndroidObjType) {
249     case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: {
250         mp->mAVPlayer = new android::StreamPlayer(&ap_params, true /*hasVideo*/);
251         mp->mAVPlayer->init(player_handleMediaPlayerEventNotifications, (void*)mp);
252         }
253         break;
254     case AUDIOVIDEOPLAYER_FROM_URIFD: {
255         mp->mAVPlayer = new android::LocAVPlayer(&ap_params, true /*hasVideo*/);
256         mp->mAVPlayer->init(player_handleMediaPlayerEventNotifications, (void*)mp);
257         switch (mp->mDataSource.mLocator.mLocatorType) {
258         case XA_DATALOCATOR_URI:
259             ((android::LocAVPlayer*)mp->mAVPlayer.get())->setDataSource(
260                     (const char*)mp->mDataSource.mLocator.mURI.URI);
261             break;
262         case XA_DATALOCATOR_ANDROIDFD: {
263             int64_t offset = (int64_t)mp->mDataSource.mLocator.mFD.offset;
264             ((android::LocAVPlayer*)mp->mAVPlayer.get())->setDataSource(
265                     (int)mp->mDataSource.mLocator.mFD.fd,
266                     offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ?
267                             (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset,
268                     (int64_t)mp->mDataSource.mLocator.mFD.length);
269             }
270             break;
271         default:
272             SL_LOGE("Invalid or unsupported data locator type %u for data source",
273                     mp->mDataSource.mLocator.mLocatorType);
274             result = XA_RESULT_PARAMETER_INVALID;
275         }
276         }
277         break;
278     case INVALID_TYPE: // intended fall-through
279     default:
280         SL_LOGE("Unable to realize MediaPlayer, invalid internal Android object type");
281         result = XA_RESULT_PARAMETER_INVALID;
282         break;
283     }
284
285     return result;
286 }
287
288 //-----------------------------------------------------------------------------
289 XAresult android_Player_destroy(CMediaPlayer *mp) {
290     SL_LOGI("android_Player_destroy(%p)", mp);
291     XAresult result = XA_RESULT_SUCCESS;
292
293     if (mp->mAVPlayer != 0) {
294         mp->mAVPlayer.clear();
295     }
296
297     return result;
298 }
299
300 //-----------------------------------------------------------------------------
301 /**
302  * pre-conditions: gp != 0, surface != 0
303  */
304 XAresult android_Player_setVideoSurface(const android::sp<android::GenericPlayer> &gp,
305         const android::sp<android::Surface> &surface) {
306     XAresult result = XA_RESULT_SUCCESS;
307
308     android::GenericMediaPlayer* gmp = static_cast<android::GenericMediaPlayer*>(gp.get());
309     gmp->setVideoSurface(surface);
310
311     return result;
312 }
313
314
315 /**
316  * pre-conditions: gp != 0, surfaceTexture != 0
317  */
318 XAresult android_Player_setVideoSurfaceTexture(const android::sp<android::GenericPlayer> &gp,
319         const android::sp<android::ISurfaceTexture> &surfaceTexture) {
320     XAresult result = XA_RESULT_SUCCESS;
321
322     android::GenericMediaPlayer* gmp = static_cast<android::GenericMediaPlayer*>(gp.get());
323     gmp->setVideoSurfaceTexture(surfaceTexture);
324
325     return result;
326 }
327
328
329 XAresult android_Player_getDuration(IPlay *pPlayItf, XAmillisecond *pDurMsec) {
330     XAresult result = XA_RESULT_SUCCESS;
331     CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis;
332
333     switch (avp->mAndroidObjType) {
334
335     case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
336     case AUDIOVIDEOPLAYER_FROM_URIFD: {
337         int dur = -1;
338         if (avp->mAVPlayer != 0) {
339             avp->mAVPlayer->getDurationMsec(&dur);
340         }
341         if (dur == ANDROID_UNKNOWN_TIME) {
342             *pDurMsec = XA_TIME_UNKNOWN;
343         } else {
344             *pDurMsec = (XAmillisecond)dur;
345         }
346     } break;
347
348     default:
349         // we shouldn't be here
350         assert(false);
351         break;
352     }
353
354     return result;
355 }
356
357
358 XAresult android_Player_getPosition(IPlay *pPlayItf, XAmillisecond *pPosMsec) {
359     SL_LOGD("android_Player_getPosition()");
360     XAresult result = XA_RESULT_SUCCESS;
361     CMediaPlayer *avp = (CMediaPlayer *)pPlayItf->mThis;
362
363     switch (avp->mAndroidObjType) {
364
365     case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
366     case AUDIOVIDEOPLAYER_FROM_URIFD: {
367         int pos = -1;
368         if (avp->mAVPlayer != 0) {
369             avp->mAVPlayer->getPositionMsec(&pos);
370         }
371         if (pos == ANDROID_UNKNOWN_TIME) {
372             *pPosMsec = XA_TIME_UNKNOWN;
373         } else {
374             *pPosMsec = (XAmillisecond)pos;
375         }
376     } break;
377
378     default:
379         // we shouldn't be here
380         assert(false);
381         break;
382     }
383
384     return result;
385 }
386
387
388 //-----------------------------------------------------------------------------
389 /**
390  * pre-condition: avp != 0, pVolItf != NULL
391  */
392 XAresult android_Player_volumeUpdate(const android::sp<android::GenericPlayer> &gp,
393         IVolume *pVolItf)
394 {
395     XAresult result = XA_RESULT_SUCCESS;
396
397     // FIXME broken
398 #if 0
399     gp->setVolume((bool)pVolItf->mMute, (bool)pVolItf->mEnableStereoPosition,
400             pVolItf->mStereoPosition, pVolItf->mLevel);
401 #endif
402
403     return result;
404 }
405
406 //-----------------------------------------------------------------------------
407 /**
408  * pre-condition: gp != 0
409  */
410 XAresult android_Player_setPlayState(const android::sp<android::GenericPlayer> &gp,
411         SLuint32 playState,
412         AndroidObjectState* pObjState)
413 {
414     XAresult result = XA_RESULT_SUCCESS;
415     AndroidObjectState objState = *pObjState;
416
417     switch (playState) {
418      case SL_PLAYSTATE_STOPPED: {
419          SL_LOGV("setting AVPlayer to SL_PLAYSTATE_STOPPED");
420          gp->stop();
421          }
422          break;
423      case SL_PLAYSTATE_PAUSED: {
424          SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PAUSED");
425          switch(objState) {
426          case ANDROID_UNINITIALIZED:
427              *pObjState = ANDROID_PREPARING;
428              gp->prepare();
429              break;
430          case ANDROID_PREPARING:
431              break;
432          case ANDROID_READY:
433              gp->pause();
434              break;
435          default:
436              SL_LOGE("Android object in invalid state");
437              break;
438          }
439          }
440          break;
441      case SL_PLAYSTATE_PLAYING: {
442          SL_LOGV("setting AVPlayer to SL_PLAYSTATE_PLAYING");
443          switch(objState) {
444          case ANDROID_UNINITIALIZED:
445              *pObjState = ANDROID_PREPARING;
446              gp->prepare();
447              // intended fall through
448          case ANDROID_PREPARING:
449              // intended fall through
450          case ANDROID_READY:
451              gp->play();
452              break;
453          default:
454              SL_LOGE("Android object in invalid state");
455              break;
456          }
457          }
458          break;
459      default:
460          // checked by caller, should not happen
461          break;
462      }
463
464     return result;
465 }
466
467
468 /**
469  * pre-condition: mp != NULL
470  */
471 XAresult android_Player_seek(CMediaPlayer *mp, SLmillisecond posMsec) {
472     XAresult result = XA_RESULT_SUCCESS;
473     switch (mp->mAndroidObjType) {
474       case AUDIOVIDEOPLAYER_FROM_URIFD:
475         if (mp->mAVPlayer !=0) {
476             mp->mAVPlayer->seek(posMsec);
477         }
478         break;
479       case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
480       default: {
481           result = XA_RESULT_PARAMETER_INVALID;
482       }
483     }
484     return result;
485 }
486
487
488 /**
489  * pre-condition: mp != NULL
490  */
491 XAresult android_Player_loop(CMediaPlayer *mp, SLboolean loopEnable) {
492     XAresult result = XA_RESULT_SUCCESS;
493     switch (mp->mAndroidObjType) {
494       case AUDIOVIDEOPLAYER_FROM_URIFD:
495         if (mp->mAVPlayer !=0) {
496             mp->mAVPlayer->loop(loopEnable);
497         }
498         break;
499       case AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
500       default: {
501           result = XA_RESULT_PARAMETER_INVALID;
502       }
503     }
504     return result;
505 }
506
507
508 //-----------------------------------------------------------------------------
509 void android_Player_androidBufferQueue_registerCallback_l(CMediaPlayer *mp) {
510     if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE)
511             && (mp->mAVPlayer != 0)) {
512         SL_LOGD("android_Player_androidBufferQueue_registerCallback_l");
513         android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get());
514         splr->registerQueueCallback(
515                 (const void*)mp, false /*userIsAudioPlayer*/,
516                 mp->mAndroidBufferQueue.mContext, (const void*)&(mp->mAndroidBufferQueue.mItf));
517
518     }
519 }
520
521
522 void android_Player_androidBufferQueue_clear_l(CMediaPlayer *mp) {
523     if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE)
524             && (mp->mAVPlayer != 0)) {
525         android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get());
526         splr->appClear_l();
527     }
528 }
529
530
531 void android_Player_androidBufferQueue_onRefilled_l(CMediaPlayer *mp) {
532     if ((mp->mAndroidObjType == AUDIOVIDEOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE)
533             && (mp->mAVPlayer != 0)) {
534         android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(mp->mAVPlayer.get());
535         splr->queueRefilled_l();
536     }
537 }
538
539
540