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 "daudio_utils.h"
17
18 #include <ctime>
19
20 #include "daudio_constants.h"
21 #include "daudio_errcode.h"
22
23 namespace OHOS {
24 namespace DistributedHardware {
25 constexpr size_t INT32_SHORT_ID_LENGTH = 20;
26 constexpr size_t INT32_PLAINTEXT_LENGTH = 4;
27 constexpr size_t INT32_MIN_ID_LENGTH = 3;
28
GetAnonyString(const std::string & value)29 std::string GetAnonyString(const std::string &value)
30 {
31 std::string res;
32 std::string tmpStr("******");
33 size_t strLen = value.length();
34 if (strLen < INT32_MIN_ID_LENGTH) {
35 return tmpStr;
36 }
37
38 if (strLen <= INT32_SHORT_ID_LENGTH) {
39 res += value[0];
40 res += tmpStr;
41 res += value[strLen - 1];
42 } else {
43 res.append(value, 0, INT32_PLAINTEXT_LENGTH);
44 res += tmpStr;
45 res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
46 }
47
48 return res;
49 }
50
GetAudioParamStr(const std::string & params,const std::string & key,std::string & value)51 int32_t GetAudioParamStr(const std::string ¶ms, const std::string &key, std::string &value)
52 {
53 size_t step = key.size();
54 if (step >= params.size()) {
55 return ERR_DH_AUDIO_HDF_FAIL;
56 }
57 size_t pos = params.find(key);
58 if (pos == params.npos || params.at(pos + step) != '=') {
59 return ERR_DH_AUDIO_COMMON_NOT_FOUND_KEY;
60 }
61 size_t splitPosEnd = params.find(';', pos);
62 if (splitPosEnd != params.npos) {
63 value = params.substr(pos + step + 1, splitPosEnd - pos - step - 1);
64 } else {
65 value = params.substr(pos + step + 1);
66 }
67 return DH_SUCCESS;
68 }
69
GetAudioParamInt(const std::string & params,const std::string & key,int32_t & value)70 int32_t GetAudioParamInt(const std::string ¶ms, const std::string &key, int32_t &value)
71 {
72 std::string val = "0";
73 int32_t ret = GetAudioParamStr(params, key, val);
74 value = std::stoi(val);
75 return ret;
76 }
77
GetAudioParamUInt(const std::string & params,const std::string & key,uint32_t & value)78 int32_t GetAudioParamUInt(const std::string ¶ms, const std::string &key, uint32_t &value)
79 {
80 value = 0;
81 return DH_SUCCESS;
82 }
83
GetAudioParamBool(const std::string & params,const std::string & key,bool & value)84 int32_t GetAudioParamBool(const std::string ¶ms, const std::string &key, bool &value)
85 {
86 std::string val;
87 GetAudioParamStr(params, key, val);
88 value = (val != "0");
89 return DH_SUCCESS;
90 }
91
SetAudioParamStr(std::string & params,const std::string & key,const std::string & value)92 int32_t SetAudioParamStr(std::string ¶ms, const std::string &key, const std::string &value)
93 {
94 params = params + key + '=' + value + ';';
95 return DH_SUCCESS;
96 }
97
GetDevTypeByDHId(int32_t dhId)98 int32_t GetDevTypeByDHId(int32_t dhId)
99 {
100 if ((uint32_t)dhId & 0x8000000) {
101 return AUDIO_DEVICE_TYPE_MIC;
102 } else if ((uint32_t)dhId & 0x7ffffff) {
103 return AUDIO_DEVICE_TYPE_SPEAKER;
104 }
105 return AUDIO_DEVICE_TYPE_UNKNOWN;
106 }
107
CalculateFrameSize(uint32_t sampleRate,uint32_t channelCount,int32_t format,uint32_t timeInterval,bool isMMAP)108 uint32_t CalculateFrameSize(uint32_t sampleRate, uint32_t channelCount,
109 int32_t format, uint32_t timeInterval, bool isMMAP)
110 {
111 return isMMAP ? (sampleRate * channelCount * static_cast<uint32_t>(format) * timeInterval) /
112 AUDIO_MS_PER_SECOND : DEFAULT_AUDIO_DATA_SIZE;
113 }
114
CalculateSampleNum(uint32_t sampleRate,uint32_t timems)115 int32_t CalculateSampleNum(uint32_t sampleRate, uint32_t timems)
116 {
117 return (sampleRate * timems) / AUDIO_MS_PER_SECOND;
118 }
119
GetCurNano()120 int64_t GetCurNano()
121 {
122 int64_t result = -1;
123 struct timespec time;
124 clockid_t clockId = CLOCK_MONOTONIC;
125 int ret = clock_gettime(clockId, &time);
126 if (ret < 0) {
127 return result;
128 }
129 result = (time.tv_sec * AUDIO_NS_PER_SECOND) + time.tv_nsec;
130 return result;
131 }
132
AbsoluteSleep(int64_t nanoTime)133 int32_t AbsoluteSleep(int64_t nanoTime)
134 {
135 int32_t ret = -1;
136 if (nanoTime <= 0) {
137 return ret;
138 }
139 struct timespec time;
140 time.tv_sec = nanoTime / AUDIO_NS_PER_SECOND;
141 time.tv_nsec = nanoTime - (time.tv_sec * AUDIO_NS_PER_SECOND);
142
143 clockid_t clockId = CLOCK_MONOTONIC;
144 ret = clock_nanosleep(clockId, TIMER_ABSTIME, &time, nullptr);
145 return ret;
146 }
147
CalculateOffset(const int64_t frameIndex,const int64_t framePeriodNs,const int64_t startTime)148 int64_t CalculateOffset(const int64_t frameIndex, const int64_t framePeriodNs, const int64_t startTime)
149 {
150 int64_t totalOffset = GetCurNano() - startTime;
151 return totalOffset - frameIndex * framePeriodNs;
152 }
153
UpdateTimeOffset(const int64_t frameIndex,const int64_t framePeriodNs,int64_t & startTime)154 int64_t UpdateTimeOffset(const int64_t frameIndex, const int64_t framePeriodNs, int64_t &startTime)
155 {
156 int64_t timeOffset = 0;
157 if (frameIndex == 0) {
158 startTime = GetCurNano();
159 } else if (frameIndex % AUDIO_OFFSET_FRAME_NUM == 0) {
160 timeOffset = CalculateOffset(frameIndex, framePeriodNs, startTime);
161 }
162 return timeOffset;
163 }
164 } // namespace DistributedHardware
165 } // namespace OHOS