Fix ducking deactivation retrial logic
[platform/core/uifw/tts.git] / server / BackgroundVolume.h
1 /*
2 *  Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3 *  Licensed under the Apache License, Version 2.0 (the "License");
4 *  you may not use this file except in compliance with the License.
5 *  You may obtain a copy of the License at
6 *  http://www.apache.org/licenses/LICENSE-2.0
7 *  Unless required by applicable law or agreed to in writing, software
8 *  distributed under the License is distributed on an "AS IS" BASIS,
9 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 *  See the License for the specific language governing permissions and
11 *  limitations under the License.
12 */
13
14 #ifndef __TTSD_BACKGROUND_VOLUME_H_
15 #define __TTSD_BACKGROUND_VOLUME_H_
16
17
18 #include <mutex>
19 #include <chrono>
20 #include <atomic>
21 #include <Ecore.h>
22
23 #include <sound_manager.h>
24
25 #include "ttse.h"
26
27 class BackgroundVolume {
28 public:
29         BackgroundVolume(long long int duckingDuration);
30         ~BackgroundVolume();
31
32         void setVolumeRatio(double ratio);
33         double getVolumeRatio();
34         void applyVolumeRatio();
35         void recoverVolumeRatio();
36
37 private:
38         static void changeVolumeOnMainThread(void* data);
39         static void modifyVolumeOnMainThread(void* data);
40         static void recoverVolumeOnMainThread(void* data);
41         static Eina_Bool postponedRecoverTimerCb(void* data);
42         static Eina_Bool postponedModifyTimerCb(void* data);
43
44         int createHandles();
45         bool isDuckingHandleValid();
46         long long int getMsecDurationAfterDucking();
47         sound_stream_ducking_h getStreamDuckingHandle(sound_stream_type_e type);
48
49         void activateDuckingAll(unsigned int duration, double ratio);
50         bool activateDucking(sound_stream_type_e type, unsigned int duration, double ratio);
51         bool deactivateDuckingAll();
52         bool deactivateDucking(sound_stream_type_e type);
53
54 private:
55         const long long int mDuckingDuration;
56         sound_stream_ducking_h mMediaStream;
57         sound_stream_ducking_h mNotificationStream;
58         sound_stream_ducking_h mAlarmStream;
59         std::atomic<double> mVolumeRatio;
60         std::atomic<bool> mVolumeDucked;
61
62         std::chrono::time_point<std::chrono::steady_clock> mChangeVolumeTime;
63         Ecore_Timer* mPostponedRecoverTimer;
64         Ecore_Timer* mPostponedModifyTimer;
65 };
66
67
68 #endif /* __TTSD_BACKGROUND_VOLUME_H_ */