TSAM-9282 Don't display message " Can't play videos during calls."/"Can't play audio... 54/93954/2
authorDenis Dolzhenko <d.dolzhenko@samsung.com>
Wed, 26 Oct 2016 09:20:44 +0000 (12:20 +0300)
committerAndrey Klimenko <and.klimenko@samsung.com>
Wed, 26 Oct 2016 13:34:29 +0000 (06:34 -0700)
Change-Id: I3e1392458fd3e26e06659d1bab5108022ae1caf0
Signed-off-by: Denis Dolzhenko <d.dolzhenko@samsung.com>
src/Viewer/Controller/inc/SmilPage.h
src/Viewer/Controller/inc/SmilPlayer.h
src/Viewer/Controller/src/SmilPage.cpp
src/Viewer/Controller/src/SmilPlayer.cpp
src/Viewer/Utils/inc/MediaPlayer.h
src/Viewer/Utils/src/MediaPlayer.cpp

index 44facf4..c47b97c 100644 (file)
@@ -42,7 +42,7 @@ namespace Msg
 
             int getDuration() const;
             bool hasMedia() const;
-            bool hasInvalidMedia() const;
+            bool hasInvalidFiles() const;
             bool hasVideo() const;
             bool hasAudio() const;
             bool hasAnimation() const;
@@ -78,7 +78,7 @@ namespace Msg
             bool m_HasAudio;
             SmilImageItemView *m_pImageItem;
             std::list<std::string> m_Attachments;
-            bool m_HasInvalidMedia;
+            bool m_HasInvalidFiles;
     };
 }
 
index f020d26..aa89c0f 100644 (file)
@@ -49,7 +49,6 @@ namespace Msg
 
             void setListener(ISmilPlayerListener *l);
             State getState() const;
-            bool isPlayFinished();
             void start();
             void stop();
             void reset();
@@ -71,7 +70,8 @@ namespace Msg
             virtual void onBeforeDelete(View &view);
 
         private:
-            void playPage();
+            bool playPage();
+            bool canPlay(SmilPage *page);
             void startMedia();
             void stopMedia();
             void prepareMedia();
@@ -84,6 +84,7 @@ namespace Msg
             void showUnableToPlayVideoNotif();
             void showUnableToPlayAudioNotif();
             void showNotSupportedFileNotif();
+            void showUnableToPlayNotif(SmilPage &page);
 
         private:
             ISmilPlayerListener *m_pListener;
@@ -93,7 +94,6 @@ namespace Msg
             unsigned m_CurrentPageIndex;
             int m_Duration;
             State m_State;
-            bool m_Finished;
             int m_PageTickCounter;
     };
 
index c4f51d0..7e90829 100644 (file)
@@ -37,7 +37,7 @@ SmilPage::SmilPage(Evas_Object *parent, const MsgPage &page)
     , m_pVideoSink(nullptr)
     , m_HasAudio(false)
     , m_pImageItem(nullptr)
-    , m_HasInvalidMedia(false)
+    , m_HasInvalidFiles(false)
 {
     build(page);
 }
@@ -48,7 +48,7 @@ SmilPage::SmilPage(Evas_Object *parent, const MsgAttachmentList &list)
     , m_pVideoSink(nullptr)
     , m_HasAudio(false)
     , m_pImageItem(nullptr)
-    , m_HasInvalidMedia(false)
+    , m_HasInvalidFiles(false)
 {
     build(list);
 }
@@ -68,9 +68,9 @@ bool SmilPage::hasMedia() const
     return !m_MediaPath.empty();
 }
 
-bool SmilPage::hasInvalidMedia() const
+bool SmilPage::hasInvalidFiles() const
 {
-    return m_HasInvalidMedia;
+    return m_HasInvalidFiles;
 }
 
 bool SmilPage::hasVideo() const
@@ -193,7 +193,7 @@ void SmilPage::build(const MsgAttachmentList &list)
 void SmilPage::buildImage(const MsgMedia &media)
 {
     m_pImageItem = new SmilImageItemView(getBox(), media.getFilePath());
-    m_HasInvalidMedia = m_pImageItem->getImage() == nullptr;
+    m_HasInvalidFiles = m_pImageItem->getImage() == nullptr;
     m_pImageItem->show();
     appendItem(*m_pImageItem);
 }
@@ -217,7 +217,7 @@ void SmilPage::buildAudio(const MsgMedia& media)
     int duration = MediaUtils::getDurationSec(m_MediaPath);
     if(duration == 0)
     {
-        m_HasInvalidMedia = true;
+        m_HasInvalidFiles = true;
         return;
     }
 
@@ -237,7 +237,7 @@ void SmilPage::buildVideo(const MsgMedia& media)
     int duration = MediaUtils::getDurationSec(m_MediaPath);
     if(duration == 0)
     {
-        m_HasInvalidMedia = true;
+        m_HasInvalidFiles = true;
         return;
     }
     if(m_Duration == 0)
