Updated priority concept has been implemented.
[platform/core/api/sound-pool.git] / src / sound_pool.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 sound_pool.c
19  * @brief This file include implementation of public API for the SoundPool.
20  */
21
22 #include "sound_pool.h"
23 #include "sound_pool_private.h"
24
25 #include "internal/soundpool.h"
26 #include "internal/source.h"
27 #include "internal/stream.h"
28
29 /*
30 * Public Implementation
31 */
32
33 static unsigned int sound_pool_amount = 0;
34
35 sound_pool_error_e sound_pool_create(sound_pool_h *pool)
36 {
37         SP_DEBUG_FENTER();
38         SP_RETVM_IF(SOUND_POOL_MAX_POOLS_AMOUNT < (sound_pool_amount + 1),
39                         SOUND_POOL_ERROR_INVALID_OPERATION,
40                         "Max pools count should be no more than %d.",
41                         SOUND_POOL_MAX_POOLS_AMOUNT);
42         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
43
44         sound_pool_t *_pool = NULL;
45         sound_pool_error_e ret = _sound_pool_create(&_pool);
46         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret,
47                         "Error while creating sound pool instance.");
48         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_OPERATION);
49
50         *pool = _pool;
51         ++sound_pool_amount;
52         SP_INFO("Number of created pools is [%u]", sound_pool_amount);
53
54         SP_DEBUG_FLEAVE();
55         return ret;
56 }
57
58 sound_pool_error_e sound_pool_destroy(sound_pool_h pool)
59 {
60         SP_DEBUG_FENTER();
61
62         sound_pool_error_e ret = _sound_pool_destroy((sound_pool_t *)pool);
63         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret,
64                         "Error occurred when destroying the sound pool");
65         --sound_pool_amount;
66         SP_INFO("Number of pools is [%u]", sound_pool_amount);
67
68         SP_DEBUG_FLEAVE();
69         return ret;
70 }
71
72 sound_pool_error_e sound_pool_load_source_from_file(sound_pool_h pool,
73                 const char *file_name, const char *tag)
74 {
75         SP_DEBUG_FENTER();
76
77         sound_pool_t *_pool = (sound_pool_t *)pool;
78         sound_source_t *_src = NULL;
79         sound_pool_error_e ret = _sound_source_create(_pool, tag, &_src);
80         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret,
81                         "Error while creating sound source instance.");
82
83         ret = _sound_source_load_from_file(_src, file_name);
84         if (ret != SOUND_POOL_ERROR_NONE) {
85                 SP_ERROR("Error occurred when loading sound source from file");
86                 if (_sound_source_destroy(_src) != SOUND_POOL_ERROR_NONE)
87                         SP_ERROR("Error occurred during removal of sound source[%s].", tag);
88         }
89
90         SP_DEBUG_FLEAVE();
91         return ret;
92 }
93
94 sound_pool_error_e sound_pool_unload_source(sound_pool_h pool, const char *tag)
95 {
96         SP_DEBUG_FENTER();
97
98         sound_pool_t *_pool = (sound_pool_t *)pool;
99         sound_source_t *_source = NULL;
100         sound_pool_error_e ret = _sound_pool_get_source_by_tag(_pool, tag, &_source);
101         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
102                         "getting sound source [%s] from the sound pool", tag);
103         SP_INST_CHECK(_source, SOUND_POOL_ERROR_INVALID_OPERATION);
104
105         ret = _sound_source_destroy(_source);
106         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
107                         "destroying sound source [%s]", tag);
108
109         SP_DEBUG_FLEAVE();
110         return ret;
111 }
112
113 sound_pool_error_e sound_pool_stream_play(sound_pool_h pool, const char *tag,
114                 unsigned loop, float volume, unsigned priority,
115                 sound_pool_stream_state_change_cb callback, void *data, unsigned *id)
116 {
117         SP_DEBUG_FENTER();
118         SP_INST_CHECK(id, SOUND_POOL_ERROR_INVALID_PARAMETER);
119
120         sound_pool_t *_pool = (sound_pool_t *)pool;
121         sound_source_t *_source = NULL;
122         sound_pool_error_e ret_destroy = SOUND_POOL_ERROR_NONE;
123         sound_pool_error_e ret = _sound_pool_get_source_by_tag(_pool, tag, &_source);
124         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
125                         "getting sound source [%s] from the sound pool", tag);
126         SP_INST_CHECK(_source, SOUND_POOL_ERROR_INVALID_OPERATION);
127
128         sound_stream_t *_stream = NULL;
129         ret = _sound_stream_create(_source, &_stream);
130         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret,
131                         "Error while creating sound source instance.");
132
133         ret = _sound_stream_set_loop(_stream, loop);
134         if (ret != SOUND_POOL_ERROR_NONE)
135                 GOTO_FAIL("Error occurred when setting sound stream loop parameter", cfail);
136
137         ret = _sound_stream_set_volume(_stream, volume);
138         if (ret != SOUND_POOL_ERROR_NONE)
139                 GOTO_FAIL("Error occurred when setting sound stream volume " "parameter",
140                                 cfail);
141
142         if (callback) {
143                 ret = _sound_stream_set_callback(_stream, callback, data);
144                 if (ret != SOUND_POOL_ERROR_NONE)
145                         GOTO_FAIL("Error occurred when setting sound stream callback", cfail);
146         }
147
148         ret = _sound_stream_set_priority(_stream, priority);
149         if (ret != SOUND_POOL_ERROR_NONE)
150                 GOTO_FAIL("Error occurred when setting sound stream priority "
151                                 "parameter", cfail);
152
153         *id = _stream->id;
154
155         SP_DEBUG_FLEAVE();
156         return ret;
157
158 cfail:
159         ret_destroy = _sound_stream_destroy(_stream);
160         SP_RETVM_IF(SOUND_POOL_ERROR_NONE != ret_destroy, ret_destroy,
161                         "Error occurred during removal of uncompleted sound stream.");
162         SP_DEBUG_FLEAVE();
163         return ret;
164 }
165
166 sound_pool_error_e sound_pool_stream_pause(sound_pool_h pool, unsigned id)
167 {
168         SP_DEBUG_FENTER();
169
170         sound_pool_t *_pool = (sound_pool_t *)pool;
171         sound_stream_t *_stream = NULL;
172         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
173         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
174                         "getting sound stream [%u] from the sound pool", id);
175         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
176
177         ret = _sound_stream_pause(_stream);
178         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
179                         "pausing sound stream [%u]", id);
180
181         _sound_stream_priority_update_playback(_pool->mgr_priority);
182
183         SP_DEBUG_FLEAVE();
184         return ret;
185 }
186
187 sound_pool_error_e sound_pool_stream_resume(sound_pool_h pool, unsigned id)
188 {
189         SP_DEBUG_FENTER();
190         sound_pool_t *_pool = (sound_pool_t *)pool;
191         sound_stream_t *_stream = NULL;
192         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
193         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
194                         "getting sound stream [%u] from the sound pool", id);
195         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
196
197         ret = _sound_stream_resume(_stream);
198         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
199                         "resuming sound stream [%u]", id);
200
201         _sound_stream_priority_update_playback(_pool->mgr_priority);
202
203         SP_DEBUG_FLEAVE();
204         return ret;
205 }
206
207 sound_pool_error_e sound_pool_stream_stop(sound_pool_h pool, unsigned id)
208 {
209         SP_DEBUG_FENTER();
210
211         sound_pool_t *_pool = (sound_pool_t *)pool;
212         sound_stream_t *_stream = NULL;
213         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
214         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
215                         "getting sound stream [%u] from the sound pool", id);
216         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
217
218         ret = _sound_stream_stop(_stream);
219         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
220                         "stopping sound stream [%u]", id);
221
222         SP_DEBUG_FLEAVE();
223         return ret;
224 }
225
226 sound_pool_error_e sound_pool_get_state(sound_pool_h pool, sound_pool_state_e *state)
227 {
228         SP_DEBUG_FENTER();
229
230         sound_pool_error_e ret = _sound_pool_get_state(pool, state);
231         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
232                         "getting state of the sound pool");
233
234         SP_DEBUG_FLEAVE();
235         return ret;
236 }
237
238 sound_pool_error_e sound_pool_activate(sound_pool_h pool)
239 {
240         SP_DEBUG_FENTER();
241
242         sound_pool_error_e ret = _sound_pool_activate((sound_pool_t *)pool);
243         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
244                         "activating sound pool");
245
246         SP_DEBUG_FLEAVE();
247         return ret;
248 }
249
250 sound_pool_error_e sound_pool_deactivate(sound_pool_h pool)
251 {
252         SP_DEBUG_FENTER();
253
254         sound_pool_error_e ret = _sound_pool_deactivate((sound_pool_t *)pool);
255         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
256                         "deactivating sound pool");
257
258         SP_DEBUG_FLEAVE();
259         return ret;
260 }
261
262 sound_pool_error_e sound_pool_set_volume(sound_pool_h pool, float volume)
263 {
264         SP_DEBUG_FENTER();
265
266         sound_pool_error_e ret = _sound_pool_set_volume((sound_pool_t *)pool, volume);
267         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
268                         "setting volume for sound pool");
269
270         SP_DEBUG_FLEAVE();
271         return ret;
272 }
273
274 sound_pool_error_e sound_pool_get_volume(sound_pool_h pool, float *volume)
275 {
276         SP_DEBUG_FENTER();
277
278         sound_pool_error_e ret = _sound_pool_get_volume((sound_pool_t *)pool, volume);
279         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
280                         "getting volume for sound pool");
281
282         SP_DEBUG_FLEAVE();
283         return ret;
284 }
285
286 sound_pool_error_e sound_pool_stream_get_state(sound_pool_h pool, unsigned id,
287                 sound_pool_stream_state_e *state)
288 {
289         SP_DEBUG_FENTER();
290
291         sound_pool_t *_pool = (sound_pool_t *)pool;
292         sound_stream_t *_stream = NULL;
293         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
294         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
295                         "getting sound stream [%u] from the sound pool", id);
296         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
297
298         ret = _sound_stream_get_state(_stream, state);
299         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
300                         "getting sound stream [%u] state", id);
301
302         SP_DEBUG_FLEAVE();
303         return ret;
304 }
305
306 sound_pool_error_e sound_pool_stream_set_loop(sound_pool_h pool, unsigned id,
307                 unsigned loop)
308 {
309         SP_DEBUG_FENTER();
310
311         sound_pool_t *_pool = (sound_pool_t *)pool;
312         sound_stream_t *_stream = NULL;
313         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
314         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
315                         "getting sound stream [%u] from the sound pool", id);
316         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
317
318         ret = _sound_stream_set_loop(_stream, loop);
319         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
320                         "setting sound stream [%u] loop parameter", id);
321
322         SP_DEBUG_FLEAVE();
323         return ret;
324 }
325
326 sound_pool_error_e sound_pool_stream_get_loop(sound_pool_h pool, unsigned id,
327                 unsigned *loop)
328 {
329         SP_DEBUG_FENTER();
330
331         sound_pool_t *_pool = (sound_pool_t *)pool;
332         sound_stream_t *_stream = NULL;
333         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
334         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
335                         "getting sound stream [%u] from the sound pool", id);
336         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
337
338         ret = _sound_stream_get_loop(_stream, loop);
339         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
340                         "getting sound stream [%u] loop parameter", id);
341
342         SP_DEBUG_FLEAVE();
343         return ret;
344 }
345
346 sound_pool_error_e sound_pool_stream_set_volume(sound_pool_h pool, unsigned id,
347                 float volume)
348 {
349         SP_DEBUG_FENTER();
350
351         sound_pool_t *_pool = (sound_pool_t *)pool;
352         sound_stream_t *_stream = NULL;
353         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
354         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
355                         "getting sound stream [%u] from the sound pool", id);
356         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
357
358         ret = _sound_stream_set_volume(_stream, volume);
359         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
360                         "setting sound stream [%u] volume parameter", id);
361
362         SP_DEBUG_FLEAVE();
363         return ret;
364 }
365
366 sound_pool_error_e sound_pool_stream_get_volume(sound_pool_h pool, unsigned id,
367                 float *volume)
368 {
369         SP_DEBUG_FENTER();
370
371         sound_pool_t *_pool = (sound_pool_t *)pool;
372         sound_stream_t *_stream = NULL;
373         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
374         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
375                         "getting sound stream [%u] from the sound pool", id);
376         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
377
378         ret = _sound_stream_get_volume(_stream, volume);
379         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
380                         "getting sound stream [%u] volume parameter", id);
381
382         SP_DEBUG_FLEAVE();
383         return ret;
384 }
385
386 sound_pool_error_e sound_pool_stream_set_priority(sound_pool_h pool,
387                 unsigned id, unsigned priority)
388 {
389         SP_DEBUG_FENTER();
390
391         sound_pool_t *_pool = (sound_pool_t *)pool;
392         sound_stream_t *_stream = NULL;
393         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
394         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
395                         "getting sound stream [%u] from the sound pool", id);
396         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
397
398         ret = _sound_stream_set_priority(_stream, priority);
399         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
400                         "setting sound stream [%u] priority parameter", id);
401
402         SP_DEBUG_FLEAVE();
403         return ret;
404 }
405
406 sound_pool_error_e sound_pool_stream_get_priority(sound_pool_h pool,
407                 unsigned id, unsigned *priority)
408 {
409         SP_DEBUG_FENTER();
410
411         sound_pool_t *_pool = (sound_pool_t *)pool;
412         sound_stream_t *_stream = NULL;
413         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
414         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
415                         "getting sound stream [%u] from the sound pool", id);
416         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
417
418         ret = _sound_stream_get_priority(_stream, priority);
419         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
420                         "getting sound stream [%u] priority parameter", id);
421
422         SP_DEBUG_FLEAVE();
423         return ret;
424 }
425
426 sound_pool_error_e sound_pool_set_state_change_callback(sound_pool_h pool,
427                 sound_pool_state_change_cb callback, void *data)
428 {
429         SP_DEBUG_FENTER();
430
431         sound_pool_error_e ret = _sound_pool_set_callback((sound_pool_t *)pool, callback, data);
432         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
433                         "setting state changing callback for sound pool");
434
435         SP_DEBUG_FLEAVE();
436         return ret;
437 }
438
439 sound_pool_error_e sound_pool_unset_state_change_callback(sound_pool_h pool)
440 {
441         SP_DEBUG_FENTER();
442
443         sound_pool_error_e ret = _sound_pool_unset_callback((sound_pool_t *)pool);
444         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
445                         "unsetting state changing callback for sound pool");
446
447         SP_DEBUG_FLEAVE();
448         return ret;
449 }
450
451 sound_pool_error_e sound_pool_stream_set_state_change_callback(
452                 sound_pool_h pool, unsigned id,
453                 sound_pool_stream_state_change_cb callback, void *data)
454 {
455         SP_DEBUG_FENTER();
456
457         sound_pool_t *_pool = (sound_pool_t *)pool;
458         sound_stream_t *_stream = NULL;
459         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
460         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
461                         "getting sound stream [%u] from the sound pool", id);
462         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
463
464         ret = _sound_stream_set_callback(_stream, callback, data);
465         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
466                         "setting sound stream [%u] state changing callback", id);
467
468         SP_DEBUG_FLEAVE();
469         return ret;
470 }
471
472 sound_pool_error_e sound_pool_stream_unset_state_change_callback(
473                 sound_pool_h pool, unsigned id)
474 {
475         SP_DEBUG_FENTER();
476
477         sound_pool_t *_pool = (sound_pool_t *)pool;
478         sound_stream_t *_stream = NULL;
479         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
480         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
481                         "getting sound stream [%u] from the sound pool", id);
482         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
483
484         ret = _sound_stream_unset_callback(_stream);
485         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
486                         "unsetting sound stream [%u] state changing callback", id);
487
488         SP_DEBUG_FLEAVE();
489         return ret;
490 }