1 /* 2 * Copyright (c) 2022-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_V1_0_H 17 #define OHOS_DSCREEN_V1_0_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 namespace V1_0 { 32 class DScreen; 33 class IDScreenCallback { 34 public: ~IDScreenCallback()35 virtual ~IDScreenCallback() {}; 36 virtual void OnRegResult(const std::shared_ptr<DScreen> &dScreen, 37 const std::string &reqId, const int32_t status, const std::string &data) = 0; 38 virtual void OnUnregResult(const std::shared_ptr<DScreen> &dScreen, 39 const std::string &reqId, const int32_t status, const std::string &data) = 0; 40 }; 41 42 class ScreenSourceTransCallback : public IScreenSourceTransCallback { 43 public: ~ScreenSourceTransCallback()44 ~ScreenSourceTransCallback() override {}; 45 virtual void OnTransError(int32_t err, const std::string &content) = 0; OnError(int32_t err,const std::string & content)46 void OnError(int32_t err, const std::string &content) override 47 { 48 OnTransError(err, content); 49 } 50 }; 51 52 class Task { 53 public: Task(TaskType taskType,const std::string & taskId,const std::string & taskParam)54 Task(TaskType taskType, const std::string &taskId, const std::string &taskParam) : taskType_(taskType), 55 taskId_(taskId), taskParam_(taskParam) {}; Task(TaskType taskType,const std::string & taskParam)56 Task(TaskType taskType, const std::string &taskParam) : taskType_(taskType), 57 taskId_(""), taskParam_(taskParam) {}; ~Task()58 ~Task() {}; 59 GetTaskType()60 TaskType GetTaskType() 61 { 62 return taskType_; 63 }; GetTaskId()64 std::string GetTaskId() 65 { 66 return taskId_; 67 }; GetTaskParam()68 std::string GetTaskParam() 69 { 70 return taskParam_; 71 }; 72 73 private: 74 TaskType taskType_; 75 std::string taskId_; 76 std::string taskParam_; 77 }; 78 79 class DScreen : public ScreenSourceTransCallback, public std::enable_shared_from_this<DScreen> { 80 public: 81 ~DScreen() override; 82 DScreen(const std::string &devId, const std::string &dhId, std::shared_ptr<IDScreenCallback> dscreenCallback); 83 void OnTransError(int32_t err, const std::string &content) override; 84 85 void SetVideoParam(std::shared_ptr<VideoParam> &videoParam); 86 std::shared_ptr<VideoParam> GetVideoParam(); 87 void SetState(DScreenState state); 88 DScreenState GetState() const; 89 uint64_t GetScreenId() const; 90 std::string GetDHId() const; 91 std::string GetDevId() const; 92 int32_t AddTask(const std::shared_ptr<Task> &task); 93 void SetScreenVersion(const std::string &version); 94 std::string GetScreenVersion(); 95 96 private: 97 void TaskThreadLoop(); 98 void HandleTask(const std::shared_ptr<Task> &task); 99 void HandleEnable(const std::string ¶m, const std::string &taskId); 100 void HandleDisable(const std::string &taskId); 101 void HandleConnect(); 102 void HandleDisconnect(); 103 int32_t CheckJsonData(json &attrJson); 104 int32_t NegotiateCodecType(const std::string &remoteCodecInfoStr); 105 int32_t SetUp(); 106 int32_t Start(); 107 int32_t Stop(); 108 109 std::string devId_; 110 std::string dhId_; 111 uint64_t screenId_ = SCREEN_ID_INVALID; 112 std::shared_ptr<VideoParam> videoParam_ = nullptr; 113 std::shared_ptr<IScreenSourceTrans> sourceTrans_ = nullptr; 114 std::shared_ptr<IDScreenCallback> dscreenCallback_ = nullptr; 115 116 DScreenState curState_; 117 std::mutex stateMtx_; 118 std::thread taskQueueThread_; 119 std::condition_variable taskQueueCond_; 120 std::mutex taskQueueMtx_; 121 std::queue<std::shared_ptr<Task>> taskQueue_; 122 bool taskThreadRunning_; 123 std::string version_ = "1.0"; 124 }; 125 } // namespace V1_0 126 } // namespace DistributedHardware 127 } // namespace OHOS 128 #endif