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