• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 class VirtualScreen {
29 public:
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 protected:
95     int32_t orignalResolutionWidth;
96     int32_t orignalResolutionHeight;
97     int32_t compressionResolutionWidth;
98     int32_t compressionResolutionHeight;
99     static uint32_t validFrameCountPerMinute;
100     static uint32_t invalidFrameCountPerMinute;
101     static uint32_t sendFrameCountPerMinute;
102 
103     LocalSocket* screenSocket;
104     std::unique_ptr<CppTimer> frameCountTimer;
105 
106     const int32_t sendPeriod = 40;              // A frame is sent per 40 ms.
107     const uint16_t pixelSize = 4;               // 4 bytes per pixel
108     const size_t headSize = 40;                 // The packet header length is 40 bytes.
109     const size_t headReservedSize = 20;         // The reserved length of the packet header is 20 bytes.
110     const uint32_t headStart = 0x12345678;      // Buffer header starts with magic value 0x12345678
111     const int32_t frameCountPeriod = 60 * 1000; // Frame count per minute
112     uint16_t protocolVersion = static_cast<uint16_t>(VirtualScreen::ProtocolVersion::LOADNORMAL);
113     bool isWebSocketConfiged;
114     std::string currentRouter;
115     std::string abilityCurrentRouter;
116     std::string fastPreviewMsg;
117     uint8_t* jpgScreenBuffer;
118     unsigned long jpgBufferSize;
119     int jpgPix = 3; // jpg color components
120     int redPos = 0;
121     int greenPos = 1;
122     int bluePos = 2;
123 
124     static std::chrono::system_clock::time_point startTime;
125     static std::chrono::system_clock::time_point staticCardStartTime;
126     VirtualScreen::LoadDocType startLoadDoc = VirtualScreen::LoadDocType::INIT;
127     std::chrono::system_clock::time_point startDropFrameTime;   // record start drop frame time
128     int dropFrameFrequency = 0; // save drop frame frequency
129 };
130 
131 #endif // VIRTUALSCREEN_H
132