Fix to prevent seg fault
[platform/core/api/sound-pool.git] / src / source.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * @file source.c
19  * @brief This file include implementation of protected API
20  * for the sound sources(OpenAL buffers) as part of SoundPool.
21  */
22
23 #include "internal/source.h"
24 #include "internal/stream.h"
25 #include <unistd.h>
26 #ifdef ENABLE_ALURE
27 #include <AL/alure.h>
28 #else
29 #include <AL/alut.h>
30 #endif
31
32 static sound_pool_error_e __sound_pool_add_source(sound_pool_t *pool, sound_source_t *src);
33 static sound_pool_error_e __sound_pool_remove_source(sound_pool_t *pool, sound_source_t *src);
34 static sound_pool_error_e __probe_file_access(const char *file, int amode);
35
36 static sound_pool_error_e __sound_pool_add_source(sound_pool_t *pool, sound_source_t *src)
37 {
38         SP_DEBUG_FENTER();
39         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
40         SP_INST_CHECK(src, SOUND_POOL_ERROR_INVALID_PARAMETER);
41         SP_RETVM_IF(!pool->sources, SOUND_POOL_ERROR_INVALID_OPERATION, "Sound "
42                         "pool to add the source is corrupted: NULL sources hash table");
43         SP_RETVM_IF(!src->tag_name, SOUND_POOL_ERROR_INVALID_OPERATION,
44                         "Source to be added to sound pool is corrupted: NULL tag name");
45         SP_RETVM_IF(src->parent_pool != pool, SOUND_POOL_ERROR_INVALID_OPERATION,
46                         "Sound source can't be added to the pool different to the pool for"
47                         " which sound source was created");
48         SP_RETVM_IF(g_hash_table_contains(pool->sources, src->tag_name),
49                         SOUND_POOL_ERROR_INVALID_PARAMETER,
50                         "Tag already exists in sources hash table.");
51
52         SP_RETVM_IF(!g_hash_table_insert(pool->sources, src->tag_name, src),
53                         SOUND_POOL_ERROR_INVALID_OPERATION, "Error occurred when adding "
54                         "the source tagged [%s] to the sound pool", src->tag_name);
55
56         SP_DEBUG_FLEAVE();
57         return SOUND_POOL_ERROR_NONE;
58 }
59
60 static sound_pool_error_e __sound_pool_remove_source(sound_pool_t *pool, sound_source_t *src)
61 {
62         SP_DEBUG_FENTER();
63         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
64         SP_INST_CHECK(pool->sources, SOUND_POOL_ERROR_INVALID_PARAMETER);
65         SP_INST_CHECK(src, SOUND_POOL_ERROR_INVALID_PARAMETER);
66
67         if (src->parent_pool == pool && src->tag_name) {
68                 if (!g_hash_table_steal(pool->sources, src->tag_name)) {
69                         SP_DEBUG("Tag [%s] doesn't exist in sources hash table",
70                                         src->tag_name);
71                         SP_DEBUG_FLEAVE();
72                         return SOUND_POOL_ERROR_NONE;
73                 }
74                 GHashTableIter iter;
75                 gpointer key, value;
76                 g_hash_table_iter_init(&iter, pool->streams);
77                 while (g_hash_table_iter_next(&iter, &key, &value)) {
78                         guint size_before = g_hash_table_size(pool->streams);
79                         sound_stream_t *stream = (sound_stream_t*)value;
80                         if (src == stream->parent_source) {
81                                 if (stream->state == SOUND_POOL_STREAM_STATE_STOPPED || stream->state == SOUND_POOL_STREAM_STATE_FINISHED) {
82                                         SP_DEBUG("Callback isolator thread destroying the stream");
83                                 } else {
84                                         _sound_stream_destroy((sound_stream_t*)value);
85                                 }
86                         }
87                         guint size_after = g_hash_table_size(pool->streams);
88                         if (size_before != size_after)
89                                 g_hash_table_iter_init(&iter, pool->streams);
90                 }
91         } else {
92                         SP_DEBUG("Source wasn't removed from sound pool as it isn't known by "
93                                         "the pool for which operation is performed, or tag corrupted");
94         }
95
96         SP_DEBUG_FLEAVE();
97         return SOUND_POOL_ERROR_NONE;
98 }
99
100 /* file parameter should be not-NULL and not empty c-string */
101 static sound_pool_error_e __probe_file_access(const char *file, int amode)
102 {
103         SP_DEBUG_FENTER();
104         sound_pool_error_e ret = SOUND_POOL_ERROR_NONE;
105
106         if (-1 == access(file, amode)) {
107                 char errmsg[256];
108                 strerror_r(errno, errmsg, sizeof(errmsg));
109                 SP_ERROR("Couldn`t open file in [%i] mode, reason [%s].", amode, errmsg);
110                 if (EACCES == errno)
111                         ret = SOUND_POOL_ERROR_NOT_PERMITTED;
112                 else if (ENOENT == errno)
113                         ret = SOUND_POOL_ERROR_NO_SUCH_FILE;
114                 else
115                         ret = SOUND_POOL_ERROR_INVALID_OPERATION;
116                 SP_DEBUG_FLEAVE();
117                 return ret;
118         }
119
120         SP_DEBUG_FLEAVE();
121         return ret;
122 }
123
124 sound_pool_error_e _sound_source_create(sound_pool_t *pool, const char *tag,
125                 sound_source_t **src)
126 {
127         SP_DEBUG_FENTER();
128         SP_RETVM_IF(!pool, SOUND_POOL_ERROR_INVALID_PARAMETER,
129                         "Can't create sound source in NULL sound pool");
130         SP_RETVM_IF(!tag, SOUND_POOL_ERROR_INVALID_PARAMETER,
131                         "Can't create sound source with NULL tag name");
132         SP_RETVM_IF(!src, SOUND_POOL_ERROR_INVALID_PARAMETER,
133                         "Can't create sound source. Source pointer is NULL");
134         SP_RETVM_IF(strnlen(tag, MAX_TAG_LEN + 1) == MAX_TAG_LEN + 1,
135                         SOUND_POOL_ERROR_INVALID_PARAMETER, "Can't create sound source "
136                         "with tag name longer than %u", MAX_TAG_LEN);
137         SP_RETVM_IF(!pool->sources, SOUND_POOL_ERROR_INVALID_OPERATION, "Sound "
138                         "pool to create source is corrupted: NULL sources hash table.");
139         SP_RETVM_IF(g_hash_table_contains(pool->sources, tag),
140                         SOUND_POOL_ERROR_INVALID_PARAMETER,
141                         "Tag already exists in sources hash table.");
142
143         SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
144                         SOUND_POOL_ERROR_INVALID_OPERATION,
145                         "Can't set current context.");
146
147         sound_source_t *_src = NULL;
148         SP_RETVM_IF(!(_src = g_try_malloc0(sizeof(*_src))),
149                         SOUND_POOL_ERROR_OUT_OF_MEMORY,
150                         "Memory alloc failure. Can't create sound _src");
151
152         sound_pool_error_e ret_destroy = SOUND_POOL_ERROR_NONE;
153         sound_pool_error_e ret = SOUND_POOL_ERROR_NONE;
154         alGenBuffers(1, &_src->al_buffer);
155         if (alGetError() != AL_NO_ERROR) {
156                 ret = SOUND_POOL_ERROR_OUT_OF_MEMORY;
157                 GOTO_FAIL("OpenAL error occurred when trying to generate Buffer id", cfail);
158         }
159
160         _src->parent_pool = pool;
161         _src->tag_name = NULL;
162         _src->tag_name = g_strndup(tag, MAX_TAG_LEN);
163
164         ret = __sound_pool_add_source(pool, _src);
165         if (ret != SOUND_POOL_ERROR_NONE)
166                 GOTO_FAIL("Error occurred when trying to add source to pool", cfail);
167
168         *src = _src;
169         SP_DEBUG_FLEAVE();
170         return ret;
171
172 cfail:
173         ret_destroy = _sound_source_destroy(_src);
174         SP_RETVM_IF(SOUND_POOL_ERROR_NONE != ret_destroy, ret_destroy,
175                         "Error occurred during removal of sound source[%s].", tag);
176         SP_DEBUG_FLEAVE();
177         return ret;
178 }
179
180 sound_pool_error_e _sound_source_destroy(sound_source_t *src)
181 {
182         SP_DEBUG_FENTER();
183         SP_RETVM_IF(!src, SOUND_POOL_ERROR_INVALID_PARAMETER,
184                         "Can't destroy NULL sound source");
185
186         /* If parent pool exists, then source has to be removed from the pool */
187         if (src->parent_pool && __sound_pool_remove_source(src->parent_pool, src)
188                         != SOUND_POOL_ERROR_NONE)
189                 SP_DEBUG("Source wasn't removed from sound pool.");
190
191         SP_DEBUG("Deleting OpenAL buffer with id [%u]", src->al_buffer);
192         alDeleteBuffers(1, &src->al_buffer);
193
194         SP_SAFE_GFREE(src->tag_name);
195         SP_SAFE_GFREE(src);
196
197         SP_DEBUG_FLEAVE();
198         return SOUND_POOL_ERROR_NONE;
199 }
200
201 sound_pool_error_e _sound_source_load_from_file(sound_source_t *src,
202                 const char *fname)
203 {
204         SP_DEBUG_FENTER();
205         SP_INST_CHECK(src, SOUND_POOL_ERROR_INVALID_PARAMETER);
206         SP_INST_CHECK(fname, SOUND_POOL_ERROR_INVALID_PARAMETER);
207         SP_INST_CHECK(src->parent_pool, SOUND_POOL_ERROR_INVALID_OPERATION);
208         SP_RETVM_IF(!alcMakeContextCurrent(src->parent_pool->al_context),
209                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current AL context.");
210
211         sound_pool_error_e ret = __probe_file_access(fname, R_OK);
212         SP_RETVM_IF(SOUND_POOL_ERROR_NONE != ret, ret,
213                         "Can't load source from [%s] file.", fname);
214
215         ALuint buffer_handle = AL_NONE;
216         src->al_buffer = AL_NONE;
217
218 #ifdef ENABLE_ALURE
219         buffer_handle = alureCreateBufferFromFile((const ALchar *)fname);
220         SP_RETVM_IF(buffer_handle == AL_NONE, SOUND_POOL_ERROR_INVALID_OPERATION,
221                         "Can't load audio file [%s]. Error message [%s]", fname,
222                         alureGetErrorString());
223 #else
224         alutInitWithoutContext(NULL, NULL);
225         buffer_handle = alutCreateBufferFromFile(fname);
226         ALenum error = alutGetError();
227         alutExit();
228         SP_RETVM_IF(error != ALUT_ERROR_NO_ERROR || buffer_handle == AL_NONE,
229                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't load audio file [%s]. "
230                         "Error message [%s]", fname, alutGetErrorString(error));
231 #endif
232
233         src->al_buffer = buffer_handle;
234         SP_DEBUG_FLEAVE();
235         return ret;
236 }
237
238 sound_pool_error_e _sound_pool_get_source_by_tag(sound_pool_t *pool,
239                 const char *tag, sound_source_t **src)
240 {
241         SP_DEBUG_FENTER();
242         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
243         SP_INST_CHECK(tag, SOUND_POOL_ERROR_INVALID_PARAMETER);
244         SP_INST_CHECK(src, SOUND_POOL_ERROR_INVALID_PARAMETER);
245         SP_RETVM_IF(!pool->sources, SOUND_POOL_ERROR_INVALID_PARAMETER, "Corrupted "
246                         "sound pool. Sources hash table is NULL");
247
248         *src = (sound_source_t *)g_hash_table_lookup(pool->sources, tag);
249         SP_RETVM_IF(!(*src), SOUND_POOL_ERROR_KEY_NOT_AVAILABLE, "Tag doesn't "
250                         "exist in sources hash table");
251
252         SP_DEBUG_FLEAVE();
253         return SOUND_POOL_ERROR_NONE;
254 }