Changed logic of stream creation to avoid redundant transition to playing state 15/135815/8
authorIevgen Vagin <i.vagin@samsung.com>
Mon, 26 Jun 2017 19:40:48 +0000 (22:40 +0300)
committerIevgen Vagin <i.vagin@samsung.com>
Thu, 29 Jun 2017 09:54:31 +0000 (12:54 +0300)
(Redundant transition was occurred when stream with
low priority had been added to the active pool with
already playing stream(s) with higher priority)

Change-Id: If25cdd918cf923c1885bb5f8578b61fa80f92af5
Signed-off-by: Ievgen Vagin <i.vagin@samsung.com>
include/internal/stream.h
src/priority.c
src/sound_pool.c
src/stream.c

index 9d67b34..463e97a 100644 (file)
@@ -54,7 +54,9 @@ typedef struct sound_stream_s {
        ALuint al_source;
 } sound_stream_t;
 
-sound_pool_error_e _sound_stream_create(sound_source_t *src, sound_stream_t **stream);
+sound_pool_error_e _sound_stream_create(sound_source_t *src, unsigned loop,
+               float volume, unsigned priority, sound_pool_stream_state_change_cb cb,
+               void *data, sound_stream_t **stream);
 
 sound_pool_error_e _sound_stream_destroy(sound_stream_t *src);
 
index 124946d..a392bf6 100644 (file)
@@ -203,7 +203,11 @@ void _sound_stream_priority_update_playback(stream_priority_manager_t *mgr)
                                SOUND_POOL_STREAM_STATE_PAUSED != stream->state)
                                _sound_stream_play(stream);
                } else if (SOUND_POOL_STREAM_STATE_PLAYING == stream->state) {
+                       stream->stopped = TRUE; /* Shows it was stopped due to priority */
                        _sound_stream_pause(stream);
+               } else if (SOUND_POOL_STREAM_STATE_NONE == stream->state) {
+                       /* This case handles newly created streams in active pool, but with
+                          too low priority for transition to playing state: */
                        stream->state_previous = stream->state;
                        stream->state = SOUND_POOL_STREAM_STATE_SUSPENDED;
                        if (_stream_cb_manager_register_event(mgr->pool->cbmgr, stream) !=
index ea23673..20a5743 100644 (file)
@@ -208,47 +208,20 @@ sound_pool_error_e sound_pool_stream_play(sound_pool_h pool, const char *tag,
 
        sound_pool_t *_pool = (sound_pool_t *)pool;
        sound_source_t *_source = NULL;
-       sound_pool_error_e ret_destroy = SOUND_POOL_ERROR_NONE;
        sound_pool_error_e ret = _sound_pool_get_source_by_tag(_pool, tag, &_source);
        SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret, "Error occurred when "
                        "getting sound source [%s] from the sound pool", tag);
 
        sound_stream_t *_stream = NULL;
-       ret = _sound_stream_create(_source, &_stream);
+       ret = _sound_stream_create(_source, loop, volume, priority, callback, data,
+                       &_stream);
        SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret,
                        "Error while creating sound source instance.");
 
-       ret = _sound_stream_set_loop(_stream, loop);
-       if (ret != SOUND_POOL_ERROR_NONE)
-               GOTO_FAIL("Error occurred when setting sound stream loop parameter", cfail);
-
-       ret = _sound_stream_set_volume(_stream, volume);
-       if (ret != SOUND_POOL_ERROR_NONE)
-               GOTO_FAIL("Error occurred when setting sound stream volume " "parameter",
-                               cfail);
-
-       if (callback) {
-               ret = _sound_stream_set_callback(_stream, callback, data);
-               if (ret != SOUND_POOL_ERROR_NONE)
-                       GOTO_FAIL("Error occurred when setting sound stream callback", cfail);
-       }
-
-       ret = _sound_stream_set_priority(_stream, priority);
-       if (ret != SOUND_POOL_ERROR_NONE)
-               GOTO_FAIL("Error occurred when setting sound stream priority "
-                               "parameter", cfail);
-
        *id = _stream->id;
 
        SP_DEBUG_FLEAVE();
        return ret;
-
-cfail:
-       ret_destroy = _sound_stream_destroy(_stream);
-       SP_RETVM_IF(SOUND_POOL_ERROR_NONE != ret_destroy, ret_destroy,
-                       "Error occurred during removal of uncompleted sound stream.");
-       SP_DEBUG_FLEAVE();
-       return ret;
 }
 
 sound_pool_error_e sound_pool_stream_pause(sound_pool_h pool, unsigned id)
