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 OHOS_DSCREEN_V2_0_H 17 #define OHOS_DSCREEN_V2_0_H 18 19 #include <condition_variable> 20 #include <cstdio> 21 #include <fstream> 22 #include <mutex> 23 #include <queue> 24 #include <thread> 25 #include <string> 26 #include "sync_fence.h" 27 28 #include "surface.h" 29 #include "iconsumer_surface.h" 30 31 #include "dscreen_constants.h" 32 #include "distributed_hardware_fwk_kit_paras.h" 33 #include "histreamer_ability_parser.h" 34 #include "video_param.h" 35 #include "av_sender_engine_adapter.h" 36 #include "transaction/rs_interfaces.h" 37 38 namespace OHOS { 39 namespace DistributedHardware { 40 namespace V2_0 { 41 class DScreen; 42 class IDScreenCallback { 43 public: ~IDScreenCallback()44 virtual ~IDScreenCallback() {}; 45 virtual void OnRegResult(const std::shared_ptr<DScreen> &dScreen, 46 const std::string &reqId, const int32_t status, const std::string &data) = 0; 47 virtual void OnUnregResult(const std::shared_ptr<DScreen> &dScreen, 48 const std::string &reqId, const int32_t status, const std::string &data) = 0; 49 }; 50 51 class Task { 52 public: Task(TaskType taskType,const std::string & taskId,const std::string & taskParam)53 Task(TaskType taskType, const std::string &taskId, const std::string &taskParam) : taskType_(taskType), 54 taskId_(taskId), taskParam_(taskParam) {}; Task(TaskType taskType,const std::string & taskParam)55 Task(TaskType taskType, const std::string &taskParam) : taskType_(taskType), 56 taskId_(""), taskParam_(taskParam) {}; ~Task()57 ~Task() {}; 58 GetTaskType()59 TaskType GetTaskType() const 60 { 61 return taskType_; 62 }; GetTaskId()63 std::string GetTaskId() const 64 { 65 return taskId_; 66 }; GetTaskParam()67 std::string GetTaskParam() const 68 { 69 return taskParam_; 70 }; 71 72 private: 73 TaskType taskType_; 74 std::string taskId_; 75 std::string taskParam_; 76 }; 77 78 class ConsumBufferListener : public IBufferConsumerListener { 79 public: ConsumBufferListener(const std::shared_ptr<DScreen> dScreen)80 ConsumBufferListener(const std::shared_ptr<DScreen> dScreen) : dScreen_(dScreen) {}; 81 ~ConsumBufferListener() = default; 82 void OnBufferAvailable() override; 83 private: 84 static const constexpr char *DSCREEN_LOG_TAG = "ConsumBufferListener"; 85 std::shared_ptr<DScreen> dScreen_; 86 }; 87 88 class DScreen : public AVSenderAdapterCallback, public std::enable_shared_from_this<DScreen> { 89 public: 90 DScreen(const std::string &devId, const std::string &dhId, std::shared_ptr<IDScreenCallback> dscreenCallback); 91 ~DScreen(); 92 93 // interfaces from AVSenderAdapterCallback 94 void OnEngineEvent(DScreenEventType event, const std::string &content) override; 95 void OnEngineMessage(const std::shared_ptr<AVTransMessage> &message) override; 96 97 int32_t AddTask(const std::shared_ptr<Task> &task); 98 int32_t InitSenderEngine(IAVEngineProvider *providerPtr, const std::string &peerDevId); 99 void ConsumeSurface(); 100 std::string GetDHId() const; 101 std::string GetDevId() const; 102 uint64_t GetScreenId() const; 103 DScreenState GetState() const; 104 std::shared_ptr<VideoParam> GetVideoParam(); 105 106 private: 107 void TaskThreadLoop(); 108 void HandleTask(const std::shared_ptr<Task> &task); 109 void HandleEnable(const std::string ¶m, const std::string &taskId); 110 void HandleDisable(const std::string &taskId); 111 void HandleConnect(); 112 void HandleDisconnect(); 113 int32_t StartSenderEngine(); 114 int32_t StopSenderEngine(); 115 void ParseInputScreenParam(const std::string ¶m, const std::string &taskId); 116 117 /* 118 * Negotiate the codec format between local to remote device, 119 * for DScreen, local encoder screen, remote decoder it. 120 * rmtDecoderStr: remote Decoder description in JSON format 121 */ 122 int32_t NegotiateCodecType(const std::string &rmtDecoderStr); 123 int32_t ChooseCodecType(const std::vector<VideoEncoder> &localVideoEncoders, 124 const std::vector<VideoDecoder> &rmtVideoDecoders); 125 int32_t ConfigSurface(); 126 int32_t RemoveSurface(); 127 int32_t SetUp(); 128 void ChooseParameter(std::string &codecType, std::string &pixelFormat); 129 bool CheckJsonData(const json &attrJson); 130 void SetState(DScreenState state); 131 int32_t WaitForSinkStarted(); 132 133 std::string devId_; 134 std::string dhId_; 135 uint64_t screenId_ = SCREEN_ID_INVALID; 136 std::shared_ptr<VideoParam> videoParam_ = nullptr; 137 std::shared_ptr<IDScreenCallback> dscreenCallback_ = nullptr; 138 sptr<Surface> consumerSurface_ = nullptr; 139 sptr<IBufferConsumerListener> consumerBufferListener_ = nullptr; 140 141 DScreenState curState_; 142 std::mutex stateMtx_; 143 std::mutex ableMtx_; 144 std::thread taskQueueThread_; 145 std::condition_variable taskQueueCond_; 146 std::mutex taskQueueMtx_; 147 std::queue<std::shared_ptr<Task>> taskQueue_; 148 bool taskThreadRunning_; 149 bool watchdogFlag_; 150 std::shared_ptr<AVTransSenderAdapter> senderAdapter_; 151 OHOS::sptr<OHOS::SyncFence> syncFence_ = SyncFence::INVALID_FENCE; 152 153 std::mutex waitSinkMtx_; 154 std::condition_variable waitSinkCondVar_; 155 std::atomic<bool> sinkStartSuccess_ {false}; 156 }; 157 } // namespace V2_0 158 } // namespace DistributedHardware 159 } // namespace OHOS 160 #endif