1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "android_webview/native/external_video_surface_container_impl.h"
7 #include "base/android/jni_android.h"
8 #include "content/public/browser/android/content_view_core.h"
9 #include "jni/ExternalVideoSurfaceContainer_jni.h"
10 #include "ui/gfx/rect_f.h"
12 using android_webview::ExternalVideoSurfaceContainerImpl
;
13 using base::android::AttachCurrentThread
;
14 using content::ContentViewCore
;
16 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ExternalVideoSurfaceContainerImpl
);
21 void ExternalVideoSurfaceContainer::CreateForWebContents(
22 WebContents
* web_contents
) {
23 WebContentsUserData
<ExternalVideoSurfaceContainerImpl
>::CreateForWebContents(
28 ExternalVideoSurfaceContainer
* ExternalVideoSurfaceContainer::FromWebContents(
29 WebContents
* web_contents
) {
30 return WebContentsUserData
<ExternalVideoSurfaceContainerImpl
>::
31 FromWebContents(web_contents
);
34 } // namespace content
36 namespace android_webview
{
38 ExternalVideoSurfaceContainerImpl::ExternalVideoSurfaceContainerImpl(
39 content::WebContents
* web_contents
) {
40 ContentViewCore
* cvc
= ContentViewCore::FromWebContents(web_contents
);
42 JNIEnv
* env
= AttachCurrentThread();
44 Java_ExternalVideoSurfaceContainer_create(
45 env
, reinterpret_cast<intptr_t>(this), cvc
->GetJavaObject().obj()));
49 ExternalVideoSurfaceContainerImpl::~ExternalVideoSurfaceContainerImpl() {
50 JNIEnv
* env
= AttachCurrentThread();
51 Java_ExternalVideoSurfaceContainer_destroy(env
, jobject_
.obj());
55 void ExternalVideoSurfaceContainerImpl::RequestExternalVideoSurface(
57 const SurfaceCreatedCB
& surface_created_cb
,
58 const SurfaceDestroyedCB
& surface_destroyed_cb
) {
59 surface_created_cb_
= surface_created_cb
;
60 surface_destroyed_cb_
= surface_destroyed_cb
;
62 JNIEnv
* env
= AttachCurrentThread();
63 Java_ExternalVideoSurfaceContainer_requestExternalVideoSurface(
64 env
, jobject_
.obj(), static_cast<jint
>(player_id
));
67 void ExternalVideoSurfaceContainerImpl::ReleaseExternalVideoSurface(
69 JNIEnv
* env
= AttachCurrentThread();
70 Java_ExternalVideoSurfaceContainer_releaseExternalVideoSurface(
71 env
, jobject_
.obj(), static_cast<jint
>(player_id
));
73 surface_created_cb_
.Reset();
74 surface_destroyed_cb_
.Reset();
77 void ExternalVideoSurfaceContainerImpl::OnFrameInfoUpdated() {
78 JNIEnv
* env
= AttachCurrentThread();
79 Java_ExternalVideoSurfaceContainer_onFrameInfoUpdated(env
, jobject_
.obj());
82 void ExternalVideoSurfaceContainerImpl::OnExternalVideoSurfacePositionChanged(
83 int player_id
, const gfx::RectF
& rect
) {
84 JNIEnv
* env
= AttachCurrentThread();
85 Java_ExternalVideoSurfaceContainer_onExternalVideoSurfacePositionChanged(
88 static_cast<jint
>(player_id
),
89 static_cast<jfloat
>(rect
.x()),
90 static_cast<jfloat
>(rect
.y()),
91 static_cast<jfloat
>(rect
.x() + rect
.width()),
92 static_cast<jfloat
>(rect
.y() + rect
.height()));
95 // Methods called from Java.
96 void ExternalVideoSurfaceContainerImpl::SurfaceCreated(
97 JNIEnv
* env
, jobject obj
, jint player_id
, jobject jsurface
) {
98 if (!surface_created_cb_
.is_null())
99 surface_created_cb_
.Run(static_cast<int>(player_id
), jsurface
);
102 void ExternalVideoSurfaceContainerImpl::SurfaceDestroyed(
103 JNIEnv
* env
, jobject obj
, jint player_id
) {
104 if (!surface_destroyed_cb_
.is_null())
105 surface_destroyed_cb_
.Run(static_cast<int>(player_id
));
108 bool RegisterExternalVideoSurfaceContainer(JNIEnv
* env
) {
109 return RegisterNativesImpl(env
) >= 0;
112 } // namespace android_webview