Updated API according to the changed stream prioritization concept
[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 = _sound_pool_get_source_by_tag(_pool, tag, &_source);
123         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
124                         "getting sound source [%s] from the sound pool", tag);
125         SP_INST_CHECK(_source, SOUND_POOL_ERROR_INVALID_OPERATION);
126
127         sound_stream_t *_stream = NULL;
128         ret = _sound_stream_create(_source, &_stream);
129         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret,
130                         "Error while creating sound source instance.");
131
132         ret = _sound_stream_set_loop(_stream, loop);
133         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
134                         "setting sound stream loop parameter", tag);
135
136         ret = _sound_stream_set_volume(_stream, volume);
137         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
138                         "setting sound stream volume parameter", tag);
139
140         if (callback) {
141                 ret = _sound_stream_set_callback(_stream, callback, data);
142                 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
143                                 "setting sound stream callback", tag);
144         }
145
146         ret = _sound_stream_set_priority(_stream, priority);
147         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
148                         "setting sound stream priority parameter", tag);
149
150         *id = _stream->id;
151
152         SP_DEBUG_FLEAVE();
153         return ret;
154 }
155
156 sound_pool_error_e sound_pool_stream_pause(sound_pool_h pool, unsigned id)
157 {
158         SP_DEBUG_FENTER();
159
160         sound_pool_t *_pool = (sound_pool_t *)pool;
161         sound_stream_t *_stream = NULL;
162         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
163         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
164                         "getting sound stream [%u] from the sound pool", id);
165         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
166
167         ret = _sound_stream_pause(_stream);
168         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
169                         "pausing sound stream [%u]", id);
170
171         SP_DEBUG_FLEAVE();
172         return ret;
173 }
174
175 sound_pool_error_e sound_pool_stream_resume(sound_pool_h pool, unsigned id)
176 {
177         SP_DEBUG_FENTER();
178         sound_pool_t *_pool = (sound_pool_t *)pool;
179         sound_stream_t *_stream = NULL;
180         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
181         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
182                         "getting sound stream [%u] from the sound pool", id);
183         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
184
185         ret = _sound_stream_resume(_stream);
186         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
187                         "resuming sound stream [%u]", id);
188
189         SP_DEBUG_FLEAVE();
190         return ret;
191 }
192
193 sound_pool_error_e sound_pool_stream_stop(sound_pool_h pool, unsigned id)
194 {
195         SP_DEBUG_FENTER();
196
197         sound_pool_t *_pool = (sound_pool_t *)pool;
198         sound_stream_t *_stream = NULL;
199         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
200         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
201                         "getting sound stream [%u] from the sound pool", id);
202         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
203
204         ret = _sound_stream_stop(_stream);
205         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
206                         "stopping sound stream [%u]", id);
207
208         SP_DEBUG_FLEAVE();
209         return ret;
210 }
211
212 sound_pool_error_e sound_pool_get_state(sound_pool_h pool, sound_pool_state_e *state)
213 {
214         SP_DEBUG_FENTER();
215
216         sound_pool_error_e ret = _sound_pool_get_state(pool, state);
217         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
218                         "getting state of the sound pool");
219
220         SP_DEBUG_FLEAVE();
221         return ret;
222 }
223
224 sound_pool_error_e sound_pool_activate(sound_pool_h pool)
225 {
226         SP_DEBUG_FENTER();
227
228         sound_pool_error_e ret = _sound_pool_activate((sound_pool_t *)pool);
229         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
230                         "activating sound pool");
231
232         SP_DEBUG_FLEAVE();
233         return ret;
234 }
235
236 sound_pool_error_e sound_pool_deactivate(sound_pool_h pool)
237 {
238         SP_DEBUG_FENTER();
239
240         sound_pool_error_e ret = _sound_pool_deactivate((sound_pool_t *)pool);
241         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
242                         "deactivating sound pool");
243
244         SP_DEBUG_FLEAVE();
245         return ret;
246 }
247
248 sound_pool_error_e sound_pool_set_volume(sound_pool_h pool, float volume)
249 {
250         SP_DEBUG_FENTER();
251
252         sound_pool_error_e ret = _sound_pool_set_volume((sound_pool_t *)pool, volume);
253         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
254                         "setting volume for sound pool");
255
256         SP_DEBUG_FLEAVE();
257         return ret;
258 }
259
260 sound_pool_error_e sound_pool_get_volume(sound_pool_h pool, float *volume)
261 {
262         SP_DEBUG_FENTER();
263
264         sound_pool_error_e ret = _sound_pool_get_volume((sound_pool_t *)pool, volume);
265         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
266                         "getting volume for sound pool");
267
268         SP_DEBUG_FLEAVE();
269         return ret;
270 }
271
272 sound_pool_error_e sound_pool_stream_get_state(sound_pool_h pool, unsigned id,
273                 sound_pool_stream_state_e *state)
274 {
275         SP_DEBUG_FENTER();
276
277         sound_pool_t *_pool = (sound_pool_t *)pool;
278         sound_stream_t *_stream = NULL;
279         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
280         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
281                         "getting sound stream [%u] from the sound pool", id);
282         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
283
284         ret = _sound_stream_get_state(_stream, state);
285         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
286                         "getting sound stream [%u] state", id);
287
288         SP_DEBUG_FLEAVE();
289         return ret;
290 }
291
292 sound_pool_error_e sound_pool_stream_set_loop(sound_pool_h pool, unsigned id,
293                 unsigned loop)
294 {
295         SP_DEBUG_FENTER();
296
297         sound_pool_t *_pool = (sound_pool_t *)pool;
298         sound_stream_t *_stream = NULL;
299         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
300         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
301                         "getting sound stream [%u] from the sound pool", id);
302         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
303
304         ret = _sound_stream_set_loop(_stream, loop);
305         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
306                         "setting sound stream [%u] loop parameter", id);
307
308         SP_DEBUG_FLEAVE();
309         return ret;
310 }
311
312 sound_pool_error_e sound_pool_stream_get_loop(sound_pool_h pool, unsigned id,
313                 unsigned *loop)
314 {
315         SP_DEBUG_FENTER();
316
317         sound_pool_t *_pool = (sound_pool_t *)pool;
318         sound_stream_t *_stream = NULL;
319         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
320         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
321                         "getting sound stream [%u] from the sound pool", id);
322         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
323
324         ret = _sound_stream_get_loop(_stream, loop);
325         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
326                         "getting sound stream [%u] loop parameter", id);
327
328         SP_DEBUG_FLEAVE();
329         return ret;
330 }
331
332 sound_pool_error_e sound_pool_stream_set_volume(sound_pool_h pool, unsigned id,
333                 float volume)
334 {
335         SP_DEBUG_FENTER();
336
337         sound_pool_t *_pool = (sound_pool_t *)pool;
338         sound_stream_t *_stream = NULL;
339         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
340         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
341                         "getting sound stream [%u] from the sound pool", id);
342         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
343
344         ret = _sound_stream_set_volume(_stream, volume);
345         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
346                         "setting sound stream [%u] volume parameter", id);
347
348         SP_DEBUG_FLEAVE();
349         return ret;
350 }
351
352 sound_pool_error_e sound_pool_stream_get_volume(sound_pool_h pool, unsigned id,
353                 float *volume)
354 {
355         SP_DEBUG_FENTER();
356
357         sound_pool_t *_pool = (sound_pool_t *)pool;
358         sound_stream_t *_stream = NULL;
359         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
360         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
361                         "getting sound stream [%u] from the sound pool", id);
362         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
363
364         ret = _sound_stream_get_volume(_stream, volume);
365         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
366                         "getting sound stream [%u] volume parameter", id);
367
368         SP_DEBUG_FLEAVE();
369         return ret;
370 }
371
372 sound_pool_error_e sound_pool_stream_set_priority(sound_pool_h pool,
373                 unsigned id, unsigned priority)
374 {
375         SP_DEBUG_FENTER();
376
377         sound_pool_t *_pool = (sound_pool_t *)pool;
378         sound_stream_t *_stream = NULL;
379         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
380         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
381                         "getting sound stream [%u] from the sound pool", id);
382         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
383
384         ret = _sound_stream_set_priority(_stream, priority);
385         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
386                         "setting sound stream [%u] priority parameter", id);
387
388         SP_DEBUG_FLEAVE();
389         return ret;
390 }
391
392 sound_pool_error_e sound_pool_stream_get_priority(sound_pool_h pool,
393                 unsigned id, unsigned *priority)
394 {
395         SP_DEBUG_FENTER();
396
397         sound_pool_t *_pool = (sound_pool_t *)pool;
398         sound_stream_t *_stream = NULL;
399         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
400         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
401                         "getting sound stream [%u] from the sound pool", id);
402         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
403
404         ret = _sound_stream_get_priority(_stream, priority);
405         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
406                         "getting sound stream [%u] priority parameter", id);
407
408         SP_DEBUG_FLEAVE();
409         return ret;
410 }
411
412 sound_pool_error_e sound_pool_set_state_change_callback(sound_pool_h pool,
413                 sound_pool_state_change_cb callback, void *data)
414 {
415         SP_DEBUG_FENTER();
416
417         sound_pool_error_e ret = _sound_pool_set_callback((sound_pool_t *)pool, callback, data);
418         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
419                         "setting state changing callback for sound pool");
420
421         SP_DEBUG_FLEAVE();
422         return ret;
423 }
424
425 sound_pool_error_e sound_pool_unset_state_change_callback(sound_pool_h pool)
426 {
427         SP_DEBUG_FENTER();
428
429         sound_pool_error_e ret = _sound_pool_unset_callback((sound_pool_t *)pool);
430         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
431                         "unsetting state changing callback for sound pool");
432
433         SP_DEBUG_FLEAVE();
434         return ret;
435 }
436
437 sound_pool_error_e sound_pool_stream_set_state_change_callback(
438                 sound_pool_h pool, unsigned id,
439                 sound_pool_stream_state_change_cb callback, void *data)
440 {
441         SP_DEBUG_FENTER();
442
443         sound_pool_t *_pool = (sound_pool_t *)pool;
444         sound_stream_t *_stream = NULL;
445         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
446         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
447                         "getting sound stream [%u] from the sound pool", id);
448         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
449
450         ret = _sound_stream_set_callback(_stream, callback, data);
451         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
452                         "setting sound stream [%u] state changing callback", id);
453
454         SP_DEBUG_FLEAVE();
455         return ret;
456 }
457
458 sound_pool_error_e sound_pool_stream_unset_state_change_callback(
459                 sound_pool_h pool, unsigned id)
460 {
461         SP_DEBUG_FENTER();
462
463         sound_pool_t *_pool = (sound_pool_t *)pool;
464         sound_stream_t *_stream = NULL;
465         sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
466         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
467                         "getting sound stream [%u] from the sound pool", id);
468         SP_INST_CHECK(_stream, SOUND_POOL_ERROR_INVALID_OPERATION);
469
470         ret = _sound_stream_unset_callback(_stream);
471         SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
472                         "unsetting sound stream [%u] state changing callback", id);
473
474         SP_DEBUG_FLEAVE();
475         return ret;
476 }