• 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 "1.0/include/dscreen.h"
17 
18 #include "avcodec_info.h"
19 #include "avcodec_list.h"
20 #include <pthread.h>
21 
22 #include "dscreen_constants.h"
23 #include "dscreen_errcode.h"
24 #include "dscreen_hisysevent.h"
25 #include "dscreen_json_util.h"
26 #include "dscreen_log.h"
27 #include "dscreen_util.h"
28 #include "common/include/screen_manager_adapter.h"
29 #include "screen_source_trans.h"
30 
31 namespace OHOS {
32 namespace DistributedHardware {
33 namespace V1_0 {
34 constexpr const char* TASK_THREAD = "TaskThread";
DScreen(const std::string & devId,const std::string & dhId,std::shared_ptr<IDScreenCallback> dscreenCallback)35 DScreen::DScreen(const std::string &devId, const std::string &dhId,
36     std::shared_ptr<IDScreenCallback> dscreenCallback)
37 {
38     DHLOGD("DScreen construct, devId: %s, dhId: %s", GetAnonyString(devId).c_str(),
39         GetAnonyString(dhId).c_str());
40     devId_ = devId;
41     dhId_ = dhId;
42     dscreenCallback_ = dscreenCallback;
43     SetState(DISABLED);
44     taskThreadRunning_ = true;
45     taskQueueThread_ = std::thread(&DScreen::TaskThreadLoop, this);
46 }
47 
~DScreen()48 DScreen::~DScreen()
49 {
50     DHLOGD("DScreen deconstruct, devId: %s, dhId: %s", GetAnonyString(devId_).c_str(),
51         GetAnonyString(dhId_).c_str());
52     taskThreadRunning_ = false;
53     taskQueueCond_.notify_all();
54     if (taskQueueThread_.joinable()) {
55         taskQueueThread_.join();
56     }
57     int32_t ret = DH_SUCCESS;
58     if (sourceTrans_ != nullptr) {
59         ret = sourceTrans_->Release();
60     }
61     if (ret != DH_SUCCESS) {
62         DHLOGE("source trans release failed. ret: %" PRId32, ret);
63     }
64 
65     if (screenId_ != SCREEN_ID_INVALID) {
66         ret = ScreenMgrAdapter::GetInstance().RemoveVirtualScreen(screenId_);
67     }
68 
69     if (ret != DH_SUCCESS) {
70         DHLOGE("remove virtual screen failed.");
71     }
72     videoParam_ = nullptr;
73     sourceTrans_ = nullptr;
74     DHLOGD("DScreen deconstruct end.");
75 }
76 
OnTransError(int32_t err,const std::string & content)77 void DScreen::OnTransError(int32_t err, const std::string &content)
78 {
79     DHLOGD("OnTransError, err: %" PRId32, err);
80     AddTask(std::make_shared<Task>(TaskType::TASK_DISCONNECT, ""));
81     ScreenMgrAdapter::GetInstance().RemoveScreenFromGroup(screenId_);
82 }
83 
SetVideoParam(std::shared_ptr<VideoParam> & videoParam)84 void DScreen::SetVideoParam(std::shared_ptr<VideoParam> &videoParam)
85 {
86     videoParam_ = videoParam;
87 }
88 
GetVideoParam()89 std::shared_ptr<VideoParam> DScreen::GetVideoParam()
90 {
91     return videoParam_;
92 }
93 
SetState(DScreenState state)94 void DScreen::SetState(DScreenState state)
95 {
96     std::lock_guard<std::mutex> lock(stateMtx_);
97     curState_ = state;
98 }
99 
GetState() const100 DScreenState DScreen::GetState() const
101 {
102     return curState_;
103 }
104 
GetScreenId() const105 uint64_t DScreen::GetScreenId() const
106 {
107     return screenId_;
108 }
109 
SetScreenVersion(const std::string & version)110 void DScreen::SetScreenVersion(const std::string &version)
111 {
112     version_ = version;
113 }
114 
GetScreenVersion()115 std::string DScreen::GetScreenVersion()
116 {
117     return version_;
118 }
119 
GetDHId() const120 std::string DScreen::GetDHId() const
121 {
122     return dhId_;
123 }
124 
GetDevId() const125 std::string DScreen::GetDevId() const
126 {
127     return devId_;
128 }
129 
AddTask(const std::shared_ptr<Task> & task)130 int32_t DScreen::AddTask(const std::shared_ptr<Task> &task)
131 {
132     DHLOGI("DScreen::AddTask, devId: %s, dhId: %s", GetAnonyString(devId_).c_str(),
133         GetAnonyString(dhId_).c_str());
134     if (task == nullptr) {
135         DHLOGE("AddTask, task is invalid.");
136         return ERR_DH_SCREEN_SA_DSCREEN_TASK_NOT_VALID;
137     }
138     DHLOGI("AddTask, task type: %" PRId32, task->GetTaskType());
139     {
140         std::lock_guard<std::mutex> lock(taskQueueMtx_);
141         taskQueue_.push(task);
142     }
143     taskQueueCond_.notify_all();
144     return DH_SUCCESS;
145 }
146 
TaskThreadLoop()147 void DScreen::TaskThreadLoop()
148 {
149     DHLOGI("DScreen taskThread start. devId: %s, dhId: %s", GetAnonyString(devId_).c_str(),
150         GetAnonyString(dhId_).c_str());
151     int32_t ret = pthread_setname_np(pthread_self(), TASK_THREAD);
152     if (ret != DH_SUCCESS) {
153         DHLOGE("Dscreen set thread name failed, ret %" PRId32, ret);
154     }
155     while (taskThreadRunning_) {
156         std::shared_ptr<Task> task;
157         {
158             std::unique_lock<std::mutex> lock(taskQueueMtx_);
159             taskQueueCond_.wait_for(lock, std::chrono::seconds(TASK_WAIT_SECONDS),
160                 [this]() { return !taskQueue_.empty(); });
161             if (taskQueue_.empty()) {
162                 continue;
163             }
164             task = taskQueue_.front();
165             taskQueue_.pop();
166         }
167 
168         if (task == nullptr) {
169             DHLOGD("task is null.");
170             continue;
171         }
172 
173         DHLOGD("run task, task queue size: %zu", taskQueue_.size());
174         HandleTask(task);
175     }
176 }
177 
HandleTask(const std::shared_ptr<Task> & task)178 void DScreen::HandleTask(const std::shared_ptr<Task> &task)
179 {
180     int32_t taskType = task->GetTaskType();
181     DHLOGI("HandleTask, devId: %s, dhId: %s, task type: %" PRId32, GetAnonyString(devId_).c_str(),
182         GetAnonyString(dhId_).c_str(), taskType);
183     switch (taskType) {
184         case TaskType::TASK_ENABLE:
185             HandleEnable(task->GetTaskParam(), task->GetTaskId());
186             break;
187         case TaskType::TASK_DISABLE:
188             HandleDisable(task->GetTaskId());
189             break;
190         case TaskType::TASK_CONNECT:
191             HandleConnect();
192             break;
193         case TaskType::TASK_DISCONNECT:
194             HandleDisconnect();
195             break;
196         default:
197             DHLOGD("task type unkown.");
198     }
199 }
200 
HandleEnable(const std::string & param,const std::string & taskId)201 void DScreen::HandleEnable(const std::string &param, const std::string &taskId)
202 {
203     json attrJson = json::parse(param, nullptr, false);
204     if (attrJson.is_discarded()) {
205         DHLOGE("HandleEnable attrJson is invalid");
206         return;
207     }
208     if (dscreenCallback_ == nullptr) {
209         DHLOGE("DScreen::HandleEnable, dscreenCallback_ is nullptr");
210         return;
211     }
212     DHLOGI("HandleEnable, devId: %s, dhId: %s", GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str());
213     if (curState_ == ENABLED || curState_ == ENABLING || curState_ == CONNECTING || curState_ == CONNECTED) {
214         dscreenCallback_->OnRegResult(shared_from_this(), taskId, DH_SUCCESS, "dscreen enable success.");
215         return;
216     }
217     SetState(ENABLING);
218     if (videoParam_ == nullptr) {
219         videoParam_ = std::make_shared<VideoParam>();
220     }
221 
222     int32_t ret = CheckJsonData(attrJson);
223     if (ret != DH_SUCCESS) {
224         dscreenCallback_->OnRegResult(shared_from_this(), taskId, ERR_DH_SCREEN_SA_ENABLE_FAILED,
225             "enable param json is invalid.");
226         ReportRegisterFail(DSCREEN_REGISTER_FAIL, ERR_DH_SCREEN_SA_ENABLE_FAILED, GetAnonyString(devId_).c_str(),
227             GetAnonyString(dhId_).c_str(), "check json data failed.");
228         return;
229     }
230 
231     videoParam_->SetScreenWidth(attrJson[KEY_SCREEN_WIDTH].get<uint32_t>());
232     videoParam_->SetScreenHeight(attrJson[KEY_SCREEN_HEIGHT].get<uint32_t>());
233 
234     // negotiate codecType
235     ret = NegotiateCodecType(attrJson[KEY_CODECTYPE]);
236     if (ret != DH_SUCCESS) {
237         dscreenCallback_->OnRegResult(shared_from_this(), taskId, ERR_DH_SCREEN_SA_ENABLE_FAILED,
238             "negotiate codec type failed.");
239         ReportRegisterFail(DSCREEN_REGISTER_FAIL, ERR_DH_SCREEN_SA_ENABLE_FAILED, GetAnonyString(devId_).c_str(),
240             GetAnonyString(dhId_).c_str(), "negotiate codec type failed.");
241         return;
242     }
243 
244     screenId_ = ScreenMgrAdapter::GetInstance().CreateVirtualScreen(devId_, dhId_, videoParam_);
245     if (screenId_ == SCREEN_ID_INVALID) {
246         dscreenCallback_->OnRegResult(shared_from_this(), taskId, ERR_DH_SCREEN_SA_ENABLE_FAILED,
247             "create virtual screen failed.");
248         ReportRegisterFail(DSCREEN_REGISTER_FAIL, ERR_DH_SCREEN_SA_ENABLE_FAILED, GetAnonyString(devId_).c_str(),
249             GetAnonyString(dhId_).c_str(), "create virtual screen failed.");
250         return;
251     }
252     SetState(ENABLED);
253     dscreenCallback_->OnRegResult(shared_from_this(), taskId, DH_SUCCESS, "dscreen enable success.");
254     ReportRegisterScreenEvent(DSCREEN_REGISTER, GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(),
255         "dscreen enable success.");
256 }
257 
CheckJsonData(json & attrJson)258 int32_t DScreen::CheckJsonData(json &attrJson)
259 {
260     if (attrJson.is_discarded()) {
261         DHLOGE("enable param json is invalid.");
262         return ERR_DH_SCREEN_SA_ENABLE_JSON_ERROR;
263     }
264 
265     if (!IsUInt32(attrJson, KEY_SCREEN_WIDTH) || !IsUInt32(attrJson, KEY_SCREEN_HEIGHT) ||
266         !attrJson.contains(KEY_CODECTYPE)) {
267         DHLOGE("enable param is invalid.");
268         return ERR_DH_SCREEN_SA_ENABLE_JSON_ERROR;
269     }
270     return DH_SUCCESS;
271 }
272 
NegotiateCodecType(const std::string & remoteCodecInfoStr)273 int32_t DScreen::NegotiateCodecType(const std::string &remoteCodecInfoStr)
274 {
275     json remoteCodecArray = json::parse(remoteCodecInfoStr, nullptr, false);
276     if (remoteCodecArray.is_discarded() || !remoteCodecArray.is_array()) {
277         DHLOGE("remoteCodecInfoStrjson is invalid.");
278         return ERR_DH_SCREEN_SA_DSCREEN_NEGOTIATE_CODEC_FAIL;
279     }
280 
281     std::vector<std::string> localCodecArray;
282     // query local support encoder type
283     std::shared_ptr<Media::AVCodecList> codecList = Media::AVCodecListFactory::CreateAVCodecList();
284     if (codecList == nullptr) {
285         DHLOGE("codecList is nullptr.");
286         return ERR_DH_SCREEN_SA_DSCREEN_NEGOTIATE_CODEC_FAIL;
287     }
288     std::vector<std::shared_ptr<Media::VideoCaps>> caps = codecList->GetVideoEncoderCaps();
289     for (const auto &cap : caps) {
290         if (cap == nullptr) {
291             continue;
292         }
293         std::shared_ptr<Media::AVCodecInfo> codecInfo = cap->GetCodecInfo();
294         if (codecInfo == nullptr) {
295             continue;
296         }
297         localCodecArray.push_back(codecInfo->GetName());
298     }
299     std::vector<std::string> codecTypeCandidates;
300     for (const auto &remoteCodecType : remoteCodecArray) {
301         if (std::find(localCodecArray.begin(), localCodecArray.end(),
302             remoteCodecType) != localCodecArray.end()) {
303             codecTypeCandidates.push_back(remoteCodecType);
304         }
305     }
306 
307     if (std::find(codecTypeCandidates.begin(), codecTypeCandidates.end(),
308         CODEC_NAME_H264) != codecTypeCandidates.end()) {
309         videoParam_->SetCodecType(VIDEO_CODEC_TYPE_VIDEO_H264);
310         videoParam_->SetVideoFormat(VIDEO_DATA_FORMAT_NV12);
311         videoParam_->SetPartialRefreshFlag(true);
312     } else if (std::find(codecTypeCandidates.begin(), codecTypeCandidates.end(),
313         CODEC_NAME_MPEG4) != codecTypeCandidates.end()) {
314         videoParam_->SetCodecType(VIDEO_CODEC_TYPE_VIDEO_MPEG4);
315         videoParam_->SetVideoFormat(VIDEO_DATA_FORMAT_RGBA8888);
316     } else if (std::find(codecTypeCandidates.begin(), codecTypeCandidates.end(),
317         CODEC_NAME_H265) != codecTypeCandidates.end()) {
318         videoParam_->SetCodecType(VIDEO_CODEC_TYPE_VIDEO_H265);
319         videoParam_->SetVideoFormat(VIDEO_DATA_FORMAT_NV12);
320         videoParam_->SetPartialRefreshFlag(true);
321     } else {
322         DHLOGI("codec type not support.");
323         return ERR_DH_SCREEN_SA_DSCREEN_NEGOTIATE_CODEC_FAIL;
324     }
325     return DH_SUCCESS;
326 }
327 
HandleDisable(const std::string & taskId)328 void DScreen::HandleDisable(const std::string &taskId)
329 {
330     if (dscreenCallback_ == nullptr) {
331         DHLOGE("DScreen::HandleDisable, dscreenCallback_ is nullptr");
332         return;
333     }
334     DHLOGI("HandleDisable, devId: %s, dhId: %s", GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str());
335     SetState(DISABLING);
336     int32_t ret = ScreenMgrAdapter::GetInstance().RemoveVirtualScreen(screenId_);
337     if (ret != DH_SUCCESS) {
338         DHLOGE("remove virtual screen failed.");
339         dscreenCallback_->OnUnregResult(shared_from_this(), taskId, ERR_DH_SCREEN_SA_DISABLE_FAILED,
340             "remove virtual screen failed.");
341         ReportUnRegisterFail(DSCREEN_UNREGISTER_FAIL, ERR_DH_SCREEN_SA_DISABLE_FAILED, GetAnonyString(devId_).c_str(),
342             GetAnonyString(dhId_).c_str(), "remove virtual screen failed.");
343         return;
344     }
345     SetState(DISABLED);
346     dscreenCallback_->OnUnregResult(shared_from_this(), taskId, DH_SUCCESS, "");
347     ReportUnRegisterScreenEvent(DSCREEN_UNREGISTER, GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(),
348         "dscreen disable success.");
349 }
350 
HandleConnect()351 void DScreen::HandleConnect()
352 {
353     DHLOGI("HandleConnect, devId: %s, dhId: %s", GetAnonyString(devId_).c_str(),
354         GetAnonyString(dhId_).c_str());
355 
356     int32_t ret = SetUp();
357     if (ret != DH_SUCCESS) {
358         SetState(ENABLED);
359         ScreenMgrAdapter::GetInstance().RemoveScreenFromGroup(screenId_);
360         DHLOGE("dScreen SetUp failed.");
361         return;
362     }
363 
364     ret = Start();
365     if (ret != DH_SUCCESS) {
366         SetState(ENABLED);
367         ScreenMgrAdapter::GetInstance().RemoveScreenFromGroup(screenId_);
368         DHLOGE("dScreen Start failed.");
369         return;
370     }
371     SetState(CONNECTED);
372     ReportScreenMirrorEvent(DSCREEN_PROJECT_START, GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(),
373         "dscreen connect success");
374 }
375 
HandleDisconnect()376 void DScreen::HandleDisconnect()
377 {
378     DHLOGD("HandleDisconnect, devId: %s, dhId: %s", GetAnonyString(devId_).c_str(),
379         GetAnonyString(dhId_).c_str());
380     if (curState_ != CONNECTED) {
381         DHLOGE("dscreen is not connected, cannot disconnect");
382         return;
383     }
384     SetState(DISCONNECTING);
385     int32_t ret = Stop();
386     if (ret != DH_SUCCESS) {
387         SetState(CONNECTED);
388         DHLOGE("dScreen Stop failed.");
389         return;
390     }
391     SetState(ENABLED);
392     Rosen::RSInterfaces::GetInstance().SetVirtualScreenUsingStatus(false);
393     ReportScreenMirrorEvent(DSCREEN_PROJECT_END, GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(),
394         "dscreen disconnect success");
395 }
396 
SetUp()397 int32_t DScreen::SetUp()
398 {
399     DHLOGD("SetUp, devId: %s, dhId: %s", GetAnonyString(devId_).c_str(),
400         GetAnonyString(dhId_).c_str());
401 
402     if (sourceTrans_ == nullptr) {
403         sourceTrans_ = std::make_shared<ScreenSourceTrans>();
404     }
405     sourceTrans_->SetScreenVersion(version_);
406     sourceTrans_->RegisterStateCallback(shared_from_this());
407     int32_t ret = sourceTrans_->SetUp(*videoParam_, *videoParam_, devId_);
408     if (ret != DH_SUCCESS) {
409         DHLOGE("source trans SetUp failed.");
410         return ret;
411     }
412     DHLOGI("DScreen SetUp success.");
413     return DH_SUCCESS;
414 }
415 
Start()416 int32_t DScreen::Start()
417 {
418     DHLOGD("Start, devId: %s, dhId: %s", GetAnonyString(devId_).c_str(),
419         GetAnonyString(dhId_).c_str());
420     if (sourceTrans_ == nullptr) {
421         DHLOGE("source trans not init.");
422         return ERR_DH_SCREEN_SA_SOURCETRANS_NOT_INIT;
423     }
424     int32_t ret = sourceTrans_->Start();
425     if (ret != DH_SUCCESS) {
426         DHLOGE("source trans start failed.");
427         return ret;
428     }
429     sptr<OHOS::Surface> windowSurface = sourceTrans_->GetImageSurface();
430     if (windowSurface == nullptr) {
431         DHLOGE("DScreen SetUp failed.");
432         return ERR_DH_SCREEN_SA_DSCREEN_SETUP_FAILED;
433     }
434     ScreenMgrAdapter::GetInstance().SetImageSurface(screenId_, windowSurface);
435     return DH_SUCCESS;
436 }
437 
Stop()438 int32_t DScreen::Stop()
439 {
440     DHLOGD("Stop, devId: %s, dhId: %s", GetAnonyString(devId_).c_str(),
441         GetAnonyString(dhId_).c_str());
442     if (sourceTrans_ == nullptr) {
443         DHLOGE("source trans not init.");
444         return ERR_DH_SCREEN_SA_SOURCETRANS_NOT_INIT;
445     }
446 
447     int32_t ret = sourceTrans_->Stop();
448     if (ret != DH_SUCCESS) {
449         DHLOGE("source trans stop failed.");
450         return ret;
451     }
452 
453     ret = sourceTrans_->Release();
454     if (ret != DH_SUCCESS) {
455         DHLOGE("source trans release failed.");
456         return ret;
457     }
458 
459     sourceTrans_ = nullptr;
460     return DH_SUCCESS;
461 }
462 } // namespace V1_0
463 } // namespace DistributedHardware
464 } // namespace OHOS