@@ -250,7 +250,7 @@ void SmilPage::buildVideo(const MsgMedia& media)
     if(width * height == 0)
     {
         MSG_LOG_ERROR("Wrong video dimension");
-        m_HasInvalidMedia = true;
+        m_HasInvalidFiles = true;
         return;
     }
     SmilVideoItemView *item = new SmilVideoItemView(getBox(), width, height);
index a9ad502..20a9657 100644 (file)
@@ -31,7 +31,6 @@ SmilPlayer::SmilPlayer(Evas_Object *parent, const MessageMms &mms)
     , m_CurrentPageIndex(0)
     , m_Duration(0)
     , m_State(StopState)
-    , m_Finished(false)
     , m_PageTickCounter(0)
 {
     create(mms);
@@ -110,11 +109,7 @@ Eina_Bool SmilPlayer::onTick()
         m_pTimer = nullptr;
 
         if(!nextPage())
-        {
             stop();
-            m_Finished = true;
-        }
-
         return false;
     }
     return true;
@@ -135,28 +130,27 @@ SmilPlayer::State SmilPlayer::getState() const
     return m_State;
 }
 
-bool SmilPlayer::isPlayFinished()
-{
-    return m_Finished;
-}
-
 void SmilPlayer::start()
 {
     if(m_pTimer)
     {
-        MSG_LOG("Continue");
-
-        setState(PlayState);
-        continueTimer();
-        startMedia();
+        if(canPlay(getCurrentPage()))
+        {
+            MSG_LOG("Continue");
+            setState(PlayState);
+            continueTimer();
+            startMedia();
+        }
     }
     else
     {
         MSG_LOG("Restart");
-
         reset();
-        setState(PlayState);
-        playPage();
+        if(canPlay(getCurrentPage()))
+        {
+            setState(PlayState);
+            playPage();
+        }
     }
 }
 
@@ -170,30 +164,47 @@ void SmilPlayer::stop()
 void SmilPlayer::reset()
 {
     stop();
-    m_Finished = false;
     m_CurrentPageIndex = 0;
     m_PageTickCounter = 0;
 }
 
-void SmilPlayer::playPage()
+bool SmilPlayer::playPage()
 {
-    m_Finished = false;
     SmilPage *page = getCurrentPage();
+    if(!page)
+        return false;
+
     SmilPlayerView::displayPage(*page);
-    if(page->hasInvalidMedia())
+    startTimer(page->getDuration());
+
+    if(page->hasMedia())
+    {
+        prepareMedia();
+        if(!canPlay(page))
+        {
+            stop();
+            showUnableToPlayNotif(*page);
+            return false;
+        }
+        startMedia();
+    }
+
+    if(page->hasInvalidFiles())
         showNotSupportedFileNotif();
 
-    prepareMedia();
-    startMedia();
-    startTimer(page->getDuration());
     if(m_pListener)
         m_pListener->onSmilPlayerPageChanged();
+
+    return true;
 }
 
 void SmilPlayer::prepareMedia()
 {
     if(m_MediaPlayer)
+    {
         m_MediaPlayer->stop();
+        m_MediaPlayer->setPosition(0);
+    }
 
     SmilPage *page = getCurrentPage();
     if(page->hasMedia())
@@ -208,21 +219,39 @@ void SmilPlayer::prepareMedia()
         Evas_Object *videoSink = page->getVideoSink();
         m_MediaPlayer->setDisplay(videoSink);
     }
-
 }
 
 void SmilPlayer::stopMedia()
 {
     if(m_MediaPlayer)
         m_MediaPlayer->pause();
-    getCurrentPage()->playAnimation(false);
+    SmilPage *page = getCurrentPage();
+    if(page)
+        page->playAnimation(false);
+}
+
+bool SmilPlayer::canPlay(SmilPage *page)
+{
+    if(!page)
+        return false;
+
+    if(page->hasMedia() && m_MediaPlayer && !m_MediaPlayer->getFocus())
+    {
+        showUnableToPlayNotif(*page);
+        return false;
+    }
+    return true;
 }
 
 void SmilPlayer::startMedia()
 {
-    if(m_MediaPlayer && getCurrentPage()->hasMedia())
+    SmilPage *page = getCurrentPage();
+    if(!page)
+        return;
+
+    if(page->hasMedia() && m_MediaPlayer)
         m_MediaPlayer->start();
-    getCurrentPage()->playAnimation(true);
+    page->playAnimation(true);
 }
 
 double SmilPlayer::getPosition() const
