• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
MallocCString(const std::string & origin)21 char *MallocCString(const std::string &origin)
22 {
23     if (origin.empty()) {
24         return nullptr;
25     }
26     auto len = origin.length() + 1;
27     char *res = static_cast<char *>(malloc(sizeof(char) * len));
28     if (res == nullptr) {
29         return nullptr;
30     }
31     return std::char_traits<char>::copy(res, origin.c_str(), len);
32 }
33 
Convert2AudioCapturerOptions(AudioCapturerOptions & opions,const CAudioCapturerOptions & cOptions)34 void Convert2AudioCapturerOptions(AudioCapturerOptions &opions, const CAudioCapturerOptions &cOptions)
35 {
36     opions.capturerInfo.sourceType = static_cast<SourceType>(cOptions.audioCapturerInfo.source);
37     opions.streamInfo.channels = static_cast<AudioChannel>(cOptions.audioStreamInfo.channels);
38     opions.streamInfo.channelLayout = static_cast<AudioChannelLayout>(cOptions.audioStreamInfo.channelLayout);
39     opions.streamInfo.encoding = static_cast<AudioEncodingType>(cOptions.audioStreamInfo.encodingType);
40     opions.streamInfo.format = static_cast<AudioSampleFormat>(cOptions.audioStreamInfo.sampleFormat);
41     opions.streamInfo.samplingRate = static_cast<AudioSamplingRate>(cOptions.audioStreamInfo.samplingRate);
42 
43     /* only support flag 0 */
44     opions.capturerInfo.capturerFlags =
45         (cOptions.audioCapturerInfo.capturerFlags != 0) ? 0 : cOptions.audioCapturerInfo.capturerFlags;
46 }
47 
Convert2CAudioCapturerInfo(CAudioCapturerInfo & cInfo,const AudioCapturerInfo & capturerInfo)48 void Convert2CAudioCapturerInfo(CAudioCapturerInfo &cInfo, const AudioCapturerInfo &capturerInfo)
49 {
50     cInfo.capturerFlags = capturerInfo.capturerFlags;
51     cInfo.source = static_cast<int32_t>(capturerInfo.sourceType);
52 }
53 
Convert2CAudioStreamInfo(CAudioStreamInfo & cInfo,const AudioStreamInfo & streamInfo)54 void Convert2CAudioStreamInfo(CAudioStreamInfo &cInfo, const AudioStreamInfo &streamInfo)
55 {
56     cInfo.channels = static_cast<int32_t>(streamInfo.channels);
57     cInfo.encodingType = static_cast<int32_t>(streamInfo.encoding);
58     cInfo.sampleFormat = static_cast<int32_t>(streamInfo.format);
59     cInfo.samplingRate = static_cast<int32_t>(streamInfo.samplingRate);
60     cInfo.channelLayout = static_cast<int64_t>(streamInfo.channelLayout);
61 }
62 
Convert2CAudioCapturerChangeInfo(CAudioCapturerChangeInfo & cInfo,const AudioCapturerChangeInfo & changeInfo,int32_t * errorCode)63 void Convert2CAudioCapturerChangeInfo(CAudioCapturerChangeInfo &cInfo, const AudioCapturerChangeInfo &changeInfo,
64     int32_t *errorCode)
65 {
66     cInfo.muted = changeInfo.muted;
67     cInfo.streamId = changeInfo.sessionId;
68     Convert2CAudioCapturerInfo(cInfo.audioCapturerInfo, changeInfo.capturerInfo);
69     Convert2CArrDeviceDescriptorByDeviceInfo(cInfo.deviceDescriptors, changeInfo.inputDeviceInfo, errorCode);
70 }
71 
Convert2CArrDeviceDescriptorByDeviceInfo(CArrDeviceDescriptor & devices,const AudioDeviceDescriptor & deviceInfo,int32_t * errorCode)72 void Convert2CArrDeviceDescriptorByDeviceInfo(CArrDeviceDescriptor &devices, const AudioDeviceDescriptor &deviceInfo,
73     int32_t *errorCode)
74 {
75     size_t deviceSize = 1;
76     int32_t mallocSize = static_cast<int32_t>(sizeof(CDeviceDescriptor) * deviceSize);
77     if (mallocSize <= 0 || mallocSize > static_cast<int32_t>(sizeof(CDeviceDescriptor) * MAX_MEM_MALLOC_SIZE)) {
78         *errorCode = CJ_ERR_SYSTEM;
79         return;
80     }
81     CDeviceDescriptor *device = static_cast<CDeviceDescriptor *>(malloc(mallocSize));
82     if (device == nullptr) {
83         *errorCode = CJ_ERR_NO_MEMORY;
84         return;
85     }
86     if (memset_s(device, mallocSize, 0, mallocSize) != EOK) {
87         *errorCode = CJ_ERR_SYSTEM;
88         free(device);
89         return;
90     }
91     devices.head = device;
92     devices.size = static_cast<int64_t>(deviceSize);
93     for (int32_t i = 0; i < static_cast<int32_t>(deviceSize); i++) {
94         Convert2CDeviceDescriptor(&(device[i]), deviceInfo, errorCode);
95         if (*errorCode != SUCCESS_CODE) {
96             return;
97         }
98     }
99 }
100 
InitializeDeviceChannels(CDeviceDescriptor * device,const AudioDeviceDescriptor & deviceInfo,int32_t * errorCode)101 void InitializeDeviceChannels(CDeviceDescriptor *device, const AudioDeviceDescriptor &deviceInfo,
102     int32_t *errorCode)
103 {
104     size_t channelSize = deviceInfo.audioStreamInfo_.channels.size();
105     if (channelSize == 0 || channelSize > MAX_MEM_MALLOC_SIZE) {
106         *errorCode = CJ_ERR_SYSTEM;
107         return;
108     }
109     int32_t mallocSize = static_cast<int32_t>(sizeof(int32_t) * channelSize);
110     if (mallocSize <= 0 || mallocSize > static_cast<int32_t>(sizeof(int32_t) * MAX_MEM_MALLOC_SIZE)) {
111         *errorCode = CJ_ERR_SYSTEM;
112         return;
113     }
114     auto channels = static_cast<int32_t *>(malloc(mallocSize));
115     if (channels == nullptr) {
116         *errorCode = CJ_ERR_NO_MEMORY;
117         return;
118     }
119 
120     if (memset_s(channels, mallocSize, 0, mallocSize) != EOK) {
121         *errorCode = CJ_ERR_SYSTEM;
122         free(channels);
123         return;
124     }
125     int32_t iter = 0;
126     device->channelCounts.size = static_cast<int64_t>(channelSize);
127     device->channelCounts.head = channels;
128     for (auto channel : deviceInfo.audioStreamInfo_.channels) {
129         channels[iter] = static_cast<int32_t>(channel);
130         iter++;
131     }
132 }
133 
InitializeDeviceRates(CDeviceDescriptor * device,const AudioDeviceDescriptor & deviceInfo,int32_t * errorCode)134 void InitializeDeviceRates(CDeviceDescriptor *device, const AudioDeviceDescriptor &deviceInfo,
135     int32_t *errorCode)
136 {
137     size_t rateSize = deviceInfo.audioStreamInfo_.samplingRate.size();
138     if (rateSize == 0 || rateSize > MAX_MEM_MALLOC_SIZE) {
139         *errorCode = CJ_ERR_SYSTEM;
140         return;
141     }
142     int32_t mallocSize = static_cast<int32_t>(sizeof(int32_t) * rateSize);
143     if (mallocSize <= 0 || mallocSize > static_cast<int32_t>(sizeof(int32_t) * MAX_MEM_MALLOC_SIZE)) {
144         *errorCode = CJ_ERR_SYSTEM;
145         return;
146     }
147     auto rates = static_cast<int32_t *>(malloc(mallocSize));
148     if (rates == nullptr) {
149         *errorCode = CJ_ERR_NO_MEMORY;
150         return;
151     }
152 
153     if (memset_s(rates, mallocSize, 0, mallocSize) != EOK) {
154         *errorCode = CJ_ERR_SYSTEM;
155         free(rates);
156         return;
157     }
158     int32_t iter = 0;
159     device->sampleRates.size = static_cast<int64_t>(rateSize);
160     device->sampleRates.head = rates;
161     for (auto rate : deviceInfo.audioStreamInfo_.samplingRate) {
162         rates[iter] = static_cast<int32_t>(rate);
163         iter++;
164     }
165 }
166 
Convert2CDeviceDescriptor(CDeviceDescriptor * device,const AudioDeviceDescriptor & deviceInfo,int32_t * errorCode)167 void Convert2CDeviceDescriptor(CDeviceDescriptor *device, const AudioDeviceDescriptor &deviceInfo, int32_t *errorCode)
168 {
169     int32_t deviceSize = 1;
170     device->deviceRole = static_cast<int32_t>(deviceInfo.deviceRole_);
171     device->deviceType = static_cast<int32_t>(deviceInfo.deviceType_);
172     device->displayName = MallocCString(deviceInfo.displayName_);
173     device->address = MallocCString(deviceInfo.macAddress_);
174     device->name = MallocCString(deviceInfo.deviceName_);
175     device->id = deviceInfo.deviceId_;
176 
177     InitializeDeviceRates(device, deviceInfo, errorCode);
178     InitializeDeviceChannels(device, deviceInfo, errorCode);
179     if (*errorCode != SUCCESS_CODE) {
180         return;
181     }
182     int32_t mallocSize = static_cast<int32_t>(sizeof(int32_t)) * deviceSize;
183     if (mallocSize <= 0 || mallocSize > static_cast<int32_t>(sizeof(int32_t) * MAX_MEM_MALLOC_SIZE)) {
184         *errorCode = CJ_ERR_SYSTEM;
185         return;
186     }
187     auto masks = static_cast<int32_t *>(malloc(mallocSize));
188     if (masks == nullptr) {
189         *errorCode = CJ_ERR_NO_MEMORY;
190         return;
191     }
192 
193     if (memset_s(masks, mallocSize, 0, mallocSize) != EOK) {
194         *errorCode = CJ_ERR_SYSTEM;
195         free(masks);
196         return;
197     }
198     int32_t iter = 0;
199     device->channelMasks.size = static_cast<int64_t>(deviceSize);
200     device->channelMasks.head = masks;
201     masks[iter] = static_cast<int32_t>(deviceInfo.channelMasks_);
202 
203     auto encodings = static_cast<int32_t *>(malloc(mallocSize));
204     if (encodings == nullptr) {
205         *errorCode = CJ_ERR_NO_MEMORY;
206         return;
207     }
208     if (memset_s(encodings, mallocSize, 0, mallocSize) != EOK) {
209         *errorCode = CJ_ERR_SYSTEM;
210         free(encodings);
211         return;
212     }
213     device->encodingTypes.hasValue = true;
214     device->encodingTypes.arr.size = static_cast<int64_t>(deviceSize);
215     device->encodingTypes.arr.head = encodings;
216     encodings[iter] = static_cast<int32_t>(deviceInfo.audioStreamInfo_.encoding);
217 }
218 
Convert2CArrDeviceDescriptor(CArrDeviceDescriptor & devices,const std::vector<std::shared_ptr<AudioDeviceDescriptor>> & deviceDescriptors,int32_t * errorCode)219 void Convert2CArrDeviceDescriptor(CArrDeviceDescriptor &devices,
220     const std::vector<std::shared_ptr<AudioDeviceDescriptor>> &deviceDescriptors, int32_t *errorCode)
221 {
222     if (deviceDescriptors.empty()) {
223         *errorCode = CJ_ERR_SYSTEM;
224         return;
225     } else {
226         auto deviceSize = deviceDescriptors.size();
227         devices.size = static_cast<int64_t>(deviceSize);
228         int32_t mallocSize = static_cast<int32_t>(sizeof(CDeviceDescriptor)) * static_cast<int32_t>(deviceSize);
229         if (mallocSize <= 0 || mallocSize > static_cast<int32_t>(sizeof(CDeviceDescriptor) * MAX_MEM_MALLOC_SIZE)) {
230             *errorCode = CJ_ERR_SYSTEM;
231             return;
232         }
233         CDeviceDescriptor *device = static_cast<CDeviceDescriptor *>(malloc(mallocSize));
234         if (device == nullptr) {
235             *errorCode = CJ_ERR_NO_MEMORY;
236             return;
237         }
238         if (memset_s(device, mallocSize, 0, mallocSize) != EOK) {
239             *errorCode = CJ_ERR_SYSTEM;
240             free(device);
241             return;
242         }
243         devices.head = device;
244         for (int32_t i = 0; i < static_cast<int32_t>(deviceSize); i++) {
245             AudioDeviceDescriptor dInfo(AudioDeviceDescriptor::DEVICE_INFO);
246             ConvertAudioDeviceDescriptor2DeviceInfo(dInfo, deviceDescriptors[i]);
247             Convert2CDeviceDescriptor(&(device[i]), dInfo, errorCode);
248             if (*errorCode != SUCCESS_CODE) {
249                 return;
250             }
251         }
252     }
253 }
254 
ConvertAudioDeviceDescriptor2DeviceInfo(AudioDeviceDescriptor & deviceInfo,std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor)255 void ConvertAudioDeviceDescriptor2DeviceInfo(AudioDeviceDescriptor &deviceInfo,
256     std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor)
257 {
258     deviceInfo.deviceRole_ = audioDeviceDescriptor->deviceRole_;
259     deviceInfo.deviceType_ = audioDeviceDescriptor->deviceType_;
260     deviceInfo.deviceId_ = audioDeviceDescriptor->deviceId_;
261     deviceInfo.channelMasks_ = audioDeviceDescriptor->channelMasks_;
262     deviceInfo.channelIndexMasks_ = audioDeviceDescriptor->channelIndexMasks_;
263     deviceInfo.deviceName_ = audioDeviceDescriptor->deviceName_;
264     deviceInfo.macAddress_ = audioDeviceDescriptor->macAddress_;
265     deviceInfo.interruptGroupId_ = audioDeviceDescriptor->interruptGroupId_;
266     deviceInfo.volumeGroupId_ = audioDeviceDescriptor->volumeGroupId_;
267     deviceInfo.networkId_ = audioDeviceDescriptor->networkId_;
268     deviceInfo.displayName_ = audioDeviceDescriptor->displayName_;
269     deviceInfo.audioStreamInfo_.samplingRate = audioDeviceDescriptor->audioStreamInfo_.samplingRate;
270     deviceInfo.audioStreamInfo_.encoding = audioDeviceDescriptor->audioStreamInfo_.encoding;
271     deviceInfo.audioStreamInfo_.format = audioDeviceDescriptor->audioStreamInfo_.format;
272     deviceInfo.audioStreamInfo_.channels = audioDeviceDescriptor->audioStreamInfo_.channels;
273 }
274 
FreeCDeviceDescriptor(CDeviceDescriptor & device)275 void FreeCDeviceDescriptor(CDeviceDescriptor &device)
276 {
277     free(device.address);
278     device.address = nullptr;
279     free(device.displayName);
280     device.displayName = nullptr;
281     free(device.name);
282     device.name = nullptr;
283     if (device.channelCounts.size != 0) {
284         free(device.channelCounts.head);
285     }
286     device.channelCounts.head = nullptr;
287     if (device.channelMasks.size != 0) {
288         free(device.channelMasks.head);
289     }
290     device.channelMasks.head = nullptr;
291     if (device.sampleRates.size != 0) {
292         free(device.sampleRates.head);
293     }
294     device.sampleRates.head = nullptr;
295     if (device.encodingTypes.hasValue && device.encodingTypes.arr.size != 0) {
296         free(device.encodingTypes.arr.head);
297     }
298     device.encodingTypes.arr.head = nullptr;
299 }
300 
FreeCArrDeviceDescriptor(CArrDeviceDescriptor & devices)301 void FreeCArrDeviceDescriptor(CArrDeviceDescriptor &devices)
302 {
303     if (devices.head == nullptr) {
304         return;
305     }
306     for (int64_t i = 0; i < devices.size; i++) {
307         FreeCDeviceDescriptor((devices.head)[i]);
308     }
309     free(devices.head);
310     devices.head = nullptr;
311 }
312 
FreeCArrAudioCapturerChangeInfo(CArrAudioCapturerChangeInfo & infos)313 void FreeCArrAudioCapturerChangeInfo(CArrAudioCapturerChangeInfo &infos)
314 {
315     if (infos.head == nullptr) {
316         return;
317     }
318     for (int64_t i = 0; i < infos.size; i++) {
319         FreeCArrDeviceDescriptor((infos.head)[i].deviceDescriptors);
320     }
321     free(infos.head);
322     infos.head = nullptr;
323 }
324 
FreeCArrAudioRendererChangeInfo(CArrAudioRendererChangeInfo & infos)325 void FreeCArrAudioRendererChangeInfo(CArrAudioRendererChangeInfo &infos)
326 {
327     if (infos.head == nullptr) {
328         return;
329     }
330     for (int64_t i = 0; i < infos.size; i++) {
331         FreeCArrDeviceDescriptor((infos.head)[i].deviceDescriptors);
332     }
333     free(infos.head);
334     infos.head = nullptr;
335 }
336 
Convert2AudioRendererOptions(AudioRendererOptions & opions,const CAudioRendererOptions & cOptions)337 void Convert2AudioRendererOptions(AudioRendererOptions &opions, const CAudioRendererOptions &cOptions)
338 {
339     opions.rendererInfo.streamUsage = static_cast<StreamUsage>(cOptions.audioRendererInfo.usage);
340     opions.streamInfo.channels = static_cast<AudioChannel>(cOptions.audioStreamInfo.channels);
341     opions.streamInfo.channelLayout = static_cast<AudioChannelLayout>(cOptions.audioStreamInfo.channelLayout);
342     opions.streamInfo.encoding = static_cast<AudioEncodingType>(cOptions.audioStreamInfo.encodingType);
343     opions.streamInfo.format = static_cast<AudioSampleFormat>(cOptions.audioStreamInfo.sampleFormat);
344     opions.streamInfo.samplingRate = static_cast<AudioSamplingRate>(cOptions.audioStreamInfo.samplingRate);
345     opions.privacyType = static_cast<AudioPrivacyType>(cOptions.privacyType);
346 
347     /* only support flag 0 */
348     opions.rendererInfo.rendererFlags =
349         (cOptions.audioRendererInfo.rendererFlags != 0) ? 0 : cOptions.audioRendererInfo.rendererFlags;
350 }
351 
Convert2AudioRendererInfo(CAudioRendererInfo & cInfo,const AudioRendererInfo & rendererInfo)352 void Convert2AudioRendererInfo(CAudioRendererInfo &cInfo, const AudioRendererInfo &rendererInfo)
353 {
354     cInfo.usage = static_cast<int32_t>(rendererInfo.streamUsage);
355     cInfo.rendererFlags = rendererInfo.rendererFlags;
356 }
357 
Convert2CAudioRendererChangeInfo(CAudioRendererChangeInfo & cInfo,const AudioRendererChangeInfo & changeInfo,int32_t * errorCode)358 void Convert2CAudioRendererChangeInfo(CAudioRendererChangeInfo &cInfo, const AudioRendererChangeInfo &changeInfo,
359     int32_t *errorCode)
360 {
361     cInfo.streamId = changeInfo.sessionId;
362     Convert2CArrDeviceDescriptorByDeviceInfo(cInfo.deviceDescriptors, changeInfo.outputDeviceInfo, errorCode);
363     Convert2AudioRendererInfo(cInfo.rendererInfo, changeInfo.rendererInfo);
364 }
365 
FreeBufferDesc(BufferDesc & buf)366 void FreeBufferDesc(BufferDesc &buf)
367 {
368     if (buf.buffer != nullptr) {
369         free(buf.buffer);
370     }
371     if (buf.metaBuffer != nullptr) {
372         free(buf.metaBuffer);
373     }
374     buf.buffer = nullptr;
375     buf.metaBuffer = nullptr;
376 }
377 } // namespace AudioStandard
378 } // namespace OHOS
379