Isolated ALURE/ALUT source loading logic, avoided usage of deprecated alutLoadWAVFile() 57/86357/5
authorIevgen Vagin <i.vagin@samsung.com>
Wed, 31 Aug 2016 17:08:24 +0000 (20:08 +0300)
committerIevgen Vagin <i.vagin@samsung.com>
Fri, 2 Sep 2016 15:15:47 +0000 (18:15 +0300)
Change-Id: I8c74ac0a7eb0bec149596838dcd3c05ec8397746
Signed-off-by: Ievgen Vagin <i.vagin@samsung.com>
src/source.c
src/stream.c

index 09f6906..21de78d 100644 (file)
 #include "internal/source.h"
 #include "internal/stream.h"
 
+#include <unistd.h>
 #include <AL/alut.h>
 #ifdef ENABLE_ALURE
 #      include <AL/alure.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);
@@ -90,6 +92,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_PERMISSION_DENIED;
+               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 +203,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;
 }
index eefe07f..453e847 100644 (file)
@@ -473,7 +473,7 @@ sound_pool_error_e _sound_stream_set_priority(sound_stream_t *stream,
                if (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(        stream->parent_source->parent_pool->cbmgr,
+                       ret = _stream_cb_manager_register_event(stream->parent_source->parent_pool->cbmgr,
                                        stream);
                        SP_RETVM_IF(SOUND_POOL_ERROR_NONE != ret, ret, "State changing event "
                                        "wasn't registered. Callbacks will be not called");