• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-related APIs, including custom data types and functions for loading drivers,
21  * accessing a driver adapter, and rendering and capturing audios.
22  *
23  * @since 1.0
24  * @version 1.0
25  */
26 
27 /**
28  * @file audio_adapter.h
29  *
30  * @brief Declares APIs for operations related to the audio adapter.
31  *
32  * @since 1.0
33  * @version 1.0
34  */
35 
36 #ifndef AUDIO_HDI_COMMON_H
37 #define AUDIO_HDI_COMMON_H
38 
39 #include <gtest/gtest.h>
40 #include "audio_adapter.h"
41 #include "audio_internal.h"
42 #include "audio_types.h"
43 #include "hdf_io_service_if.h"
44 #include "hdf_sbuf.h"
45 
46 namespace OHOS {
47 namespace Audio {
48 #ifdef AUDIO_ADM_SO
49     const std::string FUNCTION_NAME = "GetAudioManagerFuncs";
50     const std::string RESOLVED_PATH = HDF_LIBRARY_FULL_PATH("libhdi_audio");
51     const int IS_ADM = true;
52 #endif
53 #ifdef AUDIO_ADM_SERVICE
54     const std::string FUNCTION_NAME = "GetAudioManagerFuncs";
55     const std::string RESOLVED_PATH = HDF_LIBRARY_FULL_PATH("libhdi_audio_client");
56     using TestAudioManager = struct AudioManager;
57     const int IS_ADM = true;
58 #endif
59 #ifdef __LITEOS__
60     const std::string FUNCTION_NAME = "GetAudioManagerFuncs";
61     const std::string RESOLVED_PATH = "/usr/lib/libhdi_audio.so";
62     const int IS_ADM = true;
63     const std::string AUDIO_FILE = "/userdata/audiorendertest.wav";
64     const std::string LOW_LATENCY_AUDIO_FILE = "/userdata/lowlatencyrendertest.wav";
65     const std::string AUDIO_CAPTURE_FILE = "/userdata/audiocapture.wav";
66     const std::string AUDIO_LOW_LATENCY_CAPTURE_FILE = "/userdata/lowlatencycapturetest.wav";
67 #else
68     const std::string AUDIO_FILE = "/data/test/audiorendertest.wav";
69     const std::string LOW_LATENCY_AUDIO_FILE = "/data/test/lowlatencyrendertest.wav";
70     const std::string AUDIO_CAPTURE_FILE = "/data/audiocapture.wav";
71     const std::string AUDIO_LOW_LATENCY_CAPTURE_FILE = "/data/lowlatencycapturetest.wav";
72 #endif
73 const std::string ADAPTER_NAME = "primary";
74 const std::string ADAPTER_NAME_OUT = "primary_ext";
75 
76 using TestAudioManager = struct AudioManager;
77 const std::string AUDIO_RIFF = "RIFF";
78 const std::string AUDIO_WAVE = "WAVE";
79 const std::string AUDIO_DATA = "data";
80 const uint32_t INT_32_MAX = 0x7fffffff;
81 const uint32_t INDEX_END = 555;
82 const uint32_t MOVE_RIGHT_NUM = 3;
83 const int MOVE_LEFT_NUM = 8;
84 const int CHANNELCOUNT = 2;
85 const int SAMPLERATE = 48000;
86 const int DEEP_BUFFER_RENDER_PERIOD_SIZE = 4*1024;
87 const float GAIN_MIN = 0;
88 const float GAIN_MAX = 15;
89 const uint64_t INITIAL_VALUE = 0;
90 const int BUFFER_LENTH = 1024 * 16;
91 const int FILE_CAPTURE_SIZE = 1024 * 1024 * 1;
92 const uint64_t MEGABYTE = 1024;
93 const int FRAME_SIZE = 1024;
94 const int FRAME_COUNT = 4;
95 const int ADAPTER_COUNT = 32;
96 const int TRY_NUM_FRAME = 20;
97 const int AUDIO_ADAPTER_MAX_NUM = 5;
98 const int AUDIO_WRITE_COMPELETED_VALUE = 1;
99 const int AUDIO_RENDER_FULL_VALUE = 2;
100 const int AUDIO_FLUSH_COMPLETED_VALUE = 3;
101 const int64_t SECTONSEC = 1000000000;
102 const int MICROSECOND = 1000000;
103 const int RANGE = 3;
104 const std::string HDF_CONTROL_SERVICE = "hdf_audio_control";
105 const std::string HDF_RENDER_SERVICE = "hdf_audio_render";
106 const std::string HDF_CAPTURE_SERVICE = "hdf_audio_capture";
107 
108 const int AUDIODRV_CTL_ELEM_IFACE_MIXER = 2; /* virtual adc device */
109 
110 constexpr int SAMPLE_RATE_8000 = 8000;
111 constexpr int SAMPLE_RATE_11025 = 11025;
112 constexpr int SAMPLE_RATE_22050 = 22050;
113 constexpr int SAMPLE_RATE_32000 = 32000;
114 constexpr int SAMPLE_RATE_44100 = 44100;
115 constexpr int SAMPLE_RATE_48000 = 48000;
116 constexpr int SAMPLE_RATE_12000 = 12000;
117 constexpr int SAMPLE_RATE_16000 = 16000;
118 constexpr int SAMPLE_RATE_24000 = 24000;
119 constexpr int SAMPLE_RATE_64000 = 64000;
120 constexpr int SAMPLE_RATE_96000 = 96000;
121 constexpr int DOUBLE_CHANNEL_COUNT = 2;
122 constexpr int SINGLE_CHANNEL_COUNT = 1;
123 
124 enum ControlDispMethodCmd {
125     AUDIODRV_CTRL_IOCTRL_ELEM_INFO,
126     AUDIODRV_CTRL_IOCTRL_ELEM_READ,
127     AUDIODRV_CTRL_IOCTRL_ELEM_WRITE,
128     AUDIODRV_CTRL_IOCTRL_ELEM_BUTT,
129 };
130 
131 constexpr int PCM_8_BIT  = 8;
132 constexpr int PCM_16_BIT = 16;
133 constexpr int PCM_24_BIT = 24;
134 constexpr int PCM_32_BIT = 32;
135 
136 constexpr int PCM_BYTE_MIN = 1024;
137 constexpr int PCM_BYTE_MAX = 8 * 1024;
138 
139 struct AudioCtlElemId {
140     const char *cardServiceName;
141     int32_t iface;
142     const char *itemName; /* ASCII name of item */
143 };
144 
145 struct AudioCtlElemValue {
146     struct AudioCtlElemId id;
147     int32_t value[2];
148 };
149 
150 struct AudioHeadInfo {
151     uint32_t testFileRiffId;
152     uint32_t testFileRiffSize;
153     uint32_t testFileFmt;
154     uint32_t audioFileFmtId;
155     uint32_t audioFileFmtSize;
156     uint16_t audioFileFormat;
157     uint16_t audioChannelNum;
158     uint32_t audioSampleRate;
159     uint32_t audioByteRate;
160     uint16_t audioBlockAlign;
161     uint16_t audioBitsPerSample;
162     uint32_t dataId;
163     uint32_t dataSize;
164 };
165 
166 struct AudioCharacteristic {
167     bool setmute;
168     bool getmute;
169     float setvolume;
170     float getvolume;
171     float setgain;
172     float getgain;
173     float gainthresholdmin;
174     float gainthresholdmax;
175     uint64_t getframes;
176     uint64_t getframesize;
177     uint64_t getframecount;
178     uint32_t getcurrentchannelId;
179     enum AudioChannelMode setmode;
180     enum AudioChannelMode getmode;
181     bool supported;
182     uint32_t latencyTime;
183 };
184 
185 struct PrepareAudioPara {
186     TestAudioManager *manager;
187     enum AudioPortDirection portType;
188     const char *adapterName;
189     struct AudioAdapter *adapter;
190     struct AudioPort *audioPort;
191     void *self;
192     void *result;
193     pthread_t tids;
194     enum AudioPortPin pins;
195     const char *path;
196     struct AudioRender *render;
197     struct AudioCapture *capture;
198     struct AudioHeadInfo headInfo;
199     struct AudioAdapterDescriptor *desc;
200     struct AudioAdapterDescriptor *descs;
201     char *frame;
202     uint64_t requestBytes;
203     uint64_t replyBytes;
204     uint64_t fileSize;
205     struct AudioSampleAttributes attrs;
206     struct AudioCharacteristic character;
207     struct AudioSampleAttributes attrsValue;
208     struct AudioSceneDescriptor scenes;
209     struct AudioPortCapability capability;
210     enum AudioPortPassthroughMode mode;
211     struct AudioTimeStamp time;
212     struct timeval start;
213     struct timeval end;
214     int64_t delayTime;
215     int64_t totalTime;
216     float averageDelayTime;
217     struct AudioDeviceDescriptor devDesc;
218 };
219 
220 int32_t InitAttrs(struct AudioSampleAttributes &attrs);
221 
222 int32_t InitDevDesc(struct AudioDeviceDescriptor &devDesc, const uint32_t portId, int pins);
223 
224 int32_t SwitchAdapter(struct AudioAdapterDescriptor *descs, const std::string &adapterNameCase,
225     int portFlag, struct AudioPort *&audioPort, int size);
226 
227 uint32_t PcmFormatToBits(int format);
228 
229 uint32_t PcmFramesToBytes(const struct AudioSampleAttributes attrs);
230 
231 int32_t WavHeadAnalysis(struct AudioHeadInfo &wavHeadInfo, FILE *file, struct AudioSampleAttributes &attrs);
232 
233 int32_t GetAdapters(TestAudioManager *manager, struct AudioAdapterDescriptor **descs, int &size);
234 
235 int32_t GetLoadAdapter(TestAudioManager *manager, int portType,
236     const std::string &adapterName, struct AudioAdapter **adapter, struct AudioPort *&audioPort);
237 
238 int32_t AudioCreateRender(TestAudioManager *manager, int pins, const std::string &adapterName,
239     struct AudioAdapter **adapter, struct AudioRender **render);
240 
241 int32_t AudioCreateCapture(TestAudioManager *manager, int pins, const std::string &adapterName,
242     struct AudioAdapter **adapter, struct AudioCapture **capture);
243 
244 int32_t FrameStart(struct AudioHeadInfo wavHeadInfo, struct AudioRender *render, FILE *file,
245     struct AudioSampleAttributes attrs);
246 
247 int32_t FrameStartCapture(struct AudioCapture *capture, FILE *file, const struct AudioSampleAttributes attrs);
248 
249 int32_t RenderFramePrepare(const std::string &path, char *&frame, uint64_t &numRead);
250 
251 void FrameStatus(int status);
252 
253 int32_t StartRecord(struct AudioCapture *capture, FILE *file, uint64_t filesize);
254 
255 int32_t WriteIdToBuf(struct HdfSBuf *sBuf, struct AudioCtlElemId id);
256 
257 int32_t WriteEleValueToBuf(struct HdfSBuf *sBuf, struct AudioCtlElemValue elemvalue);
258 
259 int32_t PowerOff(struct AudioCtlElemValue firstElemValue, struct AudioCtlElemValue secondElemValue);
260 
261 int32_t CheckRegisterStatus(const struct AudioCtlElemId firstId, const struct AudioCtlElemId secondId,
262     const int firstStatus, const int secondStatus);
263 
264 int32_t AudioCreateStartRender(TestAudioManager *manager, struct AudioRender **render, struct AudioAdapter **adapter,
265     const std::string &adapterName);
266 
267 int32_t AudioRenderStartAndOneFrame(struct AudioRender *render);
268 
269 int32_t StopAudio(struct PrepareAudioPara &audiopara);
270 
271 int32_t ThreadRelease(struct PrepareAudioPara &audiopara);
272 
273 int32_t AudioCreateStartCapture(TestAudioManager *manager, struct AudioCapture **capture,
274     struct AudioAdapter **adapter, const std::string &adapterName);
275 
276 int32_t AudioCaptureStartAndOneFrame(struct AudioCapture *capture);
277 
278 int32_t PlayAudioFile(struct PrepareAudioPara &audiopara);
279 
280 int32_t RecordAudio(struct PrepareAudioPara &audiopara);
281 
282 int32_t InitAttrsUpdate(struct AudioSampleAttributes &attrs, int format, uint32_t channelCount,
283     uint32_t sampleRate, uint32_t silenceThreshold = BUFFER_LENTH);
284 
285 int32_t AudioRenderSetGetSampleAttributes(struct AudioSampleAttributes attrs, struct AudioSampleAttributes &attrsValue,
286     struct AudioRender *render);
287 
288 int32_t AudioCaptureSetGetSampleAttributes(struct AudioSampleAttributes attrs, struct AudioSampleAttributes &attrsValue,
289     struct AudioCapture *capture);
290 
291 int32_t InitMmapDesc(FILE *fp, struct AudioMmapBufferDescripter &desc, int32_t &reqSize, bool flag);
292 
293 int32_t PlayMapAudioFile(struct PrepareAudioPara &audiopara);
294 
295 int32_t RecordMapAudio(struct PrepareAudioPara &audiopara);
296 int32_t AudioRenderCallback(enum AudioCallbackType type, void *reserved, void *cookie);
297 int32_t CheckFlushValue();
298 int32_t CheckRenderFullValue();
299 int32_t CheckWriteCompleteValue();
300 }
301 }
302 #endif // AUDIO_HDI_COMMON_H
303 
304