1 /*
2 * Copyright (c) 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 "SleAudioDeviceManager"
17 #endif
18
19 #include "sle_audio_device_manager.h"
20
21 #include "audio_errors.h"
22 #include "audio_policy_log.h"
23 #include "audio_policy_utils.h"
24
25 namespace OHOS {
26 namespace AudioStandard {
27 namespace {
28 const std::map<uint32_t, std::set<StreamUsage>> STREAM_USAGE_TO_SLE_STREAM_TYPE = {
29 {SLE_AUDIO_STREAM_NONE, {}},
30 {SLE_AUDIO_STREAM_UNDEFINED, {STREAM_USAGE_ULTRASONIC, STREAM_USAGE_ENFORCED_TONE, STREAM_USAGE_DTMF,
31 STREAM_USAGE_ALARM, STREAM_USAGE_NOTIFICATION, STREAM_USAGE_SYSTEM,
32 STREAM_USAGE_ACCESSIBILITY, STREAM_USAGE_VOICE_MESSAGE}},
33 {SLE_AUDIO_STREAM_MUSIC, {STREAM_USAGE_UNKNOWN, STREAM_USAGE_MEDIA, STREAM_USAGE_MUSIC, STREAM_USAGE_AUDIOBOOK,
34 STREAM_USAGE_VOICE_ASSISTANT}},
35 {SLE_AUDIO_STREAM_VOICE_CALL, {STREAM_USAGE_VOICE_MODEM_COMMUNICATION}},
36 {SLE_AUDIO_STREAM_VOICE_ASSISTANT, {}},
37 {SLE_AUDIO_STREAM_RING, {STREAM_USAGE_NOTIFICATION_RINGTONE, STREAM_USAGE_RINGTONE, STREAM_USAGE_RANGING,
38 STREAM_USAGE_VOICE_RINGTONE}},
39 {SLE_AUDIO_STREAM_VOIP, {STREAM_USAGE_VOICE_COMMUNICATION, STREAM_USAGE_VIDEO_COMMUNICATION,
40 STREAM_USAGE_VOICE_CALL_ASSISTANT}},
41 {SLE_AUDIO_STREAM_GAME, {STREAM_USAGE_GAME}},
42 {SLE_AUDIO_STREAM_ALERT, {}},
43 {SLE_AUDIO_STREAM_VIDEO, {STREAM_USAGE_MOVIE}},
44 {SLE_AUDIO_STREAM_GUID, {STREAM_USAGE_NAVIGATION}}
45 };
46
47 const std::map<uint32_t, std::set<SourceType>> SOURCE_TYPE_TO_SLE_STREAM_TYPE = {
48 {SLE_AUDIO_STREAM_NONE, {}},
49 {SLE_AUDIO_STREAM_VOICE_CALL, {SOURCE_TYPE_VIRTUAL_CAPTURE, SOURCE_TYPE_VOICE_CALL}},
50 {SLE_AUDIO_STREAM_VOIP, {SOURCE_TYPE_VOICE_COMMUNICATION}},
51 {SLE_AUDIO_STREAM_VOICE_ASSISTANT, {SOURCE_TYPE_VOICE_RECOGNITION, SOURCE_TYPE_VOICE_TRANSCRIPTION}},
52 {SLE_AUDIO_STREAM_RECORD, {SOURCE_TYPE_MIC, SOURCE_TYPE_ULTRASONIC, SOURCE_TYPE_VOICE_MESSAGE,
53 SOURCE_TYPE_CAMCORDER, SOURCE_TYPE_UNPROCESSED, SOURCE_TYPE_LIVE}}
54 };
55 } // namespace
SetSleAudioOperationCallback(const sptr<IStandardSleAudioOperationCallback> & callback)56 int32_t SleAudioDeviceManager::SetSleAudioOperationCallback(const sptr<IStandardSleAudioOperationCallback> &callback)
57 {
58 CHECK_AND_RETURN_RET_LOG(callback != nullptr, ERR_INVALID_PARAM, "callback is nullptr");
59 callback_ = callback;
60 return SUCCESS;
61 }
62
GetSleAudioDeviceList(std::vector<AudioDeviceDescriptor> & devices)63 void SleAudioDeviceManager::GetSleAudioDeviceList(std::vector<AudioDeviceDescriptor> &devices)
64 {
65 CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback is nullptr");
66 callback_->GetSleAudioDeviceList(devices);
67 }
68
GetSleVirtualAudioDeviceList(std::vector<AudioDeviceDescriptor> & devices)69 void SleAudioDeviceManager::GetSleVirtualAudioDeviceList(std::vector<AudioDeviceDescriptor> &devices)
70 {
71 CHECK_AND_RETURN_LOG(callback_ != nullptr, "callback is nullptr");
72 callback_->GetSleVirtualAudioDeviceList(devices);
73 }
74
IsInBandRingOpen(const std::string & device) const75 bool SleAudioDeviceManager::IsInBandRingOpen(const std::string &device) const
76 {
77 CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, false, "callback is nullptr");
78 bool ret = false;
79 callback_->IsInBandRingOpen(device, ret);
80 return ret;
81 }
82
GetSupportStreamType(const std::string & device) const83 uint32_t SleAudioDeviceManager::GetSupportStreamType(const std::string &device) const
84 {
85 CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, ERR_INVALID_PARAM, "callback is nullptr");
86 uint32_t retType = static_cast<uint32_t>(ERROR);
87 callback_->GetSupportStreamType(device, retType);
88 return retType;
89 }
90
SetActiveSinkDevice(const std::string & device,uint32_t streamType)91 int32_t SleAudioDeviceManager::SetActiveSinkDevice(const std::string &device, uint32_t streamType)
92 {
93 CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, ERR_INVALID_PARAM, "callback is nullptr");
94 int32_t ret = ERROR;
95 callback_->SetActiveSinkDevice(device, streamType, ret);
96 return ret;
97 }
98
StartPlaying(const std::string & device,uint32_t streamType)99 int32_t SleAudioDeviceManager::StartPlaying(const std::string &device, uint32_t streamType)
100 {
101 CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, ERR_INVALID_PARAM, "callback is nullptr");
102
103 AUDIO_INFO_LOG("sle streamType %{public}u", streamType);
104 std::lock_guard<std::mutex> lock(startedSleStreamTypeMutex_);
105 int32_t ret = ERROR;
106 if (!startedSleStreamType_[device][streamType].empty()) {
107 AUDIO_INFO_LOG("sle stream type %{public}u is already started", streamType);
108 return SUCCESS;
109 }
110 callback_->StartPlaying(device, streamType, ret);
111 return ret;
112 }
113
StopPlaying(const std::string & device,uint32_t streamType)114 int32_t SleAudioDeviceManager::StopPlaying(const std::string &device, uint32_t streamType)
115 {
116 CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, ERR_INVALID_PARAM, "callback is nullptr");
117
118 int32_t ret = ERROR;
119 AUDIO_INFO_LOG("sle streamType %{public}u", streamType);
120 callback_->StopPlaying(device, streamType, ret);
121 return ret;
122 }
123
ConnectAllowedProfiles(const std::string & remoteAddr) const124 int32_t SleAudioDeviceManager::ConnectAllowedProfiles(const std::string &remoteAddr) const
125 {
126 CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, ERR_INVALID_PARAM, "callback is nullptr");
127 int32_t ret = ERROR;
128 callback_->ConnectAllowedProfiles(remoteAddr, ret);
129 return ret;
130 }
131
SetDeviceAbsVolume(const std::string & remoteAddr,uint32_t volume,uint32_t streamType)132 int32_t SleAudioDeviceManager::SetDeviceAbsVolume(const std::string &remoteAddr, uint32_t volume, uint32_t streamType)
133 {
134 CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, ERR_INVALID_PARAM, "callback is nullptr");
135 int32_t ret = ERROR;
136 callback_->SetDeviceAbsVolume(remoteAddr, volume, streamType, ret);
137 return ret;
138 }
139
SendUserSelection(const std::string & device,uint32_t streamType)140 int32_t SleAudioDeviceManager::SendUserSelection(const std::string &device, uint32_t streamType)
141 {
142 CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, ERR_INVALID_PARAM, "callback is nullptr");
143 int32_t ret = ERROR;
144 callback_->SendUserSelection(device, streamType, ret);
145 return ret;
146 }
147
GetRenderPosition(const std::string & device,uint32_t & delayValue)148 int32_t SleAudioDeviceManager::GetRenderPosition(const std::string &device, uint32_t &delayValue)
149 {
150 CHECK_AND_RETURN_RET_LOG(callback_ != nullptr, ERR_INVALID_PARAM, "callback is nullptr");
151 return callback_->GetRenderPosition(device, delayValue);
152 }
153
GetSleStreamTypeByStreamUsage(StreamUsage streamUsage) const154 uint32_t SleAudioDeviceManager::GetSleStreamTypeByStreamUsage(StreamUsage streamUsage) const
155 {
156 for (const auto &pair : STREAM_USAGE_TO_SLE_STREAM_TYPE) {
157 if (pair.second.find(streamUsage) != pair.second.end()) {
158 return pair.first;
159 }
160 }
161 return STREAM_USAGE_TO_SLE_STREAM_TYPE.begin()->first; // Default to NONE
162 }
163
GetSleStreamTypeBySourceType(SourceType sourceType) const164 uint32_t SleAudioDeviceManager::GetSleStreamTypeBySourceType(SourceType sourceType) const
165 {
166 for (const auto &pair : SOURCE_TYPE_TO_SLE_STREAM_TYPE) {
167 if (pair.second.find(sourceType) != pair.second.end()) {
168 return pair.first;
169 }
170 }
171 return SOURCE_TYPE_TO_SLE_STREAM_TYPE.begin()->first; // Default to NONE
172 }
173
GetStreamUsagesBySleStreamType(uint32_t streamType) const174 std::set<StreamUsage> SleAudioDeviceManager::GetStreamUsagesBySleStreamType(uint32_t streamType) const
175 {
176 if (STREAM_USAGE_TO_SLE_STREAM_TYPE.contains(streamType)) {
177 return STREAM_USAGE_TO_SLE_STREAM_TYPE.at(streamType);
178 }
179
180 return std::set<StreamUsage>();
181 }
182
GetSourceTypesBySleStreamType(uint32_t streamType) const183 std::set<SourceType> SleAudioDeviceManager::GetSourceTypesBySleStreamType(uint32_t streamType) const
184 {
185 if (SOURCE_TYPE_TO_SLE_STREAM_TYPE.contains(streamType)) {
186 return SOURCE_TYPE_TO_SLE_STREAM_TYPE.at(streamType);
187 }
188
189 return std::set<SourceType>();
190 }
191
SetActiveDevice(const AudioDeviceDescriptor & deviceDesc,StreamUsage streamUsage)192 int32_t SleAudioDeviceManager::SetActiveDevice(const AudioDeviceDescriptor &deviceDesc, StreamUsage streamUsage)
193 {
194 CHECK_AND_RETURN_RET_LOG(IsNearlinkDevice(deviceDesc.deviceType_), ERROR, "device type is not nearlink");
195 return SetActiveSinkDevice(deviceDesc.macAddress_, GetSleStreamTypeByStreamUsage(streamUsage));
196 }
197
SetActiveDevice(const AudioDeviceDescriptor & deviceDesc,SourceType sourceType)198 int32_t SleAudioDeviceManager::SetActiveDevice(const AudioDeviceDescriptor &deviceDesc, SourceType sourceType)
199 {
200 CHECK_AND_RETURN_RET_LOG(IsNearlinkDevice(deviceDesc.deviceType_), ERROR, "device type is not nearlink");
201 return SetActiveSinkDevice(deviceDesc.macAddress_, GetSleStreamTypeBySourceType(sourceType));
202 }
203
StartPlaying(const AudioDeviceDescriptor & deviceDesc,StreamUsage streamUsage)204 int32_t SleAudioDeviceManager::StartPlaying(const AudioDeviceDescriptor &deviceDesc, StreamUsage streamUsage)
205 {
206 CHECK_AND_RETURN_RET_LOG(deviceDesc.deviceType_ == DEVICE_TYPE_NEARLINK, ERROR, "device type is not nearlink");
207 return StartPlaying(deviceDesc.macAddress_, GetSleStreamTypeByStreamUsage(streamUsage));
208 }
209
StartPlaying(const AudioDeviceDescriptor & deviceDesc,SourceType sourceType)210 int32_t SleAudioDeviceManager::StartPlaying(const AudioDeviceDescriptor &deviceDesc, SourceType sourceType)
211 {
212 CHECK_AND_RETURN_RET_LOG(deviceDesc.deviceType_ == DEVICE_TYPE_NEARLINK_IN, ERROR, "device type is not nearlink");
213 return StartPlaying(deviceDesc.macAddress_, GetSleStreamTypeBySourceType(sourceType));
214 }
215
StopPlaying(const AudioDeviceDescriptor & deviceDesc,StreamUsage streamUsage)216 int32_t SleAudioDeviceManager::StopPlaying(const AudioDeviceDescriptor &deviceDesc, StreamUsage streamUsage)
217 {
218 CHECK_AND_RETURN_RET_LOG(deviceDesc.deviceType_ == DEVICE_TYPE_NEARLINK, ERROR, "device type is not nearlink");
219 return StopPlaying(deviceDesc.macAddress_, GetSleStreamTypeByStreamUsage(streamUsage));
220 }
221
StopPlaying(const AudioDeviceDescriptor & deviceDesc,SourceType sourceType)222 int32_t SleAudioDeviceManager::StopPlaying(const AudioDeviceDescriptor &deviceDesc, SourceType sourceType)
223 {
224 CHECK_AND_RETURN_RET_LOG(deviceDesc.deviceType_ == DEVICE_TYPE_NEARLINK_IN, ERROR, "device type is not nearlink");
225 return StopPlaying(deviceDesc.macAddress_, GetSleStreamTypeBySourceType(sourceType));
226 }
227
SetDeviceAbsVolume(const std::string & device,AudioStreamType streamType,int32_t volume)228 int32_t SleAudioDeviceManager::SetDeviceAbsVolume(const std::string &device, AudioStreamType streamType, int32_t volume)
229 {
230 CHECK_AND_RETURN_RET_LOG(volume >= 0, ERR_INVALID_PARAM, "volume is invalid");
231
232 auto it = deviceVolumeConfigInfo_.find(device);
233 CHECK_AND_RETURN_RET_LOG(it != deviceVolumeConfigInfo_.end(), ERR_INVALID_PARAM, "device not found");
234
235 int32_t ret = SUCCESS;
236 if (streamType == STREAM_MUSIC) {
237 ret = SetDeviceAbsVolume(device, static_cast<uint32_t>(volume), 0x00000002); // MEDIA
238 } else {
239 ret = SetDeviceAbsVolume(device, static_cast<uint32_t>(volume), 0x00000004); // VOICE_CALL
240 }
241 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "set device to nearlink failed");
242
243 return ret;
244 }
245
SendUserSelection(const AudioDeviceDescriptor & deviceDesc,StreamUsage streamUsage)246 int32_t SleAudioDeviceManager::SendUserSelection(const AudioDeviceDescriptor &deviceDesc, StreamUsage streamUsage)
247 {
248 CHECK_AND_RETURN_RET_LOG(deviceDesc.deviceType_ == DEVICE_TYPE_NEARLINK, ERROR, "device type is not nearlink");
249 return SendUserSelection(deviceDesc.macAddress_, GetSleStreamTypeByStreamUsage(streamUsage));
250 }
251
SendUserSelection(const AudioDeviceDescriptor & deviceDesc,SourceType sourceType)252 int32_t SleAudioDeviceManager::SendUserSelection(const AudioDeviceDescriptor &deviceDesc, SourceType sourceType)
253 {
254 CHECK_AND_RETURN_RET_LOG(deviceDesc.deviceType_ == DEVICE_TYPE_NEARLINK_IN, ERROR, "device type is not nearlink");
255 return SendUserSelection(deviceDesc.macAddress_, GetSleStreamTypeBySourceType(sourceType));
256 }
257
AddNearlinkDevice(const AudioDeviceDescriptor & deviceDesc)258 int32_t SleAudioDeviceManager::AddNearlinkDevice(const AudioDeviceDescriptor &deviceDesc)
259 {
260 CHECK_AND_RETURN_RET_LOG(deviceDesc.deviceType_ == DEVICE_TYPE_NEARLINK &&
261 deviceDesc.connectState_ == CONNECTED, ERROR, "device type is not nearlink");
262 std::lock_guard<std::mutex> lock(deviceVolumeConfigMutex_);
263 deviceVolumeConfigInfo_[deviceDesc.macAddress_] =
264 std::make_pair(SleVolumeConfigInfo{STREAM_MUSIC, deviceDesc.mediaVolume_},
265 SleVolumeConfigInfo{STREAM_VOICE_CALL, deviceDesc.callVolume_});
266 return SUCCESS;
267 }
268
RemoveNearlinkDevice(const AudioDeviceDescriptor & deviceDesc)269 int32_t SleAudioDeviceManager::RemoveNearlinkDevice(const AudioDeviceDescriptor &deviceDesc)
270 {
271 CHECK_AND_RETURN_RET_LOG(deviceDesc.deviceType_ == DEVICE_TYPE_NEARLINK &&
272 deviceDesc.connectState_ == CONNECTED, ERROR, "device type is not nearlink");
273 std::lock_guard<std::mutex> lock(deviceVolumeConfigMutex_);
274 deviceVolumeConfigInfo_.erase(deviceDesc.macAddress_);
275 startedSleStreamType_.erase(deviceDesc.macAddress_);
276 return SUCCESS;
277 }
278
IsNearlinkDevice(DeviceType deviceType)279 bool SleAudioDeviceManager::IsNearlinkDevice(DeviceType deviceType)
280 {
281 return deviceType == DEVICE_TYPE_NEARLINK || deviceType == DEVICE_TYPE_NEARLINK_IN;
282 }
283
IsMoveToNearlinkDevice(const std::shared_ptr<AudioStreamDescriptor> & streamDesc)284 bool SleAudioDeviceManager::IsMoveToNearlinkDevice(const std::shared_ptr<AudioStreamDescriptor> &streamDesc)
285 {
286 CHECK_AND_RETURN_RET_LOG(streamDesc != nullptr, false, "streamDesc is nullptr");
287 CHECK_AND_RETURN_RET_LOG(streamDesc->newDeviceDescs_.size() != 0 && streamDesc->newDeviceDescs_[0] != nullptr,
288 false, "newDeviceDescs is invaild");
289 return IsNearlinkDevice(streamDesc->newDeviceDescs_[0]->deviceType_);
290 }
291
IsNearlinkMoveToOtherDevice(const std::shared_ptr<AudioStreamDescriptor> & streamDesc)292 bool SleAudioDeviceManager::IsNearlinkMoveToOtherDevice(const std::shared_ptr<AudioStreamDescriptor> &streamDesc)
293 {
294 CHECK_AND_RETURN_RET_LOG(streamDesc != nullptr, false, "streamDesc is nullptr");
295 CHECK_AND_RETURN_RET_LOG(streamDesc->newDeviceDescs_.size() != 0 && streamDesc->newDeviceDescs_[0] != nullptr,
296 false, "newDeviceDescs is invaild");
297 if (streamDesc->oldDeviceDescs_.size() != 0 && streamDesc->oldDeviceDescs_[0] != nullptr &&
298 IsNearlinkDevice(streamDesc->oldDeviceDescs_[0]->deviceType_)) {
299 return !streamDesc->oldDeviceDescs_[0]->IsSameDeviceDescPtr(streamDesc->newDeviceDescs_[0]);
300 }
301 return false;
302 }
303
UpdateStreamTypeMap(const std::string & deviceAddr,uint32_t streamType,uint32_t sessionId,bool isAdd)304 void SleAudioDeviceManager::UpdateStreamTypeMap(const std::string &deviceAddr, uint32_t streamType,
305 uint32_t sessionId, bool isAdd)
306 {
307 std::lock_guard<std::mutex> lock(startedSleStreamTypeMutex_);
308 auto &sessionSet = startedSleStreamType_[deviceAddr][streamType];
309 AUDIO_INFO_LOG("sle streamType %{public}u sessionId %{public}d", streamType, sessionId);
310 if (isAdd) {
311 sessionSet.insert(sessionId);
312 } else {
313 bool isErased = sessionSet.erase(sessionId) > 0;
314 if (isErased && sessionSet.empty()) {
315 StopPlaying(deviceAddr, streamType);
316 }
317 }
318 }
319
UpdateSleStreamTypeCount(const std::shared_ptr<AudioStreamDescriptor> & streamDesc,bool isRemoved)320 void SleAudioDeviceManager::UpdateSleStreamTypeCount(const std::shared_ptr<AudioStreamDescriptor> &streamDesc,
321 bool isRemoved)
322 {
323 CHECK_AND_RETURN_LOG(streamDesc != nullptr, "streamDesc is nullptr");
324
325 uint32_t sessionId = streamDesc->sessionId_;
326 uint32_t streamType = 0;
327 std::string newDeviceAddr = "";
328 std::string oldDeviceAddr = "";
329 if (streamDesc->audioMode_ == AUDIO_MODE_PLAYBACK) {
330 StreamUsage streamUsage = streamDesc->rendererInfo_.streamUsage;
331 streamType = GetSleStreamTypeByStreamUsage(streamUsage);
332
333 if (IsNearlinkMoveToOtherDevice(streamDesc)) {
334 oldDeviceAddr = streamDesc->oldDeviceDescs_[0]->macAddress_;
335 UpdateStreamTypeMap(oldDeviceAddr, streamType, sessionId, false);
336 }
337 if (IsMoveToNearlinkDevice(streamDesc)) {
338 newDeviceAddr = streamDesc->newDeviceDescs_[0]->macAddress_;
339 if (streamDesc->streamStatus_ == STREAM_STATUS_STARTED) {
340 UpdateStreamTypeMap(newDeviceAddr, streamType, sessionId, true);
341 } else {
342 AUDIO_INFO_LOG("session %{public}d is not running", sessionId);
343 UpdateStreamTypeMap(newDeviceAddr, streamType, sessionId, false);
344 }
345 if (isRemoved) {
346 UpdateStreamTypeMap(oldDeviceAddr, streamType, sessionId, false);
347 }
348 }
349 } else {
350 SourceType sourceType = streamDesc->capturerInfo_.sourceType;
351 streamType = GetSleStreamTypeBySourceType(sourceType);
352
353 if (IsNearlinkMoveToOtherDevice(streamDesc)) {
354 oldDeviceAddr = streamDesc->oldDeviceDescs_[0]->macAddress_;
355 UpdateStreamTypeMap(oldDeviceAddr, streamType, sessionId, false);
356 }
357 if (IsMoveToNearlinkDevice(streamDesc)) {
358 newDeviceAddr = streamDesc->newDeviceDescs_[0]->macAddress_;
359 if (streamDesc->streamStatus_ == STREAM_STATUS_STARTED) {
360 UpdateStreamTypeMap(newDeviceAddr, streamType, sessionId, true);
361 } else {
362 AUDIO_INFO_LOG("session %{public}d is not running", sessionId);
363 UpdateStreamTypeMap(newDeviceAddr, streamType, sessionId, false);
364 }
365 if (isRemoved) {
366 UpdateStreamTypeMap(oldDeviceAddr, streamType, sessionId, false);
367 }
368 }
369 }
370 }
371
ResetSleStreamTypeCount(const std::shared_ptr<AudioDeviceDescriptor> & deviceDesc)372 void SleAudioDeviceManager::ResetSleStreamTypeCount(const std::shared_ptr<AudioDeviceDescriptor> &deviceDesc)
373 {
374 CHECK_AND_RETURN_LOG(deviceDesc != nullptr, "deviceDesc is nullptr");
375
376 std::lock_guard<std::mutex> lock(startedSleStreamTypeMutex_);
377 auto it = startedSleStreamType_.find(deviceDesc->macAddress_);
378 CHECK_AND_RETURN_LOG(it != startedSleStreamType_.end(), "device %{public}s not found",
379 AudioPolicyUtils::GetInstance().GetEncryptAddr(deviceDesc->macAddress_).c_str());
380
381 for (const auto &pair : it->second) {
382 uint32_t streamType = pair.first;
383 CHECK_AND_CONTINUE_LOG(!pair.second.empty(), "streamType %{public}u has no session", streamType);
384 StopPlaying(deviceDesc->macAddress_, streamType);
385 }
386
387 startedSleStreamType_.erase(it);
388 }
389
GetNearlinkStreamTypeMapByDevice(const std::string & deviceAddr)390 std::unordered_map<uint32_t, std::unordered_set<uint32_t>> SleAudioDeviceManager::GetNearlinkStreamTypeMapByDevice(
391 const std::string &deviceAddr)
392 {
393 std::lock_guard<std::mutex> lock(startedSleStreamTypeMutex_);
394 auto it = startedSleStreamType_.find(deviceAddr);
395 auto ret = std::unordered_map<uint32_t, std::unordered_set<uint32_t>>();
396 if (it != startedSleStreamType_.end()) {
397 for (const auto &pair : it->second) {
398 uint32_t streamType = pair.first;
399 std::unordered_set<uint32_t> sessionSet = pair.second;
400 if (!sessionSet.empty()) {
401 ret[streamType] = sessionSet;
402 }
403 }
404 }
405 return ret;
406 }
407
SetNearlinkDeviceMute(const std::string & device,AudioStreamType streamType,bool isMute)408 int32_t SleAudioDeviceManager::SetNearlinkDeviceMute(const std::string &device, AudioStreamType streamType, bool isMute)
409 {
410 std::lock_guard<std::mutex> lock(deviceVolumeConfigMutex_);
411 CHECK_AND_RETURN_RET_LOG(deviceVolumeConfigInfo_.find(device) != deviceVolumeConfigInfo_.end(),
412 ERR_INVALID_PARAM, "device not found");
413 if (streamType == STREAM_MUSIC) {
414 deviceVolumeConfigInfo_[device].first.isMute = isMute;
415 } else {
416 AUDIO_ERR_LOG("voice call cannot set mute");
417 }
418 return SUCCESS;
419 }
420
SetNearlinkDeviceVolumeLevel(const std::string & device,AudioStreamType streamType,const int32_t volumeLevel)421 int32_t SleAudioDeviceManager::SetNearlinkDeviceVolumeLevel(const std::string &device, AudioStreamType streamType,
422 const int32_t volumeLevel)
423 {
424 std::lock_guard<std::mutex> lock(deviceVolumeConfigMutex_);
425 CHECK_AND_RETURN_RET_LOG(deviceVolumeConfigInfo_.find(device) != deviceVolumeConfigInfo_.end(),
426 ERR_INVALID_PARAM, "device not found");
427 if (streamType == STREAM_MUSIC) {
428 deviceVolumeConfigInfo_[device].first.volumeLevel = volumeLevel;
429 } else if (streamType == STREAM_VOICE_CALL && volumeLevel > 0) {
430 deviceVolumeConfigInfo_[device].second.volumeLevel = volumeLevel;
431 }
432 return SUCCESS;
433 }
434
GetVolumeLevelByVolumeType(AudioVolumeType volumeType,const AudioDeviceDescriptor & deviceDesc)435 int32_t SleAudioDeviceManager::GetVolumeLevelByVolumeType(AudioVolumeType volumeType,
436 const AudioDeviceDescriptor &deviceDesc)
437 {
438 std::lock_guard<std::mutex> lock(deviceVolumeConfigMutex_);
439 CHECK_AND_RETURN_RET_LOG(deviceVolumeConfigInfo_.find(deviceDesc.macAddress_) != deviceVolumeConfigInfo_.end(),
440 ERR_INVALID_PARAM, "device not found");
441 if (volumeType == STREAM_MUSIC) {
442 return deviceVolumeConfigInfo_[deviceDesc.macAddress_].first.isMute ? 0 :
443 deviceVolumeConfigInfo_[deviceDesc.macAddress_].first.volumeLevel;
444 } else if (volumeType == STREAM_VOICE_CALL) {
445 return deviceVolumeConfigInfo_[deviceDesc.macAddress_].second.volumeLevel;
446 }
447 return 0;
448 }
449 } // namespace AudioStandard
450 } // namespace OHOS
451