Fix build error on gcc-13 Build
[platform/core/uifw/voice-control.git] / client / vc_mgr_ducking.cpp
1 /*
2 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 #include <sound_manager.h>
18
19 #include "vc_main.h"
20
21 #include "vc_mgr_ducking.h"
22
23 #define SND_MGR_DUCKING_DURATION 500
24
25 /* for changing volume on each sound stream */
26 static sound_stream_ducking_h g_media_stream_h = nullptr;
27 static sound_stream_ducking_h g_system_stream_h = nullptr;
28 static sound_stream_ducking_h g_notification_stream_h = nullptr;
29 static sound_stream_ducking_h g_alarm_stream_h = nullptr;
30 static sound_stream_ducking_h g_voice_information_stream_h = nullptr;
31
32 static bool g_is_created = false;
33
34
35 static const char *get_ducking_stream(sound_stream_type_e stream_type)
36 {
37         if (SOUND_STREAM_TYPE_MEDIA == stream_type)
38                 return "Media stream";
39         else if (SOUND_STREAM_TYPE_SYSTEM == stream_type)
40                 return "System stream";
41         else if (SOUND_STREAM_TYPE_NOTIFICATION == stream_type)
42                 return "Notification stream";
43         else if (SOUND_STREAM_TYPE_ALARM == stream_type)
44                 return "Alarm stream";
45         else if (SOUND_STREAM_TYPE_VOICE_INFORMATION == stream_type)
46                 return "Voice information stream";
47
48         return "Non matched stream";
49 }
50
51 static sound_stream_ducking_h create_ducking_handle(sound_stream_type_e type)
52 {
53         sound_stream_ducking_h ducking_handle = nullptr;
54
55         int ret = sound_manager_create_stream_ducking(type, nullptr, nullptr, &ducking_handle);
56         if (SOUND_MANAGER_ERROR_NONE != ret) {
57                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to create stream ducking. type(%d) ret(%d/%s)", type, ret, get_error_message(ret));
58                 return nullptr;
59         }
60
61         SLOG(LOG_INFO, TAG_VCM, "Create ducking handle. type(%d) handle(%p)", type, ducking_handle);
62         return ducking_handle;
63 }
64
65 static void destroy_ducking_handle(sound_stream_ducking_h ducking_handle)
66 {
67         if (nullptr == ducking_handle) {
68                 return;
69         }
70
71         SLOG(LOG_WARN, TAG_VCM, "Destroy ducking handle. handle(%p)", ducking_handle);
72         int ret = sound_manager_destroy_stream_ducking(ducking_handle);
73         SLOG(LOG_WARN, TAG_VCM, "Destroy ducking handle DONE. ret(%d/%s)", ret, get_error_message(ret));
74 }
75
76 static bool is_all_ducking_handle_valid()
77 {
78         if (nullptr == g_media_stream_h || nullptr == g_system_stream_h || nullptr == g_notification_stream_h ||
79                         nullptr == g_alarm_stream_h || nullptr == g_voice_information_stream_h) {
80                 return false;
81         }
82
83         return true;
84 }
85
86 static void destroy_all_ducking_handle()
87 {
88         destroy_ducking_handle(g_media_stream_h);
89         destroy_ducking_handle(g_system_stream_h);
90         destroy_ducking_handle(g_notification_stream_h);
91         destroy_ducking_handle(g_alarm_stream_h);
92         destroy_ducking_handle(g_voice_information_stream_h);
93
94         g_media_stream_h = nullptr;
95         g_system_stream_h = nullptr;
96         g_notification_stream_h = nullptr;
97         g_alarm_stream_h = nullptr;
98         g_voice_information_stream_h = nullptr;
99 }
100
101 int vc_mgr_ducking_create(void)
102 {
103         if (g_is_created) {
104                 SLOG(LOG_INFO, TAG_VCM, "All ducking handle is already created");
105                 return VC_ERROR_NONE;
106         }
107
108         g_media_stream_h = create_ducking_handle(SOUND_STREAM_TYPE_MEDIA);
109         g_system_stream_h = create_ducking_handle(SOUND_STREAM_TYPE_SYSTEM);
110         g_notification_stream_h = create_ducking_handle(SOUND_STREAM_TYPE_NOTIFICATION);
111         g_alarm_stream_h = create_ducking_handle(SOUND_STREAM_TYPE_ALARM);
112         g_voice_information_stream_h = create_ducking_handle(SOUND_STREAM_TYPE_VOICE_INFORMATION);
113
114         if (false == is_all_ducking_handle_valid()) {
115                 destroy_all_ducking_handle();
116                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to create ducking handles");
117                 return VC_ERROR_OPERATION_FAILED;
118         }
119
120         g_is_created = true;
121         return VC_ERROR_NONE;
122 }
123
124 int vc_mgr_ducking_destroy(void)
125 {
126         destroy_all_ducking_handle();
127
128         g_is_created = false;
129         return VC_ERROR_NONE;
130 }
131
132 static int activate_ducking_sound_stream(sound_stream_type_e type, sound_stream_ducking_h header, double ratio)
133 {
134         bool is_ducked = false;
135         int ret = SOUND_MANAGER_ERROR_NONE;
136         ret = sound_manager_is_ducked(header, &is_ducked);
137         if (is_ducked) {
138                 SLOG(LOG_DEBUG, TAG_VCM, "The %s is already ducked", get_ducking_stream(type));
139                 return ret;
140         }
141
142         ret = sound_manager_activate_ducking(header, SND_MGR_DUCKING_DURATION, ratio);
143         if (SOUND_MANAGER_ERROR_NONE != ret)
144                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to activate ducking for %s", get_ducking_stream(type));
145         else
146                 SLOG(LOG_INFO, TAG_VCM, "Activate ducking for %s", get_ducking_stream(type));
147
148         return ret;
149 }
150
151 int vc_mgr_ducking_activate(double ratio)
152 {
153         SLOG(LOG_INFO, TAG_VCM, "vc_mgr_ducking_activate ratio(%lf)", ratio);
154
155         int ret = VC_ERROR_NONE;
156         ret = activate_ducking_sound_stream(SOUND_STREAM_TYPE_MEDIA, g_media_stream_h, ratio);
157         if (SOUND_MANAGER_ERROR_NONE != ret)
158                 return ret;
159
160         ret = activate_ducking_sound_stream(SOUND_STREAM_TYPE_SYSTEM, g_system_stream_h, ratio);
161         if (SOUND_MANAGER_ERROR_NONE != ret)
162                 return ret;
163
164         ret = activate_ducking_sound_stream(SOUND_STREAM_TYPE_NOTIFICATION, g_notification_stream_h, ratio);
165         if (SOUND_MANAGER_ERROR_NONE != ret)
166                 return ret;
167
168         ret = activate_ducking_sound_stream(SOUND_STREAM_TYPE_ALARM, g_alarm_stream_h, ratio);
169         if (SOUND_MANAGER_ERROR_NONE != ret)
170                 return ret;
171
172         ret = activate_ducking_sound_stream(SOUND_STREAM_TYPE_VOICE_INFORMATION, g_voice_information_stream_h, ratio);
173         return ret;
174 }
175
176 static int deactivate_ducking_sound_stream(sound_stream_type_e type, sound_stream_ducking_h header)
177 {
178         bool is_ducked = false;
179         int ret = SOUND_MANAGER_ERROR_NONE;
180         ret = sound_manager_is_ducked(header, &is_ducked);
181         if (false == is_ducked) {
182                 SLOG(LOG_DEBUG, TAG_VCM, "The %s is already recovered from ducking", get_ducking_stream(type));
183                 return ret;
184         }
185
186         ret = sound_manager_deactivate_ducking(header);
187         if (SOUND_MANAGER_ERROR_NONE != ret)
188                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to deactivate ducking for %s", get_ducking_stream(type));
189         else
190                 SLOG(LOG_INFO, TAG_VCM, "Deactivate ducking for %s", get_ducking_stream(type));
191
192         return ret;
193 }
194
195 int vc_mgr_ducking_deactivate(void)
196 {
197         SLOG(LOG_INFO, TAG_VCM, "vc_mgr_ducking_deactivate");
198
199         int ret = VC_ERROR_NONE;
200         ret = deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_MEDIA, g_media_stream_h);
201         if (SOUND_MANAGER_ERROR_NONE != ret)
202                 return ret;
203
204         ret = deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_SYSTEM, g_system_stream_h);
205         if (SOUND_MANAGER_ERROR_NONE != ret)
206                 return ret;
207
208         ret = deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_NOTIFICATION, g_notification_stream_h);
209         if (SOUND_MANAGER_ERROR_NONE != ret)
210                 return ret;
211
212         ret = deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_ALARM, g_alarm_stream_h);
213         if (SOUND_MANAGER_ERROR_NONE != ret)
214                 return ret;
215
216         ret = deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_VOICE_INFORMATION, g_voice_information_stream_h);
217         return ret;
218 }