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 Defines audio ADM test-related APIs, including data types and functions for writing data
21 to buffer.
22 *
23 * @since 1.0
24 * @version 1.0
25 */
26
27 /**
28 * @file audio_adm_common.h
29 *
30 * @brief Declares APIs for operations related to the audio ADM testing.
31 *
32 * @since 1.0
33 * @version 1.0
34 */
35
36 #include "audio_adm_common.h"
37
38 using namespace std;
39
40 namespace OHOS {
41 namespace Audio {
WriteHwParamsToBuf(struct HdfSBuf * sBuf,struct AudioPcmHwParams hwParams)42 int32_t WriteHwParamsToBuf(struct HdfSBuf *sBuf, struct AudioPcmHwParams hwParams)
43 {
44 if (sBuf == nullptr) {
45 return HDF_FAILURE;
46 }
47 if (!HdfSbufWriteUint32(sBuf, (uint32_t)hwParams.streamType)) {
48 return HDF_FAILURE;
49 }
50 if (!HdfSbufWriteUint32(sBuf, hwParams.channels)) {
51 return HDF_FAILURE;
52 }
53 if (!HdfSbufWriteUint32(sBuf, hwParams.rate)) {
54 return HDF_FAILURE;
55 }
56 if (!HdfSbufWriteUint32(sBuf, hwParams.periodSize)) {
57 return HDF_FAILURE;
58 }
59 if (!HdfSbufWriteUint32(sBuf, hwParams.periodCount)) {
60 return HDF_FAILURE;
61 }
62 if (!HdfSbufWriteUint32(sBuf, (uint32_t)(hwParams.format))) {
63 return HDF_FAILURE;
64 }
65 if (!HdfSbufWriteString(sBuf, hwParams.cardServiceName)) {
66 return HDF_FAILURE;
67 }
68 if (!HdfSbufWriteUint32(sBuf, hwParams.period)) {
69 return HDF_FAILURE;
70 }
71 if (!HdfSbufWriteUint32(sBuf, hwParams.frameSize)) {
72 return HDF_FAILURE;
73 }
74 if (!HdfSbufWriteUint32(sBuf, (uint32_t)(hwParams.isBigEndian))) {
75 return HDF_FAILURE;
76 }
77 if (!HdfSbufWriteUint32(sBuf, (uint32_t)(hwParams.isSignedData))) {
78 return HDF_FAILURE;
79 }
80 if (!HdfSbufWriteUint32(sBuf, hwParams.startThreshold)) {
81 return HDF_FAILURE;
82 }
83 if (!HdfSbufWriteUint32(sBuf, hwParams.stopThreshold)) {
84 return HDF_FAILURE;
85 }
86 if (!HdfSbufWriteUint32(sBuf, hwParams.silenceThreshold)) {
87 return HDF_FAILURE;
88 }
89 return HDF_SUCCESS;
90 }
91
AdmRenderFramePrepare(const std::string & path,char * & frame,uint32_t & readSize,uint32_t & frameSize)92 int32_t AdmRenderFramePrepare(const std::string& path, char *&frame, uint32_t& readSize, uint32_t& frameSize)
93 {
94 int32_t ret = -1;
95 size_t numRead = 0;
96 uint32_t bufferSize = 0;
97
98 struct AudioSampleAttributes attrs = {};
99 InitAttrs(attrs);
100 char absPath[PATH_MAX] = {0};
101 if (realpath(path.c_str(), absPath) == nullptr) {
102 return HDF_FAILURE;
103 }
104 FILE *file = fopen(absPath, "rb");
105 if (file == nullptr) {
106 return HDF_FAILURE;
107 }
108 struct AudioHeadInfo headInfo = {};
109 ret = WavHeadAnalysis(headInfo, file, attrs);
110 if (ret < 0) {
111 fclose(file);
112 return HDF_FAILURE;
113 }
114 bufferSize = PcmFramesToBytes(attrs);
115 if (bufferSize == 0) {
116 fclose(file);
117 return HDF_FAILURE;
118 }
119 frame = reinterpret_cast<char *>(calloc(1, bufferSize));
120 if (frame == nullptr) {
121 fclose(file);
122 return HDF_FAILURE;
123 }
124 uint32_t remainingDataSize = 0;
125 remainingDataSize = headInfo.dataSize;
126 readSize = (remainingDataSize) > (bufferSize) ? (bufferSize) : (remainingDataSize);
127 numRead = fread(frame, readSize, 1, file);
128 if (numRead < 1) {
129 fclose(file);
130 free(frame);
131 frame = nullptr;
132 return HDF_FAILURE;
133 }
134 frameSize = readSize / (attrs.channelCount * (PcmFormatToBits(attrs.format) >> MOVE_RIGHT_NUM));
135 (void)fclose(file);
136 return HDF_SUCCESS;
137 }
138
WriteFrameToSBuf(struct HdfSBuf * & sBufT,const std::string & path)139 int32_t WriteFrameToSBuf(struct HdfSBuf *&sBufT, const std::string& path)
140 {
141 int32_t ret = -1;
142 sBufT = HdfSbufObtainDefaultSize();
143 char *buf = nullptr;
144 uint32_t bufsize = 0;
145 uint32_t frameSize = 0;
146 if (sBufT == NULL) {
147 return HDF_FAILURE;
148 }
149
150 ret = AdmRenderFramePrepare(path, buf, bufsize, frameSize);
151 if (ret < 0) {
152 return HDF_FAILURE;
153 }
154
155 if (!HdfSbufWriteString(sBufT, CARD_SEVICE_NAME.c_str())) {
156 free(buf);
157 buf = nullptr;
158 return HDF_FAILURE;
159 }
160
161 if (!HdfSbufWriteUint32(sBufT, frameSize)) {
162 free(buf);
163 buf = nullptr;
164 return HDF_FAILURE;
165 }
166 if (!HdfSbufWriteBuffer(sBufT, buf, bufsize)) {
167 free(buf);
168 buf = nullptr;
169 return HDF_FAILURE;
170 }
171 free(buf);
172 buf = nullptr;
173 return HDF_SUCCESS;
174 }
175
WriteToSBuf(struct HdfSBuf * & sBufT)176 int32_t WriteToSBuf(struct HdfSBuf *&sBufT)
177 {
178 sBufT = HdfSbufObtainDefaultSize();
179 if (sBufT == NULL) {
180 return HDF_FAILURE;
181 }
182 if (!HdfSbufWriteString(sBufT, CARD_SEVICE_NAME.c_str())) {
183 HdfSbufRecycle(sBufT);
184 sBufT = nullptr;
185 return HDF_FAILURE;
186 }
187
188 if (!HdfSbufWriteUint32(sBufT, 0)) {
189 HdfSbufRecycle(sBufT);
190 sBufT = nullptr;
191 return HDF_FAILURE;
192 }
193 return HDF_SUCCESS;
194 }
WriteCardSeviceNameToSBuf(struct HdfSBuf * & sBufT)195 int32_t WriteCardSeviceNameToSBuf(struct HdfSBuf *&sBufT)
196 {
197 sBufT = HdfSbufObtainDefaultSize();
198 if (sBufT == NULL) {
199 return HDF_FAILURE;
200 }
201
202 if (!HdfSbufWriteString(sBufT, CARD_SEVICE_NAME.c_str())) {
203 HdfSbufRecycle(sBufT);
204 sBufT = nullptr;
205 return HDF_FAILURE;
206 }
207 return HDF_SUCCESS;
208 }
209
ObtainBuf(struct HdfSBuf * & readBuf,struct HdfSBuf * & readReply)210 int32_t ObtainBuf(struct HdfSBuf *&readBuf, struct HdfSBuf *&readReply)
211 {
212 readBuf = HdfSbufObtainDefaultSize();
213 if (readBuf == nullptr) {
214 return HDF_FAILURE;
215 }
216 readReply = HdfSbufObtainDefaultSize();
217 if (readReply == nullptr) {
218 HdfSbufRecycle(readBuf);
219 return HDF_FAILURE;
220 }
221 return HDF_SUCCESS;
222 }
223
RecycleBuf(struct HdfSBuf * & readBuf,struct HdfSBuf * & readReply)224 void RecycleBuf(struct HdfSBuf *&readBuf, struct HdfSBuf *&readReply)
225 {
226 HdfSbufRecycle(readBuf);
227 HdfSbufRecycle(readReply);
228 }
229
WriteCtrlInfo(struct HdfIoService * service,struct AudioCtlElemValue writeElemValue)230 int32_t WriteCtrlInfo(struct HdfIoService *service, struct AudioCtlElemValue writeElemValue)
231 {
232 int32_t ret = -1;
233 struct HdfSBuf *writeBuf = nullptr;
234 if (service == nullptr || service->dispatcher == nullptr) {
235 return HDF_FAILURE;
236 }
237
238 writeBuf = HdfSbufObtainDefaultSize();
239 if (writeBuf == nullptr) {
240 return HDF_FAILURE;
241 }
242 ret = WriteEleValueToBuf(writeBuf, writeElemValue);
243 if (ret < 0) {
244 HdfSbufRecycle(writeBuf);
245 return HDF_FAILURE;
246 }
247 ret = service->dispatcher->Dispatch(&service->object, AUDIODRV_CTRL_IOCTRL_ELEM_WRITE, writeBuf, nullptr);
248 if (ret < 0) {
249 HdfSbufRecycle(writeBuf);
250 return HDF_FAILURE;
251 }
252 HdfSbufRecycle(writeBuf);
253 return HDF_SUCCESS;
254 }
255
ReadCtrlInfo(struct HdfIoService * service,struct AudioCtlElemId id,int32_t expectValue)256 int32_t ReadCtrlInfo(struct HdfIoService *service, struct AudioCtlElemId id, int32_t expectValue)
257 {
258 int32_t ret = -1;
259 struct HdfSBuf *readBuf = nullptr;
260 struct HdfSBuf *readReply = nullptr;
261 struct AudioCtlElemValue readElemValue = {};
262 if (service == nullptr || service->dispatcher == nullptr) {
263 return HDF_FAILURE;
264 }
265 ret = ObtainBuf(readBuf, readReply);
266 if (ret < 0) {
267 return HDF_FAILURE;
268 }
269 ret = WriteIdToBuf(readBuf, id);
270 if (ret < 0) {
271 RecycleBuf(readBuf, readReply);
272 return HDF_FAILURE;
273 }
274 ret = service->dispatcher->Dispatch(&service->object, AUDIODRV_CTRL_IOCTRL_ELEM_READ, readBuf, readReply);
275 if (ret < 0) {
276 RecycleBuf(readBuf, readReply);
277 return HDF_FAILURE;
278 }
279 ret = HdfSbufReadInt32(readReply, &readElemValue.value[0]);
280 if (ret < 0 || expectValue != readElemValue.value[0]) {
281 RecycleBuf(readBuf, readReply);
282 return HDF_FAILURE;
283 }
284 RecycleBuf(readBuf, readReply);
285 return HDF_SUCCESS;
286 }
WriteHwParams(const string serviceName,struct HdfIoService * & service,struct AudioPcmHwParams hwParams)287 int32_t WriteHwParams(const string serviceName, struct HdfIoService *&service, struct AudioPcmHwParams hwParams)
288 {
289 int32_t ret = -1;
290 int32_t cmdid = -1;
291 struct HdfSBuf *writeBuf = nullptr;
292 struct HdfSBuf *writeReply = nullptr;
293 if (!strcmp(serviceName.c_str(), HDF_CAPTURE_SERVICE.c_str())) {
294 cmdid = AUDIO_DRV_PCM_IOCTRL_CAPTURE_OPEN;
295 } else {
296 cmdid = AUDIO_DRV_PCM_IOCTRL_RENDER_OPEN;
297 }
298 service = HdfIoServiceBind(serviceName.c_str());
299 if (service == nullptr || service->dispatcher == nullptr) {
300 return HDF_FAILURE;
301 }
302 writeBuf = HdfSbufObtainDefaultSize();
303 if (writeBuf == nullptr) {
304 HdfIoServiceRecycle(service);
305 service = nullptr;
306 return HDF_FAILURE;
307 }
308
309 if (!HdfSbufWriteString(writeBuf, CARD_SEVICE_NAME.c_str())) {
310 HdfIoServiceRecycle(service);
311 return HDF_FAILURE;
312 }
313 ret = service->dispatcher->Dispatch(&service->object, cmdid, writeBuf, writeReply);
314 if (ret < 0) {
315 HdfSbufRecycle(writeBuf);
316 HdfIoServiceRecycle(service);
317 service = nullptr;
318 return HDF_FAILURE;
319 }
320 HdfSbufFlush(writeBuf);
321 ret = WriteHwParamsToBuf(writeBuf, hwParams);
322 if (ret < 0) {
323 HdfSbufRecycle(writeBuf);
324 HdfIoServiceRecycle(service);
325 service = nullptr;
326 return HDF_FAILURE;
327 }
328 ret = service->dispatcher->Dispatch(&service->object, AUDIO_DRV_PCM_IOCTRL_HW_PARAMS, writeBuf, writeReply);
329 if (ret < 0) {
330 HdfSbufRecycle(writeBuf);
331 HdfIoServiceRecycle(service);
332 service = nullptr;
333 return HDF_FAILURE;
334 }
335 HdfSbufRecycle(writeBuf);
336 return HDF_SUCCESS;
337 }
338 }
339 }
340