1 /*
2  * Copyright (c) 2024 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 "OHAudioDeviceDescriptor.h"
17 
convertDeviceDescriptor(OH_AudioDeviceDescriptor * deviceDescriptor)18 static OHOS::AudioStandard::OHAudioDeviceDescriptor *convertDeviceDescriptor(
19     OH_AudioDeviceDescriptor* deviceDescriptor)
20 {
21     return (OHOS::AudioStandard::OHAudioDeviceDescriptor*) deviceDescriptor;
22 }
23 
OH_AudioDeviceDescriptor_GetDeviceRole(OH_AudioDeviceDescriptor * audioDeviceDescriptor,OH_AudioDevice_Role * deviceRole)24 OH_AudioCommon_Result OH_AudioDeviceDescriptor_GetDeviceRole(OH_AudioDeviceDescriptor *audioDeviceDescriptor,
25                                                              OH_AudioDevice_Role *deviceRole)
26 {
27     OHOS::AudioStandard::OHAudioDeviceDescriptor* deviceDescriptor = convertDeviceDescriptor(audioDeviceDescriptor);
28     CHECK_AND_RETURN_RET_LOG(deviceDescriptor != nullptr,
29         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioDeviceDescriptor is nullptr");
30     CHECK_AND_RETURN_RET_LOG(deviceRole != nullptr,
31         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "deviceRole is nullptr");
32     return deviceDescriptor->GetDeviceRole(deviceRole);
33 }
34 
OH_AudioDeviceDescriptor_GetDeviceType(OH_AudioDeviceDescriptor * audioDeviceDescriptor,OH_AudioDevice_Type * deviceType)35 OH_AudioCommon_Result OH_AudioDeviceDescriptor_GetDeviceType(OH_AudioDeviceDescriptor *audioDeviceDescriptor,
36                                                              OH_AudioDevice_Type *deviceType)
37 {
38     OHOS::AudioStandard::OHAudioDeviceDescriptor* deviceDescriptor = convertDeviceDescriptor(audioDeviceDescriptor);
39     CHECK_AND_RETURN_RET_LOG(deviceDescriptor != nullptr,
40         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioDeviceDescriptor is nullptr");
41     CHECK_AND_RETURN_RET_LOG(deviceType != nullptr,
42         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "deviceType is nullptr");
43     return deviceDescriptor->GetDeviceType(deviceType);
44 }
45 
OH_AudioDeviceDescriptor_GetDeviceId(OH_AudioDeviceDescriptor * audioDeviceDescriptor,uint32_t * id)46 OH_AudioCommon_Result OH_AudioDeviceDescriptor_GetDeviceId(OH_AudioDeviceDescriptor *audioDeviceDescriptor,
47                                                            uint32_t *id)
48 {
49     OHOS::AudioStandard::OHAudioDeviceDescriptor* deviceDescriptor = convertDeviceDescriptor(audioDeviceDescriptor);
50     CHECK_AND_RETURN_RET_LOG(deviceDescriptor != nullptr,
51         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioDeviceDescriptor is nullptr");
52     CHECK_AND_RETURN_RET_LOG(id != nullptr,
53         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "id is nullptr");
54     return deviceDescriptor->GetDeviceId(id);
55 }
56 
OH_AudioDeviceDescriptor_GetDeviceName(OH_AudioDeviceDescriptor * audioDeviceDescriptor,char ** name)57 OH_AudioCommon_Result OH_AudioDeviceDescriptor_GetDeviceName(OH_AudioDeviceDescriptor *audioDeviceDescriptor,
58                                                              char **name)
59 {
60     OHOS::AudioStandard::OHAudioDeviceDescriptor* deviceDescriptor = convertDeviceDescriptor(audioDeviceDescriptor);
61     CHECK_AND_RETURN_RET_LOG(deviceDescriptor != nullptr,
62         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioDeviceDescriptor is nullptr");
63     CHECK_AND_RETURN_RET_LOG(name != nullptr,
64         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "name is nullptr");
65     return deviceDescriptor->GetDeviceName(name);
66 }
67 
OH_AudioDeviceDescriptor_GetDeviceAddress(OH_AudioDeviceDescriptor * audioDeviceDescriptor,char ** address)68 OH_AudioCommon_Result OH_AudioDeviceDescriptor_GetDeviceAddress(OH_AudioDeviceDescriptor *audioDeviceDescriptor,
69                                                                 char **address)
70 {
71     OHOS::AudioStandard::OHAudioDeviceDescriptor* deviceDescriptor = convertDeviceDescriptor(audioDeviceDescriptor);
72     CHECK_AND_RETURN_RET_LOG(deviceDescriptor != nullptr,
73         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioDeviceDescriptor is nullptr");
74     CHECK_AND_RETURN_RET_LOG(address != nullptr,
75         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "address is nullptr");
76     return deviceDescriptor->GetDeviceAddress(address);
77 }
78 
OH_AudioDeviceDescriptor_GetDeviceSampleRates(OH_AudioDeviceDescriptor * audioDeviceDescriptor,uint32_t ** sampleRates,uint32_t * size)79 OH_AudioCommon_Result OH_AudioDeviceDescriptor_GetDeviceSampleRates(OH_AudioDeviceDescriptor *audioDeviceDescriptor,
80                                                                     uint32_t **sampleRates, uint32_t *size)
81 {
82     OHOS::AudioStandard::OHAudioDeviceDescriptor* deviceDescriptor = convertDeviceDescriptor(audioDeviceDescriptor);
83     CHECK_AND_RETURN_RET_LOG(deviceDescriptor != nullptr,
84         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioDeviceDescriptor is nullptr");
85     CHECK_AND_RETURN_RET_LOG(sampleRates != nullptr,
86         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "sampleRates is nullptr");
87     CHECK_AND_RETURN_RET_LOG(size != nullptr,
88         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "size is nullptr");
89     return deviceDescriptor->GetDeviceSampleRates(sampleRates, size);
90 }
91 
OH_AudioDeviceDescriptor_GetDeviceChannelCounts(OH_AudioDeviceDescriptor * audioDeviceDescriptor,uint32_t ** channelCounts,uint32_t * size)92 OH_AudioCommon_Result OH_AudioDeviceDescriptor_GetDeviceChannelCounts(OH_AudioDeviceDescriptor *audioDeviceDescriptor,
93                                                                       uint32_t **channelCounts, uint32_t *size)
94 {
95     OHOS::AudioStandard::OHAudioDeviceDescriptor* deviceDescriptor = convertDeviceDescriptor(audioDeviceDescriptor);
96     CHECK_AND_RETURN_RET_LOG(deviceDescriptor != nullptr,
97         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioDeviceDescriptor is nullptr");
98     CHECK_AND_RETURN_RET_LOG(channelCounts != nullptr,
99         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "channelCounts is nullptr");
100     CHECK_AND_RETURN_RET_LOG(size != nullptr,
101         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "size is nullptr");
102     return deviceDescriptor->GetDeviceChannelCounts(channelCounts, size);
103 }
104 
OH_AudioDeviceDescriptor_GetDeviceDisplayName(OH_AudioDeviceDescriptor * audioDeviceDescriptor,char ** displayName)105 OH_AudioCommon_Result OH_AudioDeviceDescriptor_GetDeviceDisplayName(OH_AudioDeviceDescriptor *audioDeviceDescriptor,
106                                                                     char **displayName)
107 {
108     OHOS::AudioStandard::OHAudioDeviceDescriptor* deviceDescriptor = convertDeviceDescriptor(audioDeviceDescriptor);
109     CHECK_AND_RETURN_RET_LOG(deviceDescriptor != nullptr,
110         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioDeviceDescriptor is nullptr");
111     CHECK_AND_RETURN_RET_LOG(displayName != nullptr,
112         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "displayName is nullptr");
113     return deviceDescriptor->GetDeviceDisplayName(displayName);
114 }
115 
OH_AudioDeviceDescriptor_GetDeviceEncodingTypes(OH_AudioDeviceDescriptor * audioDeviceDescriptor,OH_AudioStream_EncodingType ** encodingTypes,uint32_t * size)116 OH_AudioCommon_Result OH_AudioDeviceDescriptor_GetDeviceEncodingTypes(OH_AudioDeviceDescriptor *audioDeviceDescriptor,
117     OH_AudioStream_EncodingType **encodingTypes, uint32_t *size)
118 {
119     OHOS::AudioStandard::OHAudioDeviceDescriptor* deviceDescriptor = convertDeviceDescriptor(audioDeviceDescriptor);
120     CHECK_AND_RETURN_RET_LOG(deviceDescriptor != nullptr,
121         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "audioDeviceDescriptor is nullptr");
122     CHECK_AND_RETURN_RET_LOG(encodingTypes != nullptr,
123         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "encodingTypes is nullptr");
124     CHECK_AND_RETURN_RET_LOG(size != nullptr,
125         AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM, "size is nullptr");
126     return deviceDescriptor->GetDeviceEncodingTypes(encodingTypes, size);
127 }
128 
129 namespace OHOS {
130 namespace AudioStandard {
131 
OHAudioDeviceDescriptor(sptr<AudioDeviceDescriptor> audioDeviceDescriptor)132 OHAudioDeviceDescriptor::OHAudioDeviceDescriptor(sptr<AudioDeviceDescriptor> audioDeviceDescriptor)
133     : audioDeviceDescriptor_(audioDeviceDescriptor)
134 {
135     AUDIO_INFO_LOG("OHAudioDeviceDescriptor Constructor is called\n");
136 }
137 
~OHAudioDeviceDescriptor()138 OHAudioDeviceDescriptor::~OHAudioDeviceDescriptor()
139 {
140     AUDIO_INFO_LOG("~OHAudioDeviceDescriptor is called\n");
141     if (audioDeviceDescriptor_ != nullptr) {
142         audioDeviceDescriptor_ = nullptr;
143     }
144     if (audioSamplingRate_ != nullptr) {
145         delete[] audioSamplingRate_;
146         audioSamplingRate_ = nullptr;
147     }
148     if (audioChannel_ != nullptr) {
149         delete[] audioChannel_;
150         audioChannel_ = nullptr;
151     }
152     if (encodingType_ != nullptr) {
153         delete[] encodingType_;
154         encodingType_ = nullptr;
155     }
156 }
157 
GetDeviceRole(OH_AudioDevice_Role * deviceRole)158 OH_AudioCommon_Result OHAudioDeviceDescriptor::GetDeviceRole(OH_AudioDevice_Role *deviceRole)
159 {
160     CHECK_AND_RETURN_RET_LOG(audioDeviceDescriptor_ != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM,
161         "audioDeviceDescriptor_ is nullptr");
162     *deviceRole = (OH_AudioDevice_Role)audioDeviceDescriptor_->deviceRole_;
163     return AUDIOCOMMON_RESULT_SUCCESS;
164 }
165 
GetDeviceType(OH_AudioDevice_Type * deviceType)166 OH_AudioCommon_Result OHAudioDeviceDescriptor::GetDeviceType(OH_AudioDevice_Type *deviceType)
167 {
168     CHECK_AND_RETURN_RET_LOG(audioDeviceDescriptor_ != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM,
169         "audioDeviceDescriptor_ is nullptr");
170     *deviceType = (OH_AudioDevice_Type)audioDeviceDescriptor_->deviceType_;
171     return AUDIOCOMMON_RESULT_SUCCESS;
172 }
173 
GetDeviceId(uint32_t * id)174 OH_AudioCommon_Result OHAudioDeviceDescriptor::GetDeviceId(uint32_t *id)
175 {
176     CHECK_AND_RETURN_RET_LOG(audioDeviceDescriptor_ != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM,
177         "audioDeviceDescriptor_ is nullptr");
178     *id = (uint32_t)audioDeviceDescriptor_->deviceId_;
179     return AUDIOCOMMON_RESULT_SUCCESS;
180 }
181 
GetDeviceName(char ** name)182 OH_AudioCommon_Result OHAudioDeviceDescriptor::GetDeviceName(char **name)
183 {
184     CHECK_AND_RETURN_RET_LOG(audioDeviceDescriptor_ != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM,
185         "audioDeviceDescriptor_ is nullptr");
186     const char *deviceName = audioDeviceDescriptor_->deviceName_.c_str();
187     *name = const_cast<char*>(deviceName);
188     return AUDIOCOMMON_RESULT_SUCCESS;
189 }
190 
GetDeviceAddress(char ** address)191 OH_AudioCommon_Result OHAudioDeviceDescriptor::GetDeviceAddress(char **address)
192 {
193     CHECK_AND_RETURN_RET_LOG(audioDeviceDescriptor_ != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM,
194         "audioDeviceDescriptor_ is nullptr");
195     const char *macAddress = audioDeviceDescriptor_->macAddress_.c_str();
196     *address = const_cast<char*>(macAddress);
197     return AUDIOCOMMON_RESULT_SUCCESS;
198 }
199 
GetDeviceSampleRates(uint32_t ** sampleRates,uint32_t * size)200 OH_AudioCommon_Result OHAudioDeviceDescriptor::GetDeviceSampleRates(uint32_t **sampleRates, uint32_t *size)
201 {
202     CHECK_AND_RETURN_RET_LOG(audioDeviceDescriptor_ != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM,
203         "audioDeviceDescriptor_ is nullptr");
204     DeviceStreamInfo audioStreamInfo = audioDeviceDescriptor_->audioStreamInfo_;
205 
206     uint32_t samplingRateSize = (uint32_t)audioStreamInfo.samplingRate.size();
207     if (samplingRateSize == 0) {
208         return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
209     }
210     if (audioSamplingRate_ == nullptr) {
211         audioSamplingRate_ = new uint32_t[samplingRateSize];
212         int index = 0;
213         for (const auto samplingRate : audioStreamInfo.samplingRate) {
214             audioSamplingRate_[index++]= static_cast<uint32_t>(samplingRate);
215         }
216     }
217     *size = samplingRateSize;
218     *sampleRates = audioSamplingRate_;
219     return AUDIOCOMMON_RESULT_SUCCESS;
220 }
221 
GetDeviceChannelCounts(uint32_t ** channelCounts,uint32_t * size)222 OH_AudioCommon_Result OHAudioDeviceDescriptor::GetDeviceChannelCounts(uint32_t **channelCounts, uint32_t *size)
223 {
224     CHECK_AND_RETURN_RET_LOG(audioDeviceDescriptor_ != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM,
225         "audioDeviceDescriptor_ is nullptr");
226     DeviceStreamInfo audioStreamInfo = audioDeviceDescriptor_->audioStreamInfo_;
227     uint32_t channelsSize = (uint32_t)audioStreamInfo.channels.size();
228     if (channelsSize == 0) {
229         return AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM;
230     }
231     if (audioChannel_ == nullptr) {
232         audioChannel_ = new uint32_t[channelsSize];
233         int index = 0;
234         for (const auto channels : audioStreamInfo.channels) {
235             audioChannel_[index++] = static_cast<uint32_t>(channels);
236         }
237     }
238     *size = channelsSize;
239     *channelCounts = audioChannel_;
240     return AUDIOCOMMON_RESULT_SUCCESS;
241 }
242 
GetDeviceDisplayName(char ** displayName)243 OH_AudioCommon_Result OHAudioDeviceDescriptor::GetDeviceDisplayName(char **displayName)
244 {
245     CHECK_AND_RETURN_RET_LOG(audioDeviceDescriptor_ != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM,
246         "audioDeviceDescriptor_ is nullptr");
247     const char *name = audioDeviceDescriptor_->displayName_.c_str();
248     *displayName = const_cast<char*>(name);
249     return AUDIOCOMMON_RESULT_SUCCESS;
250 }
251 
GetDeviceEncodingTypes(OH_AudioStream_EncodingType ** encodingTypes,uint32_t * size)252 OH_AudioCommon_Result OHAudioDeviceDescriptor::GetDeviceEncodingTypes(OH_AudioStream_EncodingType **encodingTypes,
253     uint32_t *size)
254 {
255     CHECK_AND_RETURN_RET_LOG(audioDeviceDescriptor_ != nullptr, AUDIOCOMMON_RESULT_ERROR_INVALID_PARAM,
256         "audioDeviceDescriptor_ is nullptr");
257     DeviceStreamInfo audioStreamInfo = audioDeviceDescriptor_->audioStreamInfo_;
258     if (encodingType_ == nullptr) {
259         encodingType_ = new OH_AudioStream_EncodingType[1];
260         encodingType_[0] = (OH_AudioStream_EncodingType)audioStreamInfo.encoding;
261     }
262     *size = 1;
263     *encodingTypes = encodingType_;
264     return AUDIOCOMMON_RESULT_SUCCESS;
265 }
266 }
267 }