17c804472Sopenharmony_ci/*
27c804472Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
37c804472Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47c804472Sopenharmony_ci * you may not use this file except in compliance with the License.
57c804472Sopenharmony_ci * You may obtain a copy of the License at
67c804472Sopenharmony_ci *
77c804472Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
87c804472Sopenharmony_ci *
97c804472Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107c804472Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117c804472Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127c804472Sopenharmony_ci * See the License for the specific language governing permissions and
137c804472Sopenharmony_ci * limitations under the License.
147c804472Sopenharmony_ci */
157c804472Sopenharmony_ci
167c804472Sopenharmony_ci#ifndef VIRTUALSCREEN_H
177c804472Sopenharmony_ci#define VIRTUALSCREEN_H
187c804472Sopenharmony_ci
197c804472Sopenharmony_ci#include <atomic>
207c804472Sopenharmony_ci#include <cstdint>
217c804472Sopenharmony_ci#include <memory>
227c804472Sopenharmony_ci#include <string>
237c804472Sopenharmony_ci
247c804472Sopenharmony_ci#include "CppTimer.h"
257c804472Sopenharmony_ci#include "LocalSocket.h"
267c804472Sopenharmony_ci#include "WebSocketServer.h"
277c804472Sopenharmony_ci
287c804472Sopenharmony_ciclass VirtualScreen {
297c804472Sopenharmony_cipublic:
307c804472Sopenharmony_ci    VirtualScreen();
317c804472Sopenharmony_ci    virtual ~VirtualScreen();
327c804472Sopenharmony_ci
337c804472Sopenharmony_ci    int32_t GetOrignalWidth() const;
347c804472Sopenharmony_ci    void SetOrignalWidth(const int32_t& value);
357c804472Sopenharmony_ci
367c804472Sopenharmony_ci    std::string GetCurrentRouter() const;
377c804472Sopenharmony_ci    void SetCurrentRouter(const std::string currentRouterValue);
387c804472Sopenharmony_ci
397c804472Sopenharmony_ci    std::string GetAbilityCurrentRouter() const;
407c804472Sopenharmony_ci    void SetAbilityCurrentRouter(const std::string currentRouterValue);
417c804472Sopenharmony_ci
427c804472Sopenharmony_ci    int32_t GetOrignalHeight() const;
437c804472Sopenharmony_ci    void SetOrignalHeight(const int32_t& value);
447c804472Sopenharmony_ci
457c804472Sopenharmony_ci    int32_t GetCompressionWidth() const;
467c804472Sopenharmony_ci    void SetCompressionWidth(const int32_t& value);
477c804472Sopenharmony_ci
487c804472Sopenharmony_ci    int32_t GetCompressionHeight() const;
497c804472Sopenharmony_ci    void SetCompressionHeight(const int32_t& value);
507c804472Sopenharmony_ci
517c804472Sopenharmony_ci    void InitPipe(std::string pipeName, std::string pipePort);
527c804472Sopenharmony_ci
537c804472Sopenharmony_ci    void InitVirtualScreen();
547c804472Sopenharmony_ci
557c804472Sopenharmony_ci    void InitFrameCountTimer();
567c804472Sopenharmony_ci
577c804472Sopenharmony_ci    static void PrintFrameCount();
587c804472Sopenharmony_ci
597c804472Sopenharmony_ci    std::atomic<bool> isFrameUpdated;
607c804472Sopenharmony_ci    static bool isWebSocketListening;
617c804472Sopenharmony_ci    static std::string webSocketPort;
627c804472Sopenharmony_ci
637c804472Sopenharmony_ci    void WidthAndHeightReverse();
647c804472Sopenharmony_ci
657c804472Sopenharmony_ci    void SetVirtualScreenWidthAndHeight(const int32_t& orignalWidth,
667c804472Sopenharmony_ci                                        const int32_t& orignalHeight,
677c804472Sopenharmony_ci                                        const int32_t& compressionWidth,
687c804472Sopenharmony_ci                                        const int32_t& compressionHeight);
697c804472Sopenharmony_ci
707c804472Sopenharmony_ci    int GetJpgQualityValue(int32_t width, int32_t height) const;
717c804472Sopenharmony_ci
727c804472Sopenharmony_ci    enum class LoadDocType { INIT = 3, START = 1, FINISHED = 2, NORMAL = 0 };
737c804472Sopenharmony_ci    void SetLoadDocFlag(VirtualScreen::LoadDocType flag);
747c804472Sopenharmony_ci    VirtualScreen::LoadDocType GetLoadDocFlag() const;
757c804472Sopenharmony_ci
767c804472Sopenharmony_ci    enum class ProtocolVersion { LOADNORMAL = 2, LOADDOC = 3 };
777c804472Sopenharmony_ci
787c804472Sopenharmony_ci    enum class JpgPixCountLevel { LOWCOUNT = 100000, MIDDLECOUNT = 300000, HIGHCOUNT = 500000};
797c804472Sopenharmony_ci    enum class JpgQualityLevel { HIGHLEVEL = 100, MIDDLELEVEL = 90, LOWLEVEL = 85, DEFAULTLEVEL = 75};
807c804472Sopenharmony_ci
817c804472Sopenharmony_ci    static bool isOutOfSeconds;
827c804472Sopenharmony_ci    static bool isStartCount;
837c804472Sopenharmony_ci
847c804472Sopenharmony_ci    std::string GetFastPreviewMsg() const;
857c804472Sopenharmony_ci    void SetFastPreviewMsg(const std::string msg);
867c804472Sopenharmony_ci    bool JudgeAndDropFrame();
877c804472Sopenharmony_ci    void SetDropFrameFrequency(const int32_t& value);
887c804472Sopenharmony_ci    static bool JudgeStaticImage(const int duration);
897c804472Sopenharmony_ci    static bool StopSendStaticCardImage(const int duration);
907c804472Sopenharmony_ci    void RgbToJpg(unsigned char* data, const int32_t width, const int32_t height);
917c804472Sopenharmony_ci    static uint32_t inputKeyCountPerMinute;
927c804472Sopenharmony_ci    static uint32_t inputMethodCountPerMinute;
937c804472Sopenharmony_ci
947c804472Sopenharmony_ci    void SetFoldable(const bool value);
957c804472Sopenharmony_ci    bool GetFoldable() const;
967c804472Sopenharmony_ci    void SetFoldStatus(const std::string& value);
977c804472Sopenharmony_ci    std::string GetFoldStatus() const;
987c804472Sopenharmony_ci    void SetFoldResolution(int32_t changedFoldWidth, int32_t changedFoldHeight);
997c804472Sopenharmony_ci    int32_t GetFoldWidth() const;
1007c804472Sopenharmony_ci    int32_t GetFoldHeight() const;
1017c804472Sopenharmony_ci    void SetCurrentResolution(int32_t width, int32_t height);
1027c804472Sopenharmony_ci    int32_t GetCurrentWidth() const;
1037c804472Sopenharmony_ci    int32_t GetCurrentHeight() const;
1047c804472Sopenharmony_ci    virtual void InitFlushEmptyTime();
1057c804472Sopenharmony_ci    void InitResolution();
1067c804472Sopenharmony_ci
1077c804472Sopenharmony_ciprotected:
1087c804472Sopenharmony_ci    // start width and height
1097c804472Sopenharmony_ci    int32_t orignalResolutionWidth;
1107c804472Sopenharmony_ci    int32_t orignalResolutionHeight;
1117c804472Sopenharmony_ci    int32_t compressionResolutionWidth;
1127c804472Sopenharmony_ci    int32_t compressionResolutionHeight;
1137c804472Sopenharmony_ci    int32_t foldWidth = 0;
1147c804472Sopenharmony_ci    int32_t foldHeight = 0;
1157c804472Sopenharmony_ci    // width and height after resize
1167c804472Sopenharmony_ci    int32_t currentWidth = 0;
1177c804472Sopenharmony_ci    int32_t currentHeight = 0;
1187c804472Sopenharmony_ci    std::string foldStatus = "unfold";
1197c804472Sopenharmony_ci    bool foldable = false;
1207c804472Sopenharmony_ci    static uint32_t validFrameCountPerMinute;
1217c804472Sopenharmony_ci    static uint32_t invalidFrameCountPerMinute;
1227c804472Sopenharmony_ci    static uint32_t sendFrameCountPerMinute;
1237c804472Sopenharmony_ci
1247c804472Sopenharmony_ci    LocalSocket* screenSocket;
1257c804472Sopenharmony_ci    std::unique_ptr<CppTimer> frameCountTimer;
1267c804472Sopenharmony_ci
1277c804472Sopenharmony_ci    const int32_t sendPeriod = 40;              // A frame is sent per 40 ms.
1287c804472Sopenharmony_ci    const uint16_t pixelSize = 4;               // 4 bytes per pixel
1297c804472Sopenharmony_ci    const size_t headSize = 40;                 // The packet header length is 40 bytes.
1307c804472Sopenharmony_ci    const size_t headReservedSize = 20;         // The reserved length of the packet header is 20 bytes.
1317c804472Sopenharmony_ci    const uint32_t headStart = 0x12345678;      // Buffer header starts with magic value 0x12345678
1327c804472Sopenharmony_ci    const int32_t frameCountPeriod = 60 * 1000; // Frame count per minute
1337c804472Sopenharmony_ci    uint16_t protocolVersion = static_cast<uint16_t>(VirtualScreen::ProtocolVersion::LOADNORMAL);
1347c804472Sopenharmony_ci    bool isWebSocketConfiged;
1357c804472Sopenharmony_ci    std::string currentRouter;
1367c804472Sopenharmony_ci    std::string abilityCurrentRouter;
1377c804472Sopenharmony_ci    std::string fastPreviewMsg;
1387c804472Sopenharmony_ci    uint8_t* jpgScreenBuffer;
1397c804472Sopenharmony_ci    unsigned long jpgBufferSize;
1407c804472Sopenharmony_ci    int jpgPix = 3; // jpg color components
1417c804472Sopenharmony_ci    int redPos = 0;
1427c804472Sopenharmony_ci    int greenPos = 1;
1437c804472Sopenharmony_ci    int bluePos = 2;
1447c804472Sopenharmony_ci
1457c804472Sopenharmony_ci    static std::chrono::system_clock::time_point startTime;
1467c804472Sopenharmony_ci    static std::chrono::system_clock::time_point staticCardStartTime;
1477c804472Sopenharmony_ci    VirtualScreen::LoadDocType startLoadDoc = VirtualScreen::LoadDocType::INIT;
1487c804472Sopenharmony_ci    std::chrono::system_clock::time_point startDropFrameTime;   // record start drop frame time
1497c804472Sopenharmony_ci    int dropFrameFrequency = 0; // save drop frame frequency
1507c804472Sopenharmony_ci};
1517c804472Sopenharmony_ci
1527c804472Sopenharmony_ci#endif // VIRTUALSCREEN_H
153