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_COMMON_H
17#define AUDIO_COMMON_H
18#include <securec.h>
19#include "audio_internal.h"
20#include "osal_time.h"
21namespace OHOS::HDI::Audio_Bluetooth {
22void AudioMemFree(void **ppMem)
23{
24    if ((ppMem != NULL) && ((*ppMem) != NULL)) {
25        free(*ppMem);
26        *ppMem = NULL;
27    }
28    return;
29}
30
31int32_t AudioGetSysTime(char *s, int32_t len)
32{
33    OsalTimespec time;
34    if (s == NULL) {
35        return -1;
36    }
37    OsalGetTime(&time);
38    s[0] = 0;
39    int32_t ret = snprintf_s(s, len, len - 1, "[%llu.%llu]", time.sec, time.usec);
40    return ret;
41}
42
43int32_t CheckAttrSamplingRate(uint32_t param)
44{
45    switch (param) {
46        case TELHPONE_RATE:
47        case BROADCAST_AM_RATE:
48        case BROADCAST_FM_RATE:
49        case MINI_CAM_DV_RATE:
50        case MUSIC_RATE:
51        case HIGHT_MUSIC_RATE:
52        case AUDIO_SAMPLE_RATE_12000:
53        case AUDIO_SAMPLE_RATE_16000:
54        case AUDIO_SAMPLE_RATE_24000:
55        case AUDIO_SAMPLE_RATE_64000:
56        case AUDIO_SAMPLE_RATE_96000:
57            return HDF_SUCCESS;
58        default:
59            return HDF_ERR_NOT_SUPPORT;
60    }
61}
62
63int32_t CheckAttrFormat(AudioFormat param)
64{
65    switch (param) {
66        case AUDIO_FORMAT_TYPE_PCM_8_BIT:
67        case AUDIO_FORMAT_TYPE_PCM_16_BIT:
68        case AUDIO_FORMAT_TYPE_PCM_24_BIT:
69        case AUDIO_FORMAT_TYPE_PCM_32_BIT:
70        case AUDIO_FORMAT_TYPE_AAC_MAIN:
71        case AUDIO_FORMAT_TYPE_AAC_LC:
72        case AUDIO_FORMAT_TYPE_AAC_LD:
73        case AUDIO_FORMAT_TYPE_AAC_ELD:
74        case AUDIO_FORMAT_TYPE_AAC_HE_V1:
75        case AUDIO_FORMAT_TYPE_AAC_HE_V2:
76            break;
77        default:
78            return HDF_ERR_NOT_SUPPORT;
79    }
80    return HDF_SUCCESS;
81}
82
83int32_t AudioCheckParaAttr(const struct AudioSampleAttributes *attrs)
84{
85    if (attrs == NULL) {
86        return HDF_FAILURE;
87    }
88    int32_t ret;
89    AudioCategory audioCategory = attrs->type;
90    if (AUDIO_IN_MEDIA != audioCategory && AUDIO_IN_COMMUNICATION != audioCategory) {
91        return HDF_ERR_NOT_SUPPORT;
92    }
93    AudioFormat audioFormat = attrs->format;
94    ret = CheckAttrFormat(audioFormat);
95    if (ret < 0) {
96        return ret;
97    }
98    uint32_t sampleRateTemp = attrs->sampleRate;
99    return CheckAttrSamplingRate(sampleRateTemp);
100}
101
102int32_t TimeToAudioTimeStamp(uint64_t bufferFrameSize, struct AudioTimeStamp *time, uint32_t sampleRate)
103{
104    if (time == NULL) {
105        return HDF_FAILURE;
106    }
107    time->tvSec += (int64_t)bufferFrameSize / (int64_t)sampleRate;
108    int64_t lastBufFrames = bufferFrameSize % ((int64_t)sampleRate);
109    time->tvNSec += (lastBufFrames * SEC_TO_NSEC) / ((int64_t)sampleRate);
110    if (time->tvNSec >= SEC_TO_NSEC) {
111        time->tvSec += 1;
112        time->tvNSec -= SEC_TO_NSEC;
113    }
114    return HDF_SUCCESS;
115}
116}
117#endif