From ced5cba55bcc24c05e78a9d45bae7e5d3e50f59e Mon Sep 17 00:00:00 2001 From: Sangchul Lee Date: Mon, 6 Feb 2017 10:03:46 +0900 Subject: [PATCH] Add input audio device according to voip session mode Missing input device for VoIP session modes is added. [Version] 0.3.95 [Profile] Common [Issue Type] Bug fix(backward compatibility) Change-Id: I15c11aa17f4b394e8fbf564043c06158016ec130 Signed-off-by: Sangchul Lee --- packaging/capi-media-sound-manager.spec | 2 +- src/sound_manager.c | 4 +- src/sound_manager_private.c | 233 +++++++++++++----------- 3 files changed, 129 insertions(+), 110 deletions(-) diff --git a/packaging/capi-media-sound-manager.spec b/packaging/capi-media-sound-manager.spec index 5e54c06..110db00 100755 --- a/packaging/capi-media-sound-manager.spec +++ b/packaging/capi-media-sound-manager.spec @@ -1,6 +1,6 @@ Name: capi-media-sound-manager Summary: Sound Manager library -Version: 0.3.94 +Version: 0.3.95 Release: 0 Group: Multimedia/API License: Apache-2.0 diff --git a/src/sound_manager.c b/src/sound_manager.c index c159e8e..691c7ce 100644 --- a/src/sound_manager.c +++ b/src/sound_manager.c @@ -29,6 +29,7 @@ _focus_watch_info_s focus_watch_info_arr[SOUND_STREAM_INFO_ARR_MAX]; sound_session_type_e g_cached_session = -1; _session_mode_e g_cached_session_mode = -1; int g_cached_voip_device_id = -1; +int g_cached_voip_device_id2 = -1; extern sound_stream_info_s *g_voip_stream_info; extern virtual_sound_stream_info_s *g_voip_vstream_h; @@ -673,6 +674,7 @@ int sound_manager_set_session_type(sound_session_type_e type) if (cur_session == MM_SESSION_TYPE_VOIP) { g_cached_session_mode = -1; g_cached_voip_device_id = -1; + g_cached_voip_device_id2 = -1; if (g_voip_vstream_h) { _stop_virtual_stream(g_voip_vstream_h); _destroy_virtual_stream(g_voip_vstream_h); @@ -999,7 +1001,7 @@ int sound_manager_set_voip_session_mode(sound_session_voip_mode_e mode) ret = _set_session_mode((_session_mode_e)mode); - LOGI("session=%d, mode=%d", session, mode); + LOGI("session=%d, mode=%d(0:RINGTONE 1:MIC_RCV 2:MIC_SPK 3:EAR 4:BT)", session, mode); LEAVE: return _convert_sound_manager_error_code(__func__, ret); diff --git a/src/sound_manager_private.c b/src/sound_manager_private.c index 6d0374f..4068a67 100644 --- a/src/sound_manager_private.c +++ b/src/sound_manager_private.c @@ -34,9 +34,22 @@ #define VCONF_PATH_PREFIX_VOLUME "file/private/sound/volume/" #define VCONF_PATH_MAX 64 +#define CHECK_AND_SET_DEVICE(x_dst_device1, x_dst_device2, x_src_device) \ +do { \ + if (!x_dst_device1) \ + x_dst_device1 = x_src_device; \ + else if (!x_dst_device2) \ + x_dst_device2 = x_src_device; \ + else { \ + LOGE("all dst devices are already set"); \ + goto ERROR_CASE; \ + } \ +} while (0) \ + extern _session_interrupt_info_s g_session_interrupt_cb_table; extern _session_mode_e g_cached_session_mode; extern int g_cached_voip_device_id; +extern int g_cached_voip_device_id2; extern _focus_watch_info_s focus_watch_info_arr[SOUND_STREAM_INFO_ARR_MAX]; sound_stream_info_s *sound_stream_info_arr[SOUND_STREAM_INFO_ARR_MAX]; sound_stream_info_s *g_voip_stream_info = NULL; @@ -1305,13 +1318,12 @@ void _voip_focus_state_change_callback(sound_stream_info_h stream_info, int _set_session_mode(_session_mode_e mode) { int ret = MM_ERROR_NONE; - int w_ret = MM_ERROR_NONE; - int i_ret = SOUND_MANAGER_ERROR_NONE; - bool is_found = false; - MMSoundDeviceList_t device_list; + MMSoundDeviceList_t device_list = NULL; MMSoundDevice_t tmp_device = NULL; MMSoundDevice_t proper_device = NULL; + MMSoundDevice_t proper_device2 = NULL; MMSoundDevice_t prev_device = NULL; + MMSoundDevice_t prev_device2 = NULL; mm_sound_device_type_e type; int id = -1; @@ -1341,89 +1353,81 @@ int _set_session_mode(_session_mode_e mode) } /* create stream info and acquire focus for rintone-voip stream */ g_voip_stream_info->stream_type = "ringtone-voip"; - ret = _make_pa_connection_and_register_focus(g_voip_stream_info, true, _voip_focus_state_change_callback, NULL); - if (ret != MM_ERROR_NONE) { + if ((ret = _make_pa_connection_and_register_focus(g_voip_stream_info, true, _voip_focus_state_change_callback, NULL))) { SM_SAFE_FREE(g_voip_stream_info); goto ERROR_CASE; - } else { - /* acquire focus */ - ret = mm_sound_acquire_focus(g_voip_stream_info->index, FOCUS_FOR_BOTH, "for voip session"); - if (ret == MM_ERROR_NONE) { - g_voip_stream_info->acquired_focus |= FOCUS_FOR_BOTH; - _update_focus_status(g_voip_stream_info->index, (unsigned int)g_voip_stream_info->acquired_focus); - } else - goto ERROR_CASE; - } + + /* acquire focus */ + if ((ret = mm_sound_acquire_focus(g_voip_stream_info->index, FOCUS_FOR_BOTH, "for voip session"))) + goto ERROR_CASE; + + g_voip_stream_info->acquired_focus |= FOCUS_FOR_BOTH; + _update_focus_status(g_voip_stream_info->index, (unsigned int)g_voip_stream_info->acquired_focus); + /* create virtual stream for ringtone-voip */ - i_ret = _create_virtual_stream(g_voip_stream_info, &g_voip_vstream_h); - if (i_ret == SOUND_MANAGER_ERROR_NONE) { - i_ret = _start_virtual_stream(g_voip_vstream_h); - if (i_ret != SOUND_MANAGER_ERROR_NONE) - goto ERROR_CASE; - } else { + if ((ret = _create_virtual_stream(g_voip_stream_info, &g_voip_vstream_h))) { ret = MM_ERROR_SOUND_INTERNAL; goto ERROR_CASE; } + + if ((ret = _start_virtual_stream(g_voip_vstream_h))) + goto ERROR_CASE; + break; case _SESSION_MODE_VOICE_WITH_BUILTIN_RECEIVER: /* Built-in RCV and Built-in MIC */ case _SESSION_MODE_VOICE_WITH_BUILTIN_SPEAKER: /* Built-in SPK and Built-in MIC */ case _SESSION_MODE_VOICE_WITH_AUDIO_JACK: /* Earphone spk & mic */ case _SESSION_MODE_VOICE_WITH_BLUETOOTH_SCO: /* Bluetooth spk & mic */ /* check if the device is available now */ - ret = mm_sound_get_device_list(MM_SOUND_DEVICE_ALL_FLAG, &device_list); - if (ret != MM_ERROR_NONE) { + if ((ret = mm_sound_get_device_list(MM_SOUND_DEVICE_ALL_FLAG, &device_list))) { LOGE("failed to get current device list"); goto ERROR_CASE; - } else { - while (!is_found && ((w_ret = mm_sound_get_next_device(device_list, &tmp_device)) == MM_ERROR_NONE)) { - ret = mm_sound_get_device_type(tmp_device, &type); - if (ret != MM_ERROR_NONE) - goto ERROR_CASE; - ret = mm_sound_get_device_id(tmp_device, &id); - if (ret != MM_ERROR_NONE) - goto ERROR_CASE; - else { - if (g_cached_voip_device_id != -1 && g_cached_voip_device_id == id) - prev_device = tmp_device; - } - switch (mode) { - case _SESSION_MODE_VOICE_WITH_BUILTIN_RECEIVER: - if (type == MM_SOUND_DEVICE_TYPE_BUILTIN_RECEIVER) { - is_found = true; - proper_device = tmp_device; - break; - } - case _SESSION_MODE_VOICE_WITH_BUILTIN_SPEAKER: - if (type == MM_SOUND_DEVICE_TYPE_BUILTIN_SPEAKER) { - is_found = true; - proper_device = tmp_device; - break; - } - case _SESSION_MODE_VOICE_WITH_AUDIO_JACK: - if (type == MM_SOUND_DEVICE_TYPE_AUDIOJACK) { - is_found = true; - proper_device = tmp_device; - break; - } - break; - case _SESSION_MODE_VOICE_WITH_BLUETOOTH_SCO: - if (type == MM_SOUND_DEVICE_TYPE_BLUETOOTH_SCO) { - is_found = true; - proper_device = tmp_device; - break; - } - default: - break; - } - } - if (!is_found) { - LOGE("could not found a proper connected device for this mode(%d)", mode); - ret = MM_ERROR_SOUND_INTERNAL; - goto ERROR_CASE_NO_DESTROY; + } + + while ((mm_sound_get_next_device(device_list, &tmp_device) == MM_ERROR_NONE)) { + if ((ret = mm_sound_get_device_type(tmp_device, &type))) + goto ERROR_CASE; + if ((ret = mm_sound_get_device_id(tmp_device, &id))) + goto ERROR_CASE; + + if (g_cached_voip_device_id == id) + CHECK_AND_SET_DEVICE(prev_device, prev_device2, tmp_device); + else if (g_cached_voip_device_id2 == id) + CHECK_AND_SET_DEVICE(prev_device, prev_device2, tmp_device); + + + switch (mode) { + case _SESSION_MODE_VOICE_WITH_BUILTIN_RECEIVER: + if (type == MM_SOUND_DEVICE_TYPE_BUILTIN_RECEIVER) + CHECK_AND_SET_DEVICE(proper_device, proper_device2, tmp_device); + else if (type == MM_SOUND_DEVICE_TYPE_BUILTIN_MIC) + CHECK_AND_SET_DEVICE(proper_device, proper_device2, tmp_device); + break; + case _SESSION_MODE_VOICE_WITH_BUILTIN_SPEAKER: + if (type == MM_SOUND_DEVICE_TYPE_BUILTIN_SPEAKER) + CHECK_AND_SET_DEVICE(proper_device, proper_device2, tmp_device); + else if (type == MM_SOUND_DEVICE_TYPE_BUILTIN_MIC) + CHECK_AND_SET_DEVICE(proper_device, proper_device2, tmp_device); + break; + case _SESSION_MODE_VOICE_WITH_AUDIO_JACK: + if (type == MM_SOUND_DEVICE_TYPE_AUDIOJACK) + CHECK_AND_SET_DEVICE(proper_device, proper_device2, tmp_device); + break; + case _SESSION_MODE_VOICE_WITH_BLUETOOTH_SCO: + if (type == MM_SOUND_DEVICE_TYPE_BLUETOOTH_SCO) + CHECK_AND_SET_DEVICE(proper_device, proper_device2, tmp_device); + break; + default: + break; } } - /* if ok for the device, go below */ + + if (!proper_device && !proper_device2) { + LOGE("failed to find any proper device"); + goto ERROR_CASE_NO_DESTROY; + } + if (g_cached_session_mode == _SESSION_MODE_RINGTONE) { /* stop vstream and destroy vstream */ _stop_virtual_stream(g_voip_vstream_h); @@ -1438,65 +1442,72 @@ int _set_session_mode(_session_mode_e mode) if (!g_voip_stream_info) { g_voip_stream_info = malloc(sizeof(sound_stream_info_s)); if (!g_voip_stream_info) { - ret = MM_ERROR_OUT_OF_MEMORY; + LOGE("failed to malloc for g_voip_stream_info"); goto ERROR_CASE; } memset(g_voip_stream_info, 0, sizeof(sound_stream_info_s)); + LOGI("g_voip_stream_info(%p) is newly created", g_voip_stream_info); } g_voip_stream_info->stream_type = "voip"; - ret = _make_pa_connection_and_register_focus(g_voip_stream_info, true, _voip_focus_state_change_callback, NULL); - if (ret != MM_ERROR_NONE) { + if ((ret = _make_pa_connection_and_register_focus(g_voip_stream_info, true, _voip_focus_state_change_callback, NULL))) { SM_SAFE_FREE(g_voip_stream_info); goto ERROR_CASE; } - /* set device for routing to the device */ - i_ret = _add_device_for_stream_routing(g_voip_stream_info, proper_device); - if (i_ret == SOUND_MANAGER_ERROR_NONE) { - i_ret = _apply_stream_routing(g_voip_stream_info); - if (i_ret != SOUND_MANAGER_ERROR_NONE) + if (proper_device) { + if ((ret = _add_device_for_stream_routing(g_voip_stream_info, proper_device))) + goto ERROR_CASE; + } + if (proper_device2) { + if ((ret = _add_device_for_stream_routing(g_voip_stream_info, proper_device2))) goto ERROR_CASE; - } else + } + + if ((ret = _apply_stream_routing(g_voip_stream_info))) goto ERROR_CASE; /* acquire focus */ - ret = mm_sound_acquire_focus(g_voip_stream_info->index, FOCUS_FOR_BOTH, "for voip session"); - if (ret == MM_ERROR_NONE) { - g_voip_stream_info->acquired_focus |= FOCUS_FOR_BOTH; - _update_focus_status(g_voip_stream_info->index, (unsigned int)g_voip_stream_info->acquired_focus); - } else + if ((ret = mm_sound_acquire_focus(g_voip_stream_info->index, FOCUS_FOR_BOTH, "for voip session"))) goto ERROR_CASE; + g_voip_stream_info->acquired_focus |= FOCUS_FOR_BOTH; + _update_focus_status(g_voip_stream_info->index, (unsigned int)g_voip_stream_info->acquired_focus); + /* create virtual stream for voip */ - i_ret = _create_virtual_stream(g_voip_stream_info, &g_voip_vstream_h); - if (i_ret == SOUND_MANAGER_ERROR_NONE) { - i_ret = _start_virtual_stream(g_voip_vstream_h); - if (i_ret != SOUND_MANAGER_ERROR_NONE) - goto ERROR_CASE; - } else + if ((ret = _create_virtual_stream(g_voip_stream_info, &g_voip_vstream_h))) goto ERROR_CASE; - } else { - if (g_cached_voip_device_id != -1 && prev_device) { - /* remove cached device */ - i_ret = _remove_device_for_stream_routing(g_voip_stream_info, prev_device); - if (i_ret != SOUND_MANAGER_ERROR_NONE) - goto ERROR_CASE; - /* set device for routing to the device */ - i_ret = _add_device_for_stream_routing(g_voip_stream_info, proper_device); - if (i_ret == SOUND_MANAGER_ERROR_NONE) { - i_ret = _apply_stream_routing(g_voip_stream_info); - if (i_ret != SOUND_MANAGER_ERROR_NONE) - goto ERROR_CASE; - } else - goto ERROR_CASE; - } else + if ((ret = _start_virtual_stream(g_voip_vstream_h))) goto ERROR_CASE; + + } else { + if (prev_device) { + if ((ret = _remove_device_for_stream_routing(g_voip_stream_info, prev_device))) + goto ERROR_CASE_NO_DESTROY; + } + if (prev_device2) { + if ((ret = _remove_device_for_stream_routing(g_voip_stream_info, prev_device2))) + goto ERROR_CASE_NO_DESTROY; + } + if (proper_device) { + if ((ret = _add_device_for_stream_routing(g_voip_stream_info, proper_device))) + goto ERROR_CASE_NO_DESTROY; + } + if (proper_device2) { + if ((ret = _add_device_for_stream_routing(g_voip_stream_info, proper_device2))) + goto ERROR_CASE_NO_DESTROY; + } + if ((ret = _apply_stream_routing(g_voip_stream_info))) + goto ERROR_CASE_NO_DESTROY; } - mm_sound_free_device_list(device_list); break; } g_cached_session_mode = mode; - g_cached_voip_device_id = id; + g_cached_voip_device_id = -1; + g_cached_voip_device_id2 = -1; + if (proper_device) + mm_sound_get_device_id(proper_device, &g_cached_voip_device_id); + if (proper_device2) + mm_sound_get_device_id(proper_device2, &g_cached_voip_device_id2); return ret; ERROR_CASE: @@ -1504,7 +1515,6 @@ ERROR_CASE: /* destroy virtual stream handle */ _destroy_virtual_stream(g_voip_vstream_h); g_voip_vstream_h = NULL; - ret = MM_ERROR_SOUND_INTERNAL; } if (g_voip_stream_info) { /* destroy stream info */ @@ -1512,6 +1522,9 @@ ERROR_CASE: SM_SAFE_FREE(g_voip_stream_info); } ERROR_CASE_NO_DESTROY: + if (device_list) + mm_sound_free_device_list(device_list); + ret = MM_ERROR_SOUND_INTERNAL; return ret; } @@ -1764,6 +1777,8 @@ int _add_device_for_stream_routing(sound_stream_info_s *stream_info, sound_devic if (!added_successfully) ret = MM_ERROR_POLICY_INTERNAL; + LOGI("*** add device(type[%s],id[%d]), ret(0x%x)", device_type_str, device_id, ret); + return ret; } @@ -1832,6 +1847,8 @@ int _remove_device_for_stream_routing(sound_stream_info_s *stream_info, sound_de if (!removed_successfully) ret = MM_ERROR_INVALID_ARGUMENT; + LOGI("*** remove device(type[%s],id[%d]), ret(0x%x)", device_type_str, device_id, ret); + return ret; } -- 2.34.1