TIVI-1947: Enable automaticly restoring media elements by audio manager 06/10706/1
authorRusty Lynch <rusty.lynch@intel.com>
Wed, 9 Oct 2013 23:55:35 +0000 (16:55 -0700)
committerRusty Lynch <rusty.lynch@intel.com>
Wed, 9 Oct 2013 23:55:35 +0000 (16:55 -0700)
Change-Id: Ibb2d1a2fc0dc08099b6055e04867eb7fd5c88ceb
Signed-off-by: Rusty Lynch <rusty.lynch@intel.com>
Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.cpp
Source/WebCore/platform/graphics/gstreamer/MediaPlayerPrivateGStreamer.h

index 40db7d9..a6e9826 100755 (executable)
@@ -250,10 +250,16 @@ static GstBusSyncReply mediaPlayerPrivateSyncHandler(GstBus* bus, GstMessage* me
 #if ENABLE(TIZEN_AUDIO_SESSION_MANAGER)
 static ASM_cb_result_t MediaPlayerAudioSessionEventSourcePause(ASM_event_sources_t eventSource, void* callbackData)
 {
-    MediaPlayer* player = static_cast<MediaPlayer*>(callbackData);
+    MediaPlayerPrivateGStreamer *priv = static_cast<MediaPlayerPrivateGStreamer*>(callbackData);
+    MediaPlayer* player = priv->player();
     if (!player)
         return ASM_CB_RES_IGNORE;
 
+    // This command will come in for any media element, regardless of the state of playback,
+    // so just ignore request on media that is not currently playing
+    if (!priv->playing())
+        return ASM_CB_RES_NONE;
+
     switch (eventSource) {
     case ASM_EVENT_SOURCE_CALL_START:
     case ASM_EVENT_SOURCE_ALARM_START:
@@ -261,6 +267,7 @@ static ASM_cb_result_t MediaPlayerAudioSessionEventSourcePause(ASM_event_sources
     case ASM_EVENT_SOURCE_EMERGENCY_START:
     case ASM_EVENT_SOURCE_OTHER_PLAYER_APP:
     case ASM_EVENT_SOURCE_RESOURCE_CONFLICT:
+        priv->m_preemptionRequested = true;
         player->pause();
         player->playbackStateChanged();
         return ASM_CB_RES_PAUSE;
@@ -277,23 +284,18 @@ static ASM_cb_result_t MediaPlayerAudioSessionEventSourcePause(ASM_event_sources
 
 static ASM_cb_result_t MediaPlayerAudioSessionEventSourcePlay(ASM_event_sources_t eventSource, void* callbackData)
 {
-    MediaPlayer* player = static_cast<MediaPlayer*>(callbackData);
+    MediaPlayerPrivateGStreamer *priv = static_cast<MediaPlayerPrivateGStreamer*>(callbackData);
+    MediaPlayer* player = priv->player();
     if (!player)
         return ASM_CB_RES_IGNORE;
 
-    switch (eventSource) {
-    case ASM_EVENT_SOURCE_ALARM_END:
-        if (!player->hasVideo()) {
-            player->play();
-            return ASM_CB_RES_PLAYING;
-        } else if (player->url().string().contains("camera://")) {
-            player->play();
-            return ASM_CB_RES_PLAYING;
-        }
-        return ASM_CB_RES_NONE;
-    default:
-        return ASM_CB_RES_NONE;
+    // Regardless of event source, start to play if resource manager
+    // commands it, but only if the specific media element is currently preempted
+    if (priv->m_preemptionActive) {
+        player->play();
+        player->playbackStateChanged();
     }
+    return ASM_CB_RES_PLAYING;
 }
 
 static ASM_cb_result_t mediaPlayerPrivateAudioSessionNotifyCallback(int, ASM_event_sources_t eventSource, ASM_sound_commands_t command, unsigned int, void* callbackData)
@@ -391,6 +393,8 @@ MediaPlayerPrivateGStreamer::MediaPlayerPrivateGStreamer(MediaPlayer* player)
 #endif // ENABLE(TIZEN_ACCELERATED_COMPOSITING) && USE(TIZEN_TEXTURE_MAPPER)
 #if ENABLE(TIZEN_AUDIO_SESSION_MANAGER)
     , m_audioSessionManager(AudioSessionManagerGStreamerTizen::createAudioSessionManager())
+    , m_preemptionRequested(false)
+    , m_preemptionActive(false)
 #endif
     , m_originalPreloadWasAutoAndWasOverridden(false)
     , m_preservesPitch(false)
@@ -599,6 +603,7 @@ void MediaPlayerPrivateGStreamer::prepareToPlay()
 void MediaPlayerPrivateGStreamer::play()
 {
 #if ENABLE(TIZEN_AUDIO_SESSION_MANAGER)
+    m_preemptionActive = false;
     if (m_audioSessionManager && !m_audioSessionManager->setSoundState(ASM_STATE_PLAYING))
         return;
 #endif
@@ -619,8 +624,17 @@ void MediaPlayerPrivateGStreamer::play()
 void MediaPlayerPrivateGStreamer::pause()
 {
 #if ENABLE(TIZEN_AUDIO_SESSION_MANAGER)
-    if (m_audioSessionManager && !m_audioSessionManager->setSoundState(ASM_STATE_PAUSE))
-        return;
+    if (m_preemptionRequested) {
+        // resource manager preempted the resource
+        m_preemptionActive = true;
+        m_preemptionRequested = false;
+    }
+    else {
+        // user requested the pause, release the resource
+        m_preemptionActive = false;
+        if (m_audioSessionManager && !m_audioSessionManager->setSoundState(ASM_STATE_PAUSE))
+            return;
+    }
 #endif
 
     if (m_playBinReady == false) {
@@ -751,6 +765,13 @@ bool MediaPlayerPrivateGStreamer::paused() const
     return state == GST_STATE_PAUSED;
 }
 
+bool MediaPlayerPrivateGStreamer::playing() const
+{
+    GstState state;
+    gst_element_get_state(m_playBin.get(), &state, 0, 0);
+    return state == GST_STATE_PLAYING;
+}
+
 bool MediaPlayerPrivateGStreamer::seeking() const
 {
     return m_seeking;
@@ -2319,7 +2340,7 @@ void MediaPlayerPrivateGStreamer::createGSTPlayBin()
     g_object_set(m_playBin.get(), "audio-sink", realSink, NULL);
 #endif
     if (m_audioSessionManager)
-        m_audioSessionManager->registerAudioSessionManager(MM_SESSION_TYPE_SHARE, mediaPlayerPrivateAudioSessionNotifyCallback, player());
+        m_audioSessionManager->registerAudioSessionManager(MM_SESSION_TYPE_SHARE, mediaPlayerPrivateAudioSessionNotifyCallback, this);
 #if ENABLE(TIZEN_AUDIO_SESSION_MANAGER_WITH_MURPHY)
     GstElement *sink = gst_element_factory_make("pulsesink", "audio-sink");
     if (sink != NULL) {
index 61a7897..d5f43e1 100644 (file)
@@ -77,6 +77,7 @@ class MediaPlayerPrivateGStreamer : public MediaPlayerPrivateInterface
             void pause();
 
             bool paused() const;
+            bool playing() const;
             bool seeking() const;
 
             float duration() const;
@@ -163,6 +164,11 @@ class MediaPlayerPrivateGStreamer : public MediaPlayerPrivateInterface
             virtual void suspend();
             virtual void resume();
 #endif
+#if ENABLE(TIZEN_AUDIO_SESSION_MANAGER)
+            // public, because needs to be accessed from C callbacks
+            bool m_preemptionRequested;
+            bool m_preemptionActive;
+#endif
 
         private:
             MediaPlayerPrivateGStreamer(MediaPlayer*);