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;
38 * VC Server Internal Functions
40 static Eina_Bool __stop_by_silence(void *data)
42 SLOG(LOG_DEBUG, TAG_VCD, "===== Silence Detected ");
44 vcd_server_mgr_stop();
46 SLOG(LOG_DEBUG, TAG_VCD, "=====");
47 SLOG(LOG_DEBUG, TAG_VCD, " ");
51 static Eina_Bool __cancel_by_interrupt(void *data)
53 SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by interrupt");
55 vcd_server_mgr_cancel();
57 SLOG(LOG_DEBUG, TAG_VCD, "=====");
58 SLOG(LOG_DEBUG, TAG_VCD, " ");
62 static Eina_Bool __restart_engine(void *data)
64 SLOG(LOG_DEBUG, TAG_VCD, "===== Restart by no result");
66 /* Restart recognition */
67 int ret = vcd_engine_recognize_start(true);
69 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to restart recognition : result(%d)", ret);
73 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
75 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
76 vcd_config_set_service_state(VCD_STATE_RECORDING);
77 vcdc_send_service_state(VCD_STATE_RECORDING);
80 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Restart recognition");
82 SLOG(LOG_DEBUG, TAG_VCD, "=====");
83 SLOG(LOG_DEBUG, TAG_VCD, " ");
87 static int __server_recorder_callback(const void* data, const unsigned int length)
89 vcd_state_e state = vcd_config_get_service_state();
90 if (VCD_STATE_RECORDING != state) {
91 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Skip by engine processing");
95 vcp_speech_detect_e speech_detected = VCP_SPEECH_DETECT_NONE;
98 ret = vcd_engine_recognize_audio(data, length, &speech_detected);
102 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set recording data to engine(%d)", ret);
103 ecore_timer_add(0, __cancel_by_interrupt, NULL);
107 if (VCP_SPEECH_DETECT_BEGIN == speech_detected) {
108 if (-1 != vcd_client_manager_get_pid()) {
109 /* Manager client is available */
110 if (0 != vcdc_send_speech_detected(vcd_client_manager_get_pid())) {
111 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send speech detected");
114 } else if (VCP_SPEECH_DETECT_END == speech_detected) {
115 if (VCD_RECOGNITION_MODE_STOP_BY_SILENCE == vcd_client_get_recognition_mode()) {
116 /* silence detected */
117 ecore_timer_add(0, __stop_by_silence, NULL);
118 } else if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
119 /* Stop engine recognition */
120 int ret = vcd_engine_recognize_stop();
122 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
124 vcd_config_set_service_state(VCD_STATE_PROCESSING);
125 vcdc_send_service_state(VCD_STATE_PROCESSING);
127 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop engine only by silence");
128 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
129 /* Stop engine recognition */
130 int ret = vcd_engine_recognize_stop();
132 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
140 void __server_recorder_interrupt_callback()
142 SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by sound interrupt");
144 ecore_timer_add(0, __cancel_by_interrupt, NULL);
146 SLOG(LOG_DEBUG, TAG_VCD, "=====");
147 SLOG(LOG_DEBUG, TAG_VCD, " ");
150 static void __config_lang_changed_cb(const char* current_lang, void* user_data)
152 SLOG(LOG_DEBUG, TAG_VCD, "===== Change language ");
154 /* Current state is recording */
155 vcd_state_e state = vcd_config_get_service_state();
156 if (VCD_STATE_RECORDING == state || VCD_STATE_PROCESSING == state) {
157 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is 'Recording'. Cancel recognition");
158 vcd_server_mgr_cancel();
162 ret = vcd_engine_set_current_language(current_lang);
164 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set language of engine : %d", ret);
167 SLOG(LOG_DEBUG, TAG_VCD, "=====");
168 SLOG(LOG_DEBUG, TAG_VCD, " ");
173 static void __config_foreground_changed_cb(int previous, int current, void* user_data)
175 SLOG(LOG_DEBUG, TAG_VCD, "===== Change foreground");
177 SLOG(LOG_DEBUG, TAG_VCD, "Foreground pid(%d)", current);
179 if (VC_NO_FOREGROUND_PID != current) {
180 /* Foreground app is changed */
181 vcd_state_e state = vcd_config_get_service_state();
182 if (VCD_STATE_RECORDING == state) {
183 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Foreground pid(%d) is changed. Cancel recognition", current);
184 ecore_timer_add(0, __cancel_by_interrupt, NULL);
188 SLOG(LOG_DEBUG, TAG_VCD, "=====");
189 SLOG(LOG_DEBUG, TAG_VCD, " ");
194 static Eina_Bool __vcd_send_selected_result(void *data)
196 GSList* pid_list = NULL;
197 if (0 != vc_info_parser_get_result_pid_list(&pid_list)) {
198 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get pid list. No result");
200 if (0 < g_slist_length(pid_list)) {
202 vc_cmd_s* temp_cmd = NULL;
206 iter = g_slist_nth(pid_list, 0);
207 while (NULL != iter) {
208 temp_cmd = iter->data;
210 if (NULL != temp_cmd) {
213 /* send result noti */
214 ret = vcdc_send_result(temp_cmd->pid, temp_cmd->type);
216 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
217 if (VCD_ERROR_TIMED_OUT != ret) {
221 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
225 if (100 == count) break;
226 /* While is retry code */
231 pid_list = g_slist_remove_link(pid_list, iter);
232 iter = g_slist_nth(pid_list, 0);
237 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
238 vcd_config_set_service_state(VCD_STATE_READY);
239 vcdc_send_service_state(VCD_STATE_READY);
245 static void __vcd_server_result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result,
246 const char* non_fixed_result, const char* msg, void *user_data)
248 if (VCD_STATE_PROCESSING != vcd_config_get_service_state()) {
249 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
250 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not 'Processing' and mode is not 'Restart continuously'");
255 vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
257 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
258 event, all_result, non_fixed_result, msg, count);
260 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
261 if (VCP_RESULT_EVENT_REJECTED == event) {
262 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
263 /* If no result and restart option is ON */
264 /* Send reject message */
265 bool temp = vcd_client_manager_get_exclusive();
266 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
267 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
268 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
271 ecore_timer_add(0, __restart_engine, NULL);
274 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
276 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
277 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
278 /* Restart option is ON */
279 ecore_timer_add(0, __restart_engine, NULL);
280 if (VCP_RESULT_EVENT_REJECTED == event) {
281 bool temp = vcd_client_manager_get_exclusive();
282 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
283 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
284 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
291 if (NULL == result_id) {
293 if (NULL != all_result) {
294 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
295 bool temp = vcd_client_manager_get_exclusive();
296 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
299 int pid = vcd_client_widget_get_foreground_pid();
301 if (NULL != all_result) {
302 /* Send result text to widget */
303 vcdc_send_result(pid, VC_COMMAND_TYPE_WIDGET);
306 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
307 /* Send to hide tooltip */
308 vcdc_send_show_tooltip(pid, false);
311 if (-1 != vcd_client_manager_get_pid()) {
312 /* Manager client is available */
313 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
314 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
318 vcd_client_manager_set_exclusive(false);
324 SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
327 vc_cmd_s* temp_cmd = NULL;
328 vc_cmd_list_h vc_cmd_list = NULL;
330 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
331 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to create command list");
332 vcd_client_manager_set_exclusive(false);
333 vcd_config_set_service_state(VCD_STATE_READY);
334 vcdc_send_service_state(VCD_STATE_READY);
339 for (i = 0; i < count; i++) {
340 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
342 if (result_id[i] < 0) {
343 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
347 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
348 if (0 == ret && NULL != temp_cmd) {
349 switch (temp_cmd->format) {
350 case VC_CMD_FORMAT_FIXED:
351 /* Nonfixed result is NOT valid */
353 case VC_CMD_FORMAT_FIXED_AND_EXTRA:
354 if (NULL == temp_cmd->parameter) {
355 if (NULL != non_fixed_result) {
356 temp_cmd->parameter = strdup(non_fixed_result);
359 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
362 case VC_CMD_FORMAT_EXTRA_AND_FIXED:
363 if (NULL == temp_cmd->command) {
364 if (NULL != non_fixed_result) {
365 temp_cmd->command = strdup(non_fixed_result);
368 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
373 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
377 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
378 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
379 vc_cmd_destroy((vc_cmd_h)temp_cmd);
382 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
386 vc_cmd_print_list(vc_cmd_list);
388 SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
390 int result_count = 0;
391 vc_cmd_list_get_count(vc_cmd_list, &result_count);
393 if (false == vcd_client_manager_get_exclusive()) {
394 int pid = vcd_client_widget_get_foreground_pid();
396 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
397 vcdc_send_show_tooltip(pid, false);
400 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
402 if (-1 != vcd_client_manager_get_pid()) {
403 /* Manager client is available */
404 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
405 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
408 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
409 /* Send result to client */
410 ecore_timer_add(0, __vcd_send_selected_result, NULL);
413 /* exclusive command */
414 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
416 if (-1 != vcd_client_manager_get_pid()) {
417 /* Manager client is available */
418 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
419 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
422 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
425 vcd_client_manager_set_exclusive(false);
428 vc_cmd_list_destroy(vc_cmd_list, true);
434 * vcd server Interfaces
436 static void __vcd_file_clean_up()
438 SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
443 struct dirent *dirp = NULL;
445 dp = opendir(VC_RUNTIME_INFO_ROOT);
447 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
451 char remove_path[256] = {0, };
453 ret = readdir_r(dp, &entry, &dirp);
455 SLOG(LOG_ERROR, TAG_VCD, "[File ERROR] Fail to read directory");
460 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
461 memset(remove_path, 0, 256);
462 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
465 if (0 != remove(remove_path)) {
466 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
468 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
472 } while (NULL != dirp);
483 /* Remove old file */
484 __vcd_file_clean_up();
486 /* initialize modules */
487 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
489 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
492 vcd_config_set_service_state(VCD_STATE_NONE);
494 ret = vcd_engine_agent_init(__vcd_server_result_cb);
496 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
500 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
501 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
502 return VCD_ERROR_OPERATION_FAILED;
506 ret = vcd_engine_agent_initialize_current_engine();
508 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
509 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
511 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
519 if (0 != vcd_engine_agent_load_current_engine()) {
520 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
521 return VCD_ERROR_OPERATION_FAILED;
524 /* Initialize manager info */
525 vcd_client_manager_unset();
527 vcd_config_set_service_state(VCD_STATE_READY);
528 vcdc_send_service_state(VCD_STATE_READY);
530 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
537 vcd_state_e state = vcd_config_get_service_state();
538 if (VCD_STATE_READY != state) {
539 if (VCD_STATE_RECORDING == state) {
542 vcd_engine_recognize_cancel();
544 if (0 != vcd_recorder_destroy()) {
545 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
547 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
550 if (0 != vcd_engine_agent_release()) {
551 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
553 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
556 vcd_config_set_service_state(VCD_STATE_NONE);
557 vcdc_send_service_state(VCD_STATE_NONE);
559 SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
564 static Eina_Bool __finalize_quit_ecore_loop(void *data)
566 SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
567 ecore_main_loop_quit();
571 Eina_Bool vcd_cleanup_client(void *data)
574 int* client_list = NULL;
575 int client_count = 0;
579 if (0 == vcd_client_get_list(&client_list, &client_count)) {
580 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
581 if (NULL != client_list && client_count > 0) {
582 for (i = 0; i < client_count; i++) {
583 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
586 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
587 vcd_server_finalize(client_list[i]);
588 } else if (-1 == result) {
589 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
593 SLOG(LOG_DEBUG, TAG_VCD, "=====");
594 SLOG(LOG_DEBUG, TAG_VCD, " ");
596 if (NULL != client_list) {
603 /* If app is in background state, app cannot response message. */
604 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
605 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
606 if (NULL != client_list && client_count > 0) {
607 for (i = 0; i < client_count; i++) {
608 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
611 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
612 vcd_server_widget_finalize(client_list[i]);
613 } else if (-1 == result) {
614 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
618 SLOG(LOG_DEBUG, TAG_VCD, "=====");
619 SLOG(LOG_DEBUG, TAG_VCD, " ");
622 if (NULL != client_list) {
633 int vcd_server_get_service_state()
635 return vcd_config_get_service_state();
641 int vcd_server_mgr_initialize(int pid)
643 if (false == g_is_engine) {
644 if (0 != vcd_engine_agent_initialize_current_engine()) {
645 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
647 return VCD_ERROR_ENGINE_NOT_FOUND;
653 /* check if pid is valid */
654 if (false == vcd_client_manager_is_valid(pid)) {
655 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid(%d) is already exist", pid);
656 return VCD_ERROR_INVALID_PARAMETER;
659 /* Add client information to client manager */
660 if (0 != vcd_client_manager_set(pid)) {
661 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
662 return VCD_ERROR_OPERATION_FAILED;
665 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
667 return VCD_ERROR_NONE;
670 int vcd_server_mgr_finalize(int pid)
672 /* check if pid is valid */
673 if (false == vcd_client_manager_is_valid(pid)) {
674 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
675 return VCD_ERROR_INVALID_PARAMETER;
678 /* Remove manager information */
679 if (0 != vcd_client_manager_unset()) {
680 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
683 if (0 == vcd_client_get_ref_count()) {
684 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
685 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
688 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
690 return VCD_ERROR_NONE;
693 int vcd_server_mgr_set_command(int pid)
695 if (0 != vcd_client_manager_set_command(pid)) {
696 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
697 return VCD_ERROR_INVALID_PARAMETER;
699 return VCD_ERROR_NONE;
702 int vcd_server_mgr_unset_command(int pid)
704 if (0 != vcd_client_manager_unset_command(pid)) {
705 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
706 return VCD_ERROR_INVALID_PARAMETER;
708 return VCD_ERROR_NONE;
711 int vcd_server_mgr_set_demandable_client(int pid)
713 /* check if pid is valid */
714 if (false == vcd_client_manager_is_valid(pid)) {
715 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
716 return VCD_ERROR_INVALID_PARAMETER;
719 GSList* client_list = NULL;
720 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
721 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
722 return VCD_ERROR_OPERATION_FAILED;
725 /* Save client list */
726 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
727 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
728 return VCD_ERROR_OPERATION_FAILED;
731 return VCD_ERROR_NONE;
734 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
736 /* check if pid is valid */
737 if (false == vcd_client_manager_is_valid(pid)) {
738 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
739 return VCD_ERROR_INVALID_PARAMETER;
743 vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
747 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
749 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
750 return VCD_ERROR_OPERATION_FAILED;
753 ret = vcd_recorder_set(audio_type, type, rate, channel);
755 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
756 return VCD_ERROR_OPERATION_FAILED;
759 return VCD_ERROR_NONE;
762 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
764 /* check if pid is valid */
765 if (false == vcd_client_manager_is_valid(pid)) {
766 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
767 return VCD_ERROR_INVALID_PARAMETER;
770 int ret = vcd_recorder_get(audio_type);
772 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
773 return VCD_ERROR_OPERATION_FAILED;
776 return VCD_ERROR_NONE;
779 int vcd_server_mgr_set_client_info(int pid)
781 /* check if pid is valid */
782 if (false == vcd_client_manager_is_valid(pid)) {
783 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
784 return VCD_ERROR_INVALID_PARAMETER;
787 int ret = vcd_client_save_client_info();
789 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
790 return VCD_ERROR_OPERATION_FAILED;
793 return VCD_ERROR_NONE;
796 static int __start_internal_recognition()
800 /* 2. Get commands */
801 ret = vcd_client_command_collect_command();
803 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
804 return VCD_ERROR_OPERATION_FAILED;
807 ret = vcd_client_get_length();
809 SLOG(LOG_WARN, TAG_VCD, "[Server WARNIING] No current command : %d", ret);
810 return VCD_ERROR_OPERATION_FAILED;
813 /* 3. Set command to engine */
814 ret = vcd_engine_set_commands();
816 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
817 return VCD_ERROR_OPERATION_FAILED;
820 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
822 /* 4. start recognition */
823 ret = vcd_engine_recognize_start(true);
825 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
826 return VCD_ERROR_OPERATION_FAILED;
829 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
831 /* 5. recorder start */
832 ret = vcd_recorder_start();
834 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
835 vcd_engine_recognize_cancel();
839 vcd_config_set_service_state(VCD_STATE_RECORDING);
840 vcdc_send_service_state(VCD_STATE_RECORDING);
842 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition");
847 static Eina_Bool __vcd_request_show_tooltip(void *data)
849 int pid = vcd_client_widget_get_foreground_pid();
851 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
852 vcdc_send_show_tooltip(pid, (bool)data);
858 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
860 /* 1. check current state */
861 vcd_state_e state = vcd_config_get_service_state();
863 if (VCD_STATE_READY != state) {
864 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
865 return VCD_ERROR_INVALID_STATE;
868 SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
869 vcd_client_set_recognition_mode(recognition_mode);
871 if (false == exclusive_cmd) {
872 /* Notify show tooltip */
873 int pid = vcd_client_widget_get_foreground_pid();
875 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
876 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
880 vcd_client_manager_set_exclusive(exclusive_cmd);
884 if (true == start_by_client) {
885 /* Get foreground pid */
886 if (0 != vcd_config_get_foreground(&fg_pid)) {
887 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
890 /* Set client exclusive option */
891 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
892 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
896 int ret = __start_internal_recognition();
898 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
902 if (true == start_by_client) {
903 vcd_client_unset_exclusive_command(fg_pid);
906 return VCD_ERROR_NONE;
909 int vcd_server_mgr_stop()
911 /* 1. Check current state is recording */
912 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
913 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
914 return VCD_ERROR_INVALID_STATE;
917 /* 2. Stop recorder */
920 /* 3. Stop engine recognition */
921 int ret = vcd_engine_recognize_stop();
923 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
926 /* 4. Set original mode */
927 vcd_config_set_service_state(VCD_STATE_PROCESSING);
928 vcdc_send_service_state(VCD_STATE_PROCESSING);
930 return VCD_ERROR_NONE;
933 int vcd_server_mgr_cancel()
935 /* 1. Check current state */
936 vcd_state_e state = vcd_config_get_service_state();
937 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
938 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
939 return VCD_ERROR_INVALID_STATE;
942 /* 2. Stop recorder */
944 /* 3. Cancel engine */
945 int ret = vcd_engine_recognize_cancel();
947 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
950 if (false == vcd_client_manager_get_exclusive()) {
951 int pid = vcd_client_widget_get_foreground_pid();
953 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
954 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
957 vcd_client_manager_set_exclusive(false);
961 vcd_config_set_service_state(VCD_STATE_READY);
962 vcdc_send_service_state(VCD_STATE_READY);
964 return VCD_ERROR_NONE;
968 int vcd_server_mgr_result_select()
970 __vcd_send_selected_result(NULL);
972 return VCD_ERROR_NONE;
976 * VC Server Functions for Client
978 int vcd_server_initialize(int pid)
980 if (false == g_is_engine) {
981 if (0 != vcd_engine_agent_initialize_current_engine()) {
982 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
984 return VCD_ERROR_ENGINE_NOT_FOUND;
990 if (false == vcd_engine_is_available_engine()) {
991 if (0 != vcd_engine_agent_initialize_current_engine()) {
992 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
993 return VCD_ERROR_ENGINE_NOT_FOUND;
997 /* check if pid is valid */
998 if (true == vcd_client_is_available(pid)) {
999 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1000 return VCD_ERROR_INVALID_PARAMETER;
1003 /* Add client information to client manager */
1004 if (0 != vcd_client_add(pid)) {
1005 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1006 return VCD_ERROR_OPERATION_FAILED;
1009 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1011 return VCD_ERROR_NONE;
1014 int vcd_server_finalize(int pid)
1016 /* check if pid is valid */
1017 if (false == vcd_client_is_available(pid)) {
1018 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1019 return VCD_ERROR_INVALID_PARAMETER;
1022 /* Remove client information */
1023 if (0 != vcd_client_delete(pid)) {
1024 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1027 if (0 == vcd_client_get_ref_count()) {
1028 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1029 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1032 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1034 return VCD_ERROR_NONE;
1037 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1039 /* check if pid is valid */
1040 if (false == vcd_client_is_available(pid)) {
1041 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1042 return VCD_ERROR_INVALID_PARAMETER;
1045 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1046 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1047 return VCD_ERROR_OPERATION_FAILED;
1053 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1055 /* check if pid is valid */
1056 if (false == vcd_client_is_available(pid)) {
1057 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1058 return VCD_ERROR_INVALID_PARAMETER;
1061 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1062 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1063 return VCD_ERROR_OPERATION_FAILED;
1070 int vcd_server_set_exclusive_command(int pid, bool value)
1072 /* check if pid is valid */
1073 if (false == vcd_client_is_available(pid)) {
1074 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1075 return VCD_ERROR_INVALID_PARAMETER;
1078 if (true == value) {
1079 if (0 != vcd_client_set_exclusive_command(pid)) {
1080 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
1081 return VCD_ERROR_OPERATION_FAILED;
1084 if (0 != vcd_client_unset_exclusive_command(pid)) {
1085 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
1086 return VCD_ERROR_OPERATION_FAILED;
1093 int vcd_server_request_start(int pid, bool stop_by_silence)
1095 /* check if pid is valid */
1096 if (false == vcd_client_is_available(pid)) {
1097 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
1098 return VCD_ERROR_INVALID_PARAMETER;
1102 /* Check current state */
1103 vcd_state_e state = vcd_config_get_service_state();
1105 /* Service state should be ready */
1106 if (VCD_STATE_READY != state) {
1107 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1108 return VCD_ERROR_INVALID_STATE;
1111 if (-1 != vcd_client_manager_get_pid()) {
1112 /* Check current pid is valid */
1113 if (false == vcd_client_manager_check_demandable_client(pid)) {
1114 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1115 return VCD_ERROR_INVALID_PARAMETER;
1119 ret = vcd_server_mgr_start(stop_by_silence, false);
1121 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1122 return VCD_ERROR_INVALID_PARAMETER;
1128 int vcd_server_request_stop(int pid)
1131 /* Check current state */
1132 vcd_state_e state = vcd_config_get_service_state();
1134 /* Service state should be ready */
1135 if (VCD_STATE_RECORDING != state) {
1136 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
1137 return VCD_ERROR_INVALID_STATE;
1140 if (-1 != vcd_client_manager_get_pid()) {
1141 /* Check current pid is valid */
1142 if (false == vcd_client_manager_check_demandable_client(pid)) {
1143 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1144 return VCD_ERROR_INVALID_PARAMETER;
1148 ret = vcd_server_mgr_stop();
1150 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1151 return VCD_ERROR_OPERATION_FAILED;
1154 return VCD_ERROR_NONE;
1157 int vcd_server_request_cancel(int pid)
1160 /* Check current state */
1161 vcd_state_e state = vcd_config_get_service_state();
1163 /* Service state should be recording or processing */
1164 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1165 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1166 return VCD_ERROR_INVALID_STATE;
1169 if (-1 != vcd_client_manager_get_pid()) {
1170 /* Check current pid is valid */
1171 if (false == vcd_client_manager_check_demandable_client(pid)) {
1172 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1173 return VCD_ERROR_INVALID_PARAMETER;
1177 ret = vcd_server_mgr_cancel();
1179 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1180 return VCD_ERROR_OPERATION_FAILED;
1183 return VCD_ERROR_NONE;
1188 * VC Server Functions for Widget lib
1190 int vcd_server_widget_initialize(int pid)
1192 if (false == g_is_engine) {
1193 if (0 != vcd_engine_agent_initialize_current_engine()) {
1194 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1195 g_is_engine = false;
1196 return VCD_ERROR_ENGINE_NOT_FOUND;
1202 if (false == vcd_engine_is_available_engine()) {
1203 if (0 != vcd_engine_agent_initialize_current_engine()) {
1204 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1205 return VCD_ERROR_ENGINE_NOT_FOUND;
1209 /* check if pid is valid */
1210 if (true == vcd_client_widget_is_available(pid)) {
1211 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1212 return VCD_ERROR_INVALID_PARAMETER;
1215 /* Add client information to client manager */
1216 if (0 != vcd_client_widget_add(pid)) {
1217 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1218 return VCD_ERROR_OPERATION_FAILED;
1221 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
1223 return VCD_ERROR_NONE;
1226 int vcd_server_widget_finalize(int pid)
1228 /* check if pid is valid */
1229 if (false == vcd_client_widget_is_available(pid)) {
1230 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1231 return VCD_ERROR_INVALID_PARAMETER;
1234 /* Remove client information */
1235 if (0 != vcd_client_widget_delete(pid)) {
1236 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1239 if (0 == vcd_client_get_ref_count()) {
1240 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
1241 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1244 return VCD_ERROR_NONE;
1247 int vcd_server_widget_start_recording(int pid, bool widget_command)
1249 /* check if pid is valid */
1250 if (false == vcd_client_widget_is_available(pid)) {
1251 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1252 return VCD_ERROR_INVALID_PARAMETER;
1255 if (true == widget_command) {
1256 vcd_client_widget_set_command(pid);
1257 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
1259 vcd_client_widget_unset_command(pid);
1260 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is NOT available");
1263 int ret = __start_internal_recognition();
1265 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1266 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1272 int vcd_server_widget_start(int pid, bool stop_by_silence)
1274 /* check if pid is valid */
1275 int fore_pid = vcd_client_widget_get_foreground_pid();
1276 if (pid != fore_pid) {
1277 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1278 return VCD_ERROR_INVALID_PARAMETER;
1281 /* Check current state */
1282 vcd_state_e state = vcd_config_get_service_state();
1284 /* Service state should be ready */
1285 if (VCD_STATE_READY != state) {
1286 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1287 return VCD_ERROR_INVALID_STATE;
1290 vcd_client_set_slience_detection(stop_by_silence);
1292 /* Notify show tooltip */
1293 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1298 int vcd_server_widget_stop(int pid)
1300 /* check if pid is valid */
1301 int fore_pid = vcd_client_widget_get_foreground_pid();
1302 if (pid != fore_pid) {
1303 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1304 return VCD_ERROR_INVALID_PARAMETER;
1308 /* Check current state */
1309 vcd_state_e state = vcd_config_get_service_state();
1311 /* Service state should be recording */
1312 if (VCD_STATE_RECORDING != state) {
1313 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
1314 return VCD_ERROR_INVALID_STATE;
1317 ret = vcd_server_mgr_stop();
1319 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1320 return VCD_ERROR_OPERATION_FAILED;
1323 return VCD_ERROR_NONE;
1326 int vcd_server_widget_cancel(int pid)
1328 /* check if pid is valid */
1329 int fore_pid = vcd_client_widget_get_foreground_pid();
1330 if (pid != fore_pid) {
1331 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1332 return VCD_ERROR_INVALID_PARAMETER;
1336 /* Check current state */
1337 vcd_state_e state = vcd_config_get_service_state();
1339 /* Service state should be recording or processing */
1340 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1341 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1342 return VCD_ERROR_INVALID_STATE;
1345 ret = vcd_server_mgr_cancel();
1347 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
1351 return VCD_ERROR_NONE;