e62ae59f52cdb02dd2d5f3242cf37e9ae47587a6
[platform/core/api/sound-pool.git] / src / soundpool.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file soundpool.c
19  * @brief This file include implementation of protected API for the SoundPool.
20  */
21
22 #include "internal/soundpool.h"
23
24 #include "internal/priority.h"
25 #include "internal/stream_cb_manager.h"
26
27 #include "internal/source.h"
28 #include "internal/stream.h"
29
30 #define DEFAULT_VOLUME_VALUE 1.0f
31
32 static void __stream_activate_iter(gpointer key, gpointer value,
33                 gpointer user_data);
34 static void __stream_deactivate_iter(gpointer key, gpointer value,
35                 gpointer user_data);
36
37 static void __stream_activate_iter(gpointer key, gpointer value,
38                 gpointer user_data)
39 {
40         SP_DEBUG_FENTER();
41         SP_RETM_IF(!user_data, "Empty user data.");
42         SP_RETM_IF(!value, "Empty sound stream data.");
43         guint *len = (guint*)user_data;
44         sound_stream_t *stream = (sound_stream_t *)value;
45
46         gboolean can_run = TRUE;
47         if (SOUND_POOL_ERROR_NONE
48                         == _sound_stream_priority_check(stream->parent_source->parent_pool->mgr_priority,
49                                         stream, &can_run))
50                 if (stream->state == SOUND_POOL_STREAM_STATE_SUSPENDED && TRUE == can_run) {
51                         alSourcePlay(stream->al_source);
52                         (*len)++;
53                 }
54
55         SP_DEBUG_FLEAVE();
56 }
57
58 static void __stream_deactivate_iter(gpointer key, gpointer value,
59                 gpointer user_data)
60 {
61         SP_DEBUG_FENTER();
62         SP_RETM_IF(!user_data, "Empty user data.");
63         SP_RETM_IF(!value, "Empty sound stream data.");
64         guint *len = (guint*)user_data;
65         sound_stream_t *stream = (sound_stream_t *)value;
66
67         if (stream->state == SOUND_POOL_STREAM_STATE_PLAYING) {
68                 alSourcePause(stream->al_source);
69                 (*len)++;
70         }
71
72         SP_DEBUG_FLEAVE();
73 }
74
75 sound_pool_error_e _sound_pool_create(sound_pool_t **pool)
76 {
77         SP_DEBUG_FENTER();
78         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
79
80         sound_pool_t *_pool = NULL;
81         SP_RETVM_IF(!(_pool = g_try_malloc0(sizeof(*_pool))),
82                         SOUND_POOL_ERROR_OUT_OF_MEMORY,
83                         "Memory alloc failure. Can't create sound pool");
84         SP_RETVM_IF(!pool, SOUND_POOL_ERROR_INVALID_PARAMETER, "Can't create "
85                         "sound pool. Pool pointer is NULL");
86
87         sound_pool_error_e ret = SOUND_POOL_ERROR_NONE;
88         _pool->al_context = NULL;
89         _pool->al_context = alcCreateContext(alcOpenDevice(NULL), NULL);
90         if (!_pool->al_context) {
91                 ret = SOUND_POOL_ERROR_OUT_OF_MEMORY;
92                 GOTO_FAIL("Memory alloc failure. Can't create OpenAL Context", cfail);
93         }
94
95         _pool->sources = NULL;
96         _pool->streams = NULL;
97
98         _pool->sources = g_hash_table_new(g_str_hash, g_str_equal);
99         if (!_pool->sources) {
100                 ret = SOUND_POOL_ERROR_OUT_OF_MEMORY;
101                 GOTO_FAIL("Memory alloc failure. Can't create internal sources hash "
102                                 "table in sound pool", cfail);
103         }
104
105         _pool->streams = g_hash_table_new(g_int_hash, g_int_equal);
106         if (!_pool->streams) {
107                 ret = SOUND_POOL_ERROR_OUT_OF_MEMORY;
108                 GOTO_FAIL("Memory alloc failure. Can't create internal streams hash "
109                                 "table in sound pool", cfail);
110         }
111
112         _pool->volume = DEFAULT_VOLUME_VALUE;
113         _pool->max_stream_index = 0;
114         _pool->state = SOUND_POOL_STATE_INACTIVE;
115         _pool->state_cb_info.callback = NULL;
116         _pool->state_cb_info.user_data = NULL;
117
118         _pool->cbmgr = NULL;
119         ret = _stream_cb_manager_create(_pool, &_pool->cbmgr);
120         if (ret != SOUND_POOL_ERROR_NONE)
121                 GOTO_FAIL("Error occurred when trying to create stream state changing "
122                 "callback manager instance", cfail);
123
124         _pool->mgr_priority = NULL;
125         ret = _sound_stream_priority_create(_pool, &_pool->mgr_priority);
126         if (ret != SOUND_POOL_ERROR_NONE)
127                 GOTO_FAIL("Error occurred initialising of priorities.", cfail);
128
129         *pool = _pool;
130         SP_DEBUG_FLEAVE();
131         return ret;
132
133 cfail:
134         _sound_pool_destroy(_pool);
135         SP_DEBUG_FLEAVE();
136         return ret;
137 }
138
139 sound_pool_error_e _sound_pool_destroy(sound_pool_t *pool)
140 {
141         SP_DEBUG_FENTER();
142         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
143         _sound_pool_deactivate(pool);
144
145         if (pool->cbmgr) {
146                 sound_pool_error_e ret = _stream_cb_manager_destroy(pool->cbmgr);
147                 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret,
148                                 "Error occurred when " "trying to destroy sound pool callback manager.");
149                 pool->cbmgr = NULL;
150         }
151
152         if (pool->sources) {
153                 GHashTableIter iter;
154                 gpointer key, value;
155                 g_hash_table_iter_init(&iter, pool->sources);
156                 while (g_hash_table_iter_next(&iter, &key, &value)) {
157                         guint size_before = g_hash_table_size(pool->sources);
158                         _sound_source_destroy((sound_source_t*)value);
159                         guint size_after = g_hash_table_size(pool->sources);
160                         if (size_before != size_after)
161                                 g_hash_table_iter_init(&iter, pool->sources);
162                 }
163                 g_hash_table_unref(pool->sources);
164                 pool->sources = NULL;
165         }
166
167         if (pool->streams) {
168                 g_hash_table_unref(pool->streams);
169                 pool->streams = NULL;
170         }
171
172         if (pool->mgr_priority) {
173                 sound_pool_error_e ret = _sound_stream_priority_destroy(pool->mgr_priority);
174                 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret,
175                                 "Error occurred when trying to destroy priority manager.");
176                 pool->mgr_priority = NULL;
177         }
178
179         if (pool->al_context) {
180                 ALCdevice *device = alcGetContextsDevice(pool->al_context);
181                 alcMakeContextCurrent(NULL);
182                 alcDestroyContext(pool->al_context);
183                 alcCloseDevice(device);
184         }
185
186         SP_SAFE_GFREE(pool);
187
188         SP_DEBUG_FLEAVE();
189         return SOUND_POOL_ERROR_NONE;
190 }
191
192 sound_pool_error_e _sound_pool_activate(sound_pool_t *pool)
193 {
194         SP_DEBUG_FENTER();
195         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
196
197         SP_RETVM_IF(pool->state == SOUND_POOL_STATE_ACTIVE,
198                         SOUND_POOL_ERROR_INVALID_OPERATION,
199                         "Already activated sound pool can't be activated again.");
200
201         sound_pool_state_e old_state = pool->state;
202         pool->state = SOUND_POOL_STATE_ACTIVE;
203
204         /* Generate array of all AlSources in pool */
205         GPtrArray *streams = NULL;
206         if (g_hash_table_size(pool->streams) > 0) {
207                 SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
208                                 SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
209                 guint len = 0;
210                 g_hash_table_foreach(pool->streams, __stream_activate_iter, &len);
211                 SP_INFO("Resuming [%d] number of streams.", len);
212         }
213
214         if (streams)
215                 g_ptr_array_free(streams, TRUE);
216
217         SP_INFO("Sound pool has been activated");
218
219         if (pool->state_cb_info.callback)
220                 pool->state_cb_info.callback(pool, old_state, pool->state,
221                                 pool->state_cb_info.user_data);
222
223         SP_DEBUG_FLEAVE();
224         return SOUND_POOL_ERROR_NONE;
225 }
226
227 sound_pool_error_e _sound_pool_deactivate(sound_pool_t *pool)
228 {
229         SP_DEBUG_FENTER();
230         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
231         SP_RETVM_IF(pool->state == SOUND_POOL_STATE_INACTIVE,
232                         SOUND_POOL_ERROR_INVALID_OPERATION,
233                         "Already deactivated sound pool can't be deactivated again.");
234
235         sound_pool_state_e old_state = pool->state;
236         pool->state = SOUND_POOL_STATE_INACTIVE;
237
238         /* Generate array of all AlSources in pool */
239         GPtrArray *streams = NULL;
240         if (g_hash_table_size(pool->streams) > 0) {
241                 SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
242                                 SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
243                 guint len = 0;
244                 g_hash_table_foreach(pool->streams, __stream_deactivate_iter, &len);
245                 SP_INFO("Suspending [%d] number of streams.", len);
246         }
247
248         if (streams)
249                 g_ptr_array_free(streams, TRUE);
250
251         SP_INFO("Sound pool has been deactivated");
252
253         if (pool->state_cb_info.callback)
254                 pool->state_cb_info.callback(pool, old_state, pool->state,
255                                 pool->state_cb_info.user_data);
256
257         SP_DEBUG_FLEAVE();
258         return SOUND_POOL_ERROR_NONE;
259 }
260
261 sound_pool_error_e _sound_pool_get_state(sound_pool_t *pool,
262                 sound_pool_state_e *state)
263 {
264         SP_DEBUG_FENTER();
265         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
266         SP_INST_CHECK(state, SOUND_POOL_ERROR_INVALID_PARAMETER);
267         *state = pool->state;
268         SP_DEBUG_FLEAVE();
269         return SOUND_POOL_ERROR_NONE;
270 }
271
272 sound_pool_error_e _sound_pool_set_volume(sound_pool_t *pool, float volume)
273 {
274         SP_DEBUG_FENTER();
275         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
276         SP_RETVM_IF((volume < 0.0f || volume > 1.0f),
277                         SOUND_POOL_ERROR_INVALID_PARAMETER,
278                         "Sound pool volume is not in [0.0..1.0] range.");
279
280         SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
281                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
282         alListenerf(AL_GAIN, volume);
283         pool->volume = volume;
284
285         SP_DEBUG_FLEAVE();
286         return SOUND_POOL_ERROR_NONE;
287 }
288
289 sound_pool_error_e _sound_pool_get_volume(sound_pool_t *pool, float *volume)
290 {
291         SP_DEBUG_FENTER();
292         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
293         SP_INST_CHECK(volume, SOUND_POOL_ERROR_INVALID_PARAMETER);
294         *volume = pool->volume;
295         SP_DEBUG_FLEAVE();
296         return SOUND_POOL_ERROR_NONE;
297 }
298
299 sound_pool_error_e _sound_pool_set_callback(sound_pool_t *pool,
300                 sound_pool_state_change_cb callback, void *data)
301 {
302         SP_DEBUG_FENTER();
303         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
304         SP_INST_CHECK(callback, SOUND_POOL_ERROR_INVALID_PARAMETER);
305
306         pool->state_cb_info.callback = callback;
307         pool->state_cb_info.user_data = data;
308
309         SP_DEBUG_FLEAVE();
310         return SOUND_POOL_ERROR_NONE;
311 }
312
313 sound_pool_error_e _sound_pool_unset_callback(sound_pool_t *pool)
314 {
315         SP_DEBUG_FENTER();
316         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
317
318         pool->state_cb_info.callback = NULL;
319         pool->state_cb_info.user_data = NULL;
320
321         SP_DEBUG_FLEAVE();
322         return SOUND_POOL_ERROR_NONE;
323 }