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"
28 #include "internal/priority.h"
32 * Public Implementation
35 static unsigned int sound_pool_amount = 0;
37 /*------------------- Pool related APIs -------------------------*/
39 sound_pool_error_e sound_pool_create(sound_pool_h *pool)
42 SP_RETVM_IF(SOUND_POOL_MAX_POOLS_AMOUNT < (sound_pool_amount + 1),
43 SOUND_POOL_ERROR_INVALID_OPERATION,
44 "Max pools count should be no more than %d.",
45 SOUND_POOL_MAX_POOLS_AMOUNT);
46 SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
48 sound_pool_t *_pool = NULL;
49 sound_pool_error_e ret;
51 _sound_pool_disable_session_backward_compatibility();
53 ret = _sound_pool_create(&_pool);
54 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret,
55 "Error while creating sound pool instance.");
56 SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_OPERATION);
60 SP_INFO("Number of created pools is [%u]", sound_pool_amount);
66 sound_pool_error_e sound_pool_destroy(sound_pool_h pool)
70 sound_pool_error_e ret = _sound_pool_destroy((sound_pool_t *)pool);
71 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret,
72 "Error occurred when destroying the sound pool");
74 SP_INFO("Number of pools is [%u]", sound_pool_amount);
80 sound_pool_error_e sound_pool_load_source_from_file(sound_pool_h pool,
81 const char *file_name, const char *tag)
85 sound_pool_t *_pool = (sound_pool_t *)pool;
86 sound_source_t *_src = NULL;
87 sound_pool_error_e ret = _sound_source_create(_pool, tag, &_src);
88 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret,
89 "Error while creating sound source instance.");
91 ret = _sound_source_load_from_file(_src, file_name);
92 if (ret != SOUND_POOL_ERROR_NONE) {
93 SP_ERROR("Error occurred when loading sound source from file");
94 if (_sound_source_destroy(_src) != SOUND_POOL_ERROR_NONE)
95 SP_ERROR("Error occurred during removal of sound source[%s].", tag);
102 sound_pool_error_e sound_pool_unload_source(sound_pool_h pool, const char *tag)
106 sound_pool_t *_pool = (sound_pool_t *)pool;
107 sound_source_t *_source = NULL;
108 sound_pool_error_e ret = _sound_pool_get_source_by_tag(_pool, tag, &_source);
109 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
110 "getting sound source [%s] from the sound pool", tag);
112 ret = _sound_source_destroy(_source);
113 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
114 "destroying sound source [%s]", tag);
120 sound_pool_error_e sound_pool_activate(sound_pool_h pool)
124 sound_pool_error_e ret = _sound_pool_activate((sound_pool_t *)pool);
125 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
126 "activating sound pool");
132 sound_pool_error_e sound_pool_deactivate(sound_pool_h pool)
136 sound_pool_error_e ret = _sound_pool_deactivate((sound_pool_t *)pool);
137 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
138 "deactivating sound pool");
144 sound_pool_error_e sound_pool_set_volume(sound_pool_h pool, float volume)
148 sound_pool_error_e ret = _sound_pool_set_volume((sound_pool_t *)pool, volume);
149 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
150 "setting volume for sound pool");
156 sound_pool_error_e sound_pool_get_volume(sound_pool_h pool, float *volume)
160 sound_pool_error_e ret = _sound_pool_get_volume((sound_pool_t *)pool, volume);
161 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
162 "getting volume for sound pool");
168 sound_pool_error_e sound_pool_get_state(sound_pool_h pool, sound_pool_state_e *state)
172 sound_pool_error_e ret = _sound_pool_get_state(pool, state);
173 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
174 "getting state of the sound pool");
180 sound_pool_error_e sound_pool_set_state_changed_cb(sound_pool_h pool,
181 sound_pool_state_changed_cb callback, void *data)
185 sound_pool_error_e ret = _sound_pool_set_callback((sound_pool_t *)pool, callback, data);
186 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
187 "setting state changing callback for sound pool");
193 sound_pool_error_e sound_pool_unset_state_changed_cb(sound_pool_h pool)
197 sound_pool_error_e ret = _sound_pool_unset_callback((sound_pool_t *)pool);
198 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
199 "unsetting state changing callback for sound pool");
205 /*------------------ Stream related APIs -------------------------*/
207 sound_pool_error_e sound_pool_stream_play(sound_pool_h pool, const char *tag,
208 unsigned loop, float volume, unsigned priority,
209 sound_pool_stream_priority_policy_e priority_policy,
210 sound_pool_stream_state_changed_cb callback, void *data, unsigned *id)
213 SP_INST_CHECK(id, SOUND_POOL_ERROR_INVALID_PARAMETER);
215 sound_pool_t *_pool = (sound_pool_t *)pool;
216 sound_source_t *_source = NULL;
217 sound_pool_error_e ret = _sound_pool_get_source_by_tag(_pool, tag, &_source);
218 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
219 "getting sound source [%s] from the sound pool", tag);
221 sound_stream_t *_stream = NULL;
222 ret = _sound_stream_create(_source, loop, volume, priority, priority_policy,
223 callback, data, &_stream);
224 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret,
225 "Error while creating sound source instance.");
233 sound_pool_error_e sound_pool_stream_pause(sound_pool_h pool, unsigned id)
237 sound_pool_t *_pool = (sound_pool_t *)pool;
238 sound_stream_t *_stream = NULL;
239 sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
240 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
241 "getting sound stream [%u] from the sound pool", id);
243 ret = _sound_stream_pause(_stream);
244 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
245 "pausing sound stream [%u]", id);
247 _sound_stream_priority_update_playback(_pool->mgr_priority);
253 sound_pool_error_e sound_pool_stream_resume(sound_pool_h pool, unsigned id)
256 sound_pool_t *_pool = (sound_pool_t *)pool;
257 sound_stream_t *_stream = NULL;
258 sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
259 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
260 "getting sound stream [%u] from the sound pool", id);
262 ret = _sound_stream_resume(_stream);
263 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
264 "resuming sound stream [%u]", id);
266 _sound_stream_priority_update_playback(_pool->mgr_priority);
272 sound_pool_error_e sound_pool_stream_stop(sound_pool_h pool, unsigned id)
276 sound_pool_t *_pool = (sound_pool_t *)pool;
277 sound_stream_t *_stream = NULL;
278 sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
279 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
280 "getting sound stream [%u] from the sound pool", id);
282 ret = _sound_stream_stop(_stream);
283 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
284 "stopping sound stream [%u]", id);
290 sound_pool_error_e sound_pool_stream_set_volume(sound_pool_h pool, unsigned id,
295 sound_pool_t *_pool = (sound_pool_t *)pool;
296 sound_stream_t *_stream = NULL;
297 sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
298 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
299 "getting sound stream [%u] from the sound pool", id);
301 ret = _sound_stream_set_volume(_stream, volume);
302 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
303 "setting sound stream [%u] volume parameter", id);
309 sound_pool_error_e sound_pool_stream_get_volume(sound_pool_h pool, unsigned id,
314 sound_pool_t *_pool = (sound_pool_t *)pool;
315 sound_stream_t *_stream = NULL;
316 sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
317 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
318 "getting sound stream [%u] from the sound pool", id);
320 ret = _sound_stream_get_volume(_stream, volume);
321 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
322 "getting sound stream [%u] volume parameter", id);
328 sound_pool_error_e sound_pool_stream_set_priority(sound_pool_h pool,
329 unsigned id, unsigned priority)
333 sound_pool_t *_pool = (sound_pool_t *)pool;
334 sound_stream_t *_stream = NULL;
335 sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
336 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
337 "getting sound stream [%u] from the sound pool", id);
339 ret = _sound_stream_set_priority(_stream, priority);
340 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
341 "setting sound stream [%u] priority parameter", id);
347 sound_pool_error_e sound_pool_stream_get_priority(sound_pool_h pool,
348 unsigned id, unsigned *priority)
352 sound_pool_t *_pool = (sound_pool_t *)pool;
353 sound_stream_t *_stream = NULL;
354 sound_pool_error_e ret = _sound_pool_get_stream_by_id(_pool, id, &_stream);
355 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
356 "getting sound stream [%u] from the sound pool", id);
358 ret = _sound_stream_get_priority(_stream, priority);
359 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
360 "getting sound stream [%u] priority parameter", id);
366 sound_pool_error_e sound_pool_stream_get_state(sound_pool_h pool, unsigned id,
367 sound_pool_stream_state_e *state)
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);
377 ret = _sound_stream_get_state(_stream, state);
378 SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
379 "getting sound stream [%u] state", id);