1 /*
2  * Copyright (c) 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 HDI_CAMERA_DUMP_H
17 #define HDI_CAMERA_DUMP_H
18 
19 #include <string>
20 #include <map>
21 #include <thread>
22 #include <mutex>
23 #include <condition_variable>
24 #include "camera_metadata_operator.h"
25 #include "camera_metadata_info.h"
26 #include "devhost_dump_reg.h"
27 #include "hdf_sbuf.h"
28 #include "ibuffer.h"
29 #include <iostream>
30 
31 namespace OHOS::Camera {
32 enum DumpType {
33     MedataType,
34     BufferType,
35     OpenType
36 };
37 
38 const std::string ENABLE_DQ_BUFFER_DUMP = "enableDQBufDump";
39 const std::string ENABLE_UVC_NODE = "enableUVCNodeBufferDump";
40 const std::string ENABLE_UVC_NODE_CONVERTED = "enableUVCNodeConvertedBufferDump";
41 const std::string ENABLE_EXIF_NODE_CONVERTED = "enableExifNodeConvertedBufferDump";
42 const std::string ENABLE_FACE_NODE_CONVERTED = "enableFaceNodeConvertedBufferDump";
43 const std::string ENABLE_FORK_NODE_CONVERTED = "enableForkNodeConvertedBufferDump";
44 const std::string ENABLE_RKFACE_NODE_CONVERTED = "enableRKFaceNodeConvertedBufferDump";
45 const std::string ENABLE_RKEXIF_NODE_CONVERTED = "enableRKExifNodeConvertedBufferDump";
46 const std::string ENABLE_CODEC_NODE_CONVERTED = "enableCodecNodeConvertedBufferDump";
47 const std::string ENABLE_RKCODEC_NODE_CONVERTED = "enableRKCodecNodeConvertedBufferDump";
48 const std::string ENABLE_STREAM_TUNNEL = "enableSreamTunnelBufferDump";
49 const std::string ENABLE_METADATA = "enableMetadataDump";
50 const std::string PREVIEW_INTERVAL = "previewInterval";
51 const std::string CAPTURE_INTERVAL = "captureInterval";
52 
53 class CameraDumper {
54 public:
55     ~CameraDumper ();
56     bool DumpBuffer(std::string name, std::string type, const std::shared_ptr<IBuffer>& buffer,
57         uint32_t width = 0, uint32_t height = 0);
58     bool DumpStart();
59     bool ReadDumpConfig();
60     bool IsDumpCommandOpened(std::string type);
61     bool DumpMetadata(std::string name, std::string type, const std::shared_ptr<CameraMetadata>& metadata);
62     void ShowDumpMenu(struct HdfSBuf *reply);
63     void CameraHostDumpProcess(struct HdfSBuf *data, struct HdfSBuf *reply);
64     static CameraDumper& GetInstance();
65 
66 private:
CameraDumper()67     CameraDumper() {}
68     bool IsDumpOpened(DumpType type);
69     uint64_t GetCurrentLocalTimeStamp();
70     void UpdateDumpMode(DumpType type, bool isDump, struct HdfSBuf *reply);
71     bool SaveDataToFile(const char *fileName, const void *data, uint32_t size);
72     void CheckDiskInfo();
73     void ThreadWorkFun();
74     void StartCheckDiskInfo();
75     void StopCheckDiskInfo();
76 
77 private:
78     std::mutex dumpStateLock_;
79     std::condition_variable cv_;
80     std::mutex terminateLock_;
81     bool terminate_ = true;
82     std::unique_ptr<std::thread> handleThread_ = nullptr;
83     int32_t dumpCount_ = 0;
84 };
85 
86 int32_t CameraDumpEvent(struct HdfSBuf *data, struct HdfSBuf *reply);
87 
88 } // OHOS::Camera
89 
90 #endif // HDI_CAMERA_DUMP_H
91