Add new functions to support ducking state changed callback
[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 EXPORT_API
58 int mm_sound_volume_remove_callback(volume_type_t type)
59 {
60         /* FIXME : Will be removed */
61         return MM_ERROR_NOT_SUPPORT_API;
62 }
63
64 EXPORT_API
65 int mm_sound_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* user_data, unsigned int *subs_id)
66 {
67         int ret = MM_ERROR_NONE;
68
69         if (func == NULL || subs_id == NULL) {
70                 debug_error("argument is not valid");
71                 return MM_ERROR_INVALID_ARGUMENT;
72         }
73
74         ret = mm_sound_client_add_volume_changed_callback(func, user_data, subs_id);
75         if (ret < 0) {
76                 debug_error("Can not add volume changed callback, ret = %x", ret);
77         }
78
79         return ret;
80 }
81
82 EXPORT_API
83 int mm_sound_remove_volume_changed_callback(unsigned int subs_id)
84 {
85         int ret = MM_ERROR_NONE;
86
87         ret = mm_sound_client_remove_volume_changed_callback(subs_id);
88         if (ret < 0) {
89                 debug_error("Can not remove volume changed callback, ret = %x", ret);
90         }
91
92         return ret;
93 }
94
95 EXPORT_API
96 int mm_sound_volume_set_value(volume_type_t type, const unsigned int value)
97 {
98         int ret = MM_ERROR_NONE;
99
100         /* request daemon to set volume */
101         ret = mm_sound_client_set_volume_by_type(type, value);
102         if (ret)
103                 debug_error("can not set volume, type(%s), value(%d), ret=0x%x", _get_volume_str(type), value, ret);
104         else
105                 debug_msg("set (%s) volume to (%d)",  _get_volume_str(type), value);
106
107         return ret;
108 }
109
110 EXPORT_API
111 int mm_sound_volume_get_value(volume_type_t type, unsigned int *value)
112 {
113         int ret = MM_ERROR_NONE;
114
115         ret = mm_sound_client_get_volume_by_type(type, value);
116         if (ret)
117                 debug_error("can not get volume, type(%s), ret=0x%x", _get_volume_str(type), ret);
118         else
119                 debug_msg("(%s) volume : %d",  _get_volume_str(type), *value);
120
121         return ret;
122 }
123
124 EXPORT_API
125 int mm_sound_set_mute(volume_type_t type, bool mute)
126 {
127         int ret = MM_ERROR_NONE;
128
129         ret = mm_sound_client_set_mute_by_type(type, mute);
130         if (ret)
131                 debug_error("can not set mute, type(%s), mute(%d), ret=0x%x", _get_volume_str(type), mute, ret);
132         else
133                 debug_msg("set (%s) mute to (%d)",  _get_volume_str(type), mute);
134
135         return ret;
136 }
137
138 EXPORT_API
139 int mm_sound_get_mute(volume_type_t type, bool  *muted)
140 {
141         int ret = MM_ERROR_NONE;
142
143         /* Check input param */
144         if (!muted) {
145                 debug_error("invalid argument");
146                 return MM_ERROR_INVALID_ARGUMENT;
147         }
148         if (type > VOLUME_TYPE_VOICE) {
149                 debug_error("invalid volume type value %d", type);
150                 return MM_ERROR_INVALID_ARGUMENT;
151         }
152
153         ret = mm_sound_client_get_mute_by_type(type, muted);
154         if (ret)
155                 debug_error("can not get mute, type(%s), ret=0x%x", _get_volume_str(type), ret);
156         else
157                 debug_msg("(%s) mute state : %d",  _get_volume_str(type), *muted);
158
159         return ret;
160 }
161
162 EXPORT_API
163 int mm_sound_set_filter(const char *stream_type, const char *filter_name, const char *filter_parameters, const char *filter_group)
164 {
165         /* Check input param */
166         if (!stream_type || !filter_name) {
167                 debug_error("invalid argument");
168                 return MM_ERROR_INVALID_ARGUMENT;
169         }
170         if (!filter_parameters)
171                 filter_parameters = "";
172         if (!filter_group)
173                 filter_group = "default";
174
175         debug_msg("stream_type(%s), filter_name(%s), filter_parameters(%s), filter_group(%s)", stream_type, filter_name, filter_parameters, filter_group);
176
177         return mm_sound_client_set_filter_by_type(stream_type, filter_name, filter_parameters, filter_group);
178 }
179
180 EXPORT_API
181 int mm_sound_unset_filter(const char *stream_type)
182 {
183         /* Check input param */
184         if (!stream_type) {
185                 debug_error("invalid argument");
186                 return MM_ERROR_INVALID_ARGUMENT;
187         }
188
189         debug_msg("stream_type(%s)", stream_type);
190
191         return mm_sound_client_unset_filter_by_type(stream_type);
192 }
193
194 EXPORT_API
195 int mm_sound_control_filter(const char *stream_type, const char *filter_name, const char *filter_controls)
196 {
197         /* Check input param */
198         if (!stream_type || !filter_name || !filter_controls) {
199                 debug_error("invalid argument");
200                 return MM_ERROR_INVALID_ARGUMENT;
201         }
202
203         debug_msg("stream_type(%s), filter_name(%s), filter_controls(%s)", stream_type, filter_name, filter_controls);
204
205         return mm_sound_client_control_filter_by_type(stream_type, filter_name, filter_controls);
206 }
207
208 ///////////////////////////////////
209 ////     MMSOUND PLAY APIs
210 ///////////////////////////////////
211 EXPORT_API
212 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)
213 {
214         MMSoundPlayParam param = { 0, };
215         int err;
216
217         param.filename = filename;
218         param.volume = 0; //volume value dose not effect anymore
219         param.callback = callback;
220         param.data = data;
221
222         if (loop == 0)
223                 param.loop = -1;
224         else
225                 param.loop = loop;
226
227         err = mm_sound_client_play_sound_with_stream_info(&param, handle, stream_type, stream_id);
228         if (err < 0) {
229                 debug_error("Failed to play sound");
230                 return err;
231         }
232
233         debug_warning("success : handle=[%p]", handle);
234
235         return MM_ERROR_NONE;
236
237 }
238
239
240 EXPORT_API
241 int mm_sound_stop_sound(int handle)
242 {
243         int err;
244
245         debug_warning("enter : handle=[%d]", handle);
246         /* Stop sound */
247         err = mm_sound_client_stop_sound(handle);
248         if (err < 0) {
249                 debug_error("Fail to stop sound");
250                 return err;
251         }
252         debug_warning("success : handle=[%d]", handle);
253
254         return MM_ERROR_NONE;
255 }
256
257 ///////////////////////////////////
258 ////     MMSOUND TONE APIs
259 ///////////////////////////////////
260 EXPORT_API
261 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)
262 {
263
264         int err = MM_ERROR_NONE;
265
266         err = mm_sound_client_play_tone_with_stream_info(tone, stream_type, stream_id, volume, duration, handle);
267         if (err < 0) {
268                 debug_error("Failed to play sound");
269                 return err;
270         }
271
272         return err;
273
274 }
275
276 ///////////////////////////////////
277 ////     MMSOUND ROUTING APIs
278 ///////////////////////////////////
279
280 EXPORT_API
281 int mm_sound_test(int a, int b, int* getv)
282 {
283         int ret = MM_ERROR_NONE;
284
285         debug_log("mm_sound_test enter");
286         if (!getv) {
287                 debug_error("argu null");
288                 return MM_ERROR_INVALID_ARGUMENT;
289         }
290         ret = mm_sound_client_test(a, b, getv);
291         if (ret < 0) {
292                 debug_error("Can not mm sound test, ret = %x", ret);
293         }
294         debug_log("mm_sound_test leave");
295
296         return ret;
297 }
298
299 EXPORT_API
300 int mm_sound_add_test_callback(mm_sound_test_cb func, void *user_data, unsigned int *subs_id)
301 {
302         int ret = MM_ERROR_NONE;
303
304         debug_log("enter");
305         if (!func || !subs_id) {
306                 debug_error("argument is not valid");
307                 return MM_ERROR_INVALID_ARGUMENT;
308         }
309
310         ret = mm_sound_client_add_test_callback(func, user_data, subs_id);
311         if (ret < 0) {
312                 debug_error("Can not add test callback, ret = %x", ret);
313         }
314         debug_log("leave");
315
316         return ret;
317 }
318
319 EXPORT_API
320 int mm_sound_remove_test_callback(unsigned int subs_id)
321 {
322         int ret = MM_ERROR_NONE;
323
324         debug_log("enter");
325         ret = mm_sound_client_remove_test_callback(subs_id);
326         if (ret < 0) {
327                 debug_error("Can not remove test callback, ret = %x", ret);
328         }
329         debug_log("leave");
330
331         return ret;
332 }
333
334 EXPORT_API
335 int mm_sound_add_ducking_state_changed_callback(mm_sound_ducking_state_changed_cb func, void *user_data, unsigned int *subs_id)
336 {
337         int ret = MM_ERROR_NONE;
338
339         if (func == NULL || subs_id == NULL) {
340                 debug_error("argument is not valid");
341                 return MM_ERROR_INVALID_ARGUMENT;
342         }
343
344         ret = mm_sound_client_add_ducking_state_changed_callback(func, user_data, subs_id);
345         if (ret < 0) {
346                 debug_error("Can not add ducking state changed callback, ret = %x", ret);
347         }
348
349         return ret;
350 }
351
352 EXPORT_API
353 int mm_sound_remove_ducking_state_changed_callback(unsigned int subs_id)
354 {
355         int ret = MM_ERROR_NONE;
356
357         ret = mm_sound_client_remove_ducking_state_changed_callback(subs_id);
358         if (ret < 0) {
359                 debug_error("Can not remove ducking state changed callback, ret = %x", ret);
360         }
361
362         return ret;
363 }
364
365 #ifdef TIZEN_TV
366 EXPORT_API
367 void mm_sound_dotnet_cleanup(int signo)
368 {
369 }
370 #endif
371
372 __attribute__ ((constructor))
373 static void _mm_sound_initialize(void)
374 {
375         mm_sound_client_initialize();
376         /* Will be Fixed */
377 }
378
379 __attribute__ ((destructor))
380 static void _mm_sound_finalize(void)
381 {
382         mm_sound_client_finalize();
383 }
384
385