1/* 2 * Copyright (c) 2021-2024 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_DCAMERA_UTILS_TOOL_H 17#define OHOS_DCAMERA_UTILS_TOOL_H 18 19#include <atomic> 20#include <cstdint> 21#include <string> 22#include <fstream> 23#include <map> 24#include "single_instance.h" 25 26#ifdef DCAMERA_MMAP_RESERVE 27#include "image_converter.h" 28#endif 29 30namespace OHOS { 31namespace DistributedHardware { 32const std::string BASE_64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 33 34int32_t GetLocalDeviceNetworkId(std::string& networkId); 35int64_t GetNowTimeStampMs(); 36int64_t GetNowTimeStampUs(); 37int32_t GetAlignedHeight(int32_t width); 38std::string Base64Encode(const unsigned char *toEncode, unsigned int len); 39std::string Base64Decode(const std::string& basicString); 40void DumpBufferToFile(std::string fileName, uint8_t *buffer, size_t bufSize); 41bool IsBase64(unsigned char c); 42int32_t IsUnderDumpMaxSize(std::string fileName); 43 44#ifdef DCAMERA_MMAP_RESERVE 45class ConverterHandle { 46 DECLARE_SINGLE_INSTANCE(ConverterHandle); 47 48public: 49 void InitConverter(); 50 void DeInitConverter(); 51 const OHOS::OpenSourceLibyuv::ImageConverter &GetHandle(); 52 53 using DlHandle = void *; 54private: 55 std::atomic<bool> isInited_ = false; 56 DlHandle dlHandler_ = nullptr; 57 OHOS::OpenSourceLibyuv::ImageConverter converter_ = {0}; 58}; 59#endif 60 61const std::string DUMP_SERVER_PARA = "sys.dcamera.dump.write.enable"; 62const std::string DUMP_SERVICE_DIR = "/data/local/tmp/"; 63const std::string DUMP_DCAMERA_AFTER_ENC_FILENAME = "dump_after_enc_dcamsink.h265"; 64const std::string DUMP_DCAMERA_BEFORE_DEC_FILENAME = "dump_before_dec_dcamsource.h265"; 65const std::string DUMP_DCAMERA_AFTER_DEC_FILENAME = "dump_after_dec_dcamsource.yuv"; 66const std::string DUMP_DCAMERA_AFTER_SCALE_FILENAME = "dump_after_scale_dcamsource.yuv"; 67 68class DumpFileUtil { 69public: 70 static void WriteDumpFile(FILE *dumpFile, void *buffer, size_t bufferSize); 71 static void CloseDumpFile(FILE **dumpFile); 72 static std::map<std::string, std::string> g_lastPara; 73 static void OpenDumpFile(std::string para, std::string fileName, FILE **file); 74private: 75 static FILE *OpenDumpFileInner(std::string para, std::string fileName); 76 static void ChangeDumpFileState(std::string para, FILE **dumpFile, std::string fileName); 77}; 78 79class ManageSelectChannel { 80DECLARE_SINGLE_INSTANCE(ManageSelectChannel); 81 82public: 83 void SetSrcConnect(bool isSoftbusConnect); 84 void SetSinkConnect(bool isSoftbusConnect); 85 bool GetSrcConnect(); 86 bool GetSinkConnect(); 87private: 88 bool isSoftbusConnectSource_ = false; 89 bool isSoftbusConnectSink_ = false; 90}; 91} // namespace DistributedHardware 92} // namespace OHOS 93#endif // OHOS_DCAMERA_UTILS_TOOL_H 94