Remove deprecated APIs and codes for session backward compatibility
[platform/core/multimedia/libmm-sound.git] / mm_sound.c
1 /*
2  * libmm-sound
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungbae Shin <seungbae.shin@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <memory.h>
24 #include <unistd.h>
25 #include <pthread.h>
26
27 #include <errno.h>
28
29 #include <vconf.h>
30 #include <mm_types.h>
31 #include <mm_error.h>
32 #include <mm_debug.h>
33 #include "include/mm_sound_private.h"
34 #include "include/mm_sound_utils.h"
35 #include "include/mm_sound_client.h"
36 #include "include/mm_sound_pa_client.h"
37 #include "include/mm_sound_common.h"
38
39
40 #define VOLUME_MAX_MULTIMEDIA   16
41 #define VOLUME_MAX_BASIC                8
42 #define VOLUME_MAX_SINGLE               1
43
44
45 #define MASTER_VOLUME_MAX 100
46 #define MASTER_VOLUME_MIN 0
47
48 #include <gio/gio.h>
49
50 #define MM_SOUND_DBUS_BUS_NAME_PREPIX  "org.tizen.MMSound"
51 #define MM_SOUND_DBUS_OBJECT_PATH  "/org/tizen/MMSound"
52 #define MM_SOUND_DBUS_INTERFACE    "org.tizen.mmsound"
53
54 static const char* _get_volume_str(volume_type_t type)
55 {
56         static const char *volume_type_str[VOLUME_TYPE_MAX] = {
57                 "SYSTEM", "NOTIFICATION", "ALARM", "RINGTONE", "MEDIA", "CALL", "VOIP", "VOICE", "FIXED"
58         };
59
60         return (type >= VOLUME_TYPE_SYSTEM && type < VOLUME_TYPE_MAX) ? volume_type_str[type] : "Unknown";
61 }
62
63 static int _validate_volume(volume_type_t type, int value)
64 {
65         if (value < 0)
66                 return -1;
67
68         switch (type) {
69         case VOLUME_TYPE_CALL:
70         case VOLUME_TYPE_VOIP:
71                 if (value >= VOLUME_MAX_BASIC) {
72                         return -1;
73                 }
74                 break;
75         case VOLUME_TYPE_SYSTEM:
76         case VOLUME_TYPE_MEDIA:
77         case VOLUME_TYPE_ALARM:
78         case VOLUME_TYPE_NOTIFICATION:
79         case VOLUME_TYPE_RINGTONE:
80         case VOLUME_TYPE_VOICE:
81                 if (value >= VOLUME_MAX_MULTIMEDIA) {
82                         return -1;
83                 }
84                 break;
85         default:
86                 return -1;
87                 break;
88         }
89         return 0;
90 }
91
92 EXPORT_API
93 int mm_sound_volume_remove_callback(volume_type_t type)
94 {
95         /* FIXME : Will be removed */
96         return MM_ERROR_NOT_SUPPORT_API;
97 }
98
99 EXPORT_API
100 int mm_sound_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* user_data, unsigned int *subs_id)
101 {
102         int ret = MM_ERROR_NONE;
103
104         if (func == NULL || subs_id == NULL) {
105                 debug_error("argument is not valid");
106                 return MM_ERROR_INVALID_ARGUMENT;
107         }
108
109         ret = mm_sound_client_add_volume_changed_callback(func, user_data, subs_id);
110         if (ret < 0) {
111                 debug_error("Can not add volume changed callback, ret = %x", ret);
112         }
113
114         return ret;
115 }
116
117 EXPORT_API
118 int mm_sound_remove_volume_changed_callback(unsigned int subs_id)
119 {
120         int ret = MM_ERROR_NONE;
121
122         ret = mm_sound_client_remove_volume_changed_callback(subs_id);
123         if (ret < 0) {
124                 debug_error("Can not remove volume changed callback, ret = %x", ret);
125         }
126
127         return ret;
128 }
129
130 EXPORT_API
131 int mm_sound_volume_set_value(volume_type_t volume_type, const unsigned int volume_level)
132 {
133         int ret = MM_ERROR_NONE;
134
135         debug_msg("type = (%d)%s, value = %d", volume_type, _get_volume_str(volume_type), volume_level);
136
137         /* Check input param */
138         if (0 > _validate_volume(volume_type, (int)volume_level)) {
139                 debug_error("invalid volume type %d, value %u", volume_type, volume_level);
140                 return MM_ERROR_INVALID_ARGUMENT;
141         }
142
143         /* request daemon to set volume */
144         ret = mm_sound_client_set_volume_by_type(volume_type, volume_level);
145         if (MM_ERROR_NONE != ret)
146                 debug_error("Can not set volume, ret=0x%x", ret);
147
148         return ret;
149 }
150
151 EXPORT_API
152 int mm_sound_volume_get_value(volume_type_t type, unsigned int *value)
153 {
154         int ret = MM_ERROR_NONE;
155
156         /* Check input param */
157         if (value == NULL) {
158                 debug_error("invalid argument");
159                 return MM_ERROR_INVALID_ARGUMENT;
160         }
161         if (type < 0 || type >= VOLUME_TYPE_MAX) {
162                 debug_error("invalid volume type value %d", type);
163                 return MM_ERROR_INVALID_ARGUMENT;
164         }
165
166         ret = mm_sound_util_volume_get_value_by_type(type, value);
167
168         debug_msg("returned %s = %d", _get_volume_str(type), *value);
169         return ret;
170 }
171
172 EXPORT_API
173 int mm_sound_volume_primary_type_set(volume_type_t type)
174 {
175         int ret = MM_ERROR_NONE;
176
177         /* Check input param */
178         if (type < VOLUME_TYPE_UNKNOWN || type >= VOLUME_TYPE_MAX) {
179                 debug_error("invalid argument");
180                 return MM_ERROR_INVALID_ARGUMENT;
181         }
182
183         if (vconf_set_int(VCONFKEY_SOUND_PRIMARY_VOLUME_TYPE, type)) {
184                 debug_error("could not set vconf for RIMARY_VOLUME_TYPE");
185                 ret = MM_ERROR_SOUND_INTERNAL;
186         } else {
187                 debug_msg("set primary volume type forcibly %d(%s)", type, _get_volume_str(type));
188         }
189
190         return ret;
191 }
192
193 EXPORT_API
194 int mm_sound_volume_primary_type_get(volume_type_t *type)
195 {
196         int ret = MM_ERROR_NONE;
197         int voltype = VOLUME_TYPE_RINGTONE;
198
199         /* Check input param */
200         if (type == NULL) {
201                 debug_error("invalid argument");
202                 return MM_ERROR_INVALID_ARGUMENT;
203         }
204
205         /* check force set */
206         if (vconf_get_int(VCONFKEY_SOUND_PRIMARY_VOLUME_TYPE, &voltype)) {
207                 debug_error("could not get vconf for PRIMARY_VOLUME_TYPE");
208                 ret = MM_ERROR_SOUND_INTERNAL;
209         } else {
210                 debug_msg("get primary volume type %d(%s)", voltype, _get_volume_str(voltype));
211                 *type = voltype;
212         }
213
214         return ret;
215 }
216
217 EXPORT_API
218 int mm_sound_set_filter(const char *stream_type, const char *filter_name, const char *filter_parameters, const char *filter_group)
219 {
220         /* Check input param */
221         if (!stream_type || !filter_name) {
222                 debug_error("invalid argument");
223                 return MM_ERROR_INVALID_ARGUMENT;
224         }
225         if (!filter_parameters)
226                 filter_parameters = "";
227         if (!filter_group)
228                 filter_group = "default";
229
230         debug_msg("stream_type(%s), filter_name(%s), filter_parameters(%s), filter_group(%s)", stream_type, filter_name, filter_parameters, filter_group);
231
232         return mm_sound_client_set_filter_by_type(stream_type, filter_name, filter_parameters, filter_group);
233 }
234
235 EXPORT_API
236 int mm_sound_unset_filter(const char *stream_type)
237 {
238         /* Check input param */
239         if (!stream_type) {
240                 debug_error("invalid argument");
241                 return MM_ERROR_INVALID_ARGUMENT;
242         }
243
244         debug_msg("stream_type(%s)", stream_type);
245
246         return mm_sound_client_unset_filter_by_type(stream_type);
247 }
248
249 EXPORT_API
250 int mm_sound_control_filter(const char *stream_type, const char *filter_name, const char *filter_controls)
251 {
252         /* Check input param */
253         if (!stream_type || !filter_name || !filter_controls) {
254                 debug_error("invalid argument");
255                 return MM_ERROR_INVALID_ARGUMENT;
256         }
257
258         debug_msg("stream_type(%s), filter_name(%s), filter_controls(%s)", stream_type, filter_name, filter_controls);
259
260         return mm_sound_client_control_filter_by_type(stream_type, filter_name, filter_controls);
261 }
262
263 ///////////////////////////////////
264 ////     MMSOUND PLAY APIs
265 ///////////////////////////////////
266 EXPORT_API
267 int mm_sound_play_sound_with_stream_info(const char *filename, char *stream_type, int stream_id, unsigned int loop, mm_sound_stop_callback_func callback, void *data, int *handle)
268 {
269         MMSoundPlayParam param = { 0, };
270         int err;
271
272         param.filename = filename;
273         param.volume = 0; //volume value dose not effect anymore
274         param.callback = callback;
275         param.data = data;
276
277         if (loop == 0)
278                 param.loop = -1;
279         else
280                 param.loop = loop;
281
282         err = mm_sound_client_play_sound_with_stream_info(&param, handle, stream_type, stream_id);
283         if (err < 0) {
284                 debug_error("Failed to play sound");
285                 return err;
286         }
287
288         debug_warning("success : handle=[%p]", handle);
289
290         return MM_ERROR_NONE;
291
292 }
293
294
295 EXPORT_API
296 int mm_sound_stop_sound(int handle)
297 {
298         int err;
299
300         debug_warning("enter : handle=[%d]", handle);
301         /* Stop sound */
302         err = mm_sound_client_stop_sound(handle);
303         if (err < 0) {
304                 debug_error("Fail to stop sound");
305                 return err;
306         }
307         debug_warning("success : handle=[%d]", handle);
308
309         return MM_ERROR_NONE;
310 }
311
312 ///////////////////////////////////
313 ////     MMSOUND TONE APIs
314 ///////////////////////////////////
315 EXPORT_API
316 int mm_sound_play_tone_with_stream_info(MMSoundTone_t tone, char *stream_type, int stream_id, const double volume, const int duration, int *handle)
317 {
318
319         int err = MM_ERROR_NONE;
320
321         err = mm_sound_client_play_tone_with_stream_info(tone, stream_type, stream_id, volume, duration, handle);
322         if (err < 0) {
323                 debug_error("Failed to play sound");
324                 return err;
325         }
326
327         return err;
328
329 }
330
331 ///////////////////////////////////
332 ////     MMSOUND ROUTING APIs
333 ///////////////////////////////////
334
335 EXPORT_API
336 int mm_sound_test(int a, int b, int* getv)
337 {
338         int ret = MM_ERROR_NONE;
339
340         debug_log("mm_sound_test enter");
341         if (!getv) {
342                 debug_error("argu null");
343                 return MM_ERROR_INVALID_ARGUMENT;
344         }
345         ret = mm_sound_client_test(a, b, getv);
346         if (ret < 0) {
347                 debug_error("Can not mm sound test, ret = %x", ret);
348         }
349         debug_log("mm_sound_test leave");
350
351         return ret;
352 }
353
354 EXPORT_API
355 int mm_sound_add_test_callback(mm_sound_test_cb func, void *user_data, unsigned int *subs_id)
356 {
357         int ret = MM_ERROR_NONE;
358
359         debug_log("enter");
360         if (!func || !subs_id) {
361                 debug_error("argument is not valid");
362                 return MM_ERROR_INVALID_ARGUMENT;
363         }
364
365         ret = mm_sound_client_add_test_callback(func, user_data, subs_id);
366         if (ret < 0) {
367                 debug_error("Can not add test callback, ret = %x", ret);
368         }
369         debug_log("leave");
370
371         return ret;
372 }
373
374 EXPORT_API
375 int mm_sound_remove_test_callback(unsigned int subs_id)
376 {
377         int ret = MM_ERROR_NONE;
378
379         debug_log("enter");
380         ret = mm_sound_client_remove_test_callback(subs_id);
381         if (ret < 0) {
382                 debug_error("Can not remove test callback, ret = %x", ret);
383         }
384         debug_log("leave");
385
386         return ret;
387 }
388
389 #ifdef TIZEN_TV
390 EXPORT_API
391 void mm_sound_dotnet_cleanup(int signo)
392 {
393         mm_sound_client_cleanup();
394 }
395 #endif
396
397 __attribute__ ((constructor))
398 static void _mm_sound_initialize(void)
399 {
400         mm_sound_client_initialize();
401         /* Will be Fixed */
402 }
403
404 __attribute__ ((destructor))
405 static void _mm_sound_finalize(void)
406 {
407         mm_sound_client_finalize();
408 }
409
410