Add stream routing for USB audio device
authorwn.jang <wn.jang@samsung.com>
Fri, 17 May 2019 04:37:31 +0000 (13:37 +0900)
committerwn.jang <wn.jang@samsung.com>
Fri, 17 May 2019 04:38:32 +0000 (13:38 +0900)
Change-Id: I6e986aa5721ab4cbe84ea09a6e116524547080d6

plugins/wakeup-manager/inc/wakeup_audio_manager.h
plugins/wakeup-manager/src/wakeup_audio_manager.cpp

index ba07975..85d96e4 100644 (file)
@@ -87,6 +87,10 @@ public:
 
        void change_system_volume();
        void recover_system_volume();
+
+       int mDeviceId{-1};
+       sound_stream_info_h mStreamInfo{nullptr};
+
 private:
        void recorder_thread_func(void);
        void streaming_speech_data_thread_func();
@@ -95,7 +99,6 @@ private:
        vector<IAudioEventObserver*> mObservers;
 
        audio_in_h mAudioIn{nullptr};
-       sound_stream_info_h mStreamInfo{nullptr};
 
        sound_stream_info_h mVolumeStream{nullptr};
        virtual_sound_stream_h mVirtualSoundStream{nullptr};
index be49e0b..e5f9ec9 100644 (file)
@@ -134,6 +134,87 @@ static void _ffv_audio_function_cb(void* data, unsigned int length, void* user_d
 }
 #endif
 
