• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/video/resource/ext_surface.h"
17 
18 #include <sstream>
19 
20 #include "base/log/log.h"
21 #include "base/memory/referenced.h"
22 #include "base/utils/utils.h"
23 
24 namespace OHOS::Ace {
25 
26 const char SURFACE_ERRORCODE_CREATEFAIL[] = "error_video_000001";
27 const char SURFACE_ERRORMSG_CREATEFAIL[] = "Unable to initialize video player.";
28 
29 const char SURFACE_METHOD_ONCREATE[] = "onCreate";
30 const char SURFACE_METHOD_ONCHANGED[] = "onChanged";
31 
32 const char SET_SURFACE_BOUNDS[] = "setSurfaceBounds";
33 const char SURFACE_ID[] = "surfaceId";
34 const char SURFACE_LEFT[] = "surfaceLeft";
35 const char SURFACE_TOP[] = "surfaceTop";
36 const char SURFACE_HEIGHT[] = "surfaceHeight";
37 const char SURFACE_WIDTH[] = "surfaceWidth";
38 const char SET_IS_FULLSCREEN[] = "setIsFullScreen";
39 const char IS_FULLSCREEN[] = "isFullScreen";
40 
~ExtSurface()41 ExtSurface::~ExtSurface()
42 {
43     auto context = context_.Upgrade();
44     CHECK_NULL_VOID(context);
45     auto resRegister = context->GetPlatformResRegister();
46     CHECK_NULL_VOID(resRegister);
47     auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
48     if (platformTaskExecutor.IsRunOnCurrentThread()) {
49         resRegister->UnregisterEvent(MakeEventHash(SURFACE_METHOD_ONCREATE));
50     } else {
51         WeakPtr<PlatformResRegister> weak = resRegister;
52         platformTaskExecutor.PostTask([eventHash = MakeEventHash(SURFACE_METHOD_ONCREATE), weak] {
53             auto resRegister = weak.Upgrade();
54             CHECK_NULL_VOID(resRegister);
55             resRegister->UnregisterEvent(eventHash);
56         });
57     }
58 }
59 
Create(const std::function<void (int64_t)> & onCreate)60 void ExtSurface::Create(const std::function<void(int64_t)>& onCreate)
61 {
62     auto context = context_.Upgrade();
63     CHECK_NULL_VOID(context);
64 
65     auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
66     platformTaskExecutor.PostTask([weak = WeakClaim(this), onCreate] {
67         auto surface = weak.Upgrade();
68         if (surface) {
69             surface->CreateExtSurface(onCreate);
70         }
71     });
72 }
73 
CreateExtSurface(const std::function<void (int64_t)> & onCreate)74 void ExtSurface::CreateExtSurface(const std::function<void(int64_t)>& onCreate)
75 {
76     auto context = context_.Upgrade();
77     CHECK_NULL_VOID(context);
78     auto resRegister = context->GetPlatformResRegister();
79     CHECK_NULL_VOID(resRegister);
80     id_ = resRegister->CreateResource(type_, PARAM_NONE);
81     if (id_ == INVALID_ID) {
82         if (onError_) {
83             onError_(SURFACE_ERRORCODE_CREATEFAIL, SURFACE_ERRORMSG_CREATEFAIL);
84         }
85         return;
86     }
87     hash_ = MakeResourceHash();
88     resRegister->RegisterEvent(
89         MakeEventHash(SURFACE_METHOD_ONCREATE), [weak = WeakClaim(this)](const std::string& param) {
90             auto surface = weak.Upgrade();
91             if (surface) {
92                 surface->OnSurfaceCreated();
93             }
94         });
95 
96     resRegister->RegisterEvent(
97         MakeEventHash(SURFACE_METHOD_ONCHANGED), [weak = WeakClaim(this)](const std::string& param) {
98             auto surface = weak.Upgrade();
99             if (surface) {
100                 auto width = surface->GetIntParam(param, SURFACE_WIDTH);
101                 auto height = surface->GetIntParam(param, SURFACE_HEIGHT);
102                 surface->OnSurfaceChanged(width, height);
103             }
104         });
105 
106     if (onCreate) {
107         onCreate(id_);
108     }
109 #if defined(IOS_PLATFORM)
110     OnSurfaceCreated();
111 #endif
112 }
113 
SetBounds(int64_t surfaceId,int32_t left,int32_t top,int32_t width,int32_t height)114 void ExtSurface::SetBounds(int64_t surfaceId, int32_t left, int32_t top, int32_t width, int32_t height)
115 {
116     std::stringstream paramStream;
117     paramStream << SURFACE_ID << PARAM_EQUALS << surfaceId << PARAM_AND << SURFACE_LEFT << PARAM_EQUALS << left
118                 << PARAM_AND << SURFACE_TOP << PARAM_EQUALS << top << PARAM_AND << SURFACE_WIDTH << PARAM_EQUALS
119                 << width << PARAM_AND << SURFACE_HEIGHT << PARAM_EQUALS << height;
120     std::string param = paramStream.str();
121     CallResRegisterMethod(MakeMethodHash(SET_SURFACE_BOUNDS), param);
122 }
123 
SetIsFullScreen(bool isFullScreen)124 void ExtSurface::SetIsFullScreen(bool isFullScreen)
125 {
126     std::stringstream paramStream;
127     paramStream << IS_FULLSCREEN << PARAM_EQUALS << isFullScreen;
128     std::string param = paramStream.str();
129     CallResRegisterMethod(MakeMethodHash(SET_IS_FULLSCREEN), param);
130 }
131 
OnSurfaceCreated()132 void ExtSurface::OnSurfaceCreated()
133 {
134     if (onSurfaceCreated_) {
135         onSurfaceCreated_();
136     }
137 }
138 
OnSurfaceChanged(int32_t width,int32_t height)139 void ExtSurface::OnSurfaceChanged(int32_t width, int32_t height)
140 {
141     LOGI("OnSurfaceChanged. width: %{public}d height: %{public}d", width, height);
142     if (onSurfaceChanged_) {
143         onSurfaceChanged_(width, height);
144     }
145 }
146 
147 } // namespace OHOS::Ace