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