• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_capture_vendor.h"
17 
18 #include <hdf_base.h>
19 #include <limits.h>
20 #include "audio_common_vendor.h"
21 #include "audio_uhdf_log.h"
22 #include "osal_mem.h"
23 #include "securec.h"
24 
25 #define HDF_LOG_TAG    HDF_AUDIO_PRIMARY_IMPL
26 
27 struct AudioCaptureInfo {
28     struct IAudioCapture capture;
29     struct AudioDeviceDescriptor desc;
30     enum AudioCategory streamType;
31     unsigned int sampleRate;
32     unsigned int channelCount;
33     int sourceType;
34     struct AudioHwiCapture *hwiCapture;
35     uint32_t captureId;
36     unsigned int usrCount;
37 };
38 
39 struct AudioHwiCapturePriv {
40     struct AudioCaptureInfo *captureInfos[AUDIO_HW_STREAM_NUM_MAX];
41     uint32_t captureCnt;
42 };
43 
44 static struct AudioHwiCapturePriv g_audioHwiCapturePriv;
45 
AudioHwiCaptureGetPriv(void)46 static struct AudioHwiCapturePriv *AudioHwiCaptureGetPriv(void)
47 {
48     return &g_audioHwiCapturePriv;
49 }
50 
51 
AudioHwiGetHwiCaptureById(uint32_t captureId)52 struct AudioHwiCapture *AudioHwiGetHwiCaptureById(uint32_t captureId)
53 {
54     struct AudioHwiCapturePriv *priv = AudioHwiCaptureGetPriv();
55     if (priv->captureInfos[captureId] == NULL) {
56         AUDIO_FUNC_LOGE("not match capture");
57         return NULL;
58     }
59 
60     return priv->captureInfos[captureId]->hwiCapture;
61 }
62 
AudioHwiCaptureFrame(struct IAudioCapture * capture,int8_t * frame,uint32_t * frameLen,uint64_t * replyBytes)63 int32_t AudioHwiCaptureFrame(struct IAudioCapture *capture, int8_t *frame, uint32_t *frameLen, uint64_t *replyBytes)
64 {
65     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
66     CHECK_NULL_PTR_RETURN_VALUE(frame, HDF_ERR_INVALID_PARAM);
67     CHECK_NULL_PTR_RETURN_VALUE(frameLen, HDF_ERR_INVALID_PARAM);
68     CHECK_NULL_PTR_RETURN_VALUE(replyBytes, HDF_ERR_INVALID_PARAM);
69 
70     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
71     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
72     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
73     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->CaptureFrame, HDF_ERR_INVALID_PARAM);
74 
75     int32_t ret = hwiCapture->CaptureFrame(hwiCapture, frame, *frameLen, replyBytes);
76     if (ret != HDF_SUCCESS) {
77         AUDIO_FUNC_LOGE("audio capture frame fail, ret=%{public}d", ret);
78         return ret;
79     }
80 
81     return HDF_SUCCESS;
82 }
83 
AudioHwiGetCapturePosition(struct IAudioCapture * capture,uint64_t * frames,struct AudioTimeStamp * time)84 int32_t AudioHwiGetCapturePosition(struct IAudioCapture *capture, uint64_t *frames, struct AudioTimeStamp *time)
85 {
86     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
87     CHECK_NULL_PTR_RETURN_VALUE(frames, HDF_ERR_INVALID_PARAM);
88     CHECK_NULL_PTR_RETURN_VALUE(time, HDF_ERR_INVALID_PARAM);
89 
90     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
91     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
92     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
93     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->GetCapturePosition, HDF_ERR_INVALID_PARAM);
94 
95     int32_t ret = hwiCapture->GetCapturePosition(hwiCapture, frames, (struct AudioHwiTimeStamp *)time);
96     if (ret != HDF_SUCCESS) {
97         AUDIO_FUNC_LOGE("audio capture get position fail, ret=%{public}d", ret);
98         return ret;
99     }
100 
101     return HDF_SUCCESS;
102 }
103 
AudioHwiCaptureCheckSceneCapability(struct IAudioCapture * capture,const struct AudioSceneDescriptor * scene,bool * supported)104 int32_t AudioHwiCaptureCheckSceneCapability(struct IAudioCapture *capture, const struct AudioSceneDescriptor* scene,
105     bool* supported)
106 {
107     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
108     CHECK_NULL_PTR_RETURN_VALUE(scene, HDF_ERR_INVALID_PARAM);
109     CHECK_NULL_PTR_RETURN_VALUE(supported, HDF_ERR_INVALID_PARAM);
110 
111     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
112     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
113     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
114     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->scene.CheckSceneCapability, HDF_ERR_INVALID_PARAM);
115 
116     struct AudioHwiSceneDescriptor hwiScene;
117     (void)memset_s((void *)&hwiScene, sizeof(hwiScene), 0, sizeof(hwiScene));
118     int32_t ret = AudioHwiCommonSceneToHwiScene(scene, &hwiScene);
119     if (ret != HDF_SUCCESS) {
120         AUDIO_FUNC_LOGE("audio capture scene To hwiScene fail");
121         return HDF_FAILURE;
122     }
123 
124     ret = hwiCapture->scene.CheckSceneCapability(hwiCapture, &hwiScene, supported);
125     OsalMemFree((void *)hwiScene.desc.desc);
126     if (ret != HDF_SUCCESS) {
127         AUDIO_FUNC_LOGE("audio capture CheckSceneCapability fail, ret=%{public}d", ret);
128         return ret;
129     }
130 
131     return HDF_SUCCESS;
132 }
133 
AudioHwiCaptureSelectScene(struct IAudioCapture * capture,const struct AudioSceneDescriptor * scene)134 int32_t AudioHwiCaptureSelectScene(struct IAudioCapture *capture, const struct AudioSceneDescriptor* scene)
135 {
136     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
137     CHECK_NULL_PTR_RETURN_VALUE(scene, HDF_ERR_INVALID_PARAM);
138 
139     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
140     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
141     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
142     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->scene.SelectScene, HDF_ERR_INVALID_PARAM);
143 
144     struct AudioHwiSceneDescriptor hwiScene;
145     (void)memset_s((void *)&hwiScene, sizeof(hwiScene), 0, sizeof(hwiScene));
146     int32_t ret = AudioHwiCommonSceneToHwiScene(scene, &hwiScene);
147     if (ret != HDF_SUCCESS) {
148         AUDIO_FUNC_LOGE("audio hwiAdapter scene To hwiScene fail");
149         return HDF_FAILURE;
150     }
151 
152     ret = hwiCapture->scene.SelectScene(hwiCapture, &hwiScene);
153     OsalMemFree((void *)hwiScene.desc.desc);
154     if (ret != HDF_SUCCESS) {
155         AUDIO_FUNC_LOGE("audio capture select scene fail, ret=%{public}d", ret);
156         return ret;
157     }
158 
159     return HDF_SUCCESS;
160 }
161 
AudioHwiCaptureSetMute(struct IAudioCapture * capture,bool mute)162 int32_t AudioHwiCaptureSetMute(struct IAudioCapture *capture, bool mute)
163 {
164     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
165 
166     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
167     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
168     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
169     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->volume.SetMute, HDF_ERR_INVALID_PARAM);
170 
171     int32_t ret = hwiCapture->volume.SetMute(hwiCapture, mute);
172     if (ret != HDF_SUCCESS) {
173         AUDIO_FUNC_LOGE("audio capture SetMute fail, ret=%{public}d", ret);
174         return ret;
175     }
176 
177     return HDF_SUCCESS;
178 }
179 
AudioHwiCaptureGetMute(struct IAudioCapture * capture,bool * mute)180 int32_t AudioHwiCaptureGetMute(struct IAudioCapture *capture, bool *mute)
181 {
182     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
183     CHECK_NULL_PTR_RETURN_VALUE(mute, HDF_ERR_INVALID_PARAM);
184 
185     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
186     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
187     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
188     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->volume.GetMute, HDF_ERR_INVALID_PARAM);
189 
190     int32_t ret = hwiCapture->volume.GetMute(hwiCapture, mute);
191     if (ret != HDF_SUCCESS) {
192         AUDIO_FUNC_LOGE("audio capture GetMute fail, ret=%{public}d", ret);
193         return ret;
194     }
195 
196     return HDF_SUCCESS;
197 }
198 
AudioHwiCaptureSetVolume(struct IAudioCapture * capture,float volume)199 int32_t AudioHwiCaptureSetVolume(struct IAudioCapture *capture, float volume)
200 {
201     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
202 
203     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
204     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
205     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
206     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->volume.SetVolume, HDF_ERR_INVALID_PARAM);
207 
208     int32_t ret = hwiCapture->volume.SetVolume(hwiCapture, volume);
209     if (ret != HDF_SUCCESS) {
210         AUDIO_FUNC_LOGE("audio capture SetVolume fail, ret=%{public}d", ret);
211         return ret;
212     }
213 
214     return HDF_SUCCESS;
215 }
216 
AudioHwiCaptureGetVolume(struct IAudioCapture * capture,float * volume)217 int32_t AudioHwiCaptureGetVolume(struct IAudioCapture *capture, float *volume)
218 {
219     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
220     CHECK_NULL_PTR_RETURN_VALUE(volume, HDF_ERR_INVALID_PARAM);
221 
222     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
223     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
224     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
225     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->volume.GetVolume, HDF_ERR_INVALID_PARAM);
226 
227     int32_t ret = hwiCapture->volume.GetVolume(hwiCapture, volume);
228     if (ret != HDF_SUCCESS) {
229         AUDIO_FUNC_LOGE("audio capture GetVolume fail, ret=%{public}d", ret);
230         return ret;
231     }
232 
233     return HDF_SUCCESS;
234 }
235 
AudioHwiCaptureGetGainThreshold(struct IAudioCapture * capture,float * min,float * max)236 int32_t AudioHwiCaptureGetGainThreshold(struct IAudioCapture *capture, float *min, float *max)
237 {
238     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
239     CHECK_NULL_PTR_RETURN_VALUE(min, HDF_ERR_INVALID_PARAM);
240     CHECK_NULL_PTR_RETURN_VALUE(max, HDF_ERR_INVALID_PARAM);
241 
242     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
243     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
244     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
245     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->volume.GetGainThreshold, HDF_ERR_INVALID_PARAM);
246 
247     int32_t ret = hwiCapture->volume.GetGainThreshold(hwiCapture, min, max);
248     if (ret != HDF_SUCCESS) {
249         AUDIO_FUNC_LOGE("audio capture GetGainThreshold fail, ret=%{public}d", ret);
250         return ret;
251     }
252 
253     return HDF_SUCCESS;
254 }
255 
AudioHwiCaptureGetGain(struct IAudioCapture * capture,float * gain)256 int32_t AudioHwiCaptureGetGain(struct IAudioCapture *capture, float *gain)
257 {
258     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
259     CHECK_NULL_PTR_RETURN_VALUE(gain, HDF_ERR_INVALID_PARAM);
260 
261     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
262     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
263     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
264     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->volume.GetGain, HDF_ERR_INVALID_PARAM);
265 
266     int32_t ret = hwiCapture->volume.GetGain(hwiCapture, gain);
267     if (ret != HDF_SUCCESS) {
268         AUDIO_FUNC_LOGE("audio capture GetGain fail, ret=%{public}d", ret);
269         return ret;
270     }
271 
272     return HDF_SUCCESS;
273 }
274 
AudioHwiCaptureSetGain(struct IAudioCapture * capture,float gain)275 int32_t AudioHwiCaptureSetGain(struct IAudioCapture *capture, float gain)
276 {
277     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
278 
279     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
280     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
281     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
282     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->volume.SetGain, HDF_ERR_INVALID_PARAM);
283 
284     int32_t ret = hwiCapture->volume.SetGain(hwiCapture, gain);
285     if (ret != HDF_SUCCESS) {
286         AUDIO_FUNC_LOGE("audio capture SetGain fail, ret=%{public}d", ret);
287         return ret;
288     }
289 
290     return HDF_SUCCESS;
291 }
292 
AudioHwiCaptureGetFrameSize(struct IAudioCapture * capture,uint64_t * size)293 int32_t AudioHwiCaptureGetFrameSize(struct IAudioCapture *capture, uint64_t *size)
294 {
295     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
296     CHECK_NULL_PTR_RETURN_VALUE(size, HDF_ERR_INVALID_PARAM);
297 
298     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
299     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
300     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
301     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->attr.GetFrameSize, HDF_ERR_INVALID_PARAM);
302 
303     int32_t ret = hwiCapture->attr.GetFrameSize(hwiCapture, size);
304     if (ret != HDF_SUCCESS) {
305         AUDIO_FUNC_LOGE("audio capture GetFrameSize fail, ret=%{public}d", ret);
306         return ret;
307     }
308 
309     return HDF_SUCCESS;
310 }
311 
AudioHwiCaptureGetFrameCount(struct IAudioCapture * capture,uint64_t * count)312 int32_t AudioHwiCaptureGetFrameCount(struct IAudioCapture *capture, uint64_t *count)
313 {
314     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
315     CHECK_NULL_PTR_RETURN_VALUE(count, HDF_ERR_INVALID_PARAM);
316 
317     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
318     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
319     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
320     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->attr.GetFrameCount, HDF_ERR_INVALID_PARAM);
321 
322     int32_t ret = hwiCapture->attr.GetFrameCount(hwiCapture, count);
323     if (ret != HDF_SUCCESS) {
324         AUDIO_FUNC_LOGE("audio capture GetFrameCount fail, ret=%{public}d", ret);
325         return ret;
326     }
327 
328     return HDF_SUCCESS;
329 }
330 
AudioHwiCaptureSetSampleAttributes(struct IAudioCapture * capture,const struct AudioSampleAttributes * attrs)331 int32_t AudioHwiCaptureSetSampleAttributes(struct IAudioCapture *capture, const struct AudioSampleAttributes *attrs)
332 {
333     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
334     CHECK_NULL_PTR_RETURN_VALUE(attrs, HDF_ERR_INVALID_PARAM);
335 
336     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
337     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
338     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
339     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->attr.SetSampleAttributes, HDF_ERR_INVALID_PARAM);
340 
341     struct AudioHwiSampleAttributes hwiAttrs;
342     (void)memset_s((void *)&hwiAttrs, sizeof(hwiAttrs), 0, sizeof(hwiAttrs));
343     int32_t ret = AudioHwiCommonSampleAttrToHwiSampleAttr(attrs, &hwiAttrs);
344     if (ret != HDF_SUCCESS) {
345         AUDIO_FUNC_LOGE("audio capture SampleAttr to hwisampleAttr fail, ret=%{public}d", ret);
346         return ret;
347     }
348 
349     ret = hwiCapture->attr.SetSampleAttributes(hwiCapture, &hwiAttrs);
350     if (ret != HDF_SUCCESS) {
351         AUDIO_FUNC_LOGE("audio capture SetSampleAttributes fail, ret=%{public}d", ret);
352         return ret;
353     }
354 
355     return HDF_SUCCESS;
356 }
357 
AudioHwiCaptureGetSampleAttributes(struct IAudioCapture * capture,struct AudioSampleAttributes * attrs)358 int32_t AudioHwiCaptureGetSampleAttributes(struct IAudioCapture *capture, struct AudioSampleAttributes *attrs)
359 {
360     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
361     CHECK_NULL_PTR_RETURN_VALUE(attrs, HDF_ERR_INVALID_PARAM);
362 
363     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
364     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
365     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
366     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->attr.GetSampleAttributes, HDF_ERR_INVALID_PARAM);
367 
368     struct AudioHwiSampleAttributes hwiAttrs;
369     (void)memset_s((void *)&hwiAttrs, sizeof(hwiAttrs), 0, sizeof(hwiAttrs));
370     int32_t ret = hwiCapture->attr.GetSampleAttributes(hwiCapture, &hwiAttrs);
371     if (ret != HDF_SUCCESS) {
372         AUDIO_FUNC_LOGE("audio capture GetSampleAttributes fail, ret=%{public}d", ret);
373         return ret;
374     }
375 
376     ret = AudioHwiCommonHwiSampleAttrToSampleAttr(&hwiAttrs, attrs);
377     if (ret != HDF_SUCCESS) {
378         AUDIO_FUNC_LOGE("audio capture hwiSampleAttr to SampleAttr fail, ret=%{public}d", ret);
379         return ret;
380     }
381 
382     return HDF_SUCCESS;
383 }
384 
AudioHwiCaptureGetCurrentChannelId(struct IAudioCapture * capture,uint32_t * channelId)385 int32_t AudioHwiCaptureGetCurrentChannelId(struct IAudioCapture *capture, uint32_t *channelId)
386 {
387     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
388     CHECK_NULL_PTR_RETURN_VALUE(channelId, HDF_ERR_INVALID_PARAM);
389 
390     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
391     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
392     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
393     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->attr.GetCurrentChannelId, HDF_ERR_INVALID_PARAM);
394 
395     int32_t ret = hwiCapture->attr.GetCurrentChannelId(hwiCapture, channelId);
396     if (ret != HDF_SUCCESS) {
397         AUDIO_FUNC_LOGE("audio capture GetCurrentChannelId fail, ret=%{public}d", ret);
398         return ret;
399     }
400 
401     return HDF_SUCCESS;
402 }
403 
AudioHwiCaptureSetExtraParams(struct IAudioCapture * capture,const char * keyValueList)404 int32_t AudioHwiCaptureSetExtraParams(struct IAudioCapture *capture, const char *keyValueList)
405 {
406     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
407     CHECK_NULL_PTR_RETURN_VALUE(keyValueList, HDF_ERR_INVALID_PARAM);
408 
409     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
410     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
411     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
412     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->attr.SetExtraParams, HDF_ERR_INVALID_PARAM);
413 
414     int32_t ret = hwiCapture->attr.SetExtraParams(hwiCapture, keyValueList);
415     if (ret != HDF_SUCCESS) {
416         AUDIO_FUNC_LOGE("audio capture SetExtraParams fail, ret=%{public}d", ret);
417         return ret;
418     }
419 
420     return HDF_SUCCESS;
421 }
422 
AudioHwiCaptureGetExtraParams(struct IAudioCapture * capture,char * keyValueList,uint32_t keyValueListLen)423 int32_t AudioHwiCaptureGetExtraParams(struct IAudioCapture *capture, char *keyValueList, uint32_t keyValueListLen)
424 {
425     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
426     CHECK_NULL_PTR_RETURN_VALUE(keyValueList, HDF_ERR_INVALID_PARAM);
427 
428     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
429     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
430     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
431     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->attr.GetExtraParams, HDF_ERR_INVALID_PARAM);
432 
433     int32_t ret = hwiCapture->attr.GetExtraParams(hwiCapture, keyValueList, keyValueListLen);
434     if (ret != HDF_SUCCESS) {
435         AUDIO_FUNC_LOGE("audio capture GetExtraParams fail, ret=%{public}d", ret);
436         return ret;
437     }
438 
439     return HDF_SUCCESS;
440 }
441 
AudioHwiCaptureReqMmapBuffer(struct IAudioCapture * capture,int32_t reqSize,struct AudioMmapBufferDescriptor * desc)442 int32_t AudioHwiCaptureReqMmapBuffer(struct IAudioCapture *capture, int32_t reqSize,
443     struct AudioMmapBufferDescriptor *desc)
444 {
445     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
446     CHECK_NULL_PTR_RETURN_VALUE(desc, HDF_ERR_INVALID_PARAM);
447 
448     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
449     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
450     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
451     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->attr.ReqMmapBuffer, HDF_ERR_INVALID_PARAM);
452 
453     struct AudioHwiMmapBufferDescriptor hwiDesc = {0};
454     int32_t ret = hwiCapture->attr.ReqMmapBuffer(hwiCapture, reqSize, &hwiDesc);
455     if (ret != HDF_SUCCESS) {
456         AUDIO_FUNC_LOGE("audio capture ReqMmapBuffer fail, ret=%{pubilc}d", ret);
457         return ret;
458     }
459 
460     desc->memoryFd = hwiDesc.memoryFd;
461     desc->totalBufferFrames = hwiDesc.totalBufferFrames;
462     desc->transferFrameSize = hwiDesc.transferFrameSize;
463     desc->isShareable = hwiDesc.isShareable;
464     desc->filePath = strdup("");  // which will be released after send reply
465     if (desc->totalBufferFrames < 0) {
466         // make the totalBufferFrames valid
467         desc->totalBufferFrames *= -1;
468         desc->isShareable = 1;
469     }
470 
471     return HDF_SUCCESS;
472 }
473 
AudioHwiCaptureGetMmapPosition(struct IAudioCapture * capture,uint64_t * frames,struct AudioTimeStamp * time)474 int32_t AudioHwiCaptureGetMmapPosition(struct IAudioCapture *capture, uint64_t *frames, struct AudioTimeStamp *time)
475 {
476     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
477     CHECK_NULL_PTR_RETURN_VALUE(frames, HDF_ERR_INVALID_PARAM);
478     CHECK_NULL_PTR_RETURN_VALUE(time, HDF_ERR_INVALID_PARAM);
479 
480     struct AudioHwiTimeStamp hwiTime;
481     hwiTime.tvSec = 0;
482     hwiTime.tvNSec = 0;
483 
484     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
485     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
486     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
487     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->attr.GetMmapPosition, HDF_ERR_INVALID_PARAM);
488 
489     int32_t ret = hwiCapture->attr.GetMmapPosition(hwiCapture, frames, &hwiTime);
490     if (ret != HDF_SUCCESS) {
491         AUDIO_FUNC_LOGE("audio capture GetMmapPosition fail, ret=%{public}d", ret);
492         return ret;
493     }
494 
495     time->tvSec = hwiTime.tvSec;
496     time->tvNSec = hwiTime.tvNSec;
497 
498     return HDF_SUCCESS;
499 }
500 
AudioHwiCaptureAddAudioEffect(struct IAudioCapture * capture,uint64_t effectid)501 int32_t AudioHwiCaptureAddAudioEffect(struct IAudioCapture *capture, uint64_t effectid)
502 {
503     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
504 
505     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
506     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
507     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
508     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->attr.AddAudioEffect, HDF_ERR_INVALID_PARAM);
509 
510     int32_t ret = hwiCapture->attr.AddAudioEffect(hwiCapture, effectid);
511     if (ret != HDF_SUCCESS) {
512         AUDIO_FUNC_LOGE("audio capture AddAudioEffect fail, ret=%{public}d", ret);
513         return ret;
514     }
515 
516     return HDF_SUCCESS;
517 }
518 
AudioHwiCaptureRemoveAudioEffect(struct IAudioCapture * capture,uint64_t effectid)519 int32_t AudioHwiCaptureRemoveAudioEffect(struct IAudioCapture *capture, uint64_t effectid)
520 {
521     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
522 
523     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
524     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
525     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
526     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->attr.RemoveAudioEffect, HDF_ERR_INVALID_PARAM);
527 
528     int32_t ret = hwiCapture->attr.RemoveAudioEffect(hwiCapture, effectid);
529     if (ret != HDF_SUCCESS) {
530         AUDIO_FUNC_LOGE("audio capture RemoveAudioEffect fail, ret=%{public}d", ret);
531         return ret;
532     }
533 
534     return HDF_SUCCESS;
535 }
536 
AudioHwiCaptureGetFrameBufferSize(struct IAudioCapture * capture,uint64_t * bufferSize)537 int32_t AudioHwiCaptureGetFrameBufferSize(struct IAudioCapture *capture, uint64_t *bufferSize)
538 {
539     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
540     CHECK_NULL_PTR_RETURN_VALUE(bufferSize, HDF_ERR_INVALID_PARAM);
541 
542     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
543     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
544     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
545     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->attr.GetFrameBufferSize, HDF_ERR_INVALID_PARAM);
546 
547     int32_t ret = hwiCapture->attr.GetFrameBufferSize(hwiCapture, bufferSize);
548     if (ret != HDF_SUCCESS) {
549         AUDIO_FUNC_LOGE("audio capture GetFrameBufferSize fail, ret=%{public}d", ret);
550         return ret;
551     }
552 
553     return HDF_SUCCESS;
554 }
555 
AudioHwiCaptureStart(struct IAudioCapture * capture)556 int32_t AudioHwiCaptureStart(struct IAudioCapture *capture)
557 {
558     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
559 
560     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
561     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
562     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
563     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->control.Start, HDF_ERR_INVALID_PARAM);
564 
565     int32_t ret = hwiCapture->control.Start(hwiCapture);
566     if (ret != HDF_SUCCESS) {
567         AUDIO_FUNC_LOGE("audio capture Start fail, ret=%{public}d", ret);
568         return ret;
569     }
570 
571     return HDF_SUCCESS;
572 }
573 
AudioHwiCaptureStop(struct IAudioCapture * capture)574 int32_t AudioHwiCaptureStop(struct IAudioCapture *capture)
575 {
576     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
577 
578     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
579     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
580     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
581     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->control.Stop, HDF_ERR_INVALID_PARAM);
582 
583     int32_t ret = hwiCapture->control.Stop(hwiCapture);
584     if (ret != HDF_SUCCESS) {
585         AUDIO_FUNC_LOGE("audio capture Stop fail, ret=%{public}d", ret);
586         return HDF_ERR_NOT_SUPPORT;
587     }
588 
589     return HDF_SUCCESS;
590 }
591 
AudioHwiCapturePause(struct IAudioCapture * capture)592 int32_t AudioHwiCapturePause(struct IAudioCapture *capture)
593 {
594     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
595 
596     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
597     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
598     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
599     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->control.Pause, HDF_ERR_INVALID_PARAM);
600 
601     int32_t ret = hwiCapture->control.Pause(hwiCapture);
602     if (ret != HDF_SUCCESS) {
603         AUDIO_FUNC_LOGE("audio capture Pause fail, ret=%{public}d", ret);
604         return ret;
605     }
606 
607     return HDF_SUCCESS;
608 }
609 
AudioHwiCaptureResume(struct IAudioCapture * capture)610 int32_t AudioHwiCaptureResume(struct IAudioCapture *capture)
611 {
612     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
613 
614     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
615     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
616     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
617     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->control.Resume, HDF_ERR_INVALID_PARAM);
618 
619     int32_t ret = hwiCapture->control.Resume(hwiCapture);
620     if (ret != HDF_SUCCESS) {
621         AUDIO_FUNC_LOGE("audio capture Resume fail, ret=%{public}d", ret);
622         return ret;
623     }
624 
625     return HDF_SUCCESS;
626 }
627 
AudioHwiCaptureFlush(struct IAudioCapture * capture)628 int32_t AudioHwiCaptureFlush(struct IAudioCapture *capture)
629 {
630     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
631 
632     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
633     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
634     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
635     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->control.Flush, HDF_ERR_INVALID_PARAM);
636 
637     int32_t ret = hwiCapture->control.Flush(hwiCapture);
638     if (ret != HDF_SUCCESS) {
639         AUDIO_FUNC_LOGE("audio capture Flush fail, ret=%{public}d", ret);
640         return ret;
641     }
642 
643     return HDF_SUCCESS;
644 }
645 
AudioHwiCaptureTurnStandbyMode(struct IAudioCapture * capture)646 int32_t AudioHwiCaptureTurnStandbyMode(struct IAudioCapture *capture)
647 {
648     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
649 
650     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
651     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
652     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
653     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->control.TurnStandbyMode, HDF_ERR_INVALID_PARAM);
654 
655     int32_t ret = hwiCapture->control.TurnStandbyMode(hwiCapture);
656     if (ret != HDF_SUCCESS) {
657         AUDIO_FUNC_LOGE("audio capture TurnStandbyMode fail, ret=%{public}d", ret);
658         return ret;
659     }
660 
661     return HDF_SUCCESS;
662 }
663 
AudioHwiCaptureAudioDevDump(struct IAudioCapture * capture,int32_t range,int32_t fd)664 int32_t AudioHwiCaptureAudioDevDump(struct IAudioCapture *capture, int32_t range, int32_t fd)
665 {
666     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
667 
668     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
669     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
670     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
671     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->control.AudioDevDump, HDF_ERR_INVALID_PARAM);
672 
673     int32_t ret = hwiCapture->control.AudioDevDump(hwiCapture, range, fd);
674     if (ret != HDF_SUCCESS) {
675         AUDIO_FUNC_LOGE("audio capture AudioDevDump fail, ret=%{public}d", ret);
676         return ret;
677     }
678 
679     return HDF_SUCCESS;
680 }
681 
AudioHwiCaptureIsSupportsPauseAndResume(struct IAudioCapture * capture,bool * supportPause,bool * supportResume)682 int32_t AudioHwiCaptureIsSupportsPauseAndResume(struct IAudioCapture *capture, bool *supportPause, bool *supportResume)
683 {
684     CHECK_NULL_PTR_RETURN_VALUE(capture, HDF_ERR_INVALID_PARAM);
685     CHECK_NULL_PTR_RETURN_VALUE(supportPause, HDF_ERR_INVALID_PARAM);
686     CHECK_NULL_PTR_RETURN_VALUE(supportResume, HDF_ERR_INVALID_PARAM);
687 
688     struct AudioCaptureInfo *captureInfo = (struct AudioCaptureInfo *)(capture);
689     struct AudioHwiCapture *hwiCapture = captureInfo->hwiCapture;
690     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture, HDF_ERR_INVALID_PARAM);
691     CHECK_NULL_PTR_RETURN_VALUE(hwiCapture->control.IsSupportsPauseAndResume, HDF_ERR_INVALID_PARAM);
692 
693     int32_t ret = hwiCapture->control.IsSupportsPauseAndResume(hwiCapture, supportPause, supportResume);
694     if (ret != HDF_SUCCESS) {
695         AUDIO_FUNC_LOGE("audio capture IsSupportsPauseAndResume fail, ret=%{public}d", ret);
696         return ret;
697     }
698 
699     return HDF_SUCCESS;
700 }
701 
AudioHwiInitCaptureInstance(struct IAudioCapture * capture)702 static void AudioHwiInitCaptureInstance(struct IAudioCapture *capture)
703 {
704     capture->CaptureFrame = AudioHwiCaptureFrame;
705     capture->GetCapturePosition = AudioHwiGetCapturePosition;
706     capture->CheckSceneCapability = AudioHwiCaptureCheckSceneCapability;
707     capture->SelectScene = AudioHwiCaptureSelectScene;
708     capture->SetMute = AudioHwiCaptureSetMute;
709     capture->GetMute = AudioHwiCaptureGetMute;
710     capture->SetVolume = AudioHwiCaptureSetVolume;
711     capture->GetVolume = AudioHwiCaptureGetVolume;
712     capture->GetGainThreshold = AudioHwiCaptureGetGainThreshold;
713     capture->GetGain = AudioHwiCaptureGetGain;
714     capture->SetGain = AudioHwiCaptureSetGain;
715     capture->GetFrameSize = AudioHwiCaptureGetFrameSize;
716     capture->GetFrameCount = AudioHwiCaptureGetFrameCount;
717     capture->SetSampleAttributes = AudioHwiCaptureSetSampleAttributes;
718     capture->GetSampleAttributes = AudioHwiCaptureGetSampleAttributes;
719     capture->GetCurrentChannelId = AudioHwiCaptureGetCurrentChannelId;
720     capture->SetExtraParams = AudioHwiCaptureSetExtraParams;
721     capture->GetExtraParams = AudioHwiCaptureGetExtraParams;
722     capture->ReqMmapBuffer = AudioHwiCaptureReqMmapBuffer;
723     capture->GetMmapPosition = AudioHwiCaptureGetMmapPosition;
724     capture->AddAudioEffect = AudioHwiCaptureAddAudioEffect;
725     capture->RemoveAudioEffect = AudioHwiCaptureRemoveAudioEffect;
726     capture->GetFrameBufferSize = AudioHwiCaptureGetFrameBufferSize;
727     capture->Start = AudioHwiCaptureStart;
728     capture->Stop = AudioHwiCaptureStop;
729     capture->Pause = AudioHwiCapturePause;
730     capture->Resume = AudioHwiCaptureResume;
731     capture->Flush = AudioHwiCaptureFlush;
732     capture->TurnStandbyMode = AudioHwiCaptureTurnStandbyMode;
733     capture->AudioDevDump = AudioHwiCaptureAudioDevDump;
734     capture->IsSupportsPauseAndResume = AudioHwiCaptureIsSupportsPauseAndResume;
735 }
736 
JudgeParameters(enum AudioPortPin pin,const struct AudioSampleAttributes * attrs,struct AudioCaptureInfo * captureInfos)737 static int32_t JudgeParameters(enum AudioPortPin pin, const struct AudioSampleAttributes *attrs,
738     struct AudioCaptureInfo *captureInfos)
739 {
740     CHECK_NULL_PTR_RETURN_VALUE(attrs, HDF_ERR_INVALID_PARAM);
741     CHECK_NULL_PTR_RETURN_VALUE(captureInfos, HDF_ERR_INVALID_PARAM);
742 
743     if (captureInfos->desc.pins != pin) {
744         return HDF_FAILURE;
745     }
746     if (captureInfos->streamType != attrs->type) {
747         return HDF_FAILURE;
748     }
749     if (captureInfos->sampleRate != attrs->sampleRate) {
750         return HDF_FAILURE;
751     }
752     if (captureInfos->channelCount != attrs->channelCount) {
753         return HDF_FAILURE;
754     }
755     if (captureInfos->sourceType != attrs->sourceType) {
756         return HDF_FAILURE;
757     }
758     return HDF_SUCCESS;
759 }
760 
FindCaptureCreated(enum AudioPortPin pin,const struct AudioSampleAttributes * attrs,uint32_t * captureId)761 struct IAudioCapture *FindCaptureCreated(enum AudioPortPin pin, const struct AudioSampleAttributes *attrs,
762     uint32_t *captureId)
763 {
764     uint32_t index = 0;
765     if (captureId == NULL || attrs == NULL) {
766         AUDIO_FUNC_LOGE("audio params is null");
767         return NULL;
768     }
769 
770     struct AudioHwiCapturePriv *capturePriv = AudioHwiCaptureGetPriv();
771     if (capturePriv == NULL) {
772         AUDIO_FUNC_LOGE("Parameter error!");
773         return NULL;
774     }
775 
776     if (capturePriv->captureCnt == 0) {
777         AUDIO_FUNC_LOGI("no capture created");
778         return NULL;
779     }
780 
781     for (index = 0; index < AUDIO_HW_STREAM_NUM_MAX; index++) {
782         if (capturePriv->captureInfos[index] == NULL) {
783             continue;
784         }
785 
786         int32_t ret = JudgeParameters(pin, attrs, capturePriv->captureInfos[index]);
787         if (ret != HDF_SUCCESS) {
788             continue;
789         }
790 
791         *captureId = capturePriv->captureInfos[index]->captureId;
792         capturePriv->captureInfos[index]->usrCount++;
793         return &capturePriv->captureInfos[index]->capture;
794     }
795     return NULL;
796 }
797 
GetAvailableCaptureId(struct AudioHwiCapturePriv * capturePriv)798 static uint32_t GetAvailableCaptureId(struct AudioHwiCapturePriv *capturePriv)
799 {
800     uint32_t captureId = AUDIO_HW_STREAM_NUM_MAX;
801     uint32_t index = 0;
802     if (capturePriv == NULL) {
803         AUDIO_FUNC_LOGE("Parameter error!");
804         return captureId;
805     }
806 
807     if (capturePriv->captureCnt < AUDIO_HW_STREAM_NUM_MAX) {
808         captureId = capturePriv->captureCnt;
809         capturePriv->captureCnt++;
810     } else {
811         for (index = 0; index < AUDIO_HW_STREAM_NUM_MAX; index++) {
812             if (capturePriv->captureInfos[index] == NULL) {
813                 captureId = index;
814                 break;
815             }
816         }
817     }
818 
819     return captureId;
820 }
821 
AudioHwiCreateCaptureById(const struct AudioSampleAttributes * attrs,uint32_t * captureId,struct AudioHwiCapture * hwiCapture,const struct AudioDeviceDescriptor * desc)822 struct IAudioCapture *AudioHwiCreateCaptureById(const struct AudioSampleAttributes *attrs, uint32_t *captureId,
823     struct AudioHwiCapture *hwiCapture, const struct AudioDeviceDescriptor *desc)
824 {
825     if (attrs == NULL || captureId == NULL || hwiCapture == NULL || desc == NULL) {
826         AUDIO_FUNC_LOGE("audio capture is null");
827         return NULL;
828     }
829 
830     *captureId = AUDIO_HW_STREAM_NUM_MAX;
831     struct IAudioCapture *capture = NULL;
832     struct AudioHwiCapturePriv *priv = AudioHwiCaptureGetPriv();
833 
834     *captureId = GetAvailableCaptureId(priv);
835     if (*captureId >= AUDIO_HW_STREAM_NUM_MAX) {
836         AUDIO_FUNC_LOGE("audio hwicapture capture capture index fail, captureId=%{public}d", *captureId);
837         return NULL;
838     }
839 
840     priv->captureInfos[*captureId] = (struct AudioCaptureInfo *)OsalMemCalloc(sizeof(struct AudioCaptureInfo));
841     if (priv->captureInfos[*captureId] == NULL) {
842         AUDIO_FUNC_LOGE("audio Hwicapture malloc captureInfos fail");
843         return NULL;
844     }
845 
846     priv->captureInfos[*captureId]->hwiCapture = hwiCapture;
847     priv->captureInfos[*captureId]->streamType = attrs->type;
848     priv->captureInfos[*captureId]->sampleRate = attrs->sampleRate;
849     priv->captureInfos[*captureId]->channelCount = attrs->channelCount;
850     priv->captureInfos[*captureId]->sourceType = attrs->sourceType;
851     priv->captureInfos[*captureId]->desc.portId = desc->portId;
852     priv->captureInfos[*captureId]->desc.pins = desc->pins;
853     priv->captureInfos[*captureId]->desc.desc = strdup(desc->desc);
854     priv->captureInfos[*captureId]->captureId = *captureId;
855     priv->captureInfos[*captureId]->usrCount = 1;
856     capture = &(priv->captureInfos[*captureId]->capture);
857     AudioHwiInitCaptureInstance(capture);
858 
859     AUDIO_FUNC_LOGI("audio create capture success");
860     return capture;
861 };
862 
DecreaseCaptureUsrCount(uint32_t captureId)863 uint32_t DecreaseCaptureUsrCount(uint32_t captureId)
864 {
865     uint32_t usrCnt = 0;
866     if (captureId >= AUDIO_HW_STREAM_NUM_MAX) {
867         AUDIO_FUNC_LOGE("audio check capture index fail, descIndex=%{public}d", captureId);
868         return usrCnt;
869     }
870     struct AudioHwiCapturePriv *priv = AudioHwiCaptureGetPriv();
871     if (priv->captureInfos[captureId] == NULL) {
872         AUDIO_FUNC_LOGE("audio check capture index fail, descIndex=%{public}d", captureId);
873         return usrCnt;
874     }
875 
876     priv->captureInfos[captureId]->usrCount--;
877     usrCnt = priv->captureInfos[captureId]->usrCount;
878     return usrCnt;
879 }
880 
AudioHwiDestroyCaptureById(uint32_t captureId)881 void AudioHwiDestroyCaptureById(uint32_t captureId)
882 {
883     if (captureId >= AUDIO_HW_STREAM_NUM_MAX) {
884         AUDIO_FUNC_LOGE("audio hwiCapture destroy capture index fail, captureId=%{public}d", captureId);
885         return;
886     }
887     struct AudioHwiCapturePriv *priv = AudioHwiCaptureGetPriv();
888     if (priv->captureInfos[captureId] == NULL) {
889         AUDIO_FUNC_LOGE("audio hwiCapture destroy capture index fail, captureId=%{public}d", captureId);
890         return;
891     }
892 
893     OsalMemFree((void *)priv->captureInfos[captureId]->desc.desc);
894     priv->captureInfos[captureId]->hwiCapture = NULL;
895     priv->captureInfos[captureId]->desc.desc = NULL;
896     priv->captureInfos[captureId]->desc.portId = UINT_MAX;
897     priv->captureInfos[captureId]->desc.pins = PIN_NONE;
898 
899     OsalMemFree(priv->captureInfos[captureId]);
900     priv->captureInfos[captureId] = NULL;
901 }
902