index eaae813..bed069b 100644 (file)
 
 #include "internal/stream_cb_manager.h"
 
-#define DEFAULT_STREAM_PRIORITY 255U
-#define DEFAULT_STREAM_LOOP     1U
-#define DEFAULT_STREAM_VOLUME   1.0f
-
 static const char *__stringify_stream_state(sound_pool_stream_state_e state);
 static void __al_source_state_cb(ALuint source, ALenum state, ALvoid *data);
 static sound_pool_error_e __sound_pool_add_stream(sound_pool_t *pool, sound_stream_t *stream);
@@ -74,10 +70,16 @@ static void __al_source_state_cb(ALuint source, ALenum state, ALvoid *data)
                stream->state = SOUND_POOL_STREAM_STATE_PLAYING;
                break;
        case AL_PAUSED:
-               if (pool->state == SOUND_POOL_STATE_INACTIVE)
+               if (pool->state == SOUND_POOL_STATE_INACTIVE) {
                        stream->state = SOUND_POOL_STREAM_STATE_SUSPENDED;
-               else if (pool->state == SOUND_POOL_STATE_ACTIVE)
-                       stream->state = SOUND_POOL_STREAM_STATE_PAUSED;
+               } else if (pool->state == SOUND_POOL_STATE_ACTIVE) {
+                       if (stream->stopped) { /* Stream stopped due to priority issue */
+                               stream->state = SOUND_POOL_STREAM_STATE_SUSPENDED;
+                               stream->stopped = FALSE;
+                       } else {
+                               stream->state = SOUND_POOL_STREAM_STATE_PAUSED;
+                       }
+               }
                break;
        case AL_STOPPED:
                if (stream->stopped)
@@ -157,8 +159,38 @@ static sound_pool_error_e __sound_pool_remove_stream(sound_pool_t *pool, sound_s
        return SOUND_POOL_ERROR_NONE;
 }
 
