1 /*
2  * Copyright (c) 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 "core/components_ng/pattern/video/video_full_screen_pattern.h"
17 
18 #include "base/log/log_wrapper.h"
19 #include "base/utils/utils.h"
20 #include "core/components_ng/pattern/image/image_pattern.h"
21 #include "core/components_ng/pattern/video/video_full_screen_node.h"
22 
23 namespace OHOS::Ace::NG {
InitFullScreenParam(const RefPtr<VideoPattern> & videoPattern,const RefPtr<RenderSurface> & renderSurface,const RefPtr<MediaPlayer> & mediaPlayer,const RefPtr<RenderContext> & context)24 void VideoFullScreenPattern::InitFullScreenParam(const RefPtr<VideoPattern>& videoPattern,
25     const RefPtr<RenderSurface>& renderSurface, const RefPtr<MediaPlayer>& mediaPlayer,
26     const RefPtr<RenderContext>& context)
27 {
28     UpdateMediaParam(mediaPlayer, renderSurface, context);
29     videoPattern->ResetMediaParam();
30     videoPattern_ = AceType::WeakClaim(AceType::RawPtr(videoPattern));
31     RecoverState(videoPattern);
32     auto video = videoPattern->GetHost();
33     CHECK_NULL_VOID(video);
34     SetEventHub(video->GetEventHub<EventHub>());
35 }
36 
SetEventHub(const RefPtr<EventHub> & eventHub)37 void VideoFullScreenPattern::SetEventHub(const RefPtr<EventHub>& eventHub)
38 {
39     eventHub_ = eventHub;
40     originGestureEventHub_ = eventHub->GetGestureEventHub();
41     // create a new gestureEventHub and copy original event
42     auto gestureEventHub = MakeRefPtr<GestureEventHub>(eventHub);
43     gestureEventHub->CopyEvent(originGestureEventHub_);
44     gestureEventHub->CopyGestures(originGestureEventHub_);
45     eventHub_->SetGestureEventHub(gestureEventHub);
46     eventHub_->AttachHost(GetHost());
47 }
48 
RequestFullScreen(const RefPtr<VideoNode> & videoNode)49 void VideoFullScreenPattern::RequestFullScreen(const RefPtr<VideoNode>& videoNode)
50 {
51     ContainerScope scope(instanceId_);
52     auto fullScreenNode = AceType::DynamicCast<VideoFullScreenNode>(GetHost());
53     CHECK_NULL_VOID(fullScreenNode);
54     fullScreenNode->InitVideoFullScreenNode(videoNode);
55     // add node to root
56     auto host = GetHost();
57     CHECK_NULL_VOID(host);
58     auto pipelienContext = host->GetContext();
59     CHECK_NULL_VOID(pipelienContext);
60     auto rootNode = pipelienContext->GetRootElement();
61     if (!rootNode) {
62         auto videoPattern = AceType::DynamicCast<VideoPattern>(videoNode->GetPattern());
63         videoPattern->UpdateMediaParam(mediaPlayer_, renderSurface_, renderContextForMediaPlayer_);
64         ResetMediaParam();
65         return;
66     }
67     fullScreenNode->MountToParent(rootNode);
68     // set video size all window
69     LayoutConstraintF parentConstraint;
70     float rootWidth = PipelineContext::GetCurrentRootWidth();
71     float rootHeight = PipelineContext::GetCurrentRootHeight();
72     parentConstraint.maxSize.SetWidth(rootWidth);
73     parentConstraint.maxSize.SetHeight(rootHeight);
74     auto geometryNode = fullScreenNode->GetGeometryNode();
75     geometryNode->SetParentLayoutConstraint(parentConstraint);
76     geometryNode->SetMarginFrameOffset(OffsetF { 0.0f, 0.0f });
77     fullScreenNode->MarkModifyDone();
78     fullScreenNode->RebuildRenderContextTree();
79     fullScreenNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE_SELF_AND_CHILD);
80     rootNode->RebuildRenderContextTree();
81     rootNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE_SELF);
82     FocusViewShow();
83     OnFullScreenChange(true);
84 }
85 
ExitFullScreen()86 bool VideoFullScreenPattern::ExitFullScreen()
87 {
88     auto videoPattern = videoPattern_.Upgrade();
89     CHECK_NULL_RETURN(videoPattern, false);
90     videoPattern->UpdateMediaParam(mediaPlayer_, renderSurface_, renderContextForMediaPlayer_);
91     ResetMediaParam();
92     // remove full screen node
93     auto fullScreenNode = GetHost();
94     CHECK_NULL_RETURN(fullScreenNode, false);
95     auto rootNode = fullScreenNode->GetParent();
96     CHECK_NULL_RETURN(rootNode, false);
97     rootNode->RemoveChild(fullScreenNode);
98     rootNode->RebuildRenderContextTree();
99 
100     auto videoNode = AceType::DynamicCast<VideoNode>(videoPattern->GetHost());
101     CHECK_NULL_RETURN(videoNode, false);
102     // change value about time and playing status
103     videoPattern->RecoverState(AceType::Claim(this));
104     // change full screen button
105     videoPattern->OnFullScreenChange(false);
106     // recover gestureEventHub
107     eventHub_->SetGestureEventHub(originGestureEventHub_);
108     eventHub_->AttachHost(videoPattern->GetHost());
109     videoNode->MarkModifyDone();
110     FocusViewClose();
111     return true;
112 }
113 
UpdateState()114 void VideoFullScreenPattern::UpdateState()
115 {
116     auto videoPattern = videoPattern_.Upgrade();
117     CHECK_NULL_VOID(videoPattern);
118     UpdateLoop(videoPattern->GetLoop());
119     UpdateMuted(videoPattern->GetMuted());
120     UpdateAutoPlay(videoPattern->GetAutoPlay());
121     UpdateProgressRate(videoPattern->GetProgressRate());
122     UpdateAnalyzerState(videoPattern->GetAnalyzerState());
123 
124     // update full screen layout
125     auto fullScreenNode = GetHost();
126     CHECK_NULL_VOID(fullScreenNode);
127     auto fullScreenLayout = fullScreenNode->GetLayoutProperty<VideoLayoutProperty>();
128     auto videoNode = videoPattern->GetHost();
129     CHECK_NULL_VOID(videoNode);
130     auto videoLayout = videoNode->GetLayoutProperty<VideoLayoutProperty>();
131     if (videoLayout->HasObjectFit() && (fullScreenLayout->GetObjectFit() != videoLayout->GetObjectFit())) {
132         fullScreenLayout->UpdateObjectFit(videoLayout->GetObjectFit().value());
133     }
134     if (videoLayout->HasVideoSource() && (fullScreenLayout->GetVideoSource() != videoLayout->GetVideoSource())) {
135         fullScreenLayout->UpdateVideoSource(videoLayout->GetVideoSource().value());
136     }
137     if (videoLayout->HasPosterImageInfo() &&
138         (fullScreenLayout->GetPosterImageInfo() != videoLayout->GetPosterImageInfo())) {
139         fullScreenLayout->UpdatePosterImageInfo(videoLayout->GetPosterImageInfo().value());
140     }
141     if (videoLayout->HasControls() && (fullScreenLayout->GetControls() != videoLayout->GetControls())) {
142         fullScreenLayout->UpdateControls(videoLayout->GetControls().value());
143     }
144     fullScreenNode->MarkModifyDone();
145     fullScreenNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE_SELF_AND_CHILD);
146 }
147 } // namespace OHOS::Ace::NG
148