1 /* 2 * Copyright (c) 2022 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_H 17 #define OHOS_DSCREEN_H 18 19 #include <condition_variable> 20 #include <mutex> 21 #include <queue> 22 #include <thread> 23 24 #include "dscreen_constants.h" 25 #include "iscreen_source_trans.h" 26 #include "iscreen_source_trans_callback.h" 27 #include "video_param.h" 28 29 namespace OHOS { 30 namespace DistributedHardware { 31 class DScreen; 32 class IDScreenCallback { 33 public: ~IDScreenCallback()34 virtual ~IDScreenCallback() {}; 35 virtual void OnRegResult(const std::shared_ptr<DScreen> &dScreen, 36 const std::string &reqId, int32_t status, const std::string &data) = 0; 37 virtual void OnUnregResult(const std::shared_ptr<DScreen> &dScreen, 38 const std::string &reqId, int32_t status, const std::string &data) = 0; 39 }; 40 41 class ScreenSourceTransCallback : public IScreenSourceTransCallback { 42 public: ~ScreenSourceTransCallback()43 ~ScreenSourceTransCallback() override {}; 44 virtual void OnTransError(int32_t err, const std::string &content) = 0; OnError(int32_t err,const std::string & content)45 virtual void OnError(int32_t err, const std::string &content) override 46 { 47 OnTransError(err, content); 48 } 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() 60 { 61 return taskType_; 62 }; GetTaskId()63 std::string GetTaskId() 64 { 65 return taskId_; 66 }; GetTaskParam()67 std::string GetTaskParam() 68 { 69 return taskParam_; 70 }; 71 72 private: 73 TaskType taskType_; 74 std::string taskId_; 75 std::string taskParam_; 76 }; 77 78 class DScreen : public ScreenSourceTransCallback, public std::enable_shared_from_this<DScreen> { 79 public: 80 ~DScreen(); 81 DScreen(const std::string &devId, const std::string &dhId, std::shared_ptr<IDScreenCallback> dscreenCallback); 82 void OnTransError(int32_t err, const std::string &content) override; 83 84 void SetVideoParam(std::shared_ptr<VideoParam> &videoParam); 85 std::shared_ptr<VideoParam> GetVideoParam(); 86 void SetState(DScreenState state); 87 DScreenState GetState() const; 88 uint64_t GetScreenId() const; 89 std::string GetDHId() const; 90 std::string GetDevId() const; 91 int32_t AddTask(const std::shared_ptr<Task> &task); 92 93 private: 94 void TaskThreadLoop(); 95 void HandleTask(const std::shared_ptr<Task> &task); 96 void HandleEnable(const std::string ¶m, const std::string &taskId); 97 void HandleDisable(const std::string &taskId); 98 void HandleConnect(); 99 void HandleDisconnect(); 100 int32_t NegotiateCodecType(const std::string &remoteCodecInfoStr); 101 int32_t SetUp(); 102 int32_t Start(); 103 int32_t Stop(); 104 105 static constexpr uint8_t TASK_WAIT_SECONDS = 1; 106 107 std::string devId_; 108 std::string dhId_; 109 uint64_t screenId_ = SCREEN_ID_INVALID; 110 std::shared_ptr<VideoParam> videoParam_ = nullptr; 111 std::shared_ptr<IScreenSourceTrans> sourceTrans_ = nullptr; 112 std::shared_ptr<IDScreenCallback> dscreenCallback_ = nullptr; 113 114 DScreenState curState_; 115 std::mutex stateMtx_; 116 std::thread taskQueueThread_; 117 std::condition_variable taskQueueCond_; 118 std::mutex taskQueueMtx_; 119 std::queue<std::shared_ptr<Task>> taskQueue_; 120 bool taskThreadRunning_; 121 }; 122 } // namespace DistributedHardware 123 } // namespace OHOS 124 #endif