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);
103 SP_INST_CHECK(_source, SOUND_POOL_ERROR_INVALID_OPERATION);
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);
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)
118 SP_INST_CHECK(id, SOUND_POOL_ERROR_INVALID_PARAMETER);
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);
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.");
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);
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",
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);
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 "
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.");
166 sound_pool_error_e sound_pool_stream_pause(sound_pool_h pool, unsigned id)
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);
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);
181 _sound_stream_priority_update_playback(_pool->mgr_priority);
187 sound_pool_error_e sound_pool_stream_resume(sound_pool_h pool, unsigned id)
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);
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);
201 _sound_stream_priority_update_playback(_pool->mgr_priority);
207 sound_pool_error_e sound_pool_stream_stop(sound_pool_h pool, unsigned id)
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);
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);
226 sound_pool_error_e sound_pool_get_state(sound_pool_h pool, sound_pool_state_e *state)
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");
238 sound_pool_error_e sound_pool_activate(sound_pool_h pool)
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");
250 sound_pool_error_e sound_pool_deactivate(sound_pool_h pool)
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");
262 sound_pool_error_e sound_pool_set_volume(sound_pool_h pool, float volume)
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");
274 sound_pool_error_e sound_pool_get_volume(sound_pool_h pool, float *volume)
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");
286 sound_pool_error_e sound_pool_stream_get_state(sound_pool_h pool, unsigned id,
287 sound_pool_stream_state_e *state)
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);
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);
306 sound_pool_error_e sound_pool_stream_set_loop(sound_pool_h pool, unsigned id,
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);
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);
326 sound_pool_error_e sound_pool_stream_get_loop(sound_pool_h pool, unsigned id,
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);
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);
346 sound_pool_error_e sound_pool_stream_set_volume(sound_pool_h pool, unsigned id,
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);
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);
366 sound_pool_error_e sound_pool_stream_get_volume(sound_pool_h pool, unsigned id,
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);
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);
386 sound_pool_error_e sound_pool_stream_set_priority(sound_pool_h pool,
387 unsigned id, unsigned priority)
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);
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);
406 sound_pool_error_e sound_pool_stream_get_priority(sound_pool_h pool,
407 unsigned id, unsigned *priority)
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);
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);
426 sound_pool_error_e sound_pool_set_state_change_callback(sound_pool_h pool,
427 sound_pool_state_change_cb callback, void *data)
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");
439 sound_pool_error_e sound_pool_unset_state_change_callback(sound_pool_h pool)
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");
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)
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);
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);
472 sound_pool_error_e sound_pool_stream_unset_state_change_callback(
473 sound_pool_h pool, unsigned id)
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);
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);