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 "multimedia_audio_common.h"
17 #include "multimedia_audio_error.h"
18
19 namespace OHOS {
20 namespace AudioStandard {
21 const size_t MAX_VALID_SIZE = 128;
MallocCString(const std::string &origin)22 char *MallocCString(const std::string &origin)
23 {
24 if (origin.empty()) {
25 return nullptr;
26 }
27 auto len = origin.length() + 1;
28 char *res = static_cast<char *>(malloc(sizeof(char) * len));
29 if (res == nullptr) {
30 return nullptr;
31 }
32 return std::char_traits<char>::copy(res, origin.c_str(), len);
33 }
34
Convert2AudioCapturerOptions(AudioCapturerOptions &opions, const CAudioCapturerOptions &cOptions)35 void Convert2AudioCapturerOptions(AudioCapturerOptions &opions, const CAudioCapturerOptions &cOptions)
36 {
37 opions.capturerInfo.sourceType = static_cast<SourceType>(cOptions.audioCapturerInfo.source);
38 opions.streamInfo.channels = static_cast<AudioChannel>(cOptions.audioStreamInfo.channels);
39 opions.streamInfo.channelLayout = static_cast<AudioChannelLayout>(cOptions.audioStreamInfo.channelLayout);
40 opions.streamInfo.encoding = static_cast<AudioEncodingType>(cOptions.audioStreamInfo.encodingType);
41 opions.streamInfo.format = static_cast<AudioSampleFormat>(cOptions.audioStreamInfo.sampleFormat);
42 opions.streamInfo.samplingRate = static_cast<AudioSamplingRate>(cOptions.audioStreamInfo.samplingRate);
43
44 /* only support flag 0 */
45 opions.capturerInfo.capturerFlags =
46 (cOptions.audioCapturerInfo.capturerFlags != 0) ? 0 : cOptions.audioCapturerInfo.capturerFlags;
47 }
48
Convert2CAudioCapturerInfo(CAudioCapturerInfo &cInfo, const AudioCapturerInfo &capturerInfo)49 void Convert2CAudioCapturerInfo(CAudioCapturerInfo &cInfo, const AudioCapturerInfo &capturerInfo)
50 {
51 cInfo.capturerFlags = capturerInfo.capturerFlags;
52 cInfo.source = static_cast<int32_t>(capturerInfo.sourceType);
53 }
54
Convert2CAudioStreamInfo(CAudioStreamInfo &cInfo, const AudioStreamInfo &streamInfo)55 void Convert2CAudioStreamInfo(CAudioStreamInfo &cInfo, const AudioStreamInfo &streamInfo)
56 {
57 cInfo.channels = static_cast<int32_t>(streamInfo.channels);
58 cInfo.encodingType = static_cast<int32_t>(streamInfo.encoding);
59 cInfo.sampleFormat = static_cast<int32_t>(streamInfo.format);
60 cInfo.samplingRate = static_cast<int32_t>(streamInfo.samplingRate);
61 cInfo.channelLayout = static_cast<int64_t>(streamInfo.channelLayout);
62 }
63
Convert2CAudioCapturerChangeInfo(CAudioCapturerChangeInfo &cInfo, const AudioCapturerChangeInfo &changeInfo, int32_t *errorCode)64 void Convert2CAudioCapturerChangeInfo(CAudioCapturerChangeInfo &cInfo, const AudioCapturerChangeInfo &changeInfo,
65 int32_t *errorCode)
66 {
67 cInfo.muted = changeInfo.muted;
68 cInfo.streamId = changeInfo.sessionId;
69 Convert2CAudioCapturerInfo(cInfo.audioCapturerInfo, changeInfo.capturerInfo);
70 Convert2CArrDeviceDescriptorByDeviceInfo(cInfo.deviceDescriptors, changeInfo.inputDeviceInfo, errorCode);
71 }
72
Convert2CArrDeviceDescriptorByDeviceInfo(CArrDeviceDescriptor &devices, const DeviceInfo &deviceInfo, int32_t *errorCode)73 void Convert2CArrDeviceDescriptorByDeviceInfo(CArrDeviceDescriptor &devices, const DeviceInfo &deviceInfo,
74 int32_t *errorCode)
75 {
76 size_t deviceSize = 1;
77 CDeviceDescriptor *device = static_cast<CDeviceDescriptor *>(malloc(sizeof(CDeviceDescriptor) * deviceSize));
78 if (device == nullptr) {
79 *errorCode = CJ_ERR_NO_MEMORY;
80 return;
81 }
82 for (int32_t i = 0; i < static_cast<int32_t>(deviceSize); i++) {
83 Convert2CDeviceDescriptor(&(device[i]), deviceInfo, errorCode);
84 }
85 devices.head = device;
86 devices.size = static_cast<int64_t>(deviceSize);
87 }
88
InitializeDeviceRatesAndChannels(CDeviceDescriptor *device, const DeviceInfo &deviceInfo, int32_t *errorCode)89 void InitializeDeviceRatesAndChannels(CDeviceDescriptor *device, const DeviceInfo &deviceInfo, int32_t *errorCode)
90 {
91 size_t rateSize = deviceInfo.audioStreamInfo.samplingRate.size();
92 if (rateSize == 0 || rateSize > MAX_VALID_SIZE) {
93 *errorCode = CJ_ERR_SYSTEM;
94 return;
95 }
96 auto rates = static_cast<int32_t *>(malloc(sizeof(int32_t) * rateSize));
97 if (rates == nullptr) {
98 *errorCode = CJ_ERR_NO_MEMORY;
99 return;
100 }
101 int32_t iter = 0;
102 for (auto rate : deviceInfo.audioStreamInfo.samplingRate) {
103 rates[iter] = static_cast<int32_t>(rate);
104 iter++;
105 }
106 iter = 0;
107 device->sampleRates.size = rateSize;
108 device->sampleRates.head = rates;
109
110 size_t channelSize = deviceInfo.audioStreamInfo.channels.size();
111 if (channelSize == 0 || channelSize > MAX_VALID_SIZE) {
112 *errorCode = CJ_ERR_SYSTEM;
113 return;
114 }
115 auto channels = static_cast<int32_t *>(malloc(sizeof(int32_t) * channelSize));
116 if (channels == nullptr) {
117 *errorCode = CJ_ERR_NO_MEMORY;
118 return;
119 }
120 for (auto channel : deviceInfo.audioStreamInfo.channels) {
121 rates[iter] = static_cast<int32_t>(channel);
122 iter++;
123 }
124 iter = 0;
125 device->channelCounts.size = channelSize;
126 device->channelCounts.head = channels;
127 }
128
Convert2CDeviceDescriptor(CDeviceDescriptor *device, const DeviceInfo &deviceInfo, int32_t *errorCode)129 void Convert2CDeviceDescriptor(CDeviceDescriptor *device, const DeviceInfo &deviceInfo, int32_t *errorCode)
130 {
131 int32_t deviceSize = 1;
132 device->deviceRole = static_cast<int32_t>(deviceInfo.deviceRole);
133 device->deviceType = static_cast<int32_t>(deviceInfo.deviceType);
134 device->displayName = MallocCString(deviceInfo.displayName);
135 device->address = MallocCString(deviceInfo.macAddress);
136 device->name = MallocCString(deviceInfo.deviceName);
137 device->id = deviceInfo.deviceId;
138
139 InitializeDeviceRatesAndChannels(device, deviceInfo, errorCode);
140 auto masks = static_cast<int32_t *>(malloc(sizeof(int32_t) * deviceSize));
141 if (masks == nullptr) {
142 *errorCode = CJ_ERR_NO_MEMORY;
143 return;
144 }
145 int32_t iter = 0;
146 masks[iter] = static_cast<int32_t>(deviceInfo.channelMasks);
147 device->channelMasks.size = deviceSize;
148 device->channelMasks.head = masks;
149
150 auto encodings = static_cast<int32_t *>(malloc(sizeof(int32_t) * deviceSize));
151 if (encodings == nullptr) {
152 *errorCode = CJ_ERR_NO_MEMORY;
153 return;
154 }
155 encodings[iter] = static_cast<int32_t>(deviceInfo.audioStreamInfo.encoding);
156 device->encodingTypes.hasValue = true;
157 device->encodingTypes.arr.size = deviceSize;
158 device->encodingTypes.arr.head = encodings;
159 }
160
Convert2CArrDeviceDescriptor(CArrDeviceDescriptor &devices, const std::vector<sptr<AudioDeviceDescriptor>> &deviceDescriptors, int32_t *errorCode)161 void Convert2CArrDeviceDescriptor(CArrDeviceDescriptor &devices,
162 const std::vector<sptr<AudioDeviceDescriptor>> &deviceDescriptors, int32_t *errorCode)
163 {
164 if (deviceDescriptors.empty()) {
165 *errorCode = CJ_ERR_SYSTEM;
166 return;
167 } else {
168 devices.size = static_cast<int64_t>(deviceDescriptors.size());
169 CDeviceDescriptor *device = static_cast<CDeviceDescriptor *>(malloc(sizeof(CDeviceDescriptor) * devices.size));
170 if (device == nullptr) {
171 *errorCode = CJ_ERR_NO_MEMORY;
172 return;
173 }
174 for (int32_t i = 0; i < static_cast<int32_t>(deviceDescriptors.size()); i++) {
175 DeviceInfo dInfo;
176 ConvertAudioDeviceDescriptor2DeviceInfo(dInfo, deviceDescriptors[i]);
177 Convert2CDeviceDescriptor(&(device[i]), dInfo, errorCode);
178 }
179 devices.head = device;
180 }
181 }
182
ConvertAudioDeviceDescriptor2DeviceInfo(DeviceInfo &deviceInfo, sptr<AudioDeviceDescriptor> audioDeviceDescriptor)183 void ConvertAudioDeviceDescriptor2DeviceInfo(DeviceInfo &deviceInfo, sptr<AudioDeviceDescriptor> audioDeviceDescriptor)
184 {
185 deviceInfo.deviceRole = audioDeviceDescriptor->deviceRole_;
186 deviceInfo.deviceType = audioDeviceDescriptor->deviceType_;
187 deviceInfo.deviceId = audioDeviceDescriptor->deviceId_;
188 deviceInfo.channelMasks = audioDeviceDescriptor->channelMasks_;
189 deviceInfo.channelIndexMasks = audioDeviceDescriptor->channelIndexMasks_;
190 deviceInfo.deviceName = audioDeviceDescriptor->deviceName_;
191 deviceInfo.macAddress = audioDeviceDescriptor->macAddress_;
192 deviceInfo.interruptGroupId = audioDeviceDescriptor->interruptGroupId_;
193 deviceInfo.volumeGroupId = audioDeviceDescriptor->volumeGroupId_;
194 deviceInfo.networkId = audioDeviceDescriptor->networkId_;
195 deviceInfo.displayName = audioDeviceDescriptor->displayName_;
196 deviceInfo.audioStreamInfo.samplingRate = audioDeviceDescriptor->audioStreamInfo_.samplingRate;
197 deviceInfo.audioStreamInfo.encoding = audioDeviceDescriptor->audioStreamInfo_.encoding;
198 deviceInfo.audioStreamInfo.format = audioDeviceDescriptor->audioStreamInfo_.format;
199 deviceInfo.audioStreamInfo.channels = audioDeviceDescriptor->audioStreamInfo_.channels;
200 }
201 } // namespace AudioStandard
202 } // namespace OHOS
203