Updated API according to the changed stream prioritization concept
[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
26 #include <AL/alut.h>
27 #ifdef ENABLE_ALURE
28 #       include <AL/alure.h>
29 #endif
30
31 static int __sound_pool_add_source(sound_pool_t *pool, sound_source_t *src);
32 static int __sound_pool_remove_source(sound_pool_t *pool, sound_source_t *src);
33
34 static int __sound_pool_add_source(sound_pool_t *pool, sound_source_t *src)
35 {
36         SP_DEBUG_FENTER();
37         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
38         SP_INST_CHECK(src, SOUND_POOL_ERROR_INVALID_PARAMETER);
39         SP_RETVM_IF(!pool->sources, SOUND_POOL_ERROR_INVALID_OPERATION, "Sound "
40                         "pool to add the source is corrupted: NULL sources hash table");
41         SP_RETVM_IF(!src->tag_name, SOUND_POOL_ERROR_INVALID_OPERATION,
42                         "Source to be added to sound pool is corrupted: NULL tag name");
43         SP_RETVM_IF(src->parent_pool != pool, SOUND_POOL_ERROR_INVALID_OPERATION,
44                         "Sound source can't be added to the pool different to the pool for"
45                         " which sound source was created");
46         SP_RETVM_IF(g_hash_table_contains(pool->sources, src->tag_name),
47                         SOUND_POOL_ERROR_INVALID_PARAMETER,
48                         "Tag already exists in sources hash table.");
49
50         SP_RETVM_IF(!g_hash_table_insert(pool->sources, src->tag_name, src),
51                         SOUND_POOL_ERROR_INVALID_OPERATION, "Error occurred when adding "
52                         "the source tagged [%s] to the sound pool", src->tag_name);
53
54         SP_DEBUG_FLEAVE();
55         return SOUND_POOL_ERROR_NONE;
56 }
57
58 static int __sound_pool_remove_source(sound_pool_t *pool, sound_source_t *src)
59 {
60         SP_DEBUG_FENTER();
61         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
62         SP_INST_CHECK(pool->sources, SOUND_POOL_ERROR_INVALID_PARAMETER);
63         SP_INST_CHECK(src, SOUND_POOL_ERROR_INVALID_PARAMETER);
64
65         if (src->parent_pool == pool && src->tag_name) {
66                 if (!g_hash_table_steal(pool->sources, src->tag_name)) {
67                         SP_DEBUG("Tag [%s] doesn't exist in sources hash table",
68                                         src->tag_name);
69                         SP_DEBUG_FLEAVE();
70                         return SOUND_POOL_ERROR_NONE;
71                 }
72                 GHashTableIter iter;
73                 gpointer key, value;
74                 g_hash_table_iter_init(&iter, pool->streams);
75                 while (g_hash_table_iter_next(&iter, &key, &value)) {
76                         guint size_before = g_hash_table_size(pool->streams);
77                         sound_stream_t *stream = (sound_stream_t*)value;
78                         if (src == stream->parent_source)
79                                 _sound_stream_destroy((sound_stream_t*)value);
80                         guint size_after = g_hash_table_size(pool->streams);
81                         if (size_before != size_after)
82                                 g_hash_table_iter_init(&iter, pool->streams);
83                 }
84         } else {
85                         SP_DEBUG("Source wasn't removed from sound pool as it isn't known by "
86                                         "the pool for which operation is performed, or tag corrupted");
87         }
88
89         SP_DEBUG_FLEAVE();
90         return SOUND_POOL_ERROR_NONE;
91 }
92
93 sound_pool_error_e _sound_source_create(sound_pool_t *pool, const char *tag,
94                 sound_source_t **src)
95 {
96         SP_DEBUG_FENTER();
97         SP_RETVM_IF(!pool, SOUND_POOL_ERROR_INVALID_PARAMETER,
98                         "Can't create sound source in NULL sound pool");
99         SP_RETVM_IF(!tag, SOUND_POOL_ERROR_INVALID_PARAMETER,
100                         "Can't create sound source with NULL tag name");
101         SP_RETVM_IF(!src, SOUND_POOL_ERROR_INVALID_PARAMETER,
102                         "Can't create sound source. Source pointer is NULL");
103         SP_RETVM_IF(strnlen(tag, MAX_TAG_LEN + 1) == MAX_TAG_LEN + 1,
104                         SOUND_POOL_ERROR_INVALID_PARAMETER, "Can't create sound source "
105                         "with tag name longer than %u", MAX_TAG_LEN);
106         SP_RETVM_IF(!pool->sources, SOUND_POOL_ERROR_INVALID_OPERATION, "Sound "
107                         "pool to create source is corrupted: NULL sources hash table.");
108         SP_RETVM_IF(g_hash_table_contains(pool->sources, tag),
109                         SOUND_POOL_ERROR_INVALID_PARAMETER,
110                         "Tag already exists in sources hash table.");
111
112         SP_RETVM_IF(!alcMakeContextCurrent(pool->al_context),
113                         SOUND_POOL_ERROR_INVALID_OPERATION,
114                         "Can't set current context.");
115
116         sound_source_t *_src = NULL;
117         SP_RETVM_IF(!(_src = g_try_malloc0(sizeof(*_src))),
118                         SOUND_POOL_ERROR_OUT_OF_MEMORY,
119                         "Memory alloc failure. Can't create sound _src");
120
121         sound_pool_error_e ret = SOUND_POOL_ERROR_NONE;
122         alGenBuffers(1, &_src->al_buffer);
123         if (alGetError() != AL_NO_ERROR) {
124                 ret = SOUND_POOL_ERROR_OUT_OF_MEMORY;
125                 GOTO_FAIL("OpenAL error occurred when trying to generate Buffer id", cfail);
126         }
127
128         _src->parent_pool = pool;
129         _src->tag_name = NULL;
130         _src->tag_name = g_strndup(tag, MAX_TAG_LEN);
131
132         ret = __sound_pool_add_source(pool, _src);
133         if (ret != SOUND_POOL_ERROR_NONE)
134                 GOTO_FAIL("Error occurred when trying to add source to pool", cfail);
135
136         *src = _src;
137         SP_DEBUG_FLEAVE();
138         return ret;
139
140 cfail:
141         ret = _sound_source_destroy(_src);
142         SP_RETVM_IF(SOUND_POOL_ERROR_NONE != ret, ret, "Error occurred during removal "
143                         "of sound source[%s].", tag);
144         SP_DEBUG_FLEAVE();
145         return ret;
146 }
147
148 sound_pool_error_e _sound_source_destroy(sound_source_t *src)
149 {
150         SP_DEBUG_FENTER();
151         SP_RETVM_IF(!src, SOUND_POOL_ERROR_INVALID_PARAMETER,
152                         "Can't destroy NULL sound source");
153
154         /* If parent pool exists, then source has to be removed from the pool */
155         if (src->parent_pool && __sound_pool_remove_source(src->parent_pool, src)
156                         != SOUND_POOL_ERROR_NONE)
157                 SP_DEBUG("Source wasn't removed from sound pool.");
158
159         SP_DEBUG("Deleting OpenAL buffer with id [%u]", src->al_buffer);
160         alDeleteBuffers(1, &src->al_buffer);
161
162         SP_SAFE_GFREE(src->tag_name);
163         SP_SAFE_GFREE(src);
164
165         SP_DEBUG_FLEAVE();
166         return SOUND_POOL_ERROR_NONE;
167 }
168
169 sound_pool_error_e _sound_source_load_from_file(sound_source_t *src,
170                 const char *fname)
171 {
172         SP_DEBUG_FENTER();
173         SP_INST_CHECK(src, SOUND_POOL_ERROR_INVALID_PARAMETER);
174         SP_INST_CHECK(fname, SOUND_POOL_ERROR_INVALID_PARAMETER);
175         SP_INST_CHECK(src->parent_pool, SOUND_POOL_ERROR_INVALID_OPERATION);
176         SP_RETVM_IF(!alcMakeContextCurrent(src->parent_pool->al_context),
177                         SOUND_POOL_ERROR_INVALID_OPERATION, "Can't set current AL context.");
178
179         sound_pool_error_e ret = SOUND_POOL_ERROR_NONE;
180         ALenum format;
181         ALsizei size;
182         ALvoid* data = NULL;
183         ALsizei freq;
184         ALboolean loop;
185
186         alutLoadWAVFile((ALbyte*)fname, &format, &data, &size, &freq, &loop);
187
188 #ifdef ENABLE_ALURE
189         if (alGetError() != AL_NO_ERROR || !data) {
190                 if (alureBufferDataFromFile(fname, src->al_buffer) == AL_FALSE) {
191                         src->al_buffer = AL_NONE;
192                         SP_ERROR("Can't load audio file. No such file [%s]", fname);
193                         ret = SOUND_POOL_ERROR_NO_SUCH_FILE;
194                 }
195                 SP_DEBUG_FLEAVE();
196                 return ret;
197         }
198 #else
199         SP_RETVM_IF(alGetError() != AL_NO_ERROR || !data,
200                         SOUND_POOL_ERROR_NO_SUCH_FILE, "Can't load audio file. No such "
201                         "file [%s]", fname);
202 #endif
203
204         alBufferData(src->al_buffer, format, data, size, freq);
205         if (alGetError() != AL_NO_ERROR) {
206                 SP_ERROR("Can't create audio buffer from file [%s]", fname);
207                 ret = SOUND_POOL_ERROR_INVALID_OPERATION;
208         }
209
210         alutUnloadWAV(format, data, size, freq);
211
212         SP_DEBUG_FLEAVE();
213         return ret;
214 }
215
216 sound_pool_error_e _sound_pool_get_source_by_tag(sound_pool_t *pool,
217                 const char *tag, sound_source_t **src)
218 {
219         SP_DEBUG_FENTER();
220         SP_INST_CHECK(pool, SOUND_POOL_ERROR_INVALID_PARAMETER);
221         SP_INST_CHECK(tag, SOUND_POOL_ERROR_INVALID_PARAMETER);
222         SP_INST_CHECK(src, SOUND_POOL_ERROR_INVALID_PARAMETER);
223         SP_RETVM_IF(!pool->sources, SOUND_POOL_ERROR_INVALID_PARAMETER, "Corrupted "
224                         "sound pool. Sources hash table is NULL");
225
226         *src = (sound_source_t *)g_hash_table_lookup(pool->sources, tag);
227         SP_RETVM_IF(!(*src), SOUND_POOL_ERROR_KEY_NOT_AVAILABLE, "Tag doesn't "
228                         "exist in sources hash table");
229
230         SP_DEBUG_FLEAVE();
231         return SOUND_POOL_ERROR_NONE;
232 }