Update year information of license boilerplate
[platform/core/uifw/multi-assistant-service.git] / plugins / wakeup-manager / inc / wakeup_audio_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
18 #ifndef _WAKEUP_AUDIO_MANAGER_H_
19 #define _WAKEUP_AUDIO_MANAGER_H_
20
21 #include <atomic>
22 #include <list>
23 #include <mutex>
24 #include <thread>
25 #include <vector>
26
27 #include <multi_assistant_service.h>
28 #include <audio_io.h>
29 #include <sound_manager.h>
30 #include <sound_manager_internal.h>
31
32 namespace multiassistant
33 {
34 namespace wakeup
35 {
36
37 using namespace std;
38
39 class IAudioEventObserver
40 {
41 public:
42         virtual ~IAudioEventObserver() = default;
43         virtual bool on_recording_audio_data(long time, void* data, int len) = 0;
44         virtual bool on_streaming_audio_data(
45                 mas_speech_streaming_event_e event, void* buffer, unsigned int len) = 0;
46 };
47
48 typedef enum
49 {
50         RECORDING_SESSION_WAKE_WORD,
51         RECORDING_SESSION_UTTERANCE,
52         RECORDING_SESSION_FOLLOW_UP,
53 } recording_session;
54
55 class CAudioManager
56 {
57 public:
58         CAudioManager();
59         CAudioManager(IAudioEventObserver *observer);
60         ~CAudioManager();
61
62         CAudioManager(const CAudioManager&) = delete;
63         CAudioManager& operator=(const CAudioManager&) = delete;
64
65         int initialize();
66         int deinitialize();
67
68         void sound_focus_changed();
69
70         void subscribe(IAudioEventObserver *observer);
71         void unsubscribe(IAudioEventObserver *observer);
72
73         void start_recording(bool proactive);
74         void stop_recording(bool proactive);
75
76         void set_recording_session(recording_session session);
77
78         void feed_audio_data(mas_speech_streaming_event_e event, void* buffer, int len);
79         void finalize_audio_data();
80         void clear_audio_data();
81
82         void start_streaming_current_utterance_data(long start_time = 0);
83         void stop_streaming_current_utterance_data();
84
85         void start_streaming_previous_utterance_data();
86         void stop_streaming_previous_utterance_data();
87
88         void start_streaming_follow_up_data();
89         void stop_streaming_follow_up_data();
90
91         void set_background_volume(double ratio);
92 private:
93         void add_audio_data(mas_speech_data& data, long time);
94         void notify_audio_data_recording(long time, void* data, int len);
95
96         void streaming_previous_audio_data_thread_func();
97         void streaming_audio_data_thread_func(long start_time);
98
99         int mSoundFocusWatchId{0};
100         bool mRecordingRequired{false};
101         bool mIsRecording{false};
102
103         vector<IAudioEventObserver*> mObservers;
104
105         thread mStreamingThread;
106         atomic_bool mStopStreamingThread{false};
107
108         thread mStreamingPreviousThread;
109         atomic_bool mStopStreamingPreviousThread{false};
110
111         static constexpr long mAudioRecordingDurationMilliseconds = 10 * 1000;
112         typedef struct {
113                 long time;
114                 mas_speech_data data;
115         } mas_speech_data_with_time;
116         vector<mas_speech_data_with_time> mPreviousAudioData;
117         list<mas_speech_data_with_time> mAudioData;
118
119         mutex mMutex;
120         bool mVoiceKeyPressed{false};
121 };
122
123 } // wakeup
124 } // multiassistant
125
126 #endif /* _WAKEUP_AUDIO_MANAGER_H_ */