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 #include "dscreen_handler.h"
17
18 #include "avcodec_info.h"
19 #include "avcodec_list.h"
20 #include "nlohmann/json.hpp"
21 #include "screen.h"
22
23 #include "dscreen_constants.h"
24 #include "dscreen_errcode.h"
25 #include "dscreen_log.h"
26 #include "dscreen_util.h"
27
28 using json = nlohmann::json;
29
30 namespace OHOS {
31 namespace DistributedHardware {
32 IMPLEMENT_SINGLE_INSTANCE(DScreenHandler);
33
DScreenHandler()34 DScreenHandler::DScreenHandler()
35 {
36 DHLOGI("DScreenHandler constructor.");
37 }
38
~DScreenHandler()39 DScreenHandler::~DScreenHandler()
40 {
41 DHLOGI("~DScreenHandler");
42 Rosen::ScreenManager::GetInstance().UnregisterScreenListener(screenListener_);
43 }
44
Initialize()45 int32_t DScreenHandler::Initialize()
46 {
47 DHLOGI("DScreenHandler Initialize");
48 if (screenListener_ == nullptr) {
49 screenListener_ = new (std::nothrow) ScreenListener();
50 }
51 bool ret = Rosen::ScreenManager::GetInstance().RegisterScreenListener(screenListener_);
52 if (!ret) {
53 DHLOGE("register screen listener failed.");
54 return DSCREEN_INIT_ERR;
55 }
56 return DH_SUCCESS;
57 }
58
OnConnect(uint64_t screenId)59 void ScreenListener::OnConnect(uint64_t screenId)
60 {
61 DHLOGI("on screen connect");
62 if (screenId != SCREEN_ID_DEFAULT) {
63 DHLOGI("screenId is invalid, screenId: %ulld", screenId);
64 return;
65 }
66 sptr<Rosen::Screen> screen = Rosen::ScreenManager::GetInstance().GetScreenById(screenId);
67 if (screen == nullptr) {
68 DHLOGE("screen not found, screenId: %ulld", screenId);
69 return;
70 }
71
72 std::string dhId = DSCREEN_PREFIX + SEPERATOR + std::to_string(screenId);
73
74 uint32_t screenWidth = screen->GetWidth();
75 uint32_t screenHeight = screen->GetHeight();
76
77 json attrJson;
78 attrJson[KEY_VERSION] = DSCREEN_VERSION;
79 attrJson[KEY_SCREEN_WIDTH] = screenWidth;
80 attrJson[KEY_SCREEN_HEIGHT] = screenHeight;
81 attrJson[KEY_CODECTYPE] = DScreenHandler::GetInstance().QueryCodecInfo();
82
83 DScreenHandler::GetInstance().PluginHardware(dhId, attrJson.dump());
84 }
85
OnDisconnect(uint64_t screenId)86 void ScreenListener::OnDisconnect(uint64_t screenId)
87 {
88 DHLOGI("on screen disconnect");
89 std::string dhId = DSCREEN_PREFIX + SEPERATOR + std::to_string(screenId);
90 DScreenHandler::GetInstance().UnPluginHardware(dhId);
91 }
92
PluginHardware(const std::string & dhId,const std::string & attr)93 void DScreenHandler::PluginHardware(const std::string &dhId, const std::string &attr)
94 {
95 DHLOGI("DScreenHandler PluginHardware");
96 if (listener_ != nullptr) {
97 listener_->PluginHardware(dhId, attr);
98 }
99 }
100
UnPluginHardware(const std::string & dhId)101 void DScreenHandler::UnPluginHardware(const std::string &dhId)
102 {
103 DHLOGI("DScreenHandler UnPluginHardware");
104 if (listener_ != nullptr) {
105 listener_->UnPluginHardware(dhId);
106 }
107 }
108
Query()109 std::vector<DHItem> DScreenHandler::Query()
110 {
111 DHLOGI("DScreenHandler query hardware info");
112 std::vector<DHItem> dhItemVec;
113 std::vector<sptr<Rosen::Screen>> screens = Rosen::ScreenManager::GetInstance().GetAllScreens();
114 DHLOGI("screens size is: %d.", screens.size());
115 for (const auto &screen : screens) {
116 if (screen == nullptr) {
117 DHLOGE("screen is nullptr.");
118 continue;
119 }
120 if (screen->GetWidth() <= 0) {
121 continue;
122 }
123 std::string dhId = SCREEN_PREFIX + SEPERATOR + std::to_string(screen->GetId());
124 uint32_t screenWidth = screen->GetWidth();
125 uint32_t screenHeight = screen->GetHeight();
126
127 json attrJson;
128 attrJson[KEY_VERSION] = DSCREEN_VERSION;
129 attrJson[KEY_SCREEN_WIDTH] = screenWidth;
130 attrJson[KEY_SCREEN_HEIGHT] = screenHeight;
131 attrJson[KEY_CODECTYPE] = QueryCodecInfo();
132
133 DHItem dhItem;
134 dhItem.dhId = dhId;
135 dhItem.attrs = attrJson.dump();
136 dhItemVec.push_back(dhItem);
137 DHLOGD("query result: dhId: %s, attrs: %s", GetAnonyString(dhId).c_str(), attrJson.dump().c_str());
138 }
139 return dhItemVec;
140 }
141
QueryExtraInfo()142 std::map<std::string, std::string> DScreenHandler::QueryExtraInfo()
143 {
144 DHLOGD("DScreenHandler queryExtraInfo");
145 std::map<std::string, std::string> extraInfo;
146 return extraInfo;
147 }
148
IsSupportPlugin()149 bool DScreenHandler::IsSupportPlugin()
150 {
151 DHLOGD("DScreenHandler IsSupportPlugin");
152 return true;
153 }
154
RegisterPluginListener(std::shared_ptr<PluginListener> listener)155 void DScreenHandler::RegisterPluginListener(std::shared_ptr<PluginListener> listener)
156 {
157 DHLOGD("DScreenHandler register plugin listener");
158 if (listener == nullptr) {
159 DHLOGE("DScreenHandler unregistering plugin listener");
160 return;
161 }
162 listener_ = listener;
163 }
164
UnRegisterPluginListener()165 void DScreenHandler::UnRegisterPluginListener()
166 {
167 DHLOGI("DScreenHandler unRegister plugin listener");
168 listener_ = nullptr;
169 }
170
QueryCodecInfo()171 std::string DScreenHandler::QueryCodecInfo()
172 {
173 DHLOGD("DScreenHandler QueryCodecInfo");
174 if (!codecInfoStr_.empty()) {
175 return codecInfoStr_;
176 }
177
178 // query codec info
179 std::shared_ptr<Media::AVCodecList> codecList = Media::AVCodecListFactory::CreateAVCodecList();
180 if (codecList == nullptr) {
181 return codecInfoStr_;
182 }
183 std::vector<std::shared_ptr<Media::VideoCaps>> caps = codecList->GetVideoEncoderCaps();
184 json codecTypeArray = json::array();
185
186 for (const auto &cap : caps) {
187 if (cap == nullptr) {
188 continue;
189 }
190 std::shared_ptr<Media::AVCodecInfo> codecInfo = cap->GetCodecInfo();
191 if (codecInfo == nullptr) {
192 continue;
193 }
194 codecTypeArray.push_back(codecInfo->GetName());
195 }
196
197 codecInfoStr_ = codecTypeArray.dump();
198 return codecInfoStr_;
199 }
200
GetHardwareHandler()201 IHardwareHandler* GetHardwareHandler()
202 {
203 DHLOGI("DScreenHandler::GetHardwareHandler");
204 return &DScreenHandler::GetInstance();
205 }
206 } // namespace DistributedHardware
207 } // namespace OHOS