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/geometry/rect_f.h"
12 using base::android::AttachCurrentThread
;
13 using content::ContentViewCore
;
15 namespace android_webview
{
18 ExternalVideoSurfaceContainerImpl
* ExternalVideoSurfaceContainerImpl::Create(
19 content::WebContents
* web_contents
) {
20 ContentViewCore
* cvc
= ContentViewCore::FromWebContents(web_contents
);
23 base::android::ScopedJavaLocalRef
<jobject
> jcvc
= cvc
->GetJavaObject();
26 return new ExternalVideoSurfaceContainerImpl(jcvc
);
29 ExternalVideoSurfaceContainerImpl::ExternalVideoSurfaceContainerImpl(
30 base::android::ScopedJavaLocalRef
<jobject
> java_content_view_core
) {
31 JNIEnv
* env
= AttachCurrentThread();
32 jobject_
.Reset(Java_ExternalVideoSurfaceContainer_create(
33 env
, reinterpret_cast<intptr_t>(this), java_content_view_core
.obj()));
36 ExternalVideoSurfaceContainerImpl::~ExternalVideoSurfaceContainerImpl() {
37 JNIEnv
* env
= AttachCurrentThread();
38 Java_ExternalVideoSurfaceContainer_destroy(env
, jobject_
.obj());
42 void ExternalVideoSurfaceContainerImpl::RequestExternalVideoSurface(
44 const SurfaceCreatedCB
& surface_created_cb
,
45 const SurfaceDestroyedCB
& surface_destroyed_cb
) {
46 surface_created_cb_
= surface_created_cb
;
47 surface_destroyed_cb_
= surface_destroyed_cb
;
49 JNIEnv
* env
= AttachCurrentThread();
50 Java_ExternalVideoSurfaceContainer_requestExternalVideoSurface(
51 env
, jobject_
.obj(), static_cast<jint
>(player_id
));
54 int ExternalVideoSurfaceContainerImpl::GetCurrentPlayerId() {
55 JNIEnv
* env
= AttachCurrentThread();
57 int current_player
= static_cast<int>(
58 Java_ExternalVideoSurfaceContainer_getCurrentPlayerId(
59 env
, jobject_
.obj()));
61 if (current_player
< 0)
62 return kInvalidPlayerId
;
64 return current_player
;
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
);
112 } // namespace android_webview