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