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 "core/components_ng/pattern/image/image_pattern.h"
19 #include "core/components_ng/pattern/video/video_full_screen_node.h"
20
21 namespace OHOS::Ace::NG {
InitFullScreenParam(const RefPtr<VideoPattern> & videoPattern,const RefPtr<RenderSurface> & renderSurface,const RefPtr<MediaPlayer> & mediaPlayer,const RefPtr<RenderContext> & context)22 void VideoFullScreenPattern::InitFullScreenParam(const RefPtr<VideoPattern>& videoPattern,
23 const RefPtr<RenderSurface>& renderSurface, const RefPtr<MediaPlayer>& mediaPlayer,
24 const RefPtr<RenderContext>& context)
25 {
26 UpdateMediaParam(mediaPlayer, renderSurface, context);
27 videoPattern->ResetMediaParam();
28 videoPattern_ = AceType::WeakClaim(AceType::RawPtr(videoPattern));
29 RecoverState(videoPattern);
30 auto video = videoPattern->GetHost();
31 CHECK_NULL_VOID(video);
32 SetEventHub(video->GetEventHub<EventHub>());
33 }
34
RequestFullScreen(const RefPtr<VideoNode> & videoNode)35 void VideoFullScreenPattern::RequestFullScreen(const RefPtr<VideoNode>& videoNode)
36 {
37 auto fullScreenNode = AceType::DynamicCast<VideoFullScreenNode>(GetHost());
38 CHECK_NULL_VOID(fullScreenNode);
39 fullScreenNode->InitVideoFullScreenNode(videoNode);
40 // add node to root
41 auto rootNode = PipelineContext::GetCurrentContext()->GetRootElement();
42 if (!rootNode) {
43 LOGI("rootNode is nullptr");
44 auto videoPattern = AceType::DynamicCast<VideoPattern>(videoNode->GetPattern());
45 videoPattern->UpdateMediaParam(mediaPlayer_, renderSurface_, renderContextForMediaPlayer_);
46 ResetMediaParam();
47 return;
48 }
49 fullScreenNode->MountToParent(rootNode);
50 // set video size all window
51 LayoutConstraintF parentConstraint;
52 float rootWidth = PipelineContext::GetCurrentRootWidth();
53 float rootHeight = PipelineContext::GetCurrentRootHeight();
54 parentConstraint.maxSize.SetWidth(rootWidth);
55 parentConstraint.maxSize.SetHeight(rootHeight);
56 auto geometryNode = fullScreenNode->GetGeometryNode();
57 geometryNode->SetParentLayoutConstraint(parentConstraint);
58 geometryNode->SetMarginFrameOffset(OffsetF { 0.0f, 0.0f });
59 fullScreenNode->MarkModifyDone();
60 fullScreenNode->RebuildRenderContextTree();
61 fullScreenNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE_SELF_AND_CHILD);
62 rootNode->RebuildRenderContextTree();
63 rootNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE_SELF);
64 OnFullScreenChange(true);
65 }
66
ExitFullScreen()67 void VideoFullScreenPattern::ExitFullScreen()
68 {
69 auto videoPattern = videoPattern_.Upgrade();
70 CHECK_NULL_VOID(videoPattern);
71 videoPattern->UpdateMediaParam(mediaPlayer_, renderSurface_, renderContextForMediaPlayer_);
72 ResetMediaParam();
73 // remove full screen node
74 auto fullScreenNode = GetHost();
75 CHECK_NULL_VOID(fullScreenNode);
76 auto rootNode = fullScreenNode->GetParent();
77 CHECK_NULL_VOID(rootNode);
78 rootNode->RemoveChild(fullScreenNode);
79 rootNode->RebuildRenderContextTree();
80
81 auto videoNode = AceType::DynamicCast<VideoNode>(videoPattern->GetHost());
82 CHECK_NULL_VOID(videoNode);
83 // change value about time and playing status
84 videoPattern->RecoverState(AceType::Claim(this));
85 // change full screen button
86 videoPattern->OnFullScreenChange(false);
87 videoNode->MarkModifyDone();
88 }
89
UpdateState()90 void VideoFullScreenPattern::UpdateState()
91 {
92 auto videoPattern = videoPattern_.Upgrade();
93 CHECK_NULL_VOID(videoPattern);
94 UpdateLoop(videoPattern->GetLoop());
95 UpdateMuted(videoPattern->GetMuted());
96 UpdateAutoPlay(videoPattern->GetAutoPlay());
97 UpdateProgressRate(videoPattern->GetProgressRate());
98
99 // update full screen layout
100 auto fullScreenNode = GetHost();
101 CHECK_NULL_VOID(fullScreenNode);
102 auto fullScreenLayout = fullScreenNode->GetLayoutProperty<VideoLayoutProperty>();
103 auto videoNode = videoPattern->GetHost();
104 CHECK_NULL_VOID(videoNode);
105 auto videoLayout = videoNode->GetLayoutProperty<VideoLayoutProperty>();
106 if (videoLayout->HasObjectFit() && (fullScreenLayout->GetObjectFit() != videoLayout->GetObjectFit())) {
107 fullScreenLayout->UpdateObjectFit(videoLayout->GetObjectFit().value());
108 }
109 if (videoLayout->HasVideoSource() && (fullScreenLayout->GetVideoSource() != videoLayout->GetVideoSource())) {
110 fullScreenLayout->UpdateVideoSource(videoLayout->GetVideoSource().value());
111 }
112 if (videoLayout->HasPosterImageInfo() &&
113 (fullScreenLayout->GetPosterImageInfo() != videoLayout->GetPosterImageInfo())) {
114 fullScreenLayout->UpdatePosterImageInfo(videoLayout->GetPosterImageInfo().value());
115 }
116 if (videoLayout->HasControls() && (fullScreenLayout->GetControls() != videoLayout->GetControls())) {
117 fullScreenLayout->UpdateControls(videoLayout->GetControls().value());
118 }
119 fullScreenNode->MarkModifyDone();
120 fullScreenNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE_SELF_AND_CHILD);
121 }
122 } // namespace OHOS::Ace::NG
123