2 * Copyright (c) 2011-2015 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 #include <sound_manager.h>
20 #include "vc_info_parser.h"
22 #include "vcd_server.h"
23 #include "vcd_client_data.h"
25 #include "vcd_engine_agent.h"
26 #include "vcd_config.h"
27 #include "vcd_recorder.h"
30 #include "voice_control_command_expand.h"
33 * VC Server static variable
35 static bool g_is_engine;
37 static GList *g_proc_list = NULL;
40 * VC Server Internal Functions
42 static Eina_Bool __stop_by_silence(void *data)
44 SLOG(LOG_DEBUG, TAG_VCD, "===== Silence Detected ");
46 vcd_server_mgr_stop();
48 SLOG(LOG_DEBUG, TAG_VCD, "=====");
49 SLOG(LOG_DEBUG, TAG_VCD, " ");
53 static Eina_Bool __cancel_by_interrupt(void *data)
55 SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by interrupt");
57 vcd_server_mgr_cancel();
59 SLOG(LOG_DEBUG, TAG_VCD, "=====");
60 SLOG(LOG_DEBUG, TAG_VCD, " ");
64 static Eina_Bool __restart_engine(void *data)
66 SLOG(LOG_DEBUG, TAG_VCD, "===== Restart by no result");
68 /* Restart recognition */
69 int ret = vcd_engine_recognize_start(true);
71 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to restart recognition : result(%d)", ret);
75 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
77 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
78 vcd_config_set_service_state(VCD_STATE_RECORDING);
79 vcdc_send_service_state(VCD_STATE_RECORDING);
82 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Restart recognition");
84 SLOG(LOG_DEBUG, TAG_VCD, "=====");
85 SLOG(LOG_DEBUG, TAG_VCD, " ");
89 static int __server_recorder_callback(const void* data, const unsigned int length)
91 vcd_state_e state = vcd_config_get_service_state();
92 if (VCD_STATE_RECORDING != state) {
93 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Skip by engine processing");
97 vcp_speech_detect_e speech_detected = VCP_SPEECH_DETECT_NONE;
100 ret = vcd_engine_recognize_audio(data, length, &speech_detected);
104 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set recording data to engine(%d)", ret);
105 ecore_timer_add(0, __cancel_by_interrupt, NULL);
109 if (VCP_SPEECH_DETECT_BEGIN == speech_detected) {
110 if (-1 != vcd_client_manager_get_pid()) {
111 /* Manager client is available */
112 if (0 != vcdc_send_speech_detected(vcd_client_manager_get_pid())) {
113 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send speech detected");
116 } else if (VCP_SPEECH_DETECT_END == speech_detected) {
117 if (VCD_RECOGNITION_MODE_STOP_BY_SILENCE == vcd_client_get_recognition_mode()) {
118 /* silence detected */
119 ecore_timer_add(0, __stop_by_silence, NULL);
120 } else if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
121 /* Stop engine recognition */
122 int ret = vcd_engine_recognize_stop();
124 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
126 vcd_config_set_service_state(VCD_STATE_PROCESSING);
127 vcdc_send_service_state(VCD_STATE_PROCESSING);
129 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop engine only by silence");
130 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
131 /* Stop engine recognition */
132 int ret = vcd_engine_recognize_stop();
134 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
142 void __server_recorder_interrupt_callback()
144 SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by sound interrupt");
146 ecore_timer_add(0, __cancel_by_interrupt, NULL);
148 SLOG(LOG_DEBUG, TAG_VCD, "=====");
149 SLOG(LOG_DEBUG, TAG_VCD, " ");
152 static void __config_lang_changed_cb(const char* current_lang, void* user_data)
154 SLOG(LOG_DEBUG, TAG_VCD, "===== Change language ");
156 /* Current state is recording */
157 vcd_state_e state = vcd_config_get_service_state();
158 if (VCD_STATE_RECORDING == state || VCD_STATE_PROCESSING == state) {
159 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is 'Recording'. Cancel recognition");
160 vcd_server_mgr_cancel();
164 ret = vcd_engine_set_current_language(current_lang);
166 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set language of engine : %d", ret);
169 SLOG(LOG_DEBUG, TAG_VCD, "=====");
170 SLOG(LOG_DEBUG, TAG_VCD, " ");
175 static void __config_foreground_changed_cb(int previous, int current, void* user_data)
177 SLOG(LOG_DEBUG, TAG_VCD, "===== Change foreground");
179 SLOG(LOG_DEBUG, TAG_VCD, "Foreground pid(%d)", current);
181 if (VC_NO_FOREGROUND_PID != current) {
182 /* Foreground app is changed */
183 vcd_state_e state = vcd_config_get_service_state();
184 if (VCD_STATE_RECORDING == state) {
185 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Foreground pid(%d) is changed. Cancel recognition", current);
186 ecore_timer_add(0, __cancel_by_interrupt, NULL);
190 SLOG(LOG_DEBUG, TAG_VCD, "=====");
191 SLOG(LOG_DEBUG, TAG_VCD, " ");
196 static Eina_Bool __vcd_send_selected_result(void *data)
198 GSList* pid_list = NULL;
199 if (0 != vc_info_parser_get_result_pid_list(&pid_list)) {
200 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get pid list. No result");
202 if (0 < g_slist_length(pid_list)) {
204 vc_cmd_s* temp_cmd = NULL;
208 iter = g_slist_nth(pid_list, 0);
209 while (NULL != iter) {
210 temp_cmd = iter->data;
212 if (NULL != temp_cmd) {
215 /* send result noti */
216 ret = vcdc_send_result(temp_cmd->pid, temp_cmd->type);
218 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
219 if (VCD_ERROR_TIMED_OUT != ret) {
223 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
227 if (100 == count) break;
228 /* While is retry code */
233 pid_list = g_slist_remove_link(pid_list, iter);
234 iter = g_slist_nth(pid_list, 0);
239 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
240 vcd_config_set_service_state(VCD_STATE_READY);
241 vcdc_send_service_state(VCD_STATE_READY);
247 static int __convert_type_to_priority(vc_cmd_type_e type)
250 case VC_COMMAND_TYPE_NONE: return 0; break;
251 case VC_COMMAND_TYPE_BACKGROUND: return 1; break;
252 case VC_COMMAND_TYPE_FOREGROUND: return 2; break;
253 case VC_COMMAND_TYPE_WIDGET: return 2; break;
254 case VC_COMMAND_TYPE_SYSTEM: return 3; break;
255 case VC_COMMAND_TYPE_EXCLUSIVE: return 3; break;
256 default: return 0; break;
260 static void __vcd_server_result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result,
261 const char* non_fixed_result, const char* msg, void *user_data)
263 if (VCD_STATE_PROCESSING != vcd_config_get_service_state()) {
264 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
265 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not 'Processing' and mode is not 'Restart continuously'");
270 vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
272 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
273 event, all_result, non_fixed_result, msg, count);
275 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
276 if (VCP_RESULT_EVENT_REJECTED == event) {
277 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
278 /* If no result and restart option is ON */
279 /* Send reject message */
280 bool temp = vcd_client_manager_get_exclusive();
281 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
282 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
283 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
286 ecore_timer_add(0, __restart_engine, NULL);
289 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
291 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
292 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
293 /* Restart option is ON */
294 ecore_timer_add(0, __restart_engine, NULL);
295 if (VCP_RESULT_EVENT_REJECTED == event) {
296 bool temp = vcd_client_manager_get_exclusive();
297 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
298 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
299 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
306 if (NULL == result_id) {
308 if (NULL != all_result) {
309 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
310 bool temp = vcd_client_manager_get_exclusive();
311 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
314 int pid = vcd_client_widget_get_foreground_pid();
316 if (NULL != all_result) {
317 /* Send result text to widget */
318 vcdc_send_result(pid, VC_COMMAND_TYPE_WIDGET);
321 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
322 /* Send to hide tooltip */
323 vcdc_send_show_tooltip(pid, false);
326 if (-1 != vcd_client_manager_get_pid()) {
327 /* Manager client is available */
328 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
329 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
333 vcd_client_manager_set_exclusive(false);
339 SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
342 vc_cmd_s* temp_cmd = NULL;
343 vc_cmd_list_h vc_cmd_list = NULL;
345 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
346 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to create command list");
347 vcd_client_manager_set_exclusive(false);
348 vcd_config_set_service_state(VCD_STATE_READY);
349 vcdc_send_service_state(VCD_STATE_READY);
355 for (i = 0; i < count; i++) {
356 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
358 if (result_id[i] < 0) {
359 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
363 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
364 if (0 == ret && NULL != temp_cmd) {
365 /* Add priority filter */
366 int temp_priority = __convert_type_to_priority(temp_cmd->type);
367 if (priority > temp_priority) {
368 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Ignore result by priority");
370 } else if (priority < temp_priority) {
371 SLOG(LOG_DEBUG, TAG_VCD, "[Server] High priority result!!");
372 priority = temp_priority;
374 if (0 != vc_cmd_list_remove_all(vc_cmd_list, true)) {
375 SLOG(LOG_ERROR, TAG_VCD, "[Server] Fail to list remove all");
379 switch (temp_cmd->format) {
380 case VC_CMD_FORMAT_FIXED:
381 /* Nonfixed result is NOT valid */
383 case VC_CMD_FORMAT_FIXED_AND_EXTRA:
384 if (NULL == temp_cmd->parameter) {
385 if (NULL != non_fixed_result) {
386 temp_cmd->parameter = strdup(non_fixed_result);
389 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
392 case VC_CMD_FORMAT_EXTRA_AND_FIXED:
393 if (NULL == temp_cmd->command) {
394 if (NULL != non_fixed_result) {
395 temp_cmd->command = strdup(non_fixed_result);
398 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
403 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
407 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
408 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
409 vc_cmd_destroy((vc_cmd_h)temp_cmd);
412 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
416 vc_cmd_print_list(vc_cmd_list);
418 SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
420 int result_count = 0;
421 vc_cmd_list_get_count(vc_cmd_list, &result_count);
423 if (false == vcd_client_manager_get_exclusive()) {
424 int pid = vcd_client_widget_get_foreground_pid();
426 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
427 vcdc_send_show_tooltip(pid, false);
430 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
432 if (-1 != vcd_client_manager_get_pid()) {
433 /* Manager client is available */
434 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
435 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
438 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
439 /* Send result to client */
440 ecore_timer_add(0, __vcd_send_selected_result, NULL);
443 /* exclusive command */
444 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
446 if (-1 != vcd_client_manager_get_pid()) {
447 /* Manager client is available */
448 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
449 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
452 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
455 vcd_client_manager_set_exclusive(false);
458 vc_cmd_list_destroy(vc_cmd_list, true);
464 * vcd server Interfaces
466 static void __vcd_file_clean_up()
468 SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
473 struct dirent *dirp = NULL;
475 dp = opendir(VC_RUNTIME_INFO_ROOT);
477 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
481 char remove_path[256] = {0, };
483 ret = readdir_r(dp, &entry, &dirp);
485 SLOG(LOG_ERROR, TAG_VCD, "[File ERROR] Fail to read directory");
490 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
491 memset(remove_path, 0, 256);
492 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
495 if (0 != remove(remove_path)) {
496 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
498 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
502 } while (NULL != dirp);
513 /* Remove old file */
514 __vcd_file_clean_up();
516 /* initialize modules */
517 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
519 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
522 vcd_config_set_service_state(VCD_STATE_NONE);
524 ret = vcd_engine_agent_init(__vcd_server_result_cb);
526 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
530 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
531 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
532 return VCD_ERROR_OPERATION_FAILED;
536 ret = vcd_engine_agent_initialize_current_engine();
538 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
539 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
541 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
549 if (0 != vcd_engine_agent_load_current_engine()) {
550 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
551 return VCD_ERROR_OPERATION_FAILED;
554 /* Initialize manager info */
555 vcd_client_manager_unset();
557 vcd_config_set_service_state(VCD_STATE_READY);
558 vcdc_send_service_state(VCD_STATE_READY);
560 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
568 if (0 < g_list_length(g_proc_list)) {
569 iter = g_list_first(g_proc_list);
570 while (NULL != iter) {
571 g_proc_list = g_list_remove_link(g_proc_list, iter);
572 iter = g_list_first(g_proc_list);
576 vcd_state_e state = vcd_config_get_service_state();
577 if (VCD_STATE_READY != state) {
578 if (VCD_STATE_RECORDING == state) {
581 vcd_engine_recognize_cancel();
583 if (0 != vcd_recorder_destroy()) {
584 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
586 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
589 if (0 != vcd_engine_agent_release()) {
590 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
592 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
595 vcd_config_set_service_state(VCD_STATE_NONE);
596 vcdc_send_service_state(VCD_STATE_NONE);
598 SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
603 static Eina_Bool __finalize_quit_ecore_loop(void *data)
605 SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
606 ecore_main_loop_quit();
610 static void __read_proc()
614 struct dirent *dirp = NULL;
619 if (0 < g_list_length(g_proc_list)) {
620 iter = g_list_first(g_proc_list);
621 while (NULL != iter) {
622 g_proc_list = g_list_remove_link(g_proc_list, iter);
623 iter = g_list_first(g_proc_list);
627 dp = opendir("/proc");
629 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
632 ret = readdir_r(dp, &entry, &dirp);
634 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to readdir");
639 tmp = atoi(dirp->d_name);
640 if (0 >= tmp) continue;
641 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
643 } while (NULL != dirp);
649 static void __vcd_cleanup_client(vcd_client_type_e type)
651 int* client_list = NULL;
652 int client_count = 0;
659 if (VCD_CLIENT_TYPE_NORMAL == type) {
660 ret = vcd_client_get_list(&client_list, &client_count);
661 } else if (VCD_CLIENT_TYPE_WIDGET == type) {
662 ret = vcd_client_widget_get_list(&client_list, &client_count);
663 } else if (VCD_CLIENT_TYPE_MANAGER == type) {
664 mgr_pid = vcd_client_manager_get_pid();
665 client_list = &mgr_pid;
669 if (0 == ret || mgr_pid > 0) {
670 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
671 if (NULL != client_list && client_count > 0) {
672 for (i = 0; i < client_count; i++) {
675 for (j = 0; j < g_list_length(g_proc_list); j++) {
676 iter = g_list_nth(g_proc_list, j);
678 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
679 SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
686 if (false == exist) {
687 SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
688 if (VCD_CLIENT_TYPE_NORMAL == type)
689 vcd_server_finalize(client_list[i]);
690 else if (VCD_CLIENT_TYPE_WIDGET == type)
691 vcd_server_widget_finalize(client_list[i]);
693 vcd_server_mgr_finalize(mgr_pid);
697 SLOG(LOG_DEBUG, TAG_VCD, "=====");
698 SLOG(LOG_DEBUG, TAG_VCD, " ");
700 if (NULL != client_list && -1 == mgr_pid) {
707 Eina_Bool vcd_cleanup_client_all(void *data)
711 __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
712 __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
713 __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
716 if (0 == vcd_client_get_list(&client_list, &client_count)) {
717 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
718 if (NULL != client_list && client_count > 0) {
719 for (i = 0; i < client_count; i++) {
722 for (j = 0; j < g_list_length(g_proc_list); j++) {
723 iter = g_list_nth(g_proc_list, j);
725 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
726 SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
733 if (false == exist) {
734 SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
735 vcd_server_finalize(client_list[i]);
738 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
741 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
742 vcd_server_finalize(client_list[i]);
743 } else if (-1 == result) {
744 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
749 SLOG(LOG_DEBUG, TAG_VCD, "=====");
750 SLOG(LOG_DEBUG, TAG_VCD, " ");
752 if (NULL != client_list) {
757 /* If app is in background state, app cannot response message. */
758 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
759 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
760 if (NULL != client_list && client_count > 0) {
761 for (i = 0; i < client_count; i++) {
764 for (j = 0; j < g_list_length(g_proc_list); j++) {
765 iter = g_list_nth(g_proc_list, j);
767 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
768 SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
775 if (false == exist) {
776 SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
777 vcd_server_widget_finalize(client_list[i]);
780 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
783 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
784 vcd_server_widget_finalize(client_list[i]);
785 } else if (-1 == result) {
786 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
791 SLOG(LOG_DEBUG, TAG_VCD, "=====");
792 SLOG(LOG_DEBUG, TAG_VCD, " ");
795 if (NULL != client_list) {
803 int mgr_pid = vcd_client_manager_get_pid();
805 for (j = 0; j < g_list_length(g_proc_list); j++) {
806 iter = g_list_nth(g_proc_list, j);
808 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
809 SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
816 if (false == exist) {
817 SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
818 vcd_server_mgr_finalize(mgr_pid);
825 int vcd_server_get_service_state()
827 return vcd_config_get_service_state();
830 int vcd_server_get_foreground()
833 vcd_config_get_foreground(&pid);
841 int vcd_server_mgr_initialize(int pid)
843 if (false == g_is_engine) {
844 if (0 != vcd_engine_agent_initialize_current_engine()) {
845 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
847 return VCD_ERROR_ENGINE_NOT_FOUND;
853 /* check if pid is valid */
854 if (false == vcd_client_manager_is_valid(pid)) {
855 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid(%d) is already exist", pid);
856 return VCD_ERROR_INVALID_PARAMETER;
859 /* Add client information to client manager */
860 if (0 != vcd_client_manager_set(pid)) {
861 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
862 return VCD_ERROR_OPERATION_FAILED;
865 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
867 return VCD_ERROR_NONE;
870 int vcd_server_mgr_finalize(int pid)
872 /* check if pid is valid */
873 if (false == vcd_client_manager_is_valid(pid)) {
874 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
875 return VCD_ERROR_INVALID_PARAMETER;
878 /* Cancel recognition */
879 vcd_server_mgr_cancel();
881 /* Remove manager information */
882 if (0 != vcd_client_manager_unset()) {
883 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
886 if (0 == vcd_client_get_ref_count()) {
887 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
888 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
891 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
893 return VCD_ERROR_NONE;
896 int vcd_server_mgr_set_command(int pid)
898 if (0 != vcd_client_manager_set_command(pid)) {
899 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
900 return VCD_ERROR_INVALID_PARAMETER;
902 return VCD_ERROR_NONE;
905 int vcd_server_mgr_unset_command(int pid)
907 if (0 != vcd_client_manager_unset_command(pid)) {
908 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
909 return VCD_ERROR_INVALID_PARAMETER;
911 return VCD_ERROR_NONE;
914 int vcd_server_mgr_set_demandable_client(int pid)
916 /* check if pid is valid */
917 if (false == vcd_client_manager_is_valid(pid)) {
918 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
919 return VCD_ERROR_INVALID_PARAMETER;
922 GSList* client_list = NULL;
923 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
924 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
925 return VCD_ERROR_OPERATION_FAILED;
928 /* Save client list */
929 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
930 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
931 return VCD_ERROR_OPERATION_FAILED;
934 return VCD_ERROR_NONE;
937 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
939 /* check if pid is valid */
940 if (false == vcd_client_manager_is_valid(pid)) {
941 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
942 return VCD_ERROR_INVALID_PARAMETER;
946 vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
950 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
952 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
953 return VCD_ERROR_OPERATION_FAILED;
956 ret = vcd_recorder_set(audio_type, type, rate, channel);
958 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
959 return VCD_ERROR_OPERATION_FAILED;
962 return VCD_ERROR_NONE;
965 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
967 /* check if pid is valid */
968 if (false == vcd_client_manager_is_valid(pid)) {
969 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
970 return VCD_ERROR_INVALID_PARAMETER;
973 int ret = vcd_recorder_get(audio_type);
975 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
976 return VCD_ERROR_OPERATION_FAILED;
979 return VCD_ERROR_NONE;
982 int vcd_server_mgr_set_client_info(int pid)
984 /* check if pid is valid */
985 if (false == vcd_client_manager_is_valid(pid)) {
986 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
987 return VCD_ERROR_INVALID_PARAMETER;
990 int ret = vcd_client_save_client_info();
992 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
993 return VCD_ERROR_OPERATION_FAILED;
996 return VCD_ERROR_NONE;
999 static int __start_internal_recognition()
1003 /* 2. Get commands */
1004 ret = vcd_client_command_collect_command();
1006 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1007 return VCD_ERROR_OPERATION_FAILED;
1010 ret = vcd_client_get_length();
1012 SLOG(LOG_WARN, TAG_VCD, "[Server WARNIING] No current command : %d", ret);
1013 return VCD_ERROR_OPERATION_FAILED;
1016 /* 3. Set command to engine */
1017 ret = vcd_engine_set_commands();
1019 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
1020 return VCD_ERROR_OPERATION_FAILED;
1023 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
1025 bool stop_by_silence = true;
1026 if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
1027 stop_by_silence = false;
1030 /* 4. start recognition */
1031 ret = vcd_engine_recognize_start(stop_by_silence);
1033 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1034 return VCD_ERROR_OPERATION_FAILED;
1037 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1039 /* 5. recorder start */
1040 ret = vcd_recorder_start();
1042 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1043 vcd_engine_recognize_cancel();
1047 vcd_config_set_service_state(VCD_STATE_RECORDING);
1048 vcdc_send_service_state(VCD_STATE_RECORDING);
1050 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1055 static Eina_Bool __vcd_request_show_tooltip(void *data)
1057 int pid = vcd_client_widget_get_foreground_pid();
1059 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1060 vcdc_send_show_tooltip(pid, (bool)data);
1066 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1068 /* 1. check current state */
1069 vcd_state_e state = vcd_config_get_service_state();
1071 if (VCD_STATE_READY != state) {
1072 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1073 return VCD_ERROR_INVALID_STATE;
1076 SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1077 vcd_client_set_recognition_mode(recognition_mode);
1079 if (false == exclusive_cmd) {
1080 /* Notify show tooltip */
1081 int pid = vcd_client_widget_get_foreground_pid();
1083 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1084 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1088 vcd_client_manager_set_exclusive(exclusive_cmd);
1092 if (true == start_by_client) {
1093 /* Get foreground pid */
1094 if (0 != vcd_config_get_foreground(&fg_pid)) {
1095 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1098 /* Set client exclusive option */
1099 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1100 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1104 int ret = __start_internal_recognition();
1106 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1110 if (true == start_by_client) {
1111 vcd_client_unset_exclusive_command(fg_pid);
1114 return VCD_ERROR_NONE;
1117 int vcd_server_mgr_stop()
1119 /* 1. Check current state is recording */
1120 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1121 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1122 return VCD_ERROR_INVALID_STATE;
1125 /* 2. Stop recorder */
1126 vcd_recorder_stop();
1128 /* 3. Stop engine recognition */
1129 int ret = vcd_engine_recognize_stop();
1131 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1134 /* 4. Set original mode */
1135 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1136 vcdc_send_service_state(VCD_STATE_PROCESSING);
1138 return VCD_ERROR_NONE;
1141 int vcd_server_mgr_cancel()
1143 /* 1. Check current state */
1144 vcd_state_e state = vcd_config_get_service_state();
1145 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1146 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
1147 return VCD_ERROR_INVALID_STATE;
1150 /* 2. Stop recorder */
1151 vcd_recorder_stop();
1152 /* 3. Cancel engine */
1153 int ret = vcd_engine_recognize_cancel();
1155 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1158 if (false == vcd_client_manager_get_exclusive()) {
1159 int pid = vcd_client_widget_get_foreground_pid();
1161 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1162 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1165 vcd_client_manager_set_exclusive(false);
1169 vcd_config_set_service_state(VCD_STATE_READY);
1170 vcdc_send_service_state(VCD_STATE_READY);
1172 return VCD_ERROR_NONE;
1176 int vcd_server_mgr_result_select()
1178 __vcd_send_selected_result(NULL);
1180 return VCD_ERROR_NONE;
1184 * VC Server Functions for Client
1186 int vcd_server_initialize(int pid)
1188 if (false == g_is_engine) {
1189 if (0 != vcd_engine_agent_initialize_current_engine()) {
1190 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1191 g_is_engine = false;
1192 return VCD_ERROR_ENGINE_NOT_FOUND;
1198 if (false == vcd_engine_is_available_engine()) {
1199 if (0 != vcd_engine_agent_initialize_current_engine()) {
1200 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1201 return VCD_ERROR_ENGINE_NOT_FOUND;
1205 /* check if pid is valid */
1206 if (true == vcd_client_is_available(pid)) {
1207 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1208 return VCD_ERROR_INVALID_PARAMETER;
1211 /* Add client information to client manager */
1212 if (0 != vcd_client_add(pid)) {
1213 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1214 return VCD_ERROR_OPERATION_FAILED;
1217 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1219 return VCD_ERROR_NONE;
1222 int vcd_server_finalize(int pid)
1224 /* check if pid is valid */
1225 if (false == vcd_client_is_available(pid)) {
1226 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1227 return VCD_ERROR_INVALID_PARAMETER;
1230 /* Remove client information */
1231 if (0 != vcd_client_delete(pid)) {
1232 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1235 if (0 == vcd_client_get_ref_count()) {
1236 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1237 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1240 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1242 return VCD_ERROR_NONE;
1245 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1247 /* check if pid is valid */
1248 if (false == vcd_client_is_available(pid)) {
1249 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1250 return VCD_ERROR_INVALID_PARAMETER;
1253 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1254 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1255 return VCD_ERROR_OPERATION_FAILED;
1261 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1263 /* check if pid is valid */
1264 if (false == vcd_client_is_available(pid)) {
1265 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1266 return VCD_ERROR_INVALID_PARAMETER;
1269 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1270 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1271 return VCD_ERROR_OPERATION_FAILED;
1277 int vcd_server_set_foreground(int pid, bool value)
1279 /* check if pid is valid */
1280 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1281 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1282 return VCD_ERROR_INVALID_PARAMETER;
1285 if (0 != vcd_config_set_foreground(pid, value)) {
1286 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1287 return VCD_ERROR_OPERATION_FAILED;
1294 int vcd_server_set_exclusive_command(int pid, bool value)
1296 /* check if pid is valid */
1297 if (false == vcd_client_is_available(pid)) {
1298 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1299 return VCD_ERROR_INVALID_PARAMETER;
1302 if (true == value) {
1303 if (0 != vcd_client_set_exclusive_command(pid)) {
1304 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
1305 return VCD_ERROR_OPERATION_FAILED;
1308 if (0 != vcd_client_unset_exclusive_command(pid)) {
1309 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
1310 return VCD_ERROR_OPERATION_FAILED;
1317 int vcd_server_request_start(int pid, bool stop_by_silence)
1319 /* check if pid is valid */
1320 if (false == vcd_client_is_available(pid)) {
1321 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
1322 return VCD_ERROR_INVALID_PARAMETER;
1326 /* Check current state */
1327 vcd_state_e state = vcd_config_get_service_state();
1329 /* Service state should be ready */
1330 if (VCD_STATE_READY != state) {
1331 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1332 return VCD_ERROR_INVALID_STATE;
1335 if (-1 != vcd_client_manager_get_pid()) {
1336 /* Check current pid is valid */
1337 if (false == vcd_client_manager_check_demandable_client(pid)) {
1338 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1339 return VCD_ERROR_INVALID_PARAMETER;
1343 ret = vcd_server_mgr_start(stop_by_silence, false);
1345 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1346 return VCD_ERROR_INVALID_PARAMETER;
1352 int vcd_server_request_stop(int pid)
1355 /* Check current state */
1356 vcd_state_e state = vcd_config_get_service_state();
1358 /* Service state should be ready */
1359 if (VCD_STATE_RECORDING != state) {
1360 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
1361 return VCD_ERROR_INVALID_STATE;
1364 if (-1 != vcd_client_manager_get_pid()) {
1365 /* Check current pid is valid */
1366 if (false == vcd_client_manager_check_demandable_client(pid)) {
1367 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1368 return VCD_ERROR_INVALID_PARAMETER;
1372 ret = vcd_server_mgr_stop();
1374 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1375 return VCD_ERROR_OPERATION_FAILED;
1378 return VCD_ERROR_NONE;
1381 int vcd_server_request_cancel(int pid)
1384 /* Check current state */
1385 vcd_state_e state = vcd_config_get_service_state();
1387 /* Service state should be recording or processing */
1388 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1389 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1390 return VCD_ERROR_INVALID_STATE;
1393 if (-1 != vcd_client_manager_get_pid()) {
1394 /* Check current pid is valid */
1395 if (false == vcd_client_manager_check_demandable_client(pid)) {
1396 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1397 return VCD_ERROR_INVALID_PARAMETER;
1401 ret = vcd_server_mgr_cancel();
1403 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1404 return VCD_ERROR_OPERATION_FAILED;
1407 return VCD_ERROR_NONE;
1412 * VC Server Functions for Widget lib
1414 int vcd_server_widget_initialize(int pid)
1416 if (false == g_is_engine) {
1417 if (0 != vcd_engine_agent_initialize_current_engine()) {
1418 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1419 g_is_engine = false;
1420 return VCD_ERROR_ENGINE_NOT_FOUND;
1426 if (false == vcd_engine_is_available_engine()) {
1427 if (0 != vcd_engine_agent_initialize_current_engine()) {
1428 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1429 return VCD_ERROR_ENGINE_NOT_FOUND;
1433 /* check if pid is valid */
1434 if (true == vcd_client_widget_is_available(pid)) {
1435 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1436 return VCD_ERROR_INVALID_PARAMETER;
1439 /* Add client information to client manager */
1440 if (0 != vcd_client_widget_add(pid)) {
1441 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1442 return VCD_ERROR_OPERATION_FAILED;
1445 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
1447 return VCD_ERROR_NONE;
1450 int vcd_server_widget_finalize(int pid)
1452 /* check if pid is valid */
1453 if (false == vcd_client_widget_is_available(pid)) {
1454 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1455 return VCD_ERROR_INVALID_PARAMETER;
1458 /* Remove client information */
1459 if (0 != vcd_client_widget_delete(pid)) {
1460 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1463 if (0 == vcd_client_get_ref_count()) {
1464 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
1465 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1468 return VCD_ERROR_NONE;
1471 int vcd_server_widget_start_recording(int pid, bool widget_command)
1473 /* check if pid is valid */
1474 if (false == vcd_client_widget_is_available(pid)) {
1475 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1476 return VCD_ERROR_INVALID_PARAMETER;
1479 if (true == widget_command) {
1480 vcd_client_widget_set_command(pid);
1481 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
1483 vcd_client_widget_unset_command(pid);
1484 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
1487 int ret = __start_internal_recognition();
1489 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1490 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1496 int vcd_server_widget_start(int pid, bool stop_by_silence)
1498 /* check if pid is valid */
1499 int fore_pid = vcd_client_widget_get_foreground_pid();
1500 if (pid != fore_pid) {
1501 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1502 return VCD_ERROR_INVALID_PARAMETER;
1505 /* Check current state */
1506 vcd_state_e state = vcd_config_get_service_state();
1508 /* Service state should be ready */
1509 if (VCD_STATE_READY != state) {
1510 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1511 return VCD_ERROR_INVALID_STATE;
1514 vcd_client_set_slience_detection(stop_by_silence);
1516 /* Notify show tooltip */
1517 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1522 int vcd_server_widget_stop(int pid)
1524 /* check if pid is valid */
1525 int fore_pid = vcd_client_widget_get_foreground_pid();
1526 if (pid != fore_pid) {
1527 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1528 return VCD_ERROR_INVALID_PARAMETER;
1532 /* Check current state */
1533 vcd_state_e state = vcd_config_get_service_state();
1535 /* Service state should be recording */
1536 if (VCD_STATE_RECORDING != state) {
1537 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
1538 return VCD_ERROR_INVALID_STATE;
1541 ret = vcd_server_mgr_stop();
1543 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1544 return VCD_ERROR_OPERATION_FAILED;
1547 return VCD_ERROR_NONE;
1550 int vcd_server_widget_cancel(int pid)
1552 /* check if pid is valid */
1553 int fore_pid = vcd_client_widget_get_foreground_pid();
1554 if (pid != fore_pid) {
1555 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1556 return VCD_ERROR_INVALID_PARAMETER;
1560 /* Check current state */
1561 vcd_state_e state = vcd_config_get_service_state();
1563 /* Service state should be recording or processing */
1564 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1565 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1566 return VCD_ERROR_INVALID_STATE;
1569 ret = vcd_server_mgr_cancel();
1571 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
1575 return VCD_ERROR_NONE;