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 #ifndef OHOS_DAUDIO_UTILS_H
17 #define OHOS_DAUDIO_UTILS_H
18 
19 #include <fstream>
20 #include <string>
21 
22 #include "cJSON.h"
23 
24 #define AUDIO_MS_PER_SECOND 1000
25 #define AUDIO_US_PER_SECOND 1000000
26 #define AUDIO_NS_PER_SECOND ((int64_t)1000000000)
27 #define AUDIO_MMAP_NOIRQ_INTERVAL 5
28 #define AUDIO_MMAP_VOIP_INTERVAL  20
29 #define AUDIO_NORMAL_INTERVAL     20
30 namespace OHOS {
31 namespace DistributedHardware {
32 std::string GetAnonyString(const std::string &value);
33 
34 std::string GetChangeDevIdMap(int32_t devId);
35 
36 int32_t GetAudioParamStr(const std::string &params, const std::string &key, std::string &value);
37 
38 int32_t GetAudioParamInt(const std::string &params, const std::string &key, int32_t &value);
39 
40 int32_t GetAudioParamUInt(const std::string &params, const std::string &key, uint32_t &value);
41 
42 int32_t GetAudioParamBool(const std::string &params, const std::string &key, bool &value);
43 
44 int32_t SetAudioParamStr(std::string &params, const std::string &key, const std::string &value);
45 
46 int32_t GetDevTypeByDHId(int32_t dhId);
47 
48 int64_t GetNowTimeUs();
49 
50 uint32_t CalculateFrameSize(uint32_t sampleRate, uint32_t channelCount,
51     int32_t format, uint32_t timeInterval, bool isMMAP);
52 
53 int32_t CalculateSampleNum(uint32_t sampleRate, uint32_t timems);
54 
55 int64_t GetCurNano();
56 
57 int32_t AbsoluteSleep(int64_t nanoTime);
58 
59 int64_t CalculateOffset(const int64_t frameindex, const int64_t framePeriodNs, const int64_t startTime);
60 
61 int64_t UpdateTimeOffset(const int64_t frameIndex, const int64_t framePeriodNs, int64_t &startTime);
62 
63 bool IsOutDurationRange(int64_t startTime, int64_t endTime, int64_t lastStartTime);
64 
65 void SaveFile(std::string fileName, uint8_t *audioData, int32_t size);
66 
67 int32_t WrapCJsonItem(const std::initializer_list<std::pair<std::string, std::string>> &keys, std::string &content);
68 
69 bool CJsonParamCheck(const cJSON *jsonObj, const std::initializer_list<std::string> &keys);
70 
71 std::string ParseStringFromArgs(const std::string &args, const char *key);
72 
73 bool CheckIsNum(const std::string &jsonString);
74 } // DistributedHardware
75 } // OHOS
76 #endif