(de)activate wakeup engines by voice_input_language
[platform/core/uifw/multi-assistant-service.git] / plugins / wakeup-manager / inc / wakeup_engine_manager.h
1 /*
2  * Copyright 2018  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 #define MA_WAKEUP_ENGINE_FUNC_INITIALIZE "wakeup_engine_initialize"
43 typedef int (*wakeup_engine_initialize)(void);
44 #define MA_WAKEUP_ENGINE_FUNC_DEINITIALIZE "wakeup_engine_deinitialize"
45 typedef int (*wakeup_engine_deinitialize)(void);
46 #define MA_WAKEUP_ENGINE_FUNC_ACTIVATE "wakeup_engine_activate"
47 typedef int (*wakeup_engine_activate)(void);
48 #define MA_WAKEUP_ENGINE_FUNC_DEACTIVATE "wakeup_engine_deactivate"
49 typedef int (*wakeup_engine_deactivate)(void);
50 #define MA_WAKEUP_ENGINE_FUNC_ADD_WAKEUP_WORD "wakeup_engine_add_wakeup_word"
51 typedef int (*wakeup_engine_add_wakeup_word)(const char* appid, const char* wakeup_word, const char* language);
52 #define MA_WAKEUP_ENGINE_FUNC_ADD_LANGUAGE "wakeup_engine_add_language"
53 typedef int (*wakeup_engine_add_language)(const char* appid, const char* language);
54 #define MA_WAKEUP_ENGINE_FUNC_SET_LANGUAGE "wakeup_engine_set_language"
55 typedef int (*wakeup_engine_set_language)(const char* language);
56 #define MA_WAKEUP_ENGINE_FUNC_UPDATE_MANAGER_STATE "wakeup_engine_update_manager_state"
57 typedef int (*wakeup_engine_update_manager_state)(wakeup_manager_state_e state);
58 #define MA_WAKEUP_ENGINE_FUNC_SET_AUDIO_FORMAT "wakeup_engine_set_audio_format"
59 typedef int (*wakeup_engine_set_audio_format)(int rate, int channel, int audio_type);
60 #define MA_WAKEUP_ENGINE_FUNC_GET_AUDIO_FORMAT "wakeup_engine_get_audio_format"
61 typedef int (*wakeup_engine_get_audio_format)(int* rate, int* channel, int* audio_type);
62 #define MA_WAKEUP_ENGINE_FUNC_FEED_AUDIO_DATA "wakeup_engine_feed_audio_data"
63 typedef int (*wakeup_engine_feed_audio_data)(long time, void* data, int len);
64 #define MA_WAKEUP_ENGINE_FUNC_GET_UTTERANCE_DATA_COUNT "wakeup_engine_get_utterance_data_count"
65 typedef int (*wakeup_engine_get_utterance_data_count)(void);
66 #define MA_WAKEUP_ENGINE_FUNC_GET_UTTERANCE_DATA "wakeup_engine_get_utterance_data"
67 typedef int (*wakeup_engine_get_utterance_data)(int index, wakeup_speech_data *data);
68 #define MA_WAKEUP_ENGINE_FUNC_SET_ASSISTANT_SPECIFIC_COMMAND "wakeup_engine_set_assistant_specific_command"
69 typedef int (*wakeup_engine_set_assistant_specific_command)(const char* appid, const char* command);
70 #define MA_WAKEUP_ENGINE_FUNC_SET_WAKEUP_EVENT_CALLBACK "wakeup_engine_set_wakeup_event_callback"
71 typedef int (*wakeup_engine_set_wakeup_event_callback)(wakeup_service_wakeup_event_cb callback, void* user_data);
72 #define MA_WAKEUP_ENGINE_FUNC_SET_SPEECH_STATUS_CALLBACK "wakeup_engine_set_speech_status_callback"
73 typedef int (*wakeup_engine_set_speech_status_callback)(wakeup_service_speech_status_cb callback, void* user_data);
74 #define MA_WAKEUP_ENGINE_FUNC_SET_ERROR_CALLBACK "wakeup_engine_set_error_callback"
75 typedef int (*wakeup_engine_set_error_callback)(wakeup_service_error_cb callback, void* user_data);
76 #define MA_WAKEUP_ENGINE_FUNC_SET_AUDIO_DATA_REQUIRE_STATUS_CALLBACK "wakeup_engine_set_audio_data_require_status_callback"
77 typedef int (*wakeup_engine_set_audio_data_require_status_callback)(wakeup_service_audio_data_require_status_cb callback, void* user_data);
78
79 typedef struct {
80         wakeup_engine_initialize                                                                initialize;
81         wakeup_engine_deinitialize                                                              deinitialize;
82         wakeup_engine_activate                                                                  activate;
83         wakeup_engine_deactivate                                                                deactivate;
84         wakeup_engine_add_wakeup_word                                                   add_wakeup_word;
85         wakeup_engine_add_language                                                              add_language;
86         wakeup_engine_set_language                                                              set_language;
87         wakeup_engine_update_manager_state                                              update_manager_state;
88         wakeup_engine_set_audio_format                                                  set_audio_format;
89         wakeup_engine_get_audio_format                                                  get_audio_format;
90         wakeup_engine_feed_audio_data                                                   feed_audio_data;
91         wakeup_engine_get_utterance_data_count                                  get_utterance_data_count;
92         wakeup_engine_get_utterance_data                                                get_utterance_data;
93         wakeup_engine_set_assistant_specific_command                    set_assistant_specific_command;
94         wakeup_engine_set_wakeup_event_callback                                 set_wakeup_event_callback;
95         wakeup_engine_set_speech_status_callback                                set_speech_status_callback;
96         wakeup_engine_set_error_callback                                                set_error_callback;
97         wakeup_engine_set_audio_data_require_status_callback    set_audio_data_require_status_callback;
98 } wakeup_engine_interface;
99
100 class IEngineEventObserver
101 {
102 public:
103         virtual ~IEngineEventObserver() = default;
104         virtual bool on_wakeup_event(string engine_name, wakeup_event_info info) = 0;
105         virtual bool on_speech_status(string engine_name, wakeup_service_speech_status_e status) = 0;
106         virtual bool on_error(string engine_name, int error_code, string error_message) = 0;
107         virtual bool on_audio_data_require_status(string engine_name, bool require) = 0;
108
109         virtual bool on_streaming_audio_data(
110                 wakeup_speech_streaming_event_e event, void* buffer, unsigned int len) = 0;
111 };
112
113 class CWakeupEngineManager
114 {
115 public:
116         CWakeupEngineManager();
117         CWakeupEngineManager(IEngineEventObserver *observer);
118         virtual ~CWakeupEngineManager();
119
120         CWakeupEngineManager(const CWakeupEngineManager&) = delete;
121         CWakeupEngineManager& operator=(const CWakeupEngineManager&) = delete;
122
123         void initialize();
124         void deinitialize();
125
126         void subscribe(IEngineEventObserver *observer);
127         void unsubscribe(IEngineEventObserver *observer);
128
129         bool get_audio_data_required();
130         void set_selected_wakeup_info(wakeup_event_info info);
131
132         bool set_language(string language);
133         void set_assistant_activated(string appid, bool activated);
134
135         void start_streaming_current_utterance_data();
136         void stop_streaming_current_utterance_data();
137
138         void update_manager_state(wakeup_manager_state_e state);
139
140         void engine_add_target_assistant(string engine_name, string appid);
141         void engine_add_wakeup_word(string appid, string wakeup_word, string language);
142         void engine_set_assistant_specific_command(string appid, string command);
143         void engine_feed_audio_data(long time, void* data, int len);
144
145         bool on_wakeup_event(string engine_name, wakeup_event_info info);
146         bool on_speech_status(string engine_name, wakeup_service_speech_status_e status);
147         bool on_error(string engine_name, int error_code, string error_message);
148         bool on_audio_data_require_status(string engine_name, bool require);
149 private:
150         typedef struct {
151                 string engine_name;
152                 bool activated{false};
153                 bool audio_data_require_status{false};
154                 string engine_path;
155                 wakeup_engine_interface interface{nullptr, };
156                 vector<string> assistant_list;
157                 set<string> activated_assistants;
158                 void *engine_handle{nullptr};
159         } EngineInfo;
160
161         void add_engine_directory(string name, string path);
162         void add_engine(string name, string path);
163
164         vector<IEngineEventObserver*> mObservers;
165
166         void streaming_speech_data_thread_func();
167
168         vector<EngineInfo> mEngineInfo;
169         const EngineInfo* mSelectedEngine{nullptr};
170         bool mAudioDataRequired{false};
171
172         thread mStreamingThread;
173         atomic_bool mStopStreamingThread{false};
174 };
175
176 } // wakeup
177 } // multiassistant
178
179 #endif /* _WAKEUP_ENGINE_MANAGER_H_ */