2 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <sound_manager.h>
21 #include "vc_mgr_ducking.h"
23 #define SND_MGR_DUCKING_DURATION 500
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;
32 static bool g_is_created = false;
35 static const char *get_ducking_stream(sound_stream_type_e stream_type)
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";
48 return "Non matched stream";
51 static sound_stream_ducking_h create_ducking_handle(sound_stream_type_e type)
53 sound_stream_ducking_h ducking_handle = nullptr;
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));
61 SLOG(LOG_INFO, TAG_VCM, "Create ducking handle. type(%d) handle(%p)", type, ducking_handle);
62 return ducking_handle;
65 static void destroy_ducking_handle(sound_stream_ducking_h ducking_handle)
67 if (nullptr == ducking_handle) {
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));
76 static bool is_all_ducking_handle_valid()
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) {
86 static void destroy_all_ducking_handle()
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);
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;
101 int vc_mgr_ducking_create(void)
104 SLOG(LOG_INFO, TAG_VCM, "All ducking handle is already created");
105 return VC_ERROR_NONE;
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);
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;
121 return VC_ERROR_NONE;
124 int vc_mgr_ducking_destroy(void)
126 destroy_all_ducking_handle();
128 g_is_created = false;
129 return VC_ERROR_NONE;
132 static int activate_ducking_sound_stream(sound_stream_type_e type, sound_stream_ducking_h header, double ratio)
134 bool is_ducked = false;
135 int ret = SOUND_MANAGER_ERROR_NONE;
136 ret = sound_manager_is_ducked(header, &is_ducked);
138 SLOG(LOG_DEBUG, TAG_VCM, "The %s is already ducked", get_ducking_stream(type));
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));
146 SLOG(LOG_INFO, TAG_VCM, "Activate ducking for %s", get_ducking_stream(type));
151 int vc_mgr_ducking_activate(double ratio)
153 SLOG(LOG_INFO, TAG_VCM, "vc_mgr_ducking_activate ratio(%lf)", ratio);
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)
160 ret = activate_ducking_sound_stream(SOUND_STREAM_TYPE_SYSTEM, g_system_stream_h, ratio);
161 if (SOUND_MANAGER_ERROR_NONE != ret)
164 ret = activate_ducking_sound_stream(SOUND_STREAM_TYPE_NOTIFICATION, g_notification_stream_h, ratio);
165 if (SOUND_MANAGER_ERROR_NONE != ret)
168 ret = activate_ducking_sound_stream(SOUND_STREAM_TYPE_ALARM, g_alarm_stream_h, ratio);
169 if (SOUND_MANAGER_ERROR_NONE != ret)
172 ret = activate_ducking_sound_stream(SOUND_STREAM_TYPE_VOICE_INFORMATION, g_voice_information_stream_h, ratio);
176 static int deactivate_ducking_sound_stream(sound_stream_type_e type, sound_stream_ducking_h header)
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));
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));
190 SLOG(LOG_INFO, TAG_VCM, "Deactivate ducking for %s", get_ducking_stream(type));
195 int vc_mgr_ducking_deactivate(void)
197 SLOG(LOG_INFO, TAG_VCM, "vc_mgr_ducking_deactivate");
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)
204 ret = deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_SYSTEM, g_system_stream_h);
205 if (SOUND_MANAGER_ERROR_NONE != ret)
208 ret = deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_NOTIFICATION, g_notification_stream_h);
209 if (SOUND_MANAGER_ERROR_NONE != ret)
212 ret = deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_ALARM, g_alarm_stream_h);
213 if (SOUND_MANAGER_ERROR_NONE != ret)
216 ret = deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_VOICE_INFORMATION, g_voice_information_stream_h);