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 void __vcd_server_result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result,
248 const char* non_fixed_result, const char* msg, void *user_data)
250 if (VCD_STATE_PROCESSING != vcd_config_get_service_state()) {
251 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
252 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not 'Processing' and mode is not 'Restart continuously'");
257 vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
259 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
260 event, all_result, non_fixed_result, msg, count);
262 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
263 if (VCP_RESULT_EVENT_REJECTED == event) {
264 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
265 /* If no result and restart option is ON */
266 /* Send reject message */
267 bool temp = vcd_client_manager_get_exclusive();
268 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
269 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
270 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
273 ecore_timer_add(0, __restart_engine, NULL);
276 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
278 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
279 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
280 /* Restart option is ON */
281 ecore_timer_add(0, __restart_engine, NULL);
282 if (VCP_RESULT_EVENT_REJECTED == event) {
283 bool temp = vcd_client_manager_get_exclusive();
284 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
285 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
286 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
293 if (NULL == result_id) {
295 if (NULL != all_result) {
296 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
297 bool temp = vcd_client_manager_get_exclusive();
298 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
301 int pid = vcd_client_widget_get_foreground_pid();
303 if (NULL != all_result) {
304 /* Send result text to widget */
305 vcdc_send_result(pid, VC_COMMAND_TYPE_WIDGET);
308 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
309 /* Send to hide tooltip */
310 vcdc_send_show_tooltip(pid, false);
313 if (-1 != vcd_client_manager_get_pid()) {
314 /* Manager client is available */
315 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
316 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
320 vcd_client_manager_set_exclusive(false);
326 SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
329 vc_cmd_s* temp_cmd = NULL;
330 vc_cmd_list_h vc_cmd_list = NULL;
332 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
333 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to create command list");
334 vcd_client_manager_set_exclusive(false);
335 vcd_config_set_service_state(VCD_STATE_READY);
336 vcdc_send_service_state(VCD_STATE_READY);
341 for (i = 0; i < count; i++) {
342 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
344 if (result_id[i] < 0) {
345 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
349 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
350 if (0 == ret && NULL != temp_cmd) {
351 switch (temp_cmd->format) {
352 case VC_CMD_FORMAT_FIXED:
353 /* Nonfixed result is NOT valid */
355 case VC_CMD_FORMAT_FIXED_AND_EXTRA:
356 if (NULL == temp_cmd->parameter) {
357 if (NULL != non_fixed_result) {
358 temp_cmd->parameter = strdup(non_fixed_result);
361 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
364 case VC_CMD_FORMAT_EXTRA_AND_FIXED:
365 if (NULL == temp_cmd->command) {
366 if (NULL != non_fixed_result) {
367 temp_cmd->command = strdup(non_fixed_result);
370 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
375 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
379 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
380 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
381 vc_cmd_destroy((vc_cmd_h)temp_cmd);
384 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
388 vc_cmd_print_list(vc_cmd_list);
390 SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
392 int result_count = 0;
393 vc_cmd_list_get_count(vc_cmd_list, &result_count);
395 if (false == vcd_client_manager_get_exclusive()) {
396 int pid = vcd_client_widget_get_foreground_pid();
398 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
399 vcdc_send_show_tooltip(pid, false);
402 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
404 if (-1 != vcd_client_manager_get_pid()) {
405 /* Manager client is available */
406 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
407 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
410 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
411 /* Send result to client */
412 ecore_timer_add(0, __vcd_send_selected_result, NULL);
415 /* exclusive command */
416 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
418 if (-1 != vcd_client_manager_get_pid()) {
419 /* Manager client is available */
420 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
421 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
424 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
427 vcd_client_manager_set_exclusive(false);
430 vc_cmd_list_destroy(vc_cmd_list, true);
436 * vcd server Interfaces
438 static void __vcd_file_clean_up()
440 SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
445 struct dirent *dirp = NULL;
447 dp = opendir(VC_RUNTIME_INFO_ROOT);
449 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
453 char remove_path[256] = {0, };
455 ret = readdir_r(dp, &entry, &dirp);
457 SLOG(LOG_ERROR, TAG_VCD, "[File ERROR] Fail to read directory");
462 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
463 memset(remove_path, 0, 256);
464 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
467 if (0 != remove(remove_path)) {
468 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
470 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
474 } while (NULL != dirp);
485 /* Remove old file */
486 __vcd_file_clean_up();
488 /* initialize modules */
489 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
491 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
494 vcd_config_set_service_state(VCD_STATE_NONE);
496 ret = vcd_engine_agent_init(__vcd_server_result_cb);
498 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
502 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
503 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
504 return VCD_ERROR_OPERATION_FAILED;
508 ret = vcd_engine_agent_initialize_current_engine();
510 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
511 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
513 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
521 if (0 != vcd_engine_agent_load_current_engine()) {
522 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
523 return VCD_ERROR_OPERATION_FAILED;
526 /* Initialize manager info */
527 vcd_client_manager_unset();
529 vcd_config_set_service_state(VCD_STATE_READY);
530 vcdc_send_service_state(VCD_STATE_READY);
532 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
540 if (0 < g_list_length(g_proc_list)) {
541 iter = g_list_first(g_proc_list);
542 while (NULL != iter) {
543 g_proc_list = g_list_remove_link(g_proc_list, iter);
544 iter = g_list_first(g_proc_list);
548 vcd_state_e state = vcd_config_get_service_state();
549 if (VCD_STATE_READY != state) {
550 if (VCD_STATE_RECORDING == state) {
553 vcd_engine_recognize_cancel();
555 if (0 != vcd_recorder_destroy()) {
556 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
558 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
561 if (0 != vcd_engine_agent_release()) {
562 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
564 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
567 vcd_config_set_service_state(VCD_STATE_NONE);
568 vcdc_send_service_state(VCD_STATE_NONE);
570 SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
575 static Eina_Bool __finalize_quit_ecore_loop(void *data)
577 SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
578 ecore_main_loop_quit();
582 static void __read_proc()
586 struct dirent *dirp = NULL;
591 if (0 < g_list_length(g_proc_list)) {
592 iter = g_list_first(g_proc_list);
593 while (NULL != iter) {
594 g_proc_list = g_list_remove_link(g_proc_list, iter);
595 iter = g_list_first(g_proc_list);
599 dp = opendir("/proc");
601 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open proc");
604 ret = readdir_r(dp, &entry, &dirp);
606 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to readdir");
611 tmp = atoi(dirp->d_name);
612 if (0 >= tmp) continue;
613 g_proc_list = g_list_append(g_proc_list, GINT_TO_POINTER(tmp));
615 } while (NULL != dirp);
621 static void __vcd_cleanup_client(vcd_client_type_e type)
623 int* client_list = NULL;
624 int client_count = 0;
631 if (VCD_CLIENT_TYPE_NORMAL == type) {
632 ret = vcd_client_get_list(&client_list, &client_count);
633 } else if (VCD_CLIENT_TYPE_WIDGET == type) {
634 ret = vcd_client_widget_get_list(&client_list, &client_count);
635 } else if (VCD_CLIENT_TYPE_MANAGER == type) {
636 mgr_pid = vcd_client_manager_get_pid();
637 client_list = &mgr_pid;
641 if (0 == ret || mgr_pid > 0) {
642 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up %s client ", type ? (type == 1) ? "Widget" : "Manager" : "Normal");
643 if (NULL != client_list && client_count > 0) {
644 for (i = 0; i < client_count; i++) {
647 for (j = 0; j < g_list_length(g_proc_list); j++) {
648 iter = g_list_nth(g_proc_list, j);
650 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
651 SLOG(LOG_DEBUG, TAG_VCD, "%s pid(%d) is running", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
658 if (false == exist) {
659 SLOG(LOG_ERROR, TAG_VCD, "%s pid(%d) should be removed", type ? (type == 1) ? "Widget" : "Manager" : "Normal", client_list[i]);
660 if (VCD_CLIENT_TYPE_NORMAL == type)
661 vcd_server_finalize(client_list[i]);
662 else if (VCD_CLIENT_TYPE_WIDGET == type)
663 vcd_server_widget_finalize(client_list[i]);
665 vcd_server_mgr_finalize(mgr_pid);
669 SLOG(LOG_DEBUG, TAG_VCD, "=====");
670 SLOG(LOG_DEBUG, TAG_VCD, " ");
672 if (NULL != client_list && -1 == mgr_pid) {
679 Eina_Bool vcd_cleanup_client_all(void *data)
683 __vcd_cleanup_client(VCD_CLIENT_TYPE_NORMAL);
684 __vcd_cleanup_client(VCD_CLIENT_TYPE_WIDGET);
685 __vcd_cleanup_client(VCD_CLIENT_TYPE_MANAGER);
688 if (0 == vcd_client_get_list(&client_list, &client_count)) {
689 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
690 if (NULL != client_list && client_count > 0) {
691 for (i = 0; i < client_count; i++) {
694 for (j = 0; j < g_list_length(g_proc_list); j++) {
695 iter = g_list_nth(g_proc_list, j);
697 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
698 SLOG(LOG_DEBUG, TAG_VCD, "pid(%d) is running", client_list[i]);
705 if (false == exist) {
706 SLOG(LOG_ERROR, TAG_VCD, "pid(%d) should be removed", client_list[i]);
707 vcd_server_finalize(client_list[i]);
710 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
713 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
714 vcd_server_finalize(client_list[i]);
715 } else if (-1 == result) {
716 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
721 SLOG(LOG_DEBUG, TAG_VCD, "=====");
722 SLOG(LOG_DEBUG, TAG_VCD, " ");
724 if (NULL != client_list) {
729 /* If app is in background state, app cannot response message. */
730 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
731 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
732 if (NULL != client_list && client_count > 0) {
733 for (i = 0; i < client_count; i++) {
736 for (j = 0; j < g_list_length(g_proc_list); j++) {
737 iter = g_list_nth(g_proc_list, j);
739 if (client_list[i] == GPOINTER_TO_INT(iter->data)) {
740 SLOG(LOG_DEBUG, TAG_VCD, "widget pid(%d) is running", client_list[i]);
747 if (false == exist) {
748 SLOG(LOG_ERROR, TAG_VCD, "widget pid(%d) should be removed", client_list[i]);
749 vcd_server_widget_finalize(client_list[i]);
752 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
755 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
756 vcd_server_widget_finalize(client_list[i]);
757 } else if (-1 == result) {
758 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
763 SLOG(LOG_DEBUG, TAG_VCD, "=====");
764 SLOG(LOG_DEBUG, TAG_VCD, " ");
767 if (NULL != client_list) {
775 int mgr_pid = vcd_client_manager_get_pid();
777 for (j = 0; j < g_list_length(g_proc_list); j++) {
778 iter = g_list_nth(g_proc_list, j);
780 if (mgr_pid == GPOINTER_TO_INT(iter->data)) {
781 SLOG(LOG_DEBUG, TAG_VCD, "manager pid(%d) is running", mgr_pid);
788 if (false == exist) {
789 SLOG(LOG_ERROR, TAG_VCD, "manager pid (%d) should be removed", mgr_pid);
790 vcd_server_mgr_finalize(mgr_pid);
797 int vcd_server_get_service_state()
799 return vcd_config_get_service_state();
802 int vcd_server_get_foreground()
805 vcd_config_get_foreground(&pid);
813 int vcd_server_mgr_initialize(int pid)
815 if (false == g_is_engine) {
816 if (0 != vcd_engine_agent_initialize_current_engine()) {
817 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
819 return VCD_ERROR_ENGINE_NOT_FOUND;
825 /* check if pid is valid */
826 if (false == vcd_client_manager_is_valid(pid)) {
827 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid(%d) is already exist", pid);
828 return VCD_ERROR_INVALID_PARAMETER;
831 /* Add client information to client manager */
832 if (0 != vcd_client_manager_set(pid)) {
833 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
834 return VCD_ERROR_OPERATION_FAILED;
837 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
839 return VCD_ERROR_NONE;
842 int vcd_server_mgr_finalize(int pid)
844 /* check if pid is valid */
845 if (false == vcd_client_manager_is_valid(pid)) {
846 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
847 return VCD_ERROR_INVALID_PARAMETER;
850 /* Cancel recognition */
851 vcd_server_mgr_cancel();
853 /* Remove manager information */
854 if (0 != vcd_client_manager_unset()) {
855 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
858 if (0 == vcd_client_get_ref_count()) {
859 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
860 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
863 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
865 return VCD_ERROR_NONE;
868 int vcd_server_mgr_set_command(int pid)
870 if (0 != vcd_client_manager_set_command(pid)) {
871 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
872 return VCD_ERROR_INVALID_PARAMETER;
874 return VCD_ERROR_NONE;
877 int vcd_server_mgr_unset_command(int pid)
879 if (0 != vcd_client_manager_unset_command(pid)) {
880 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
881 return VCD_ERROR_INVALID_PARAMETER;
883 return VCD_ERROR_NONE;
886 int vcd_server_mgr_set_demandable_client(int pid)
888 /* check if pid is valid */
889 if (false == vcd_client_manager_is_valid(pid)) {
890 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
891 return VCD_ERROR_INVALID_PARAMETER;
894 GSList* client_list = NULL;
895 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
896 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
897 return VCD_ERROR_OPERATION_FAILED;
900 /* Save client list */
901 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
902 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
903 return VCD_ERROR_OPERATION_FAILED;
906 return VCD_ERROR_NONE;
909 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
911 /* check if pid is valid */
912 if (false == vcd_client_manager_is_valid(pid)) {
913 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
914 return VCD_ERROR_INVALID_PARAMETER;
918 vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
922 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
924 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
925 return VCD_ERROR_OPERATION_FAILED;
928 ret = vcd_recorder_set(audio_type, type, rate, channel);
930 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
931 return VCD_ERROR_OPERATION_FAILED;
934 return VCD_ERROR_NONE;
937 int vcd_server_mgr_get_audio_type(int pid, 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;
945 int ret = vcd_recorder_get(audio_type);
947 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
948 return VCD_ERROR_OPERATION_FAILED;
951 return VCD_ERROR_NONE;
954 int vcd_server_mgr_set_client_info(int pid)
956 /* check if pid is valid */
957 if (false == vcd_client_manager_is_valid(pid)) {
958 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
959 return VCD_ERROR_INVALID_PARAMETER;
962 int ret = vcd_client_save_client_info();
964 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
965 return VCD_ERROR_OPERATION_FAILED;
968 return VCD_ERROR_NONE;
971 static int __start_internal_recognition()
975 /* 2. Get commands */
976 ret = vcd_client_command_collect_command();
978 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
979 return VCD_ERROR_OPERATION_FAILED;
982 ret = vcd_client_get_length();
984 SLOG(LOG_WARN, TAG_VCD, "[Server WARNIING] No current command : %d", ret);
985 return VCD_ERROR_OPERATION_FAILED;
988 /* 3. Set command to engine */
989 ret = vcd_engine_set_commands();
991 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
992 return VCD_ERROR_OPERATION_FAILED;
995 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
997 bool stop_by_silence = true;
998 if (VCD_RECOGNITION_MODE_MANUAL == vcd_client_get_recognition_mode()) {
999 stop_by_silence = false;
1002 /* 4. start recognition */
1003 ret = vcd_engine_recognize_start(stop_by_silence);
1005 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
1006 return VCD_ERROR_OPERATION_FAILED;
1009 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
1011 /* 5. recorder start */
1012 ret = vcd_recorder_start();
1014 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
1015 vcd_engine_recognize_cancel();
1019 vcd_config_set_service_state(VCD_STATE_RECORDING);
1020 vcdc_send_service_state(VCD_STATE_RECORDING);
1022 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition(%d)", stop_by_silence);
1027 static Eina_Bool __vcd_request_show_tooltip(void *data)
1029 int pid = vcd_client_widget_get_foreground_pid();
1031 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1032 vcdc_send_show_tooltip(pid, (bool)data);
1038 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
1040 /* 1. check current state */
1041 vcd_state_e state = vcd_config_get_service_state();
1043 if (VCD_STATE_READY != state) {
1044 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
1045 return VCD_ERROR_INVALID_STATE;
1048 SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
1049 vcd_client_set_recognition_mode(recognition_mode);
1051 if (false == exclusive_cmd) {
1052 /* Notify show tooltip */
1053 int pid = vcd_client_widget_get_foreground_pid();
1055 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
1056 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1060 vcd_client_manager_set_exclusive(exclusive_cmd);
1064 if (true == start_by_client) {
1065 /* Get foreground pid */
1066 if (0 != vcd_config_get_foreground(&fg_pid)) {
1067 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
1070 /* Set client exclusive option */
1071 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
1072 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
1076 int ret = __start_internal_recognition();
1078 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1082 if (true == start_by_client) {
1083 vcd_client_unset_exclusive_command(fg_pid);
1086 return VCD_ERROR_NONE;
1089 int vcd_server_mgr_stop()
1091 /* 1. Check current state is recording */
1092 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
1093 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
1094 return VCD_ERROR_INVALID_STATE;
1097 /* 2. Stop recorder */
1098 vcd_recorder_stop();
1100 /* 3. Stop engine recognition */
1101 int ret = vcd_engine_recognize_stop();
1103 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
1106 /* 4. Set original mode */
1107 vcd_config_set_service_state(VCD_STATE_PROCESSING);
1108 vcdc_send_service_state(VCD_STATE_PROCESSING);
1110 return VCD_ERROR_NONE;
1113 int vcd_server_mgr_cancel()
1115 /* 1. Check current state */
1116 vcd_state_e state = vcd_config_get_service_state();
1117 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1118 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
1119 return VCD_ERROR_INVALID_STATE;
1122 /* 2. Stop recorder */
1123 vcd_recorder_stop();
1124 /* 3. Cancel engine */
1125 int ret = vcd_engine_recognize_cancel();
1127 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
1130 if (false == vcd_client_manager_get_exclusive()) {
1131 int pid = vcd_client_widget_get_foreground_pid();
1133 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
1134 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1137 vcd_client_manager_set_exclusive(false);
1141 vcd_config_set_service_state(VCD_STATE_READY);
1142 vcdc_send_service_state(VCD_STATE_READY);
1144 return VCD_ERROR_NONE;
1148 int vcd_server_mgr_result_select()
1150 __vcd_send_selected_result(NULL);
1152 return VCD_ERROR_NONE;
1156 * VC Server Functions for Client
1158 int vcd_server_initialize(int pid)
1160 if (false == g_is_engine) {
1161 if (0 != vcd_engine_agent_initialize_current_engine()) {
1162 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1163 g_is_engine = false;
1164 return VCD_ERROR_ENGINE_NOT_FOUND;
1170 if (false == vcd_engine_is_available_engine()) {
1171 if (0 != vcd_engine_agent_initialize_current_engine()) {
1172 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1173 return VCD_ERROR_ENGINE_NOT_FOUND;
1177 /* check if pid is valid */
1178 if (true == vcd_client_is_available(pid)) {
1179 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1180 return VCD_ERROR_INVALID_PARAMETER;
1183 /* Add client information to client manager */
1184 if (0 != vcd_client_add(pid)) {
1185 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1186 return VCD_ERROR_OPERATION_FAILED;
1189 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1191 return VCD_ERROR_NONE;
1194 int vcd_server_finalize(int pid)
1196 /* check if pid is valid */
1197 if (false == vcd_client_is_available(pid)) {
1198 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1199 return VCD_ERROR_INVALID_PARAMETER;
1202 /* Remove client information */
1203 if (0 != vcd_client_delete(pid)) {
1204 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1207 if (0 == vcd_client_get_ref_count()) {
1208 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1209 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1212 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1214 return VCD_ERROR_NONE;
1217 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1219 /* check if pid is valid */
1220 if (false == vcd_client_is_available(pid)) {
1221 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1222 return VCD_ERROR_INVALID_PARAMETER;
1225 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1226 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1227 return VCD_ERROR_OPERATION_FAILED;
1233 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1235 /* check if pid is valid */
1236 if (false == vcd_client_is_available(pid)) {
1237 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1238 return VCD_ERROR_INVALID_PARAMETER;
1241 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1242 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1243 return VCD_ERROR_OPERATION_FAILED;
1249 int vcd_server_set_foreground(int pid, bool value)
1251 /* check if pid is valid */
1252 if (false == vcd_client_is_available(pid) && false == vcd_client_widget_is_available(pid)) {
1253 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1254 return VCD_ERROR_INVALID_PARAMETER;
1257 if (0 != vcd_config_set_foreground(pid, value)) {
1258 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set foreground : pid(%d), value(%d)", pid, value);
1259 return VCD_ERROR_OPERATION_FAILED;
1266 int vcd_server_set_exclusive_command(int pid, bool value)
1268 /* check if pid is valid */
1269 if (false == vcd_client_is_available(pid)) {
1270 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1271 return VCD_ERROR_INVALID_PARAMETER;
1274 if (true == value) {
1275 if (0 != vcd_client_set_exclusive_command(pid)) {
1276 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
1277 return VCD_ERROR_OPERATION_FAILED;
1280 if (0 != vcd_client_unset_exclusive_command(pid)) {
1281 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
1282 return VCD_ERROR_OPERATION_FAILED;
1289 int vcd_server_request_start(int pid, bool stop_by_silence)
1291 /* check if pid is valid */
1292 if (false == vcd_client_is_available(pid)) {
1293 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
1294 return VCD_ERROR_INVALID_PARAMETER;
1298 /* Check current state */
1299 vcd_state_e state = vcd_config_get_service_state();
1301 /* Service state should be ready */
1302 if (VCD_STATE_READY != state) {
1303 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1304 return VCD_ERROR_INVALID_STATE;
1307 if (-1 != vcd_client_manager_get_pid()) {
1308 /* Check current pid is valid */
1309 if (false == vcd_client_manager_check_demandable_client(pid)) {
1310 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1311 return VCD_ERROR_INVALID_PARAMETER;
1315 ret = vcd_server_mgr_start(stop_by_silence, false);
1317 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1318 return VCD_ERROR_INVALID_PARAMETER;
1324 int vcd_server_request_stop(int pid)
1327 /* Check current state */
1328 vcd_state_e state = vcd_config_get_service_state();
1330 /* Service state should be ready */
1331 if (VCD_STATE_RECORDING != state) {
1332 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
1333 return VCD_ERROR_INVALID_STATE;
1336 if (-1 != vcd_client_manager_get_pid()) {
1337 /* Check current pid is valid */
1338 if (false == vcd_client_manager_check_demandable_client(pid)) {
1339 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1340 return VCD_ERROR_INVALID_PARAMETER;
1344 ret = vcd_server_mgr_stop();
1346 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1347 return VCD_ERROR_OPERATION_FAILED;
1350 return VCD_ERROR_NONE;
1353 int vcd_server_request_cancel(int pid)
1356 /* Check current state */
1357 vcd_state_e state = vcd_config_get_service_state();
1359 /* Service state should be recording or processing */
1360 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1361 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1362 return VCD_ERROR_INVALID_STATE;
1365 if (-1 != vcd_client_manager_get_pid()) {
1366 /* Check current pid is valid */
1367 if (false == vcd_client_manager_check_demandable_client(pid)) {
1368 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1369 return VCD_ERROR_INVALID_PARAMETER;
1373 ret = vcd_server_mgr_cancel();
1375 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1376 return VCD_ERROR_OPERATION_FAILED;
1379 return VCD_ERROR_NONE;
1384 * VC Server Functions for Widget lib
1386 int vcd_server_widget_initialize(int pid)
1388 if (false == g_is_engine) {
1389 if (0 != vcd_engine_agent_initialize_current_engine()) {
1390 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1391 g_is_engine = false;
1392 return VCD_ERROR_ENGINE_NOT_FOUND;
1398 if (false == vcd_engine_is_available_engine()) {
1399 if (0 != vcd_engine_agent_initialize_current_engine()) {
1400 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1401 return VCD_ERROR_ENGINE_NOT_FOUND;
1405 /* check if pid is valid */
1406 if (true == vcd_client_widget_is_available(pid)) {
1407 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1408 return VCD_ERROR_INVALID_PARAMETER;
1411 /* Add client information to client manager */
1412 if (0 != vcd_client_widget_add(pid)) {
1413 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1414 return VCD_ERROR_OPERATION_FAILED;
1417 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
1419 return VCD_ERROR_NONE;
1422 int vcd_server_widget_finalize(int pid)
1424 /* check if pid is valid */
1425 if (false == vcd_client_widget_is_available(pid)) {
1426 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1427 return VCD_ERROR_INVALID_PARAMETER;
1430 /* Remove client information */
1431 if (0 != vcd_client_widget_delete(pid)) {
1432 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1435 if (0 == vcd_client_get_ref_count()) {
1436 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
1437 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1440 return VCD_ERROR_NONE;
1443 int vcd_server_widget_start_recording(int pid, bool widget_command)
1445 /* check if pid is valid */
1446 if (false == vcd_client_widget_is_available(pid)) {
1447 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1448 return VCD_ERROR_INVALID_PARAMETER;
1451 if (true == widget_command) {
1452 vcd_client_widget_set_command(pid);
1453 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
1455 vcd_client_widget_unset_command(pid);
1456 SLOG(LOG_WARN, TAG_VCD, "[Server] widget command is NOT available");
1459 int ret = __start_internal_recognition();
1461 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1462 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1468 int vcd_server_widget_start(int pid, bool stop_by_silence)
1470 /* check if pid is valid */
1471 int fore_pid = vcd_client_widget_get_foreground_pid();
1472 if (pid != fore_pid) {
1473 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1474 return VCD_ERROR_INVALID_PARAMETER;
1477 /* Check current state */
1478 vcd_state_e state = vcd_config_get_service_state();
1480 /* Service state should be ready */
1481 if (VCD_STATE_READY != state) {
1482 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1483 return VCD_ERROR_INVALID_STATE;
1486 vcd_client_set_slience_detection(stop_by_silence);
1488 /* Notify show tooltip */
1489 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1494 int vcd_server_widget_stop(int pid)
1496 /* check if pid is valid */
1497 int fore_pid = vcd_client_widget_get_foreground_pid();
1498 if (pid != fore_pid) {
1499 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1500 return VCD_ERROR_INVALID_PARAMETER;
1504 /* Check current state */
1505 vcd_state_e state = vcd_config_get_service_state();
1507 /* Service state should be recording */
1508 if (VCD_STATE_RECORDING != state) {
1509 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
1510 return VCD_ERROR_INVALID_STATE;
1513 ret = vcd_server_mgr_stop();
1515 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1516 return VCD_ERROR_OPERATION_FAILED;
1519 return VCD_ERROR_NONE;
1522 int vcd_server_widget_cancel(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 or processing */
1536 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1537 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1538 return VCD_ERROR_INVALID_STATE;
1541 ret = vcd_server_mgr_cancel();
1543 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
1547 return VCD_ERROR_NONE;