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 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 105 protected: 106 // start width and height 107 int32_t orignalResolutionWidth; 108 int32_t orignalResolutionHeight; 109 int32_t compressionResolutionWidth; 110 int32_t compressionResolutionHeight; 111 int32_t foldWidth = 0; 112 int32_t foldHeight = 0; 113 // width and height after resize 114 int32_t currentWidth = 0; 115 int32_t currentHeight = 0; 116 std::string foldStatus = "unfold"; 117 bool foldable = false; 118 static uint32_t validFrameCountPerMinute; 119 static uint32_t invalidFrameCountPerMinute; 120 static uint32_t sendFrameCountPerMinute; 121 122 LocalSocket* screenSocket; 123 std::unique_ptr<CppTimer> frameCountTimer; 124 125 const int32_t sendPeriod = 40; // A frame is sent per 40 ms. 126 const uint16_t pixelSize = 4; // 4 bytes per pixel 127 const size_t headSize = 40; // The packet header length is 40 bytes. 128 const size_t headReservedSize = 20; // The reserved length of the packet header is 20 bytes. 129 const uint32_t headStart = 0x12345678; // Buffer header starts with magic value 0x12345678 130 const int32_t frameCountPeriod = 60 * 1000; // Frame count per minute 131 uint16_t protocolVersion = static_cast<uint16_t>(VirtualScreen::ProtocolVersion::LOADNORMAL); 132 bool isWebSocketConfiged; 133 std::string currentRouter; 134 std::string abilityCurrentRouter; 135 std::string fastPreviewMsg; 136 uint8_t* jpgScreenBuffer; 137 unsigned long jpgBufferSize; 138 int jpgPix = 3; // jpg color components 139 int redPos = 0; 140 int greenPos = 1; 141 int bluePos = 2; 142 143 static std::chrono::system_clock::time_point startTime; 144 static std::chrono::system_clock::time_point staticCardStartTime; 145 VirtualScreen::LoadDocType startLoadDoc = VirtualScreen::LoadDocType::INIT; 146 std::chrono::system_clock::time_point startDropFrameTime; // record start drop frame time 147 int dropFrameFrequency = 0; // save drop frame frequency 148 }; 149 150 #endif // VIRTUALSCREEN_H 151