Merge branch 'tizen_5.5' into tizen
[platform/core/uifw/multi-assistant-service.git] / plugins / wakeup-manager / inc / wakeup_engine_manager.h
1 /*
2  * Copyright 2018-2019 Samsung Electronics Co., Ltd
3  *
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
7  *
8  * http://floralicense.org/license/
9  *
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.
15  */
16
17 #ifndef _WAKEUP_ENGINE_MANAGER_H_
18 #define _WAKEUP_ENGINE_MANAGER_H_
19
20 #include "wakeup_manager_wrapper.h"
21
22 #include <atomic>
23 #include <string>
24 #include <thread>
25 #include <vector>
26 #include <set>
27
28 namespace multiassistant
29 {
30 namespace wakeup
31 {
32
33 using namespace std;
34
35 /**************************************************************************************
36  *** Definitions for wakeup engine interface
37  *************************************************************************************/
38 #define MA_WAKEUP_ENGINE_PATH                                   tzplatform_mkpath(tzplatform_getid("TZ_SYS_RO_SHARE"), "multiassistant/engines")
39 /* Need to check whether this dedicated engine path also needs to be configurable */
40 #define MA_WAKEUP_DEDICATED_ENGINE_PATH                 "shared/lib/libwakeup-engine.so"
41
42 /* Interfaces without version information */
43 #define MA_WAKEUP_ENGINE_FUNC_INITIALIZE "wakeup_engine_initialize"
44 typedef int (*wakeup_engine_initialize)(void);
45 #define MA_WAKEUP_ENGINE_FUNC_DEINITIALIZE "wakeup_engine_deinitialize"
46 typedef int (*wakeup_engine_deinitialize)(void);
47 #define MA_WAKEUP_ENGINE_FUNC_ACTIVATE "wakeup_engine_activate"
48 typedef int (*wakeup_engine_activate)(void);
49 #define MA_WAKEUP_ENGINE_FUNC_DEACTIVATE "wakeup_engine_deactivate"
50 typedef int (*wakeup_engine_deactivate)(void);
51 #define MA_WAKEUP_ENGINE_FUNC_ADD_WAKEUP_WORD "wakeup_engine_add_wakeup_word"
52 typedef int (*wakeup_engine_add_wakeup_word)(const char* appid, const char* wakeup_word, const char* language);
53 #define MA_WAKEUP_ENGINE_FUNC_REMOVE_WAKEUP_WORD "wakeup_engine_remove_wakeup_word"
54 typedef int (*wakeup_engine_remove_wakeup_word)(const char* appid, const char* wakeup_word, const char* language);
55 #define MA_WAKEUP_ENGINE_FUNC_ADD_LANGUAGE "wakeup_engine_add_language"
56 typedef int (*wakeup_engine_add_language)(const char* appid, const char* language);
57 #define MA_WAKEUP_ENGINE_FUNC_SET_LANGUAGE "wakeup_engine_set_language"
58 typedef int (*wakeup_engine_set_language)(const char* language);
59 #define MA_WAKEUP_ENGINE_FUNC_UPDATE_MANAGER_STATE "wakeup_engine_update_manager_state"
60 typedef int (*wakeup_engine_update_manager_state)(wakeup_manager_state_e state);
61 #define MA_WAKEUP_ENGINE_FUNC_UPDATE_RECOGNITION_RESULT "wakeup_engine_update_recognition_result"
62 typedef int (*wakeup_engine_update_recognition_result)(const char* appid, int result);
63 #define MA_WAKEUP_ENGINE_FUNC_SET_AUDIO_FORMAT "wakeup_engine_set_audio_format"
64 typedef int (*wakeup_engine_set_audio_format)(int rate, int channel, int audio_type);
65 #define MA_WAKEUP_ENGINE_FUNC_GET_AUDIO_FORMAT "wakeup_engine_get_audio_format"
66 typedef int (*wakeup_engine_get_audio_format)(int* rate, int* channel, int* audio_type);
67 #define MA_WAKEUP_ENGINE_FUNC_FEED_AUDIO_DATA "wakeup_engine_feed_audio_data"
68 typedef int (*wakeup_engine_feed_audio_data)(long time, void* data, int len);
69 #define MA_WAKEUP_ENGINE_FUNC_GET_UTTERANCE_DATA_COUNT "wakeup_engine_get_utterance_data_count"
70 typedef int (*wakeup_engine_get_utterance_data_count)(void);
71 #define MA_WAKEUP_ENGINE_FUNC_GET_UTTERANCE_DATA "wakeup_engine_get_utterance_data"
72 typedef int (*wakeup_engine_get_utterance_data)(int index, mas_speech_data *data);
73 #define MA_WAKEUP_ENGINE_FUNC_GET_WAKE_WORD_DATA_COUNT "wakeup_engine_get_wake_word_data_count"
74 typedef int (*wakeup_engine_get_wake_word_data_count)(void);
75 #define MA_WAKEUP_ENGINE_FUNC_GET_WAKE_WORD_DATA "wakeup_engine_get_wake_word_data"
76 typedef int (*wakeup_engine_get_wake_word_data)(int index, mas_speech_data *data);
77 #define MA_WAKEUP_ENGINE_FUNC_SET_ASSISTANT_SPECIFIC_COMMAND "wakeup_engine_set_assistant_specific_command"
78 typedef int (*wakeup_engine_set_assistant_specific_command)(const char* appid, const char* command);
79 #define MA_WAKEUP_ENGINE_FUNC_SET_WAKE_WORD_AUDIO_REQUIRE_FLAG "wakeup_engine_set_wake_word_audio_require_flag"
80 typedef int (*wakeup_engine_set_wake_word_audio_require_flag)(bool require);
81 #define MA_WAKEUP_ENGINE_FUNC_SET_WAKEUP_EVENT_CALLBACK "wakeup_engine_set_wakeup_event_callback"
82 typedef int (*wakeup_engine_set_wakeup_event_callback)(wakeup_service_wakeup_event_cb callback, void* user_data);
83 #define MA_WAKEUP_ENGINE_FUNC_SET_SPEECH_STATUS_CALLBACK "wakeup_engine_set_speech_status_callback"
84 typedef int (*wakeup_engine_set_speech_status_callback)(wakeup_service_speech_status_cb callback, void* user_data);
85 #define MA_WAKEUP_ENGINE_FUNC_SET_ERROR_CALLBACK "wakeup_engine_set_error_callback"
86 typedef int (*wakeup_engine_set_error_callback)(wakeup_service_error_cb callback, void* user_data);
87 #define MA_WAKEUP_ENGINE_FUNC_SET_AUDIO_DATA_REQUIRE_STATUS_CALLBACK "wakeup_engine_set_audio_data_require_status_callback"
88 typedef int (*wakeup_engine_set_audio_data_require_status_callback)(wakeup_service_audio_data_require_status_cb callback, void* user_data);
89 #define MA_WAKEUP_ENGINE_FUNC_SET_WAKEUP_ENGINE_COMMAND_CALLBACK "wakeup_engine_set_wakeup_engine_command_callback"
90 typedef int (*wakeup_engine_set_wakeup_engine_command_callback)(wakeup_service_engine_command_cb callback, void* user_data);
91
92 /* Interfaces after version 1 */
93 #define MA_WAKEUP_ENGINE_FUNC_GET_VERSION "wakeup_engine_get_version"
94 typedef int (*wakeup_engine_get_version)(int* version);
95 #define MA_WAKEUP_ENGINE_FUNC_SET_DEPENDENCY_MODULE_COMMAND "wakeup_engine_set_dependency_module_command"
96 typedef int (*wakeup_engine_set_dependency_module_command)(const char* command);
97
98 typedef struct {
99         wakeup_engine_initialize                                                                initialize;
100         wakeup_engine_deinitialize                                                              deinitialize;
101         wakeup_engine_activate                                                                  activate;
102         wakeup_engine_deactivate                                                                deactivate;
103         wakeup_engine_add_wakeup_word                                                   add_wakeup_word;
104         wakeup_engine_remove_wakeup_word                                                remove_wakeup_word;
105         wakeup_engine_add_language                                                              add_language;
106         wakeup_engine_set_language                                                              set_language;
107         wakeup_engine_update_manager_state                                              update_manager_state;
108         wakeup_engine_update_recognition_result                                 update_recognition_result;
109         wakeup_engine_set_audio_format                                                  set_audio_format;
110         wakeup_engine_get_audio_format                                                  get_audio_format;
111         wakeup_engine_feed_audio_data                                                   feed_audio_data;
112         wakeup_engine_get_utterance_data_count                                  get_utterance_data_count;
113         wakeup_engine_get_utterance_data                                                get_utterance_data;
114         wakeup_engine_get_wake_word_data_count                                  get_wake_word_data_count;
115         wakeup_engine_get_wake_word_data                                                get_wake_word_data;
116         wakeup_engine_get_version                                                               get_version;
117         wakeup_engine_set_assistant_specific_command                    set_assistant_specific_command;
118         wakeup_engine_set_wake_word_audio_require_flag                  set_wake_word_audio_require_flag;
119         wakeup_engine_set_dependency_module_command                             set_dependency_module_command;
120         wakeup_engine_set_wakeup_event_callback                                 set_wakeup_event_callback;
121         wakeup_engine_set_speech_status_callback                                set_speech_status_callback;
122         wakeup_engine_set_error_callback                                                set_error_callback;
123         wakeup_engine_set_audio_data_require_status_callback    set_audio_data_require_status_callback;
124         wakeup_engine_set_wakeup_engine_command_callback                set_wakeup_engine_command_callback;
125 } wakeup_engine_interface;
126
127 class IEngineEventObserver
128 {
129 public:
130         virtual ~IEngineEventObserver() = default;
131         virtual bool on_wakeup_event(string engine_name, mas_wakeup_event_info wakeup_info) = 0;
132         virtual bool on_speech_status(string engine_name, mas_speech_status_e status) = 0;
133         virtual bool on_error(string engine_name, int error_code, string error_message) = 0;
134         virtual bool on_audio_data_require_status(string engine_name, bool require) = 0;
135
136         virtual bool on_streaming_audio_data(
137                 mas_speech_streaming_event_e event, void* buffer, unsigned int len) = 0;
138         virtual bool on_audio_streaming_data_section(ma_audio_streaming_data_section_e section) = 0;
139         virtual bool on_wakeup_engine_command(
140                 mas_wakeup_engine_command_target_e target, string engine_name, string assistant_name, string command) = 0;
141 };
142
143 class CWakeupEngineManager
144 {
145 public:
146         CWakeupEngineManager();
147         CWakeupEngineManager(IEngineEventObserver *observer);
148         virtual ~CWakeupEngineManager();
149
150         CWakeupEngineManager(const CWakeupEngineManager&) = delete;
151         CWakeupEngineManager& operator=(const CWakeupEngineManager&) = delete;
152
153         void initialize();
154         void deinitialize();
155
156         void subscribe(IEngineEventObserver *observer);
157         void unsubscribe(IEngineEventObserver *observer);
158
159         bool get_audio_data_required();
160         void set_selected_wakeup_info(mas_wakeup_event_info wakeup_info);
161
162         bool set_language(string language);
163         bool set_assistant_language(string appid, string language);
164         void set_assistant_activated(string appid, bool activated);
165         bool get_assistant_activated(string appid);
166         void set_wake_word_audio_require_flag(bool require);
167
168         void start_streaming_current_utterance_data();
169         void stop_streaming_current_utterance_data();
170
171         void update_manager_state(wakeup_manager_state_e state);
172         void update_recognition_result(string appid, int result);
173
174         void engine_add_target_assistant(string engine_name, string appid);
175         void engine_add_wakeup_word(string appid, string wakeup_word, string language);
176         void engine_remove_wakeup_word(string appid, string wakeup_word, string language);
177         void engine_set_assistant_specific_command(string appid, string command);
178         void engine_set_dependency_module_command(string engine_name, string command);
179         void engine_feed_audio_data(long time, void* data, int len);
180
181         bool on_wakeup_event(string engine_name, mas_wakeup_event_info wakeup_info);
182         bool on_speech_status(string engine_name, mas_speech_status_e status);
183         bool on_error(string engine_name, int error_code, string error_message);
184         bool on_audio_data_require_status(string engine_name, bool require);
185         bool on_wakeup_engine_command(string engine_name,
186                 mas_wakeup_engine_command_target_e target, string assistant_name, string command);
187 private:
188         typedef struct {
189                 int version;
190                 string engine_name;
191                 bool activated{false};
192                 bool audio_data_require_status{false};
193                 string engine_path;
194                 wakeup_engine_interface interface{nullptr, };
195                 vector<string> assistant_list;
196                 set<string> activated_assistants;
197                 void *engine_handle{nullptr};
198         } EngineInfo;
199
200         void add_engine_directory(string name, string path);
201         void add_engine(string name, string path);
202
203         vector<IEngineEventObserver*> mObservers;
204
205         void streaming_speech_data_thread_func();
206
207         vector<EngineInfo> mEngineInfo;
208         const EngineInfo* mSelectedEngine{nullptr};
209         bool mAudioDataRequired{false};
210
211         thread mStreamingThread;
212         atomic_bool mStopStreamingThread{false};
213         wakeup_manager_state_e mWakeupManagerState{WAKEUP_MANAGER_STATE_INACTIVE};
214
215         bool mWakeWordAudioRequired{false};
216 };
217
218 } // wakeup
219 } // multiassistant
220
221 #endif /* _WAKEUP_ENGINE_MANAGER_H_ */