summaryrefslogtreecommitdiff
path: root/src/model/SoundManager.cpp
blob: 0d83a73c20cb25cbbf379c0a49072644ace72f5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * 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 "gallery/model/SoundManager.h"

#include "common.h"

namespace gallery {

	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);
		}
	}
}