OSDN Git Service

Hide media player looper from AudioPlayer.
authorJean-Michel Trivi <jmtrivi@google.com>
Sun, 12 Sep 2010 18:06:51 +0000 (11:06 -0700)
committerJean-Michel Trivi <jmtrivi@google.com>
Sun, 12 Sep 2010 18:11:47 +0000 (11:11 -0700)
The ALooper that takes care of the rendering loop in SfPlayer is
 only used inside SfPlayer. This CL makes SfPlayer manage the
 lifecycle of its rendering looper.

Change-Id: Ia4d5e02ce0a44bcf79021f428b490612a9fead9c

opensles/libopensles/android_AudioPlayer.cpp
opensles/libopensles/android_SfPlayer.cpp
opensles/libopensles/android_SfPlayer.h
opensles/libopensles/sles_allinclusive.h

index 95526f3..1ddcaee 100644 (file)
@@ -209,23 +209,17 @@ void audioPlayer_dispatch_headAtEnd_lockPlay(CAudioPlayer *ap, bool setPlayState
     void * playContext = NULL;
     // SLPlayItf callback or no callback?
     if (needToLock) {
-        interface_lock_peek(&ap->mPlay);
+        interface_lock_exclusive(&ap->mPlay);
     }
     if (ap->mPlay.mEventFlags & SL_PLAYEVENT_HEADATEND) {
         playCallback = ap->mPlay.mCallback;
         playContext = ap->mPlay.mContext;
     }
-    if (needToLock) {
-        interface_unlock_peek(&ap->mPlay);
-    }
     if (setPlayStateToPaused) {
-        if (needToLock) {
-            interface_lock_poke(&ap->mPlay)
-            ap->mPlay.mState = SL_PLAYSTATE_PAUSED;
-            interface_unlock_poke(&ap->mPlay);
-        } else {
-            ap->mPlay.mState = SL_PLAYSTATE_PAUSED;
-        }
+        ap->mPlay.mState = SL_PLAYSTATE_PAUSED;
+    }
+    if (needToLock) {
+        interface_unlock_exclusive(&ap->mPlay);
     }
     // callback with no lock held
     if (NULL != playCallback) {
@@ -686,7 +680,6 @@ SLresult android_audioPlayer_create(
     pAudioPlayer->mAudioTrack = NULL;
 #ifndef USE_BACKPORT
     pAudioPlayer->mSfPlayer.clear();
-    pAudioPlayer->mRenderLooper.clear();
 #endif
 
     pAudioPlayer->mAmplFromVolLevel = 1.0f;
@@ -749,13 +742,10 @@ SLresult android_audioPlayer_realize(CAudioPlayer *pAudioPlayer, SLboolean async
         object_lock_exclusive(&pAudioPlayer->mObject);
         pAudioPlayer->mAndroidObjState = ANDROID_PREPARING;
 
-        pAudioPlayer->mRenderLooper = new android::ALooper();
-        pAudioPlayer->mSfPlayer = new android::SfPlayer(pAudioPlayer->mRenderLooper);
+        pAudioPlayer->mSfPlayer = new android::SfPlayer();
         pAudioPlayer->mSfPlayer->setNotifListener(sfplayer_handlePrefetchEvent,
-                (void*)pAudioPlayer /*notifUSer*/);
-        pAudioPlayer->mRenderLooper->registerHandler(pAudioPlayer->mSfPlayer);
-        pAudioPlayer->mRenderLooper->start(false /*runOnCallingThread*/, false /*canCallJava*/,
-                ANDROID_PRIORITY_AUDIO);
+                        (void*)pAudioPlayer /*notifUSer*/);
+        pAudioPlayer->mSfPlayer->armLooper();
         object_unlock_exclusive(&pAudioPlayer->mObject);
 
         int res;
@@ -825,8 +815,8 @@ SLresult android_audioPlayer_realize(CAudioPlayer *pAudioPlayer, SLboolean async
     if (ANDROID_UNINITIALIZED == pAudioPlayer->mAndroidObjState) {
         return result;
     }
-    // proceed with effect initialization
 
+    // proceed with effect initialization
     int sessionId = pAudioPlayer->mAudioTrack->getSessionId();
     // initialize EQ
     // FIXME use a table of effect descriptors when adding support for more effects
@@ -918,12 +908,8 @@ SLresult android_audioPlayer_destroy(CAudioPlayer *pAudioPlayer) {
     //-----------------------------------
     // MediaPlayer
     case MEDIAPLAYER:
-        // FIXME group in one function?
-        if (pAudioPlayer->mSfPlayer != NULL) {
-            pAudioPlayer->mRenderLooper->stop();
-            pAudioPlayer->mRenderLooper->unregisterHandler(pAudioPlayer->mSfPlayer->id());
+        if (pAudioPlayer->mSfPlayer != 0) {
             pAudioPlayer->mSfPlayer.clear();
-            pAudioPlayer->mRenderLooper.clear();
         }
         break;
 #endif
index 76042b1..93b5a22 100644 (file)
@@ -32,9 +32,8 @@
 
 namespace android {
 
-SfPlayer::SfPlayer(const sp<ALooper> &renderLooper)
+SfPlayer::SfPlayer()
     : mAudioTrack(NULL),
-      mRenderLooper(renderLooper),
       mFlags(0),
       mBitrate(-1),
       mNumChannels(1),
@@ -48,12 +47,18 @@ SfPlayer::SfPlayer(const sp<ALooper> &renderLooper)
       mNotifyClient(NULL),
       mNotifyUser(NULL),
       mDecodeBuffer(NULL) {
+
+      mRenderLooper = new android::ALooper();
 }
 
 
 SfPlayer::~SfPlayer() {
     LOGV("SfPlayer::~SfPlayer()");
 
+    mRenderLooper->stop();
+    mRenderLooper->unregisterHandler(this->id());
+    mRenderLooper.clear();
+
     if (mAudioSource != NULL) {
         {
             // don't even think about stopping the media source without releasing the decode buffer
@@ -69,6 +74,11 @@ SfPlayer::~SfPlayer() {
     resetDataLocator();
 }
 
+void SfPlayer::armLooper() {
+    mRenderLooper->registerHandler(this);
+    mRenderLooper->start(false /*runOnCallingThread*/, false /*canCallJava*/,
+            ANDROID_PRIORITY_AUDIO);
+}
 
 void SfPlayer::useAudioTrack(AudioTrack* pTrack) {
     mAudioTrack = pTrack;
index dd958b7..3fdf68d 100644 (file)
@@ -44,7 +44,7 @@ namespace android {
     typedef void (*notif_client_t)(int event, const int data1, void* notifUser);
 
 struct SfPlayer : public AHandler {
-    SfPlayer(const sp<ALooper> &renderLooper);
+    SfPlayer();
 
     enum CacheStatus {
         kStatusEmpty = 0,
@@ -60,6 +60,7 @@ struct SfPlayer : public AHandler {
         kEventEndOfStream             = 'eos',
     };
 
+    void armLooper();
     void useAudioTrack(AudioTrack* pTrack);
     void setNotifListener(const notif_client_t cbf, void* notifUser);
 
@@ -129,7 +130,7 @@ private:
 
     AudioTrack *mAudioTrack;
 
-    wp<ALooper> mRenderLooper;
+    sp<ALooper> mRenderLooper;
     sp<DataSource> mDataSource;
     sp<MediaSource> mAudioSource;
     uint32_t mFlags;
index a1f6026..f10ca73 100644 (file)
@@ -964,7 +964,6 @@ enum AndroidObject_state {
     android::AudioTrack *mAudioTrack;
 #ifndef USE_BACKPORT
     android::sp<android::SfPlayer> mSfPlayer;
-    android::sp<android::ALooper>  mRenderLooper;
 #endif
     /**
      * Amplification (can be attenuation) factor derived for the VolumeLevel