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 #ifndef AUDIO_INTERNAL_H 17 #define AUDIO_INTERNAL_H 18 19 #include <math.h> 20 #include <sys/mman.h> 21 #include <sys/types.h> 22 #include <unistd.h> 23 #include <pthread.h> 24 #include <errno.h> 25 #include <servmgr_hdi.h> 26 #include "audio_manager.h" 27 #include "audio_common.h" 28 #include "hdf_base.h" 29 namespace OHOS::HDI::Audio_Bluetooth { 30 #ifdef __cplusplus 31 extern "C" { 32 #endif 33 34 #define LOG_ENABLE 1 35 #define LOGV_ENABLE 1 36 #define NAME_LEN 64 37 #define BIT_NUM_32 32 38 #define BIT_NUM_24 24 39 #define BIT_NUM_16 16 40 #define BIT_NUM_8 8 41 #define PERIOD_SIZE 1024 42 #define PERIOD_COUNT 2 43 #define FRAME_DATA (8192 * 2) 44 #define PATHPLAN_LEN 64 45 #define PATHPLAN_COUNT 32 46 #define PATH_NAME_LEN 128 47 #define VOLUME_CHANGE 100 48 #define SEC_TO_NSEC 1000000000 49 #define HDMI_PORT_ID 12 50 #define MAP_MAX 100 51 #define FORMAT_ONE "%-5d %-10d %-20llu %-15s %s\n" 52 #define FORMAT_TWO "%-5d %-10d %s\n" 53 #define ERROR_LOG_MAX_NUM 8 54 #define ERROR_REASON_DESC_LEN 64 55 #define RANGE_MAX 5 56 #define RANGE_MIN 4 57 #define EXTPARAM_LEN 32 58 59 /** 60 * @brief Enumerates HAL return value types. 61 */ 62 typedef enum { 63 AUDIO_HAL_SUCCESS = 0, 64 AUDIO_HAL_ERR_INTERNAL = -1, /* audio system internal errors */ 65 AUDIO_HAL_ERR_NOT_SUPPORT = -2, /* operation is not supported */ 66 AUDIO_HAL_ERR_INVALID_PARAM = -3, /* parameter is invalid */ 67 AUDIO_HAL_ERR_INVALID_OBJECT = -4, /**< Invalid object. */ 68 AUDIO_HAL_ERR_MALLOC_FAIL = -6, /**< Memory allocation fails. */ 69 70 #define HDF_AUDIO_HAL_ERR_START (-7000) /**< Defines the start of the device module error codes. */ 71 #define HDF_AUDIO_HAL_ERR_NUM(v) (HDF_AUDIO_HAL_ERR_START + (v)) /**< Defines the device module error codes. */ 72 AUDIO_HAL_ERR_NOTREADY = HDF_AUDIO_HAL_ERR_NUM(-1), /* audio adapter is not ready */ 73 AUDIO_HAL_ERR_AI_BUSY = HDF_AUDIO_HAL_ERR_NUM(-2), /* audio capture is busy now */ 74 AUDIO_HAL_ERR_AO_BUSY = HDF_AUDIO_HAL_ERR_NUM(-3), /* audio render is busy now */ 75 } AUDIO_HAL_ERR_CODE; 76 77 #ifndef LOGE 78 #define LOGE(fmt, arg...) printf("[Audio:E]" fmt "\n", ##arg) 79 #endif 80 #ifndef LOGI 81 #define LOGI(fmt, arg...) printf("[Audio:I]" fmt "\n", ##arg) 82 #endif 83 #ifndef LOGV 84 #define LOGV(fmt, arg...) printf("[Audio:V]" fmt "\n", ##arg) 85 #endif 86 87 #if !LOG_ENABLE 88 #undef LOGE 89 #undef LOGI 90 #undef LOGV 91 92 #define LOGE(...) 93 #define LOGI(...) 94 #define LOGV(...) 95 #else 96 #if !LOGV_ENABLE 97 #undef LOGV 98 #define LOGV(...) 99 #endif // !LOGV_ENABLE 100 #endif // LOG_ENABLE 101 102 #ifndef UNUSED 103 #define UNUSED(x) ((void)(x)) 104 #endif 105 106 #ifndef UT_TEST 107 #define STATIC_T static 108 #else 109 #define STATIC_T 110 #endif 111 112 #define USECASE_AUDIO_RENDER_DEEP_BUFFER "deep-buffer-render" 113 #define USECASE_AUDIO_RENDER_LOW_LATENCY "low-latency-render" 114 115 #define AUDIO_ATTR_PARAM_ROUTE "attr-route" 116 #define ROUTE_SAMPLE "attr-route=x;" 117 #define AUDIO_ATTR_PARAM_FORMAT "attr-format" 118 #define FORMAT_SAMPLE "attr-format=xx;" 119 #define AUDIO_ATTR_PARAM_CHANNELS "attr-channels" 120 #define CHANNELS_SAMPLE "attr-channels=x;" 121 #define AUDIO_ATTR_PARAM_FRAME_COUNT "attr-frame-count" 122 #define FRAME_COUNT_SAMPLE "attr-frame-count=xxxxxxxxxxxxxxxxxxxx;" 123 #define AUDIO_ATTR_PARAM_SAMPLING_RATE "attr-sampling-rate" 124 #define SAMPLING_RATE_SAMPLE "attr-sampling-rate=xxxxx" 125 #define AUDIO_ATTR_PARAM_CONNECT "usb-connect" 126 #define AUDIO_ATTR_PARAM_DISCONNECT "usb-disconnect" 127 #define TELHPONE_RATE 8000 128 #define BROADCAST_AM_RATE 11025 129 #define BROADCAST_FM_RATE 22050 130 #define MINI_CAM_DV_RATE 32000 131 #define MUSIC_RATE 44100 132 #define HIGHT_MUSIC_RATE 48000 133 #define AUDIO_SAMPLE_RATE_12000 12000 134 #define AUDIO_SAMPLE_RATE_16000 16000 135 #define AUDIO_SAMPLE_RATE_24000 24000 136 #define AUDIO_SAMPLE_RATE_64000 64000 137 #define AUDIO_SAMPLE_RATE_96000 96000 138 #define MAX_TIME_INFO_LEN 64 139 140 #ifndef OPEN_AUDIO_LOG_WITH_TIME 141 #define LOG_FUN_INFO() do { \ 142 printf("%s: [%s]: [%d]\n", __FILE__, __func__, __LINE__); \ 143 } while (0) 144 145 #define LOG_FUN_ERR(fmt, arg...) do { \ 146 printf("%s: [%s]: [%d]:[ERROR]:" fmt"\n", __FILE__, __func__, __LINE__, ##arg); \ 147 } while (0) 148 149 #define LOG_PARA_INFO(fmt, arg...) do { \ 150 printf("%s: [%s]: [%d]:[INFO]:" fmt"\n", __FILE__, __func__, __LINE__, ##arg); \ 151 } while (0) 152 #else 153 #define LOG_FUN_INFO() do { \ 154 } while (0) 155 156 #define LOG_FUN_ERR(fmt, arg...) do { \ 157 char s[MAX_TIME_INFO_LEN] = {0}; \ 158 AudioGetSysTime(s, MAX_TIME_INFO_LEN); \ 159 printf("%s %s: [%s]: [%d]:[ERROR]:" fmt"\n", s, __FILE__, __func__, __LINE__, ##arg); \ 160 } while (0) 161 162 #define LOG_PARA_INFO(fmt, arg...) do { \ 163 } while (0) 164 #endif 165 166 struct DevHandleCapture { 167 void *object; 168 }; 169 170 struct DevHandle { 171 void *object; 172 }; 173 174 struct AudioPortAndCapability { 175 struct AudioPort port; 176 struct AudioPortCapability capability; 177 AudioPortPassthroughMode mode; 178 }; 179 180 struct AudioHwAdapter { 181 struct AudioAdapter common; 182 struct AudioAdapterDescriptor adapterDescriptor; 183 struct AudioPortAndCapability *portCapabilitys; 184 struct HdfRemoteService *proxyRemoteHandle; // proxyRemoteHandle 185 int32_t adapterMgrRenderFlag; 186 }; 187 188 struct AudioFrameRenderMode { 189 uint64_t frames; 190 struct AudioTimeStamp time; 191 struct AudioSampleAttributes attrs; 192 AudioChannelMode mode; 193 uint32_t byteRate; 194 uint32_t periodSize; 195 uint32_t periodCount; 196 uint32_t startThreshold; 197 uint32_t stopThreshold; 198 uint32_t silenceThreshold; 199 uint32_t silenceSize; 200 char *buffer; 201 uint64_t bufferFrameSize; 202 uint64_t bufferSize; 203 RenderCallback callback; 204 void* cookie; 205 struct AudioMmapBufferDescripter mmapBufDesc; 206 }; 207 208 struct AudioGain { 209 float gain; 210 float gainMin; 211 float gainMax; 212 }; 213 214 struct AudioVol { 215 int volMin; 216 int volMax; 217 }; 218 219 struct AudioCtlParam { 220 bool mute; 221 float volume; 222 float speed; 223 bool pause; 224 bool stop; 225 pthread_mutex_t mutex; 226 bool mutexFlag; 227 pthread_cond_t functionCond; 228 struct AudioVol volThreshold; 229 struct AudioGain audioGain; 230 }; 231 232 enum PathRoute { 233 DEEP_BUFF = 0, 234 RECORD, 235 RECORD_LOW_LATRNCY, 236 LOW_LATRNCY, 237 }; 238 239 struct PathPlan { 240 char pathPlanName[PATHPLAN_LEN]; 241 int value; 242 }; 243 244 struct PathDeviceSwitch { 245 char deviceSwitch[PATHPLAN_LEN]; 246 int32_t value; 247 }; 248 249 struct PathDeviceInfo { 250 char deviceType[NAME_LEN]; 251 int32_t deviceNum; 252 struct PathDeviceSwitch deviceSwitchs[PATHPLAN_COUNT]; 253 }; 254 255 struct PathSelect { 256 char useCase[NAME_LEN]; 257 struct PathDeviceInfo deviceInfo; 258 int useCaseDeviceNum; 259 struct PathPlan pathPlan[PATHPLAN_COUNT]; 260 }; 261 262 struct HwInfo { 263 uint32_t card; 264 uint32_t device; 265 int flags; 266 char adapterName[NAME_LEN]; 267 struct AudioPort portDescript; 268 struct AudioDeviceDescriptor deviceDescript; 269 PathRoute pathroute; 270 struct PathSelect pathSelect; 271 }; 272 273 struct AudioHwRenderMode { 274 struct AudioCtlParam ctlParam; 275 struct HwInfo hwInfo; 276 }; 277 278 struct AudioHwRenderParam { 279 struct AudioHwRenderMode renderMode; 280 struct AudioFrameRenderMode frameRenderMode; 281 }; 282 283 struct ErrorDump { 284 int32_t errorCode; 285 int32_t count; 286 unsigned long long frames; 287 char* reason; // Specific reasons for failure 288 char* currentTime; 289 }; 290 291 struct ErrorLog { 292 uint32_t totalErrors; 293 uint32_t iter; 294 struct ErrorDump errorDump[ERROR_LOG_MAX_NUM]; 295 }; 296 297 struct AudioHwRender { 298 struct AudioRender common; 299 struct AudioHwRenderParam renderParam; 300 struct HdfRemoteService *proxyRemoteHandle; // proxyRemoteHandle 301 struct ErrorLog errorLog; 302 }; 303 304 struct ParamValMap { 305 char key[EXTPARAM_LEN]; 306 char value[EXTPARAM_LEN]; 307 }; 308 309 struct ExtraParams { 310 int32_t route; 311 int32_t format; 312 uint32_t channels; 313 uint64_t frames; 314 uint32_t sampleRate; 315 bool flag; 316 }; 317 318 enum ErrorDumpCode { 319 WRITE_FRAME_ERROR_CODE = -5, 320 }; 321 322 enum AudioServiceNameType { 323 AUDIO_SERVICE_IN = 0, 324 AUDIO_SERVICE_OUT, 325 AUDIO_SERVICE_MAX, 326 }; 327 328 int32_t GetAudioRenderFunc(struct AudioHwRender *hwRender); 329 int32_t CheckParaDesc(const struct AudioDeviceDescriptor *desc, const char *type); 330 int32_t CheckParaAttr(const struct AudioSampleAttributes *attrs); 331 int32_t AttrFormatToBit(const struct AudioSampleAttributes *attrs, int32_t *format); 332 int32_t InitHwRenderParam(struct AudioHwRender *hwRender, const struct AudioDeviceDescriptor *desc, 333 const struct AudioSampleAttributes *attrs); 334 int32_t InitForGetPortCapability(struct AudioPort portIndex, struct AudioPortCapability *capabilityIndex); 335 void AudioAdapterReleaseCapSubPorts(const struct AudioPortAndCapability *portCapabilitys, int32_t num); 336 int32_t AudioAdapterInitAllPorts(struct AudioAdapter *adapter); 337 void AudioReleaseRenderHandle(struct AudioHwRender *hwRender); 338 int32_t AudioAdapterCreateRenderPre(struct AudioHwRender *hwRender, const struct AudioDeviceDescriptor *desc, 339 const struct AudioSampleAttributes *attrs, const struct AudioHwAdapter *hwAdapter); 340 int32_t AudioAdapterBindServiceRender(struct AudioHwRender *hwRender); 341 int32_t AudioAdapterCreateRender(struct AudioAdapter *adapter, const struct AudioDeviceDescriptor *desc, 342 const struct AudioSampleAttributes *attrs, struct AudioRender **render); 343 int32_t AudioAdapterDestroyRender(struct AudioAdapter *adapter, struct AudioRender *render); 344 int32_t AudioAdapterGetPortCapability(struct AudioAdapter *adapter, const struct AudioPort *port, 345 struct AudioPortCapability *capability); 346 int32_t AudioAdapterSetPassthroughMode(struct AudioAdapter *adapter, const struct AudioPort *port, 347 AudioPortPassthroughMode mode); 348 int32_t AudioAdapterGetPassthroughMode(struct AudioAdapter *adapter, const struct AudioPort *port, 349 AudioPortPassthroughMode *mode); 350 int32_t PcmBytesToFrames(const struct AudioFrameRenderMode *frameRenderMode, uint64_t bytes, uint32_t *frameCount); 351 int32_t AudioRenderStart(AudioHandle handle); 352 int32_t AudioRenderStop(AudioHandle handle); 353 int32_t AudioRenderPause(AudioHandle handle); 354 int32_t AudioRenderResume(AudioHandle handle); 355 int32_t AudioRenderFlush(AudioHandle handle); 356 int32_t AudioRenderGetFrameSize(AudioHandle handle, uint64_t *size); 357 int32_t AudioRenderGetFrameCount(AudioHandle handle, uint64_t *count); 358 int32_t AudioRenderSetSampleAttributes(AudioHandle handle, const struct AudioSampleAttributes *attrs); 359 int32_t AudioRenderGetSampleAttributes(AudioHandle handle, struct AudioSampleAttributes *attrs); 360 int32_t AudioRenderGetCurrentChannelId(AudioHandle handle, uint32_t *channelId); 361 int32_t AudioRenderCheckSceneCapability(AudioHandle handle, const struct AudioSceneDescriptor *scene, 362 bool *supported); 363 int32_t AudioRenderSelectScene(AudioHandle handle, const struct AudioSceneDescriptor *scene); 364 int32_t AudioRenderSetMute(AudioHandle handle, bool mute); 365 int32_t AudioRenderGetMute(AudioHandle handle, bool *mute); 366 int32_t AudioRenderSetVolume(AudioHandle handle, float volume); 367 int32_t AudioRenderGetVolume(AudioHandle handle, float *volume); 368 int32_t AudioRenderGetGainThreshold(AudioHandle handle, float *min, float *max); 369 int32_t AudioRenderGetGain(AudioHandle handle, float *gain); 370 int32_t AudioRenderSetGain(AudioHandle handle, float gain); 371 int32_t AudioRenderGetLatency(struct AudioRender *render, uint32_t *ms); 372 int32_t AudioRenderRenderFrame(struct AudioRender *render, const void *frame, 373 uint64_t requestBytes, uint64_t *replyBytes); 374 int32_t AudioRenderGetRenderPosition(struct AudioRender *render, uint64_t *frames, struct AudioTimeStamp *time); 375 int32_t AudioRenderSetRenderSpeed(struct AudioRender *render, float speed); 376 int32_t AudioRenderGetRenderSpeed(struct AudioRender *render, float *speed); 377 int32_t AudioRenderSetChannelMode(struct AudioRender *render, AudioChannelMode mode); 378 int32_t AudioRenderGetChannelMode(struct AudioRender *render, AudioChannelMode *mode); 379 int32_t AudioRenderSetExtraParams(AudioHandle handle, const char *keyValueList); 380 int32_t AudioRenderGetExtraParams(AudioHandle handle, char *keyValueList, int32_t listLenth); 381 int32_t AudioRenderReqMmapBuffer(AudioHandle handle, int32_t reqSize, struct AudioMmapBufferDescripter *desc); 382 int32_t AudioRenderGetMmapPosition(AudioHandle handle, uint64_t *frames, struct AudioTimeStamp *time); 383 int32_t AudioRenderTurnStandbyMode(AudioHandle handle); 384 int32_t AudioRenderAudioDevDump(AudioHandle handle, int32_t range, int32_t fd); 385 int32_t AudioRenderRegCallback(struct AudioRender *render, RenderCallback callback, void* cookie); 386 int32_t AudioRenderDrainBuffer(struct AudioRender *render, AudioDrainNotifyType *type); 387 388 #ifdef __cplusplus 389 } 390 #endif 391 } 392 #endif 393