Sound (SoundFeedback) class refactor 83/161683/4
authorPawel Kurowski <p.kurowski2@samsung.com>
Fri, 24 Nov 2017 19:44:20 +0000 (20:44 +0100)
committerPawel Kurowski <p.kurowski2@samsung.com>
Thu, 30 Nov 2017 12:37:03 +0000 (13:37 +0100)
sound object will be released in passive mode.

Change-Id: I101cba28f0df5accd25cab669984f45fbe311bc9

src/RowScanner.cpp
src/ScreenScannerManager.cpp
src/Sound.cpp [deleted file]
src/SoundFeedback.cpp [new file with mode: 0644]
src/SoundFeedback.hpp [moved from src/Sound.hpp with 54% similarity]
src/UniversalSwitch.cpp
src/UniversalSwitch.hpp

index b76ae66..cf5baaf 100644 (file)
@@ -19,7 +19,7 @@
 #include "NavigationInterface.hpp"
 #include "RowScanner.hpp"
 #include "ScanningProperties.hpp"
-#include "Sound.hpp"
+#include "SoundFeedback.hpp"
 #include "TextToSpeech.hpp"
 #include "UniversalSwitch.hpp"
 #include "UniversalSwitchLog.hpp"
@@ -42,7 +42,7 @@ namespace
                        auto uiElem = std::make_shared<UIElement>(std::move(elem));
                        Singleton<UniversalSwitch>::instance().getTextToSpeech()->speak(std::move(uiElem));
                }
-               Sound::playSoundFeedback(Sound::ID::NAVIGATION_ITERATED);
+               Singleton<UniversalSwitch>::instance().getSoundFeedback()->play(SoundFeedback::Sound::NAVIGATION_ITERATED);
        }
 
        static const std::string BACK_BUTTON_CODE = "XF86Back";
index f80deed..4e3f244 100644 (file)
@@ -17,7 +17,7 @@
 #include "ScreenScannerManager.hpp"
 #include "UniversalSwitchLog.hpp"
 #include "Window.hpp"
-#include "Sound.hpp"
+#include "SoundFeedback.hpp"
 #include "TextToSpeech.hpp"
 #include "UniversalSwitch.hpp"
 
