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_UTIL_H 17 #define OHOS_DAUDIO_UTIL_H 18 19 #include <chrono> 20 #include <string> 21 #include "nlohmann/json.hpp" 22 23 using json = nlohmann::json; 24 25 #define AUDIO_MS_PER_SECOND 1000 26 #define AUDIO_US_PER_SECOND 1000000 27 #define AUDIO_NS_PER_SECOND ((int64_t)1000000000) 28 namespace OHOS { 29 namespace DistributedHardware { 30 int32_t GetLocalDeviceNetworkId(std::string &networkId); 31 std::string GetRandomID(); 32 std::string GetAnonyString(const std::string &value); 33 int32_t GetDevTypeByDHId(int32_t dhId); 34 int64_t GetNowTimeUs(); 35 int32_t GetAudioParamStr(const std::string ¶ms, const std::string &key, std::string &value); 36 int32_t GetAudioParamBool(const std::string ¶ms, const std::string &key, bool &value); 37 int32_t GetAudioParamInt(const std::string ¶ms, const std::string &key, int32_t &value); 38 bool JsonParamCheck(const json &jsonObj, const std::initializer_list<std::string> &key); 39 bool IsString(const json &jsonObj, const std::string &key); 40 bool IsInt32(const json &jsonObj, const std::string &key); 41 bool IsAudioParam(const json &jsonObj, const std::string &key); 42 int32_t CalculateSampleNum(uint32_t sampleRate, uint32_t timems); 43 int64_t GetCurNano(); 44 int32_t AbsoluteSleep(int64_t nanoTime); 45 int64_t CalculateOffset(const int64_t frameIndex, const int64_t framePeriodNs, const int64_t startTime); 46 int64_t UpdateTimeOffset(const int64_t frameIndex, const int64_t framePeriodNs, int64_t &startTime); 47 void GetCurrentTime(int64_t &tvSec, int64_t &tvNSec); 48 bool CheckIsNum(const std::string &jsonString); 49 bool CheckDevIdIsLegal(const std::string &devId); 50 51 template <typename T> 52 bool GetSysPara(const char *key, T &value); 53 bool IsParamEnabled(const std::string &key, bool &isEnabled); 54 } // namespace DistributedHardware 55 } // namespace OHOS 56 #endif // OHOS_DAUDIO_UTIL_H 57