TizenRefApp-8517 [Gallery] Implement SoundManager 67/129967/1
authorIgor Nazarov <i.nazarov@samsung.com>
Thu, 18 May 2017 14:59:19 +0000 (17:59 +0300)
committerIgor Nazarov <i.nazarov@samsung.com>
Thu, 18 May 2017 14:59:19 +0000 (17:59 +0300)
- Implemented SoundManager class.

Change-Id: Ib0b842ebff361e59afaa9e6227e119d7b91a1fff

inc/model/SoundManager.h [new file with mode: 0644]
src/model/SoundManager.cpp [new file with mode: 0644]

diff --git a/inc/model/SoundManager.h b/inc/model/SoundManager.h
new file mode 100644 (file)
index 0000000..df5e36a
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2017 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * 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.
+ */
+
+#ifndef __GALLERY_MODEL_SOUND_MANAGER_H__
+#define __GALLERY_MODEL_SOUND_MANAGER_H__
+
+#include <sound_manager.h>
+
+#include "ucl/misc/Event.h"
+
+#include "types.h"
+
+namespace gallery {
+
+       UCL_DECLARE_REF_ALIASES(SoundManager);
+
+       class SoundManager final : public ucl::NonCopyable {
+       public:
+               static SoundManagerSRef newInstance();
+
+               bool isMediaDeviceReady() const;
+
+               int getCurrentMediaVolume() const;
+               int getMaxMediaVolume() const;
+
+               void addMediaDeviceStateChangeHandler(const NotiHandler &handler);
+               void delMediaDeviceStateChangeHandler(const NotiHandler &handler);
+
+               void addMediaVolumeChangeHandler(const NotiHandler &handler);
+               void delMediaVolumeChangeHandler(const NotiHandler &handler);
+
+       private:
+               friend class ucl::RefCountObj<SoundManager>;
+               SoundManager();
+               ~SoundManager();
+
+               ucl::Result prepare();
+
+               void setMediaDeviceState(bool isReady);
+               void setCurrentMediaVolume(int value);
+
+               ucl::Result updateMediaDeviceState();
+
+               void onDeviceConnectionChanged(sound_device_h device, bool isConnected);
+               void onDeviceStateChanged(sound_device_h device,
+                               sound_device_state_e state);
+               void onVolumeChanged(sound_type_e type, unsigned int volume);
+
+       private:
+               ucl::Event<NotiHandler> m_onMediaDeviceStateChange;
+               ucl::Event<NotiHandler> m_onMediaVolumeChange;
+               int m_devConnChangedCbId;
+               int m_devStateChangedCbId;
+               int m_volumeChangedCbId;
+               int m_currentMediaVolume;
+               int m_maxMediaVolume;
+               bool m_isMediaDeviceReady;
+       };
+}
+
+#endif // __GALLERY_MODEL_SOUND_MANAGER_H__
diff --git a/src/model/SoundManager.cpp b/src/model/SoundManager.cpp
new file mode 100644 (file)
index 0000000..b540d79
--- /dev/null
@@ -0,0 +1,186 @@
+/*
+ * Copyright 2017 Samsung Electronics Co., Ltd
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * 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 "model/SoundManager.h"
+
+#include "common.h"
+
+namespace gallery {
+
+       using namespace ucl;
+
+       SoundManagerSRef SoundManager::newInstance()
+       {
+               auto result = makeShared<SoundManager>();
+
+               FAIL_RETURN_VALUE(result->prepare(), {},
+                               "result->prepare() failed!");
+
+               return result;
+       }
+
+       SoundManager::SoundManager() :
+                       m_devConnChangedCbId(-1),
+                       m_devStateChangedCbId(-1),
+                       m_volumeChangedCbId(-1),
+                       m_currentMediaVolume(0),
+                       m_maxMediaVolume(0),
+                       m_isMediaDeviceReady(false)
+       {
+       }
+
+       SoundManager::~SoundManager()
+       {
+               if (m_devConnChangedCbId >= 0) {
+                       sound_manager_remove_device_connection_changed_cb(
+                                       m_devConnChangedCbId);
+               }
+               if (m_devStateChangedCbId >= 0) {
+                       sound_manager_remove_device_state_changed_cb(m_devStateChangedCbId);
+               }
+               if (m_volumeChangedCbId >= 0) {
+                       sound_manager_remove_volume_changed_cb(m_volumeChangedCbId);
+               }
+       }
+
+       Result SoundManager::prepare()
+       {
+               const int devMask = SOUND_DEVICE_ALL_MASK;
+
+               FAIL_RETURN(util::get(sound_manager_add_device_connection_changed_cb,
+                               m_devConnChangedCbId, devMask,
+                               CALLBACK_B(SoundManager::onDeviceConnectionChanged), this),
+                               "sound_manager_add_device_connection_changed_cb() failed!");
+
+               FAIL_RETURN(util::get(sound_manager_add_device_state_changed_cb,
+                               m_devStateChangedCbId, devMask,
+                               CALLBACK_B(SoundManager::onDeviceStateChanged), this),
+                               "sound_manager_add_device_state_changed_cb() failed!");
+
+               FAIL_RETURN(util::get(sound_manager_add_volume_changed_cb,
+                               m_volumeChangedCbId,
+                               CALLBACK_B(SoundManager::onVolumeChanged), this),
+                               "sound_manager_add_volume_changed_cb() failed!");
+
+               FAIL_RETURN(util::get(sound_manager_get_volume,
+                               m_currentMediaVolume, SOUND_TYPE_MEDIA),
+                               "sound_manager_get_max_volume() failed!");
+
+               FAIL_RETURN(util::get(sound_manager_get_max_volume,
+                               m_maxMediaVolume, SOUND_TYPE_MEDIA),
+                               "sound_manager_get_max_volume() failed!");
+
+               FAIL_RETURN(updateMediaDeviceState(),
+                               "updateMediaDeviceState() failed!");
+
+               return RES_OK;
+       }
+
+       bool SoundManager::isMediaDeviceReady() const
+       {
+               return m_isMediaDeviceReady;
+       }
+
+       int SoundManager::getCurrentMediaVolume() const
+       {
+               return m_currentMediaVolume;
+       }
+
+       int SoundManager::getMaxMediaVolume() const
+       {
+               return m_maxMediaVolume;
+       }
+
+       void SoundManager::addMediaDeviceStateChangeHandler(
+                       const NotiHandler &handler)
+       {
+               m_onMediaDeviceStateChange += handler;
+       }
+
+       void SoundManager::delMediaDeviceStateChangeHandler(
+                       const NotiHandler &handler)
+       {
+               m_onMediaDeviceStateChange -= handler;
+       }
+
+       void SoundManager::addMediaVolumeChangeHandler(const NotiHandler &handler)
+       {
+               m_onMediaVolumeChange += handler;
+       }
+
+       void SoundManager::delMediaVolumeChangeHandler(const NotiHandler &handler)
+       {
+               m_onMediaVolumeChange -= handler;
+       }
+
+       void SoundManager::setMediaDeviceState(const bool isReady)
+       {
+               if (isReady != m_isMediaDeviceReady) {
+                       m_isMediaDeviceReady = isReady;
+                       m_onMediaDeviceStateChange.dispatch();
+               }
+       }
+
+       void SoundManager::setCurrentMediaVolume(const int value)
+       {
+               if (value != m_currentMediaVolume) {
+                       m_currentMediaVolume = value;
+                       m_onMediaVolumeChange.dispatch();
+               }
+       }
+
+       Result SoundManager::updateMediaDeviceState()
+       {
+               sound_device_type_e devType = SOUND_DEVICE_BUILTIN_SPEAKER;
+               const int r = sound_manager_get_current_media_playback_device_type(
+                               &devType);
+               DLOG("devType: %d (r: %d);", devType, r);
+
+               switch (r) {
+               case 0:
+                       setMediaDeviceState(true);
+                       break;
+               case SOUND_MANAGER_ERROR_NO_DATA:
+                       setMediaDeviceState(false);
+                       break;
+               default:
+                       LOG_RETURN(RES_FAIL,
+                                       "sound_manager_get_current_media_playback_device_type() "
+                                       "failed: %d;", r);
+               }
+
+               return RES_OK;
+       }
+
+       void SoundManager::onDeviceConnectionChanged(sound_device_h device,
+                       bool isConnected)
+       {
+               updateMediaDeviceState();
+       }
+
+       void SoundManager::onDeviceStateChanged(sound_device_h device,
+                       sound_device_state_e state)
+       {
+               updateMediaDeviceState();
+       }
+
+       void SoundManager::onVolumeChanged(sound_type_e type, unsigned int volume)
+       {
+               if (type == SOUND_TYPE_MEDIA) {
+                       setCurrentMediaVolume(volume);
+               }
+       }
+}