1 /** 2 * Copyright (c) 2022 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 #ifndef AUDIO_IDLHDI_COMMON_H 17 #define AUDIO_IDLHDI_COMMON_H 18 #include <inttypes.h> 19 #include <limits.h> 20 #include <pthread.h> 21 #include <securec.h> 22 #include <signal.h> 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string> 26 #include <sys/mman.h> 27 #include <sys/time.h> 28 #include <unistd.h> 29 #include "hdf_base.h" 30 #include "v5_0/iaudio_manager.h" 31 #include "v5_0/audio_types.h" 32 33 namespace OHOS { 34 namespace Audio { 35 #ifdef AUDIO_ADM_PASSTHROUGH 36 constexpr bool IS_STUB = true; 37 #endif 38 #ifdef AUDIO_ADM_SERVICE 39 constexpr bool IS_STUB = false; 40 #endif 41 const std::string AUDIO_FILE = "/data/test/audiorendertest.wav"; 42 const std::string LOW_LATENCY_AUDIO_FILE = "/data/test/lowlatencyrendertest.wav"; 43 const std::string AUDIO_CAPTURE_FILE = "/data/test/audiocapture.wav"; 44 const std::string AUDIO_LOW_LATENCY_CAPTURE_FILE = "/data/test/lowlatencycapturetest.wav"; 45 46 const int AUDIO_ADAPTER_MAX_NUM = 5; 47 const std::string ADAPTER_NAME = "primary"; 48 const std::string ADAPTER_NAME_OUT = "primary_ext"; 49 using TestAudioManager = struct IAudioManager; 50 const std::string AUDIO_RIFF = "RIFF"; 51 const std::string AUDIO_WAVE = "WAVE"; 52 const std::string AUDIO_DATA = "data"; 53 const uint32_t INT_32_MAX = 0x7fffffff; 54 const uint32_t INDEX_END = 555; 55 const uint32_t MOVE_RIGHT_NUM = 3; 56 const int MOVE_LEFT_NUM = 8; 57 const int CHANNELCOUNT = 2; 58 const int SAMPLERATE = 48000; 59 const int DEEP_BUFFER_RENDER_PERIOD_SIZE = 4*1024; 60 const float GAIN_MIN = 0; 61 const float GAIN_MAX = 15; 62 const uint64_t INITIAL_VALUE = 0; 63 const int BUFFER_LENTH = 1024 * 16; 64 const int FILE_CAPTURE_SIZE = 1024 * 1024 * 1; 65 const uint64_t MEGABYTE = 1024; 66 const int FRAME_SIZE = 1024; 67 const int FRAME_COUNT = 4; 68 const int PCM_BYTE_MIN = 1024; 69 const int PCM_BYTE_MAX = 8 * 1024; 70 const int ADAPTER_COUNT = 32; 71 const int TRY_NUM_FRAME = 20; 72 const int AUDIO_WRITE_COMPLETED_VALUE = 1; 73 const int AUDIO_RENDER_FULL_VALUE = 2; 74 const int AUDIO_FLUSH_COMPLETED_VALUE = 3; 75 const int64_t SECTONSEC = 1000000000; 76 const int MICROSECOND = 1000000; 77 const int RANGE = 3; 78 const int OUT_OF_RANGE = 6; 79 const int32_t AUDIO_HAL_ERR_NOTREADY = -7001; /* audio adapter is not ready */ 80 const int32_t AUDIO_HAL_ERR_AI_BUSY = -7002; /* audio capture is busy now */ 81 const int32_t AUDIO_HAL_ERR_AO_BUSY = -7003; /* audio render is busy now */ 82 83 constexpr int PCM_8_BIT = 8; 84 constexpr int PCM_16_BIT = 16; 85 constexpr int PCM_24_BIT = 24; 86 constexpr int PCM_32_BIT = 32; 87 constexpr int BUFFER_INDEX_ZERO = 0; 88 constexpr int BUFFER_INDEX_ONE = 1; 89 constexpr int BUFFER_INDEX_TWO = 2; 90 constexpr int BUFFER_INDEX_THREE = 3; 91 constexpr int SAMPLE_RATE_8000 = 8000; 92 constexpr int SAMPLE_RATE_11025 = 11025; 93 constexpr int SAMPLE_RATE_22050 = 22050; 94 constexpr int SAMPLE_RATE_32000 = 32000; 95 constexpr int SAMPLE_RATE_44100 = 44100; 96 constexpr int SAMPLE_RATE_48000 = 48000; 97 constexpr int SAMPLE_RATE_12000 = 12000; 98 constexpr int SAMPLE_RATE_16000 = 16000; 99 constexpr int SAMPLE_RATE_24000 = 24000; 100 constexpr int SAMPLE_RATE_64000 = 64000; 101 constexpr int SAMPLE_RATE_96000 = 96000; 102 constexpr int DOUBLE_CHANNEL_COUNT = 2; 103 constexpr int SINGLE_CHANNEL_COUNT = 1; 104 105 struct AudioCtlElemId { 106 const char *cardServiceName; 107 int32_t iface; 108 const char *itemName; /* ASCII name of item */ 109 }; 110 111 struct AudioCtlElemValue { 112 struct AudioCtlElemId id; 113 int32_t value[2]; 114 }; 115 116 struct AudioHeadInfo { 117 uint32_t testFileRiffId; 118 uint32_t testFileRiffSize; 119 uint32_t testFileFmt; 120 uint32_t audioFileFmtId; 121 uint32_t audioFileFmtSize; 122 uint16_t audioFileFormat; 123 uint16_t audioChannelNum; 124 uint32_t audioSampleRate; 125 uint32_t audioByteRate; 126 uint16_t audioBlockAlign; 127 uint16_t audioBitsPerSample; 128 uint32_t dataId; 129 uint32_t dataSize; 130 }; 131 132 struct AudioCharacteristic { 133 bool setmute; 134 bool getmute; 135 float setvolume; 136 float getvolume; 137 float setgain; 138 float getgain; 139 float gainthresholdmin; 140 float gainthresholdmax; 141 uint64_t getframes; 142 uint64_t getframesize; 143 uint64_t getframecount; 144 uint32_t getcurrentchannelId; 145 AudioChannelMode setmode; 146 AudioChannelMode getmode; 147 bool supported; 148 uint32_t latencyTime; 149 }; 150 151 struct PrepareAudioPara { 152 TestAudioManager *manager; 153 AudioPortDirection portType; 154 const char *adapterName; 155 struct IAudioAdapter *adapter; 156 struct AudioPort audioPort; 157 void *self; 158 void *result; 159 pthread_t tids; 160 AudioPortPin pins; 161 const char *path; 162 struct IAudioRender *render; 163 struct IAudioCapture *capture; 164 struct AudioHeadInfo headInfo; 165 struct AudioAdapterDescriptor *desc; 166 struct AudioAdapterDescriptor *descs; 167 char *frame; 168 uint64_t requestBytes; 169 uint32_t replyBytes; 170 uint64_t fileSize; 171 struct AudioSampleAttributes attrs; 172 struct AudioCharacteristic character; 173 struct AudioSampleAttributes attrsValue; 174 struct AudioSceneDescriptor scenes; 175 struct AudioPortCapability capability; 176 enum AudioPortPassthroughMode mode; 177 struct AudioTimeStamp time; 178 struct timeval start; 179 struct timeval end; 180 long delayTime; 181 long totalTime; 182 float averageDelayTime; 183 struct AudioDeviceDescriptor devDesc; 184 }; 185 186 int32_t InitAttrs(struct AudioSampleAttributes &attrs); 187 188 int32_t InitDevDesc(struct AudioDeviceDescriptor &devDesc, const uint32_t portId, int pins); 189 190 int32_t SwitchAdapter(struct AudioAdapterDescriptor *descs, const std::string &adapterNameCase, 191 int portFlag, struct AudioPort *&audioPort, int size); 192 193 uint32_t PcmFormatToBits(int format); 194 195 uint32_t PcmFramesToBytes(const struct AudioSampleAttributes attrs); 196 197 int32_t WavHeadAnalysis(struct AudioHeadInfo &wavHeadInfo, FILE *file, struct AudioSampleAttributes &attrs); 198 199 int32_t GetAdapters(TestAudioManager *manager, struct AudioAdapterDescriptor *&descs, uint32_t &size); 200 201 int32_t GetLoadAdapter(TestAudioManager *manager, int portType, 202 const std::string &adapterName, struct IAudioAdapter **adapter, struct AudioPort &audioPort); 203 204 int32_t AudioCreateRender(TestAudioManager *manager, int pins, const std::string &adapterName, 205 struct IAudioAdapter **adapter, struct IAudioRender **render, uint32_t *renderId); 206 207 #ifdef SUPPORT_OFFLOAD 208 int32_t AudioOffloadCreateRender(TestAudioManager *manager, int pins, const std::string &adapterName, 209 struct IAudioAdapter **adapter, struct IAudioRender **render, uint32_t *renderId); 210 #endif 211 212 int32_t AudioCreateCapture(TestAudioManager *manager, int pins, const std::string &adapterName, 213 struct IAudioAdapter **adapter, struct IAudioCapture **capture, uint32_t *captureId); 214 215 int32_t FrameStart(struct AudioHeadInfo wavHeadInfo, struct IAudioRender *render, FILE *file, 216 struct AudioSampleAttributes attrs); 217 218 int32_t FrameStartCapture(struct IAudioCapture *capture, FILE *file, const struct AudioSampleAttributes attrs); 219 220 int32_t RenderFramePrepare(const std::string &path, char *&frame, uint64_t &numRead); 221 222 void FrameStatus(int status); 223 224 int32_t StartRecord(struct IAudioCapture *capture, FILE *file, uint64_t filesize); 225 226 int32_t AudioRenderStartAndOneFrame(struct IAudioRender *render); 227 228 int32_t StopAudio(struct PrepareAudioPara &audiopara); 229 230 int32_t ThreadRelease(struct PrepareAudioPara &audiopara); 231 232 int32_t AudioCaptureStartAndOneFrame(struct IAudioCapture *capture); 233 234 int32_t PlayAudioFile(struct PrepareAudioPara &audiopara); 235 236 int32_t RecordAudio(struct PrepareAudioPara &audiopara); 237 238 int32_t InitAttrsUpdate(struct AudioSampleAttributes &attrs, int format, uint32_t channelCount, 239 uint32_t sampleRate, uint32_t silenceThreshold = BUFFER_LENTH); 240 241 int32_t AudioRenderSetGetSampleAttributes(struct AudioSampleAttributes attrs, struct AudioSampleAttributes &attrsValue, 242 struct IAudioRender *render); 243 244 int32_t AudioCaptureSetGetSampleAttributes(struct AudioSampleAttributes attrs, struct AudioSampleAttributes &attrsValue, 245 struct IAudioCapture *capture); 246 247 int32_t InitMmapDesc(const std::string &path, struct AudioMmapBufferDescriptor &desc, int32_t &reqSize, bool flag); 248 249 int32_t PlayMapAudioFile(struct PrepareAudioPara &audiopara); 250 251 int32_t RecordMapAudio(struct PrepareAudioPara &audiopara); 252 253 int32_t AudioRenderCallback(struct IAudioCallback *self, enum AudioCallbackType type, int8_t* reserved, 254 int8_t* cookie); 255 256 int32_t CheckFlushValue(); 257 258 int32_t CheckRenderFullValue(); 259 260 int32_t CheckWriteCompleteValue(); 261 262 void TestReleaseAdapterDescs(struct AudioAdapterDescriptor **descs, uint32_t descsLen); 263 264 void TestAudioPortCapabilityFree(struct AudioPortCapability *dataBlock, bool freeSelf); 265 266 int32_t ReleaseCaptureSource(TestAudioManager *manager, struct IAudioAdapter *&adapter, 267 struct IAudioCapture *&capture, uint32_t captureId); 268 269 int32_t ReleaseRenderSource(TestAudioManager *manager, struct IAudioAdapter *&adapter, struct IAudioRender *&render, 270 uint32_t renderId); 271 272 int32_t GetCaptureBufferSize(struct IAudioCapture *capture, uint32_t &bufferSize); 273 } 274 } 275 #endif // AUDIO_IDLHDI_COMMON_H 276