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_) {
49 screenListener_ = new ScreenListener();
50 }
51 bool ret = Rosen::ScreenManager::GetInstance().RegisterScreenListener(screenListener_);
52 if (!ret) {
53 DHLOGE("register screen listener failed.");
54 }
55 return DH_SUCCESS;
56 }
57
OnConnect(uint64_t screenId)58 void ScreenListener::OnConnect(uint64_t screenId)
59 {
60 DHLOGI("on screen connect");
61 if (screenId != SCREEN_ID_DEFAULT) {
62 return;
63 }
64 sptr<Rosen::Screen> screen = Rosen::ScreenManager::GetInstance().GetScreenById(screenId);
65 if (screen == nullptr) {
66 DHLOGE("screen not found, screenId: %ulld", screenId);
67 return;
68 }
69
70 std::string dhId = DSCREEN_PREFIX + SEPERATOR + std::to_string(screenId);
71
72 uint32_t screenWidth = screen->GetWidth();
73 uint32_t screenHeight = screen->GetHeight();
74
75 json attrJson;
76 attrJson[KEY_VERSION] = DSCREEN_VERSION;
77 attrJson[KEY_SCREEN_WIDTH] = screenWidth;
78 attrJson[KEY_SCREEN_HEIGHT] = screenHeight;
79 attrJson[KEY_CODECTYPE] = DScreenHandler::GetInstance().QueryCodecInfo();
80
81 DScreenHandler::GetInstance().PluginHardware(dhId, attrJson.dump());
82 }
83
OnDisconnect(uint64_t screenId)84 void ScreenListener::OnDisconnect(uint64_t screenId)
85 {
86 DHLOGI("on screen disconnect");
87 std::string dhId = DSCREEN_PREFIX + SEPERATOR + std::to_string(screenId);
88 DScreenHandler::GetInstance().UnPluginHardware(dhId);
89 }
90
PluginHardware(const std::string & dhId,const std::string & attr)91 void DScreenHandler::PluginHardware(const std::string &dhId, const std::string &attr)
92 {
93 if (listener_ != nullptr) {
94 listener_->PluginHardware(dhId, attr);
95 }
96 }
97
UnPluginHardware(const std::string & dhId)98 void DScreenHandler::UnPluginHardware(const std::string &dhId)
99 {
100 if (listener_ != nullptr) {
101 listener_->UnPluginHardware(dhId);
102 }
103 }
104
Query()105 std::vector<DHItem> DScreenHandler::Query()
106 {
107 DHLOGI("DScreenHandler query hardware info");
108 std::vector<DHItem> dhItemVec;
109 std::vector<sptr<Rosen::Screen>> screens = Rosen::ScreenManager::GetInstance().GetAllScreens();
110 sptr<Rosen::Screen> screen = screens[SCREEN_ID_DEFAULT];
111 std::string dhId = SCREEN_PREFIX + SEPERATOR + std::to_string(screen->GetId());
112 uint32_t screenWidth = screen->GetWidth();
113 uint32_t screenHeight = screen->GetHeight();
114
115 json attrJson;
116 attrJson[KEY_VERSION] = DSCREEN_VERSION;
117 attrJson[KEY_SCREEN_WIDTH] = screenWidth;
118 attrJson[KEY_SCREEN_HEIGHT] = screenHeight;
119 attrJson[KEY_CODECTYPE] = QueryCodecInfo();
120
121 DHItem dhItem;
122 dhItem.dhId = dhId;
123 dhItem.attrs = attrJson.dump();
124 dhItemVec.push_back(dhItem);
125 DHLOGD("query result: dhId: %s, attrs: %s", GetAnonyString(dhId).c_str(), attrJson.dump().c_str());
126
127 return dhItemVec;
128 }
129
QueryExtraInfo()130 std::map<std::string, std::string> DScreenHandler::QueryExtraInfo()
131 {
132 DHLOGD("DScreenHandler queryExtraInfo");
133 std::map<std::string, std::string> extraInfo;
134 return extraInfo;
135 }
136
IsSupportPlugin()137 bool DScreenHandler::IsSupportPlugin()
138 {
139 DHLOGD("DScreenHandler IsSupportPlugin");
140 return true;
141 }
142
RegisterPluginListener(std::shared_ptr<PluginListener> listener)143 void DScreenHandler::RegisterPluginListener(std::shared_ptr<PluginListener> listener)
144 {
145 DHLOGD("DScreenHandler register plugin listener");
146 listener_ = listener;
147 }
148
QueryCodecInfo()149 std::string DScreenHandler::QueryCodecInfo()
150 {
151 DHLOGD("DScreenHandler QueryCodecInfo");
152 if (!codecInfoStr_.empty()) {
153 return codecInfoStr_;
154 }
155
156 // query codec info
157 std::shared_ptr<Media::AVCodecList> codecList = Media::AVCodecListFactory::CreateAVCodecList();
158 std::vector<std::shared_ptr<Media::VideoCaps>> caps = codecList->GetVideoEncoderCaps();
159 json codecTypeArray = json::array();
160
161 for (const auto &cap : caps) {
162 std::shared_ptr<Media::AVCodecInfo> codecInfo = cap->GetCodecInfo();
163 codecTypeArray.push_back(codecInfo->GetName());
164 }
165
166 codecInfoStr_ = codecTypeArray.dump();
167 return codecInfoStr_;
168 }
169
GetHardwareHandler()170 IHardwareHandler* GetHardwareHandler()
171 {
172 return &DScreenHandler::GetInstance();
173 }
174 } // namespace DistributedHardware
175 } // namespace OHOS