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 "avsession_log.h"
17 #include "avsession_errors.h"
18 #include "avsession_descriptor.h"
19 #include "avsession_dynamic_display.h"
20 #include "hw_cast_display_listener.h"
21 #include "parameter.h"
22 #include "parameters.h"
23
24 namespace OHOS {
25 namespace AVSession {
26 using namespace std;
27
28 //--------------------------------------------------------------------------
29 // Display Interface
30 //--------------------------------------------------------------------------
DisplayAdapter()31 DisplayAdapter::DisplayAdapter()
32 {
33 SLOGI("DisplayAdapter ctor");
34 }
35
GetAllCastDisplays(std::vector<CastDisplayInfo>& castDisplays)36 void DisplayAdapter::GetAllCastDisplays(std::vector<CastDisplayInfo>& castDisplays)
37 {
38 std::vector<sptr<Rosen::Screen>> allDisplays;
39 Rosen::ScreenManager::GetInstance().GetAllScreens(allDisplays);
40 std::vector<CastDisplayInfo> displays;
41 for (auto &display : allDisplays) {
42 SLOGI("GetAllCastDisplays name: %{public}s, id: %{public}llu",
43 display->GetName().c_str(), (unsigned long long)display->GetId());
44 auto flag = Rosen::ScreenManager::GetInstance().GetVirtualScreenFlag(display->GetId());
45 SLOGI("GetAllCastDisplays name: %{public}s, flag: %{public}d", display->GetName().c_str(), flag);
46 if (flag == Rosen::VirtualScreenFlag::CAST) {
47 SLOGI("ReportCastDisplay start in");
48 CastDisplayInfo castDisplayInfo;
49 castDisplayInfo.displayState = CastDisplayState::STATE_ON;
50 castDisplayInfo.displayId = display->GetId();
51 castDisplayInfo.name = display->GetName();
52 castDisplayInfo.width = static_cast<int32_t>(display->GetWidth());
53 castDisplayInfo.height = static_cast<int32_t>(display->GetHeight());
54 displays.push_back(castDisplayInfo);
55 if (displayListener_ != nullptr) {
56 displayListener_->SetDisplayInfo(display);
57 }
58 }
59 }
60 castDisplays = displays;
61 SLOGI("GetAllCastDisplaysEx castDisplays.size(): %{public}zu", castDisplays.size());
62 }
63
StartCastDisplayListener(sptr<IAVSessionCallback> callback)64 void DisplayAdapter::StartCastDisplayListener(sptr<IAVSessionCallback> callback)
65 {
66 if (displayListener_ == nullptr) {
67 SLOGI("displayListener_ is null, try to create new listener");
68 displayListener_ = new HwCastDisplayListener(callback);
69 if (displayListener_ == nullptr) {
70 SLOGI("Create displayListener failed");
71 return;
72 }
73 SLOGI("Start to register display listener");
74 Rosen::DMError ret = Rosen::ScreenManager::GetInstance().RegisterScreenListener(displayListener_);
75 if (ret != Rosen::DMError::DM_OK) {
76 SLOGE("RegisterScreenListener failed, ret: %{public}d.", ret);
77 }
78 }
79 }
80
StopCastDisplayListener()81 int32_t DisplayAdapter::StopCastDisplayListener()
82 {
83 SLOGI("StopCastDisplayListener in");
84 Rosen::DMError ret = Rosen::ScreenManager::GetInstance().UnregisterScreenListener(displayListener_);
85 if (ret != Rosen::DMError::DM_OK) {
86 SLOGE("UnregisterScreenListener failed, ret: %{public}d.", ret);
87 }
88 displayListener_ = nullptr;
89 return AVSESSION_SUCCESS;
90 }
91
createAVSessionDisplayIntf()92 extern "C" AVSessionDisplayIntf *createAVSessionDisplayIntf()
93 {
94 return new DisplayAdapter();
95 }
96
97 } // namespace AVSession
98 } // namespace OHOS