#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);
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);
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)
{
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;
}