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: %{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         }
155         screenWidth = screenListener_->ByteCalculate(screenWidth);
156         uint32_t screenHeight = screen->GetHeight();
157 
158         json attrJson;
159         attrJson[KEY_VERSION] = DSCREEN_VERSION;
160         attrJson[KEY_SCREEN_WIDTH] = screenWidth;
161         attrJson[KEY_SCREEN_HEIGHT] = screenHeight;
162         attrJson[KEY_CODECTYPE] = QueryCodecInfo();
163         std::string videoEncoders =
164             HiStreamerQueryTool::GetInstance().QueryHiStreamerPluginInfo(HISTREAM_PLUGIN_TYPE::VIDEO_ENCODER);
165         DHLOGI("DScreen QueryVideoEncoderAbility info: %{public}s", videoEncoders.c_str());
166         attrJson[KEY_HISTREAMER_VIDEO_ENCODER] = videoEncoders;
167         std::string videoDecoders =
168             HiStreamerQueryTool::GetInstance().QueryHiStreamerPluginInfo(HISTREAM_PLUGIN_TYPE::VIDEO_DECODER);
169         DHLOGI("DScreen QueryVideoDecoderAbility info: %{public}s", videoDecoders.c_str());
170         attrJson[KEY_HISTREAMER_VIDEO_DECODER] = videoDecoders;
171 
172         DHItem dhItem;
173         dhItem.dhId = dhId;
174         dhItem.subtype = "screen";
175         dhItem.attrs = attrJson.dump();
176         dhItemVec.push_back(dhItem);
177         DHLOGD("query result: dhId: %{public}s, attrs: %{public}s", dhId.c_str(), attrJson.dump().c_str());
178     }
179     return dhItemVec;
180 }
181 
QueryExtraInfo()182 std::map<std::string, std::string> DScreenHandler::QueryExtraInfo()
183 {
184     DHLOGD("DScreenHandler queryExtraInfo");
185     std::map<std::string, std::string> extraInfo;
186     return extraInfo;
187 }
188 
IsSupportPlugin()189 bool DScreenHandler::IsSupportPlugin()
190 {
191     DHLOGD("DScreenHandler IsSupportPlugin");
192     return true;
193 }
194 
RegisterPluginListener(std::shared_ptr<PluginListener> listener)195 void DScreenHandler::RegisterPluginListener(std::shared_ptr<PluginListener> listener)
196 {
197     DHLOGD("DScreenHandler register plugin listener");
198     if (listener == nullptr) {
199         DHLOGE("DScreenHandler unregistering plugin listener");
200         return;
201     }
202     listener_ = listener;
203 }
204 
UnRegisterPluginListener()205 void DScreenHandler::UnRegisterPluginListener()
206 {
207     DHLOGI("DScreenHandler unRegister plugin listener");
208     listener_ = nullptr;
209 }
210 
QueryCodecInfo()211 std::string DScreenHandler::QueryCodecInfo()
212 {
213     DHLOGD("DScreenHandler QueryCodecInfo");
214     if (!codecInfoStr_.empty()) {
215         return codecInfoStr_;
216     }
217 
218     // query codec info
219     std::shared_ptr<MediaAVCodec::AVCodecList> codecList = MediaAVCodec::AVCodecListFactory::CreateAVCodecList();
220     if (codecList == nullptr) {
221         DHLOGE("Create avCodecList failed.");
222         return codecInfoStr_;
223     }
224     const std::vector<std::string> encoderName = {std::string(MediaAVCodec::CodecMimeType::VIDEO_AVC),
225                                                   std::string(MediaAVCodec::CodecMimeType::VIDEO_HEVC)};
226     json codecTypeArray = json::array();
227 
228     for (const auto &coder : encoderName) {
229         MediaAVCodec::CapabilityData *capData = codecList->GetCapability(coder, true,
230             MediaAVCodec::AVCodecCategory::AVCODEC_HARDWARE);
231             if (capData == nullptr) {
232                 continue;
233             }
234         std::string mimeType = capData->mimeType;
235         codecTypeArray.push_back(mimeType);
236     }
237 
238     codecInfoStr_ = codecTypeArray.dump();
239     return codecInfoStr_;
240 }
241 
GetHardwareHandler()242 IHardwareHandler* GetHardwareHandler()
243 {
244     DHLOGI("DScreenHandler::GetHardwareHandler");
245     return &DScreenHandler::GetInstance();
246 }
247 } // namespace DistributedHardware
248 } // namespace OHOS