@@ -257,6 +286,7 @@ bool SmilPlayer::nextPage()
 {
     if(probeNextPage())
     {
+        stopMedia();
         ++m_CurrentPageIndex;
         playPage();
         return true;
@@ -268,6 +298,7 @@ bool SmilPlayer::prevPage()
 {
     if(probePrevPage())
     {
+        stopMedia();
         --m_CurrentPageIndex;
         playPage();
         return true;
@@ -305,6 +336,14 @@ void SmilPlayer::showNotSupportedFileNotif()
     notification_status_message_post(msg("IDS_MSG_TPOP_CANT_PREVIEW_FILE_FILE_FORMAT_NOT_SUPPORTED").cStr());
 }
 
+void SmilPlayer::showUnableToPlayNotif(SmilPage &page)
+{
+    if(page.hasVideo())
+        showUnableToPlayVideoNotif();
+    else if(page.hasAudio())
+        showUnableToPlayAudioNotif();
+}
+
 void SmilPlayer::onBeforeDelete(View &view)
 {
     MSG_LOG("");
@@ -326,18 +365,16 @@ void SmilPlayer::onBeforeDelete(View &view)
 
 void SmilPlayer::onMediaPlayerSoundFocusChanged()
 {
-    if(m_MediaPlayer->isPlaying() && m_MediaPlayer->isFocusChangedCallReason())
+    if(m_MediaPlayer->isPlaying() && !m_MediaPlayer->getFocus())
     {
         SmilPage *page = getCurrentPage();
         if(page)
         {
-            m_MediaPlayer->pause();
-            if(page->hasVideo())
-                showUnableToPlayVideoNotif();
-            else if(page->hasAudio())
-                showUnableToPlayAudioNotif();
             if(page->hasMedia())
+            {
+                showUnableToPlayNotif(*page);
                 stop();
+            }
         }
     }
 }
index 3a856bb..9894e2d 100644 (file)
@@ -19,6 +19,7 @@
 #define MediaPlayer_h_
 
 #include <player.h>
+#include <sound_manager.h>
 #include <string>
 #include <Evas.h>
 
@@ -45,25 +46,22 @@ namespace Msg
             void setPosition(int msec);
             static int getDuration(const std::string &uri);
             bool getFocus() const;
-            bool isFocusChangedCallReason() const;
 
         private:
             static void on_completed_cb(void *user_data);
             static void on_seek_cb(void *user_data);
 
-            // Cll from media internal thread:
-            static void on_sound_stream_focus_state_watch_cb(int id, sound_stream_focus_mask_e focus_mask,
-                    sound_stream_focus_state_e focus_state, sound_stream_focus_change_reason_e reason,
-                    const char *extra_info, void *user_data);
+            // Call from media internal thread:
+            static void on_sound_stream_focus_state_changed_cb(sound_stream_info_h stream_info,
+                                                               sound_stream_focus_change_reason_e reason,
+                                                               const char *extra_info,
+                                                               void *user_data);
             player_state_e getState() const;
 
         private:
             player_h m_Player;
             IMediaPlayerListener *m_pListener;
-            bool m_Focus;
-            bool m_FocusCallReason;
-
-            int m_SoundManagerCallbackId;
+            sound_stream_info_h m_StreamInfo;
     };
 
     class IMediaPlayerListener
index caad317..31fc3a2 100644 (file)
@@ -19,7 +19,6 @@
 #include "Logger.h"
 #include "CallbackAssist.h"
 
-#include <sound_manager.h>
 #include <Ecore.h>
 
 using namespace Msg;
@@ -27,28 +26,36 @@ using namespace Msg;
 MediaPlayer::MediaPlayer()
     : m_Player()
     , m_pListener(nullptr)
-    , m_Focus(false)
-    , m_FocusCallReason(false)
-    , m_SoundManagerCallbackId(-1)
+    , m_StreamInfo()
 {
-    sound_manager_add_focus_state_watch_cb(SOUND_STREAM_FOCUS_FOR_PLAYBACK,
-            on_sound_stream_focus_state_watch_cb, this, &m_SoundManagerCallbackId);
-
-    MSG_LOG("callback-id = ", m_SoundManagerCallbackId);
-    player_create(&m_Player);
-    player_set_sound_type(m_Player, SOUND_TYPE_MEDIA);
-    player_set_volume(m_Player, 1.0, 1.0);
-    player_set_looping(m_Player, false);
-    player_set_completed_cb(m_Player, on_completed_cb, this);
+    sound_manager_create_stream_information(SOUND_STREAM_TYPE_MEDIA, on_sound_stream_focus_state_changed_cb, this, &m_StreamInfo);
+    if(m_StreamInfo)
+    {
+        player_create(&m_Player);
+        if(m_Player)
+        {
+            player_set_sound_type(m_Player, SOUND_TYPE_MEDIA);
+            player_set_volume(m_Player, 1.0, 1.0);
+            player_set_looping(m_Player, false);
+            player_set_audio_policy_info(m_Player, m_StreamInfo);
+            player_set_completed_cb(m_Player, on_completed_cb, this);
+        }
+    }
 }
 
 MediaPlayer::~MediaPlayer()
 {
-    sound_manager_remove_focus_state_watch_cb(m_SoundManagerCallbackId);
     m_pListener = nullptr;
     stop();
-    player_unprepare(m_Player);
-    player_destroy(m_Player);
+
+    if(m_StreamInfo)
+        sound_manager_destroy_stream_information(m_StreamInfo);
+
+    if(m_Player)
+    {
+        player_unprepare(m_Player);
+        player_destroy(m_Player);
+    }
 }
 
 void MediaPlayer::setDisplay(Evas_Object *obj)
@@ -67,16 +74,24 @@ player_state_e MediaPlayer::getState() const
 
 bool MediaPlayer::getFocus() const
 {
-    return m_Focus;
-}
+    sound_stream_focus_change_reason_e acquiredBy = SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA;
+    int flags = 0;
+    char *extraInfo = nullptr;
 
-bool MediaPlayer::isFocusChangedCallReason() const
-{
-    return m_FocusCallReason;
+    if(sound_manager_get_current_playback_focus(&acquiredBy, &flags, &extraInfo) == SOUND_MANAGER_ERROR_NONE)
+    {
+        free(extraInfo);
+        return acquiredBy != SOUND_STREAM_FOCUS_CHANGED_BY_RINGTONE &&
+               acquiredBy != SOUND_STREAM_FOCUS_CHANGED_BY_VOIP &&
+               acquiredBy != SOUND_STREAM_FOCUS_CHANGED_BY_CALL;
+    }
+
+    return true;
 }
 
 void MediaPlayer::start()
 {
+    sound_manager_acquire_focus(m_StreamInfo, SOUND_STREAM_FOCUS_FOR_PLAYBACK, nullptr);
     if(getState() == PLAYER_STATE_IDLE)
         player_prepare(m_Player);
 
@@ -86,6 +101,7 @@ void MediaPlayer::start()
 
 void MediaPlayer::stop()
 {
+    sound_manager_release_focus(m_StreamInfo, SOUND_STREAM_FOCUS_FOR_PLAYBACK, nullptr);
     player_state_e state = getState();
     if(state == PLAYER_STATE_PLAYING || state == PLAYER_STATE_PAUSED)
         player_stop(m_Player);
@@ -123,7 +139,9 @@ int MediaPlayer::getDuration() const
 
 void MediaPlayer::setPosition(int msec)
 {
-    player_set_play_position(m_Player, msec, true, on_seek_cb, this);
+    player_state_e state = getState();
+    if(state == PLAYER_STATE_PLAYING || state == PLAYER_STATE_PAUSED || state == PLAYER_STATE_READY)
+        player_set_play_position(m_Player, msec, true, on_seek_cb, this);
 }
 
 int MediaPlayer::getDuration(const std::string &uri)
@@ -156,33 +174,20 @@ void MediaPlayer::on_seek_cb(void *user_data)
     MSG_LOG("");
 }
 
-void MediaPlayer::on_sound_stream_focus_state_watch_cb(int id,
-        sound_stream_focus_mask_e focus_mask,
-        sound_stream_focus_state_e focus_state,
-        sound_stream_focus_change_reason_e reason,
-        const char *extra_info,
-        void *user_data)
+void MediaPlayer::on_sound_stream_focus_state_changed_cb(sound_stream_info_h stream_info,
+                                                         sound_stream_focus_change_reason_e reason,
+                                                         const char *extra_info,
+                                                         void *user_data)
 {
-    MSG_LOG("callback-id = ", id);
-    MSG_LOG("Interrupted focus state = ", focus_state);
     MSG_LOG("Interrupted focus change reason = ", reason);
-
     auto *self = static_cast<MediaPlayer*>(user_data);
-    if(id != self->m_SoundManagerCallbackId)
-        return;
-    self->m_Focus = focus_state == SOUND_STREAM_FOCUS_STATE_RELEASED;
-    self->m_FocusCallReason = reason == SOUND_STREAM_FOCUS_CHANGED_BY_RINGTONE ||
-                              reason == SOUND_STREAM_FOCUS_CHANGED_BY_VOIP ||
-                              reason == SOUND_STREAM_FOCUS_CHANGED_BY_CALL;
-
-    ecore_main_loop_thread_safe_call_sync
+    ecore_main_loop_thread_safe_call_async
     (
-        [](void *data)->void*
+        [](void *data)
         {
             auto *self = (MediaPlayer*)data;
             if(self->m_pListener)
                 self->m_pListener->onMediaPlayerSoundFocusChanged();
-            return nullptr;
         },
         self
     );