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_lib_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_lib_common.h"
37 
38 using namespace std;
39 
40 namespace OHOS {
41 namespace Audio {
InitRenderFramepara(struct AudioFrameRenderMode & frameRenderMode)42 int32_t InitRenderFramepara(struct AudioFrameRenderMode& frameRenderMode)
43 {
44     InitAttrs(frameRenderMode.attrs);
45     frameRenderMode.frames = AUDIO_FORMAT_TYPE_PCM_16_BIT;
46     frameRenderMode.mode = AUDIO_CHANNEL_BOTH_RIGHT;
47     frameRenderMode.periodSize = G_PERIODSIZE;
48     frameRenderMode.periodCount = G_PERIODCOUNT;
49     frameRenderMode.byteRate = G_BYTERATE;
50     frameRenderMode.bufferFrameSize = G_BUFFERFRAMESIZE;
51     frameRenderMode.bufferSize = G_BUFFERSIZE1;
52     frameRenderMode.buffer = NULL;
53     frameRenderMode.silenceThreshold = frameRenderMode.periodSize * frameRenderMode.periodCount;
54     frameRenderMode.silenceSize = G_SILENCETHRESHOLE;
55     frameRenderMode.startThreshold = frameRenderMode.periodSize;
56     frameRenderMode.stopThreshold = frameRenderMode.periodSize * frameRenderMode.periodCount;
57     return HDF_SUCCESS;
58 }
59 
InitHwCaptureFramepara(struct AudioFrameCaptureMode & frameCaptureMode)60 int32_t InitHwCaptureFramepara(struct AudioFrameCaptureMode& frameCaptureMode)
61 {
62     InitAttrs(frameCaptureMode.attrs);
63     frameCaptureMode.mode = AUDIO_CHANNEL_BOTH_RIGHT;
64     frameCaptureMode.byteRate = G_BYTERATE;
65     frameCaptureMode.periodSize = G_PERIODSIZE;
66     frameCaptureMode.periodCount = G_PERIODCOUNT;
67     frameCaptureMode.startThreshold = frameCaptureMode.periodSize;
68     frameCaptureMode.stopThreshold = frameCaptureMode.periodSize * frameCaptureMode.periodCount;
69     frameCaptureMode.silenceThreshold = frameCaptureMode.periodSize * frameCaptureMode.periodCount;
70     frameCaptureMode.silenceSize = G_SILENCETHRESHOLE;
71     frameCaptureMode.buffer = NULL;
72     frameCaptureMode.bufferFrameSize = G_BUFFERFRAMESIZE;
73     frameCaptureMode.bufferSize = G_BUFFERSIZE1;
74     return HDF_SUCCESS;
75 }
76 
InitHwRenderMode(struct AudioHwRenderMode & renderMode)77 int32_t InitHwRenderMode(struct AudioHwRenderMode& renderMode)
78 {
79     int codePrimaryLen = strlen(HDF_AUDIO_CODEC_PRIMARY.c_str());
80     int32_t ret = strncpy_s(renderMode.hwInfo.cardServiceName, NAME_LEN - 1,
81         HDF_AUDIO_CODEC_PRIMARY.c_str(), codePrimaryLen);
82     if (ret != 0) {
83         return HDF_FAILURE;
84     }
85     renderMode.hwInfo.portDescript.dir = PORT_OUT;
86     renderMode.hwInfo.portDescript.portId = G_PORTID;
87     renderMode.hwInfo.portDescript.portName = "AOP";
88     renderMode.hwInfo.deviceDescript.portId = G_PORTID;
89     renderMode.hwInfo.deviceDescript.pins = PIN_OUT_SPEAKER;
90     renderMode.hwInfo.deviceDescript.desc = nullptr;
91     return HDF_SUCCESS;
92 }
93 
InitHwCaptureMode(struct AudioHwCaptureMode & captureMode)94 int32_t InitHwCaptureMode(struct AudioHwCaptureMode& captureMode)
95 {
96     int codePrimaryLen = strlen(HDF_AUDIO_CODEC_PRIMARY.c_str());
97     int32_t ret = strncpy_s(captureMode.hwInfo.cardServiceName, NAME_LEN - 1,
98         HDF_AUDIO_CODEC_PRIMARY.c_str(), codePrimaryLen);
99     if (ret != 0) {
100         return HDF_FAILURE;
101     }
102 
103     captureMode.hwInfo.portDescript.dir = PORT_IN;
104     captureMode.hwInfo.portDescript.portId = 0;
105     captureMode.hwInfo.portDescript.portName = "AIP";
106     captureMode.hwInfo.deviceDescript.portId = 0;
107     captureMode.hwInfo.deviceDescript.pins = PIN_IN_MIC;
108     captureMode.hwInfo.deviceDescript.desc = nullptr;
109     return HDF_SUCCESS;
110 }
111 
InitHwRender(struct AudioHwRender * & hwRender,const std::string adapterNameCase)112 uint32_t InitHwRender(struct AudioHwRender *&hwRender, const std::string adapterNameCase)
113 {
114     int ret = -1;
115     if (hwRender == nullptr) {
116         return HDF_FAILURE;
117     }
118     if (InitHwRenderMode(hwRender->renderParam.renderMode) ||
119         InitRenderFramepara(hwRender->renderParam.frameRenderMode)) {
120         return HDF_FAILURE;
121     }
122     hwRender->renderParam.renderMode.hwInfo.card = AUDIO_SERVICE_IN;
123     ret = strcpy_s(hwRender->renderParam.renderMode.hwInfo.adapterName,
124         NAME_LEN, adapterNameCase.c_str());
125     if (ret < 0) {
126         return HDF_FAILURE;
127     }
128     return HDF_SUCCESS;
129 }
130 
InitHwCapture(struct AudioHwCapture * & hwCapture,const std::string adapterNameCase)131 uint32_t InitHwCapture(struct AudioHwCapture *&hwCapture, const std::string adapterNameCase)
132 {
133     int ret = -1;
134     if (hwCapture == nullptr) {
135         return HDF_FAILURE;
136     }
137     if (InitHwCaptureMode(hwCapture->captureParam.captureMode) ||
138         InitHwCaptureFramepara(hwCapture->captureParam.frameCaptureMode)) {
139         return HDF_FAILURE;
140     }
141     ret = strcpy_s(hwCapture->captureParam.captureMode.hwInfo.adapterName,
142         NAME_LEN, adapterNameCase.c_str());
143     if (ret < 0) {
144         return HDF_FAILURE;
145     }
146     return HDF_SUCCESS;
147 }
CaptureReqMmapBufferInit(struct AudioFrameCaptureMode & frameCaptureMode,const std::string path,const int64_t fileSize)148 int32_t CaptureReqMmapBufferInit(struct AudioFrameCaptureMode &frameCaptureMode,
149                                  const std::string path, const int64_t fileSize)
150 {
151     FILE *file = fopen(path.c_str(), "wb+");
152     if (file == nullptr) {
153         return HDF_FAILURE;
154     }
155     int fd = fileno(file);
156     if (fd == -1) {
157         (void)fclose(file);
158         return HDF_FAILURE;
159     }
160     uint32_t formatBits = PcmFormatToBits(frameCaptureMode.attrs.format);
161 
162     ftruncate(fd, FILE_CAPTURE_SIZE);
163     frameCaptureMode.mmapBufDesc.memoryAddress = mmap(NULL, fileSize, PROT_READ | PROT_WRITE,
164         MAP_SHARED, fd, 0);
165     if (frameCaptureMode.mmapBufDesc.memoryAddress == NULL ||
166         frameCaptureMode.mmapBufDesc.memoryAddress == reinterpret_cast<void *>(-1)) {
167         (void)fclose(file);
168         return AUDIO_HAL_ERR_INTERNAL;
169     }
170     frameCaptureMode.mmapBufDesc.totalBufferFrames = fileSize /
171         (frameCaptureMode.attrs.channelCount * (formatBits >> MOVE_RIGHT_NUM));
172     frameCaptureMode.mmapBufDesc.memoryFd = fd;
173     frameCaptureMode.mmapBufDesc.transferFrameSize = DEEP_BUFFER_RENDER_PERIOD_SIZE / FRAME_COUNT;
174     frameCaptureMode.mmapBufDesc.isShareable = 1;
175     frameCaptureMode.mmapBufDesc.offset = 0;
176     (void)fclose(file);
177     return AUDIO_HAL_SUCCESS;
178 }
179 
RenderReqMmapBufferInit(struct AudioFrameRenderMode & frameRenderMode,const std::string path,int64_t & fileSize)180 int32_t RenderReqMmapBufferInit(struct AudioFrameRenderMode &frameRenderMode, const std::string path, int64_t &fileSize)
181 {
182     char absPath[PATH_MAX] = {0};
183     if (realpath(path.c_str(), absPath) == nullptr) {
184         return HDF_FAILURE;
185     }
186 
187     FILE *file = fopen(absPath, "rb+");
188     if (file == nullptr) {
189         return HDF_FAILURE;
190     }
191     int fd = fileno(file);
192     if (fd == -1) {
193         (void)fclose(file);
194         return HDF_FAILURE;
195     }
196     uint32_t formatBits = PcmFormatToBits(frameRenderMode.attrs.format);
197     (void)fseek(file, 0, SEEK_END);
198     fileSize = ftell(file);
199     frameRenderMode.mmapBufDesc.memoryAddress = mmap(NULL, fileSize, PROT_READ,
200         MAP_SHARED, fd, 0);
201     if (frameRenderMode.mmapBufDesc.memoryAddress == NULL ||
202         frameRenderMode.mmapBufDesc.memoryAddress == reinterpret_cast<void *>(-1)) {
203         (void)fclose(file);
204         return HDF_FAILURE;
205     }
206     frameRenderMode.mmapBufDesc.totalBufferFrames = fileSize /
207         (frameRenderMode.attrs.channelCount * (formatBits >> MOVE_RIGHT_NUM));
208     frameRenderMode.mmapBufDesc.memoryFd = fd;
209     frameRenderMode.mmapBufDesc.transferFrameSize = DEEP_BUFFER_RENDER_PERIOD_SIZE / FRAME_COUNT;
210     frameRenderMode.mmapBufDesc.isShareable = 1;
211     struct AudioHeadInfo wavHeadInfo = {};
212     frameRenderMode.mmapBufDesc.offset = sizeof(wavHeadInfo);
213     (void)fclose(file);
214     return AUDIO_HAL_SUCCESS;
215 }
216 }
217 }
218