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 {
InitAttrs(struct AudioSampleAttributes & attrs)40 int32_t InitAttrs(struct AudioSampleAttributes& attrs)
41 {
42 attrs.format = AUDIO_FORMAT_TYPE_PCM_16_BIT;
43 attrs.channelCount = CHANNELCOUNT;
44 attrs.sampleRate = SAMPLERATE;
45 attrs.interleaved = 0;
46 attrs.type = AUDIO_IN_MEDIA;
47 attrs.period = DEEP_BUFFER_RENDER_PERIOD_SIZE;
48 attrs.frameSize = PCM_16_BIT * CHANNELCOUNT / MOVE_LEFT_NUM;
49 attrs.isBigEndian = false;
50 attrs.isSignedData = true;
51 attrs.startThreshold = DEEP_BUFFER_RENDER_PERIOD_SIZE / (PCM_16_BIT * attrs.channelCount / MOVE_LEFT_NUM);
52 attrs.stopThreshold = INT_32_MAX;
53 attrs.silenceThreshold = BUFFER_LENTH;
54 return HDF_SUCCESS;
55 }
56
InitRenderFramepara(struct AudioFrameRenderMode & frameRenderMode)57 int32_t InitRenderFramepara(struct AudioFrameRenderMode& frameRenderMode)
58 {
59 InitAttrs(frameRenderMode.attrs);
60 frameRenderMode.frames = AUDIO_FORMAT_TYPE_PCM_16_BIT;
61 frameRenderMode.mode = AUDIO_CHANNEL_BOTH_RIGHT;
62 frameRenderMode.periodSize = G_PERIODSIZE;
63 frameRenderMode.periodCount = G_PERIODCOUNT;
64 frameRenderMode.byteRate = G_BYTERATE;
65 frameRenderMode.bufferFrameSize = G_BUFFERFRAMESIZE;
66 frameRenderMode.bufferSize = G_BUFFERSIZE1;
67 frameRenderMode.buffer = NULL;
68 frameRenderMode.silenceThreshold = frameRenderMode.periodSize * frameRenderMode.periodCount;
69 frameRenderMode.silenceSize = G_SILENCETHRESHOLE;
70 frameRenderMode.startThreshold = frameRenderMode.periodSize;
71 frameRenderMode.stopThreshold = frameRenderMode.periodSize * frameRenderMode.periodCount;
72 return HDF_SUCCESS;
73 }
74
InitHwCaptureFramepara(struct AudioFrameCaptureMode & frameCaptureMode)75 int32_t InitHwCaptureFramepara(struct AudioFrameCaptureMode& frameCaptureMode)
76 {
77 InitAttrs(frameCaptureMode.attrs);
78 frameCaptureMode.mode = AUDIO_CHANNEL_BOTH_RIGHT;
79 frameCaptureMode.byteRate = G_BYTERATE;
80 frameCaptureMode.periodSize = G_PERIODSIZE;
81 frameCaptureMode.periodCount = G_PERIODCOUNT;
82 frameCaptureMode.startThreshold = frameCaptureMode.periodSize;
83 frameCaptureMode.stopThreshold = frameCaptureMode.periodSize * frameCaptureMode.periodCount;
84 frameCaptureMode.silenceThreshold = frameCaptureMode.periodSize * frameCaptureMode.periodCount;
85 frameCaptureMode.silenceSize = G_SILENCETHRESHOLE;
86 frameCaptureMode.buffer = NULL;
87 frameCaptureMode.bufferFrameSize = G_BUFFERFRAMESIZE;
88 frameCaptureMode.bufferSize = G_BUFFERSIZE1;
89 return HDF_SUCCESS;
90 }
91
InitHwRenderMode(struct AudioHwRenderMode & renderMode)92 int32_t InitHwRenderMode(struct AudioHwRenderMode& renderMode)
93 {
94 renderMode.hwInfo.card = AUDIO_SERVICE_IN;
95 renderMode.hwInfo.portDescript.dir = PORT_OUT;
96 renderMode.hwInfo.portDescript.portId = G_PORTID;
97 renderMode.hwInfo.portDescript.portName = "AOP";
98 renderMode.hwInfo.deviceDescript.portId = G_PORTID;
99 renderMode.hwInfo.deviceDescript.pins = PIN_OUT_SPEAKER;
100 renderMode.hwInfo.deviceDescript.desc = nullptr;
101 return HDF_SUCCESS;
102 }
103
InitHwCaptureMode(struct AudioHwCaptureMode & captureMode)104 int32_t InitHwCaptureMode(struct AudioHwCaptureMode& captureMode)
105 {
106 captureMode.hwInfo.card = AUDIO_SERVICE_IN;
107 captureMode.hwInfo.portDescript.dir = PORT_IN;
108 captureMode.hwInfo.portDescript.portId = 0;
109 captureMode.hwInfo.portDescript.portName = "AIP";
110 captureMode.hwInfo.deviceDescript.portId = 0;
111 captureMode.hwInfo.deviceDescript.pins = PIN_IN_MIC;
112 captureMode.hwInfo.deviceDescript.desc = nullptr;
113 return HDF_SUCCESS;
114 }
115
InitHwRender(struct AudioHwRender * & hwRender)116 int32_t InitHwRender(struct AudioHwRender *&hwRender)
117 {
118 int ret = -1;
119 if (hwRender == nullptr) {
120 return HDF_FAILURE;
121 }
122 if (InitHwRenderMode(hwRender->renderParam.renderMode) ||
123 InitRenderFramepara(hwRender->renderParam.frameRenderMode)) {
124 return HDF_FAILURE;
125 }
126 hwRender->renderParam.renderMode.hwInfo.card = AUDIO_SERVICE_IN;
127 ret = strcpy_s(hwRender->renderParam.renderMode.hwInfo.adapterName,
128 NAME_LEN, ADAPTER_NAME.c_str());
129 if (ret < 0) {
130 return HDF_FAILURE;
131 }
132 return HDF_SUCCESS;
133 }
134
InitHwCapture(struct AudioHwCapture * & hwCapture)135 int32_t InitHwCapture(struct AudioHwCapture *&hwCapture)
136 {
137 int ret = -1;
138 if (hwCapture == nullptr) {
139 return HDF_FAILURE;
140 }
141 if (InitHwCaptureMode(hwCapture->captureParam.captureMode) ||
142 InitHwCaptureFramepara(hwCapture->captureParam.frameCaptureMode)) {
143 return HDF_FAILURE;
144 }
145 ret = strcpy_s(hwCapture->captureParam.captureMode.hwInfo.adapterName,
146 NAME_LEN, ADAPTER_NAME.c_str());
147 if (ret < 0) {
148 return HDF_FAILURE;
149 }
150 return HDF_SUCCESS;
151 }
152
BindServiceAndHwRender(struct AudioHwRender * & hwRender)153 int32_t BindServiceAndHwRender(struct AudioHwRender *&hwRender)
154 {
155 hwRender = reinterpret_cast<struct AudioHwRender *>(calloc(1, sizeof(*hwRender)));
156 if (hwRender == nullptr) {
157 return HDF_FAILURE;
158 }
159 if (InitHwRender(hwRender)) {
160 free(hwRender);
161 hwRender = nullptr;
162 return HDF_FAILURE;
163 }
164 return HDF_SUCCESS;
165 }
BindServiceAndHwCapture(struct AudioHwCapture * & hwCapture)166 int32_t BindServiceAndHwCapture(struct AudioHwCapture *&hwCapture)
167 {
168 hwCapture = reinterpret_cast<struct AudioHwCapture *>(calloc(1, sizeof(*hwCapture)));
169 if (hwCapture == nullptr) {
170 return HDF_FAILURE;
171 }
172 if (InitHwCapture(hwCapture)) {
173 free(hwCapture);
174 hwCapture = nullptr;
175 return HDF_FAILURE;
176 }
177 return HDF_SUCCESS;
178 }
179 }
180 }