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