+static int _apply_device_for_stream_routing(void *user_data)
+{
+       CAudioManager *manager = static_cast<CAudioManager*>(user_data);
+       if (nullptr == manager) return -1;
+
+       sound_device_list_h deviceList = NULL;
+       sound_device_h device = NULL;
+       sound_device_type_e type;
+       sound_device_io_direction_e ioDirection;
+
+       if (0 != sound_manager_get_device_list(SOUND_DEVICE_IO_DIRECTION_IN_MASK, &deviceList)) {
+               MWR_LOGE("[Recorder ERROR] Fail to get current device list");
+               return -1;
+       }
+
+       bool isFound = false;
+       while (0 == sound_manager_get_next_device(deviceList, &device)) {
+               if (0 != sound_manager_get_device_type(device, &type)) {
+                       MWR_LOGE("[Recorder ERROR] Fail to get device type");
+                       continue;
+               }
+               if (0 != sound_manager_get_device_io_direction(device, &ioDirection)) {
+                       MWR_LOGE("[Recorder ERROR] Fail to get device io direction");
+                       continue;
+               }
+
+               if (SOUND_DEVICE_USB_AUDIO == type && SOUND_DEVICE_IO_DIRECTION_IN == ioDirection) {
+                       if (0 != sound_manager_add_device_for_stream_routing(manager->mStreamInfo, device)) {
+                               MWR_LOGE("[Recorder ERROR] Fail to add device");
+                               continue;
+                       }
+                       if (0 != sound_manager_apply_stream_routing(manager->mStreamInfo)) {
+                               MWR_LOGE("[Recorder ERROR] Fail to apply stream routing");
+                               continue;
+                       }
+                       isFound = true;
+                       break;
+               }
+       }
+
+       sound_manager_free_device_list(deviceList);
+       deviceList = NULL;
+
+       if (true != isFound) {
+               MWR_LOGI("[Recorder] No USB device");
+       } else {
+               MWR_LOGD("[Recorder] Apply device for stream routing");
+       }
+       return 0;
+}
+
+static void _device_connection_changed_cb(sound_device_h device, bool isConnected, void *user_data)
+{
+       CAudioManager *manager = static_cast<CAudioManager*>(user_data);
+       if (nullptr == manager) return;
+
+       sound_device_type_e type;
+       if (isConnected) {
+               if (0 != sound_manager_get_device_type(device, &type)) {
+                       MWR_LOGE("[Recorder ERROR] Fail to get device type");
+                       return;
+               }
+               if (type == SOUND_DEVICE_USB_AUDIO) {
+                       if (0 != sound_manager_remove_device_for_stream_routing(manager->mStreamInfo, device))
+                               MWR_LOGE("[Recorder ERROR] Fail to remove device");
+
+                       if (0 != sound_manager_add_device_for_stream_routing(manager->mStreamInfo, device)) {
+                               MWR_LOGE("[Recorder ERROR] Fail to add device");
+                               return;
+                       }
+                       if (0 != sound_manager_apply_stream_routing(manager->mStreamInfo)) {
+                               MWR_LOGE("[Recorder ERROR} Fail to apply stream routing");
+                               return;
+                       }
+                       MWR_LOGD("[Recorder] Apply device for stream routing");
+               }
+       }
+       return;
+}
+
+
 int CAudioManager::initialize(void)
 {
        const int rate = 16000;
@@ -164,31 +245,49 @@ int CAudioManager::initialize(void)
 
        g_farfieldvoice_h = farfield_voice_init();
        if (NULL == g_farfieldvoice_h) {
-               MWR_LOGD("[Recorder ERROR] Fail to init farfield_voice_init");
+               MWR_LOGE("[Recorder ERROR] Fail to init farfield_voice_init");
        }
 
        if (g_farfieldvoice_h) {
-               MWR_LOGD("[Recorder INFO] Register farfield voice audio callback");
+               MWR_LOGI("[Recorder INFO] Register farfield voice audio callback");
                farfield_voice_register_audio_cb(g_farfieldvoice_h, _ffv_audio_function_cb, this);
        }
 #else
        int ret = audio_in_create(rate, channel, type, &mAudioIn);
        if (AUDIO_IO_ERROR_NONE != ret) {
-               MWR_LOGD("[Recorder ERROR] Rate(%d) Channel(%d) Type(%d)", rate, channel, type);
-               MWR_LOGD("[Recorder ERROR] Fail to create audio handle : %d", ret);
+               MWR_LOGE("[Recorder ERROR] Rate(%d) Channel(%d) Type(%d)", rate, channel, type);
+               MWR_LOGE("[Recorder ERROR] Fail to create audio handle : %d", ret);
+               return -1;
+       }
+
+       ret = sound_manager_add_device_connection_changed_cb(SOUND_DEVICE_IO_DIRECTION_IN_MASK, _device_connection_changed_cb, this, &mDeviceId);
+       if (0 != ret) {
+               MWR_LOGE("[Recorder ERROR] Fail to add device connection changed callback");
                return -1;
        }
 
-       if (0 != sound_manager_create_stream_information(
-               SOUND_STREAM_TYPE_VOICE_RECOGNITION, NULL, NULL, &mStreamInfo)) {
-               MWR_LOGD("[Recorder ERROR] Fail to create stream info");
+       ret = sound_manager_create_stream_information_internal(
+               SOUND_STREAM_TYPE_VOICE_RECOGNITION_SERVICE, NULL, NULL, &mStreamInfo);
+       if (0 != ret) {
+               MWR_LOGE("[Recorder ERROR] Fail to create stream info");
+               sound_manager_remove_device_connection_changed_cb(mDeviceId);
+               audio_in_destroy(mAudioIn);
+               return -1;
+       }
+
+       ret = _apply_device_for_stream_routing(this);
+       if (0 != ret) {
+               MWR_LOGE("[Recorder ERROR] Fail to apply device for stream routing: %d", ret);
+               sound_manager_remove_device_connection_changed_cb(mDeviceId);
+               sound_manager_destroy_stream_information(mStreamInfo);
                audio_in_destroy(mAudioIn);
                return -1;
        }
 
        ret = audio_in_set_sound_stream_info(mAudioIn, mStreamInfo);
        if (AUDIO_IO_ERROR_NONE != ret) {
-               MWR_LOGD("[Recorder ERROR] Fail to set stream info : %d", ret);
+               MWR_LOGE("[Recorder ERROR] Fail to set stream info : %d", ret);
+               sound_manager_remove_device_connection_changed_cb(mDeviceId);
                sound_manager_destroy_stream_information(mStreamInfo);
                audio_in_destroy(mAudioIn);
                return -1;
@@ -198,10 +297,11 @@ int CAudioManager::initialize(void)
        if (AUDIO_IO_ERROR_NONE != ret) {
                if (AUDIO_IO_ERROR_SOUND_POLICY == ret)
                {
-                       MWR_LOGD("[Recorder ERROR] Audio is busy.");
+                       MWR_LOGE("[Recorder ERROR] Audio is busy.");
                } else {
-                       MWR_LOGD("[Recorder ERROR] Fail to start audio : %d", ret);
+                       MWR_LOGE("[Recorder ERROR] Fail to start audio : %d", ret);
                }
+               sound_manager_remove_device_connection_changed_cb(mDeviceId);
                sound_manager_destroy_stream_information(mStreamInfo);
                audio_in_destroy(mAudioIn);
                return -1;
@@ -209,7 +309,7 @@ int CAudioManager::initialize(void)
 
        ret = audio_in_pause(mAudioIn);
        if (AUDIO_IO_ERROR_NONE != ret) {
-               MWR_LOGD("[Recorder ERROR] Fail to pause audio in : %d", ret);
+               MWR_LOGE("[Recorder ERROR] Fail to pause audio in : %d", ret);
        }
 #endif
        return 0;
@@ -233,23 +333,30 @@ int CAudioManager::deinitialize(void)
        bt_hid_host_deinitialize();
        bt_product_deinit();
 #else
+       int ret = 0;
        if (mVirtualSoundStream) {
                sound_manager_stop_virtual_stream(mVirtualSoundStream);
                ret = sound_manager_destroy_virtual_stream(mVirtualSoundStream);
                if (0 != ret) {
-                       MWR_LOGD("[Audio ERROR] Fail to destroy virtual stream, ret(%d)", ret);
+                       MWR_LOGE("[Audio ERROR] Fail to destroy virtual stream, ret(%d)", ret);
                }
                mVirtualSoundStream = NULL;
        }
+
+       if (mDeviceId) {
+               ret = sound_manager_remove_device_connection_changed_cb(mDeviceId);
+               if (0 != ret)
+                       MWR_LOGE("[Audio ERROR] Fail to remove device connection changed callback, ret(%d)", ret);
+       }
+
        if (mVolumeStream) {
                ret = sound_manager_destroy_stream_information(mVolumeStream);
                if (0 != ret) {
-                       MWR_LOGD("[Audio ERROR] Fail to destroy stream information, ret(%d)", ret);
+                       MWR_LOGE("[Audio ERROR] Fail to destroy stream information, ret(%d)", ret);
                }
                mVolumeStream = NULL;
        }
 
-       int ret = 0;
        ret = audio_in_unprepare(mAudioIn);
        if (AUDIO_IO_ERROR_NONE != ret) {
                MWR_LOGD("[Recorder ERROR] Fail to stop audio : %d", ret);