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