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