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 protected API
20 * for the sound streams(OpenAL sources) as part of SoundPool.
23 #include "internal/stream.h"
24 #include "internal/priority.h"
26 #include "internal/stream_cb_manager.h"
28 #define DEFAULT_STREAM_PRIORITY 255U
29 #define DEFAULT_STREAM_LOOP 1U
30 #define DEFAULT_STREAM_VOLUME 1.0f
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);
37 static const char *__stringify_stream_state(sound_pool_stream_state_e 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";
57 static void __al_source_state_cb(ALuint source, ALenum state, ALvoid *data)
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 "
67 stream->state_previous = stream->state;
71 stream->state = SOUND_POOL_STREAM_STATE_NONE;
74 stream->state = SOUND_POOL_STREAM_STATE_PLAYING;
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;
84 stream->state = SOUND_POOL_STREAM_STATE_STOPPED;
86 stream->state = SOUND_POOL_STREAM_STATE_FINISHED;
89 SP_ERROR("Invalid OpenAl state value [%#04x]", state);
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));
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");
105 static sound_pool_error_e __sound_pool_add_stream(sound_pool_t *pool,
106 sound_stream_t *stream)
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");
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);
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);
132 return SOUND_POOL_ERROR_NONE;
135 static sound_pool_error_e __sound_pool_remove_stream(sound_pool_t *pool, sound_stream_t *stream)
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);
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,
148 SP_DEBUG("Id [%u] doesn't exist in streams hash table", stream->id);
150 SP_INFO("[%u] sound stream was removed from pool", stream->id);
152 SP_DEBUG("Stream wasn't removed from sound pool as it isn't known by "
153 "the pool for which operation is performed");
157 return SOUND_POOL_ERROR_NONE;
160 sound_pool_error_e _sound_stream_create(sound_source_t *src,
161 sound_stream_t **stream)
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");
171 SP_RETVM_IF(!alcMakeContextCurrent(src->parent_pool->al_context),
172 SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
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");
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;
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",
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);
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);
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);
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);
228 _sound_stream_destroy(_stream);
233 sound_pool_error_e _sound_stream_destroy(sound_stream_t *stream)
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.");
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);
253 SP_DEBUG("Can't set current context for deleting OpenAL source with id [%u]",
258 SP_SAFE_GFREE(stream);
261 return SOUND_POOL_ERROR_NONE;
264 sound_pool_error_e _sound_stream_play(sound_stream_t *stream)
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");
282 SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
283 SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
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.");
294 alSourcePlay(stream->al_source);
301 sound_pool_error_e _sound_stream_pause(sound_stream_t *stream)
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));
314 SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
315 SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
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.");
326 alSourcePause(stream->al_source);
333 sound_pool_error_e _sound_stream_resume(sound_stream_t *stream)
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));
345 SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
346 SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
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.");
357 alSourcePlay(stream->al_source);
364 sound_pool_error_e _sound_stream_stop(sound_stream_t *stream)
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));
377 SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
378 SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
380 stream->stopped = TRUE;
381 alSourceStop(stream->al_source);
384 return SOUND_POOL_ERROR_NONE;
387 sound_pool_error_e _sound_stream_set_loop(sound_stream_t *stream, unsigned loop)
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.");
396 alSourcei(stream->al_source, AL_LOOP_COUNT, (ALint)loop);
400 return SOUND_POOL_ERROR_NONE;
403 sound_pool_error_e _sound_stream_get_loop(sound_stream_t *stream, unsigned *loop)
406 SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
407 SP_INST_CHECK(loop, SOUND_POOL_ERROR_INVALID_PARAMETER);
409 *loop = stream->loop;
412 return SOUND_POOL_ERROR_NONE;
415 sound_pool_error_e _sound_stream_set_volume(sound_stream_t *stream, float volume)
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.");
427 alSourcef(stream->al_source, AL_GAIN, volume);
429 stream->volume = volume;
432 return SOUND_POOL_ERROR_NONE;
435 sound_pool_error_e _sound_stream_get_volume(sound_stream_t *stream, float *volume)
438 SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
439 SP_INST_CHECK(volume, SOUND_POOL_ERROR_INVALID_PARAMETER);
441 *volume = stream->volume;
444 return SOUND_POOL_ERROR_NONE;
447 sound_pool_error_e _sound_stream_get_state(sound_stream_t *stream,
448 sound_pool_stream_state_e *state)
451 SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
452 SP_INST_CHECK(state, SOUND_POOL_ERROR_INVALID_PARAMETER);
454 *state = stream->state;
457 return SOUND_POOL_ERROR_NONE;
460 sound_pool_error_e _sound_stream_set_priority(sound_stream_t *stream,
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;
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,
477 SP_RETVM_IF(SOUND_POOL_ERROR_NONE != ret, ret, "State changing event "
478 "wasn't registered. Callbacks will be not called");
480 SP_DEBUG("No need to update priority, Sound pool is INACTIVE.");
481 return SOUND_POOL_ERROR_NONE;
484 _sound_stream_priority_update_playback(stream->parent_source->parent_pool->mgr_priority);
487 return SOUND_POOL_ERROR_NONE;
490 sound_pool_error_e _sound_stream_get_priority(sound_stream_t *stream,
494 SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
495 SP_INST_CHECK(rank, SOUND_POOL_ERROR_INVALID_PARAMETER);
497 *rank = stream->priority;
500 return SOUND_POOL_ERROR_NONE;
503 sound_pool_error_e _sound_stream_set_callback(sound_stream_t *stream,
504 sound_pool_stream_state_change_cb callback, void *data)
507 SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
508 SP_INST_CHECK(callback, SOUND_POOL_ERROR_INVALID_PARAMETER);
510 stream->state_cb_info.callback = callback;
511 stream->state_cb_info.user_data = data;
514 return SOUND_POOL_ERROR_NONE;
517 sound_pool_error_e _sound_stream_unset_callback(sound_stream_t *stream)
520 SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
522 stream->state_cb_info.callback = NULL;
523 stream->state_cb_info.user_data = NULL;
526 return SOUND_POOL_ERROR_NONE;
529 sound_pool_error_e _sound_pool_get_stream_by_id(sound_pool_t *pool, unsigned id,
530 sound_stream_t **stream)
533 SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
534 SP_INST_CHECK(stream, SOUND_POOL_ERROR_INVALID_PARAMETER);
536 SP_RETVM_IF(!pool->streams, SOUND_POOL_ERROR_INVALID_OPERATION, "Corrupted "
537 "sound pool. Sources hash table is NULL");
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");
544 return SOUND_POOL_ERROR_NONE;