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 
25 #ifdef DCAMERA_MMAP_RESERVE
26 #include "image_converter.h"
27 #include "single_instance.h"
28 #endif
29 
30 namespace OHOS {
31 namespace DistributedHardware {
32 const std::string BASE_64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
33 
34 int32_t GetLocalDeviceNetworkId(std::string& networkId);
35 int64_t GetNowTimeStampMs();
36 int64_t GetNowTimeStampUs();
37 int32_t GetAlignedHeight(int32_t width);
38 std::string Base64Encode(const unsigned char *toEncode, unsigned int len);
39 std::string Base64Decode(const std::string& basicString);
40 void DumpBufferToFile(std::string fileName, uint8_t *buffer, size_t bufSize);
41 bool IsBase64(unsigned char c);
42 int32_t IsUnderDumpMaxSize(std::string fileName);
43 
44 #ifdef DCAMERA_MMAP_RESERVE
45 class ConverterHandle {
46     DECLARE_SINGLE_INSTANCE(ConverterHandle);
47 
48 public:
49     void InitConverter();
50     void DeInitConverter();
51     const OHOS::OpenSourceLibyuv::ImageConverter &GetHandle();
52 
53     using DlHandle = void *;
54 private:
55     std::atomic<bool> isInited_ = false;
56     DlHandle dlHandler_ = nullptr;
57     OHOS::OpenSourceLibyuv::ImageConverter converter_ = {0};
58 };
59 #endif
60 
61 const std::string DUMP_SERVER_PARA = "sys.dcamera.dump.write.enable";
62 const std::string DUMP_SERVICE_DIR = "/data/local/tmp/";
63 const std::string DUMP_DCAMERA_AFTER_ENC_FILENAME = "dump_after_enc_dcamsink.h265";
64 const std::string DUMP_DCAMERA_BEFORE_DEC_FILENAME = "dump_before_dec_dcamsource.h265";
65 const std::string DUMP_DCAMERA_AFTER_DEC_FILENAME = "dump_after_dec_dcamsource.yuv";
66 const std::string DUMP_DCAMERA_AFTER_SCALE_FILENAME = "dump_after_scale_dcamsource.yuv";
67 
68 class DumpFileUtil {
69 public:
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);
74 private:
75     static FILE *OpenDumpFileInner(std::string para, std::string fileName);
76     static void ChangeDumpFileState(std::string para, FILE **dumpFile, std::string fileName);
77 };
78 } // namespace DistributedHardware
79 } // namespace OHOS
80 #endif // OHOS_DCAMERA_UTILS_TOOL_H
81