xref: /ide/tools/previewer/mock/VirtualScreen.h (revision 7c804472)
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 VIRTUALSCREEN_H
17#define VIRTUALSCREEN_H
18
19#include <atomic>
20#include <cstdint>
21#include <memory>
22#include <string>
23
24#include "CppTimer.h"
25#include "LocalSocket.h"
26#include "WebSocketServer.h"
27
28class VirtualScreen {
29public:
30    VirtualScreen();
31    virtual ~VirtualScreen();
32
33    int32_t GetOrignalWidth() const;
34    void SetOrignalWidth(const int32_t& value);
35
36    std::string GetCurrentRouter() const;
37    void SetCurrentRouter(const std::string currentRouterValue);
38
39    std::string GetAbilityCurrentRouter() const;
40    void SetAbilityCurrentRouter(const std::string currentRouterValue);
41
42    int32_t GetOrignalHeight() const;
43    void SetOrignalHeight(const int32_t& value);
44
45    int32_t GetCompressionWidth() const;
46    void SetCompressionWidth(const int32_t& value);
47
48    int32_t GetCompressionHeight() const;
49    void SetCompressionHeight(const int32_t& value);
50
51    void InitPipe(std::string pipeName, std::string pipePort);
52
53    void InitVirtualScreen();
54
55    void InitFrameCountTimer();
56
57    static void PrintFrameCount();
58
59    std::atomic<bool> isFrameUpdated;
60    static bool isWebSocketListening;
61    static std::string webSocketPort;
62
63    void WidthAndHeightReverse();
64
65    void SetVirtualScreenWidthAndHeight(const int32_t& orignalWidth,
66                                        const int32_t& orignalHeight,
67                                        const int32_t& compressionWidth,
68                                        const int32_t& compressionHeight);
69
70    int GetJpgQualityValue(int32_t width, int32_t height) const;
71
72    enum class LoadDocType { INIT = 3, START = 1, FINISHED = 2, NORMAL = 0 };
73    void SetLoadDocFlag(VirtualScreen::LoadDocType flag);
74    VirtualScreen::LoadDocType GetLoadDocFlag() const;
75
76    enum class ProtocolVersion { LOADNORMAL = 2, LOADDOC = 3 };
77
78    enum class JpgPixCountLevel { LOWCOUNT = 100000, MIDDLECOUNT = 300000, HIGHCOUNT = 500000};
79    enum class JpgQualityLevel { HIGHLEVEL = 100, MIDDLELEVEL = 90, LOWLEVEL = 85, DEFAULTLEVEL = 75};
80
81    static bool isOutOfSeconds;
82    static bool isStartCount;
83
84    std::string GetFastPreviewMsg() const;
85    void SetFastPreviewMsg(const std::string msg);
86    bool JudgeAndDropFrame();
87    void SetDropFrameFrequency(const int32_t& value);
88    static bool JudgeStaticImage(const int duration);
89    static bool StopSendStaticCardImage(const int duration);
90    void RgbToJpg(unsigned char* data, const int32_t width, const int32_t height);
91    static uint32_t inputKeyCountPerMinute;
92    static uint32_t inputMethodCountPerMinute;
93
94    void SetFoldable(const bool value);
95    bool GetFoldable() const;
96    void SetFoldStatus(const std::string& value);
97    std::string GetFoldStatus() const;
98    void SetFoldResolution(int32_t changedFoldWidth, int32_t changedFoldHeight);
99    int32_t GetFoldWidth() const;
100    int32_t GetFoldHeight() const;
101    void SetCurrentResolution(int32_t width, int32_t height);
102    int32_t GetCurrentWidth() const;
103    int32_t GetCurrentHeight() const;
104    virtual void InitFlushEmptyTime();
105    void InitResolution();
106
107protected:
108    // start width and height
109    int32_t orignalResolutionWidth;
110    int32_t orignalResolutionHeight;
111    int32_t compressionResolutionWidth;
112    int32_t compressionResolutionHeight;
113    int32_t foldWidth = 0;
114    int32_t foldHeight = 0;
115    // width and height after resize
116    int32_t currentWidth = 0;
117    int32_t currentHeight = 0;
118    std::string foldStatus = "unfold";
119    bool foldable = false;
120    static uint32_t validFrameCountPerMinute;
121    static uint32_t invalidFrameCountPerMinute;
122    static uint32_t sendFrameCountPerMinute;
123
124    LocalSocket* screenSocket;
125    std::unique_ptr<CppTimer> frameCountTimer;
126
127    const int32_t sendPeriod = 40;              // A frame is sent per 40 ms.
128    const uint16_t pixelSize = 4;               // 4 bytes per pixel
129    const size_t headSize = 40;                 // The packet header length is 40 bytes.
130    const size_t headReservedSize = 20;         // The reserved length of the packet header is 20 bytes.
131    const uint32_t headStart = 0x12345678;      // Buffer header starts with magic value 0x12345678
132    const int32_t frameCountPeriod = 60 * 1000; // Frame count per minute
133    uint16_t protocolVersion = static_cast<uint16_t>(VirtualScreen::ProtocolVersion::LOADNORMAL);
134    bool isWebSocketConfiged;
135    std::string currentRouter;
136    std::string abilityCurrentRouter;
137    std::string fastPreviewMsg;
138    uint8_t* jpgScreenBuffer;
139    unsigned long jpgBufferSize;
140    int jpgPix = 3; // jpg color components
141    int redPos = 0;
142    int greenPos = 1;
143    int bluePos = 2;
144
145    static std::chrono::system_clock::time_point startTime;
146    static std::chrono::system_clock::time_point staticCardStartTime;
147    VirtualScreen::LoadDocType startLoadDoc = VirtualScreen::LoadDocType::INIT;
148    std::chrono::system_clock::time_point startDropFrameTime;   // record start drop frame time
149    int dropFrameFrequency = 0; // save drop frame frequency
150};
151
152#endif // VIRTUALSCREEN_H
153