1 /*
2 * Copyright (c) 2021 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 /*
17 * @addtogroup Audio
18 * @{
19 *
20 * @brief Test audio-related APIs, including custom data types and functions for loading drivers,
21 * accessing a driver ADM interface lib.
22 *
23 * @since 1.0
24 * @version 1.0
25 */
26
27 /*
28 * @file audio_adm_fuzzer_common.h
29 *
30 * @brief Declares APIs for operations related to the audio ADM interface lib.
31 *
32 * @since 1.0
33 * @version 1.0
34 */
35
36 #include "audio_adm_fuzzer_common.h"
37
38 namespace OHOS {
39 namespace Audio {
InitRenderFramepara(struct AudioFrameRenderMode & frameRenderMode)40 int32_t InitRenderFramepara(struct AudioFrameRenderMode& frameRenderMode)
41 {
42 InitAttrs(frameRenderMode.attrs);
43 frameRenderMode.frames = AUDIO_FORMAT_PCM_16_BIT;
44 frameRenderMode.mode = AUDIO_CHANNEL_BOTH_RIGHT;
45 frameRenderMode.periodSize = G_PERIODSIZE;
46 frameRenderMode.periodCount = G_PERIODCOUNT;
47 frameRenderMode.byteRate = G_BYTERATE;
48 frameRenderMode.bufferFrameSize = G_BUFFERFRAMESIZE;
49 frameRenderMode.bufferSize = G_BUFFERSIZE1;
50 frameRenderMode.buffer = NULL;
51 frameRenderMode.silenceThreshold = frameRenderMode.periodSize * frameRenderMode.periodCount;
52 frameRenderMode.silenceSize = G_SILENCETHRESHOLE;
53 frameRenderMode.startThreshold = frameRenderMode.periodSize;
54 frameRenderMode.stopThreshold = frameRenderMode.periodSize * frameRenderMode.periodCount;
55 return HDF_SUCCESS;
56 }
57
InitHwCaptureFramepara(struct AudioFrameCaptureMode & frameCaptureMode)58 int32_t InitHwCaptureFramepara(struct AudioFrameCaptureMode& frameCaptureMode)
59 {
60 InitAttrs(frameCaptureMode.attrs);
61 frameCaptureMode.mode = AUDIO_CHANNEL_BOTH_RIGHT;
62 frameCaptureMode.byteRate = G_BYTERATE;
63 frameCaptureMode.periodSize = G_PERIODSIZE;
64 frameCaptureMode.periodCount = G_PERIODCOUNT;
65 frameCaptureMode.startThreshold = frameCaptureMode.periodSize;
66 frameCaptureMode.stopThreshold = frameCaptureMode.periodSize * frameCaptureMode.periodCount;
67 frameCaptureMode.silenceThreshold = frameCaptureMode.periodSize * frameCaptureMode.periodCount;
68 frameCaptureMode.silenceSize = G_SILENCETHRESHOLE;
69 frameCaptureMode.buffer = NULL;
70 frameCaptureMode.bufferFrameSize = G_BUFFERFRAMESIZE;
71 frameCaptureMode.bufferSize = G_BUFFERSIZE1;
72 return HDF_SUCCESS;
73 }
74
InitHwRenderMode(struct AudioHwRenderMode & renderMode)75 int32_t InitHwRenderMode(struct AudioHwRenderMode& renderMode)
76 {
77 renderMode.hwInfo.card = AUDIO_SERVICE_IN;
78 renderMode.hwInfo.portDescript.dir = PORT_OUT;
79 renderMode.hwInfo.portDescript.portId = G_PORTID;
80 renderMode.hwInfo.portDescript.portName = "AOP";
81 renderMode.hwInfo.deviceDescript.portId = G_PORTID;
82 renderMode.hwInfo.deviceDescript.pins = PIN_OUT_SPEAKER;
83 renderMode.hwInfo.deviceDescript.desc = nullptr;
84 return HDF_SUCCESS;
85 }
86
InitHwCaptureMode(struct AudioHwCaptureMode & captureMode)87 int32_t InitHwCaptureMode(struct AudioHwCaptureMode& captureMode)
88 {
89 captureMode.hwInfo.card = AUDIO_SERVICE_IN;
90 captureMode.hwInfo.portDescript.dir = PORT_IN;
91 captureMode.hwInfo.portDescript.portId = 0;
92 captureMode.hwInfo.portDescript.portName = "AIP";
93 captureMode.hwInfo.deviceDescript.portId = 0;
94 captureMode.hwInfo.deviceDescript.pins = PIN_IN_MIC;
95 captureMode.hwInfo.deviceDescript.desc = nullptr;
96 return HDF_SUCCESS;
97 }
98
InitHwRender(struct AudioHwRender * & hwRender)99 int32_t InitHwRender(struct AudioHwRender *&hwRender)
100 {
101 int ret = -1;
102 if (hwRender == nullptr) {
103 return HDF_FAILURE;
104 }
105 if (InitHwRenderMode(hwRender->renderParam.renderMode) ||
106 InitRenderFramepara(hwRender->renderParam.frameRenderMode)) {
107 return HDF_FAILURE;
108 }
109 hwRender->renderParam.renderMode.hwInfo.card = AUDIO_SERVICE_IN;
110 ret = strcpy_s(hwRender->renderParam.renderMode.hwInfo.adapterName,
111 NAME_LEN, ADAPTER_NAME.c_str());
112 if (ret < 0) {
113 return HDF_FAILURE;
114 }
115 return HDF_SUCCESS;
116 }
117
InitHwCapture(struct AudioHwCapture * & hwCapture)118 int32_t InitHwCapture(struct AudioHwCapture *&hwCapture)
119 {
120 int ret = -1;
121 if (hwCapture == nullptr) {
122 return HDF_FAILURE;
123 }
124 if (InitHwCaptureMode(hwCapture->captureParam.captureMode) ||
125 InitHwCaptureFramepara(hwCapture->captureParam.frameCaptureMode)) {
126 return HDF_FAILURE;
127 }
128 ret = strcpy_s(hwCapture->captureParam.captureMode.hwInfo.adapterName,
129 NAME_LEN, ADAPTER_NAME.c_str());
130 if (ret < 0) {
131 return HDF_FAILURE;
132 }
133 return HDF_SUCCESS;
134 }
135
BindServiceAndHwRender(struct AudioHwRender * & hwRender)136 int32_t BindServiceAndHwRender(struct AudioHwRender *&hwRender)
137 {
138 hwRender = reinterpret_cast<struct AudioHwRender *>(calloc(1, sizeof(*hwRender)));
139 if (hwRender == nullptr) {
140 return HDF_FAILURE;
141 }
142 if (InitHwRender(hwRender)) {
143 free(hwRender);
144 hwRender = nullptr;
145 return HDF_FAILURE;
146 }
147 return HDF_SUCCESS;
148 }
BindServiceAndHwCapture(struct AudioHwCapture * & hwCapture)149 int32_t BindServiceAndHwCapture(struct AudioHwCapture *&hwCapture)
150 {
151 hwCapture = reinterpret_cast<struct AudioHwCapture *>(calloc(1, sizeof(*hwCapture)));
152 if (hwCapture == nullptr) {
153 return HDF_FAILURE;
154 }
155 if (InitHwCapture(hwCapture)) {
156 free(hwCapture);
157 hwCapture = nullptr;
158 return HDF_FAILURE;
159 }
160 return HDF_SUCCESS;
161 }
162 }
163 }