• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 namespace OHOS::Ace {
19 
20 const char SURFACE_ERRORCODE_CREATEFAIL[] = "error_video_000001";
21 const char SURFACE_ERRORMSG_CREATEFAIL[] = "Unable to initialize video player.";
22 
23 const char SURFACE_METHOD_ONCREATE[] = "onCreate";
24 const char SURFACE_METHOD_ONCHANGED[] = "onChanged";
25 const char SURFACE_METHOD_ONDESTROYED[] = "onDestroyed";
26 
27 const char SET_SURFACE_BOUNDS[] = "setSurfaceBounds";
28 const char SURFACE_ID[] = "surfaceId";
29 const char SURFACE_LEFT[] = "surfaceLeft";
30 const char SURFACE_TOP[] = "surfaceTop";
31 const char SURFACE_HEIGHT[] = "surfaceHeight";
32 const char SURFACE_WIDTH[] = "surfaceWidth";
33 const char SET_IS_FULLSCREEN[] = "setIsFullScreen";
34 const char IS_FULLSCREEN[] = "isFullScreen";
35 const char ATTACH_NATIVE_WINDOW[] = "attachNativeWindow";
36 const char NATIVE_WINDOW[] = "nativeWindow";
37 #ifdef RENDER_EXTRACT_SUPPORTED
38 const char SET_SURFACE_ROTATION[] = "setSurfaceRotation";
39 const char IS_LOCK[] = "isLock";
40 const char SET_SURFACE_RECT[] = "setSurfaceRect";
41 #endif
42 
~ExtSurface()43 ExtSurface::~ExtSurface()
44 {
45     auto context = context_.Upgrade();
46     CHECK_NULL_VOID(context);
47     auto resRegister = context->GetPlatformResRegister();
48     CHECK_NULL_VOID(resRegister);
49     auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
50     if (platformTaskExecutor.IsRunOnCurrentThread()) {
51         resRegister->UnregisterEvent(MakeEventHash(SURFACE_METHOD_ONCREATE));
52     } else {
53         WeakPtr<PlatformResRegister> weak = resRegister;
54         platformTaskExecutor.PostTask(
55             [eventHash = MakeEventHash(SURFACE_METHOD_ONCREATE), weak] {
56                 auto resRegister = weak.Upgrade();
57                 CHECK_NULL_VOID(resRegister);
58                 resRegister->UnregisterEvent(eventHash);
59             },
60             "ArkUIVideoExtSurfaceUnregisterEvent");
61     }
62 }
63 
Create(const std::function<void (int64_t)> & onCreate)64 void ExtSurface::Create(const std::function<void(int64_t)>& onCreate)
65 {
66     auto context = context_.Upgrade();
67     CHECK_NULL_VOID(context);
68 
69     auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
70     platformTaskExecutor.PostTask(
71         [weak = WeakClaim(this), onCreate] {
72             auto surface = weak.Upgrade();
73             if (surface) {
74                 surface->CreateExtSurface(onCreate);
75             }
76         },
77         "ArkUIVideoCreateExtSurface");
78 }
79 
CreateExtSurface(const std::function<void (int64_t)> & onCreate)80 void ExtSurface::CreateExtSurface(const std::function<void(int64_t)>& onCreate)
81 {
82     auto context = context_.Upgrade();
83     CHECK_NULL_VOID(context);
84     auto resRegister = context->GetPlatformResRegister();
85     CHECK_NULL_VOID(resRegister);
86     id_ = resRegister->CreateResource(type_, PARAM_NONE);
87     if (id_ == INVALID_ID) {
88         if (onError_) {
89             onError_(SURFACE_ERRORCODE_CREATEFAIL, SURFACE_ERRORMSG_CREATEFAIL);
90         }
91         return;
92     }
93     hash_ = MakeResourceHash();
94     resRegister->RegisterEvent(
95         MakeEventHash(SURFACE_METHOD_ONCREATE), [weak = WeakClaim(this)](const std::string& param) {
96             auto surface = weak.Upgrade();
97             if (surface) {
98                 surface->OnSurfaceCreated();
99             }
100         });
101 
102     resRegister->RegisterEvent(
103         MakeEventHash(SURFACE_METHOD_ONCHANGED), [weak = WeakClaim(this)](const std::string& param) {
104             auto surface = weak.Upgrade();
105             if (surface) {
106                 auto width = surface->GetIntParam(param, SURFACE_WIDTH);
107                 auto height = surface->GetIntParam(param, SURFACE_HEIGHT);
108                 surface->OnSurfaceChanged(width, height);
109             }
110         });
111 
112     resRegister->RegisterEvent(
113         MakeEventHash(SURFACE_METHOD_ONDESTROYED), [weak = WeakClaim(this)](const std::string& param) {
114             auto surface = weak.Upgrade();
115             if (surface) {
116                 surface->OnSurfaceDestroyed();
117             }
118         });
119 
120     if (onCreate) {
121         onCreate(id_);
122     }
123 #if defined(IOS_PLATFORM)
124     OnSurfaceCreated();
125 #endif
126 }
127 
SetBounds(int64_t surfaceId,int32_t left,int32_t top,int32_t width,int32_t height)128 void ExtSurface::SetBounds(int64_t surfaceId, int32_t left, int32_t top, int32_t width, int32_t height)
129 {
130     std::stringstream paramStream;
131     paramStream << SURFACE_ID << PARAM_EQUALS << surfaceId << PARAM_AND << SURFACE_LEFT << PARAM_EQUALS << left
132                 << PARAM_AND << SURFACE_TOP << PARAM_EQUALS << top << PARAM_AND << SURFACE_WIDTH << PARAM_EQUALS
133                 << width << PARAM_AND << SURFACE_HEIGHT << PARAM_EQUALS << height;
134     std::string param = paramStream.str();
135     CallResRegisterMethod(MakeMethodHash(SET_SURFACE_BOUNDS), param);
136 }
137 
SetIsFullScreen(bool isFullScreen)138 void ExtSurface::SetIsFullScreen(bool isFullScreen)
139 {
140     std::stringstream paramStream;
141     paramStream << IS_FULLSCREEN << PARAM_EQUALS << isFullScreen;
142     std::string param = paramStream.str();
143     CallResRegisterMethod(MakeMethodHash(SET_IS_FULLSCREEN), param);
144 }
145 
OnSurfaceCreated()146 void ExtSurface::OnSurfaceCreated()
147 {
148     if (onSurfaceCreated_) {
149         onSurfaceCreated_();
150     }
151 }
152 
OnSurfaceChanged(int32_t width,int32_t height)153 void ExtSurface::OnSurfaceChanged(int32_t width, int32_t height)
154 {
155     LOGI("OnSurfaceChanged. width: %{public}d height: %{public}d", width, height);
156     if (onSurfaceChanged_) {
157         onSurfaceChanged_(width, height);
158     }
159 }
160 
OnSurfaceDestroyed()161 void ExtSurface::OnSurfaceDestroyed()
162 {
163     LOGI("OnSurfaceDestroyed.");
164     if (onSurfaceDestroyed_) {
165         onSurfaceDestroyed_();
166     }
167 }
168 
AttachNativeWindow()169 void* ExtSurface::AttachNativeWindow()
170 {
171     LOGI("AttachNativeWindow called.");
172     std::stringstream paramStream;
173 
174     void* nativeWindow = nullptr;
175     CallSyncResRegisterMethod(MakeMethodHash(ATTACH_NATIVE_WINDOW), PARAM_NONE,
176         [this, &nativeWindow](std::string& result) mutable {
177             nativeWindow = reinterpret_cast<void*>(GetInt64Param(result, NATIVE_WINDOW));
178     });
179 
180     return nativeWindow;
181 }
182 
183 #ifdef RENDER_EXTRACT_SUPPORTED
SetSurfaceRotation(bool isLock)184 void ExtSurface::SetSurfaceRotation(bool isLock)
185 {
186     std::stringstream paramStream;
187     paramStream << IS_LOCK << PARAM_EQUALS << isLock;
188     std::string param = paramStream.str();
189     CallResRegisterMethod(MakeMethodHash(SET_SURFACE_ROTATION), param);
190 }
191 
SetSurfaceRect(float positionX,float positionY,float width,float height)192 void ExtSurface::SetSurfaceRect(float positionX, float positionY, float width, float height)
193 {
194     std::stringstream paramStream;
195     paramStream << SURFACE_LEFT << PARAM_EQUALS << positionX << PARAM_AND << SURFACE_TOP << PARAM_EQUALS << positionY
196                 << PARAM_AND << SURFACE_WIDTH << PARAM_EQUALS << width << PARAM_AND << SURFACE_HEIGHT << PARAM_EQUALS
197                 << height;
198     std::string param = paramStream.str();
199     CallResRegisterMethod(MakeMethodHash(SET_SURFACE_RECT), param);
200 }
201 #endif
202 
203 } // namespace OHOS::Ace