Fix for coding rule
[platform/core/api/sound-pool.git] / src / stream.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 stream.c
19  * @brief This file include implementation of protected API
20  * for the sound streams(OpenAL sources) as part of SoundPool.
21  */
22
23 #include "internal/stream.h"
24 #include "internal/priority.h"
25
26 #include "internal/stream_cb_manager.h"
27
28 #define DEFAULT_STREAM_PRIORITY 255U
29 #define DEFAULT_STREAM_LOOP     1U
30 #define DEFAULT_STREAM_VOLUME   1.0f
31
32 static const char *__stringify_stream_state(sound_pool_stream_state_e state);
33 static void __al_source_state_cb(ALuint source, ALenum state, ALvoid *data);
34 static sound_pool_error_e __sound_pool_add_stream(sound_pool_t *pool, sound_stream_t *stream);
35 static sound_pool_error_e __sound_pool_remove_stream(sound_pool_t *pool, sound_stream_t *stream);
36
37 static const char *__stringify_stream_state(sound_pool_stream_state_e state)
38 {
39         switch (state) {
40         case SOUND_POOL_STREAM_STATE_NONE:
41                 return "SOUND_POOL_STREAM_STATE_NONE";
42         case SOUND_POOL_STREAM_STATE_PLAYING:
43                 return "SOUND_POOL_STREAM_STATE_PLAYING";
44         case SOUND_POOL_STREAM_STATE_PAUSED:
45                 return "SOUND_POOL_STREAM_STATE_PAUSED";
46         case SOUND_POOL_STREAM_STATE_SUSPENDED:
47                 return "SOUND_POOL_STREAM_STATE_SUSPENDED";
48         case SOUND_POOL_STREAM_STATE_STOPPED:
49                 return "SOUND_POOL_STREAM_STATE_STOPPED";
50         case SOUND_POOL_STREAM_STATE_FINISHED:
51                 return "SOUND_POOL_STREAM_STATE_FINISHED";
52         default:
53                 return "";
54         }
55 }
56
57 static void __al_source_state_cb(ALuint source, ALenum state, ALvoid *data)
58 {
59         SP_DEBUG_FENTER();
60
61         sound_stream_t *stream = (sound_stream_t *)data;
62         SP_RETM_IF(!stream, "Can't handle stream in OpenAL callback, it is NULL");
63         sound_pool_t *pool = stream->parent_source->parent_pool;
64         SP_RETM_IF(!pool, "Can't handle stream in OpenAL callback, stream's sound "
65                         "pool is NULL");
66
67         stream->state_previous = stream->state;
68
69         switch (state) {
70         case AL_INITIAL:
71                 stream->state = SOUND_POOL_STREAM_STATE_NONE;
72                 break;
73         case AL_PLAYING:
74                 stream->state = SOUND_POOL_STREAM_STATE_PLAYING;
75                 break;
76         case AL_PAUSED:
77                 if (pool->state == SOUND_POOL_STATE_INACTIVE)
78                         stream->state = SOUND_POOL_STREAM_STATE_SUSPENDED;
79                 else if (pool->state == SOUND_POOL_STATE_ACTIVE)
80                         stream->state = SOUND_POOL_STREAM_STATE_PAUSED;
81                 break;
82         case AL_STOPPED:
83                 if (stream->stopped)
84                         stream->state = SOUND_POOL_STREAM_STATE_STOPPED;
85                 else
86                         stream->state = SOUND_POOL_STREAM_STATE_FINISHED;
87                 break;
88         default:
89                 SP_ERROR("Invalid OpenAl state value [%#04x]", state);
90                 return;
91         }
92
93         SP_INFO("Stream [%s:%d] state changed from [%s] to [%s]",
94                         stream->parent_source->tag_name, stream->id,
95                         __stringify_stream_state(stream->state_previous),
96                         __stringify_stream_state(stream->state));
97
98         SP_RETM_IF(_stream_cb_manager_register_event(pool->cbmgr, stream) !=
99                         SOUND_POOL_ERROR_NONE, "State changing event wasn't registered."
100                         "Callbacks will be not called");
101
102         SP_DEBUG_FLEAVE();
103 }
104
105 static sound_pool_error_e __sound_pool_add_stream(sound_pool_t *pool,
106                 sound_stream_t *stream)
107 {
108         SP_DEBUG_FENTER();
109         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
110         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
111         SP_RETVM_IF(!pool->streams, SOUND_POOL_ERROR_INVALID_OPERATION, "Sound "
112                         "pool to add the stream is corrupted: NULL streams hash table");
113         stream->id = ++(pool->max_stream_index);
114         SP_DEBUG("Id assigned to the stream is [%u]", stream->id);
115         SP_RETVM_IF(g_hash_table_contains(pool->streams, (gpointer)&stream->id),
116                         SOUND_POOL_ERROR_INVALID_PARAMETER,
117                         "Stream ID already exists in streams hash table.");
118         SP_RETVM_IF(stream->parent_source->parent_pool != pool,
119                         SOUND_POOL_ERROR_INVALID_OPERATION, "Sound stream can't be added "
120                         "to the pool different to the pool for which sound stream was created");
121
122         SP_RETVM_IF(!g_hash_table_insert(pool->streams, (gpointer)&stream->id, stream),
123                         SOUND_POOL_ERROR_INVALID_OPERATION, "Error occurred when adding "
124                         "the stream [%u] to the sound pool", stream->id);
125
126         sound_pool_error_e ret =
127                         _sound_stream_priority_add_stream(pool->mgr_priority, stream);
128         SP_RETVM_IF(SOUND_POOL_ERROR_NONE != ret, ret, "Error occurred when adding "
129                         "the stream [%u] to the priority queue.", stream->id);
130
131         SP_DEBUG_FLEAVE();
132         return SOUND_POOL_ERROR_NONE;
133 }
134
135 static sound_pool_error_e __sound_pool_remove_stream(sound_pool_t *pool, sound_stream_t *stream)
136 {
137         SP_DEBUG_FENTER();
138         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
139         SP_INST_CHECK(pool->mgr_priority, SOUND_POOL_ERROR_INVALID_PARAMETER);
140         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
141         SP_INST_CHECK(stream->parent_source, SOUND_POOL_ERROR_INVALID_PARAMETER);
142
143         if (stream->parent_source->parent_pool == pool) {
144                 if (!g_hash_table_remove(pool->streams, &stream->id)
145                                 || SOUND_POOL_ERROR_NONE
146                                                 != _sound_stream_priority_remove_stream(pool->mgr_priority,
147                                                                 stream))
148                         SP_DEBUG("Id [%u] doesn't exist in streams hash table", stream->id);
149                 else
150                         SP_INFO("[%u] sound stream was removed from pool", stream->id);
151         } else {
152                 SP_DEBUG("Stream wasn't removed from sound pool as it isn't known by "
153                                 "the pool for which operation is performed");
154         }
155
156         SP_DEBUG_FLEAVE();
157         return SOUND_POOL_ERROR_NONE;
158 }
159
160 sound_pool_error_e _sound_stream_create(sound_source_t *src,
161                 sound_stream_t **stream)
162 {
163         SP_DEBUG_FENTER();
164         SP_RETVM_IF(!src, SOUND_POOL_ERROR_INVALID_PARAMETER,
165                         "Can't create sound stream in NULL sound source");
166         SP_RETVM_IF(!stream, SOUND_POOL_ERROR_INVALID_PARAMETER,
167                         "Can't create sound stream. Stream pointer is NULL");
168         SP_RETVM_IF(!src->parent_pool, SOUND_POOL_ERROR_INVALID_PARAMETER,
169                         "Can't create sound stream. Source's pool is NULL");
170
171         SP_RETVM_IF(!alcMakeContextCurrent(src->parent_pool->al_context),
172                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
173
174         sound_stream_t *_stream = NULL;
175         SP_RETVM_IF(!(_stream = g_try_malloc0(sizeof(*_stream))),
176                         SOUND_POOL_ERROR_OUT_OF_MEMORY,
177                         "Memory alloc failure. Can't create sound stream");
178
179         _stream->parent_source = src;
180         _stream->priority = DEFAULT_STREAM_PRIORITY;
181         _stream->loop = DEFAULT_STREAM_LOOP;
182         _stream->volume = DEFAULT_STREAM_VOLUME;
183         _stream->state_previous = SOUND_POOL_STREAM_STATE_NONE;
184         _stream->state = SOUND_POOL_STREAM_STATE_NONE;
185         _stream->stopped = FALSE;
186         _stream->state_cb_info.callback = NULL;
187         _stream->state_cb_info.user_data = NULL;
188
189         sound_pool_error_e ret = SOUND_POOL_ERROR_NONE;
190         alGenSources((ALsizei)1, &_stream->al_source);
191         if (alGetError() != AL_NO_ERROR) {
192                 ret = SOUND_POOL_ERROR_OUT_OF_MEMORY;
193                 GOTO_FAIL("OpenAL error occurred when trying to generate OpenAL Source",
194                                 cfail);
195         }
196
197         alSourcei(_stream->al_source, AL_BUFFER, src->al_buffer);
198         if (alGetError() != AL_NO_ERROR) {
199                 ret = SOUND_POOL_ERROR_INVALID_OPERATION;
200                 GOTO_FAIL("OpenAL error occurred when trying to assign OpenAL Buffer "
201                                 "to OpenAL Source", cfail);
202         }
203
204         alSourcep(_stream->al_source, AL_SOURCE_STATE_CALLBACK,
205                         (ALvoid *)__al_source_state_cb);
206         if (alGetError() != AL_NO_ERROR) {
207                 ret = SOUND_POOL_ERROR_INVALID_OPERATION;
208                 GOTO_FAIL("OpenAL error occurred when trying to assign OpenAL Callback "
209                                 "to OpenAL Source", cfail);
210         }
211
212         alSourcep(_stream->al_source, AL_SOURCE_STATE_CALLBACK_DATA, (ALvoid *)_stream);
213         if (alGetError() != AL_NO_ERROR) {
214                 ret = SOUND_POOL_ERROR_INVALID_OPERATION;
215                 GOTO_FAIL("OpenAL error occurred when trying to assign OpenAL Callback "
216                                 "Data to OpenAL Source", cfail);
217         }
218
219         ret = __sound_pool_add_stream(src->parent_pool, _stream);
220         if (ret != SOUND_POOL_ERROR_NONE)
221                 GOTO_FAIL("Error occurred when trying to add source to pool", cfail);
222
223         *stream = _stream;
224         SP_DEBUG_FLEAVE();
225         return ret;
226
227 cfail:
228         _sound_stream_destroy(_stream);
229         SP_DEBUG_FLEAVE();
230         return ret;
231 }
232
233 sound_pool_error_e _sound_stream_destroy(sound_stream_t *stream)
234 {
235         SP_DEBUG_FENTER();
236         SP_RETVM_IF(!stream, SOUND_POOL_ERROR_INVALID_PARAMETER,
237                         "Can't destroy NULL sound stream");
238         SP_RETVM_IF(!stream->parent_source->parent_pool, SOUND_POOL_ERROR_INVALID_PARAMETER,
239                         "Empty parent pool pointer.");
240
241         if (stream->parent_source) {
242                 if (__sound_pool_remove_stream(stream->parent_source->parent_pool,
243                                 stream) != SOUND_POOL_ERROR_NONE)
244                         SP_DEBUG("Stream[%s] wasn't removed from sound pool. Continue destroying",
245                                         stream->parent_source->tag_name);
246                 if (alcMakeContextCurrent(stream->parent_source->parent_pool->al_context)) {
247                         alSourcei(stream->al_source, AL_BUFFER, 0);
248                         alDeleteSources(1, &stream->al_source);
249                         SP_DEBUG("Deleting OpenAL source with id [%u]", stream->al_source);
250                         if (alGetError() != AL_NO_ERROR)
251                                 SP_ERROR("OpenAL error while stream[%d] destroying.", stream->id);
252                 } else {
253                         SP_DEBUG("Can't set current context for deleting OpenAL source with id [%u]",
254                                         stream->al_source);
255                 }
256         }
257
258         SP_SAFE_GFREE(stream);
259
260         SP_DEBUG_FLEAVE();
261         return SOUND_POOL_ERROR_NONE;
262 }
263
264 sound_pool_error_e _sound_stream_play(sound_stream_t *stream)
265 {
266         SP_DEBUG_FENTER();
267         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
268         SP_INST_CHECK(stream->parent_source, SOUND_POOL_ERROR_INVALID_PARAMETER);
269         sound_pool_t *pool = stream->parent_source->parent_pool;
270         sound_pool_error_e ret = SOUND_POOL_ERROR_NONE;
271         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
272         SP_RETVM_IF(stream->state != SOUND_POOL_STREAM_STATE_SUSPENDED &&
273                         stream->state != SOUND_POOL_STREAM_STATE_PAUSED &&
274                         stream->state != SOUND_POOL_STREAM_STATE_NONE,
275                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't play stream[%s] in [%s] "
276                         "state", stream->parent_source->tag_name, __stringify_stream_state(stream->state));
277         SP_RETVM_IF(stream->state == SOUND_POOL_STREAM_STATE_SUSPENDED &&
278                         pool->state == SOUND_POOL_STATE_INACTIVE,
279                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't play suspended stream "
280                         "in inactive sound pool");
281
282         SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
283                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
284
285         if (pool->state == SOUND_POOL_STATE_INACTIVE &&
286                         stream->state == SOUND_POOL_STREAM_STATE_NONE) {
287                 stream->state_previous = stream->state;
288                 stream->state = SOUND_POOL_STREAM_STATE_SUSPENDED;
289                 ret = _stream_cb_manager_register_event(pool->cbmgr, stream);
290                 SP_RETVM_IF(SOUND_POOL_ERROR_NONE != ret, ret, "State changing event "
291                                 "wasn't registered. Callbacks will be not called");
292                 SP_INFO("Don't play due to SoundPool is in inactive state.");
293         } else {
294                 alSourcePlay(stream->al_source);
295         }
296
297         SP_DEBUG_FLEAVE();
298         return ret;
299 }
300
301 sound_pool_error_e _sound_stream_pause(sound_stream_t *stream)
302 {
303         SP_DEBUG_FENTER();
304         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
305         SP_INST_CHECK(stream->parent_source, SOUND_POOL_ERROR_INVALID_PARAMETER);
306         sound_pool_t *pool = stream->parent_source->parent_pool;
307         sound_pool_error_e ret = SOUND_POOL_ERROR_NONE;
308         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
309         SP_RETVM_IF(stream->state != SOUND_POOL_STREAM_STATE_SUSPENDED &&
310                         stream->state != SOUND_POOL_STREAM_STATE_PLAYING,
311                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't pause stream in [%s] "
312                         "state", __stringify_stream_state(stream->state));
313
314         SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
315                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
316
317         if (pool->state == SOUND_POOL_STATE_INACTIVE &&
318                         stream->state == SOUND_POOL_STREAM_STATE_SUSPENDED) {
319                 stream->state_previous = stream->state;
320                 stream->state = SOUND_POOL_STREAM_STATE_PAUSED;
321                 ret = _stream_cb_manager_register_event(pool->cbmgr, stream);
322                 SP_RETVM_IF(SOUND_POOL_ERROR_NONE != ret, ret, "State changing event "
323                                 "wasn't registered. Callbacks will be not called");
324                 SP_INFO("Don't paused due to SoundPool is in inactive state.");
325         } else {
326                 alSourcePause(stream->al_source);
327         }
328
329         SP_DEBUG_FLEAVE();
330         return ret;
331 }
332
333 sound_pool_error_e _sound_stream_resume(sound_stream_t *stream)
334 {
335         SP_DEBUG_FENTER();
336         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
337         SP_INST_CHECK(stream->parent_source, SOUND_POOL_ERROR_INVALID_PARAMETER);
338         sound_pool_t *pool = stream->parent_source->parent_pool;
339         sound_pool_error_e ret = SOUND_POOL_ERROR_NONE;
340         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
341         SP_RETVM_IF(stream->state != SOUND_POOL_STREAM_STATE_PAUSED,
342                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't resume stream in [%s] "
343                         "state", __stringify_stream_state(stream->state));
344
345         SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
346                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
347
348         if (pool->state == SOUND_POOL_STATE_INACTIVE &&
349                         stream->state == SOUND_POOL_STREAM_STATE_PAUSED) {
350                 stream->state_previous = stream->state;
351                 stream->state = SOUND_POOL_STREAM_STATE_SUSPENDED;
352                 ret = _stream_cb_manager_register_event(pool->cbmgr, stream);
353                 SP_RETVM_IF(SOUND_POOL_ERROR_NONE != ret, ret, "State changing event "
354                                 "wasn't registered. Callbacks will be not called");
355                 SP_INFO("Don't resumed due to SoundPool is in inactive state.");
356         } else {
357                 alSourcePlay(stream->al_source);
358         }
359
360         SP_DEBUG_FLEAVE();
361         return ret;
362 }
363
364 sound_pool_error_e _sound_stream_stop(sound_stream_t *stream)
365 {
366         SP_DEBUG_FENTER();
367         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
368         SP_INST_CHECK(stream->parent_source, SOUND_POOL_ERROR_INVALID_PARAMETER);
369         sound_pool_t *pool = stream->parent_source->parent_pool;
370         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
371         SP_RETVM_IF(stream->state != SOUND_POOL_STREAM_STATE_PLAYING &&
372                         stream->state != SOUND_POOL_STREAM_STATE_SUSPENDED &&
373                         stream->state != SOUND_POOL_STREAM_STATE_PAUSED,
374                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't stop stream in [%s] "
375                         "state", __stringify_stream_state(stream->state));
376
377         SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
378                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
379
380         stream->stopped = TRUE;
381         alSourceStop(stream->al_source);
382
383         SP_DEBUG_FLEAVE();
384         return SOUND_POOL_ERROR_NONE;
385 }
386
387 sound_pool_error_e _sound_stream_set_loop(sound_stream_t *stream, unsigned loop)
388 {
389         SP_DEBUG_FENTER();
390         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
391         sound_pool_t *pool = stream->parent_source->parent_pool;
392         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
393         SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
394                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
395
396         alSourcei(stream->al_source, AL_LOOP_COUNT, (ALint)loop);
397         stream->loop = loop;
398
399         SP_DEBUG_FLEAVE();
400         return SOUND_POOL_ERROR_NONE;
401 }
402
403 sound_pool_error_e _sound_stream_get_loop(sound_stream_t *stream, unsigned *loop)
404 {
405         SP_DEBUG_FENTER();
406         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
407         SP_INST_CHECK(loop, SOUND_POOL_ERROR_INVALID_PARAMETER);
408
409         *loop = stream->loop;
410
411         SP_DEBUG_FLEAVE();
412         return SOUND_POOL_ERROR_NONE;
413 }
414
415 sound_pool_error_e _sound_stream_set_volume(sound_stream_t *stream, float volume)
416 {
417         SP_DEBUG_FENTER();
418         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
419         SP_RETVM_IF((volume < 0.0f || volume > 1.0f),
420                         SOUND_POOL_ERROR_INVALID_PARAMETER,
421                         "Volume for sound stream should be in range 0.0..1.0.");
422         sound_pool_t *pool = stream->parent_source->parent_pool;
423         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
424         SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
425                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
426
427         alSourcef(stream->al_source, AL_GAIN, volume);
428
429         stream->volume = volume;
430
431         SP_DEBUG_FLEAVE();
432         return SOUND_POOL_ERROR_NONE;
433 }
434
435 sound_pool_error_e _sound_stream_get_volume(sound_stream_t *stream, float *volume)
436 {
437         SP_DEBUG_FENTER();
438         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
439         SP_INST_CHECK(volume, SOUND_POOL_ERROR_INVALID_PARAMETER);
440
441         *volume = stream->volume;
442
443         SP_DEBUG_FLEAVE();
444         return SOUND_POOL_ERROR_NONE;
445 }
446
447 sound_pool_error_e _sound_stream_get_state(sound_stream_t *stream,
448                 sound_pool_stream_state_e *state)
449 {
450         SP_DEBUG_FENTER();
451         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
452         SP_INST_CHECK(state, SOUND_POOL_ERROR_INVALID_PARAMETER);
453
454         *state = stream->state;
455
456         SP_DEBUG_FLEAVE();
457         return SOUND_POOL_ERROR_NONE;
458 }
459
460 sound_pool_error_e _sound_stream_set_priority(sound_stream_t *stream,
461                 unsigned rank)
462 {
463         SP_DEBUG_FENTER();
464         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
465         SP_INST_CHECK(stream->parent_source->parent_pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
466         SP_INST_CHECK(stream->parent_source->parent_pool->mgr_priority,
467                         SOUND_POOL_ERROR_INVALID_PARAMETER);
468         sound_pool_error_e ret = SOUND_POOL_ERROR_NONE;
469
470         stream->priority = rank;
471         if (stream->parent_source->parent_pool->state == SOUND_POOL_STATE_INACTIVE) {
472                 if (stream->state == SOUND_POOL_STREAM_STATE_NONE) {
473                         stream->state_previous = stream->state;
474                         stream->state = SOUND_POOL_STREAM_STATE_SUSPENDED;
475                         ret = _stream_cb_manager_register_event(stream->parent_source->parent_pool->cbmgr,
476                                         stream);
477                         SP_RETVM_IF(SOUND_POOL_ERROR_NONE != ret, ret, "State changing event "
478                                         "wasn't registered. Callbacks will be not called");
479                 }
480                 SP_DEBUG("No need to update priority, Sound pool is INACTIVE.");
481                 return SOUND_POOL_ERROR_NONE;
482         }
483
484         _sound_stream_priority_update_playback(stream->parent_source->parent_pool->mgr_priority);
485
486         SP_DEBUG_FLEAVE();
487         return SOUND_POOL_ERROR_NONE;
488 }
489
490 sound_pool_error_e _sound_stream_get_priority(sound_stream_t *stream,
491                 unsigned *rank)
492 {
493         SP_DEBUG_FENTER();
494         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
495         SP_INST_CHECK(rank, SOUND_POOL_ERROR_INVALID_PARAMETER);
496
497         *rank = stream->priority;
498
499         SP_DEBUG_FLEAVE();
500         return SOUND_POOL_ERROR_NONE;
501 }
502
503 sound_pool_error_e _sound_stream_set_callback(sound_stream_t *stream,
504                 sound_pool_stream_state_change_cb callback, void *data)
505 {
506         SP_DEBUG_FENTER();
507         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
508         SP_INST_CHECK(callback, SOUND_POOL_ERROR_INVALID_PARAMETER);
509
510         stream->state_cb_info.callback = callback;
511         stream->state_cb_info.user_data = data;
512
513         SP_DEBUG_FLEAVE();
514         return SOUND_POOL_ERROR_NONE;
515 }
516
517 sound_pool_error_e _sound_stream_unset_callback(sound_stream_t *stream)
518 {
519         SP_DEBUG_FENTER();
520         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
521
522         stream->state_cb_info.callback = NULL;
523         stream->state_cb_info.user_data = NULL;
524
525         SP_DEBUG_FLEAVE();
526         return SOUND_POOL_ERROR_NONE;
527 }
528
529 sound_pool_error_e _sound_pool_get_stream_by_id(sound_pool_t *pool, unsigned id,
530                 sound_stream_t **stream)
531 {
532         SP_DEBUG_FENTER();
533         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
534         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
535
536         SP_RETVM_IF(!pool->streams, SOUND_POOL_ERROR_INVALID_OPERATION, "Corrupted "
537                         "sound pool. Sources hash table is NULL");
538
539         *stream = (sound_stream_t *)g_hash_table_lookup(pool->streams, &id);
540         SP_RETVM_IF(!(*stream), SOUND_POOL_ERROR_KEY_NOT_AVAILABLE, "Tag doesn't "
541                         "exist in sources hash table");
542
543         SP_DEBUG_FLEAVE();
544         return SOUND_POOL_ERROR_NONE;
545 }