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