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