453e847f256085a3881f10c982afe8695b1e28ce
[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         {
351                 stream->state_previous = stream->state;
352                 stream->state = SOUND_POOL_STREAM_STATE_SUSPENDED;
353                 ret = _stream_cb_manager_register_event(pool->cbmgr, stream);
354                 SP_RETVM_IF(SOUND_POOL_ERROR_NONE != ret, ret, "State changing event "
355                                 "wasn't registered. Callbacks will be not called");
356                 SP_INFO("Don't resumed due to SoundPool is in inactive state.");
357         } else {
358                 alSourcePlay(stream->al_source);
359         }
360
361         SP_DEBUG_FLEAVE();
362         return ret;
363 }
364
365 sound_pool_error_e _sound_stream_stop(sound_stream_t *stream)
366 {
367         SP_DEBUG_FENTER();
368         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
369         SP_INST_CHECK(stream->parent_source, SOUND_POOL_ERROR_INVALID_PARAMETER);
370         sound_pool_t *pool = stream->parent_source->parent_pool;
371         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
372         SP_RETVM_IF(stream->state != SOUND_POOL_STREAM_STATE_PLAYING &&
373                         stream->state != SOUND_POOL_STREAM_STATE_SUSPENDED &&
374                         stream->state != SOUND_POOL_STREAM_STATE_PAUSED,
375                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't stop stream in [%s] "
376                         "state", __stringify_stream_state(stream->state));
377
378         SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
379                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
380
381         stream->stopped = TRUE;
382         alSourceStop(stream->al_source);
383
384         SP_DEBUG_FLEAVE();
385         return SOUND_POOL_ERROR_NONE;
386 }
387
388 sound_pool_error_e _sound_stream_set_loop(sound_stream_t *stream, unsigned loop)
389 {
390         SP_DEBUG_FENTER();
391         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
392         sound_pool_t *pool = stream->parent_source->parent_pool;
393         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
394         SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
395                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
396
397         alSourcei(stream->al_source, AL_LOOP_COUNT, (ALint)loop);
398         stream->loop = loop;
399
400         SP_DEBUG_FLEAVE();
401         return SOUND_POOL_ERROR_NONE;
402 }
403
404 sound_pool_error_e _sound_stream_get_loop(sound_stream_t *stream, unsigned *loop)
405 {
406         SP_DEBUG_FENTER();
407         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
408         SP_INST_CHECK(loop, SOUND_POOL_ERROR_INVALID_PARAMETER);
409
410         *loop = stream->loop;
411
412         SP_DEBUG_FLEAVE();
413         return SOUND_POOL_ERROR_NONE;
414 }
415
416 sound_pool_error_e _sound_stream_set_volume(sound_stream_t *stream, float volume)
417 {
418         SP_DEBUG_FENTER();
419         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
420         SP_RETVM_IF((volume < 0.0f || volume > 1.0f),
421                         SOUND_POOL_ERROR_INVALID_PARAMETER,
422                         "Volume for sound stream should be in range 0.0..1.0.");
423         sound_pool_t *pool = stream->parent_source->parent_pool;
424         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
425         SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
426                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
427
428         alSourcef(stream->al_source, AL_GAIN, volume);
429
430         stream->volume = volume;
431
432         SP_DEBUG_FLEAVE();
433         return SOUND_POOL_ERROR_NONE;
434 }
435
436 sound_pool_error_e _sound_stream_get_volume(sound_stream_t *stream, float *volume)
437 {
438         SP_DEBUG_FENTER();
439         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
440         SP_INST_CHECK(volume, SOUND_POOL_ERROR_INVALID_PARAMETER);
441
442         *volume = stream->volume;
443
444         SP_DEBUG_FLEAVE();
445         return SOUND_POOL_ERROR_NONE;
446 }
447
448 sound_pool_error_e _sound_stream_get_state(sound_stream_t *stream,
449                 sound_pool_stream_state_e *state)
450 {
451         SP_DEBUG_FENTER();
452         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
453         SP_INST_CHECK(state, SOUND_POOL_ERROR_INVALID_PARAMETER);
454
455         *state = stream->state;
456
457         SP_DEBUG_FLEAVE();
458         return SOUND_POOL_ERROR_NONE;
459 }
460
461 sound_pool_error_e _sound_stream_set_priority(sound_stream_t *stream,
462                 unsigned rank)
463 {
464         SP_DEBUG_FENTER();
465         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
466         SP_INST_CHECK(stream->parent_source->parent_pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
467         SP_INST_CHECK(stream->parent_source->parent_pool->mgr_priority,
468                         SOUND_POOL_ERROR_INVALID_PARAMETER);
469         sound_pool_error_e ret = SOUND_POOL_ERROR_NONE;
470
471         stream->priority = rank;
472         if (stream->parent_source->parent_pool->state == SOUND_POOL_STATE_INACTIVE) {
473                 if (stream->state == SOUND_POOL_STREAM_STATE_NONE) {
474                         stream->state_previous = stream->state;
475                         stream->state = SOUND_POOL_STREAM_STATE_SUSPENDED;
476                         ret = _stream_cb_manager_register_event(stream->parent_source->parent_pool->cbmgr,
477                                         stream);
478                         SP_RETVM_IF(SOUND_POOL_ERROR_NONE != ret, ret, "State changing event "
479                                         "wasn't registered. Callbacks will be not called");
480                 }
481                 SP_DEBUG("No need to update priority, Sound pool is INACTIVE.");
482                 return SOUND_POOL_ERROR_NONE;
483         }
484
485         _sound_stream_priority_update_playback(stream->parent_source->parent_pool->mgr_priority);
486
487         SP_DEBUG_FLEAVE();
488         return SOUND_POOL_ERROR_NONE;
489 }
490
491 sound_pool_error_e _sound_stream_get_priority(sound_stream_t *stream,
492                 unsigned *rank)
493 {
494         SP_DEBUG_FENTER();
495         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
496         SP_INST_CHECK(rank, SOUND_POOL_ERROR_INVALID_PARAMETER);
497
498         *rank = stream->priority;
499
500         SP_DEBUG_FLEAVE();
501         return SOUND_POOL_ERROR_NONE;
502 }
503
504 sound_pool_error_e _sound_stream_set_callback(sound_stream_t *stream,
505                 sound_pool_stream_state_change_cb callback, void *data)
506 {
507         SP_DEBUG_FENTER();
508         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
509         SP_INST_CHECK(callback, SOUND_POOL_ERROR_INVALID_PARAMETER);
510
511         stream->state_cb_info.callback = callback;
512         stream->state_cb_info.user_data = data;
513
514         SP_DEBUG_FLEAVE();
515         return SOUND_POOL_ERROR_NONE;
516 }
517
518 sound_pool_error_e _sound_stream_unset_callback(sound_stream_t *stream)
519 {
520         SP_DEBUG_FENTER();
521         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
522
523         stream->state_cb_info.callback = NULL;
524         stream->state_cb_info.user_data = NULL;
525
526         SP_DEBUG_FLEAVE();
527         return SOUND_POOL_ERROR_NONE;
528 }
529
530 sound_pool_error_e _sound_pool_get_stream_by_id(sound_pool_t *pool, unsigned id,
531                 sound_stream_t **stream)
532 {
533         SP_DEBUG_FENTER();
534         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
535         SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
536
537         SP_RETVM_IF(!pool->streams, SOUND_POOL_ERROR_INVALID_OPERATION, "Corrupted "
538                         "sound pool. Sources hash table is NULL");
539
540         *stream = (sound_stream_t *)g_hash_table_lookup(pool->streams, &id);
541         SP_RETVM_IF(!(*stream), SOUND_POOL_ERROR_KEY_NOT_AVAILABLE, "Tag doesn't "
542                         "exist in sources hash table");
543
544         SP_DEBUG_FLEAVE();
545         return SOUND_POOL_ERROR_NONE;
546 }