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
{
17 ExternalVideoSurfaceContainerImpl::ExternalVideoSurfaceContainerImpl(
18 content::WebContents
* web_contents
) {
19 ContentViewCore
* cvc
= ContentViewCore::FromWebContents(web_contents
);
21 JNIEnv
* env
= AttachCurrentThread();
23 Java_ExternalVideoSurfaceContainer_create(
24 env
, reinterpret_cast<intptr_t>(this), cvc
->GetJavaObject().obj()));
28 ExternalVideoSurfaceContainerImpl::~ExternalVideoSurfaceContainerImpl() {
29 JNIEnv
* env
= AttachCurrentThread();
30 Java_ExternalVideoSurfaceContainer_destroy(env
, jobject_
.obj());
34 void ExternalVideoSurfaceContainerImpl::RequestExternalVideoSurface(
36 const SurfaceCreatedCB
& surface_created_cb
,
37 const SurfaceDestroyedCB
& surface_destroyed_cb
) {
38 surface_created_cb_
= surface_created_cb
;
39 surface_destroyed_cb_
= surface_destroyed_cb
;
41 JNIEnv
* env
= AttachCurrentThread();
42 Java_ExternalVideoSurfaceContainer_requestExternalVideoSurface(
43 env
, jobject_
.obj(), static_cast<jint
>(player_id
));
46 int ExternalVideoSurfaceContainerImpl::GetCurrentPlayerId() {
47 JNIEnv
* env
= AttachCurrentThread();
49 int current_player
= static_cast<int>(
50 Java_ExternalVideoSurfaceContainer_getCurrentPlayerId(
51 env
, jobject_
.obj()));
53 if (current_player
< 0)
54 return kInvalidPlayerId
;
56 return current_player
;
59 void ExternalVideoSurfaceContainerImpl::ReleaseExternalVideoSurface(
61 JNIEnv
* env
= AttachCurrentThread();
62 Java_ExternalVideoSurfaceContainer_releaseExternalVideoSurface(
63 env
, jobject_
.obj(), static_cast<jint
>(player_id
));
65 surface_created_cb_
.Reset();
66 surface_destroyed_cb_
.Reset();
69 void ExternalVideoSurfaceContainerImpl::OnFrameInfoUpdated() {
70 JNIEnv
* env
= AttachCurrentThread();
71 Java_ExternalVideoSurfaceContainer_onFrameInfoUpdated(env
, jobject_
.obj());
74 void ExternalVideoSurfaceContainerImpl::OnExternalVideoSurfacePositionChanged(
75 int player_id
, const gfx::RectF
& rect
) {
76 JNIEnv
* env
= AttachCurrentThread();
77 Java_ExternalVideoSurfaceContainer_onExternalVideoSurfacePositionChanged(
80 static_cast<jint
>(player_id
),
81 static_cast<jfloat
>(rect
.x()),
82 static_cast<jfloat
>(rect
.y()),
83 static_cast<jfloat
>(rect
.x() + rect
.width()),
84 static_cast<jfloat
>(rect
.y() + rect
.height()));
87 // Methods called from Java.
88 void ExternalVideoSurfaceContainerImpl::SurfaceCreated(
89 JNIEnv
* env
, jobject obj
, jint player_id
, jobject jsurface
) {
90 if (!surface_created_cb_
.is_null())
91 surface_created_cb_
.Run(static_cast<int>(player_id
), jsurface
);
94 void ExternalVideoSurfaceContainerImpl::SurfaceDestroyed(
95 JNIEnv
* env
, jobject obj
, jint player_id
) {
96 if (!surface_destroyed_cb_
.is_null())
97 surface_destroyed_cb_
.Run(static_cast<int>(player_id
));
100 bool RegisterExternalVideoSurfaceContainer(JNIEnv
* env
) {
101 return RegisterNativesImpl(env
);
104 } // namespace android_webview