• 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 
16 #include "audio_device_descriptor.h"
17 
18 #include <cinttypes>
19 #include "audio_service_log.h"
20 #include "audio_utils.h"
21 
22 namespace OHOS {
23 namespace AudioStandard {
24 constexpr int32_t API_VERSION_18 = 18;
25 
26 const std::map<DeviceType, std::string> deviceTypeStringMap = {
27     {DEVICE_TYPE_INVALID, "INVALID"},
28     {DEVICE_TYPE_EARPIECE, "EARPIECE"},
29     {DEVICE_TYPE_SPEAKER, "SPEAKER"},
30     {DEVICE_TYPE_WIRED_HEADSET, "WIRED_HEADSET"},
31     {DEVICE_TYPE_WIRED_HEADPHONES, "WIRED_HEADPHONES"},
32     {DEVICE_TYPE_BLUETOOTH_SCO, "BLUETOOTH_SCO"},
33     {DEVICE_TYPE_BLUETOOTH_A2DP, "BLUETOOTH_A2DP"},
34     {DEVICE_TYPE_BLUETOOTH_A2DP_IN, "BLUETOOTH_A2DP_IN"},
35     {DEVICE_TYPE_HEARING_AID, "HEARING_AID"},
36     {DEVICE_TYPE_NEARLINK, "NEARLINK"},
37     {DEVICE_TYPE_NEARLINK_IN, "NEARLINK_IN"},
38     {DEVICE_TYPE_MIC, "MIC"},
39     {DEVICE_TYPE_WAKEUP, "WAKEUP"},
40     {DEVICE_TYPE_USB_HEADSET, "USB_HEADSET"},
41     {DEVICE_TYPE_DP, "DP"},
42     {DEVICE_TYPE_REMOTE_CAST, "REMOTE_CAST"},
43     {DEVICE_TYPE_USB_DEVICE, "USB_DEVICE"},
44     {DEVICE_TYPE_ACCESSORY, "ACCESSORY"},
45     {DEVICE_TYPE_REMOTE_DAUDIO, "REMOTE_DAUDIO"},
46     {DEVICE_TYPE_HDMI, "HDMI"},
47     {DEVICE_TYPE_LINE_DIGITAL, "LINE_DIGITAL"},
48     {DEVICE_TYPE_FILE_SINK, "FILE_SINK"},
49     {DEVICE_TYPE_FILE_SOURCE, "FILE_SOURCE"},
50     {DEVICE_TYPE_EXTERN_CABLE, "EXTERN_CABLE"},
51     {DEVICE_TYPE_DEFAULT, "DEFAULT"},
52     {DEVICE_TYPE_USB_ARM_HEADSET, "USB_ARM_HEADSET"}
53 };
54 
55 const DeviceStreamInfo DEFAULT_DEVICE_STREAM_INFO(SAMPLE_RATE_44100, ENCODING_PCM, AudioSampleFormat::INVALID_WIDTH,
56     CH_LAYOUT_STEREO);
57 
DeviceTypeToString(DeviceType type)58 static const char *DeviceTypeToString(DeviceType type)
59 {
60     if (deviceTypeStringMap.count(type) != 0) {
61         return deviceTypeStringMap.at(type).c_str();
62     }
63     return "UNKNOWN";
64 }
65 
CheckDeviceInfoSize(size_t & size)66 static void CheckDeviceInfoSize(size_t &size)
67 {
68     CHECK_AND_RETURN(size > AUDIO_DEVICE_INFO_SIZE_LIMIT);
69     size = AUDIO_DEVICE_INFO_SIZE_LIMIT;
70 }
71 
MarshallingDeviceStreamInfoList(const std::list<DeviceStreamInfo> & deviceStreamInfos,Parcel & parcel)72 static bool MarshallingDeviceStreamInfoList(const std::list<DeviceStreamInfo> &deviceStreamInfos, Parcel &parcel)
73 {
74     size_t size = deviceStreamInfos.size();
75     CHECK_AND_RETURN_RET(parcel.WriteUint64(size), false);
76 
77     for (const auto &deviceStreamInfo : deviceStreamInfos) {
78         CHECK_AND_RETURN_RET(deviceStreamInfo.Marshalling(parcel), false);
79     }
80     return true;
81 }
82 
UnmarshallingDeviceStreamInfoList(Parcel & parcel,std::list<DeviceStreamInfo> & deviceStreamInfos)83 static void UnmarshallingDeviceStreamInfoList(Parcel &parcel, std::list<DeviceStreamInfo> &deviceStreamInfos)
84 {
85     size_t size = parcel.ReadUint64();
86     // due to security concerns, sizelimit has been imposed
87     CheckDeviceInfoSize(size);
88 
89     for (size_t i = 0; i < size; i++) {
90         DeviceStreamInfo deviceStreamInfo;
91         deviceStreamInfo.Unmarshalling(parcel);
92         deviceStreamInfos.push_back(deviceStreamInfo);
93     }
94 }
95 
SetDefaultStreamInfoIfEmpty(std::list<DeviceStreamInfo> & streamInfo)96 static void SetDefaultStreamInfoIfEmpty(std::list<DeviceStreamInfo> &streamInfo)
97 {
98     if (streamInfo.empty()) {
99         streamInfo.push_back(DEFAULT_DEVICE_STREAM_INFO);
100     } else {
101         for (auto &info : streamInfo) {
102             // If does not set sampleRates use SAMPLE_RATE_44100 instead.
103             if (info.samplingRate.empty()) {
104                 info.samplingRate = DEFAULT_DEVICE_STREAM_INFO.samplingRate;
105             }
106             // If does not set channelCounts use STEREO instead.
107             if (info.channelLayout.empty()) {
108                 info.channelLayout = DEFAULT_DEVICE_STREAM_INFO.channelLayout;
109             }
110         }
111     }
112 }
113 
AudioDeviceDescriptor(int32_t descriptorType)114 AudioDeviceDescriptor::AudioDeviceDescriptor(int32_t descriptorType)
115     : AudioDeviceDescriptor(DeviceType::DEVICE_TYPE_NONE, DeviceRole::DEVICE_ROLE_NONE)
116 {
117     descriptorType_ = descriptorType;
118     if (descriptorType_ == DEVICE_INFO) {
119         deviceType_ = DeviceType(0);
120         deviceRole_ = DeviceRole(0);
121         networkId_ = "";
122     }
123 }
124 
AudioDeviceDescriptor(DeviceType type,DeviceRole role)125 AudioDeviceDescriptor::AudioDeviceDescriptor(DeviceType type, DeviceRole role)
126     : deviceType_(type), deviceRole_(role)
127 {
128     deviceId_ = 0;
129     audioStreamInfo_ = {};
130     channelMasks_ = 0;
131     channelIndexMasks_ = 0;
132     deviceName_ = "";
133     macAddress_ = "";
134     volumeGroupId_ = 0;
135     interruptGroupId_ = 0;
136     networkId_ = LOCAL_NETWORK_ID;
137     displayName_ = "";
138     deviceCategory_ = CATEGORY_DEFAULT;
139     connectTimeStamp_ = 0;
140     connectState_ = CONNECTED;
141     pairDeviceDescriptor_ = nullptr;
142     isScoRealConnected_ = false;
143     isEnable_ = true;
144     exceptionFlag_ = false;
145     isLowLatencyDevice_ = false;
146     a2dpOffloadFlag_ = 0;
147     descriptorType_ = AUDIO_DEVICE_DESCRIPTOR;
148     spatializationSupported_ = false;
149     isVrSupported_ = true;
150 }
151 
~AudioDeviceDescriptor()152 AudioDeviceDescriptor::~AudioDeviceDescriptor()
153 {
154     pairDeviceDescriptor_ = nullptr;
155 }
156 
AudioDeviceDescriptor(DeviceType type,DeviceRole role,int32_t interruptGroupId,int32_t volumeGroupId,std::string networkId)157 AudioDeviceDescriptor::AudioDeviceDescriptor(DeviceType type, DeviceRole role, int32_t interruptGroupId,
158     int32_t volumeGroupId, std::string networkId)
159     : deviceType_(type), deviceRole_(role), interruptGroupId_(interruptGroupId), volumeGroupId_(volumeGroupId),
160     networkId_(networkId)
161 {
162     deviceId_ = 0;
163     audioStreamInfo_ = {};
164     channelMasks_ = 0;
165     channelIndexMasks_ = 0;
166     deviceName_ = "";
167     macAddress_ = "";
168     displayName_ = "";
169     deviceCategory_ = CATEGORY_DEFAULT;
170     connectTimeStamp_ = 0;
171     connectState_ = CONNECTED;
172     pairDeviceDescriptor_ = nullptr;
173     isScoRealConnected_ = false;
174     isEnable_ = true;
175     exceptionFlag_ = false;
176     isLowLatencyDevice_ = false;
177     a2dpOffloadFlag_ = 0;
178     descriptorType_ = AUDIO_DEVICE_DESCRIPTOR;
179     spatializationSupported_ = false;
180     isVrSupported_ = true;
181 }
182 
AudioDeviceDescriptor(const AudioDeviceDescriptor & deviceDescriptor)183 AudioDeviceDescriptor::AudioDeviceDescriptor(const AudioDeviceDescriptor &deviceDescriptor)
184 {
185     deviceId_ = deviceDescriptor.deviceId_;
186     deviceName_ = deviceDescriptor.deviceName_;
187     macAddress_ = deviceDescriptor.macAddress_;
188     deviceType_ = deviceDescriptor.deviceType_;
189     deviceRole_ = deviceDescriptor.deviceRole_;
190     audioStreamInfo_ = deviceDescriptor.audioStreamInfo_;
191     channelMasks_ = deviceDescriptor.channelMasks_;
192     channelIndexMasks_ = deviceDescriptor.channelIndexMasks_;
193     volumeGroupId_ = deviceDescriptor.volumeGroupId_;
194     interruptGroupId_ = deviceDescriptor.interruptGroupId_;
195     networkId_ = deviceDescriptor.networkId_;
196     dmDeviceType_ = deviceDescriptor.dmDeviceType_;
197     displayName_ = deviceDescriptor.displayName_;
198     deviceCategory_ = deviceDescriptor.deviceCategory_;
199     connectTimeStamp_ = deviceDescriptor.connectTimeStamp_;
200     connectState_ = deviceDescriptor.connectState_;
201     pairDeviceDescriptor_ = deviceDescriptor.pairDeviceDescriptor_;
202     isScoRealConnected_ = deviceDescriptor.isScoRealConnected_;
203     isEnable_ = deviceDescriptor.isEnable_;
204     exceptionFlag_ = deviceDescriptor.exceptionFlag_;
205     deviceUsage_ = deviceDescriptor.deviceUsage_;
206     // DeviceInfo
207     isLowLatencyDevice_ = deviceDescriptor.isLowLatencyDevice_;
208     a2dpOffloadFlag_ = deviceDescriptor.a2dpOffloadFlag_;
209     // Other
210     descriptorType_ = deviceDescriptor.descriptorType_;
211     hasPair_ = deviceDescriptor.hasPair_;
212     spatializationSupported_ = deviceDescriptor.spatializationSupported_;
213     isVrSupported_ = deviceDescriptor.isVrSupported_;
214 }
215 
AudioDeviceDescriptor(const std::shared_ptr<AudioDeviceDescriptor> & deviceDescriptor)216 AudioDeviceDescriptor::AudioDeviceDescriptor(const std::shared_ptr<AudioDeviceDescriptor> &deviceDescriptor)
217 {
218     CHECK_AND_RETURN_LOG(deviceDescriptor != nullptr, "Error input parameter");
219     deviceId_ = deviceDescriptor->deviceId_;
220     deviceName_ = deviceDescriptor->deviceName_;
221     macAddress_ = deviceDescriptor->macAddress_;
222     deviceType_ = deviceDescriptor->deviceType_;
223     deviceRole_ = deviceDescriptor->deviceRole_;
224     audioStreamInfo_ = deviceDescriptor->audioStreamInfo_;
225     channelMasks_ = deviceDescriptor->channelMasks_;
226     channelIndexMasks_ = deviceDescriptor->channelIndexMasks_;
227     volumeGroupId_ = deviceDescriptor->volumeGroupId_;
228     interruptGroupId_ = deviceDescriptor->interruptGroupId_;
229     networkId_ = deviceDescriptor->networkId_;
230     dmDeviceType_ = deviceDescriptor->dmDeviceType_;
231     displayName_ = deviceDescriptor->displayName_;
232     deviceCategory_ = deviceDescriptor->deviceCategory_;
233     connectTimeStamp_ = deviceDescriptor->connectTimeStamp_;
234     connectState_ = deviceDescriptor->connectState_;
235     pairDeviceDescriptor_ = deviceDescriptor->pairDeviceDescriptor_;
236     isScoRealConnected_ = deviceDescriptor->isScoRealConnected_;
237     isEnable_ = deviceDescriptor->isEnable_;
238     exceptionFlag_ = deviceDescriptor->exceptionFlag_;
239     deviceUsage_ = deviceDescriptor->deviceUsage_;
240     // DeviceInfo
241     isLowLatencyDevice_ = deviceDescriptor->isLowLatencyDevice_;
242     a2dpOffloadFlag_ = deviceDescriptor->a2dpOffloadFlag_;
243     // Other
244     descriptorType_ = deviceDescriptor->descriptorType_;
245     hasPair_ = deviceDescriptor->hasPair_;
246     spatializationSupported_ = deviceDescriptor->spatializationSupported_;
247     isVrSupported_ = deviceDescriptor->isVrSupported_;
248 }
249 
getType() const250 DeviceType AudioDeviceDescriptor::getType() const
251 {
252     return deviceType_;
253 }
254 
getRole() const255 DeviceRole AudioDeviceDescriptor::getRole() const
256 {
257     return deviceRole_;
258 }
259 
GetDeviceCategory() const260 DeviceCategory AudioDeviceDescriptor::GetDeviceCategory() const
261 {
262     return deviceCategory_;
263 }
264 
IsAudioDeviceDescriptor() const265 bool AudioDeviceDescriptor::IsAudioDeviceDescriptor() const
266 {
267     return descriptorType_ == AUDIO_DEVICE_DESCRIPTOR;
268 }
269 
SetClientInfo(std::shared_ptr<ClientInfo> clientInfo) const270 void AudioDeviceDescriptor::SetClientInfo(std::shared_ptr<ClientInfo> clientInfo) const
271 {
272     clientInfo_ = clientInfo;
273 }
274 
Marshalling(Parcel & parcel) const275 bool AudioDeviceDescriptor::Marshalling(Parcel &parcel) const
276 {
277     bool ret = MarshallingInner(parcel);
278     if (clientInfo_) {
279         clientInfo_ = nullptr;
280     }
281     return ret;
282 }
283 
MarshallingInner(Parcel & parcel) const284 bool AudioDeviceDescriptor::MarshallingInner(Parcel &parcel) const
285 {
286     if (clientInfo_ && !IsAudioDeviceDescriptor()) {
287         return MarshallingToDeviceInfo(parcel, clientInfo_->hasBTPermission_,
288             clientInfo_->hasSystemPermission_, clientInfo_->apiVersion_);
289     }
290 
291     int32_t devType = deviceType_;
292     if (IsAudioDeviceDescriptor()) {
293         devType = MapInternalToExternalDeviceType(clientInfo_ ? clientInfo_->apiVersion_ : 0);
294     }
295 
296     return  parcel.WriteInt32(devType) &&
297         parcel.WriteInt32(static_cast<int32_t>(deviceRole_)) &&
298         parcel.WriteInt32(deviceId_) &&
299         parcel.WriteInt32(channelMasks_) &&
300         parcel.WriteInt32(channelIndexMasks_) &&
301         parcel.WriteString(deviceName_) &&
302         parcel.WriteString(macAddress_) &&
303         parcel.WriteInt32(interruptGroupId_) &&
304         parcel.WriteInt32(volumeGroupId_) &&
305         parcel.WriteString(networkId_) &&
306         parcel.WriteUint16(dmDeviceType_) &&
307         parcel.WriteString(displayName_) &&
308         MarshallingDeviceStreamInfoList(audioStreamInfo_, parcel) &&
309         parcel.WriteInt32(static_cast<int32_t>(deviceCategory_)) &&
310         parcel.WriteInt32(static_cast<int32_t>(connectState_)) &&
311         parcel.WriteBool(exceptionFlag_) &&
312         parcel.WriteInt64(connectTimeStamp_) &&
313         parcel.WriteBool(isScoRealConnected_) &&
314         parcel.WriteBool(isEnable_) &&
315         parcel.WriteInt32(mediaVolume_) &&
316         parcel.WriteInt32(callVolume_) &&
317         parcel.WriteBool(isLowLatencyDevice_) &&
318         parcel.WriteInt32(a2dpOffloadFlag_) &&
319         parcel.WriteBool(descriptorType_) &&
320         parcel.WriteBool(spatializationSupported_) &&
321         parcel.WriteBool(hasPair_) &&
322         parcel.WriteInt32(routerType_) &&
323         parcel.WriteInt32(isVrSupported_) &&
324         parcel.WriteInt32(static_cast<int32_t>(deviceUsage_));
325 }
326 
FixApiCompatibility(int apiVersion,DeviceRole deviceRole,DeviceType & deviceType,int32_t & deviceId,std::list<DeviceStreamInfo> & streamInfo)327 void AudioDeviceDescriptor::FixApiCompatibility(int apiVersion, DeviceRole deviceRole,
328     DeviceType &deviceType, int32_t &deviceId, std::list<DeviceStreamInfo> &streamInfo)
329 {
330     // If api target version < 11 && does not set deviceType, fix api compatibility.
331     if (apiVersion < API_11 && (deviceType == DEVICE_TYPE_NONE || deviceType == DEVICE_TYPE_INVALID)) {
332         // DeviceType use speaker or mic instead.
333         if (deviceRole == OUTPUT_DEVICE) {
334             deviceType = DEVICE_TYPE_SPEAKER;
335             deviceId = 1; // 1 default speaker device id.
336         } else if (deviceRole == INPUT_DEVICE) {
337             deviceType = DEVICE_TYPE_MIC;
338             deviceId = 2; // 2 default mic device id.
339         }
340 
341         SetDefaultStreamInfoIfEmpty(streamInfo);
342     }
343 }
344 
MarshallingToDeviceInfo(Parcel & parcel,bool hasBTPermission,bool hasSystemPermission,int32_t apiVersion) const345 bool AudioDeviceDescriptor::MarshallingToDeviceInfo(Parcel &parcel, bool hasBTPermission, bool hasSystemPermission,
346     int32_t apiVersion) const
347 {
348     DeviceType devType = deviceType_;
349     int32_t devId = deviceId_;
350     std::list<DeviceStreamInfo> streamInfo = audioStreamInfo_;
351 
352     FixApiCompatibility(apiVersion, deviceRole_, devType, devId, streamInfo);
353 
354     return parcel.WriteInt32(static_cast<int32_t>(devType)) &&
355         parcel.WriteInt32(static_cast<int32_t>(deviceRole_)) &&
356         parcel.WriteInt32(devId) &&
357         parcel.WriteInt32(channelMasks_) &&
358         parcel.WriteInt32(channelIndexMasks_) &&
359         parcel.WriteString((!hasBTPermission && (deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP ||
360             deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO)) ? "" : deviceName_) &&
361         parcel.WriteString((!hasBTPermission && (deviceType_ == DEVICE_TYPE_BLUETOOTH_A2DP ||
362             deviceType_ == DEVICE_TYPE_BLUETOOTH_SCO)) ? "" : macAddress_) &&
363         parcel.WriteInt32(hasSystemPermission ? interruptGroupId_ : INVALID_GROUP_ID) &&
364         parcel.WriteInt32(hasSystemPermission ? volumeGroupId_ : INVALID_GROUP_ID) &&
365         parcel.WriteString(hasSystemPermission ? networkId_ : "") &&
366         parcel.WriteUint16(dmDeviceType_) &&
367         parcel.WriteString(displayName_) &&
368         MarshallingDeviceStreamInfoList(streamInfo, parcel) &&
369         parcel.WriteInt32(static_cast<int32_t>(deviceCategory_)) &&
370         parcel.WriteInt32(static_cast<int32_t>(connectState_)) &&
371         parcel.WriteBool(exceptionFlag_) &&
372         parcel.WriteInt64(connectTimeStamp_) &&
373         parcel.WriteBool(isScoRealConnected_) &&
374         parcel.WriteBool(isEnable_) &&
375         parcel.WriteInt32(mediaVolume_) &&
376         parcel.WriteInt32(callVolume_) &&
377         parcel.WriteBool(isLowLatencyDevice_) &&
378         parcel.WriteInt32(a2dpOffloadFlag_) &&
379         parcel.WriteBool(descriptorType_) &&
380         parcel.WriteBool(spatializationSupported_) &&
381         parcel.WriteBool(hasPair_) &&
382         parcel.WriteInt32(routerType_) &&
383         parcel.WriteInt32(isVrSupported_) &&
384         parcel.WriteInt32(static_cast<int32_t>(deviceUsage_));
385 }
386 
UnmarshallingSelf(Parcel & parcel)387 void AudioDeviceDescriptor::UnmarshallingSelf(Parcel &parcel)
388 {
389     deviceType_ = static_cast<DeviceType>(parcel.ReadInt32());
390     deviceRole_ = static_cast<DeviceRole>(parcel.ReadInt32());
391     deviceId_ = parcel.ReadInt32();
392     channelMasks_ = parcel.ReadInt32();
393     channelIndexMasks_ = parcel.ReadInt32();
394     deviceName_ = parcel.ReadString();
395     macAddress_ = parcel.ReadString();
396     interruptGroupId_ = parcel.ReadInt32();
397     volumeGroupId_ = parcel.ReadInt32();
398     networkId_ = parcel.ReadString();
399     dmDeviceType_ = parcel.ReadUint16();
400     displayName_ = parcel.ReadString();
401     UnmarshallingDeviceStreamInfoList(parcel, audioStreamInfo_);
402     deviceCategory_ = static_cast<DeviceCategory>(parcel.ReadInt32());
403     connectState_ = static_cast<ConnectState>(parcel.ReadInt32());
404     exceptionFlag_ = parcel.ReadBool();
405     connectTimeStamp_ = parcel.ReadInt64();
406     isScoRealConnected_ = parcel.ReadBool();
407     isEnable_ = parcel.ReadBool();
408     mediaVolume_ = parcel.ReadInt32();
409     callVolume_ = parcel.ReadInt32();
410     isLowLatencyDevice_ = parcel.ReadBool();
411     a2dpOffloadFlag_ = parcel.ReadInt32();
412     descriptorType_ = parcel.ReadBool();
413     spatializationSupported_ = parcel.ReadBool();
414     hasPair_ = parcel.ReadBool();
415     routerType_ = static_cast<RouterType>(parcel.ReadInt32());
416     isVrSupported_ = parcel.ReadInt32();
417     deviceUsage_ = static_cast<DeviceUsage>(parcel.ReadInt32());
418 }
419 
Unmarshalling(Parcel & parcel)420 AudioDeviceDescriptor *AudioDeviceDescriptor::Unmarshalling(Parcel &parcel)
421 {
422     auto deviceDescriptor = new(std::nothrow) AudioDeviceDescriptor();
423     if (deviceDescriptor == nullptr) {
424         return nullptr;
425     }
426 
427     deviceDescriptor->UnmarshallingSelf(parcel);
428     return deviceDescriptor;
429 }
430 
SetDeviceInfo(std::string deviceName,std::string macAddress)431 void AudioDeviceDescriptor::SetDeviceInfo(std::string deviceName, std::string macAddress)
432 {
433     deviceName_ = deviceName;
434     macAddress_ = macAddress;
435 }
436 
SetDeviceCapability(const std::list<DeviceStreamInfo> & audioStreamInfo,int32_t channelMask,int32_t channelIndexMasks)437 void AudioDeviceDescriptor::SetDeviceCapability(const std::list<DeviceStreamInfo> &audioStreamInfo, int32_t channelMask,
438     int32_t channelIndexMasks)
439 {
440     audioStreamInfo_ = audioStreamInfo;
441     channelMasks_ = channelMask;
442     channelIndexMasks_ = channelIndexMasks;
443 }
444 
IsSameDeviceDesc(const AudioDeviceDescriptor & deviceDescriptor) const445 bool AudioDeviceDescriptor::IsSameDeviceDesc(const AudioDeviceDescriptor &deviceDescriptor) const
446 {
447     return deviceDescriptor.deviceType_ == deviceType_ &&
448         deviceDescriptor.macAddress_ == macAddress_ &&
449         deviceDescriptor.networkId_ == networkId_ &&
450         (!IsUsb(deviceType_) || deviceDescriptor.deviceRole_ == deviceRole_);
451 }
452 
IsSameDeviceDescPtr(std::shared_ptr<AudioDeviceDescriptor> deviceDescriptor) const453 bool AudioDeviceDescriptor::IsSameDeviceDescPtr(std::shared_ptr<AudioDeviceDescriptor> deviceDescriptor) const
454 {
455     CHECK_AND_RETURN_RET_LOG(deviceDescriptor != nullptr, false, "input deviceDescriptor is null");
456     return deviceDescriptor->deviceType_ == deviceType_ &&
457         deviceDescriptor->macAddress_ == macAddress_ &&
458         deviceDescriptor->networkId_ == networkId_ &&
459         (!IsUsb(deviceType_) || deviceDescriptor->deviceRole_ == deviceRole_);
460 }
461 
IsSameDeviceInfo(const AudioDeviceDescriptor & deviceInfo) const462 bool AudioDeviceDescriptor::IsSameDeviceInfo(const AudioDeviceDescriptor &deviceInfo) const
463 {
464     return deviceType_ == deviceInfo.deviceType_ &&
465         deviceRole_ == deviceInfo.deviceRole_ &&
466         macAddress_ == deviceInfo.macAddress_ &&
467         networkId_ == deviceInfo.networkId_;
468 }
469 
IsPairedDeviceDesc(const AudioDeviceDescriptor & deviceDescriptor) const470 bool AudioDeviceDescriptor::IsPairedDeviceDesc(const AudioDeviceDescriptor &deviceDescriptor) const
471 {
472     return ((deviceDescriptor.deviceRole_ == INPUT_DEVICE && deviceRole_ == OUTPUT_DEVICE) ||
473         (deviceDescriptor.deviceRole_ == OUTPUT_DEVICE && deviceRole_ == INPUT_DEVICE)) &&
474         deviceDescriptor.deviceType_ == deviceType_ &&
475         deviceDescriptor.macAddress_ == macAddress_ &&
476         deviceDescriptor.networkId_ == networkId_;
477 }
478 
IsDistributedSpeaker() const479 bool AudioDeviceDescriptor::IsDistributedSpeaker() const
480 {
481     return deviceType_ == DEVICE_TYPE_SPEAKER && networkId_ != "LocalDevice";
482 }
483 
Dump(std::string & dumpString)484 void AudioDeviceDescriptor::Dump(std::string &dumpString)
485 {
486     AppendFormat(dumpString, "      - device %d: role %s type %d (%s) name: %s\n",
487         deviceId_, IsOutput() ? "Output" : "Input",
488         deviceType_, DeviceTypeToString(deviceType_), deviceName_.c_str());
489 }
490 
GetDeviceTypeString()491 std::string AudioDeviceDescriptor::GetDeviceTypeString()
492 {
493     return std::string(DeviceTypeToString(deviceType_));
494 }
495 
GetKey()496 std::string AudioDeviceDescriptor::GetKey()
497 {
498     return networkId_ + "_" + std::to_string(deviceType_);
499 }
500 
MapInternalToExternalDeviceType(int32_t apiVersion) const501 DeviceType AudioDeviceDescriptor::MapInternalToExternalDeviceType(int32_t apiVersion) const
502 {
503     switch (deviceType_) {
504         case DEVICE_TYPE_USB_HEADSET:
505         case DEVICE_TYPE_USB_ARM_HEADSET:
506             if (!hasPair_ && apiVersion >= API_VERSION_18) {
507 #ifdef DETECT_SOUNDBOX
508                 return DEVICE_TYPE_USB_DEVICE;
509 #else
510                 if (deviceRole_ == INPUT_DEVICE) {
511                     return DEVICE_TYPE_USB_DEVICE;
512                 }
513 #endif
514             }
515             return DEVICE_TYPE_USB_HEADSET;
516         case DEVICE_TYPE_BLUETOOTH_A2DP_IN:
517             return DEVICE_TYPE_BLUETOOTH_A2DP;
518         case DEVICE_TYPE_NEARLINK_IN:
519             return DEVICE_TYPE_NEARLINK;
520         default:
521             return deviceType_;
522     }
523 }
524 
GetDeviceStreamInfo(void) const525 DeviceStreamInfo AudioDeviceDescriptor::GetDeviceStreamInfo(void) const
526 {
527     DeviceStreamInfo streamInfo;
528     CHECK_AND_RETURN_RET_LOG(!audioStreamInfo_.empty(), streamInfo, "streamInfo empty, get default streamInfo");
529     return *audioStreamInfo_.rbegin();
530 }
531 } // AudioStandard
532 } // namespace OHOS
533