9179195ef3dbaa4192dbacd7d943bd9c9c7d9e36
[framework/api/sound-manager.git] / src / sound_manager.c
1 /*
2 * Copyright (c) 2011 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 #define LOG_TAG "TIZEN_N_SOUND_MANGER"
19
20 #include <sound_manager.h>
21 #include <mm_sound.h>
22 #include <mm_sound_private.h>
23 #include <stdio.h>
24 #include <limits.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <dlog.h>
28 #include <mm_session.h>
29
30 #define MAX_VOLUME_TYPE 5
31
32 typedef struct {
33         void *user_data;
34         sound_manager_volume_changed_cb user_cb;
35 }_changed_volume_info_s;
36
37 typedef struct {
38         void *user_data;
39         sound_manager_route_policy_changed_cb user_cb;
40 }_changed_policy_info_s;
41
42 typedef struct {
43         int is_registered;
44         void *user_data;
45         sound_session_notify_cb user_cb;
46 }_session_notify_info_s;
47
48 static _changed_volume_info_s g_volume_changed_cb_table;
49 static _changed_policy_info_s g_policy_changed_cb_table;
50 static _session_notify_info_s g_session_notify_cb_table = {0, NULL, NULL };
51
52 static void __volume_changed_cb(void *user_data)
53 {
54         sound_type_e  type = (sound_type_e)user_data;
55         
56         int new_volume;
57         sound_manager_get_volume(       type, &new_volume);
58         if( g_volume_changed_cb_table.user_cb )
59                 (g_volume_changed_cb_table.user_cb)(type, new_volume, g_volume_changed_cb_table.user_data);
60 }
61
62 static void __policy_changed_cb(void *user_data,  system_audio_route_t policy){
63         if( g_policy_changed_cb_table.user_cb)
64                 g_policy_changed_cb_table.user_cb(policy, g_policy_changed_cb_table.user_data);
65 }
66
67 static void __session_notify_cb(session_msg_t msg, void *user_data){
68         if( g_session_notify_cb_table.user_cb ){
69                 g_session_notify_cb_table.user_cb(msg, g_session_notify_cb_table.user_data);
70         }
71 }
72
73
74 static int __convert_sound_manager_error_code(const char *func, int code){
75         int ret = SOUND_MANAGER_ERROR_NONE;
76         char *errorstr = NULL;
77         
78         switch(code)
79         {
80                 case SOUND_MANAGER_ERROR_INVALID_PARAMETER:
81                         ret = SOUND_MANAGER_ERROR_INVALID_PARAMETER;
82                         errorstr = "INVALID_PARAMETER";
83                         break;
84                 case MM_ERROR_NONE:
85                         ret = SOUND_MANAGER_ERROR_NONE;
86                         errorstr = "ERROR_NONE";
87                         break;
88                 case MM_ERROR_INVALID_ARGUMENT: 
89                 case MM_ERROR_SOUND_INVALID_POINTER:
90                         ret = SOUND_MANAGER_ERROR_INVALID_PARAMETER;
91                         errorstr = "INVALID_PARAMETER";
92                         break;
93                 case MM_ERROR_SOUND_INTERNAL:
94                         ret = SOUND_MANAGER_ERROR_INVALID_OPERATION;
95                         errorstr = "INVALID_OPERATION"  ;               
96                         break;
97                 case MM_ERROR_SOUND_VOLUME_NO_INSTANCE:
98                 case MM_ERROR_SOUND_VOLUME_CAPTURE_ONLY:
99                         ret = SOUND_MANAGER_ERROR_NO_PLAYING_SOUND;
100                         errorstr = "NO_PLAYING_SOUND"   ;               
101                         break;
102         }       
103         LOGE( "[%s] %s(0x%08x) : core frameworks error code(0x%08x)",func, errorstr, ret, code);
104         return ret;
105 }
106
107
108
109
110 int sound_manager_get_max_volume(sound_type_e type, int *max)
111 {
112         int volume;
113         if( max == NULL )
114                 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
115         
116         if( type > MAX_VOLUME_TYPE || type < 0 )
117                 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
118         int ret = mm_sound_volume_get_step(type, &volume);
119
120         if( ret == 0 )
121                 *max = volume -1;       // actual volume step can be max step - 1
122         
123         return __convert_sound_manager_error_code(__func__, ret);
124 }
125
126 int sound_manager_set_volume(sound_type_e type, int volume)
127 {
128         if( type > MAX_VOLUME_TYPE || type < 0 )
129                 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
130         if( volume < 0 )
131                 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
132
133         int ret = mm_sound_volume_set_value(type, volume);
134         
135         return __convert_sound_manager_error_code(__func__, ret);
136 }
137
138 int sound_manager_get_volume(sound_type_e type, int *volume)
139 {
140         unsigned int uvolume;
141         if( type > MAX_VOLUME_TYPE || type < 0 )
142                 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
143         if( volume == NULL)
144                 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
145         int ret = mm_sound_volume_get_value(type, &uvolume);
146
147         if( ret == 0)
148                 *volume = uvolume;
149                 
150         return __convert_sound_manager_error_code(__func__, ret);
151 }
152
153 int sound_manager_set_route_policy (sound_route_policy_e route)
154 {
155         if( route < 0 || route > SOUND_ROUTE_HANDSET_ONLY)
156                 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
157
158         int ret;
159         ret = mm_sound_route_set_system_policy(route);
160         
161         return __convert_sound_manager_error_code(__func__, ret);
162 }
163
164 int sound_manager_get_route_policy (sound_route_policy_e *route)
165 {
166         if( route == NULL)
167                 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
168         int ret;
169         ret= mm_sound_route_get_system_policy((system_audio_route_t *)route);
170         
171         return __convert_sound_manager_error_code(__func__, ret);
172 }
173
174 int sound_manager_get_current_sound_type(sound_type_e *type)
175 {
176         if( type == NULL)
177                 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
178         int ret;
179         ret = mm_sound_volume_get_current_playing_type((volume_type_t *)type);
180         
181         return __convert_sound_manager_error_code(__func__, ret);
182 }
183
184 int sound_manager_get_current_sound_device(sound_device_e *device)
185 {
186         if( device == NULL)
187                 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
188         int ret;
189         ret = mm_sound_route_get_playing_device((system_audio_route_device_t*)device);
190         return __convert_sound_manager_error_code(__func__, ret);
191 }
192
193 int sound_manager_set_volume_changed_cb(sound_manager_volume_changed_cb callback, void* user_data)
194 {
195         if( callback == NULL )
196                 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
197         int i;
198         g_volume_changed_cb_table.user_cb = callback;
199         g_volume_changed_cb_table.user_data = user_data;
200         for(i = 0 ; i <= MAX_VOLUME_TYPE ; i++)
201         {
202                 mm_sound_volume_add_callback( i , __volume_changed_cb ,(void*) i);
203         }
204         return 0;
205 }
206
207 void sound_manager_unset_volume_changed_cb(void)
208 {
209         int i;
210         for(i = 0 ; i <= MAX_VOLUME_TYPE ; i++)
211         {
212                 mm_sound_volume_remove_callback( i );
213         }
214         g_volume_changed_cb_table.user_cb = NULL;
215         g_volume_changed_cb_table.user_data = NULL;     
216 }
217
218 int sound_manager_set_route_policy_changed_cb(sound_manager_route_policy_changed_cb callback, void* user_data)
219 {
220         if( callback == NULL)
221                 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
222         g_policy_changed_cb_table.user_cb = callback;
223         g_policy_changed_cb_table.user_data = user_data;        
224         mm_sound_route_add_change_callback(__policy_changed_cb, user_data);
225         return 0;
226 }
227
228 void sound_manager_unset_route_policy_changed_cb(void)
229 {
230         mm_sound_route_remove_change_callback();
231         g_policy_changed_cb_table.user_cb = NULL;
232         g_policy_changed_cb_table.user_data = NULL;             
233 }
234
235 int sound_manager_get_a2dp_status(bool *connected , char** bt_name){
236         int ret;
237         int connect;
238         ret = mm_sound_route_get_a2dp_status(&connect , bt_name );
239         if( ret == 0 )
240                 *connected = connect;
241         return __convert_sound_manager_error_code(__func__, ret);
242 }
243
244
245
246 int sound_manager_set_session_type(sound_session_type_e type){
247         int ret = 0;
248         if( type < 0 || type >  SOUND_SESSION_TYPE_EXCLUSIVE )
249                 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
250                 
251         if( g_session_notify_cb_table.is_registered ){
252                 mm_session_finish();
253                 g_session_notify_cb_table.is_registered = 0;
254         }
255
256         ret = mm_session_init_ex( type , __session_notify_cb, NULL);
257         if( ret == 0 ){
258                 g_session_notify_cb_table.is_registered = 1;
259         }
260         return __convert_sound_manager_error_code(__func__, ret);
261 }
262
263 int sound_manager_set_session_notify_cb(sound_session_notify_cb callback , void *user_data){
264         int ret =0 ;
265         if( callback == NULL)
266                 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
267         
268         
269         if( g_session_notify_cb_table.is_registered ==0 ){
270                 ret = mm_session_init_ex( SOUND_SESSION_TYPE_SHARE /*default*/ , __session_notify_cb, NULL);
271                 if( ret != 0 )
272                         return __convert_sound_manager_error_code(__func__, ret);
273                 g_session_notify_cb_table.is_registered = 1;
274         }
275
276         g_session_notify_cb_table.user_cb = callback ; 
277         g_session_notify_cb_table.user_data  = user_data;
278         return SOUND_MANAGER_ERROR_NONE;
279 }
280
281 void sound_manager_unset_session_notify_cb(void){
282         g_session_notify_cb_table.user_cb = NULL; 
283         g_session_notify_cb_table.user_data  = NULL;
284 }
285
286
287 int sound_manager_set_volume_key_type(volume_key_type_e type){
288         if( type < VOLUME_KEY_TYPE_NONE || type > VOLUME_KEY_TYPE_CALL )
289                 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
290         int ret;
291         if( type == VOLUME_KEY_TYPE_NONE )
292                 ret = mm_sound_volume_primary_type_clear();
293         else
294                 ret = mm_sound_volume_primary_type_set(type);
295
296         return __convert_sound_manager_error_code(__func__, ret);
297 }
298