• 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      int32_t GetOrignalHeight() const;
40      void SetOrignalHeight(const int32_t& value);
41  
42      int32_t GetCompressionWidth() const;
43      void SetCompressionWidth(const int32_t& value);
44  
45      int32_t GetCompressionHeight() const;
46      void SetCompressionHeight(const int32_t& value);
47  
48      void InitPipe(std::string pipeName, std::string pipePort);
49  
50      void InitVirtualScreen();
51  
52      void InitFrameCountTimer();
53  
54      static void PrintFrameCount();
55  
56      std::atomic<bool> isFrameUpdated;
57      static bool isWebSocketListening;
58      static std::string webSocketPort;
59  
60      void WidthAndHeightReverse();
61  
62      void SetVirtualScreenWidthAndHeight(const int32_t& orignalWidth,
63                                          const int32_t& orignalHeight,
64                                          const int32_t& compressionWidth,
65                                          const int32_t& compressionHeight);
66  
67      int GetJpgQualityValue(int32_t width, int32_t height) const;
68  
69      enum class LoadDocType { INIT = 3, START = 1, FINISHED = 2, NORMAL = 0 };
70      void SetLoadDocFlag(VirtualScreen::LoadDocType flag);
71      VirtualScreen::LoadDocType GetLoadDocFlag() const;
72  
73      enum class ProtocolVersion { LOADNORMAL = 2, LOADDOC = 3 };
74  
75      enum class JpgPixCountLevel { LOWCOUNT = 100000, MIDDLECOUNT = 300000, HIGHCOUNT = 500000};
76      enum class JpgQualityLevel { HIGHLEVEL = 100, MIDDLELEVEL = 90, LOWLEVEL = 85, DEFAULTLEVEL = 75};
77  
78      static bool isOutOfSeconds;
79      static bool isStartCount;
80  
81      std::string GetFastPreviewMsg() const;
82      void SetFastPreviewMsg(const std::string msg);
83      bool JudgeAndDropFrame();
84      void SetDropFrameFrequency(const int32_t& value);
85      static bool JudgeStaticImage(const int duration);
86      void RgbToJpg(unsigned char* data, const int32_t width, const int32_t height);
87      static uint32_t inputKeyCountPerMinute;
88      static uint32_t inputMethodCountPerMinute;
89  
90  protected:
91      int32_t orignalResolutionWidth;
92      int32_t orignalResolutionHeight;
93      int32_t compressionResolutionWidth;
94      int32_t compressionResolutionHeight;
95      static uint32_t validFrameCountPerMinute;
96      static uint32_t invalidFrameCountPerMinute;
97      static uint32_t sendFrameCountPerMinute;
98  
99      LocalSocket* screenSocket;
100      std::unique_ptr<CppTimer> frameCountTimer;
101  
102      const int32_t sendPeriod = 40;              // A frame is sent per 40 ms.
103      const uint16_t pixelSize = 4;               // 4 bytes per pixel
104      const size_t headSize = 40;                 // The packet header length is 40 bytes.
105      const size_t headReservedSize = 20;         // The reserved length of the packet header is 20 bytes.
106      const uint32_t headStart = 0x12345678;      // Buffer header starts with magic value 0x12345678
107      const int32_t frameCountPeriod = 60 * 1000; // Frame count per minute
108      uint16_t protocolVersion = static_cast<uint16_t>(VirtualScreen::ProtocolVersion::LOADNORMAL);
109      bool isWebSocketConfiged;
110      std::string currentRouter;
111      std::string fastPreviewMsg;
112      uint8_t* jpgScreenBuffer;
113      unsigned long jpgBufferSize;
114      int jpgPix = 3; // jpg color components
115      int redPos = 0;
116      int greenPos = 1;
117      int bluePos = 2;
118  
119      static std::chrono::system_clock::time_point startTime;
120      VirtualScreen::LoadDocType startLoadDoc = VirtualScreen::LoadDocType::INIT;
121      std::chrono::system_clock::time_point startDropFrameTime;   // record start drop frame time
122      int dropFrameFrequency = 0; // save drop frame frequency
123  };
124  
125  #endif // VIRTUALSCREEN_H
126