}
#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;
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;
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;
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;
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);