2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #define LOG_TAG "TIZEN_N_SOUND_MANGER"
20 #include <sound_manager.h>
22 #include <mm_sound_private.h>
29 #include <mm_session.h>
30 #include <mm_session_private.h>
32 #define MAX_VOLUME_TYPE 5
36 sound_manager_volume_changed_cb user_cb;
37 }_changed_volume_info_s;
41 sound_manager_route_policy_changed_cb user_cb;
42 }_changed_policy_info_s;
47 sound_session_notify_cb user_cb;
48 void *interrupted_user_data;
49 sound_interrupted_cb interrupted_cb;
50 }_session_notify_info_s;
54 sound_available_route_changed_cb user_cb;
55 }_changed_available_route_info_s;
59 sound_active_device_changed_cb user_cb;
60 }_changed_active_device_info_s;
62 static _changed_volume_info_s g_volume_changed_cb_table;
63 static _changed_policy_info_s g_policy_changed_cb_table;
64 static _session_notify_info_s g_session_notify_cb_table = {0, NULL, NULL, NULL, NULL};
66 static void __volume_changed_cb(void *user_data)
68 sound_type_e type = (sound_type_e)user_data;
71 sound_manager_get_volume(type, &new_volume);
72 if(g_volume_changed_cb_table.user_cb)
73 (g_volume_changed_cb_table.user_cb)(type, new_volume, g_volume_changed_cb_table.user_data);
76 static void __policy_changed_cb(void *user_data, system_audio_route_t policy){
77 if( g_policy_changed_cb_table.user_cb)
78 g_policy_changed_cb_table.user_cb(policy, g_policy_changed_cb_table.user_data);
81 static void __session_notify_cb(session_msg_t msg, session_event_t event, void *user_data){
82 if(g_session_notify_cb_table.user_cb){
83 g_session_notify_cb_table.user_cb(msg, g_session_notify_cb_table.user_data);
85 if( g_session_notify_cb_table.interrupted_cb ){
86 sound_interrupted_code_e e = SOUND_INTERRUPTED_COMPLETED;
87 if( msg == MM_SESSION_MSG_STOP )
88 e = SOUND_INTERRUPTED_COMPLETED;
91 case MM_SESSION_EVENT_OTHER_APP :
92 e = SOUND_INTERRUPTED_BY_OTHER_APP;
94 case MM_SESSION_EVENT_CALL :
95 e = SOUND_INTERRUPTED_BY_CALL;
97 case MM_SESSION_EVENT_ALARM :
98 e = SOUND_INTERRUPTED_BY_ALARM;
100 case MM_SESSION_EVENT_EARJACK_UNPLUG:
101 e = SOUND_INTERRUPTED_BY_EARJACK_UNPLUG;
103 case MM_SESSION_EVENT_RESOURCE_CONFLICT:
104 e = SOUND_INTERRUPTED_BY_RESOURCE_CONFLICT;
107 e = SOUND_INTERRUPTED_BY_OTHER_APP;
111 g_session_notify_cb_table.interrupted_cb(e, g_session_notify_cb_table.interrupted_user_data);
115 static int __convert_sound_manager_error_code(const char *func, int code){
116 int ret = SOUND_MANAGER_ERROR_NONE;
117 char *errorstr = NULL;
121 case SOUND_MANAGER_ERROR_INVALID_PARAMETER:
122 ret = SOUND_MANAGER_ERROR_INVALID_PARAMETER;
123 errorstr = "INVALID_PARAMETER";
126 ret = SOUND_MANAGER_ERROR_NONE;
127 errorstr = "ERROR_NONE";
129 case MM_ERROR_INVALID_ARGUMENT:
130 case MM_ERROR_SOUND_INVALID_POINTER:
131 ret = SOUND_MANAGER_ERROR_INVALID_PARAMETER;
132 errorstr = "INVALID_PARAMETER";
134 case MM_ERROR_SOUND_INTERNAL:
135 ret = SOUND_MANAGER_ERROR_INVALID_OPERATION;
136 errorstr = "INVALID_OPERATION" ;
138 case MM_ERROR_SOUND_VOLUME_NO_INSTANCE:
139 case MM_ERROR_SOUND_VOLUME_CAPTURE_ONLY:
140 ret = SOUND_MANAGER_ERROR_NO_PLAYING_SOUND;
141 errorstr = "NO_PLAYING_SOUND" ;
144 LOGE("[%s] %s(0x%08x) : core frameworks error code(0x%08x)",func, errorstr, ret, code);
148 int sound_manager_get_max_volume(sound_type_e type, int *max)
152 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
154 if(type > MAX_VOLUME_TYPE || type < 0)
155 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
156 int ret = mm_sound_volume_get_step(type, &volume);
159 *max = volume -1; // actual volume step can be max step - 1
161 return __convert_sound_manager_error_code(__func__, ret);
164 int sound_manager_set_volume(sound_type_e type, int volume)
166 if(type > MAX_VOLUME_TYPE || type < 0)
167 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
169 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
171 int ret = mm_sound_volume_set_value(type, volume);
173 return __convert_sound_manager_error_code(__func__, ret);
176 int sound_manager_get_volume(sound_type_e type, int *volume)
178 unsigned int uvolume;
179 if(type > MAX_VOLUME_TYPE || type < 0)
180 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
182 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
183 int ret = mm_sound_volume_get_value(type, &uvolume);
188 return __convert_sound_manager_error_code(__func__, ret);
191 int sound_manager_set_route_policy (sound_route_policy_e route)
193 if( route < 0 || route > SOUND_ROUTE_HANDSET_ONLY)
194 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
197 ret = mm_sound_route_set_system_policy(route);
199 return __convert_sound_manager_error_code(__func__, ret);
202 int sound_manager_get_route_policy (sound_route_policy_e *route)
205 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
207 ret= mm_sound_route_get_system_policy((system_audio_route_t *)route);
209 return __convert_sound_manager_error_code(__func__, ret);
212 int sound_manager_get_current_sound_type(sound_type_e *type)
215 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
217 ret = mm_sound_volume_get_current_playing_type((volume_type_t *)type);
219 return __convert_sound_manager_error_code(__func__, ret);
222 int sound_manager_get_current_sound_device(sound_device_e *device)
225 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
227 ret = mm_sound_route_get_playing_device((system_audio_route_device_t*)device);
228 return __convert_sound_manager_error_code(__func__, ret);
231 int sound_manager_set_volume_changed_cb(sound_manager_volume_changed_cb callback, void* user_data)
234 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
236 g_volume_changed_cb_table.user_cb = callback;
237 g_volume_changed_cb_table.user_data = user_data;
238 for(i = 0 ; i <= MAX_VOLUME_TYPE ; i++)
240 mm_sound_volume_add_callback(i , __volume_changed_cb ,(void*) i);
245 void sound_manager_unset_volume_changed_cb(void)
248 for(i = 0 ; i <= MAX_VOLUME_TYPE ; i++)
250 mm_sound_volume_remove_callback(i);
252 g_volume_changed_cb_table.user_cb = NULL;
253 g_volume_changed_cb_table.user_data = NULL;
256 int sound_manager_set_route_policy_changed_cb(sound_manager_route_policy_changed_cb callback, void* user_data)
258 if( callback == NULL)
259 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
260 g_policy_changed_cb_table.user_cb = callback;
261 g_policy_changed_cb_table.user_data = user_data;
262 mm_sound_route_add_change_callback(__policy_changed_cb, user_data);
266 void sound_manager_unset_route_policy_changed_cb(void)
268 mm_sound_route_remove_change_callback();
269 g_policy_changed_cb_table.user_cb = NULL;
270 g_policy_changed_cb_table.user_data = NULL;
273 int sound_manager_get_a2dp_status(bool *connected , char** bt_name){
274 int ret = mm_sound_route_get_a2dp_status((int*)connected , bt_name);
276 return __convert_sound_manager_error_code(__func__, ret);
279 int sound_manager_set_session_type(sound_session_type_e type){
281 if(type < 0 || type > SOUND_SESSION_TYPE_EXCLUSIVE)
282 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
284 if(g_session_notify_cb_table.is_registered){
286 g_session_notify_cb_table.is_registered = 0;
289 ret = mm_session_init_ex(type , __session_notify_cb, NULL);
291 g_session_notify_cb_table.is_registered = 1;
293 return __convert_sound_manager_error_code(__func__, ret);
296 int sound_manager_set_session_notify_cb(sound_session_notify_cb callback , void *user_data){
299 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
302 if(g_session_notify_cb_table.is_registered ==0){
303 ret = mm_session_init_ex(SOUND_SESSION_TYPE_SHARE /*default*/ , __session_notify_cb, NULL);
305 return __convert_sound_manager_error_code(__func__, ret);
306 g_session_notify_cb_table.is_registered = 1;
309 g_session_notify_cb_table.user_cb = callback;
310 g_session_notify_cb_table.user_data = user_data;
311 return SOUND_MANAGER_ERROR_NONE;
314 void sound_manager_unset_session_notify_cb(void){
315 g_session_notify_cb_table.user_cb = NULL;
316 g_session_notify_cb_table.user_data = NULL;
319 int sound_manager_set_interrupted_cb(sound_interrupted_cb callback, void *user_data){
322 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
324 if(g_session_notify_cb_table.is_registered ==0){
325 ret = mm_session_init_ex(SOUND_SESSION_TYPE_SHARE /*default*/ , __session_notify_cb, NULL);
327 return __convert_sound_manager_error_code(__func__, ret);
328 g_session_notify_cb_table.is_registered = 1;
331 g_session_notify_cb_table.interrupted_cb= callback;
332 g_session_notify_cb_table.interrupted_user_data = user_data;
333 return SOUND_MANAGER_ERROR_NONE;
336 void sound_manager_unset_interrupted_cb(void){
337 g_session_notify_cb_table.interrupted_cb= NULL;
338 g_session_notify_cb_table.interrupted_user_data = NULL;
342 int sound_manager_set_volume_key_type(volume_key_type_e type){
343 if(type < VOLUME_KEY_TYPE_NONE || type > VOLUME_KEY_TYPE_CALL)
344 return __convert_sound_manager_error_code(__func__, SOUND_MANAGER_ERROR_INVALID_PARAMETER);
346 if(type == VOLUME_KEY_TYPE_NONE)
347 ret = mm_sound_volume_primary_type_clear();
349 ret = mm_sound_volume_primary_type_set(type);
351 return __convert_sound_manager_error_code(__func__, ret);
354 int sound_manager_foreach_available_route (sound_available_route_cb callback, void *user_data)
357 ret = mm_sound_foreach_available_route_cb((mm_sound_available_route_cb)callback, user_data);
359 return __convert_sound_manager_error_code(__func__, ret);
362 int sound_manager_set_active_route (sound_route_e route)
365 ret = mm_sound_set_active_route(route);
367 return __convert_sound_manager_error_code(__func__, ret);
370 int sound_manager_get_active_device (sound_device_in_e *in, sound_device_out_e *out)
373 ret = mm_sound_get_active_device((mm_sound_device_in *)in, (mm_sound_device_out *)out);
375 return __convert_sound_manager_error_code(__func__, ret);
378 bool sound_manager_is_route_available (sound_route_e route)
381 mm_sound_is_route_available(route, &is_available);
386 int sound_manager_set_available_route_changed_cb (sound_available_route_changed_cb callback, void *user_data)
389 ret = mm_sound_add_available_route_changed_callback((mm_sound_available_route_changed_cb)callback, user_data);
391 return __convert_sound_manager_error_code(__func__, ret);
394 void sound_manager_unset_available_route_changed_cb (void)
396 mm_sound_remove_available_route_changed_callback();
399 int sound_manager_set_active_device_changed_cb (sound_active_device_changed_cb callback, void *user_data)
402 ret = mm_sound_add_active_device_changed_callback((mm_sound_active_device_changed_cb)callback, user_data);
404 return __convert_sound_manager_error_code(__func__, ret);
407 void sound_manager_unset_active_device_changed_cb (void)
409 mm_sound_remove_active_device_changed_callback();
412 struct sound_call_session_s
417 int sound_manager_call_session_create(sound_call_session_type_e type, sound_call_session_h *session)
419 int ret = SOUND_MANAGER_ERROR_NONE;
420 sound_call_session_h handle = NULL;
422 if(type < SOUND_SESSION_TYPE_CALL || type > SOUND_SESSION_TYPE_VOIP || session == NULL) {
423 ret = SOUND_MANAGER_ERROR_INVALID_PARAMETER;
427 handle = malloc(sizeof(struct sound_call_session_s));
430 ret = SOUND_MANAGER_ERROR_INVALID_OPERATION;
434 memset(handle, 0, sizeof(struct sound_call_session_s));
437 case SOUND_SESSION_TYPE_CALL:
438 ret = mm_session_init(MM_SESSION_TYPE_CALL);
440 case SOUND_SESSION_TYPE_VOIP:
441 ret = mm_session_init(MM_SESSION_TYPE_VIDEOCALL);
445 if(ret != MM_ERROR_NONE)
450 return SOUND_MANAGER_ERROR_NONE;
456 return __convert_sound_manager_error_code(__func__, ret);
459 int sound_manager_call_session_set_mode(sound_call_session_h session, sound_call_session_mode_e mode)
461 int ret = SOUND_MANAGER_ERROR_NONE;
463 if(mode < SOUND_CALL_SESSION_MODE_VOICE || mode > SOUND_CALL_SESSION_MODE_MEDIA || session == NULL) {
464 ret = SOUND_MANAGER_ERROR_INVALID_PARAMETER;
468 ret = mm_session_set_subsession ((mm_subsession_t)mode);
470 if(ret != MM_ERROR_NONE)
473 return SOUND_MANAGER_ERROR_NONE;
476 return __convert_sound_manager_error_code(__func__, ret);
479 int sound_manager_call_session_get_mode(sound_call_session_h session, sound_call_session_mode_e *mode)
481 int ret = SOUND_MANAGER_ERROR_NONE;
483 if(mode == NULL || session == NULL) {
484 ret = SOUND_MANAGER_ERROR_INVALID_PARAMETER;
488 ret = mm_session_get_subsession ((mm_subsession_t *)mode);
490 if(ret != MM_ERROR_NONE)
493 return SOUND_MANAGER_ERROR_NONE;
496 return __convert_sound_manager_error_code(__func__, ret);
499 int sound_manager_call_session_destroy(sound_call_session_h session)
501 int ret = SOUND_MANAGER_ERROR_NONE;
502 sound_call_session_h *handle = (sound_call_session_h *)session;
504 if(session == NULL) {
505 ret = SOUND_MANAGER_ERROR_INVALID_PARAMETER;
509 ret = mm_session_finish();
511 if(ret != MM_ERROR_NONE)
517 return SOUND_MANAGER_ERROR_NONE;
520 return __convert_sound_manager_error_code(__func__, ret);