@@ -74,7 +74,7 @@ void ScreenScannerManager::onContextChanged(const std::shared_ptr<UIElement> &ob
 {
        DEBUG("Context changed callback invoked");
        stopAutoscanning();
-       Sound::playSoundFeedback(Sound::ID::NAVIGATION_CONTEXT_CHANGED);
+       Singleton<UniversalSwitch>::instance().getSoundFeedback()->play(SoundFeedback::Sound::NAVIGATION_CONTEXT_CHANGED);
        mainWindow->bringToFront();
 
        if (!obj) {
diff --git a/src/Sound.cpp b/src/Sound.cpp
deleted file mode 100644 (file)
index a71f3f6..0000000
+++ /dev/null
@@ -1,132 +0,0 @@
-/*
- * Copyright 2017  Samsung Electronics Co., Ltd
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
-
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "Sound.hpp"
-#include "UniversalSwitchLog.hpp"
-#include "VConf.hpp"
-#include "VConfKeys.hpp"
-
-#include <player.h>
-
-#include <memory>
-#include <unordered_map>
-#include <vector>
-
-static constexpr double DEFAULT_VOLUME_COEFFICIENT = 1.0;
-
-class SoundProperties
-{
-public:
-       SoundProperties()
-       {
-               callbackHandles.push_back(Singleton<VConfInterface>::instance().registerAndGet<bool>(VCONF_KEY_FEEDBACK_SOUND_ENABLED, false,
-               [this](auto state) {
-                       this->setCachedFeedbackEnabled(state);
-               }));
-
-               callbackHandles.push_back(Singleton<VConfInterface>::instance().registerAndGet<double>(VCONF_KEY_FEEDBACK_SOUND_VOLUME, DEFAULT_VOLUME_COEFFICIENT,
-               [this](auto volumeCoefficient) {
-                       this->setVolumeCoefficient(volumeCoefficient);
-               }));
-       }
-
-       bool isFeedbackEnabled() const
-       {
-               return feedbackEnabled;
-       }
-
-       double getVolumeCoefficient() const
-       {
-               return volumeCoefficient;
-       }
-
-private:
-       void setCachedFeedbackEnabled(bool state)
-       {
-               feedbackEnabled = state;
-               DEBUG("isFeedbackEnabled: %s", (feedbackEnabled ? "enabled" : "disabled"));
-       }
-
-       void setVolumeCoefficient(double value)
-       {
-               volumeCoefficient = value;
-               DEBUG("volumeCoefficient: %lf", volumeCoefficient);
-       }
-
-       std::vector<VConfInterface::CallbackHandle> callbackHandles;
-
-       bool feedbackEnabled = false;
-       double volumeCoefficient = DEFAULT_VOLUME_COEFFICIENT;
-};
-
-namespace
-{
-       static const std::unordered_map<Sound::ID, const char *> soundPaths = {
-               {Sound::ID::NAVIGATION_ITERATED, SOUNDS_DIR "navigation_iterated.ogg"},
-               {Sound::ID::NAVIGATION_CONTEXT_CHANGED, SOUNDS_DIR "navigation_context_changed.ogg"},
-       };
-
-       void shutdownAndDestroyPlayer(player_h *player)
-       {
-               if (player_stop(*player) != PLAYER_ERROR_NONE)
-                       DEBUG("Error when stopping player");
-
-               if (player_unprepare(*player) != PLAYER_ERROR_NONE)
-                       DEBUG("Error when shutting down player");
-
-               if (player_destroy(*player) != PLAYER_ERROR_NONE)
-                       ERROR("Error when destroying player");
-
-               delete player;
-       }
-
-       using PlayerUniquePtr = std::unique_ptr<player_h, void (*)(player_h *player)>;
-       PlayerUniquePtr createPlayer()
-       {
-               PlayerUniquePtr player {new player_h, shutdownAndDestroyPlayer};
-
-               if (player_create(player.get()) != PLAYER_ERROR_NONE)
-                       ERROR("Error on player_create");
-               if (player_set_sound_type(*player.get(), SOUND_TYPE_MEDIA) != PLAYER_ERROR_NONE)
-                       ERROR("Error on player_set_sound_type");
-
-               return player;
-       }
-}
-
-void Sound::playSoundFeedback(ID id)
-{
-       if (!Singleton<SoundProperties>::instance().isFeedbackEnabled())
-               return;
-
-       static auto playerPtr = createPlayer();
-       auto &player = *playerPtr.get();
-
-       player_state_e state;
-       player_get_state(player, &state);
-       if (state == PLAYER_STATE_PLAYING || state == PLAYER_STATE_PAUSED)
-               player_unprepare(player);
-
-       auto volumeCoefficient = Singleton<SoundProperties>::instance().getVolumeCoefficient();
-       if (player_set_volume(player, volumeCoefficient, volumeCoefficient) != PLAYER_ERROR_NONE)
-               ERROR("Error when setting volumeCoefficient");
-       if (player_set_uri(player, soundPaths.at(id)) != PLAYER_ERROR_NONE)
-               ERROR("Error when setting sound path");
-       if (player_prepare(player) != PLAYER_ERROR_NONE)
-               ERROR("Error when preparing player!");
-       if (player_start(player) != PLAYER_ERROR_NONE)
-               ERROR("Player do not started");
-}
diff --git a/src/SoundFeedback.cpp b/src/SoundFeedback.cpp
new file mode 100644 (file)
index 0000000..6c7ad88
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2017  Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "SoundFeedback.hpp"
+#include "UniversalSwitchLog.hpp"
+#include "VConfKeys.hpp"
+#include "utils.hpp"
+
+static constexpr double DEFAULT_VOLUME_COEFFICIENT = 1.0;
+
+SoundFeedback::SoundFeedback()
+       : player(), feedbackEnabled(false), volumeCoefficient(DEFAULT_VOLUME_COEFFICIENT)
+{
+       DEBUG("SoundFeedback class construction");
+
+       feedbackEnabledCallbackHandle = Singleton<VConfInterface>::instance().registerAndGet<bool>(VCONF_KEY_FEEDBACK_SOUND_ENABLED, false,
+       [this](auto state) {
+               feedbackEnabled = state;
+       });
+
+       volumeCoefficientCallbackHandle = Singleton<VConfInterface>::instance().registerAndGet<double>(VCONF_KEY_FEEDBACK_SOUND_VOLUME, DEFAULT_VOLUME_COEFFICIENT,
+       [this](auto value) {
+               volumeCoefficient = value;
+       });
+
+       auto error = player_create(&player);
+       if (error != PLAYER_ERROR_NONE) {
+               player = nullptr;
+               ERROR("player_create fail");
+               return;
+       }
+       error = player_set_sound_type(player, SOUND_TYPE_MEDIA);
+       if (error == PLAYER_ERROR_NONE)
+               return;
+
+       player_destroy(player);
+       player = nullptr;
+       ERROR("player_set_sound_stream_info fail");
+}
+
+SoundFeedback::~SoundFeedback()
+{
+       DEBUG("SoundFeedback class destruction");
+       int error;
+       if (feedbackEnabled) {
+               error = player_stop(player);
+               PRINT_ERROR_IF(error != PLAYER_ERROR_NONE);
+
+               error = player_unprepare(player);
+               PRINT_ERROR_IF(error != PLAYER_ERROR_NONE);
+       }
+
+       error = player_destroy(player);
+       PRINT_ERROR_IF(error != PLAYER_ERROR_NONE);
+}
+
+void SoundFeedback::play(Sound sound)
+{
+       RETURN_IF(!player);
+       if (!feedbackEnabled)
+               return;
+
+       player_state_e state;
+       auto error = player_get_state(player, &state);
+       RETURN_IF(error != PLAYER_ERROR_NONE);
+       if (state == PLAYER_STATE_PLAYING || state == PLAYER_STATE_PAUSED) {
+               error = player_unprepare(player);
+               RETURN_IF(error != PLAYER_ERROR_NONE);
+       }
+
+       error = player_set_volume(player, volumeCoefficient, volumeCoefficient);
+       RETURN_IF(error != PLAYER_ERROR_NONE);
+
+       error = player_set_uri(player, getSoundPath(sound).c_str());
+       RETURN_IF(error != PLAYER_ERROR_NONE);
+
+       error = player_prepare(player);
+       RETURN_IF(error != PLAYER_ERROR_NONE);
+
+       error = player_start(player);
+       PRINT_ERROR_IF(error != PLAYER_ERROR_NONE);
+}
+
+std::string SoundFeedback::getSoundPath(Sound sound) const
+{
+       switch (sound) {
+       case Sound::NAVIGATION_ITERATED:
+               return SOUNDS_DIR "navigation_iterated.ogg";
+       case Sound::NAVIGATION_CONTEXT_CHANGED:
+               return SOUNDS_DIR "navigation_context_changed.ogg";
+       }
+
+       ASSERT(0, "Should not be reached");
+       return {};
+}
similarity index 54%
rename from src/Sound.hpp
rename to src/SoundFeedback.hpp
index 1fcd5b5..a7f4c8b 100644 (file)
  * limitations under the License.
  */
 
-#ifndef SOUND_HPP
-#define SOUND_HPP
+#ifndef SOUND_FEEDBACK_HPP
+#define SOUND_FEEDBACK_HPP
 
-class Sound
+#include "VConf.hpp"
+#include <player.h>
+#include <vector>
+
+
+class SoundFeedback
 {
 public:
-       enum class ID {
-               NAVIGATION_ITERATED, NAVIGATION_CONTEXT_CHANGED
+       enum class Sound {
+               NAVIGATION_ITERATED,
+               NAVIGATION_CONTEXT_CHANGED
        };
 
-       static void playSoundFeedback(ID id);
+       SoundFeedback();
+       ~SoundFeedback();
+
+       void play(Sound sound);
+
+private:
+       std::string getSoundPath(Sound sound) const;
+
+       player_h player;
+       VConfInterface::CallbackHandle feedbackEnabledCallbackHandle;
+       VConfInterface::CallbackHandle volumeCoefficientCallbackHandle;
+       bool feedbackEnabled;
+       double volumeCoefficient;
 };
 
 #endif
index 22c6a68..8040b97 100644 (file)
@@ -22,6 +22,7 @@
 #include "DBusInterface.hpp"
 #include "ScreenScannerManager.hpp"
 #include "ScreenSwitchProvider.hpp"
+#include "SoundFeedback.hpp"
 #include "SQLiteConfiguration.hpp"
 #include "SwitchConfigurationItem.hpp"
 #include "SwitchManager.hpp"
@@ -75,6 +76,7 @@ void UniversalSwitch::switchToActiveMode()
        switchManager = SwitchManager::create<SwitchManager>(compositeSwitchProvider, configuration, activityFactory);
 
        textToSpeech = std::make_shared<TextToSpeech>();
+       soundFeedback = std::make_shared<SoundFeedback>();
 
        screenScannerManager = std::make_shared<ScreenScannerManager>();
        screenScannerManager->startAutoscanning();
@@ -84,11 +86,12 @@ void UniversalSwitch::switchToPasiveMode()
 {
        DEBUG("Universal-switch passive mode");
        screenScannerManager.reset();
+       soundFeedback.reset();
+       textToSpeech.reset();
        if (switchManager) {
                switchManager->terminate();
                switchManager.reset();
        }
-       textToSpeech.reset();
        navigationInterface.reset();
        atspi.reset();
 }
@@ -156,6 +159,11 @@ std::shared_ptr<TextToSpeech> UniversalSwitch::getTextToSpeech() const
        return textToSpeech;
 }
 
+std::shared_ptr<SoundFeedback> UniversalSwitch::getSoundFeedback() const
+{
+       return soundFeedback;
+}
+
 void UniversalSwitch::setSwitchManager(const std::shared_ptr<SwitchManager> &sm)
 {
        switchManager = sm;
index 838559b..5d10d2a 100644 (file)
@@ -35,6 +35,7 @@ class TextToSpeech;
 class Window;
 class NavigationInterface;
 class ScrollActivitiesData;
+class SoundFeedback;
 
 
 class UniversalSwitch
@@ -49,6 +50,7 @@ public:
        std::shared_ptr<Atspi> getAtspi() const;
        std::shared_ptr<Window> getMainWindow();
        std::shared_ptr<TextToSpeech> getTextToSpeech() const;
+       std::shared_ptr<SoundFeedback> getSoundFeedback() const;
        std::shared_ptr<NavigationInterface> getNavigationInterface() const;
        std::shared_ptr<ScrollActivitiesData> getScrollActivitiesData() const;
        void setScrollActivitiesData(std::shared_ptr<ScrollActivitiesData>);
@@ -74,6 +76,7 @@ private:
        std::shared_ptr<Atspi> atspi;
        std::shared_ptr<NavigationInterface> navigationInterface;
        std::shared_ptr<TextToSpeech> textToSpeech;
+       std::shared_ptr<SoundFeedback> soundFeedback;
 
        std::shared_ptr<ScreenScannerManager> screenScannerManager;
        std::weak_ptr<Window> mainWindow;