• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef LOG_TAG
16 #define LOG_TAG "AudioA2dpDevice"
17 #endif
18 
19 #include "audio_a2dp_device.h"
20 #include "parameter.h"
21 #include "parameters.h"
22 #include "audio_policy_log.h"
23 #include "audio_policy_manager_factory.h"
24 
25 #include "audio_policy_utils.h"
26 #include "audio_policy_service.h"
27 
28 namespace OHOS {
29 namespace AudioStandard {
30 using namespace std;
31 
GetEncryptAddr(const std::string & addr)32 static std::string GetEncryptAddr(const std::string &addr)
33 {
34     const int32_t START_POS = 6;
35     const int32_t END_POS = 13;
36     const int32_t ADDRESS_STR_LEN = 17;
37     if (addr.empty() || addr.length() != ADDRESS_STR_LEN) {
38         return std::string("");
39     }
40     std::string tmp = "**:**:**:**:**:**";
41     std::string out = addr;
42     for (int i = START_POS; i <= END_POS; i++) {
43         out[i] = tmp[i];
44     }
45     return out;
46 }
47 
GetA2dpDeviceInfo(const std::string & device,A2dpDeviceConfigInfo & info)48 bool AudioA2dpDevice::GetA2dpDeviceInfo(const std::string& device, A2dpDeviceConfigInfo& info)
49 {
50     std::lock_guard<std::mutex> lock(a2dpDeviceMapMutex_);
51     auto configInfoPos = connectedA2dpDeviceMap_.find(device);
52     if (configInfoPos != connectedA2dpDeviceMap_.end()) {
53         info.streamInfo = configInfoPos->second.streamInfo;
54         info.absVolumeSupport = configInfoPos->second.absVolumeSupport;
55         info.volumeLevel = configInfoPos->second.volumeLevel;
56         info.mute = configInfoPos->second.mute;
57         return true;
58     }
59     return false;
60 }
61 
GetA2dpInDeviceInfo(const std::string & device,A2dpDeviceConfigInfo & info)62 bool AudioA2dpDevice::GetA2dpInDeviceInfo(const std::string& device, A2dpDeviceConfigInfo& info)
63 {
64     std::lock_guard<std::mutex> lock(a2dpInDeviceMapMutex_);
65     auto configInfoPos = connectedA2dpInDeviceMap_.find(device);
66     if (configInfoPos != connectedA2dpInDeviceMap_.end()) {
67         info.streamInfo = configInfoPos->second.streamInfo;
68         info.absVolumeSupport = configInfoPos->second.absVolumeSupport;
69         info.volumeLevel = configInfoPos->second.volumeLevel;
70         info.mute = configInfoPos->second.mute;
71         return true;
72     }
73     return false;
74 }
75 
GetA2dpDeviceVolumeLevel(const std::string & device,int32_t & volumeLevel)76 bool AudioA2dpDevice::GetA2dpDeviceVolumeLevel(const std::string& device, int32_t& volumeLevel)
77 {
78     std::lock_guard<std::mutex> lock(a2dpDeviceMapMutex_);
79     auto configInfoPos = connectedA2dpDeviceMap_.find(device);
80     if (configInfoPos != connectedA2dpDeviceMap_.end()) {
81         volumeLevel = configInfoPos->second.volumeLevel;
82         return true;
83     }
84     return false;
85 }
86 
CheckA2dpDeviceExist(const std::string & device)87 bool AudioA2dpDevice::CheckA2dpDeviceExist(const std::string& device)
88 {
89     std::lock_guard<std::mutex> lock(a2dpDeviceMapMutex_);
90     auto configInfoPos = connectedA2dpDeviceMap_.find(device);
91     if (configInfoPos != connectedA2dpDeviceMap_.end()) {
92         return true;
93     }
94     return false;
95 }
96 
SetA2dpDeviceMute(const std::string & device,bool mute)97 bool AudioA2dpDevice::SetA2dpDeviceMute(const std::string& device, bool mute)
98 {
99     std::lock_guard<std::mutex> lock(a2dpDeviceMapMutex_);
100     auto configInfoPos = connectedA2dpDeviceMap_.find(device);
101     if (configInfoPos == connectedA2dpDeviceMap_.end() || !configInfoPos->second.absVolumeSupport) {
102         AUDIO_WARNING_LOG("Set Mute failed for macAddress:[%{public}s]", GetEncryptAddr(device).c_str());
103         return false;
104     }
105     configInfoPos->second.mute = mute;
106     return true;
107 }
108 
GetA2dpDeviceMute(const std::string & device,bool & isMute)109 bool AudioA2dpDevice::GetA2dpDeviceMute(const std::string& device, bool& isMute)
110 {
111     std::lock_guard<std::mutex> lock(a2dpDeviceMapMutex_);
112     auto configInfoPos = connectedA2dpDeviceMap_.find(device);
113     if (configInfoPos == connectedA2dpDeviceMap_.end() || !configInfoPos->second.absVolumeSupport) {
114         return false;
115     }
116     isMute = configInfoPos->second.mute;
117     return true;
118 }
119 
SetA2dpDeviceStreamInfo(const std::string & device,const DeviceStreamInfo & streamInfo)120 void AudioA2dpDevice::SetA2dpDeviceStreamInfo(const std::string& device, const DeviceStreamInfo& streamInfo)
121 {
122     std::lock_guard<std::mutex> lock(a2dpDeviceMapMutex_);
123     connectedA2dpDeviceMap_[device].streamInfo = streamInfo;
124 }
125 
AddA2dpDevice(const std::string & device,const A2dpDeviceConfigInfo & config)126 void AudioA2dpDevice::AddA2dpDevice(const std::string& device, const A2dpDeviceConfigInfo& config)
127 {
128     std::lock_guard<std::mutex> lock(a2dpDeviceMapMutex_);
129     connectedA2dpDeviceMap_[device] = config;
130 }
131 
AddA2dpInDevice(const std::string & device,const A2dpDeviceConfigInfo & config)132 void AudioA2dpDevice::AddA2dpInDevice(const std::string& device, const A2dpDeviceConfigInfo& config)
133 {
134     std::lock_guard<std::mutex> lock(a2dpInDeviceMapMutex_);
135     connectedA2dpInDeviceMap_[device] = config;
136 }
137 
DelA2dpInDevice(const std::string & device)138 size_t AudioA2dpDevice::DelA2dpInDevice(const std::string& device)
139 {
140     std::lock_guard<std::mutex> lock(a2dpInDeviceMapMutex_);
141     connectedA2dpInDeviceMap_.erase(device);
142     return connectedA2dpInDeviceMap_.size();
143 }
144 
DelA2dpDevice(const std::string & device)145 size_t AudioA2dpDevice::DelA2dpDevice(const std::string& device)
146 {
147     std::lock_guard<std::mutex> lock(a2dpDeviceMapMutex_);
148     connectedA2dpDeviceMap_.erase(device);
149     return connectedA2dpDeviceMap_.size();
150 }
151 
SetA2dpDeviceAbsVolumeSupport(const std::string & device,const bool support)152 bool AudioA2dpDevice::SetA2dpDeviceAbsVolumeSupport(const std::string& device, const bool support)
153 {
154     std::lock_guard<std::mutex> lock(a2dpDeviceMapMutex_);
155     auto configInfoPos = connectedA2dpDeviceMap_.find(device);
156     if (configInfoPos != connectedA2dpDeviceMap_.end()) {
157         configInfoPos->second.absVolumeSupport = support;
158         if (support && configInfoPos->second.volumeLevel == -1) {
159             configInfoPos->second.volumeLevel =
160                 AudioVolumeManager::GetInstance().GetSystemVolumeLevelNoMuteState(STREAM_MUSIC);
161             configInfoPos->second.mute = AudioVolumeManager::GetInstance().GetStreamMute(STREAM_MUSIC);
162         }
163         return true;
164     }
165     return false;
166 }
167 
SetA2dpDeviceVolumeLevel(const std::string & device,const int32_t volumeLevel)168 bool AudioA2dpDevice::SetA2dpDeviceVolumeLevel(const std::string& device, const int32_t volumeLevel)
169 {
170     std::lock_guard<std::mutex> lock(a2dpDeviceMapMutex_);
171     auto configInfoPos = connectedA2dpDeviceMap_.find(device);
172     if (configInfoPos == connectedA2dpDeviceMap_.end() || !configInfoPos->second.absVolumeSupport) {
173         AUDIO_WARNING_LOG("Set VolumeLevel failed for macAddress:[%{public}s]", GetEncryptAddr(device).c_str());
174         return false;
175     }
176     configInfoPos->second.volumeLevel = volumeLevel;
177     return true;
178 }
179 
180 }
181 }