1 /*
2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
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 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "multimedia_audio_volume_group_manager_impl.h"
17 #include "cj_lambda.h"
18 #include "audio_policy_log.h"
19 #include "multimedia_audio_common.h"
20 #include "multimedia_audio_error.h"
21
22 namespace OHOS {
23 namespace AudioStandard {
24 extern "C" {
MMAAudioVolumeGroupManagerImpl(int32_t groupId)25 MMAAudioVolumeGroupManagerImpl::MMAAudioVolumeGroupManagerImpl(int32_t groupId)
26 {
27 audioMngr_ = AudioSystemManager::GetInstance();
28 audioGroupMngr_ = audioMngr_->GetGroupManager(groupId);
29 audioRingerModeCallback_ = std::make_shared<CjAudioRingerModeCallback>();
30 micStateChangeCallback_ = std::make_shared<CjAudioManagerMicStateChangeCallback>();
31 cachedClientId_ = getpid();
32 }
GetMaxVolume(int32_t volumeType)33 int32_t MMAAudioVolumeGroupManagerImpl::GetMaxVolume(int32_t volumeType)
34 {
35 if (audioGroupMngr_ == nullptr) {
36 AUDIO_ERR_LOG("invalid audio group manager instance");
37 return CJ_ERR_INVALID_VALUE;
38 }
39 auto ret = audioGroupMngr_->GetMaxVolume(static_cast<AudioVolumeType>(volumeType));
40 return ret;
41 }
42
GetMinVolume(int32_t volumeType)43 int32_t MMAAudioVolumeGroupManagerImpl::GetMinVolume(int32_t volumeType)
44 {
45 if (audioGroupMngr_ == nullptr) {
46 AUDIO_ERR_LOG("invalid audio group manager instance");
47 return CJ_ERR_INVALID_VALUE;
48 }
49 auto ret = audioGroupMngr_->GetMinVolume(static_cast<AudioVolumeType>(volumeType));
50 return ret;
51 }
52
GetRingerMode() const53 int32_t MMAAudioVolumeGroupManagerImpl::GetRingerMode() const
54 {
55 if (audioGroupMngr_ == nullptr) {
56 AUDIO_ERR_LOG("invalid audio group manager instance");
57 return CJ_ERR_INVALID_VALUE;
58 }
59 auto ret = audioGroupMngr_->GetRingerMode();
60 return static_cast<int32_t>(ret);
61 }
62
GetSystemVolumeInDb(int32_t volumeType,int32_t volumeLevel,int32_t deviceType)63 float MMAAudioVolumeGroupManagerImpl::GetSystemVolumeInDb(int32_t volumeType, int32_t volumeLevel, int32_t deviceType)
64 {
65 if (audioGroupMngr_ == nullptr) {
66 AUDIO_ERR_LOG("invalid audio group manager instance");
67 return CJ_ERR_INVALID_RETURN_FLOAT_VALUE;
68 }
69 auto ret = audioGroupMngr_->GetSystemVolumeInDb(static_cast<AudioVolumeType>(volumeType), volumeLevel,
70 static_cast<DeviceType>(deviceType));
71 return ret;
72 }
73
GetVolume(int32_t volumeType)74 int32_t MMAAudioVolumeGroupManagerImpl::GetVolume(int32_t volumeType)
75 {
76 if (audioGroupMngr_ == nullptr) {
77 AUDIO_ERR_LOG("invalid audio group manager instance");
78 return CJ_ERR_INVALID_VALUE;
79 }
80 auto ret = audioGroupMngr_->GetVolume(static_cast<AudioVolumeType>(volumeType));
81 return ret;
82 }
83
IsMicrophoneMute()84 bool MMAAudioVolumeGroupManagerImpl::IsMicrophoneMute()
85 {
86 if (audioGroupMngr_ == nullptr) {
87 AUDIO_ERR_LOG("invalid audio group manager instance");
88 return false;
89 }
90 auto ret = audioGroupMngr_->IsMicrophoneMute();
91 return ret;
92 }
93
IsMute(int32_t volumeType)94 bool MMAAudioVolumeGroupManagerImpl::IsMute(int32_t volumeType)
95 {
96 bool isMute{ false };
97 if (audioGroupMngr_ == nullptr) {
98 AUDIO_ERR_LOG("invalid audio group manager instance");
99 return isMute;
100 }
101 auto ret = audioGroupMngr_->IsStreamMute(static_cast<AudioVolumeType>(volumeType), isMute);
102 if (ret != NATIVE_SUCCESS) {
103 AUDIO_ERR_LOG("failed to get mute status.");
104 }
105 return isMute;
106 }
107
IsVolumeUnadjustable()108 bool MMAAudioVolumeGroupManagerImpl::IsVolumeUnadjustable()
109 {
110 if (audioGroupMngr_ == nullptr) {
111 AUDIO_ERR_LOG("invalid audio group manager instance");
112 return false;
113 }
114 auto ret = audioGroupMngr_->IsVolumeUnadjustable();
115 return ret;
116 }
117
GetMaxAmplitudeForOutputDevice(const int32_t deviceId)118 float MMAAudioVolumeGroupManagerImpl::GetMaxAmplitudeForOutputDevice(const int32_t deviceId)
119 {
120 if (audioGroupMngr_ == nullptr) {
121 AUDIO_ERR_LOG("invalid audio group manager instance");
122 return CJ_ERR_INVALID_RETURN_FLOAT_VALUE;
123 }
124 auto ret = audioGroupMngr_->GetMaxAmplitude(deviceId);
125 if (ret < 0) {
126 AUDIO_ERR_LOG("failed to get MaxAmplitude.");
127 }
128 return ret;
129 }
130
GetMaxAmplitudeForInputDevice(const int32_t deviceId)131 float MMAAudioVolumeGroupManagerImpl::GetMaxAmplitudeForInputDevice(const int32_t deviceId)
132 {
133 if (audioGroupMngr_ == nullptr) {
134 AUDIO_ERR_LOG("invalid audio group manager instance");
135 return CJ_ERR_INVALID_RETURN_FLOAT_VALUE;
136 }
137 auto ret = audioGroupMngr_->GetMaxAmplitude(deviceId);
138 if (ret < 0) {
139 AUDIO_ERR_LOG("failed to get MaxAmplitude.");
140 }
141 return ret;
142 }
143
RegisterCallback(int32_t callbackType,void (* callback)(),int32_t * errorCode)144 void MMAAudioVolumeGroupManagerImpl::RegisterCallback(int32_t callbackType, void (*callback)(), int32_t *errorCode)
145 {
146 if (errorCode == nullptr) {
147 AUDIO_ERR_LOG("invalid pointer");
148 return;
149 }
150 if (audioGroupMngr_ == nullptr) {
151 AUDIO_ERR_LOG("invalid audio group manager instance");
152 *errorCode = CJ_ERR_SYSTEM;
153 return;
154 }
155 switch (callbackType) {
156 case AudioVolumeGroupManagerCallbackType::RING_MODE_CHANGE: {
157 auto func = CJLambda::Create(reinterpret_cast<void (*)(int32_t)>(callback));
158 if (func == nullptr) {
159 AUDIO_ERR_LOG("Register RING_MODE_CHANGE event failure!");
160 *errorCode = CJ_ERR_SYSTEM;
161 return;
162 }
163 audioRingerModeCallback_->RegisterFunc(func);
164 audioGroupMngr_->SetRingerModeCallback(cachedClientId_, audioRingerModeCallback_);
165 break;
166 }
167 case AudioVolumeGroupManagerCallbackType::MICSTATE_CHANGE: {
168 auto func = CJLambda::Create(reinterpret_cast<void (*)(CMicStateChangeEvent)>(callback));
169 if (func == nullptr) {
170 AUDIO_ERR_LOG("Register MICSTATE_CHANGE event failure!");
171 *errorCode = CJ_ERR_SYSTEM;
172 return;
173 }
174 micStateChangeCallback_->RegisterFunc(func);
175 audioGroupMngr_->SetMicStateChangeCallback(micStateChangeCallback_);
176 break;
177 }
178 default:
179 AUDIO_ERR_LOG("No such callback supported");
180 }
181 }
182 }
183 } // namespace AudioStandard
184 } // namespace OHOS
185