• 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 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 
~ExtSurface()38 ExtSurface::~ExtSurface()
39 {
40     auto context = context_.Upgrade();
41     CHECK_NULL_VOID(context);
42     auto resRegister = context->GetPlatformResRegister();
43     CHECK_NULL_VOID(resRegister);
44     auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
45     if (platformTaskExecutor.IsRunOnCurrentThread()) {
46         resRegister->UnregisterEvent(MakeEventHash(SURFACE_METHOD_ONCREATE));
47     } else {
48         WeakPtr<PlatformResRegister> weak = resRegister;
49         platformTaskExecutor.PostTask(
50             [eventHash = MakeEventHash(SURFACE_METHOD_ONCREATE), weak] {
51                 auto resRegister = weak.Upgrade();
52                 CHECK_NULL_VOID(resRegister);
53                 resRegister->UnregisterEvent(eventHash);
54             },
55             "ArkUIVideoExtSurfaceUnregisterEvent");
56     }
57 }
58 
Create(const std::function<void (int64_t)> & onCreate)59 void ExtSurface::Create(const std::function<void(int64_t)>& onCreate)
60 {
61     auto context = context_.Upgrade();
62     CHECK_NULL_VOID(context);
63 
64     auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
65     platformTaskExecutor.PostTask(
66         [weak = WeakClaim(this), onCreate] {
67             auto surface = weak.Upgrade();
68             if (surface) {
69                 surface->CreateExtSurface(onCreate);
70             }
71         },
72         "ArkUIVideoCreateExtSurface");
73 }
74 
CreateExtSurface(const std::function<void (int64_t)> & onCreate)75 void ExtSurface::CreateExtSurface(const std::function<void(int64_t)>& onCreate)
76 {
77     auto context = context_.Upgrade();
78     CHECK_NULL_VOID(context);
79     auto resRegister = context->GetPlatformResRegister();
80     CHECK_NULL_VOID(resRegister);
81     id_ = resRegister->CreateResource(type_, PARAM_NONE);
82     if (id_ == INVALID_ID) {
83         if (onError_) {
84             onError_(SURFACE_ERRORCODE_CREATEFAIL, SURFACE_ERRORMSG_CREATEFAIL);
85         }
86         return;
87     }
88     hash_ = MakeResourceHash();
89     resRegister->RegisterEvent(
90         MakeEventHash(SURFACE_METHOD_ONCREATE), [weak = WeakClaim(this)](const std::string& param) {
91             auto surface = weak.Upgrade();
92             if (surface) {
93                 surface->OnSurfaceCreated();
94             }
95         });
96 
97     resRegister->RegisterEvent(
98         MakeEventHash(SURFACE_METHOD_ONCHANGED), [weak = WeakClaim(this)](const std::string& param) {
99             auto surface = weak.Upgrade();
100             if (surface) {
101                 auto width = surface->GetIntParam(param, SURFACE_WIDTH);
102                 auto height = surface->GetIntParam(param, SURFACE_HEIGHT);
103                 surface->OnSurfaceChanged(width, height);
104             }
105         });
106 
107     resRegister->RegisterEvent(
108         MakeEventHash(SURFACE_METHOD_ONDESTROYED), [weak = WeakClaim(this)](const std::string& param) {
109             auto surface = weak.Upgrade();
110             if (surface) {
111                 surface->OnSurfaceDestroyed();
112             }
113         });
114 
115     if (onCreate) {
116         onCreate(id_);
117     }
118 #if defined(IOS_PLATFORM)
119     OnSurfaceCreated();
120 #endif
121 }
122 
SetBounds(int64_t surfaceId,int32_t left,int32_t top,int32_t width,int32_t height)123 void ExtSurface::SetBounds(int64_t surfaceId, int32_t left, int32_t top, int32_t width, int32_t height)
124 {
125     std::stringstream paramStream;
126     paramStream << SURFACE_ID << PARAM_EQUALS << surfaceId << PARAM_AND << SURFACE_LEFT << PARAM_EQUALS << left
127                 << PARAM_AND << SURFACE_TOP << PARAM_EQUALS << top << PARAM_AND << SURFACE_WIDTH << PARAM_EQUALS
128                 << width << PARAM_AND << SURFACE_HEIGHT << PARAM_EQUALS << height;
129     std::string param = paramStream.str();
130     CallResRegisterMethod(MakeMethodHash(SET_SURFACE_BOUNDS), param);
131 }
132 
SetIsFullScreen(bool isFullScreen)133 void ExtSurface::SetIsFullScreen(bool isFullScreen)
134 {
135     std::stringstream paramStream;
136     paramStream << IS_FULLSCREEN << PARAM_EQUALS << isFullScreen;
137     std::string param = paramStream.str();
138     CallResRegisterMethod(MakeMethodHash(SET_IS_FULLSCREEN), param);
139 }
140 
OnSurfaceCreated()141 void ExtSurface::OnSurfaceCreated()
142 {
143     if (onSurfaceCreated_) {
144         onSurfaceCreated_();
145     }
146 }
147 
OnSurfaceChanged(int32_t width,int32_t height)148 void ExtSurface::OnSurfaceChanged(int32_t width, int32_t height)
149 {
150     LOGI("OnSurfaceChanged. width: %{public}d height: %{public}d", width, height);
151     if (onSurfaceChanged_) {
152         onSurfaceChanged_(width, height);
153     }
154 }
155 
OnSurfaceDestroyed()156 void ExtSurface::OnSurfaceDestroyed()
157 {
158     LOGI("OnSurfaceDestroyed.");
159     if (onSurfaceDestroyed_) {
160         onSurfaceDestroyed_();
161     }
162 }
163 
AttachNativeWindow()164 void* ExtSurface::AttachNativeWindow()
165 {
166     LOGI("AttachNativeWindow called.");
167     std::stringstream paramStream;
168 
169     void* nativeWindow = nullptr;
170     CallSyncResRegisterMethod(MakeMethodHash(ATTACH_NATIVE_WINDOW), PARAM_NONE,
171         [this, &nativeWindow](std::string& result) mutable {
172             nativeWindow = reinterpret_cast<void*>(GetInt64Param(result, NATIVE_WINDOW));
173     });
174 
175     return nativeWindow;
176 }
177 
178 } // namespace OHOS::Ace