Disable session backward compatibility with soundpool
[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 #include "internal/priority.h"
29
30
31 /*
32 * Public Implementation
33 */
34
35 static unsigned int sound_pool_amount = 0;
36
37 /*------------------- Pool related APIs -------------------------*/
38
39 sound_pool_error_e sound_pool_create(sound_pool_h *pool)
40 {
41         SP_DEBUG_FENTER();
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);
47
48         sound_pool_t *_pool = NULL;
49         sound_pool_error_e ret;
50
51         _sound_pool_disable_session_backward_compatibility();
52
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);
57
58         *pool = _pool;
59         ++sound_pool_amount;
60         SP_INFO("Number of created pools is [%u]", sound_pool_amount);
61
62         SP_DEBUG_FLEAVE();
63         return ret;
64 }
65
66 sound_pool_error_e sound_pool_destroy(sound_pool_h pool)
67 {
68         SP_DEBUG_FENTER();
69
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");
73         --sound_pool_amount;
74         SP_INFO("Number of pools is [%u]", sound_pool_amount);
75
76         SP_DEBUG_FLEAVE();
77         return ret;
78 }
79
80 sound_pool_error_e sound_pool_load_source_from_file(sound_pool_h pool,
81                 const char *file_name, const char *tag)
82 {
83         SP_DEBUG_FENTER();
84
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.");
90
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);
96         }
97
98         SP_DEBUG_FLEAVE();
99         return ret;
100 }
101
102 sound_pool_error_e sound_pool_unload_source(sound_pool_h pool, const char *tag)
103 {
104         SP_DEBUG_FENTER();
105
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);
111
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);
115
116         SP_DEBUG_FLEAVE();
117         return ret;
118 }
119
120 sound_pool_error_e sound_pool_activate(sound_pool_h pool)
121 {
122         SP_DEBUG_FENTER();
123
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");
127
128         SP_DEBUG_FLEAVE();
129         return ret;
130 }
131
132 sound_pool_error_e sound_pool_deactivate(sound_pool_h pool)
133 {
134         SP_DEBUG_FENTER();
135
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");
139
140         SP_DEBUG_FLEAVE();
141         return ret;
142 }
143
144 sound_pool_error_e sound_pool_set_volume(sound_pool_h pool, float volume)
145 {
146         SP_DEBUG_FENTER();
147
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");
151
152         SP_DEBUG_FLEAVE();
153         return ret;
154 }
155
156 sound_pool_error_e sound_pool_get_volume(sound_pool_h pool, float *volume)
157 {
158         SP_DEBUG_FENTER();
159
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");
163
164         SP_DEBUG_FLEAVE();
165         return ret;
166 }
167
168 sound_pool_error_e sound_pool_get_state(sound_pool_h pool, sound_pool_state_e *state)
169 {
170         SP_DEBUG_FENTER();
171
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");
175
176         SP_DEBUG_FLEAVE();
177         return ret;
178 }
179
180 sound_pool_error_e sound_pool_set_state_changed_cb(sound_pool_h pool,
181                 sound_pool_state_changed_cb callback, void *data)
182 {
183         SP_DEBUG_FENTER();
184
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");
188
189         SP_DEBUG_FLEAVE();
190         return ret;
191 }
192
193 sound_pool_error_e sound_pool_unset_state_changed_cb(sound_pool_h pool)
194 {
195         SP_DEBUG_FENTER();
196
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");
200
201         SP_DEBUG_FLEAVE();
202         return ret;
203 }
204
205 /*------------------ Stream related APIs -------------------------*/
206
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)
211 {
212         SP_DEBUG_FENTER();
213         SP_INST_CHECK(id, SOUND_POOL_ERROR_INVALID_PARAMETER);
214
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);
220
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.");
226
227         *id = _stream->id;
228
229         SP_DEBUG_FLEAVE();
230         return ret;
231 }
232
233 sound_pool_error_e sound_pool_stream_pause(sound_pool_h pool, unsigned id)
234 {
235         SP_DEBUG_FENTER();
236
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);
242
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);
246
247         _sound_stream_priority_update_playback(_pool->mgr_priority);
248
249         SP_DEBUG_FLEAVE();
250         return ret;
251 }
252
253 sound_pool_error_e sound_pool_stream_resume(sound_pool_h pool, unsigned id)
254 {
255         SP_DEBUG_FENTER();
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);
261
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);
265
266         _sound_stream_priority_update_playback(_pool->mgr_priority);
267
268         SP_DEBUG_FLEAVE();
269         return ret;
270 }
271
272 sound_pool_error_e sound_pool_stream_stop(sound_pool_h pool, unsigned id)
273 {
274         SP_DEBUG_FENTER();
275
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);
281
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);
285
286         SP_DEBUG_FLEAVE();
287         return ret;
288 }
289
290 sound_pool_error_e sound_pool_stream_set_volume(sound_pool_h pool, unsigned id,
291                 float volume)
292 {
293         SP_DEBUG_FENTER();
294
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);
300
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);
304
305         SP_DEBUG_FLEAVE();
306         return ret;
307 }
308
309 sound_pool_error_e sound_pool_stream_get_volume(sound_pool_h pool, unsigned id,
310                 float *volume)
311 {
312         SP_DEBUG_FENTER();
313
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);
319
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);
323
324         SP_DEBUG_FLEAVE();
325         return ret;
326 }
327
328 sound_pool_error_e sound_pool_stream_set_priority(sound_pool_h pool,
329                 unsigned id, unsigned priority)
330 {
331         SP_DEBUG_FENTER();
332
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);
338
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);
342
343         SP_DEBUG_FLEAVE();
344         return ret;
345 }
346
347 sound_pool_error_e sound_pool_stream_get_priority(sound_pool_h pool,
348                 unsigned id, unsigned *priority)
349 {
350         SP_DEBUG_FENTER();
351
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);
357
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);
361
362         SP_DEBUG_FLEAVE();
363         return ret;
364 }
365
366 sound_pool_error_e sound_pool_stream_get_state(sound_pool_h pool, unsigned id,
367                 sound_pool_stream_state_e *state)
368 {
369         SP_DEBUG_FENTER();
370
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
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);
380
381         SP_DEBUG_FLEAVE();
382         return ret;
383 }