• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 #include "histreamer_query_tool.h"
23 
24 #include "dscreen_constants.h"
25 #include "dscreen_errcode.h"
26 #include "dscreen_log.h"
27 #include "dscreen_util.h"
28 
29 using json = nlohmann::json;
30 
31 namespace OHOS {
32 namespace DistributedHardware {
33 namespace {
34 const std::string KEY_HISTREAMER_VIDEO_ENCODER = "histmVidEnc";
35 const std::string KEY_HISTREAMER_VIDEO_DECODER = "histmVidDec";
36 }
37 IMPLEMENT_SINGLE_INSTANCE(DScreenHandler);
38 
DScreenHandler()39 DScreenHandler::DScreenHandler()
40 {
41     DHLOGI("DScreenHandler constructor.");
42 }
43 
~DScreenHandler()44 DScreenHandler::~DScreenHandler()
45 {
46     DHLOGI("~DScreenHandler");
47     Rosen::ScreenManager::GetInstance().UnregisterScreenListener(screenListener_);
48 }
49 
Initialize()50 int32_t DScreenHandler::Initialize()
51 {
52     DHLOGI("DScreenHandler Initialize");
53     if (screenListener_ == nullptr) {
54         screenListener_ = new (std::nothrow) ScreenListener();
55     }
56     Rosen::DMError ret = Rosen::ScreenManager::GetInstance().RegisterScreenListener(screenListener_);
57     if (ret != Rosen::DMError::DM_OK) {
58         DHLOGE("register screen listener failed.");
59         return DSCREEN_INIT_ERR;
60     }
61     return DH_SUCCESS;
62 }
63 
ByteCalculate(std::uint32_t screenWidth)64 uint32_t ScreenListener::ByteCalculate(std::uint32_t screenWidth)
65 {
66     if (screenWidth % BYTE_ALIGNMENT == 0) {
67         return screenWidth;
68     }
69     uint32_t alignedInt = (screenWidth + BYTE_ALIGNMENT_CALCULATION) / BYTE_ALIGNMENT * BYTE_ALIGNMENT;
70     return alignedInt;
71 }
72 
OnConnect(uint64_t screenId)73 void ScreenListener::OnConnect(uint64_t screenId)
74 {
75     DHLOGI("on screen connect");
76     if (screenId != SCREEN_ID_DEFAULT) {
77         DHLOGI("screenId is invalid, screenId is: %{public}" PRIu64, screenId);
78         return;
79     }
80     sptr<Rosen::Screen> screen = Rosen::ScreenManager::GetInstance().GetScreenById(screenId);
81     if (screen == nullptr) {
82         DHLOGE("screen not found, screenId is: %{public}" PRIu64, screenId);
83         return;
84     }
85     if (!screen->IsReal()) {
86         DHLOGI("screen is not real");
87         return;
88     }
89     std::string dhId = DSCREEN_PREFIX + SEPERATOR + std::to_string(screenId);
90     uint32_t screenWidth = screen->GetWidth();
91     screenWidth = ByteCalculate(screenWidth);
92     uint32_t screenHeight = screen->GetHeight();
93 
94     json attrJson;
95     attrJson[KEY_VERSION] = DSCREEN_VERSION;
96     attrJson[KEY_SCREEN_WIDTH] = screenWidth;
97     attrJson[KEY_SCREEN_HEIGHT] = screenHeight;
98     attrJson[KEY_CODECTYPE] = DScreenHandler::GetInstance().QueryCodecInfo();
99     std::string subtype = "screen";
100 
101     DScreenHandler::GetInstance().PluginHardware(dhId, attrJson.dump(), subtype);
102 }
103 
OnDisconnect(uint64_t screenId)104 void ScreenListener::OnDisconnect(uint64_t screenId)
105 {
106     DHLOGI("on screen disconnect");
107     std::string dhId = DSCREEN_PREFIX + SEPERATOR + std::to_string(screenId);
108     DScreenHandler::GetInstance().UnPluginHardware(dhId);
109 }
110 
PluginHardware(const std::string & dhId,const std::string & attr,const std::string & subtype)111 void DScreenHandler::PluginHardware(const std::string &dhId, const std::string &attr, const std::string &subtype)
112 {
113     DHLOGI("DScreenHandler PluginHardware");
114     if (listener_ != nullptr) {
115         listener_->PluginHardware(dhId, attr, subtype);
116     }
117 }
118 
UnPluginHardware(const std::string & dhId)119 void DScreenHandler::UnPluginHardware(const std::string &dhId)
120 {
121     DHLOGI("DScreenHandler UnPluginHardware");
122     if (listener_ != nullptr) {
123         listener_->UnPluginHardware(dhId);
124     }
125 }
126 
QueryMeta()127 std::vector<DHItem> DScreenHandler::QueryMeta()
128 {
129     return {};
130 }
131 
Query()132 std::vector<DHItem> DScreenHandler::Query()
133 {
134     DHLOGI("DScreenHandler query hardware info");
135     std::vector<DHItem> dhItemVec;
136     std::vector<sptr<Rosen::Screen>> screens;
137     Rosen::ScreenManager::GetInstance().GetAllScreens(screens);
138     DHLOGI("screens size is: %{public}zu", screens.size());
139     for (const auto &screen : screens) {
140         if (screen == nullptr) {
141             DHLOGE("screen is nullptr.");
142             continue;
143         }
144         if (screen->GetWidth() <= 0) {
145             continue;
146         }
147         if (!screen->IsReal()) {
148             continue;
149         }
150         std::string dhId = DSCREEN_PREFIX + SEPERATOR + std::to_string(screen->GetId());
151         uint32_t screenWidth = screen->GetWidth();
152         if (screenListener_ == nullptr) {
153             screenListener_ = new (std::nothrow) ScreenListener();
154             if (screenListener_ == nullptr) {
155                 DHLOGE("New ScreenListener failed, dhID: %{public}s", dhId.c_str());
156                 continue;
157             }
158         }
159         screenWidth = screenListener_->ByteCalculate(screenWidth);
160         uint32_t screenHeight = screen->GetHeight();
161 
162         json attrJson;
163         attrJson[KEY_VERSION] = DSCREEN_VERSION;
164         attrJson[KEY_SCREEN_WIDTH] = screenWidth;
165         attrJson[KEY_SCREEN_HEIGHT] = screenHeight;
166         attrJson[KEY_CODECTYPE] = QueryCodecInfo();
167         std::string videoEncoders =
168             HiStreamerQueryTool::GetInstance().QueryHiStreamerPluginInfo(HISTREAM_PLUGIN_TYPE::VIDEO_ENCODER);
169         DHLOGI("DScreen QueryVideoEncoderAbility info: %{public}s", videoEncoders.c_str());
170         attrJson[KEY_HISTREAMER_VIDEO_ENCODER] = videoEncoders;
171         std::string videoDecoders =
172             HiStreamerQueryTool::GetInstance().QueryHiStreamerPluginInfo(HISTREAM_PLUGIN_TYPE::VIDEO_DECODER);
173         DHLOGI("DScreen QueryVideoDecoderAbility info: %{public}s", videoDecoders.c_str());
174         attrJson[KEY_HISTREAMER_VIDEO_DECODER] = videoDecoders;
175 
176         DHItem dhItem;
177         dhItem.dhId = dhId;
178         dhItem.subtype = "screen";
179         dhItem.attrs = attrJson.dump();
180         dhItemVec.push_back(dhItem);
181         DHLOGD("query result: dhId: %{public}s, attrs: %{public}s", dhId.c_str(), attrJson.dump().c_str());
182     }
183     return dhItemVec;
184 }
185 
QueryExtraInfo()186 std::map<std::string, std::string> DScreenHandler::QueryExtraInfo()
187 {
188     DHLOGD("DScreenHandler queryExtraInfo");
189     std::map<std::string, std::string> extraInfo;
190     return extraInfo;
191 }
192 
IsSupportPlugin()193 bool DScreenHandler::IsSupportPlugin()
194 {
195     DHLOGD("DScreenHandler IsSupportPlugin");
196     return true;
197 }
198 
RegisterPluginListener(std::shared_ptr<PluginListener> listener)199 void DScreenHandler::RegisterPluginListener(std::shared_ptr<PluginListener> listener)
200 {
201     DHLOGD("DScreenHandler register plugin listener");
202     if (listener == nullptr) {
203         DHLOGE("DScreenHandler unregistering plugin listener");
204         return;
205     }
206     listener_ = listener;
207 }
208 
UnRegisterPluginListener()209 void DScreenHandler::UnRegisterPluginListener()
210 {
211     DHLOGI("DScreenHandler unRegister plugin listener");
212     listener_ = nullptr;
213 }
214 
QueryCodecInfo()215 std::string DScreenHandler::QueryCodecInfo()
216 {
217     DHLOGD("DScreenHandler QueryCodecInfo");
218     if (!codecInfoStr_.empty()) {
219         return codecInfoStr_;
220     }
221 
222     // query codec info
223     std::shared_ptr<MediaAVCodec::AVCodecList> codecList = MediaAVCodec::AVCodecListFactory::CreateAVCodecList();
224     if (codecList == nullptr) {
225         DHLOGE("Create avCodecList failed.");
226         return codecInfoStr_;
227     }
228     const std::vector<std::string> encoderName = {std::string(MediaAVCodec::CodecMimeType::VIDEO_AVC),
229                                                   std::string(MediaAVCodec::CodecMimeType::VIDEO_HEVC)};
230     json codecTypeArray = json::array();
231 
232     for (const auto &coder : encoderName) {
233         MediaAVCodec::CapabilityData *capData = codecList->GetCapability(coder, true,
234             MediaAVCodec::AVCodecCategory::AVCODEC_HARDWARE);
235             if (capData == nullptr) {
236                 continue;
237             }
238         std::string mimeType = capData->mimeType;
239         codecTypeArray.push_back(mimeType);
240     }
241 
242     codecInfoStr_ = codecTypeArray.dump();
243     return codecInfoStr_;
244 }
245 
GetHardwareHandler()246 IHardwareHandler* GetHardwareHandler()
247 {
248     DHLOGI("DScreenHandler::GetHardwareHandler");
249     return &DScreenHandler::GetInstance();
250 }
251 } // namespace DistributedHardware
252 } // namespace OHOS