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 #include "audio_internal.h"
17 #include "audio_hal_log.h"
18 #include "osal_time.h"
19
20 #define HDF_LOG_TAG hal_audio_common
21
AudioDlClose(void ** ppHandleSo)22 void AudioDlClose(void **ppHandleSo)
23 {
24 if ((ppHandleSo != NULL) && ((*ppHandleSo) != NULL)) {
25 dlclose(*ppHandleSo);
26 *ppHandleSo = NULL;
27 }
28 return;
29 }
30
AudioMemFree(void ** ppMem)31 void AudioMemFree(void **ppMem)
32 {
33 if ((ppMem != NULL) && ((*ppMem) != NULL)) {
34 free(*ppMem);
35 *ppMem = NULL;
36 }
37 return;
38 }
39
AudioGetSysTime(char * s,int32_t len)40 int32_t AudioGetSysTime(char *s, int32_t len)
41 {
42 OsalTimespec time;
43 if (s == NULL) {
44 return -1;
45 }
46 OsalGetTime(&time);
47 s[0] = 0;
48 int32_t ret = snprintf_s(s, len, len - 1, "[%llu.%llu]", time.sec, time.usec);
49 return ret;
50 }
51
CheckAttrSamplingRate(uint32_t param)52 int32_t CheckAttrSamplingRate(uint32_t param)
53 {
54 switch (param) {
55 case TELHPONE_RATE:
56 case BROADCAST_AM_RATE:
57 case BROADCAST_FM_RATE:
58 case MINI_CAM_DV_RATE:
59 case MUSIC_RATE:
60 case HIGHT_MUSIC_RATE:
61 case AUDIO_SAMPLE_RATE_12000:
62 case AUDIO_SAMPLE_RATE_16000:
63 case AUDIO_SAMPLE_RATE_24000:
64 case AUDIO_SAMPLE_RATE_64000:
65 case AUDIO_SAMPLE_RATE_96000:
66 return HDF_SUCCESS;
67 default:
68 return HDF_ERR_NOT_SUPPORT;
69 }
70 }
71
CheckAttrFormat(enum AudioFormat param)72 int32_t CheckAttrFormat(enum AudioFormat param)
73 {
74 switch (param) {
75 case AUDIO_FORMAT_PCM_8_BIT:
76 case AUDIO_FORMAT_PCM_16_BIT:
77 case AUDIO_FORMAT_PCM_24_BIT:
78 case AUDIO_FORMAT_PCM_32_BIT:
79 case AUDIO_FORMAT_AAC_MAIN:
80 case AUDIO_FORMAT_AAC_LC:
81 case AUDIO_FORMAT_AAC_LD:
82 case AUDIO_FORMAT_AAC_ELD:
83 case AUDIO_FORMAT_AAC_HE_V1:
84 case AUDIO_FORMAT_AAC_HE_V2:
85 break;
86 default:
87 return HDF_ERR_NOT_SUPPORT;
88 }
89 return HDF_SUCCESS;
90 }
91
AudioCheckParaAttr(const struct AudioSampleAttributes * attrs)92 int32_t AudioCheckParaAttr(const struct AudioSampleAttributes *attrs)
93 {
94 if (attrs == NULL) {
95 return HDF_FAILURE;
96 }
97 int32_t ret;
98 enum AudioCategory audioCategory = attrs->type;
99 if (AUDIO_IN_MEDIA != audioCategory && AUDIO_IN_COMMUNICATION != audioCategory) {
100 return HDF_ERR_NOT_SUPPORT;
101 }
102 enum AudioFormat audioFormat = attrs->format;
103 ret = CheckAttrFormat(audioFormat);
104 if (ret < 0) {
105 return ret;
106 }
107 uint32_t sampleRateTemp = attrs->sampleRate;
108 return CheckAttrSamplingRate(sampleRateTemp);
109 }
110
TimeToAudioTimeStamp(uint64_t bufferFrameSize,struct AudioTimeStamp * time,uint32_t sampleRate)111 int32_t TimeToAudioTimeStamp(uint64_t bufferFrameSize, struct AudioTimeStamp *time, uint32_t sampleRate)
112 {
113 if (time == NULL || sampleRate == 0) {
114 return HDF_FAILURE;
115 }
116 time->tvSec += (int64_t)(bufferFrameSize / sampleRate);
117 int64_t lastBufFrames = bufferFrameSize % ((int64_t)sampleRate);
118 time->tvNSec += (lastBufFrames * SEC_TO_NSEC) / ((int64_t)sampleRate);
119 if (time->tvNSec >= SEC_TO_NSEC) {
120 time->tvSec += 1;
121 time->tvNSec -= SEC_TO_NSEC;
122 }
123 return HDF_SUCCESS;
124 }
125
AudioLogRecord(int errorLevel,const char * format,...)126 void AudioLogRecord(int errorLevel, const char *format, ...)
127 {
128 va_list args;
129 FILE *fp = NULL;
130 char timeStr[TIME_LEN];
131 char fileName[FILE_NAME_LEN];
132 va_start(args, format);
133 time_t timeLog = time(NULL);
134 strftime(fileName, sizeof(fileName), "//data/%Y-%m-%d_audio_history.log", localtime(&timeLog));
135 if (fileName[0] == '\0') {
136 va_end(args);
137 return;
138 }
139 if ((fp = fopen(fileName, "a+")) != NULL) {
140 strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", localtime(&timeLog));
141 if (errorLevel == (int)INFO) {
142 fprintf(fp, "[%s]-[%s]", timeStr, "INFO");
143 vfprintf(fp, format, args);
144 fprintf(fp, "\n");
145 }
146 fclose(fp);
147 }
148 va_end(args);
149 return;
150 }
151