Add internal volume API set
[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 #define MASTER_VOLUME_MAX 100
40 #define MASTER_VOLUME_MIN 0
41
42 #include <gio/gio.h>
43
44 #define MM_SOUND_DBUS_BUS_NAME_PREPIX  "org.tizen.MMSound"
45 #define MM_SOUND_DBUS_OBJECT_PATH  "/org/tizen/MMSound"
46 #define MM_SOUND_DBUS_INTERFACE    "org.tizen.mmsound"
47
48 static const char* _get_volume_str(volume_type_t type)
49 {
50         static const char *volume_type_str[VOLUME_TYPE_MAX] = {
51                 "SYSTEM", "NOTIFICATION", "ALARM", "RINGTONE", "MEDIA", "CALL", "VOIP", "VOICE", "FIXED"
52         };
53
54         return (type < VOLUME_TYPE_MAX) ? volume_type_str[type] : "Unknown";
55 }
56
57 static const char* _get_volume_str_internal(volume_type_internal_t type)
58 {
59         static const char *volume_type_str[] = {
60                 "BIXBY"
61         };
62
63         return (type < (VOLUME_TYPE_BIXBY + 1)) ? volume_type_str[type] : "Unknown";
64 }
65
66 EXPORT_API
67 int mm_sound_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* user_data, unsigned int *subs_id)
68 {
69         int ret = MM_ERROR_NONE;
70
71         if (func == NULL || subs_id == NULL) {
72                 debug_error("argument is not valid");
73                 return MM_ERROR_INVALID_ARGUMENT;
74         }
75
76         ret = mm_sound_client_add_volume_changed_callback(func, user_data, subs_id);
77         if (ret < 0) {
78                 debug_error("Can not add volume changed callback, ret = %x", ret);
79         }
80
81         return ret;
82 }
83
84 EXPORT_API
85 int mm_sound_remove_volume_changed_callback(unsigned int subs_id)
86 {
87         int ret = MM_ERROR_NONE;
88
89         ret = mm_sound_client_remove_volume_changed_callback(subs_id);
90         if (ret < 0) {
91                 debug_error("Can not remove volume changed callback, ret = %x", ret);
92         }
93
94         return ret;
95 }
96
97 EXPORT_API
98 int mm_sound_volume_set_value(volume_type_t type, const unsigned int value)
99 {
100         int ret = MM_ERROR_NONE;
101
102         /* request daemon to set volume */
103         ret = mm_sound_client_set_volume_by_type(type, value);
104         if (ret)
105                 debug_error("can not set volume, type(%s), value(%d), ret=0x%x", _get_volume_str(type), value, ret);
106         else
107                 debug_msg("set (%s) volume to (%d)",  _get_volume_str(type), value);
108
109         return ret;
110 }
111
112 EXPORT_API
113 int mm_sound_volume_get_value(volume_type_t type, unsigned int *value)
114 {
115         int ret = MM_ERROR_NONE;
116
117         ret = mm_sound_client_get_volume_by_type(type, value);
118         if (ret)
119                 debug_error("can not get volume, type(%s), ret=0x%x", _get_volume_str(type), ret);
120         else
121                 debug_msg("(%s) volume : %d",  _get_volume_str(type), *value);
122
123         return ret;
124 }
125
126 EXPORT_API
127 int mm_sound_add_volume_changed_callback_internal(mm_sound_volume_changed_cb_internal func, void* user_data, unsigned int *subs_id)
128 {
129         int ret = MM_ERROR_NONE;
130
131         if (func == NULL || subs_id == NULL) {
132                 debug_error("argument is not valid");
133                 return MM_ERROR_INVALID_ARGUMENT;
134         }
135
136         ret = mm_sound_client_add_volume_changed_callback_internal(func, user_data, subs_id);
137         if (ret < 0)
138                 debug_error("Can not add internal volume changed callback, ret = %x", ret);
139
140         return ret;
141 }
142
143 EXPORT_API
144 int mm_sound_remove_volume_changed_callback_internal(unsigned int subs_id)
145 {
146         int ret = MM_ERROR_NONE;
147
148         ret = mm_sound_client_remove_volume_changed_callback(subs_id);
149         if (ret < 0)
150                 debug_error("Can not remove internal volume changed callback, ret = %x", ret);
151
152         return ret;
153 }
154
155 EXPORT_API
156 int mm_sound_volume_set_value_internal(volume_type_internal_t type, const unsigned int value)
157 {
158         int ret = MM_ERROR_NONE;
159
160         /* request daemon to set volume */
161         ret = mm_sound_client_set_volume_by_internal_type(type, value);
162         if (ret)
163                 debug_error("can not set internal volume, type(%s), value(%d), ret=0x%x", _get_volume_str_internal(type), value, ret);
164         else
165                 debug_msg("set (%s) volume to (%d)",  _get_volume_str_internal(type), value);
166
167         return ret;
168 }
169
170 EXPORT_API
171 int mm_sound_volume_get_value_internal(volume_type_internal_t type, unsigned int *value)
172 {
173         int ret = MM_ERROR_NONE;
174
175         ret = mm_sound_client_get_volume_by_internal_type(type, value);
176         if (ret)
177                 debug_error("can not get internal volume, type(%s), ret=0x%x", _get_volume_str_internal(type), ret);
178         else
179                 debug_msg("(%s) volume : %d",  _get_volume_str_internal(type), *value);
180
181         return ret;
182 }
183
184 EXPORT_API
185 int mm_sound_set_mute(volume_type_t type, bool mute)
186 {
187         int ret = MM_ERROR_NONE;
188
189         ret = mm_sound_client_set_mute_by_type(type, mute);
190         if (ret)
191                 debug_error("can not set mute, type(%s), mute(%d), ret=0x%x", _get_volume_str(type), mute, ret);
192         else
193                 debug_msg("set (%s) mute to (%d)",  _get_volume_str(type), mute);
194
195         return ret;
196 }
197
198 EXPORT_API
199 int mm_sound_get_mute(volume_type_t type, bool  *muted)
200 {
201         int ret = MM_ERROR_NONE;
202
203         /* Check input param */
204         if (!muted) {
205                 debug_error("invalid argument");
206                 return MM_ERROR_INVALID_ARGUMENT;
207         }
208         if (type > VOLUME_TYPE_VOICE) {
209                 debug_error("invalid volume type value %d", type);
210                 return MM_ERROR_INVALID_ARGUMENT;
211         }
212
213         ret = mm_sound_client_get_mute_by_type(type, muted);
214         if (ret)
215                 debug_error("can not get mute, type(%s), ret=0x%x", _get_volume_str(type), ret);
216         else
217                 debug_msg("(%s) mute state : %d",  _get_volume_str(type), *muted);
218
219         return ret;
220 }
221
222 EXPORT_API
223 int mm_sound_set_filter(const char *stream_type, const char *filter_name, const char *filter_parameters, const char *filter_group)
224 {
225         /* Check input param */
226         if (!stream_type || !filter_name) {
227                 debug_error("invalid argument");
228                 return MM_ERROR_INVALID_ARGUMENT;
229         }
230         if (!filter_parameters)
231                 filter_parameters = "";
232         if (!filter_group)
233                 filter_group = "default";
234
235         debug_msg("stream_type(%s), filter_name(%s), filter_parameters(%s), filter_group(%s)", stream_type, filter_name, filter_parameters, filter_group);
236
237         return mm_sound_client_set_filter_by_type(stream_type, filter_name, filter_parameters, filter_group);
238 }
239
240 EXPORT_API
241 int mm_sound_unset_filter(const char *stream_type)
242 {
243         /* Check input param */
244         if (!stream_type) {
245                 debug_error("invalid argument");
246                 return MM_ERROR_INVALID_ARGUMENT;
247         }
248
249         debug_msg("stream_type(%s)", stream_type);
250
251         return mm_sound_client_unset_filter_by_type(stream_type);
252 }
253
254 EXPORT_API
255 int mm_sound_control_filter(const char *stream_type, const char *filter_name, const char *filter_controls)
256 {
257         /* Check input param */
258         if (!stream_type || !filter_name || !filter_controls) {
259                 debug_error("invalid argument");
260                 return MM_ERROR_INVALID_ARGUMENT;
261         }
262
263         debug_msg("stream_type(%s), filter_name(%s), filter_controls(%s)", stream_type, filter_name, filter_controls);
264
265         return mm_sound_client_control_filter_by_type(stream_type, filter_name, filter_controls);
266 }
267
268 ///////////////////////////////////
269 ////     MMSOUND PLAY APIs
270 ///////////////////////////////////
271 EXPORT_API
272 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)
273 {
274         MMSoundPlayParam param = { 0, };
275         int err;
276
277         param.filename = filename;
278         param.volume = 0; //volume value dose not effect anymore
279         param.callback = callback;
280         param.data = data;
281
282         if (loop == 0)
283                 param.loop = -1;
284         else
285                 param.loop = loop;
286
287         err = mm_sound_client_play_sound_with_stream_info(&param, handle, stream_type, stream_id);
288         if (err < 0) {
289                 debug_error("Failed to play sound");
290                 return err;
291         }
292
293         debug_warning("success : handle=[%p]", handle);
294
295         return MM_ERROR_NONE;
296
297 }
298
299
300 EXPORT_API
301 int mm_sound_stop_sound(int handle)
302 {
303         int err;
304
305         debug_warning("enter : handle=[%d]", handle);
306         /* Stop sound */
307         err = mm_sound_client_stop_sound(handle);
308         if (err < 0) {
309                 debug_error("Fail to stop sound");
310                 return err;
311         }
312         debug_warning("success : handle=[%d]", handle);
313
314         return MM_ERROR_NONE;
315 }
316
317 ///////////////////////////////////
318 ////     MMSOUND TONE APIs
319 ///////////////////////////////////
320 EXPORT_API
321 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)
322 {
323
324         int err = MM_ERROR_NONE;
325
326         err = mm_sound_client_play_tone_with_stream_info(tone, stream_type, stream_id, volume, duration, handle);
327         if (err < 0) {
328                 debug_error("Failed to play sound");
329                 return err;
330         }
331
332         return err;
333
334 }
335
336 ///////////////////////////////////
337 ////     MMSOUND ROUTING APIs
338 ///////////////////////////////////
339
340 EXPORT_API
341 int mm_sound_test(int a, int b, int* getv)
342 {
343         int ret = MM_ERROR_NONE;
344
345         debug_log("mm_sound_test enter");
346         if (!getv) {
347                 debug_error("argu null");
348                 return MM_ERROR_INVALID_ARGUMENT;
349         }
350         ret = mm_sound_client_test(a, b, getv);
351         if (ret < 0) {
352                 debug_error("Can not mm sound test, ret = %x", ret);
353         }
354         debug_log("mm_sound_test leave");
355
356         return ret;
357 }
358
359 EXPORT_API
360 int mm_sound_add_test_callback(mm_sound_test_cb func, void *user_data, unsigned int *subs_id)
361 {
362         int ret = MM_ERROR_NONE;
363
364         debug_log("enter");
365         if (!func || !subs_id) {
366                 debug_error("argument is not valid");
367                 return MM_ERROR_INVALID_ARGUMENT;
368         }
369
370         ret = mm_sound_client_add_test_callback(func, user_data, subs_id);
371         if (ret < 0) {
372                 debug_error("Can not add test callback, ret = %x", ret);
373         }
374         debug_log("leave");
375
376         return ret;
377 }
378
379 EXPORT_API
380 int mm_sound_remove_test_callback(unsigned int subs_id)
381 {
382         int ret = MM_ERROR_NONE;
383
384         debug_log("enter");
385         ret = mm_sound_client_remove_test_callback(subs_id);
386         if (ret < 0) {
387                 debug_error("Can not remove test callback, ret = %x", ret);
388         }
389         debug_log("leave");
390
391         return ret;
392 }
393
394 EXPORT_API
395 int mm_sound_add_ducking_state_changed_callback(mm_sound_ducking_state_changed_cb func, void *user_data, unsigned int *subs_id)
396 {
397         int ret = MM_ERROR_NONE;
398
399         if (func == NULL || subs_id == NULL) {
400                 debug_error("argument is not valid");
401                 return MM_ERROR_INVALID_ARGUMENT;
402         }
403
404         ret = mm_sound_client_add_ducking_state_changed_callback(func, user_data, subs_id);
405         if (ret < 0) {
406                 debug_error("Can not add ducking state changed callback, ret = %x", ret);
407         }
408
409         return ret;
410 }
411
412 EXPORT_API
413 int mm_sound_remove_ducking_state_changed_callback(unsigned int subs_id)
414 {
415         int ret = MM_ERROR_NONE;
416
417         ret = mm_sound_client_remove_ducking_state_changed_callback(subs_id);
418         if (ret < 0) {
419                 debug_error("Can not remove ducking state changed callback, ret = %x", ret);
420         }
421
422         return ret;
423 }
424
425 #ifdef TIZEN_TV
426 EXPORT_API
427 void mm_sound_dotnet_cleanup(int signo)
428 {
429 }
430 #endif
431
432 __attribute__ ((constructor))
433 static void _mm_sound_initialize(void)
434 {
435         mm_sound_client_initialize();
436         /* Will be Fixed */
437 }
438
439 __attribute__ ((destructor))
440 static void _mm_sound_finalize(void)
441 {
442         mm_sound_client_finalize();
443 }
444
445