1 /*
2 * Copyright (c) 2023 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 "policy_handler.h"
17
18 #include <iomanip>
19 #include <thread>
20
21 #include "audio_errors.h"
22 #include "audio_log.h"
23
24 namespace OHOS {
25 namespace AudioStandard {
GetInstance()26 PolicyHandler& PolicyHandler::GetInstance()
27 {
28 static PolicyHandler PolicyHandler;
29
30 return PolicyHandler;
31 }
32
PolicyHandler()33 PolicyHandler::PolicyHandler()
34 {
35 AUDIO_INFO_LOG("PolicyHandler()");
36 }
37
~PolicyHandler()38 PolicyHandler::~PolicyHandler()
39 {
40 volumeVector_ = nullptr;
41 policyVolumeMap_ = nullptr;
42 iPolicyProvider_ = nullptr;
43 AUDIO_INFO_LOG("~PolicyHandler()");
44 }
45
Dump(std::stringstream & dumpStringStream)46 void PolicyHandler::Dump(std::stringstream &dumpStringStream)
47 {
48 AUDIO_INFO_LOG("PolicyHandler dump begin");
49 if (iPolicyProvider_ == nullptr || policyVolumeMap_ == nullptr || volumeVector_ == nullptr) {
50 dumpStringStream << "PolicyHandler is null..." << std::endl;
51 AUDIO_INFO_LOG("nothing to dump");
52 return;
53 }
54 dumpStringStream << std::endl;
55 // dump active output device
56 dumpStringStream << "active output device:[" << deviceType_ << "]" << std::endl;
57 // dump volume
58 int formatSize = 2;
59 for (size_t i = 0; i < IPolicyProvider::GetVolumeVectorSize(); i++) {
60 dumpStringStream << "streamtype[" << g_volumeIndexVector[i].first << "] ";
61 dumpStringStream << "device[" << std::setw(formatSize) << g_volumeIndexVector[i].second << "]: ";
62 dumpStringStream << "isMute[" << (volumeVector_[i].isMute ? "true" : "false") << "] ";
63 dumpStringStream << "volFloat[" << volumeVector_[i].volumeFloat << "] ";
64 dumpStringStream << "volint[" << volumeVector_[i].volumeInt << "] ";
65 dumpStringStream << std::endl;
66 }
67 }
68
ConfigPolicyProvider(const sptr<IPolicyProviderIpc> policyProvider)69 bool PolicyHandler::ConfigPolicyProvider(const sptr<IPolicyProviderIpc> policyProvider)
70 {
71 CHECK_AND_RETURN_RET_LOG(policyProvider != nullptr, false, "ConfigPolicyProvider failed with null provider.");
72 if (iPolicyProvider_ == nullptr) {
73 iPolicyProvider_ = policyProvider;
74 } else {
75 AUDIO_ERR_LOG("Provider is already configed!");
76 return false;
77 }
78 bool ret = InitVolumeMap();
79 AUDIO_INFO_LOG("ConfigPolicyProvider end and InitVolumeMap %{public}s", (ret ? "SUCCESS" : "FAILED"));
80 return ret;
81 }
82
GetProcessDeviceInfo(const AudioProcessConfig & config,DeviceInfo & deviceInfo)83 bool PolicyHandler::GetProcessDeviceInfo(const AudioProcessConfig &config, DeviceInfo &deviceInfo)
84 {
85 // send the config to AudioPolicyServer and get the device info.
86 CHECK_AND_RETURN_RET_LOG(iPolicyProvider_ != nullptr, false, "GetProcessDeviceInfo failed with null provider.");
87 int32_t ret = iPolicyProvider_->GetProcessDeviceInfo(config, deviceInfo);
88 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, false, "GetProcessDeviceInfo failed:%{public}d", ret);
89 return true;
90 }
91
InitVolumeMap()92 bool PolicyHandler::InitVolumeMap()
93 {
94 CHECK_AND_RETURN_RET_LOG(iPolicyProvider_ != nullptr, false, "InitVolumeMap failed with null provider.");
95 iPolicyProvider_->InitSharedVolume(policyVolumeMap_);
96 CHECK_AND_RETURN_RET_LOG((policyVolumeMap_ != nullptr && policyVolumeMap_->GetBase() != nullptr), false,
97 "InitSharedVolume failed.");
98 size_t mapSize = IPolicyProvider::GetVolumeVectorSize() * sizeof(Volume);
99 CHECK_AND_RETURN_RET_LOG(policyVolumeMap_->GetSize() == mapSize, false,
100 "InitSharedVolume get error size:%{public}zu, target:%{public}zu", policyVolumeMap_->GetSize(), mapSize);
101 volumeVector_ = reinterpret_cast<Volume *>(policyVolumeMap_->GetBase());
102 AUDIO_INFO_LOG("InitSharedVolume success.");
103 return true;
104 }
105
GetVolumeTypeFromStreamType(AudioStreamType streamType)106 AudioVolumeType PolicyHandler::GetVolumeTypeFromStreamType(AudioStreamType streamType)
107 {
108 switch (streamType) {
109 case STREAM_VOICE_CALL:
110 case STREAM_VOICE_MESSAGE:
111 return STREAM_VOICE_CALL;
112 case STREAM_RING:
113 case STREAM_SYSTEM:
114 case STREAM_NOTIFICATION:
115 case STREAM_SYSTEM_ENFORCED:
116 case STREAM_DTMF:
117 return STREAM_RING;
118 case STREAM_MUSIC:
119 case STREAM_MEDIA:
120 case STREAM_MOVIE:
121 case STREAM_GAME:
122 case STREAM_SPEECH:
123 case STREAM_NAVIGATION:
124 return STREAM_MUSIC;
125 case STREAM_VOICE_ASSISTANT:
126 return STREAM_VOICE_ASSISTANT;
127 case STREAM_ALARM:
128 return STREAM_ALARM;
129 case STREAM_ACCESSIBILITY:
130 return STREAM_ACCESSIBILITY;
131 case STREAM_ULTRASONIC:
132 return STREAM_ULTRASONIC;
133 case STREAM_ALL:
134 return STREAM_ALL;
135 default:
136 return STREAM_MUSIC;
137 }
138 }
139
GetSharedVolume(AudioVolumeType streamType,DeviceType deviceType,Volume & vol)140 bool PolicyHandler::GetSharedVolume(AudioVolumeType streamType, DeviceType deviceType, Volume &vol)
141 {
142 CHECK_AND_RETURN_RET_LOG((iPolicyProvider_ != nullptr && volumeVector_ != nullptr), false,
143 "GetSharedVolume failed not configed");
144 size_t index = 0;
145 if (!IPolicyProvider::GetVolumeIndex(streamType, deviceType, index) ||
146 index >= IPolicyProvider::GetVolumeVectorSize()) {
147 return false;
148 }
149 vol.isMute = volumeVector_[index].isMute;
150 vol.volumeFloat = volumeVector_[index].volumeFloat;
151 vol.volumeInt = volumeVector_[index].volumeInt;
152 return true;
153 }
154
SetActiveOutputDevice(DeviceType deviceType)155 void PolicyHandler::SetActiveOutputDevice(DeviceType deviceType)
156 {
157 AUDIO_INFO_LOG("SetActiveOutputDevice to device[%{public}d].", deviceType);
158 deviceType_ = deviceType;
159 }
160
GetActiveOutPutDevice()161 DeviceType PolicyHandler::GetActiveOutPutDevice()
162 {
163 return deviceType_;
164 }
165
SetWakeUpAudioCapturerFromAudioServer()166 int32_t PolicyHandler::SetWakeUpAudioCapturerFromAudioServer()
167 {
168 CHECK_AND_RETURN_RET_LOG(iPolicyProvider_ != nullptr, ERROR, "iPolicyProvider_ is nullptr");
169 int32_t ret = iPolicyProvider_->SetWakeUpAudioCapturerFromAudioServer();
170 return ret;
171 }
172
NotifyCapturerAdded(AudioCapturerInfo capturerInfo,AudioStreamInfo streamInfo,uint32_t sessionId)173 int32_t PolicyHandler::NotifyCapturerAdded(AudioCapturerInfo capturerInfo, AudioStreamInfo streamInfo,
174 uint32_t sessionId)
175 {
176 CHECK_AND_RETURN_RET_LOG(iPolicyProvider_ != nullptr, ERROR, "iPolicyProvider_ is nullptr");
177 return iPolicyProvider_->NotifyCapturerAdded(capturerInfo, streamInfo, sessionId);
178 }
179
NotifyWakeUpCapturerRemoved()180 int32_t PolicyHandler::NotifyWakeUpCapturerRemoved()
181 {
182 CHECK_AND_RETURN_RET_LOG(iPolicyProvider_ != nullptr, ERROR, "iPolicyProvider_ is nullptr");
183 return iPolicyProvider_->NotifyWakeUpCapturerRemoved();
184 }
185 } // namespace AudioStandard
186 } // namespace OHOS
187