1 #include <multi_assistant_service.h>
3 #include "wakeup_engine_manager.h"
4 #include "wakeup_manager_main.h"
8 #include <pkgmgr-info.h>
11 namespace multiassistant
16 /* Sound buf save for test */
22 static char g_temp_file_name[128] = {'\0', };
24 static FILE* g_pFile = NULL;
26 static int g_count = 1;
29 /* Need to check whether this value needs to be configurable */
30 static int g_speech_pcm_wait_count = 800;
32 /* Utility function for checking if an element exists in a container */
33 template<class C, class T>
34 static auto contains(const C& v, const T& x) -> decltype(end(v), true)
36 return end(v) != find(begin(v), end(v), x);
39 CWakeupEngineManager::CWakeupEngineManager()
43 CWakeupEngineManager::~CWakeupEngineManager()
45 MWR_LOGI("Wakeup Engine Manager is now being destroyed");
48 CWakeupEngineManager::CWakeupEngineManager(IEngineEventObserver *observer) : CWakeupEngineManager()
53 void CWakeupEngineManager::initialize()
55 DIR* dp = opendir(MA_WAKEUP_ENGINE_PATH);
57 MWR_LOGD("Failed opening directory : %s", (const char*)MA_WAKEUP_ENGINE_PATH);
59 struct dirent *dirp = nullptr;
60 char dirpath[_POSIX_PATH_MAX];
64 if (nullptr != dirp) {
65 const string current_directory{"."};
66 const string parent_directory{".."};
67 if (0 == current_directory.compare(dirp->d_name) ||
68 0 == parent_directory.compare(dirp->d_name))
71 if (DT_DIR != dirp->d_type) /* If not a directory */
74 int dirpath_len = strlen(MA_WAKEUP_ENGINE_PATH) + strlen(dirp->d_name) + 1;
75 if (dirpath_len >= _POSIX_PATH_MAX) {
76 MWR_LOGD("File path is too long : %s", dirp->d_name);
81 memset(dirpath, '\0', _POSIX_PATH_MAX);
82 snprintf(dirpath, _POSIX_PATH_MAX, "%s/%s",
83 (const char*)(MA_WAKEUP_ENGINE_PATH), dirp->d_name);
85 add_engine_directory(string{dirp->d_name}, dirpath);
87 } while (nullptr != dirp);
93 void CWakeupEngineManager::deinitialize()
96 if (mStreamingThread.joinable()) {
97 MWR_LOGD("mStreamingThread is joinable, trying join()");
98 mStopStreamingThread.store(true);
99 mStreamingThread.join();
101 mStopStreamingThread.store(false);
103 for (auto& info : mEngineInfo) {
105 if (info.interface.set_wakeup_event_callback) {
106 info.interface.set_wakeup_event_callback(nullptr, nullptr);
108 if (info.interface.set_speech_status_callback) {
109 info.interface.set_speech_status_callback(nullptr, nullptr);
111 if (info.interface.set_error_callback) {
112 info.interface.set_error_callback(nullptr, nullptr);
114 if (info.interface.set_audio_data_require_status_callback) {
115 info.interface.set_audio_data_require_status_callback(nullptr, nullptr);
117 if (info.interface.set_wakeup_engine_command_callback) {
118 info.interface.set_wakeup_engine_command_callback(nullptr, nullptr);
120 MWR_LOGI("Trying to deinitialize engine : %s", info.engine_name.c_str());
121 if (info.interface.deinitialize) {
122 int ret = info.interface.deinitialize();
123 MWR_LOGI("Deinitialization [%s] returned %d", info.engine_name.c_str(), ret);
125 } catch (const std::exception& e) {
126 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
127 info.engine_name.c_str(), e.what());
129 if (info.engine_handle) {
130 int ret = dlclose(info.engine_handle);
131 MWR_LOGI("Closing [%s] returned %d, [%s]", info.engine_name.c_str(), ret,
132 ((0 == ret) ? "" : dlerror()));
133 info.engine_handle = nullptr;
136 mSelectedEngine = nullptr;
141 void CWakeupEngineManager::subscribe(IEngineEventObserver *observer)
143 mObservers.push_back(observer);
144 MWR_LOGD("Added Observer : %p %zu", observer, mObservers.size());
147 void CWakeupEngineManager::unsubscribe(IEngineEventObserver *observer)
149 auto iter = find(mObservers.begin(), mObservers.end(), observer);
150 if (iter != mObservers.end()) {
151 mObservers.erase(iter);
155 bool CWakeupEngineManager::get_audio_data_required()
157 return mAudioDataRequired;
160 void CWakeupEngineManager::set_selected_wakeup_info(mas_wakeup_event_info wakeup_info)
162 mSelectedEngine = nullptr;
164 const auto& iter = find_if(mEngineInfo.begin(), mEngineInfo.end(),
165 [wakeup_info](const EngineInfo& info) {
166 if (nullptr == wakeup_info.wakeup_engine)
169 return (0 == info.engine_name.compare(wakeup_info.wakeup_engine));
172 if (iter != mEngineInfo.end()) {
173 mSelectedEngine = &(*iter);
174 MWR_LOGD("Selected : %s", iter->engine_name.c_str());
178 bool CWakeupEngineManager::set_language(string language)
180 for (const auto& info : mEngineInfo) {
181 if (info.interface.set_language) {
183 info.interface.set_language(language.c_str());
184 } catch (const std::exception& e) {
185 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
186 info.engine_name.c_str(), e.what());
193 bool CWakeupEngineManager::set_assistant_language(string appid, string language)
195 for (auto& info : mEngineInfo) {
196 const auto& iter = find_if(info.assistant_list.begin(), info.assistant_list.end(),
197 [appid](const string& assistant) {
198 return (0 == assistant.compare(appid));
201 /* If the appid is in the assistant list */
202 if (info.assistant_list.end() != iter) {
204 int ret = info.interface.set_language(language.c_str());
205 MWR_LOGI("set_language returned %d : %s %s %s",
206 ret, appid.c_str(), info.engine_name.c_str(), language.c_str());
207 } catch (const std::exception& e) {
208 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
209 info.engine_name.c_str(), e.what());
216 void CWakeupEngineManager::set_assistant_activated(string appid, bool activated)
218 MWR_LOGI("[ENTER] : %s %d", appid.c_str(), activated);
219 for (auto& info : mEngineInfo) {
220 const auto& iter = find_if(info.assistant_list.begin(), info.assistant_list.end(),
221 [appid](const string& assistant) {
222 return (0 == assistant.compare(appid));
225 /* If the appid is in the assistant list */
226 if (info.assistant_list.end() != iter) {
227 bool previously_activated = info.activated;
229 info.activated_assistants.insert(appid);
231 info.activated_assistants.erase(appid);
233 info.activated = (info.activated_assistants.size() > 0);
234 if (previously_activated != info.activated) {
235 if (info.activated) {
237 info.interface.activate();
238 } catch (const std::exception& e) {
239 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
240 info.engine_name.c_str(), e.what());
244 info.interface.deactivate();
245 } catch (const std::exception& e) {
246 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
247 info.engine_name.c_str(), e.what());
250 /* Activated status changed, need to update audio_data_require_status too */
251 on_audio_data_require_status(info.engine_name, info.audio_data_require_status);
257 void CWakeupEngineManager::set_wake_word_audio_require_flag(bool require)
260 mWakeWordAudioRequired = require;
261 for (const auto& info : mEngineInfo) {
262 if (info.interface.set_wake_word_audio_require_flag) {
264 info.interface.set_wake_word_audio_require_flag(require);
265 } catch (const std::exception& e) {
266 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
267 info.engine_name.c_str(), e.what());
273 void CWakeupEngineManager::streaming_speech_data_thread_func()
277 if (nullptr == mSelectedEngine) {
278 MWR_LOGE("No Engine Selected");
282 const wakeup_engine_interface *interface = &(mSelectedEngine->interface);
284 if (NULL == interface ||
285 NULL == interface->get_utterance_data ||
286 NULL == interface->get_utterance_data_count)
289 MWR_LOGD("data_count : %d", interface->get_utterance_data_count());
296 MWR_LOGD("[Recorder Info] File not found!");
300 snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/ma_wue_%d_%d", getpid(), g_count);
301 int ret = access(g_temp_file_name, 0);
304 MWR_LOGD("[Recorder ERROR] File is already exist");
305 if (0 == remove(g_temp_file_name)) {
306 MWR_LOGD("[Recorder] Remove file");
316 MWR_LOGD("[Recorder] Temp file name=[%s]", g_temp_file_name);
319 g_pFile = fopen(g_temp_file_name, "wb+x");
321 MWR_LOGD("[Recorder ERROR] File not found!");
327 mas_speech_data speech_data;
329 bool finish_event_sent = false;
331 if (mWakeWordAudioRequired &&
332 NULL != interface->get_wake_word_data &&
333 NULL != interface->get_wake_word_data_count) {
334 for (const auto& observer : mObservers) {
336 if (!observer->on_audio_streaming_data_section(MA_AUDIO_STREAMING_DATA_SECTION_WAKE_WORD)) {
337 LOGE("[Recorder WARNING] One of the observer returned false");
341 int count = interface->get_wake_word_data_count();
342 while (!(mStopStreamingThread.load()) && index < count) {
343 int ret = interface->get_wake_word_data(index, &speech_data);
347 fwrite(speech_data.buffer, 1, speech_data.len, g_pFile);
349 for (const auto& observer : mObservers) {
351 if (!observer->on_streaming_audio_data(
352 speech_data.event, speech_data.buffer, speech_data.len)) {
353 LOGE("[Recorder WARNING] One of the observer returned false");
363 for (const auto& observer : mObservers) {
365 if (!observer->on_audio_streaming_data_section(MA_AUDIO_STREAMING_DATA_SECTION_UTTERANCE)) {
366 LOGE("[Recorder WARNING] One of the observer returned false");
373 while (!(mStopStreamingThread.load())) {
377 /* get feedback data */
378 if (interface && interface->get_utterance_data) {
379 ret = interface->get_utterance_data(index, &speech_data);
382 MWR_LOGD("[DEBUG] No feedback data. Waiting mode : %d", ret);
385 while (!(mStopStreamingThread.load())) {
387 this_thread::sleep_for(chrono::milliseconds(10));
388 if (index < interface->get_utterance_data_count()) {
389 MWR_LOGD("[INFO] Resume thread");
392 if (g_speech_pcm_wait_count < cnt) {
393 unsigned char final_buffer[2] = {'\0', };
394 MWR_LOGE("[ERROR] Wrong request, there's no pcm data");
397 fwrite(final_buffer, 1, sizeof(final_buffer), g_pFile);
398 MWR_LOGE("[Recorder SUCCESS] File Close");
403 for (const auto& observer : mObservers) {
405 if (!observer->on_streaming_audio_data(
406 MAS_SPEECH_STREAMING_EVENT_FAIL, NULL, 0)) {
407 LOGE("[Recorder WARNING] One of the observer returned false");
409 if (!observer->on_streaming_audio_data(
410 MAS_SPEECH_STREAMING_EVENT_FINISH, final_buffer, sizeof(final_buffer))) {
411 LOGE("[Recorder WARNING] One of the observer returned false");
419 MWR_LOGD("[INFO] Finish to wait for new feedback data come");
421 /* resume feedback thread */
427 fwrite(speech_data.buffer, 1, speech_data.len, g_pFile);
429 if (MAS_SPEECH_STREAMING_EVENT_FINISH == speech_data.event) {
431 MWR_LOGE("[Recorder SUCCESS] File Close");
435 MWR_LOGE("[Recorder ERROR] File not found!");
439 const int sleep_duration_in_millis = 10;
440 const int max_burst_count = 3;
441 if (++burst_count >= max_burst_count) {
443 this_thread::sleep_for(chrono::milliseconds(sleep_duration_in_millis));
444 MWR_LOGI("[INFO] Streaming data burst transmission detected, forcing sleep");
446 for (const auto& observer : mObservers) {
448 if (!observer->on_streaming_audio_data(
449 speech_data.event, speech_data.buffer, speech_data.len)) {
450 LOGE("[Recorder WARNING] One of the observer returned false");
455 if (MAS_SPEECH_STREAMING_EVENT_FINISH == speech_data.event) {
456 MWR_LOGI("[INFO] Finish to get and send speech data");
457 finish_event_sent = true;
465 if (true != finish_event_sent) {
466 unsigned char final_buffer[2] = {'\0', };
467 for (const auto& observer : mObservers) {
469 if (!observer->on_streaming_audio_data(
470 MAS_SPEECH_STREAMING_EVENT_FINISH, final_buffer, sizeof(final_buffer))) {
471 LOGE("[Recorder WARNING] One of the observer returned false");
477 fwrite(final_buffer, 1, sizeof(final_buffer), g_pFile);
478 MWR_LOGE("[Recorder SUCCESS] File Close");
488 void CWakeupEngineManager::start_streaming_current_utterance_data()
490 if (mStreamingThread.joinable()) {
491 MWR_LOGE("ERROR : mStreamingThread is joinable, will not start a new thread");
494 mStreamingThread = thread(&CWakeupEngineManager::streaming_speech_data_thread_func, this);
497 void CWakeupEngineManager::stop_streaming_current_utterance_data()
499 if (mStreamingThread.joinable()) {
500 MWR_LOGD("mStreamingThread is joinable, trying join()");
501 mStopStreamingThread.store(true);
502 mStreamingThread.join();
504 mStopStreamingThread.store(false);
507 void CWakeupEngineManager::update_manager_state(wakeup_manager_state_e state)
509 for (const auto& info : mEngineInfo) {
510 if (info.interface.update_manager_state) {
512 info.interface.update_manager_state(state);
513 } catch (const std::exception& e) {
514 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
515 info.engine_name.c_str(), e.what());
519 mWakeupManagerState = state;
522 void CWakeupEngineManager::update_recognition_result(string appid, int result)
524 if (mSelectedEngine) {
525 if (mSelectedEngine->interface.update_recognition_result) {
526 mSelectedEngine->interface.update_recognition_result(appid.c_str(), result);
531 void CWakeupEngineManager::engine_add_target_assistant(string engine_name, string appid)
533 const auto& iter = find_if(mEngineInfo.begin(), mEngineInfo.end(),
534 [engine_name](const EngineInfo& info) {
535 return (0 == info.engine_name.compare(engine_name));
538 if (mEngineInfo.end() == iter) {
539 /* Not found, add new library */
540 pkgmgrinfo_appinfo_h handle;
541 int ret = pkgmgrinfo_appinfo_get_appinfo(engine_name.c_str(), &handle);
542 if (PMINFO_R_OK == ret) {
543 char *root_path = nullptr;
544 ret = pkgmgrinfo_appinfo_get_root_path(handle, &root_path);
545 if (PMINFO_R_OK == ret && nullptr != root_path) {
546 string path = root_path;
548 path += MA_WAKEUP_DEDICATED_ENGINE_PATH;
549 add_engine(engine_name, path);
551 pkgmgrinfo_appinfo_destroy_appinfo(handle);
553 /* Find again to add appid to the newly created engine's assistant list */
554 const auto &new_iter = find_if(mEngineInfo.begin(), mEngineInfo.end(),
555 [engine_name](const EngineInfo& info) {
556 return (0 == info.engine_name.compare(engine_name));
558 if (mEngineInfo.end() != new_iter) {
559 new_iter->assistant_list.push_back(appid);
562 /* If the engine already exists, simply add the appid to the assistant list */
563 iter->assistant_list.push_back(appid);
567 void CWakeupEngineManager::engine_add_wakeup_word(string appid, string wakeup_word, string language)
569 for (const auto& info : mEngineInfo) {
570 bool found = contains(info.assistant_list, appid);
572 if (info.interface.add_wakeup_word) {
574 info.interface.add_wakeup_word(appid.c_str(), wakeup_word.c_str(), language.c_str());
575 } catch (const std::exception& e) {
576 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
577 info.engine_name.c_str(), e.what());
580 MWR_LOGE("Wakeup Engine does not provide add_wakeup_word");
586 void CWakeupEngineManager::engine_remove_wakeup_word(string appid, string wakeup_word, string language)
588 for (const auto& info : mEngineInfo) {
589 bool found = contains(info.assistant_list, appid);
591 if (info.interface.remove_wakeup_word) {
593 info.interface.remove_wakeup_word(appid.c_str(), wakeup_word.c_str(), language.c_str());
594 } catch (const std::exception& e) {
595 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
596 info.engine_name.c_str(), e.what());
599 MWR_LOGE("Wakeup Engine does not provide remove_wakeup_word");
605 void CWakeupEngineManager::engine_set_assistant_specific_command(string appid, string command)
607 for (const auto& info : mEngineInfo) {
608 bool found = contains(info.assistant_list, appid);
610 if (info.interface.set_assistant_specific_command) {
612 info.interface.set_assistant_specific_command(appid.c_str(), command.c_str());
613 } catch (const std::exception& e) {
614 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
615 info.engine_name.c_str(), e.what());
622 void CWakeupEngineManager::engine_feed_audio_data(long time, void* data, int len)
624 for (const auto& info : mEngineInfo) {
625 if (info.activated &&
626 info.audio_data_require_status &&
627 info.interface.feed_audio_data) {
629 bool filtered_out = false;
630 /* After a wakeup event, wakeup engines other than the selected one
631 does not need to receive audio data. */
632 if (WAKEUP_MANAGER_STATE_UTTERANCE == mWakeupManagerState) {
633 if (mSelectedEngine && &info != mSelectedEngine) {
638 int ret = info.interface.feed_audio_data(time, data, len);
640 LOGE("[ERROR] Fail to feed speech data, ret(%d) : %s", ret, info.engine_name.c_str());
643 } catch (const std::exception& e) {
644 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
645 info.engine_name.c_str(), e.what());
651 void CWakeupEngineManager::engine_set_dependency_module_command(string engine_name, string command)
653 const auto& iter = find_if(mEngineInfo.begin(), mEngineInfo.end(),
654 [engine_name](const EngineInfo& info) {
655 return (0 == info.engine_name.compare(engine_name));
658 if (mEngineInfo.end() != iter) {
659 if (iter->activated &&
660 iter->interface.set_dependency_module_command) {
662 int ret = iter->interface.set_dependency_module_command(command.c_str());
664 LOGE("[ERROR] Fail to set dependency module command, ret(%d) : %s",
665 ret, iter->engine_name.c_str());
667 } catch (const std::exception& e) {
668 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
669 iter->engine_name.c_str(), e.what());
675 bool CWakeupEngineManager::on_wakeup_event(string engine_name, mas_wakeup_event_info info)
679 for (const auto& observer : mObservers) {
681 if (!observer->on_wakeup_event(engine_name, info)) {
682 LOGE("[Recorder WARNING] One of the observer returned false");
690 bool CWakeupEngineManager::on_speech_status(string engine_name, mas_speech_status_e status)
694 for (const auto& observer : mObservers) {
696 if (!observer->on_speech_status(engine_name, status)) {
697 LOGE("[Recorder WARNING] One of the observer returned false");
705 bool CWakeupEngineManager::on_error(string engine_name, int error_code, string error_message)
709 for (const auto& observer : mObservers) {
711 if (!observer->on_error(engine_name, error_code, error_message)) {
712 LOGE("[Recorder WARNING] One of the observer returned false");
720 bool CWakeupEngineManager::on_audio_data_require_status(string engine_name, bool require)
722 MWR_LOGI("[ENTER] %s, %d", engine_name.c_str(), require);
727 for (auto& info : mEngineInfo) {
728 if (info.engine_name.compare(engine_name) == 0) {
730 info.audio_data_require_status = require;
732 if (info.activated && info.audio_data_require_status) {
736 MWR_LOGD("count : %d", count);
738 mAudioDataRequired = true;
740 mAudioDataRequired = false;
744 for (const auto& observer : mObservers) {
746 if (!observer->on_audio_data_require_status(engine_name, require)) {
747 LOGE("[Recorder WARNING] One of the observer returned false");
756 bool CWakeupEngineManager::on_wakeup_engine_command(string engine_name, mas_wakeup_engine_command_target_e target, string assistant_name, string command)
758 MWR_LOGI("[ENTER] : %s %d %s %s",
759 engine_name.c_str(), target, assistant_name.c_str(), command.c_str());
761 for (const auto& observer : mObservers) {
763 if (MAS_WAKEUP_ENGINE_COMMAND_TARGET_DEPENDENCY_MODULE == target) {
764 if (!observer->on_wakeup_engine_command(target, engine_name, assistant_name, command)) {
765 LOGE("[Recorder WARNING] One of the observer returned false");
768 const auto& iter = find_if(mEngineInfo.begin(), mEngineInfo.end(),
769 [engine_name](const EngineInfo& info) {
770 return (0 == info.engine_name.compare(engine_name));
772 if (mEngineInfo.end() != iter) {
773 for (const auto& assistant : iter->assistant_list) {
774 if (0 == assistant_name.compare(assistant) ||
775 MAS_WAKEUP_ENGINE_COMMAND_TARGET_ALL_ASSISTANTS == target) {
776 MWR_LOGI("Calling on_wakeup_engine_command for %s", assistant.c_str());
777 if (!observer->on_wakeup_engine_command(target, engine_name, assistant, command)) {
778 LOGE("[Recorder WARNING] One of the observer returned false");
790 void CWakeupEngineManager::add_engine(string name, string path)
792 MWR_LOGD("Name (%s), Filepath(%s)", name.c_str(), path.c_str());
796 info.engine_handle = dlopen(path.c_str(), RTLD_LAZY);
797 if (nullptr != (error = dlerror()) || nullptr == info.engine_handle) {
798 MWR_LOGD("[ERROR] Fail to dlopen(%s), error(%s)", path.c_str(), error);
799 if (info.engine_handle) dlclose(info.engine_handle);
803 /* Interfaces without version information */
804 info.interface.initialize =
805 (wakeup_engine_initialize)dlsym(info.engine_handle,
806 MA_WAKEUP_ENGINE_FUNC_INITIALIZE);
807 info.interface.deinitialize =
808 (wakeup_engine_deinitialize)dlsym(info.engine_handle,
809 MA_WAKEUP_ENGINE_FUNC_DEINITIALIZE);
810 info.interface.activate =
811 (wakeup_engine_activate)dlsym(info.engine_handle,
812 MA_WAKEUP_ENGINE_FUNC_ACTIVATE);
813 info.interface.deactivate =
814 (wakeup_engine_deactivate)dlsym(info.engine_handle,
815 MA_WAKEUP_ENGINE_FUNC_DEACTIVATE);
816 info.interface.add_wakeup_word =
817 (wakeup_engine_add_wakeup_word)dlsym(info.engine_handle,
818 MA_WAKEUP_ENGINE_FUNC_ADD_WAKEUP_WORD);
819 info.interface.remove_wakeup_word =
820 (wakeup_engine_remove_wakeup_word)dlsym(info.engine_handle,
821 MA_WAKEUP_ENGINE_FUNC_REMOVE_WAKEUP_WORD);
822 info.interface.add_language =
823 (wakeup_engine_add_language)dlsym(info.engine_handle,
824 MA_WAKEUP_ENGINE_FUNC_ADD_LANGUAGE);
825 info.interface.set_language =
826 (wakeup_engine_set_language)dlsym(info.engine_handle,
827 MA_WAKEUP_ENGINE_FUNC_SET_LANGUAGE);
828 info.interface.update_manager_state =
829 (wakeup_engine_update_manager_state)dlsym(info.engine_handle,
830 MA_WAKEUP_ENGINE_FUNC_UPDATE_MANAGER_STATE);
831 info.interface.update_recognition_result =
832 (wakeup_engine_update_recognition_result)dlsym(info.engine_handle,
833 MA_WAKEUP_ENGINE_FUNC_UPDATE_RECOGNITION_RESULT);
834 info.interface.set_audio_format =
835 (wakeup_engine_set_audio_format)dlsym(info.engine_handle,
836 MA_WAKEUP_ENGINE_FUNC_SET_AUDIO_FORMAT);
837 info.interface.get_audio_format =
838 (wakeup_engine_get_audio_format)dlsym(info.engine_handle,
839 MA_WAKEUP_ENGINE_FUNC_GET_AUDIO_FORMAT);
840 info.interface.feed_audio_data =
841 (wakeup_engine_feed_audio_data)dlsym(info.engine_handle,
842 MA_WAKEUP_ENGINE_FUNC_FEED_AUDIO_DATA);
843 info.interface.get_utterance_data_count =
844 (wakeup_engine_get_utterance_data_count)dlsym(info.engine_handle,
845 MA_WAKEUP_ENGINE_FUNC_GET_UTTERANCE_DATA_COUNT);
846 info.interface.get_utterance_data =
847 (wakeup_engine_get_utterance_data)dlsym(info.engine_handle,
848 MA_WAKEUP_ENGINE_FUNC_GET_UTTERANCE_DATA);
849 info.interface.get_wake_word_data_count =
850 (wakeup_engine_get_wake_word_data_count)dlsym(info.engine_handle,
851 MA_WAKEUP_ENGINE_FUNC_GET_WAKE_WORD_DATA_COUNT);
852 info.interface.get_wake_word_data =
853 (wakeup_engine_get_wake_word_data)dlsym(info.engine_handle,
854 MA_WAKEUP_ENGINE_FUNC_GET_WAKE_WORD_DATA);
855 info.interface.set_assistant_specific_command =
856 (wakeup_engine_set_assistant_specific_command)dlsym(info.engine_handle,
857 MA_WAKEUP_ENGINE_FUNC_SET_ASSISTANT_SPECIFIC_COMMAND);
858 info.interface.set_wake_word_audio_require_flag =
859 (wakeup_engine_set_wake_word_audio_require_flag)dlsym(info.engine_handle,
860 MA_WAKEUP_ENGINE_FUNC_SET_WAKE_WORD_AUDIO_REQUIRE_FLAG);
861 info.interface.set_wakeup_event_callback =
862 (wakeup_engine_set_wakeup_event_callback)dlsym(info.engine_handle,
863 MA_WAKEUP_ENGINE_FUNC_SET_WAKEUP_EVENT_CALLBACK);
864 info.interface.set_speech_status_callback =
865 (wakeup_engine_set_speech_status_callback)dlsym(info.engine_handle,
866 MA_WAKEUP_ENGINE_FUNC_SET_SPEECH_STATUS_CALLBACK);
867 info.interface.set_error_callback =
868 (wakeup_engine_set_error_callback)dlsym(info.engine_handle,
869 MA_WAKEUP_ENGINE_FUNC_SET_ERROR_CALLBACK);
870 info.interface.set_audio_data_require_status_callback =
871 (wakeup_engine_set_audio_data_require_status_callback)dlsym(info.engine_handle,
872 MA_WAKEUP_ENGINE_FUNC_SET_AUDIO_DATA_REQUIRE_STATUS_CALLBACK);
873 info.interface.set_wakeup_engine_command_callback =
874 (wakeup_engine_set_wakeup_engine_command_callback)dlsym(info.engine_handle,
875 MA_WAKEUP_ENGINE_FUNC_SET_WAKEUP_ENGINE_COMMAND_CALLBACK);
877 /* Interfaces after version 1 */
878 info.interface.get_version =
879 (wakeup_engine_get_version)dlsym(info.engine_handle,
880 MA_WAKEUP_ENGINE_FUNC_GET_VERSION);
881 info.interface.set_dependency_module_command =
882 (wakeup_engine_set_dependency_module_command)dlsym(info.engine_handle,
883 MA_WAKEUP_ENGINE_FUNC_SET_DEPENDENCY_MODULE_COMMAND);
886 info.engine_path = path;
887 info.engine_name = name;
889 info.activated = false;
890 info.audio_data_require_status = false;
892 /* All the necessary information has already been set properly */
893 mEngineInfo.push_back(info);
895 MWR_LOGD("Initializing wakeup engine : %s %p",
896 info.engine_path.c_str(),
897 info.interface.initialize);
899 /* Workaround for registering C-style callbacks */
901 CWakeupEngineManager *manager;
905 static deque<CallbackUserData> callback_user_data;
907 CallbackUserData user_data;
908 user_data.manager = this;
909 user_data.engine_name = info.engine_name;
910 callback_user_data.push_back(user_data);
913 if (info.interface.set_wakeup_event_callback) {
914 info.interface.set_wakeup_event_callback(
915 [](mas_wakeup_event_info info, void* user_data) {
916 CallbackUserData *data = static_cast<CallbackUserData*>(user_data);
917 if (nullptr == data) return;
918 if (nullptr == data->manager) return;
919 info.wakeup_engine = data->engine_name.c_str();
920 data->manager->on_wakeup_event(data->engine_name, info);
921 }, &(callback_user_data.back()));
924 if (info.interface.set_audio_data_require_status_callback) {
925 info.interface.set_audio_data_require_status_callback(
926 [](bool require, void* user_data) {
927 CallbackUserData *data = static_cast<CallbackUserData*>(user_data);
928 if (nullptr == data) return;
929 if (nullptr == data->manager) return;
930 data->manager->on_audio_data_require_status(data->engine_name, require);
931 }, &(callback_user_data.back()));
934 if (info.interface.set_wakeup_engine_command_callback) {
935 info.interface.set_wakeup_engine_command_callback(
936 [](mas_wakeup_engine_command_target_e target,
937 const char* assistant_name, const char* command, void* user_data) {
938 CallbackUserData* data = static_cast<CallbackUserData*>(user_data);
939 if (nullptr == data) return;
940 if (nullptr == data->manager) return;
941 if (nullptr == command) return;
942 data->manager->on_wakeup_engine_command(
943 data->engine_name, target, (assistant_name ? assistant_name : ""), command);
944 }, &(callback_user_data.back()));
947 if (info.interface.initialize) {
948 info.interface.initialize();
950 if (info.interface.get_version) {
952 if (0 == info.interface.get_version(&version)) {
953 info.version = version;
956 } catch (const std::exception& e) {
957 MWR_LOGE("[ERROR] wakeup engine %s threw exception : %s",
958 info.engine_name.c_str(), e.what());
962 void CWakeupEngineManager::add_engine_directory(string name, string path)
964 if (0 == path.size()) return;
966 DIR* dp = opendir(path.c_str());
968 MWR_LOGD("Failed opening directory : %s", path.c_str());
970 struct dirent *dirp = NULL;
976 if (!strcmp(".", dirp->d_name) || !strcmp("..", dirp->d_name))
979 if (DT_REG != dirp->d_type) /* If not a regular file */
984 filepath += dirp->d_name;
986 if (filepath.length() >= _POSIX_PATH_MAX) {
987 MWR_LOGD("File path is too long : %s", filepath.c_str());
991 add_engine(name, filepath);
993 } while (NULL != dirp);