• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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: %" 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: %" 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     DHLOGI("screenWidth is : %" PRIu32, screenWidth);
93     uint32_t screenHeight = screen->GetHeight();
94 
95     json attrJson;
96     attrJson[KEY_VERSION] = DSCREEN_VERSION;
97     attrJson[KEY_SCREEN_WIDTH] = screenWidth;
98     attrJson[KEY_SCREEN_HEIGHT] = screenHeight;
99     attrJson[KEY_CODECTYPE] = DScreenHandler::GetInstance().QueryCodecInfo();
100     std::string subtype = "screen";
101 
102     DScreenHandler::GetInstance().PluginHardware(dhId, attrJson.dump(), subtype);
103 }
104 
OnDisconnect(uint64_t screenId)105 void ScreenListener::OnDisconnect(uint64_t screenId)
106 {
107     DHLOGI("on screen disconnect");
108     std::string dhId = DSCREEN_PREFIX + SEPERATOR + std::to_string(screenId);
109     DScreenHandler::GetInstance().UnPluginHardware(dhId);
110 }
111 
PluginHardware(const std::string & dhId,const std::string & attr,const std::string & subtype)112 void DScreenHandler::PluginHardware(const std::string &dhId, const std::string &attr, const std::string &subtype)
113 {
114     DHLOGI("DScreenHandler PluginHardware");
115     if (listener_ != nullptr) {
116         listener_->PluginHardware(dhId, attr, subtype);
117     }
118 }
119 
UnPluginHardware(const std::string & dhId)120 void DScreenHandler::UnPluginHardware(const std::string &dhId)
121 {
122     DHLOGI("DScreenHandler UnPluginHardware");
123     if (listener_ != nullptr) {
124         listener_->UnPluginHardware(dhId);
125     }
126 }
127 
Query()128 std::vector<DHItem> DScreenHandler::Query()
129 {
130     DHLOGI("DScreenHandler query hardware info");
131     std::vector<DHItem> dhItemVec;
132     std::vector<sptr<Rosen::Screen>> screens;
133     Rosen::ScreenManager::GetInstance().GetAllScreens(screens);
134     DHLOGI("screens size is: %" PRId32, screens.size());
135     for (const auto &screen : screens) {
136         if (screen == nullptr) {
137             DHLOGE("screen is nullptr.");
138             continue;
139         }
140         if (screen->GetWidth() <= 0) {
141             continue;
142         }
143         if (!screen->IsReal()) {
144             continue;
145         }
146         std::string dhId = DSCREEN_PREFIX + SEPERATOR + std::to_string(screen->GetId());
147         uint32_t screenWidth = screen->GetWidth();
148         if (screenListener_ == nullptr) {
149             screenListener_ = new (std::nothrow) ScreenListener();
150         }
151         screenWidth = screenListener_->ByteCalculate(screenWidth);
152         DHLOGI("screenWidth is : %" PRIu32, screenWidth);
153         uint32_t screenHeight = screen->GetHeight();
154 
155         json attrJson;
156         attrJson[KEY_VERSION] = DSCREEN_VERSION;
157         attrJson[KEY_SCREEN_WIDTH] = screenWidth;
158         attrJson[KEY_SCREEN_HEIGHT] = screenHeight;
159         attrJson[KEY_CODECTYPE] = QueryCodecInfo();
160         std::string videoEncoders =
161             HiStreamerQueryTool::GetInstance().QueryHiStreamerPluginInfo(HISTREAM_PLUGIN_TYPE::VIDEO_ENCODER);
162         DHLOGI("DScreen QueryVideoEncoderAbility info: %s", videoEncoders.c_str());
163         attrJson[KEY_HISTREAMER_VIDEO_ENCODER] = videoEncoders;
164         std::string videoDecoders =
165             HiStreamerQueryTool::GetInstance().QueryHiStreamerPluginInfo(HISTREAM_PLUGIN_TYPE::VIDEO_DECODER);
166         DHLOGI("DScreen QueryVideoDecoderAbility info: %s", videoDecoders.c_str());
167         attrJson[KEY_HISTREAMER_VIDEO_DECODER] = videoDecoders;
168 
169         DHItem dhItem;
170         dhItem.dhId = dhId;
171         dhItem.subtype = "screen";
172         dhItem.attrs = attrJson.dump();
173         dhItemVec.push_back(dhItem);
174         DHLOGD("query result: dhId: %s, attrs: %s", dhId.c_str(), attrJson.dump().c_str());
175     }
176     return dhItemVec;
177 }
178 
QueryExtraInfo()179 std::map<std::string, std::string> DScreenHandler::QueryExtraInfo()
180 {
181     DHLOGD("DScreenHandler queryExtraInfo");
182     std::map<std::string, std::string> extraInfo;
183     return extraInfo;
184 }
185 
IsSupportPlugin()186 bool DScreenHandler::IsSupportPlugin()
187 {
188     DHLOGD("DScreenHandler IsSupportPlugin");
189     return true;
190 }
191 
RegisterPluginListener(std::shared_ptr<PluginListener> listener)192 void DScreenHandler::RegisterPluginListener(std::shared_ptr<PluginListener> listener)
193 {
194     DHLOGD("DScreenHandler register plugin listener");
195     if (listener == nullptr) {
196         DHLOGE("DScreenHandler unregistering plugin listener");
197         return;
198     }
199     listener_ = listener;
200 }
201 
UnRegisterPluginListener()202 void DScreenHandler::UnRegisterPluginListener()
203 {
204     DHLOGI("DScreenHandler unRegister plugin listener");
205     listener_ = nullptr;
206 }
207 
QueryCodecInfo()208 std::string DScreenHandler::QueryCodecInfo()
209 {
210     DHLOGD("DScreenHandler QueryCodecInfo");
211     if (!codecInfoStr_.empty()) {
212         return codecInfoStr_;
213     }
214 
215     // query codec info
216     std::shared_ptr<Media::AVCodecList> codecList = Media::AVCodecListFactory::CreateAVCodecList();
217     if (codecList == nullptr) {
218         return codecInfoStr_;
219     }
220     std::vector<std::shared_ptr<Media::VideoCaps>> caps = codecList->GetVideoEncoderCaps();
221     json codecTypeArray = json::array();
222 
223     for (const auto &cap : caps) {
224         if (cap == nullptr) {
225             continue;
226         }
227         std::shared_ptr<Media::AVCodecInfo> codecInfo = cap->GetCodecInfo();
228         if (codecInfo == nullptr) {
229             continue;
230         }
231         codecTypeArray.push_back(codecInfo->GetName());
232     }
233 
234     codecInfoStr_ = codecTypeArray.dump();
235     return codecInfoStr_;
236 }
237 
GetHardwareHandler()238 IHardwareHandler* GetHardwareHandler()
239 {
240     DHLOGI("DScreenHandler::GetHardwareHandler");
241     return &DScreenHandler::GetInstance();
242 }
243 } // namespace DistributedHardware
244 } // namespace OHOS