2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @brief This file include implementation of public API for the SoundPool.
22 #include "sound_pool.h"
23 #include "sound_pool_private.h"
25 #include "internal/soundpool.h"
26 #include "internal/source.h"
27 #include "internal/stream.h"
30 * Public Implementation
33 static unsigned int sound_pool_amount = 0;
35 sound_pool_error_e sound_pool_create(sound_pool_h *pool)
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);
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);
52 SP_INFO("Number of created pools is [%u]", sound_pool_amount);
58 sound_pool_error_e sound_pool_destroy(sound_pool_h pool)
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");
66 SP_INFO("Number of pools is [%u]", sound_pool_amount);
72 sound_pool_error_e sound_pool_load_source_from_file(sound_pool_h pool,
73 const char *file_name, const char *tag)
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.");
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);
94 sound_pool_error_e sound_pool_unload_source(sound_pool_h pool, const char *tag)
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);
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);
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)
117 SP_INST_CHECK(id, SOUND_POOL_ERROR_INVALID_PARAMETER);
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);
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.");
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);
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",
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);
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 "
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.");
164 sound_pool_error_e sound_pool_stream_pause(sound_pool_h pool, unsigned id)
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);
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);
178 _sound_stream_priority_update_playback(_pool->mgr_priority);
184 sound_pool_error_e sound_pool_stream_resume(sound_pool_h pool, unsigned id)
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);
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);
197 _sound_stream_priority_update_playback(_pool->mgr_priority);
203 sound_pool_error_e sound_pool_stream_stop(sound_pool_h pool, unsigned id)
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);
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);
221 sound_pool_error_e sound_pool_get_state(sound_pool_h pool, sound_pool_state_e *state)
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");
233 sound_pool_error_e sound_pool_activate(sound_pool_h pool)
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");
245 sound_pool_error_e sound_pool_deactivate(sound_pool_h pool)
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");
257 sound_pool_error_e sound_pool_set_volume(sound_pool_h pool, float volume)
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");
269 sound_pool_error_e sound_pool_get_volume(sound_pool_h pool, float *volume)
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");
281 sound_pool_error_e sound_pool_stream_get_state(sound_pool_h pool, unsigned id,
282 sound_pool_stream_state_e *state)
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);
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);
300 sound_pool_error_e sound_pool_stream_set_loop(sound_pool_h pool, unsigned id,
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);
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);
319 sound_pool_error_e sound_pool_stream_get_loop(sound_pool_h pool, unsigned id,
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);
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);
338 sound_pool_error_e sound_pool_stream_set_volume(sound_pool_h pool, unsigned id,
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);
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);
357 sound_pool_error_e sound_pool_stream_get_volume(sound_pool_h pool, unsigned id,
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);
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);
376 sound_pool_error_e sound_pool_stream_set_priority(sound_pool_h pool,
377 unsigned id, unsigned priority)
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);
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);
395 sound_pool_error_e sound_pool_stream_get_priority(sound_pool_h pool,
396 unsigned id, unsigned *priority)
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);
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);
414 sound_pool_error_e sound_pool_set_state_change_callback(sound_pool_h pool,
415 sound_pool_state_change_cb callback, void *data)
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");
427 sound_pool_error_e sound_pool_unset_state_change_callback(sound_pool_h pool)
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");
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)
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);
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);
459 sound_pool_error_e sound_pool_stream_unset_state_change_callback(
460 sound_pool_h pool, unsigned id)
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);
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);