Disable session backward compatibility with soundpool
[platform/core/api/sound-pool.git] / src / source.c
index 09f6906..c35d8af 100644 (file)
 
 #include "internal/source.h"
 #include "internal/stream.h"
-
-#include <AL/alut.h>
+#include <unistd.h>
 #ifdef ENABLE_ALURE
-#      include <AL/alure.h>
+#include <AL/alure.h>
+#else
+#include <AL/alut.h>
 #endif
 
-static int __sound_pool_add_source(sound_pool_t *pool, sound_source_t *src);
-static int __sound_pool_remove_source(sound_pool_t *pool, sound_source_t *src);
+static sound_pool_error_e __sound_pool_add_source(sound_pool_t *pool, sound_source_t *src);
+static sound_pool_error_e __sound_pool_remove_source(sound_pool_t *pool, sound_source_t *src);
+static sound_pool_error_e __probe_file_access(const char *file, int amode);
 
-static int __sound_pool_add_source(sound_pool_t *pool, sound_source_t *src)
+static sound_pool_error_e __sound_pool_add_source(sound_pool_t *pool, sound_source_t *src)
 {
        SP_DEBUG_FENTER();
        SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
@@ -55,7 +57,7 @@ static int __sound_pool_add_source(sound_pool_t *pool, sound_source_t *src)
        return SOUND_POOL_ERROR_NONE;
 }
 
-static int __sound_pool_remove_source(sound_pool_t *pool, sound_source_t *src)
+static sound_pool_error_e __sound_pool_remove_source(sound_pool_t *pool, sound_source_t *src)
 {
        SP_DEBUG_FENTER();
        SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
@@ -75,8 +77,13 @@ static int __sound_pool_remove_source(sound_pool_t *pool, sound_source_t *src)
                while (g_hash_table_iter_next(&iter, &key, &value)) {
                        guint size_before = g_hash_table_size(pool->streams);
                        sound_stream_t *stream = (sound_stream_t*)value;
-                       if (src == stream->parent_source)
-                               _sound_stream_destroy((sound_stream_t*)value);
+                       if (src == stream->parent_source) {
+                               if (stream->state == SOUND_POOL_STREAM_STATE_STOPPED || stream->state == SOUND_POOL_STREAM_STATE_FINISHED) {
+                                       SP_DEBUG("Callback isolator thread destroying the stream");
+                               } else {
+                                       _sound_stream_destroy((sound_stream_t*)value);
+                               }
+                       }
                        guint size_after = g_hash_table_size(pool->streams);
                        if (size_before != size_after)
                                g_hash_table_iter_init(&iter, pool->streams);
@@ -90,6 +97,30 @@ static int __sound_pool_remove_source(sound_pool_t *pool, sound_source_t *src)
        return SOUND_POOL_ERROR_NONE;
 }
 
+/* file parameter should be not-NULL and not empty c-string */
+static sound_pool_error_e __probe_file_access(const char *file, int amode)
+{
+       SP_DEBUG_FENTER();
+       sound_pool_error_e ret = SOUND_POOL_ERROR_NONE;
+
+       if (-1 == access(file, amode)) {
+               char errmsg[256];
+               strerror_r(errno, errmsg, sizeof(errmsg));
+               SP_ERROR("Couldn`t open file in [%i] mode, reason [%s].", amode, errmsg);
+               if (EACCES == errno)
+                       ret = SOUND_POOL_ERROR_NOT_PERMITTED;
+               else if (ENOENT == errno)
+                       ret = SOUND_POOL_ERROR_NO_SUCH_FILE;
+               else
+                       ret = SOUND_POOL_ERROR_INVALID_OPERATION;
+               SP_DEBUG_FLEAVE();
+               return ret;
+       }
+
+       SP_DEBUG_FLEAVE();
+       return ret;
+}
+
 sound_pool_error_e _sound_source_create(sound_pool_t *pool, const char *tag,
                sound_source_t **src)
 {
@@ -177,39 +208,29 @@ sound_pool_error_e _sound_source_load_from_file(sound_source_t *src,
        SP_RETVM_IF(!alcMakeContextCurrent(src->parent_pool->al_context),
                        SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current AL context.");
 
-       sound_pool_error_e ret = SOUND_POOL_ERROR_NONE;
-       ALenum format;
-       ALsizei size;
-       ALvoid* data = NULL;
-       ALsizei freq;
-       ALboolean loop;
+       sound_pool_error_e ret = __probe_file_access(fname, R_OK);
+       SP_RETVM_IF(SOUND_POOL_ERROR_NONE != ret, ret,
+                       "Can't load source from [%s] file.", fname);
 
-       alutLoadWAVFile((ALbyte*)fname, &format, &data, &size, &freq, &loop);
+       ALuint buffer_handle = AL_NONE;
+       src->al_buffer = AL_NONE;
 
 #ifdef ENABLE_ALURE
-       if (alGetError() != AL_NO_ERROR || !data) {
-               if (alureBufferDataFromFile(fname, src->al_buffer) == AL_FALSE) {
-                       src->al_buffer = AL_NONE;
-                       SP_ERROR("Can't load audio file. No such file [%s]", fname);
-                       ret = SOUND_POOL_ERROR_NO_SUCH_FILE;
-               }
-               SP_DEBUG_FLEAVE();
-               return ret;
-       }
+       buffer_handle = alureCreateBufferFromFile((const ALchar *)fname);
+       SP_RETVM_IF(buffer_handle == AL_NONE, SOUND_POOL_ERROR_INVALID_OPERATION,
+                       "Can't load audio file [%s]. Error message [%s]", fname,
+                       alureGetErrorString());
 #else
-       SP_RETVM_IF(alGetError() != AL_NO_ERROR || !data,
-                       SOUND_POOL_ERROR_NO_SUCH_FILE, "Can't load audio file. No such "
-                       "file [%s]", fname);
+       alutInitWithoutContext(NULL, NULL);
+       buffer_handle = alutCreateBufferFromFile(fname);
+       ALenum error = alutGetError();
+       alutExit();
+       SP_RETVM_IF(error != ALUT_ERROR_NO_ERROR || buffer_handle == AL_NONE,
+                       SOUND_POOL_ERROR_INVALID_OPERATION, "Can't load audio file [%s]. "
+                       "Error message [%s]", fname, alutGetErrorString(error));
 #endif
 
-       alBufferData(src->al_buffer, format, data, size, freq);
-       if (alGetError() != AL_NO_ERROR) {
-               SP_ERROR("Can't create audio buffer from file [%s]", fname);
-               ret = SOUND_POOL_ERROR_INVALID_OPERATION;
-       }
-
-       alutUnloadWAV(format, data, size, freq);
-
+       src->al_buffer = buffer_handle;
        SP_DEBUG_FLEAVE();
        return ret;
 }