1 /*
2  * Copyright (c) 2024 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 "hw_cast_display_listener.h"
17 #include "avsession_log.h"
18 
19 namespace OHOS::AVSession {
20 // LCOV_EXCL_START
OnConnect(Rosen::DisplayId displayId)21 void HwCastDisplayListener::OnConnect(Rosen::DisplayId displayId)
22 {
23     SLOGI("Screen OnConnect");
24     auto display = Rosen::DisplayManagerLite::GetInstance().GetDisplayById(displayId);
25     CHECK_AND_RETURN_LOG(display != nullptr, "display is nullptr");
26     auto displayInfo = display->GetDisplayInfo();
27     CHECK_AND_RETURN_LOG(displayInfo != nullptr, "displayInfo is nullptr");
28     auto displayName = displayInfo->GetName();
29     SLOGI("DisplayId OnConnect: %{public}s", displayName.c_str());
30     auto flag = Rosen::DisplayManagerLite::GetInstance().GetVirtualScreenFlag(displayId);
31     if (flag == Rosen::VirtualScreenFlag::CAST) {
32         ReportCastDisplay(displayInfo, CastDisplayState::STATE_ON);
33         SetDisplayInfo(displayInfo);
34     }
35 }
36 // LCOV_EXCL_STOP
37 
OnDisconnect(Rosen::DisplayId displayId)38 void HwCastDisplayListener::OnDisconnect(Rosen::DisplayId displayId)
39 {
40     SLOGI("OnDisconnect in");
41     auto curDisplayInfo = GetDisplayInfo();
42     if (!curDisplayInfo || curDisplayInfo->GetDisplayId() != displayId) {
43         SLOGE("curDisplayInfo_ is null");
44         return;
45     }
46     auto displayName = curDisplayInfo->GetName();
47     SLOGI("DisplayId OnDisconnect: %{public}s", displayName.c_str());
48     ReportCastDisplay(curDisplayInfo, CastDisplayState::STATE_OFF);
49     SetDisplayInfo(nullptr);
50 }
51 
OnChange(Rosen::DisplayId displayId)52 void HwCastDisplayListener::OnChange(Rosen::DisplayId displayId) {}
53 
54 // LCOV_EXCL_START
SetDisplayInfo(sptr<Rosen::DisplayInfo> displayInfo)55 void HwCastDisplayListener::SetDisplayInfo(sptr<Rosen::DisplayInfo> displayInfo)
56 {
57     std::lock_guard<std::mutex> lock(dataMutex_);
58     curDisplayInfo_ = displayInfo;
59 }
60 // LCOV_EXCL_STOP
61 
GetDisplayInfo()62 sptr<Rosen::DisplayInfo> HwCastDisplayListener::GetDisplayInfo()
63 {
64     std::lock_guard<std::mutex> lock(dataMutex_);
65     return curDisplayInfo_;
66 }
67 
68 // LCOV_EXCL_START
ReportCastDisplay(sptr<Rosen::DisplayInfo> displayInfo,CastDisplayState displayState)69 void HwCastDisplayListener::ReportCastDisplay(sptr<Rosen::DisplayInfo> displayInfo, CastDisplayState displayState)
70 {
71     SLOGI("Screen ReportCastDisplay");
72     CastDisplayInfo castDisplayInfo;
73     castDisplayInfo.displayState = displayState;
74     castDisplayInfo.displayId = displayInfo->GetDisplayId();
75     castDisplayInfo.name = displayInfo->GetName();
76     castDisplayInfo.width = static_cast<int32_t>(displayInfo->GetWidth());
77     castDisplayInfo.height = static_cast<int32_t>(displayInfo->GetHeight());
78     listener_->OnCastDisplayChange(castDisplayInfo);
79 }
80 // LCOV_EXCL_STOP
81 } // namespace OHOS::AVSession
82