-sound_pool_error_e _sound_stream_create(sound_source_t *src,
-               sound_stream_t **stream)
+static sound_pool_error_e __sound_stream_init(sound_stream_t *stream,
+               unsigned loop, float volume, unsigned priority,
+               sound_pool_stream_state_change_cb cb, void *data)
+{
+       SP_DEBUG_FENTER();
+       sound_pool_error_e ret = SOUND_POOL_ERROR_NONE;
+
+       if (cb) {
+               ret = _sound_stream_set_callback(stream, cb, data);
+               SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret,
+                               "Error occurred when setting sound stream callback");
+       }
+
+       ret = _sound_stream_set_loop(stream, loop);
+       SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret,
+                       "Error occurred when setting sound stream loop parameter");
+
+       ret = _sound_stream_set_volume(stream, volume);
+       SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret,
+                       "Error occurred when setting sound stream volume parameter");
+
+       ret = _sound_stream_set_priority(stream, priority);
+       SP_RETVM_IF(ret != SOUND_POOL_ERROR_NONE, ret,
+                       "Error occurred when setting sound stream priority parameter");
+
+       SP_DEBUG_FLEAVE();
+       return ret;
+}
+
+sound_pool_error_e _sound_stream_create(sound_source_t *src, unsigned loop,
+               float volume, unsigned priority, sound_pool_stream_state_change_cb cb,
+               void *data, sound_stream_t **stream)
 {
        SP_DEBUG_FENTER();
        SP_RETVM_IF(!src, SOUND_POOL_ERROR_INVALID_PARAMETER,
@@ -171,22 +203,17 @@ sound_pool_error_e _sound_stream_create(sound_source_t *src,
        SP_RETVM_IF(!alcMakeContextCurrent(src->parent_pool->al_context),
                        SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
 
+       sound_pool_error_e ret = SOUND_POOL_ERROR_NONE;
        sound_stream_t *_stream = NULL;
        SP_RETVM_IF(!(_stream = g_try_malloc0(sizeof(*_stream))),
                        SOUND_POOL_ERROR_OUT_OF_MEMORY,
                        "Memory alloc failure. Can't create sound stream");
 
        _stream->parent_source = src;
-       _stream->priority = DEFAULT_STREAM_PRIORITY;
-       _stream->loop = DEFAULT_STREAM_LOOP;
-       _stream->volume = DEFAULT_STREAM_VOLUME;
        _stream->state_previous = SOUND_POOL_STREAM_STATE_NONE;
        _stream->state = SOUND_POOL_STREAM_STATE_NONE;
        _stream->stopped = FALSE;
-       _stream->state_cb_info.callback = NULL;
-       _stream->state_cb_info.user_data = NULL;
 
-       sound_pool_error_e ret = SOUND_POOL_ERROR_NONE;
        alGenSources((ALsizei)1, &_stream->al_source);
        if (alGetError() != AL_NO_ERROR) {
                ret = SOUND_POOL_ERROR_OUT_OF_MEMORY;
@@ -216,6 +243,10 @@ sound_pool_error_e _sound_stream_create(sound_source_t *src,
                                "Data to OpenAL Source", cfail);
        }
 
+       ret = __sound_stream_init(_stream, loop, volume, priority, cb, data);
+       if (ret != SOUND_POOL_ERROR_NONE)
+               GOTO_FAIL("Sound stream initialization error occurred", cfail);
+
        ret = __sound_pool_add_stream(src->parent_pool, _stream);
        if (ret != SOUND_POOL_ERROR_NONE)
                GOTO_FAIL("Error occurred when trying to add source to pool", cfail);
@@ -225,7 +256,11 @@ sound_pool_error_e _sound_stream_create(sound_source_t *src,
        return ret;
 
 cfail:
-       _sound_stream_destroy(_stream);
+       if (SOUND_POOL_ERROR_NONE == _sound_stream_destroy(_stream))
+               _stream = NULL;
+
+       SP_SAFE_GFREE(_stream);
+
        SP_DEBUG_FLEAVE();
        return ret;
 }
@@ -284,17 +319,9 @@ sound_pool_error_e _sound_stream_play(sound_stream_t *stream)
        SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
                        SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current context.");
 
-       if (pool->state == SOUND_POOL_STATE_INACTIVE &&
-                       stream->state == SOUND_POOL_STREAM_STATE_NONE) {
-               stream->state_previous = stream->state;
-               stream->state = SOUND_POOL_STREAM_STATE_SUSPENDED;
-               ret = _stream_cb_manager_register_event(pool->cbmgr, stream);
-               SP_RETVM_IF(SOUND_POOL_ERROR_NONE != ret, ret, "State changing event "
-                               "wasn't registered. Callbacks will be not called");
-               SP_INFO("Don't play due to SoundPool is in inactive state.");
-       } else {
-               alSourcePlay(stream->al_source);
-       }
+       alSourcePlay(stream->al_source);
+       SP_RETVM_IF(alGetError() != AL_NO_ERROR, SOUND_POOL_ERROR_INVALID_OPERATION,
+                       "OpenAL error occurred when trying to play OpenAL Source");
 
        SP_DEBUG_FLEAVE();
        return ret;