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 if (policyVolumeMap_->GetSize() != mapSize) {
100 AUDIO_ERR_LOG("InitSharedVolume get error size:%{public}zu, target:%{public}zu", policyVolumeMap_->GetSize(),
101 mapSize);
102 return false;
103 }
104 volumeVector_ = reinterpret_cast<Volume *>(policyVolumeMap_->GetBase());
105 AUDIO_INFO_LOG("InitSharedVolume success.");
106 return true;
107 }
108
GetSharedVolume(AudioStreamType streamType,DeviceType deviceType,Volume & vol)109 bool PolicyHandler::GetSharedVolume(AudioStreamType streamType, DeviceType deviceType, Volume &vol)
110 {
111 CHECK_AND_RETURN_RET_LOG((iPolicyProvider_ != nullptr && volumeVector_ != nullptr), false,
112 "GetSharedVolume failed not configed");
113 size_t index = 0;
114 if (!IPolicyProvider::GetVolumeIndex(streamType, deviceType, index) ||
115 index >= IPolicyProvider::GetVolumeVectorSize()) {
116 return false;
117 }
118 vol.isMute = volumeVector_[index].isMute;
119 vol.volumeFloat = volumeVector_[index].volumeFloat;
120 vol.volumeInt = volumeVector_[index].volumeInt;
121 return true;
122 }
123
SetActiveOutputDevice(DeviceType deviceType)124 void PolicyHandler::SetActiveOutputDevice(DeviceType deviceType)
125 {
126 AUDIO_INFO_LOG("SetActiveOutputDevice to device[%{public}d].", deviceType);
127 deviceType_ = deviceType;
128 }
129
GetActiveOutPutDevice()130 DeviceType PolicyHandler::GetActiveOutPutDevice()
131 {
132 return deviceType_;
133 }
134 } // namespace AudioStandard
135 } // namespace OHOS
136