From b4044c123bdc2cb483d72e0c1586b7903ad873c6 Mon Sep 17 00:00:00 2001 From: Ji-hoon Lee Date: Fri, 12 Oct 2018 16:48:52 +0900 Subject: [PATCH] Terminate existing client when changing assistant Change-Id: Iba0ef13f62b892c81fc13410a1c43d04b1f14284 --- inc/multi_assistant_service.h | 3 +++ org.tizen.multi-assistant-service.xml | 1 + src/multi_assistant_service.c | 28 +++++++++++++++++++-- src/multi_assistant_service_plugin.c | 36 ++++++++++++++++++++++++--- 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/inc/multi_assistant_service.h b/inc/multi_assistant_service.h index 93d50c1..009ee7c 100644 --- a/inc/multi_assistant_service.h +++ b/inc/multi_assistant_service.h @@ -59,6 +59,9 @@ int mas_launch_client_by_wakeup_word(const char *wakeup_word); int mas_launch_client_by_appid(const char *appid); +void ma_client_set_temp_speech_data_requested(int val); +int ma_client_get_temp_speech_data_requested(); + #ifdef __cplusplus } #endif diff --git a/org.tizen.multi-assistant-service.xml b/org.tizen.multi-assistant-service.xml index c217424..647642c 100644 --- a/org.tizen.multi-assistant-service.xml +++ b/org.tizen.multi-assistant-service.xml @@ -25,6 +25,7 @@ http://tizen.org/privilege/power http://tizen.org/privilege/packagemanager.info http://tizen.org/privilege/appmanager.launch + http://tizen.org/privilege/appmanager.kill http://tizen.org/privilege/keygrab http://tizen.org/privilege/recorder http://tizen.org/privilege/appdir.shareddata diff --git a/src/multi_assistant_service.c b/src/multi_assistant_service.c index fa8ecc3..339b8ed 100644 --- a/src/multi_assistant_service.c +++ b/src/multi_assistant_service.c @@ -61,6 +61,16 @@ static const char *g_launching_maclient_appid = NULL; /* client list */ static GSList* g_client_list = NULL; +static int g_temp_speech_data_requested = 0; +void ma_client_set_temp_speech_data_requested(int val) +{ + g_temp_speech_data_requested = val; +} +int ma_client_get_temp_speech_data_requested() +{ + return g_temp_speech_data_requested; +} + int ma_client_create(ma_client_s info) { ma_client_s* data = NULL; @@ -207,7 +217,7 @@ int mas_client_initialize(int pid) /* Since the ma_request_speech_data() is not available, we will request speech data here. Need to remove below code when ma_request_speech_data() get implemented. */ - ecore_timer_add(3.0f, __request_speech_data_timer, (void*)(pid)); + ecore_timer_add(5.0f, __request_speech_data_timer, (void*)(pid)); } else { MAS_LOGD("MA client connected, but its appid does not match with current maclient"); } @@ -256,6 +266,7 @@ int mas_client_request_speech_data(int pid) if (current_maclient_appid && 0 == strncmp(current_maclient_appid, appid, MAX_APPID_LEN)) { MAS_LOGD("appid %s matches with current MA Client, requesting speech data", appid); + ma_client_set_temp_speech_data_requested(1); ret = wakeup_service_request_speech_data(); if (0 != ret) { MAS_LOGE("[ERROR] Fail to request speech data(%d)", ret); @@ -324,9 +335,22 @@ int mas_ui_client_change_assistant(const char* appid) { MAS_LOGD("[Enter]"); + /* We are going to terminate existing clients for testing purpose */ + int pid = mas_get_current_client_pid(); + if (pid != -1 && appid) { + ma_client_s *client = ma_client_find_by_pid(pid); + if (client && strncmp(appid, client->appid, MAX_APPID_LEN) != 0) { + int ret = aul_terminate_pid(pid); + if (ret != AUL_R_OK) { + MAS_LOGW("aul_terminate_pid for pid %d returned %d", pid, ret); + } + } + } + mas_set_current_client_by_appid(appid); if (mas_get_client_pid_by_appid(appid) != -1) { MAS_LOGD("MA Client with appid %s exists, requesting speech data", (appid ? appid : "NULL")); + ma_client_set_temp_speech_data_requested(1); int ret = wakeup_service_request_speech_data(); if (0 != ret) { MAS_LOGE("[ERROR] Fail to request speech data(%d)", ret); @@ -601,7 +625,7 @@ int mas_launch_client_by_appid(const char *appid) } app_control_destroy (app_control); - if (APP_CONTROL_ERROR_NONE != ret) { + if (APP_CONTROL_ERROR_NONE == ret) { g_launching_maclient_appid = appid; } diff --git a/src/multi_assistant_service_plugin.c b/src/multi_assistant_service_plugin.c index e1fc12f..229b2c9 100644 --- a/src/multi_assistant_service_plugin.c +++ b/src/multi_assistant_service_plugin.c @@ -40,7 +40,7 @@ #ifdef BUF_SAVE_MODE static char g_temp_file_name[128] = {'\0',}; -static FILE* g_pFile; +static FILE* g_pFile = NULL; static int g_count = 1; #endif @@ -86,6 +86,7 @@ Eina_Bool __request_speech_data(void *data) mas_set_current_client_by_wakeup_word(wakeup_word); if (mas_get_client_pid_by_wakeup_word(wakeup_word) != -1) { MAS_LOGD("MA Client with wakeup word %s exists, requesting speech data", (wakeup_word ? wakeup_word : "NULL")); + ma_client_set_temp_speech_data_requested(1); int ret = wakeup_service_request_speech_data(); if (0 != ret) { MAS_LOGE("[ERROR] Fail to request speech data(%d)", ret); @@ -184,11 +185,38 @@ static void __speech_streaming_cb(wakeup_service_speech_streaming_event_e event, if (pid == -1) { MAS_LOGE("[ERROR] Fail to retrieve pid of current MA client"); } else { - int ret = masc_dbus_send_speech_data(pid, event, buffer, len); - if (0 != ret) { - MAS_LOGE("[ERROR] Fail to send speech data, ret(%d)", ret); + if (g_pFile) { + int ret = masc_dbus_send_speech_data(pid, event, buffer, len); + if (0 != ret) { + MAS_LOGE("[ERROR] Fail to send speech data, ret(%d)", ret); + } + } else { + if (ma_client_get_temp_speech_data_requested()) { + /* read file and send dbus here */ + FILE *fp = fopen(g_temp_file_name, "rb"); + if (fp) { + unsigned char content[640]; + int first = 1; + size_t size; + while (size = fread(content, 1, 640, fp)) { + wakeup_service_speech_streaming_event_e event; + if (size == 640) { + event = (first ? WAKEUP_SPEECH_STREAMING_EVENT_START : WAKEUP_SPEECH_STREAMING_EVENT_CONTINUE); + } else { + event = WAKEUP_SPEECH_STREAMING_EVENT_FINISH; + } + int ret = masc_dbus_send_speech_data(pid, event, content, size); + if (0 != ret) { + MAS_LOGE("[ERROR] Fail to send speech data, ret(%d)", ret); + } + first = 0; + } + fclose(fp); + } + } } } + ma_client_set_temp_speech_data_requested(0); #ifdef BUF_SAVE_MODE /* write pcm buffer */ -- 2.34.1