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 #ifdef A2DP_HDI_SERVICE 128 #define A2DP_SUSPEND "A2dpSuspended" 129 #endif 130 131 #define TELHPONE_RATE 8000 132 #define BROADCAST_AM_RATE 11025 133 #define BROADCAST_FM_RATE 22050 134 #define MINI_CAM_DV_RATE 32000 135 #define MUSIC_RATE 44100 136 #define HIGHT_MUSIC_RATE 48000 137 #define AUDIO_SAMPLE_RATE_12000 12000 138 #define AUDIO_SAMPLE_RATE_16000 16000 139 #define AUDIO_SAMPLE_RATE_24000 24000 140 #define AUDIO_SAMPLE_RATE_64000 64000 141 #define AUDIO_SAMPLE_RATE_96000 96000 142 #define MAX_TIME_INFO_LEN 64 143 144 #ifndef OPEN_AUDIO_LOG_WITH_TIME 145 #define LOG_FUN_INFO() do { \ 146 printf("%s: [%s]: [%d]\n", __FILE__, __func__, __LINE__); \ 147 } while (0) 148 149 #define LOG_FUN_ERR(fmt, arg...) do { \ 150 printf("%s: [%s]: [%d]:[ERROR]:" fmt"\n", __FILE__, __func__, __LINE__, ##arg); \ 151 } while (0) 152 153 #define LOG_PARA_INFO(fmt, arg...) do { \ 154 printf("%s: [%s]: [%d]:[INFO]:" fmt"\n", __FILE__, __func__, __LINE__, ##arg); \ 155 } while (0) 156 #else 157 #define LOG_FUN_INFO() do { \ 158 } while (0) 159 160 #define LOG_FUN_ERR(fmt, arg...) do { \ 161 char s[MAX_TIME_INFO_LEN] = {0}; \ 162 AudioGetSysTime(s, MAX_TIME_INFO_LEN); \ 163 printf("%s %s: [%s]: [%d]:[ERROR]:" fmt"\n", s, __FILE__, __func__, __LINE__, ##arg); \ 164 } while (0) 165 166 #define LOG_PARA_INFO(fmt, arg...) do { \ 167 } while (0) 168 #endif 169 170 struct DevHandleCapture { 171 void *object; 172 }; 173 174 struct DevHandle { 175 void *object; 176 }; 177 178 struct AudioPortAndCapability { 179 struct AudioPort port; 180 struct AudioPortCapability capability; 181 AudioPortPassthroughMode mode; 182 }; 183 184 struct AudioHwAdapter { 185 struct AudioAdapter common; 186 struct AudioAdapterDescriptor adapterDescriptor; 187 struct AudioPortAndCapability *portCapabilitys; 188 struct HdfRemoteService *proxyRemoteHandle; // proxyRemoteHandle 189 int32_t adapterMgrRenderFlag; 190 }; 191 192 struct AudioFrameRenderMode { 193 uint64_t frames; 194 struct AudioTimeStamp time; 195 struct AudioSampleAttributes attrs; 196 AudioChannelMode mode; 197 uint32_t byteRate; 198 uint32_t periodSize; 199 uint32_t periodCount; 200 uint32_t startThreshold; 201 uint32_t stopThreshold; 202 uint32_t silenceThreshold; 203 uint32_t silenceSize; 204 char *buffer; 205 uint64_t bufferFrameSize; 206 uint64_t bufferSize; 207 RenderCallback callback; 208 void* cookie; 209 struct AudioMmapBufferDescriptor mmapBufDesc; 210 }; 211 212 struct AudioGain { 213 float gain; 214 float gainMin; 215 float gainMax; 216 }; 217 218 struct AudioVol { 219 int volMin; 220 int volMax; 221 }; 222 223 struct AudioCtlParam { 224 bool mute; 225 float volume; 226 float speed; 227 bool pause; 228 bool stop; 229 pthread_mutex_t mutex; 230 bool mutexFlag; 231 pthread_cond_t functionCond; 232 struct AudioVol volThreshold; 233 struct AudioGain audioGain; 234 }; 235 236 enum PathRoute { 237 DEEP_BUFF = 0, 238 RECORD, 239 RECORD_LOW_LATRNCY, 240 LOW_LATRNCY, 241 }; 242 243 struct PathPlan { 244 char pathPlanName[PATHPLAN_LEN]; 245 int value; 246 }; 247 248 struct PathDeviceSwitch { 249 char deviceSwitch[PATHPLAN_LEN]; 250 int32_t value; 251 }; 252 253 struct PathDeviceInfo { 254 char deviceType[NAME_LEN]; 255 int32_t deviceNum; 256 struct PathDeviceSwitch deviceSwitchs[PATHPLAN_COUNT]; 257 }; 258 259 struct PathSelect { 260 char useCase[NAME_LEN]; 261 struct PathDeviceInfo deviceInfo; 262 int useCaseDeviceNum; 263 struct PathPlan pathPlan[PATHPLAN_COUNT]; 264 }; 265 266 struct HwInfo { 267 uint32_t card; 268 uint32_t device; 269 int flags; 270 char adapterName[NAME_LEN]; 271 struct AudioPort portDescript; 272 struct AudioDeviceDescriptor deviceDescript; 273 PathRoute pathroute; 274 struct PathSelect pathSelect; 275 }; 276 277 struct AudioHwRenderMode { 278 struct AudioCtlParam ctlParam; 279 struct HwInfo hwInfo; 280 }; 281 282 struct AudioHwRenderParam { 283 struct AudioHwRenderMode renderMode; 284 struct AudioFrameRenderMode frameRenderMode; 285 }; 286 287 struct ErrorDump { 288 int32_t errorCode; 289 int32_t count; 290 unsigned long long frames; 291 char* reason; // Specific reasons for failure 292 char* currentTime; 293 }; 294 295 struct ErrorLog { 296 uint32_t totalErrors; 297 uint32_t iter; 298 struct ErrorDump errorDump[ERROR_LOG_MAX_NUM]; 299 }; 300 301 struct AudioHwRender { 302 struct AudioRender common; 303 struct AudioHwRenderParam renderParam; 304 struct HdfRemoteService *proxyRemoteHandle; // proxyRemoteHandle 305 struct ErrorLog errorLog; 306 }; 307 308 struct ParamValMap { 309 char key[EXTPARAM_LEN]; 310 char value[EXTPARAM_LEN]; 311 }; 312 313 struct ExtraParams { 314 int32_t route; 315 int32_t format; 316 uint32_t channels; 317 uint64_t frames; 318 uint32_t sampleRate; 319 bool flag; 320 #ifdef A2DP_HDI_SERVICE 321 uint32_t audioStreamCtl; 322 #endif 323 }; 324 325 enum ErrorDumpCode { 326 WRITE_FRAME_ERROR_CODE = -5, 327 }; 328 329 enum AudioServiceNameType { 330 AUDIO_SERVICE_IN = 0, 331 AUDIO_SERVICE_OUT, 332 AUDIO_SERVICE_MAX, 333 }; 334 335 int32_t GetAudioRenderFunc(struct AudioHwRender *hwRender); 336 int32_t CheckParaDesc(const struct AudioDeviceDescriptor *desc, const char *type); 337 int32_t CheckParaAttr(const struct AudioSampleAttributes *attrs); 338 int32_t AttrFormatToBit(const struct AudioSampleAttributes *attrs, int32_t *format); 339 int32_t InitHwRenderParam(struct AudioHwRender *hwRender, const struct AudioDeviceDescriptor *desc, 340 const struct AudioSampleAttributes *attrs); 341 int32_t InitForGetPortCapability(struct AudioPort portIndex, struct AudioPortCapability *capabilityIndex); 342 void AudioAdapterReleaseCapSubPorts(const struct AudioPortAndCapability *portCapabilitys, int32_t num); 343 int32_t AudioAdapterInitAllPorts(struct AudioAdapter *adapter); 344 void AudioReleaseRenderHandle(struct AudioHwRender *hwRender); 345 int32_t AudioAdapterCreateRenderPre(struct AudioHwRender *hwRender, const struct AudioDeviceDescriptor *desc, 346 const struct AudioSampleAttributes *attrs, const struct AudioHwAdapter *hwAdapter); 347 int32_t AudioAdapterBindServiceRender(struct AudioHwRender *hwRender); 348 int32_t AudioAdapterCreateRender(struct AudioAdapter *adapter, const struct AudioDeviceDescriptor *desc, 349 const struct AudioSampleAttributes *attrs, struct AudioRender **render); 350 int32_t AudioAdapterDestroyRender(struct AudioAdapter *adapter, struct AudioRender *render); 351 int32_t AudioAdapterGetPortCapability(struct AudioAdapter *adapter, const struct AudioPort *port, 352 struct AudioPortCapability *capability); 353 int32_t AudioAdapterSetPassthroughMode(struct AudioAdapter *adapter, const struct AudioPort *port, 354 AudioPortPassthroughMode mode); 355 int32_t AudioAdapterGetPassthroughMode(struct AudioAdapter *adapter, const struct AudioPort *port, 356 AudioPortPassthroughMode *mode); 357 int32_t AudioAdapterSetExtraParams(struct AudioAdapter *adapter, enum AudioExtParamKey key, 358 const char *condition, const char *value); 359 int32_t AudioAdapterGetExtraParams(struct AudioAdapter *adapter, enum AudioExtParamKey key, 360 const char *condition, char *value, int32_t length); 361 int32_t PcmBytesToFrames(const struct AudioFrameRenderMode *frameRenderMode, uint64_t bytes, uint32_t *frameCount); 362 int32_t AudioRenderStart(AudioHandle handle); 363 int32_t AudioRenderStop(AudioHandle handle); 364 int32_t AudioRenderPause(AudioHandle handle); 365 int32_t AudioRenderResume(AudioHandle handle); 366 int32_t AudioRenderFlush(AudioHandle handle); 367 int32_t AudioRenderGetFrameSize(AudioHandle handle, uint64_t *size); 368 int32_t AudioRenderGetFrameCount(AudioHandle handle, uint64_t *count); 369 int32_t AudioRenderSetSampleAttributes(AudioHandle handle, const struct AudioSampleAttributes *attrs); 370 int32_t AudioRenderGetSampleAttributes(AudioHandle handle, struct AudioSampleAttributes *attrs); 371 int32_t AudioRenderGetCurrentChannelId(AudioHandle handle, uint32_t *channelId); 372 int32_t AudioRenderCheckSceneCapability(AudioHandle handle, const struct AudioSceneDescriptor *scene, 373 bool *supported); 374 int32_t AudioRenderSelectScene(AudioHandle handle, const struct AudioSceneDescriptor *scene); 375 int32_t AudioRenderSetMute(AudioHandle handle, bool mute); 376 int32_t AudioRenderGetMute(AudioHandle handle, bool *mute); 377 int32_t AudioRenderSetVolume(AudioHandle handle, float volume); 378 int32_t AudioRenderGetVolume(AudioHandle handle, float *volume); 379 int32_t AudioRenderGetGainThreshold(AudioHandle handle, float *min, float *max); 380 int32_t AudioRenderGetGain(AudioHandle handle, float *gain); 381 int32_t AudioRenderSetGain(AudioHandle handle, float gain); 382 int32_t AudioRenderGetLatency(struct AudioRender *render, uint32_t *ms); 383 int32_t AudioRenderRenderFrame(struct AudioRender *render, const void *frame, 384 uint64_t requestBytes, uint64_t *replyBytes); 385 int32_t AudioRenderGetRenderPosition(struct AudioRender *render, uint64_t *frames, struct AudioTimeStamp *time); 386 int32_t AudioRenderSetRenderSpeed(struct AudioRender *render, float speed); 387 int32_t AudioRenderGetRenderSpeed(struct AudioRender *render, float *speed); 388 int32_t AudioRenderSetChannelMode(struct AudioRender *render, AudioChannelMode mode); 389 int32_t AudioRenderGetChannelMode(struct AudioRender *render, AudioChannelMode *mode); 390 int32_t AudioRenderSetExtraParams(AudioHandle handle, const char *keyValueList); 391 int32_t AudioRenderGetExtraParams(AudioHandle handle, char *keyValueList, int32_t listLenth); 392 int32_t AudioRenderReqMmapBuffer(AudioHandle handle, int32_t reqSize, struct AudioMmapBufferDescriptor *desc); 393 int32_t AudioRenderGetMmapPosition(AudioHandle handle, uint64_t *frames, struct AudioTimeStamp *time); 394 int32_t AudioRenderTurnStandbyMode(AudioHandle handle); 395 int32_t AudioRenderAudioDevDump(AudioHandle handle, int32_t range, int32_t fd); 396 int32_t AudioRenderRegCallback(struct AudioRender *render, RenderCallback callback, void* cookie); 397 int32_t AudioRenderDrainBuffer(struct AudioRender *render, AudioDrainNotifyType *type); 398 399 #ifdef __cplusplus 400 } 401 #endif 402 } 403 #endif 404