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 static wakeup_service_state_changed_cb g_wakeup_service_state_changed_cb;
52 static void *g_wakeup_service_state_changed_user_data;
54 static wakeup_service_voice_key_status_changed_cb g_wakeup_service_voice_key_status_changed_cb;
55 static void *g_wakeup_service_voice_key_status_changed_user_data;
57 class CWakeupEventObserver : public IWakeupEventObserver
59 void on_wakeup(mas_wakeup_event_info wakeup_info) override;
60 void on_streaming_audio_data(
61 mas_speech_streaming_event_e event, void* buffer, unsigned int len) override;
62 void on_audio_streaming_data_section(ma_audio_streaming_data_section_e section) override;
63 void on_wakeup_engine_command(
64 mas_wakeup_engine_command_target_e target, const char* assistant_name, const char* command) override;
65 void on_wakeup_service_state_changed(ma_service_state_e state) override;
66 void on_voice_key_status_changed(ma_voice_key_status_e status) override;
69 class CSettingValueObserver : public ISettingValueObserver
71 void on_value_changed() override;
74 static std::unique_ptr<CWakeupEventObserver> g_wakeup_event_observer;
75 static std::unique_ptr<CSettingValueObserver> g_setting_value_observer;
76 static std::unique_ptr<CWakeupManager> g_wakeup_manager;
78 int wakeup_manager_initialize(void)
83 g_wakeup_event_observer.reset(new CWakeupEventObserver);
84 g_setting_value_observer.reset(new CSettingValueObserver);
85 if (NULL == g_wakeup_event_observer || NULL == g_setting_value_observer) {
88 g_wakeup_manager.reset(new CWakeupManager(g_wakeup_event_observer.get(), g_setting_value_observer.get()));
89 if (NULL == g_wakeup_manager) {
92 } catch (const std::exception& e) {
93 MWR_LOGE("Exception caught : %s", e.what());
97 g_wakeup_event_cb = NULL;
98 g_wakeup_event_user_data = NULL;
100 g_utterance_streaming_cb = NULL;
101 g_utterance_streaming_user_data = NULL;
103 g_follow_up_streaming_cb = NULL;
104 g_follow_up_streaming_user_data = NULL;
106 g_speech_status_cb = NULL;
107 g_speech_status_user_data = NULL;
109 g_setting_changed_cb = NULL;
110 g_setting_changed_user_data = NULL;
113 g_error_user_data = NULL;
115 g_streaming_section_changed_cb = NULL;
116 g_streaming_section_changed_user_data = NULL;
118 g_wakeup_engine_command_cb = NULL;
119 g_wakeup_engine_command_user_data = NULL;
121 g_wakeup_service_state_changed_cb = NULL;
122 g_wakeup_service_state_changed_user_data = NULL;
124 g_wakeup_service_voice_key_status_changed_cb = NULL;
125 g_wakeup_service_voice_key_status_changed_user_data = NULL;
127 g_wakeup_manager->initialize();
133 int wakeup_manager_deinitialize(void)
137 if (nullptr == g_wakeup_manager) return -1;
140 g_wakeup_manager->deinitialize();
141 g_wakeup_manager.reset();
143 g_wakeup_event_observer.reset();
144 g_setting_value_observer.reset();
145 } catch (const std::exception& e) {
146 MWR_LOGE("Exception caught : %s", e.what());
154 int wakeup_manager_get_settings(ma_plugin_settings **settings, size_t *struct_size)
156 if (NULL == settings || NULL == struct_size) {
157 MWR_LOGD("[ERROR] Parameter is invalid, settings(%p), struct_size(%p)", settings, struct_size);
161 if (nullptr == g_wakeup_manager) return -1;
162 CWakeupSettings* wakeup_settings = g_wakeup_manager->get_wakeup_settings();
163 if (wakeup_settings) {
164 const int PLUGIN_VERSION = 1;
165 static ma_plugin_settings current_settings;
166 current_settings.plugin_version = PLUGIN_VERSION;
167 current_settings.ui_panel_enabled = wakeup_settings->get_ui_panel_enabled();
168 *struct_size = sizeof(current_settings);
169 *settings = ¤t_settings;
175 int wakeup_manager_add_assistant_wakeup_word(const char* appid, const char* wakeup_word, const char* language)
179 if (NULL == appid || NULL == wakeup_word) {
180 MWR_LOGD("[ERROR] Parameter is invalid, appid(%s), wakeup_word(%s), language(%s)", appid, wakeup_word, language);
184 if (nullptr == g_wakeup_manager) return -1;
185 g_wakeup_manager->add_assistant_wakeup_word(
186 string{appid}, string{wakeup_word}, (language ? string{language} : string{}));
192 int wakeup_manager_remove_assistant_wakeup_word(const char* appid, const char* wakeup_word, const char* language)
196 if (NULL == appid || NULL == wakeup_word) {
197 MWR_LOGD("[ERROR] Parameter is invalid, appid(%s), wakeup_word(%s), language(%s)", appid, wakeup_word, language);
201 if (nullptr == g_wakeup_manager) return -1;
202 g_wakeup_manager->remove_assistant_wakeup_word(
203 string{appid}, string{wakeup_word}, (language ? string{language} : string{}));
209 int wakeup_manager_add_assistant_language(const char* appid, const char* language)
213 if (NULL == appid || NULL == language) {
214 MWR_LOGD("[ERROR] Parameter is invalid, appid(%s), language(%s)", appid, language);
218 if (nullptr == g_wakeup_manager) return -1;
219 g_wakeup_manager->add_assistant_language(string{appid}, string{language});
221 MWR_LOGD("[DEBUG] language(%s)", language);
227 int wakeup_manager_set_assistant_wakeup_engine(const char* appid, const char* engine)
231 if (NULL == appid || NULL == engine) {
232 MWR_LOGD("[ERROR] Parameter is invalid, appid(%s), wakeup engine(%s)", appid, engine);
236 if (nullptr == g_wakeup_manager) return -1;
237 MWR_LOGD("[DEBUG] appid(%s), wakeup engine(%s)", appid, engine);
238 g_wakeup_manager->set_assistant_wakeup_engine(string{appid}, string{engine});
244 int wakeup_manager_set_default_assistant(const char* appid)
249 MWR_LOGD("[ERROR] Parameter is NULL");
253 if (nullptr == g_wakeup_manager) return -1;
254 MWR_LOGD("[DEBUG] default_assistant appid(%s)", appid);
255 g_wakeup_manager->set_default_assistant(string{appid});
261 int wakeup_manager_get_default_assistant(const char** appid)
263 static string default_assistant;
265 if (nullptr == g_wakeup_manager) return -1;
266 default_assistant = g_wakeup_manager->get_default_assistant();
268 if (default_assistant.empty()) {
271 *appid = default_assistant.c_str();
276 int wakeup_manager_set_language(const char* language)
280 if (NULL == language) {
281 MWR_LOGD("[ERROR] Parameter is NULL");
285 if (nullptr == g_wakeup_manager) return -1;
286 g_wakeup_manager->set_language(string{language});
292 int wakeup_manager_change_state(wakeup_manager_state_e state)
294 if (nullptr == g_wakeup_manager) return -1;
295 g_wakeup_manager->change_manager_state(state);
299 int wakeup_manager_activate(void)
303 if (nullptr == g_wakeup_manager) return -1;
304 g_wakeup_manager->activate();
310 int wakeup_manager_deactivate(void)
314 if (nullptr == g_wakeup_manager) return -1;
315 g_wakeup_manager->deactivate();
321 int wakeup_manager_update_voice_feedback_state(const char* appid, int state)
327 MWR_LOGD("[ERROR] Parameter is NULL");
329 appid_string = appid;
332 if (nullptr == g_wakeup_manager) return -1;
333 g_wakeup_manager->update_voice_feedback_state(appid_string, state);
339 int wakeup_manager_set_assistant_specific_command(const char* appid, const char* command)
344 string command_string;
345 if (NULL == appid || NULL == command) {
346 MWR_LOGD("[ERROR] Parameter is invalid, appid(%s), command(%s)", appid, command);
348 if (appid) appid_string = appid;
349 if (command) command_string = command;
351 if (nullptr == g_wakeup_manager) return -1;
352 g_wakeup_manager->set_assistant_specific_command(appid_string, command_string);
358 int wakeup_manager_set_background_volume(const char* appid, double ratio)
364 MWR_LOGD("[ERROR] Parameter is NULL");
366 appid_string = appid;
369 if (nullptr == g_wakeup_manager) return -1;
370 g_wakeup_manager->set_background_volume(appid_string, ratio);
376 int wakeup_manager_update_recognition_result(const char* appid, int result)
382 MWR_LOGD("[ERROR] Parameter is NULL");
384 appid_string = appid;
387 if (nullptr == g_wakeup_manager) return -1;
388 g_wakeup_manager->update_recognition_result(appid_string, result);
394 int wakeup_manager_process_plugin_event(mas_plugin_event_e event, void* data, int len)
398 if (nullptr == g_wakeup_manager) return -1;
399 g_wakeup_manager->process_plugin_event(event, data, len);
405 int wakeup_manager_start_streaming_utterance_data(void)
409 if (nullptr == g_wakeup_manager) return -1;
410 g_wakeup_manager->start_streaming_utterance_data();
416 int wakeup_manager_stop_streaming_utterance_data(void)
420 if (nullptr == g_wakeup_manager) return -1;
421 g_wakeup_manager->stop_streaming_utterance_data();
427 int wakeup_manager_start_streaming_follow_up_data(void)
431 if (nullptr == g_wakeup_manager) return -1;
432 g_wakeup_manager->start_streaming_follow_up_data();
438 int wakeup_manager_stop_streaming_follow_up_data(void)
442 if (nullptr == g_wakeup_manager) return -1;
443 g_wakeup_manager->stop_streaming_follow_up_data();
449 int wakeup_manager_start_streaming_previous_utterance_data(void)
453 if (nullptr == g_wakeup_manager) return -1;
454 g_wakeup_manager->start_streaming_previous_utterance_data();
460 int wakeup_manager_stop_streaming_previous_utterance_data(void)
464 if (nullptr == g_wakeup_manager) return -1;
465 g_wakeup_manager->stop_streaming_previous_utterance_data();
471 int wakeup_manager_get_audio_format(int* rate, int* channel, int* audio_type)
475 if (!audio_type || !rate || !channel) {
476 MWR_LOGE("[ERROR] Invalid parameter");
480 if (nullptr == g_wakeup_manager) return -1;
481 g_wakeup_manager->get_audio_format(rate, channel, audio_type);
483 MWR_LOGD("[END] rate(%d), channel(%d), audio_type(%d)", *rate, *channel, *audio_type);
487 int wakeup_manager_get_audio_source_type(char** type)
492 MWR_LOGE("[ERROR] Invalid parameter");
496 if (nullptr == g_wakeup_manager) return -1;
497 g_wakeup_manager->get_audio_source_type(type);
499 MWR_LOGD("[END] type(%s)", *type);
503 int wakeup_manager_set_voice_key_tap_duration(float duration)
507 if (nullptr == g_wakeup_manager) return -1;
508 g_wakeup_manager->set_voice_key_tap_duration(duration);
510 MWR_LOGD("[END] duration(%f)", duration);
514 int wakeup_manager_unset_voice_key_tap_duration()
518 if (nullptr == g_wakeup_manager) return -1;
519 g_wakeup_manager->unset_voice_key_tap_duration();
525 int wakeup_manager_set_voice_key_support_mode(int mode)
527 MWR_LOGI("[ENTER] : %d", mode);
529 if (nullptr == g_wakeup_manager) return -1;
530 g_wakeup_manager->set_voice_key_support_mode(static_cast<VOICE_KEY_SUPPORT_MODE>(mode));
536 int wakeup_manager_set_wake_word_audio_require_flag(bool require)
538 MWR_LOGI("[ENTER] : %d", require);
540 if (nullptr == g_wakeup_manager) return -1;
541 g_wakeup_manager->set_wake_word_audio_require_flag(require);
547 int wakeup_manager_set_assistant_language(const char* appid, const char* language)
552 string language_string;
553 if (NULL == appid || NULL == language) {
554 MWR_LOGE("[ERROR] Parameter is invalid, appid(%s), language(%s)", appid, language);
557 appid_string = appid;
558 language_string = language;
560 if (nullptr == g_wakeup_manager) return -1;
561 g_wakeup_manager->set_assistant_language(appid_string, language_string);
567 int wakeup_manager_set_wakeup_event_callback(wakeup_service_wakeup_event_cb callback, void* user_data)
571 if (NULL == callback) {
572 MWR_LOGE("[ERROR] Input parameter is NULL");
576 g_wakeup_event_cb = callback;
577 g_wakeup_event_user_data = user_data;
583 int wakeup_manager_set_utterance_streaming_callback(wakeup_service_speech_streaming_cb callback, void* user_data)
587 if (NULL == callback) {
588 MWR_LOGE("[ERROR] Input parameter is NULL");
592 g_utterance_streaming_cb = callback;
593 g_utterance_streaming_user_data = user_data;
599 int wakeup_manager_set_previous_utterance_streaming_callback(wakeup_service_speech_streaming_cb callback, void* user_data)
603 if (NULL == callback) {
604 MWR_LOGE("[ERROR] Input parameter is NULL");
608 g_previous_utterance_streaming_cb = callback;
609 g_previous_utterance_streaming_user_data = user_data;
615 int wakeup_manager_set_follow_up_streaming_callback(wakeup_service_speech_streaming_cb callback, void* user_data)
619 if (NULL == callback) {
620 MWR_LOGE("[ERROR] Input parameter is NULL");
624 g_follow_up_streaming_cb = callback;
625 g_follow_up_streaming_user_data = user_data;
631 int wakeup_manager_set_speech_status_callback(wakeup_service_speech_status_cb callback, void* user_data)
635 if (NULL == callback) {
636 MWR_LOGE("[ERROR] Input parameter is NULL");
640 g_speech_status_cb = callback;
641 g_speech_status_user_data = user_data;
647 int wakeup_manager_set_setting_changed_callback(wakeup_service_setting_changed_cb callback, void* user_data)
651 if (NULL == callback) {
652 MWR_LOGE("[ERROR] Input parameter is NULL");
656 g_setting_changed_cb = callback;
657 g_setting_changed_user_data = user_data;
663 int wakeup_manager_set_error_callback(wakeup_service_error_cb callback, void* user_data)
667 if (NULL == callback) {
668 MWR_LOGE("[ERROR] Input parameter is NULL");
672 g_error_cb = callback;
673 g_error_user_data = user_data;
679 int wakeup_manager_set_streaming_section_changed_callback(wakeup_service_streaming_section_changed_cb callback, void* user_data)
683 if (NULL == callback) {
684 MWR_LOGE("[ERROR] Input parameter is NULL");
688 g_streaming_section_changed_cb = callback;
689 g_streaming_section_changed_user_data = user_data;
695 int wakeup_manager_set_wakeup_engine_command_callback(wakeup_service_engine_command_cb callback, void* user_data)
699 if (NULL == callback) {
700 MWR_LOGE("[ERROR] Input parameter is NULL");
704 g_wakeup_engine_command_cb = callback;
705 g_wakeup_engine_command_user_data = user_data;
711 int wakeup_manager_set_wakeup_service_state_changed_callback(wakeup_service_state_changed_cb callback, void* user_data)
715 if (NULL == callback) {
716 MWR_LOGE("[ERROR] Input parameter is NULL");
720 g_wakeup_service_state_changed_cb = callback;
721 g_wakeup_service_state_changed_user_data = user_data;
727 int wakeup_manager_set_voice_key_status_changed_callback(wakeup_service_voice_key_status_changed_cb callback, void* user_data)
731 if (NULL == callback) {
732 MWR_LOGE("[ERROR] Input parameter is NULL");
736 g_wakeup_service_voice_key_status_changed_cb = callback;
737 g_wakeup_service_voice_key_status_changed_user_data = user_data;
743 int wakeup_manager_feed_audio_data(mas_speech_streaming_event_e event, void* buffer, int len)
745 if (nullptr == g_wakeup_manager) return -1;
747 static unsigned int num = 0;
748 const std::chrono::seconds interval(5);
749 static auto last = std::chrono::steady_clock::now();
750 auto now = std::chrono::steady_clock::now();
751 if (now - last > interval) {
752 MWR_LOGI("Feeding audio data : num(%d), now(%" PRId64 ")",
753 num, now.time_since_epoch().count());
758 g_wakeup_manager->feed_audio_data(event, buffer, len);
763 int wakeup_manager_set_dependency_module_command(const char* engine_name, const char* command)
765 if (nullptr == g_wakeup_manager) return -1;
766 g_wakeup_manager->set_dependency_module_command(
767 engine_name ? engine_name : "",
768 command ? command : "");
772 int wakeup_manager_wakeup_assistant(const mas_wakeup_event_info* wakeup_info)
774 if (nullptr == g_wakeup_manager) return -1;
775 CWakeupPolicy* policy = g_wakeup_manager->get_wakeup_policy();
776 if (policy) policy->select_candidate(*wakeup_info);
780 void CWakeupEventObserver::on_wakeup(mas_wakeup_event_info wakeup_info)
782 if (NULL != g_wakeup_event_cb) {
783 g_wakeup_event_cb(wakeup_info, g_wakeup_event_user_data);
787 void CWakeupEventObserver::on_streaming_audio_data(
788 mas_speech_streaming_event_e event, void* buffer, unsigned int len)
790 if (MAS_SPEECH_STREAMING_EVENT_START == event) {
791 MWR_LOGI("streaming_cb START");
793 if (MAS_SPEECH_STREAMING_EVENT_FINISH == event) {
794 MWR_LOGI("streaming_cb FINISH");
796 if (NULL != g_utterance_streaming_cb) {
797 g_utterance_streaming_cb(event, buffer, len, g_utterance_streaming_user_data);
799 MWR_LOGI("[INFO] No service streaming callback");
803 void CWakeupEventObserver::on_audio_streaming_data_section(
804 ma_audio_streaming_data_section_e section)
806 if (g_streaming_section_changed_cb) {
807 g_streaming_section_changed_cb(section, g_streaming_section_changed_user_data);
811 void CWakeupEventObserver::on_wakeup_engine_command(
812 mas_wakeup_engine_command_target_e target, const char* assistant_name, const char* command)
814 if (g_wakeup_engine_command_cb) {
815 g_wakeup_engine_command_cb(target, assistant_name, command, g_wakeup_engine_command_user_data);
819 void CWakeupEventObserver::on_wakeup_service_state_changed(ma_service_state_e state)
821 if (g_wakeup_service_state_changed_cb) {
822 g_wakeup_service_state_changed_cb(state, g_wakeup_service_state_changed_user_data);
826 void CWakeupEventObserver::on_voice_key_status_changed(ma_voice_key_status_e status)
828 if (g_wakeup_service_voice_key_status_changed_cb) {
829 g_wakeup_service_voice_key_status_changed_cb(status, g_wakeup_service_voice_key_status_changed_user_data);
833 void CSettingValueObserver::on_value_changed()
835 if (g_setting_changed_cb) {
836 g_setting_changed_cb(g_setting_changed_user_data);