2 * Copyright 2018-2019 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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.
16 #include "wakeup_manager_wrapper.h"
17 #include "wakeup_manager_main.h"
18 #include "wakeup_manager.h"
22 using namespace multiassistant::wakeup;
24 static wakeup_service_wakeup_event_cb g_wakeup_event_cb;
25 static void* g_wakeup_event_user_data;
27 static wakeup_service_speech_streaming_cb g_utterance_streaming_cb;
28 static void* g_utterance_streaming_user_data;
30 static wakeup_service_speech_streaming_cb g_previous_utterance_streaming_cb;
31 static void* g_previous_utterance_streaming_user_data;
33 static wakeup_service_speech_streaming_cb g_follow_up_streaming_cb;
34 static void* g_follow_up_streaming_user_data;
36 static wakeup_service_speech_status_cb g_speech_status_cb;
37 static void* g_speech_status_user_data;
39 static wakeup_service_setting_changed_cb g_setting_changed_cb;
40 static void* g_setting_changed_user_data;
42 static wakeup_service_error_cb g_error_cb;
43 static void* g_error_user_data;
45 static wakeup_service_streaming_section_changed_cb g_streaming_section_changed_cb;
46 static void* g_streaming_section_changed_user_data;
48 static wakeup_service_engine_command_cb g_wakeup_engine_command_cb;
49 static void* g_wakeup_engine_command_user_data;
51 class CWakeupEventObserver : public IWakeupEventObserver
53 void on_wakeup(mas_wakeup_event_info wakeup_info) override;
54 void on_streaming_audio_data(
55 mas_speech_streaming_event_e event, void* buffer, unsigned int len) override;
56 void on_audio_streaming_data_section(ma_audio_streaming_data_section_e section) override;
57 void on_wakeup_engine_command(
58 mas_wakeup_engine_command_target_e target, const char* assistant_name, const char* command) override;
61 class CSettingValueObserver : public ISettingValueObserver
63 void on_value_changed() override;
66 static std::unique_ptr<CWakeupEventObserver> g_wakeup_event_observer;
67 static std::unique_ptr<CSettingValueObserver> g_setting_value_observer;
68 static std::unique_ptr<CWakeupManager> g_wakeup_manager;
70 int wakeup_manager_initialize(void)
75 g_wakeup_event_observer.reset(new CWakeupEventObserver);
76 g_setting_value_observer.reset(new CSettingValueObserver);
77 if (NULL == g_wakeup_event_observer || NULL == g_setting_value_observer) {
80 g_wakeup_manager.reset(new CWakeupManager(g_wakeup_event_observer.get(), g_setting_value_observer.get()));
81 if (NULL == g_wakeup_manager) {
84 } catch (const std::exception& e) {
85 MWR_LOGE("Exception caught : %s", e.what());
89 g_wakeup_event_cb = NULL;
90 g_wakeup_event_user_data = NULL;
92 g_utterance_streaming_cb = NULL;
93 g_utterance_streaming_user_data = NULL;
95 g_follow_up_streaming_cb = NULL;
96 g_follow_up_streaming_user_data = NULL;
98 g_speech_status_cb = NULL;
99 g_speech_status_user_data = NULL;
101 g_setting_changed_cb = NULL;
102 g_setting_changed_user_data = NULL;
105 g_error_user_data = NULL;
107 g_streaming_section_changed_cb = NULL;
108 g_streaming_section_changed_user_data = NULL;
110 g_wakeup_engine_command_cb = NULL;
111 g_wakeup_engine_command_user_data = NULL;
113 g_wakeup_manager->initialize();
119 int wakeup_manager_deinitialize(void)
123 if (nullptr == g_wakeup_manager) return -1;
126 g_wakeup_manager->deinitialize();
127 g_wakeup_manager.reset();
129 g_wakeup_event_observer.reset();
130 g_setting_value_observer.reset();
131 } catch (const std::exception& e) {
132 MWR_LOGE("Exception caught : %s", e.what());
140 int wakeup_manager_get_settings(ma_plugin_settings **settings, size_t *struct_size)
142 if (NULL == settings || NULL == struct_size) {
143 MWR_LOGD("[ERROR] Parameter is invalid, settings(%p), struct_size(%p)", settings, struct_size);
147 if (nullptr == g_wakeup_manager) return -1;
148 CWakeupSettings* wakeup_settings = g_wakeup_manager->get_wakeup_settings();
149 if (wakeup_settings) {
150 const int PLUGIN_VERSION = 1;
151 static ma_plugin_settings current_settings;
152 current_settings.plugin_version = PLUGIN_VERSION;
153 current_settings.ui_panel_enabled = wakeup_settings->get_ui_panel_enabled();
154 *struct_size = sizeof(current_settings);
155 *settings = ¤t_settings;
161 int wakeup_manager_add_assistant_wakeup_word(const char* appid, const char* wakeup_word, const char* language)
165 if (NULL == appid || NULL == wakeup_word) {
166 MWR_LOGD("[ERROR] Parameter is invalid, appid(%s), wakeup_word(%s), language(%s)", appid, wakeup_word, language);
170 if (nullptr == g_wakeup_manager) return -1;
171 g_wakeup_manager->add_assistant_wakeup_word(
172 string{appid}, string{wakeup_word}, (language ? string{language} : string{}));
178 int wakeup_manager_add_assistant_language(const char* appid, const char* language)
182 if (NULL == appid || NULL == language) {
183 MWR_LOGD("[ERROR] Parameter is invalid, appid(%s), language(%s)", appid, language);
187 if (nullptr == g_wakeup_manager) return -1;
188 g_wakeup_manager->add_assistant_language(string{appid}, string{language});
190 MWR_LOGD("[DEBUG] language(%s)", language);
196 int wakeup_manager_set_assistant_wakeup_engine(const char* appid, const char* engine)
200 if (NULL == appid || NULL == engine) {
201 MWR_LOGD("[ERROR] Parameter is invalid, appid(%s), wakeup engine(%s)", appid, engine);
205 if (nullptr == g_wakeup_manager) return -1;
206 MWR_LOGD("[DEBUG] appid(%s), wakeup engine(%s)", appid, engine);
207 g_wakeup_manager->set_assistant_wakeup_engine(string{appid}, string{engine});
213 int wakeup_manager_set_default_assistant(const char* appid)
218 MWR_LOGD("[ERROR] Parameter is invalid, appid(%s)", appid);
222 if (nullptr == g_wakeup_manager) return -1;
223 MWR_LOGD("[DEBUG] default_assistant appid(%s)", appid);
224 g_wakeup_manager->set_default_assistant(string{appid});
230 int wakeup_manager_get_default_assistant(const char** appid)
232 static string default_assistant;
234 if (nullptr == g_wakeup_manager) return -1;
235 default_assistant = g_wakeup_manager->get_default_assistant();
237 if (default_assistant.empty()) {
240 *appid = default_assistant.c_str();
245 int wakeup_manager_set_language(const char* language)
249 if (NULL == language) {
250 MWR_LOGD("[ERROR] Parameter is invalid, language(%s)", language);
254 if (nullptr == g_wakeup_manager) return -1;
255 g_wakeup_manager->set_language(string{language});
261 int wakeup_manager_change_state(wakeup_manager_state_e state)
263 if (nullptr == g_wakeup_manager) return -1;
264 g_wakeup_manager->change_manager_state(state);
268 int wakeup_manager_activate(void)
272 if (nullptr == g_wakeup_manager) return -1;
273 g_wakeup_manager->activate();
279 int wakeup_manager_deactivate(void)
283 if (nullptr == g_wakeup_manager) return -1;
284 g_wakeup_manager->deactivate();
290 int wakeup_manager_update_voice_feedback_state(const char* appid, int state)
296 MWR_LOGD("[ERROR] Parameter is invalid, appid(%s)", appid);
298 appid_string = appid;
301 if (nullptr == g_wakeup_manager) return -1;
302 g_wakeup_manager->update_voice_feedback_state(appid_string, state);
308 int wakeup_manager_send_assistant_specific_command(const char* appid, const char* command)
313 string command_string;
314 if (NULL == appid || NULL == command) {
315 MWR_LOGD("[ERROR] Parameter is invalid, appid(%s), command(%s)", appid, command);
317 if (appid) appid_string = appid;
318 if (command) command_string = command;
320 if (nullptr == g_wakeup_manager) return -1;
321 g_wakeup_manager->send_assistant_specific_command(appid_string, command_string);
327 int wakeup_manager_set_background_volume(const char* appid, double ratio)
333 MWR_LOGD("[ERROR] Parameter is invalid, appid(%s)", appid);
335 appid_string = appid;
338 if (nullptr == g_wakeup_manager) return -1;
339 g_wakeup_manager->set_background_volume(appid_string, ratio);
345 int wakeup_manager_update_recognition_result(const char* appid, int result)
351 MWR_LOGD("[ERROR] Parameter is invalid, appid(%s)", appid);
353 appid_string = appid;
356 if (nullptr == g_wakeup_manager) return -1;
357 g_wakeup_manager->update_recognition_result(appid_string, result);
363 int wakeup_manager_process_event(mas_plugin_event_e event, void* data, int len)
367 if (nullptr == g_wakeup_manager) return -1;
368 g_wakeup_manager->process_event(event, data, len);
374 int wakeup_manager_start_streaming_utterance_data(void)
378 if (nullptr == g_wakeup_manager) return -1;
379 g_wakeup_manager->start_streaming_utterance_data();
385 int wakeup_manager_stop_streaming_utterance_data(void)
389 if (nullptr == g_wakeup_manager) return -1;
390 g_wakeup_manager->stop_streaming_utterance_data();
396 int wakeup_manager_start_streaming_follow_up_data(void)
400 if (nullptr == g_wakeup_manager) return -1;
401 g_wakeup_manager->start_streaming_follow_up_data();
407 int wakeup_manager_stop_streaming_follow_up_data(void)
411 if (nullptr == g_wakeup_manager) return -1;
412 g_wakeup_manager->stop_streaming_follow_up_data();
418 int wakeup_manager_start_streaming_previous_utterance_data(void)
422 if (nullptr == g_wakeup_manager) return -1;
423 g_wakeup_manager->start_streaming_previous_utterance_data();
429 int wakeup_manager_stop_streaming_previous_utterance_data(void)
433 if (nullptr == g_wakeup_manager) return -1;
434 g_wakeup_manager->stop_streaming_previous_utterance_data();
440 int wakeup_manager_get_audio_format(int* rate, int* channel, int* audio_type)
444 if (!audio_type || !rate || !channel) {
445 MWR_LOGE("[ERROR] Invalid parameter");
449 if (nullptr == g_wakeup_manager) return -1;
450 g_wakeup_manager->get_audio_format(rate, channel, audio_type);
452 MWR_LOGD("[END] rate(%d), channel(%d), audio_type(%d)", *rate, *channel, *audio_type);
456 int wakeup_manager_get_audio_source_type(char** type)
461 MWR_LOGE("[ERROR] Invalid parameter");
465 if (nullptr == g_wakeup_manager) return -1;
466 g_wakeup_manager->get_audio_source_type(type);
468 MWR_LOGD("[END] type(%s)", *type);
472 int wakeup_manager_set_wake_word_audio_require_flag(bool require)
474 MWR_LOGD("[ENTER] : %d", require);
476 if (nullptr == g_wakeup_manager) return -1;
477 g_wakeup_manager->set_wake_word_audio_require_flag(require);
483 int wakeup_manager_set_assistant_language(const char* appid, const char* language)
488 string language_string;
489 if (NULL == appid || NULL == language) {
490 MWR_LOGE("[ERROR] Parameter is invalid, appid(%s), language(%s)", appid, language);
493 appid_string = appid;
494 language_string = language;
496 if (nullptr == g_wakeup_manager) return -1;
497 g_wakeup_manager->set_assistant_language(appid_string, language_string);
503 int wakeup_manager_set_wakeup_event_callback(wakeup_service_wakeup_event_cb callback, void* user_data)
507 if (NULL == callback) {
508 MWR_LOGE("[ERROR] Input parameter is NULL");
512 g_wakeup_event_cb = callback;
513 g_wakeup_event_user_data = user_data;
519 int wakeup_manager_set_utterance_streaming_callback(wakeup_service_speech_streaming_cb callback, void* user_data)
523 if (NULL == callback) {
524 MWR_LOGE("[ERROR] Input parameter is NULL");
528 g_utterance_streaming_cb = callback;
529 g_utterance_streaming_user_data = user_data;
535 int wakeup_manager_set_previous_utterance_streaming_callback(wakeup_service_speech_streaming_cb callback, void* user_data)
539 if (NULL == callback) {
540 MWR_LOGE("[ERROR] Input parameter is NULL");
544 g_previous_utterance_streaming_cb = callback;
545 g_previous_utterance_streaming_user_data = user_data;
551 int wakeup_manager_set_follow_up_streaming_callback(wakeup_service_speech_streaming_cb callback, void* user_data)
555 if (NULL == callback) {
556 MWR_LOGE("[ERROR] Input parameter is NULL");
560 g_follow_up_streaming_cb = callback;
561 g_follow_up_streaming_user_data = user_data;
567 int wakeup_manager_set_speech_status_callback(wakeup_service_speech_status_cb callback, void* user_data)
571 if (NULL == callback) {
572 MWR_LOGE("[ERROR] Input parameter is NULL");
576 g_speech_status_cb = callback;
577 g_speech_status_user_data = user_data;
583 int wakeup_manager_set_setting_changed_callback(wakeup_service_setting_changed_cb callback, void* user_data)
587 if (NULL == callback) {
588 MWR_LOGE("[ERROR] Input parameter is NULL");
592 g_setting_changed_cb = callback;
593 g_setting_changed_user_data = user_data;
599 int wakeup_manager_set_error_callback(wakeup_service_error_cb callback, void* user_data)
603 if (NULL == callback) {
604 MWR_LOGE("[ERROR] Input parameter is NULL");
608 g_error_cb = callback;
609 g_error_user_data = user_data;
615 int wakeup_manager_set_streaming_section_changed_callback(wakeup_service_streaming_section_changed_cb callback, void* user_data)
619 if (NULL == callback) {
620 MWR_LOGE("[ERROR] Input parameter is NULL");
624 g_streaming_section_changed_cb = callback;
625 g_streaming_section_changed_user_data = user_data;
631 int wakeup_manager_set_wakeup_engine_command_callback(wakeup_service_engine_command_cb callback, void* user_data)
635 if (NULL == callback) {
636 MWR_LOGE("[ERROR] Input parameter is NULL");
640 g_wakeup_engine_command_cb = callback;
641 g_wakeup_engine_command_user_data = user_data;
647 int wakeup_manager_feed_audio_data(mas_speech_streaming_event_e event, void* buffer, int len)
649 if (nullptr == g_wakeup_manager) return -1;
650 g_wakeup_manager->feed_audio_data(event, buffer, len);
654 int wakeup_manager_send_dependency_module_command(const char* engine_name, const char* command)
656 if (nullptr == g_wakeup_manager) return -1;
657 g_wakeup_manager->send_dependency_module_command(
658 engine_name ? engine_name : "",
659 command ? command : "");
663 void CWakeupEventObserver::on_wakeup(mas_wakeup_event_info wakeup_info)
665 if (NULL != g_wakeup_event_cb) {
666 g_wakeup_event_cb(wakeup_info, g_wakeup_event_user_data);
670 void CWakeupEventObserver::on_streaming_audio_data(
671 mas_speech_streaming_event_e event, void* buffer, unsigned int len)
673 if (MAS_SPEECH_STREAMING_EVENT_START == event) {
674 MWR_LOGD("streaming_cb START");
676 if (MAS_SPEECH_STREAMING_EVENT_FINISH == event) {
677 MWR_LOGD("streaming_cb FINISH");
679 if (NULL != g_utterance_streaming_cb) {
680 g_utterance_streaming_cb(event, buffer, len, g_utterance_streaming_user_data);
682 MWR_LOGI("[INFO] No service streaming callback");
686 void CWakeupEventObserver::on_audio_streaming_data_section(
687 ma_audio_streaming_data_section_e section)
689 if (g_streaming_section_changed_cb) {
690 g_streaming_section_changed_cb(section, g_streaming_section_changed_user_data);
694 void CWakeupEventObserver::on_wakeup_engine_command(
695 mas_wakeup_engine_command_target_e target, const char* assistant_name, const char* command)
697 if (g_wakeup_engine_command_cb) {
698 g_wakeup_engine_command_cb(target, assistant_name, command, g_wakeup_engine_command_user_data);
702 void CSettingValueObserver::on_value_changed()
704 if (g_setting_changed_cb) {
705 g_setting_changed_cb(g_setting_changed_user_data);