• 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 #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     int32_t adapterMgrCaptureFlag;
191 };
192 
193 struct AudioFrameRenderMode {
194     uint64_t frames;
195     struct AudioTimeStamp time;
196     struct AudioSampleAttributes attrs;
197     AudioChannelMode mode;
198     uint32_t byteRate;
199     uint32_t periodSize;
200     uint32_t periodCount;
201     uint32_t startThreshold;
202     uint32_t stopThreshold;
203     uint32_t silenceThreshold;
204     uint32_t silenceSize;
205     char *buffer;
206     uint64_t bufferFrameSize;
207     uint64_t bufferSize;
208     RenderCallback callback;
209     void* cookie;
210     struct AudioMmapBufferDescriptor mmapBufDesc;
211 };
212 
213 struct AudioFrameCaptureMode {
214     uint64_t frames;
215     struct AudioTimeStamp time;
216     struct AudioSampleAttributes attrs;
217     uint32_t byteRate;
218     uint32_t periodSize;
219     uint32_t periodCount;
220 };
221 
222 struct AudioGain {
223     float gain;
224     float gainMin;
225     float gainMax;
226 };
227 
228 struct AudioVol {
229     int volMin;
230     int volMax;
231 };
232 
233 struct AudioCtlParam {
234     bool mute;
235     float volume;
236     float speed;
237     bool pause;
238     bool stop;
239     pthread_mutex_t mutex;
240     bool mutexFlag;
241     pthread_cond_t functionCond;
242     struct AudioVol volThreshold;
243     struct AudioGain audioGain;
244 };
245 
246 enum PathRoute {
247     DEEP_BUFF = 0,
248     RECORD,
249     RECORD_LOW_LATRNCY,
250     LOW_LATRNCY,
251 };
252 
253 struct PathPlan {
254     char pathPlanName[PATHPLAN_LEN];
255     int value;
256 };
257 
258 struct PathDeviceSwitch {
259     char deviceSwitch[PATHPLAN_LEN];
260     int32_t value;
261 };
262 
263 struct PathDeviceInfo {
264     char deviceType[NAME_LEN];
265     int32_t deviceNum;
266     struct PathDeviceSwitch deviceSwitchs[PATHPLAN_COUNT];
267 };
268 
269 struct PathSelect {
270     char useCase[NAME_LEN];
271     struct PathDeviceInfo deviceInfo;
272     int useCaseDeviceNum;
273     struct PathPlan pathPlan[PATHPLAN_COUNT];
274 };
275 
276 struct HwInfo {
277     uint32_t card;
278     uint32_t device;
279     int flags;
280     char adapterName[NAME_LEN];
281     struct AudioPort portDescript;
282     struct AudioDeviceDescriptor deviceDescript;
283     PathRoute pathroute;
284     struct PathSelect pathSelect;
285 };
286 
287 struct AudioHwRenderMode {
288     struct AudioCtlParam ctlParam;
289     struct HwInfo hwInfo;
290 };
291 
292 struct AudioHwRenderParam {
293     struct AudioHwRenderMode renderMode;
294     struct AudioFrameRenderMode frameRenderMode;
295 };
296 
297 struct AudioHwCaptureMode {
298     struct AudioCtlParam ctlParam;
299     struct HwInfo hwInfo;
300 };
301 
302 struct AudioHwCaptureParam {
303     struct AudioHwCaptureMode captureMode;
304     struct AudioFrameCaptureMode frameCaptureMode;
305 };
306 
307 struct ErrorDump {
308     int32_t errorCode;
309     int32_t count;
310     unsigned long long frames;
311     char* reason;                       // Specific reasons for failure
312     char* currentTime;
313 };
314 
315 struct ErrorLog {
316     uint32_t totalErrors;
317     uint32_t iter;
318     struct ErrorDump errorDump[ERROR_LOG_MAX_NUM];
319 };
320 
321 struct AudioHwRender {
322     struct AudioRender common;
323     struct AudioHwRenderParam renderParam;
324     struct HdfRemoteService *proxyRemoteHandle; // proxyRemoteHandle
325     struct ErrorLog errorLog;
326 };
327 
328 struct AudioHwCapture {
329     struct AudioCapture common;
330     struct AudioHwCaptureParam captureParam;
331     struct HdfRemoteService *proxyRemoteHandle; // proxyRemoteHandle
332     struct ErrorLog errorLog;
333 };
334 
335 struct ParamValMap {
336     char key[EXTPARAM_LEN];
337     char value[EXTPARAM_LEN];
338 };
339 
340 struct ExtraParams {
341     int32_t route;
342     int32_t format;
343     uint32_t channels;
344     uint64_t frames;
345     uint32_t sampleRate;
346     bool flag;
347 #ifdef A2DP_HDI_SERVICE
348     uint32_t audioStreamCtl;
349 #endif
350 };
351 
352 enum ErrorDumpCode {
353     WRITE_FRAME_ERROR_CODE = -5,
354 };
355 
356 enum AudioServiceNameType {
357     AUDIO_SERVICE_IN = 0,
358     AUDIO_SERVICE_OUT,
359     AUDIO_SERVICE_MAX,
360 };
361 
362 int32_t GetAudioRenderFunc(struct AudioHwRender *hwRender, const char *adapterName);
363 int32_t CheckParaDesc(const struct AudioDeviceDescriptor *desc, const char *type);
364 int32_t CheckParaAttr(const struct AudioSampleAttributes *attrs);
365 int32_t AttrFormatToBit(const struct AudioSampleAttributes *attrs, int32_t *format);
366 int32_t InitHwRenderParam(struct AudioHwRender *hwRender, const struct AudioDeviceDescriptor *desc,
367                           const struct AudioSampleAttributes *attrs);
368 int32_t InitForGetPortCapability(struct AudioPort portIndex, struct AudioPortCapability *capabilityIndex);
369 void AudioAdapterReleaseCapSubPorts(const struct AudioPortAndCapability *portCapabilitys, int32_t num);
370 int32_t AudioAdapterInitAllPorts(struct AudioAdapter *adapter);
371 void AudioReleaseRenderHandle(struct AudioHwRender *hwRender);
372 int32_t AudioAdapterCreateRenderPre(struct AudioHwRender *hwRender, const struct AudioDeviceDescriptor *desc,
373                                     const struct AudioSampleAttributes *attrs, const struct AudioHwAdapter *hwAdapter);
374 int32_t AudioAdapterBindServiceRender(struct AudioHwRender *hwRender);
375 int32_t AudioAdapterCreateRender(struct AudioAdapter *adapter, const struct AudioDeviceDescriptor *desc,
376                                  const struct AudioSampleAttributes *attrs, struct AudioRender **render);
377 int32_t AudioAdapterDestroyRender(struct AudioAdapter *adapter, struct AudioRender *render);
378 int32_t AudioAdapterGetPortCapability(struct AudioAdapter *adapter, const struct AudioPort *port,
379                                       struct AudioPortCapability *capability);
380 int32_t AudioAdapterSetPassthroughMode(struct AudioAdapter *adapter, const struct AudioPort *port,
381                                        AudioPortPassthroughMode mode);
382 int32_t AudioAdapterGetPassthroughMode(struct AudioAdapter *adapter, const struct AudioPort *port,
383                                        AudioPortPassthroughMode *mode);
384 int32_t AudioAdapterSetExtraParams(struct AudioAdapter *adapter, enum AudioExtParamKey key,
385                                    const char *condition, const char *value);
386 int32_t AudioAdapterGetExtraParams(struct AudioAdapter *adapter, enum AudioExtParamKey key,
387                                    const char *condition, char *value, int32_t length);
388 int32_t PcmBytesToFrames(const struct AudioFrameRenderMode *frameRenderMode, uint64_t bytes, uint32_t *frameCount);
389 int32_t AudioRenderStart(AudioHandle handle);
390 int32_t AudioRenderStop(AudioHandle handle);
391 int32_t AudioRenderPause(AudioHandle handle);
392 int32_t AudioRenderResume(AudioHandle handle);
393 int32_t AudioRenderFlush(AudioHandle handle);
394 int32_t AudioRenderGetFrameSize(AudioHandle handle, uint64_t *size);
395 int32_t AudioRenderGetFrameCount(AudioHandle handle, uint64_t *count);
396 int32_t AudioRenderSetSampleAttributes(AudioHandle handle, const struct AudioSampleAttributes *attrs);
397 int32_t AudioRenderGetSampleAttributes(AudioHandle handle, struct AudioSampleAttributes *attrs);
398 int32_t AudioRenderGetCurrentChannelId(AudioHandle handle, uint32_t *channelId);
399 int32_t AudioRenderCheckSceneCapability(AudioHandle handle, const struct AudioSceneDescriptor *scene,
400                                         bool *supported);
401 int32_t AudioRenderSelectScene(AudioHandle handle, const struct AudioSceneDescriptor *scene);
402 int32_t AudioRenderSetMute(AudioHandle handle, bool mute);
403 int32_t AudioRenderGetMute(AudioHandle handle, bool *mute);
404 int32_t AudioRenderSetVolume(AudioHandle handle, float volume);
405 int32_t AudioRenderGetVolume(AudioHandle handle, float *volume);
406 int32_t AudioRenderGetGainThreshold(AudioHandle handle, float *min, float *max);
407 int32_t AudioRenderGetGain(AudioHandle handle, float *gain);
408 int32_t AudioRenderSetGain(AudioHandle handle, float gain);
409 int32_t AudioRenderGetLatency(struct AudioRender *render, uint32_t *ms);
410 int32_t AudioRenderRenderFrame(struct AudioRender *render, const void *frame,
411                                uint64_t requestBytes, uint64_t *replyBytes);
412 int32_t AudioRenderGetRenderPosition(struct AudioRender *render, uint64_t *frames, struct AudioTimeStamp *time);
413 int32_t AudioRenderSetRenderSpeed(struct AudioRender *render, float speed);
414 int32_t AudioRenderGetRenderSpeed(struct AudioRender *render, float *speed);
415 int32_t AudioRenderSetChannelMode(struct AudioRender *render, AudioChannelMode mode);
416 int32_t AudioRenderGetChannelMode(struct AudioRender *render, AudioChannelMode *mode);
417 int32_t AudioRenderSetExtraParams(AudioHandle handle, const char *keyValueList);
418 int32_t AudioRenderGetExtraParams(AudioHandle handle, char *keyValueList, int32_t listLenth);
419 int32_t AudioRenderReqMmapBuffer(AudioHandle handle, int32_t reqSize, struct AudioMmapBufferDescriptor *desc);
420 int32_t AudioRenderGetMmapPosition(AudioHandle handle, uint64_t *frames, struct AudioTimeStamp *time);
421 int32_t AudioRenderTurnStandbyMode(AudioHandle handle);
422 int32_t AudioRenderAudioDevDump(AudioHandle handle, int32_t range, int32_t fd);
423 int32_t AudioRenderRegCallback(struct AudioRender *render, RenderCallback callback, void* cookie);
424 int32_t AudioRenderDrainBuffer(struct AudioRender *render, AudioDrainNotifyType *type);
425 int32_t InitHwCaptureParam(struct AudioHwCapture *hwCapture, const struct AudioDeviceDescriptor *desc,
426     const struct AudioSampleAttributes *attrs);
427 int32_t GetAudioCaptureFunc(struct AudioHwCapture *hwCapture, const char *adapterName);
428 int32_t AudioAdapterCreateCapture(struct AudioAdapter *adapter, const struct AudioDeviceDescriptor *desc,
429     const struct AudioSampleAttributes *attrs, struct AudioCapture **capture);
430 int32_t AudioAdapterDestroyCapture(struct AudioAdapter *adapter, struct AudioCapture *capture);
431 int32_t AudioCaptureStart(AudioHandle handle);
432 int32_t AudioCaptureStop(AudioHandle handle);
433 int32_t AudioCapturePause(AudioHandle handle);
434 int32_t AudioCaptureResume(AudioHandle handle);
435 int32_t AudioCaptureFlush(AudioHandle handle);
436 int32_t AudioCaptureSetMute(AudioHandle handle, bool mute);
437 int32_t AudioCaptureGetMute(AudioHandle handle, bool *mute);
438 int32_t AudioCaptureCaptureFrame(struct AudioCapture *capture, void *frame, uint64_t requestBytes,
439     uint64_t *replyBytes);
440 void AudioReleaseCaptureHandle(struct AudioHwCapture *hwCapture);
441 
442 int32_t HearingAidStart(AudioHandle handle);
443 int32_t HearingAidStop(AudioHandle handle);
444 int32_t HearingAidPause(AudioHandle handle);
445 int32_t HearingAidResume(AudioHandle handle);
446 int32_t HearingAidFlush(AudioHandle handle);
447 int32_t HearingAidGetFrameSize(AudioHandle handle, uint64_t *size);
448 int32_t HearingAidGetFrameCount(AudioHandle handle, uint64_t *count);
449 int32_t HearingAidSetSampleAttributes(AudioHandle handle, const struct AudioSampleAttributes *attrs);
450 int32_t HearingAidGetSampleAttributes(AudioHandle handle, struct AudioSampleAttributes *attrs);
451 int32_t HearingAidGetCurrentChannelId(AudioHandle handle, uint32_t *channelId);
452 int32_t HearingAidCheckSceneCapability(AudioHandle handle, const struct AudioSceneDescriptor *scene, bool *supported);
453 int32_t HearingAidSelectScene(AudioHandle handle, const struct AudioSceneDescriptor *scene);
454 int32_t HearingAidSetMute(AudioHandle handle, bool mute);
455 int32_t HearingAidGetMute(AudioHandle handle, bool *mute);
456 int32_t HearingAidSetVolume(AudioHandle handle, float volume);
457 int32_t HearingAidGetVolume(AudioHandle handle, float *volume);
458 int32_t HearingAidGetGainThreshold(AudioHandle handle, float *min, float *max);
459 int32_t HearingAidGetGain(AudioHandle handle, float *gain);
460 int32_t HearingAidSetGain(AudioHandle handle, float gain);
461 int32_t HearingAidGetLatency(struct AudioRender *render, uint32_t *ms);
462 int32_t HearingAidRenderFrame(
463     struct AudioRender *render, const void *frame, uint64_t requestBytes, uint64_t *replyBytes);
464 int32_t HearingAidGetRenderPosition(struct AudioRender *render, uint64_t *frames, struct AudioTimeStamp *time);
465 int32_t HearingAidSetRenderSpeed(struct AudioRender *render, float speed);
466 int32_t HearingAidGetRenderSpeed(struct AudioRender *render, float *speed);
467 int32_t HearingAidSetChannelMode(struct AudioRender *render, AudioChannelMode mode);
468 int32_t HearingAidGetChannelMode(struct AudioRender *render, AudioChannelMode *mode);
469 int32_t HearingAidSetExtraParams(AudioHandle handle, const char *keyValueList);
470 int32_t HearingAidGetExtraParams(AudioHandle handle, char *keyValueList, int32_t listLength);
471 int32_t HearingAidReqMmapBuffer(AudioHandle handle, int32_t reqSize, struct AudioMmapBufferDescriptor *desc);
472 int32_t HearingAidGetMmapPosition(AudioHandle handle, uint64_t *frames, struct AudioTimeStamp *time);
473 int32_t HearingAidTurnStandbyMode(AudioHandle handle);
474 int32_t HearingAidAudioDevDump(AudioHandle handle, int32_t range, int32_t fd);
475 int32_t HearingAidRegCallback(struct AudioRender *render, RenderCallback callback, void *cookie);
476 int32_t HearingAidDrainBuffer(struct AudioRender *render, AudioDrainNotifyType *type);
477 
478 #ifdef __cplusplus
479 }
480 #endif
481 }
482 #endif
483