• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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_common.h"
17 #include <dlfcn.h>
18 #include "hdf_types.h"
19 #include "osal_mem.h"
20 #include "osal_time.h"
21 #include "securec.h"
22 #include "audio_uhdf_log.h"
23 #include "audio_internal.h"
24 #include "v1_0/audio_types.h"
25 
26 #define FILE_NAME_LEN 256
27 #define TIME_LEN      32
28 
29 #define HDF_LOG_TAG HDF_AUDIO_PRIMARY_IMPL
30 
AudioDlClose(void ** ppHandlePassthrough)31 void AudioDlClose(void **ppHandlePassthrough)
32 {
33     if ((ppHandlePassthrough != NULL) && ((*ppHandlePassthrough) != NULL)) {
34         dlclose(*ppHandlePassthrough);
35         *ppHandlePassthrough = NULL;
36     }
37     return;
38 }
39 
AudioMemFree(void ** ppMem)40 void AudioMemFree(void **ppMem)
41 {
42     if ((ppMem != NULL) && ((*ppMem) != NULL)) {
43         OsalMemFree(*ppMem);
44         *ppMem = NULL;
45     }
46     return;
47 }
48 
AudioGetSysTime(char * s,int32_t len)49 int32_t AudioGetSysTime(char *s, int32_t len)
50 {
51     OsalTimespec time;
52     if (s == NULL) {
53         return HDF_FAILURE;
54     }
55     OsalGetTime(&time);
56     s[0] = 0;
57 
58     int32_t ret = snprintf_s(s, len, len - 1, "[%llu.%llu]", time.sec, time.usec);
59     if (ret < 0) {
60         AUDIO_FUNC_LOGE("snprintf_s failed!");
61         return HDF_FAILURE;
62     }
63     return ret;
64 }
65 
CheckAttrSamplingRate(uint32_t param)66 int32_t CheckAttrSamplingRate(uint32_t param)
67 {
68     switch (param) {
69         case TELHPONE_RATE:
70         case BROADCAST_AM_RATE:
71         case BROADCAST_FM_RATE:
72         case MINI_CAM_DV_RATE:
73         case MUSIC_RATE:
74         case HIGHT_MUSIC_RATE:
75         case AUDIO_SAMPLE_RATE_12000:
76         case AUDIO_SAMPLE_RATE_16000:
77         case AUDIO_SAMPLE_RATE_24000:
78         case AUDIO_SAMPLE_RATE_64000:
79         case AUDIO_SAMPLE_RATE_96000:
80             return HDF_SUCCESS;
81         default:
82             return HDF_ERR_NOT_SUPPORT;
83     }
84 }
85 
CheckAttrFormat(enum AudioFormat param)86 int32_t CheckAttrFormat(enum AudioFormat param)
87 {
88     switch (param) {
89         case AUDIO_FORMAT_TYPE_PCM_8_BIT:
90         case AUDIO_FORMAT_TYPE_PCM_16_BIT:
91         case AUDIO_FORMAT_TYPE_PCM_24_BIT:
92         case AUDIO_FORMAT_TYPE_PCM_32_BIT:
93         case AUDIO_FORMAT_TYPE_AAC_MAIN:
94         case AUDIO_FORMAT_TYPE_AAC_LC:
95         case AUDIO_FORMAT_TYPE_AAC_LD:
96         case AUDIO_FORMAT_TYPE_AAC_ELD:
97         case AUDIO_FORMAT_TYPE_AAC_HE_V1:
98         case AUDIO_FORMAT_TYPE_AAC_HE_V2:
99             break;
100         default:
101             return HDF_ERR_NOT_SUPPORT;
102     }
103     return HDF_SUCCESS;
104 }
105 
AudioCheckParaAttr(const struct AudioSampleAttributes * attrs)106 int32_t AudioCheckParaAttr(const struct AudioSampleAttributes *attrs)
107 {
108     if (attrs == NULL) {
109         AUDIO_FUNC_LOGE("param is null!");
110         return HDF_FAILURE;
111     }
112 
113     enum AudioCategory audioCategory = attrs->type;
114     if (audioCategory != AUDIO_IN_MEDIA && audioCategory != AUDIO_IN_COMMUNICATION) {
115         AUDIO_FUNC_LOGE("audioCategory error!");
116         return HDF_ERR_NOT_SUPPORT;
117     }
118 
119     enum AudioFormat audioFormat = attrs->format;
120     int32_t ret = CheckAttrFormat(audioFormat);
121     if (ret < 0) {
122         AUDIO_FUNC_LOGE("CheckAttrFormat error!");
123         return ret;
124     }
125 
126     uint32_t sampleRateTemp = attrs->sampleRate;
127     return CheckAttrSamplingRate(sampleRateTemp);
128 }
129 
TimeToAudioTimeStamp(uint64_t bufferFrameSize,struct AudioTimeStamp * time,uint32_t sampleRate)130 int32_t TimeToAudioTimeStamp(uint64_t bufferFrameSize, struct AudioTimeStamp *time, uint32_t sampleRate)
131 {
132     if (time == NULL || sampleRate == 0) {
133         AUDIO_FUNC_LOGE("param is null!");
134         return HDF_FAILURE;
135     }
136 
137     time->tvSec += (int64_t)(bufferFrameSize / sampleRate);
138 
139     int64_t lastBufFrames = bufferFrameSize % ((int64_t)sampleRate);
140 
141     time->tvNSec += (lastBufFrames * SEC_TO_NSEC) / ((int64_t)sampleRate);
142     if (time->tvNSec >= SEC_TO_NSEC) {
143         time->tvSec += 1;
144         time->tvNSec -= SEC_TO_NSEC;
145     }
146     return HDF_SUCCESS;
147 }
148 
AudioLogRecord(int32_t errorLevel,const char * format,...)149 void AudioLogRecord(int32_t errorLevel, const char *format, ...)
150 {
151     va_list args;
152     FILE *fp = NULL;
153     char timeStr[TIME_LEN];
154     char fileName[FILE_NAME_LEN];
155 
156     va_start(args, format);
157 
158     time_t timeLog = time(NULL);
159     if (timeLog < 0) {
160         va_end(args);
161         return;
162     }
163 
164     struct tm *tmInfo = localtime(&timeLog);
165     if (tmInfo == NULL) {
166         AUDIO_FUNC_LOGE("localtime failed!");
167         va_end(args);
168         return;
169     }
170 
171     (void)strftime(fileName, sizeof(fileName), "//data/%Y-%m-%d_audio_history.log", tmInfo);
172     if (fileName[0] == '\0') {
173         va_end(args);
174         return;
175     }
176 
177     if ((fp = fopen(fileName, "a+")) != NULL) {
178         (void)strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", tmInfo);
179         if (errorLevel == (int)AUDIO_INFO) {
180             fprintf(fp, "[%s]-[%s]", timeStr, "INFO");
181             vfprintf(fp, format, args);
182             fprintf(fp, "\n");
183         }
184         fclose(fp);
185     }
186     va_end(args);
187     return;
188 }
189