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 "components/external_video_surface/browser/android/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 external_video_surface
{
18 bool ExternalVideoSurfaceContainerImpl::RegisterJni(JNIEnv
* env
) {
19 return RegisterNativesImpl(env
);
23 ExternalVideoSurfaceContainerImpl
* ExternalVideoSurfaceContainerImpl::Create(
24 content::WebContents
* web_contents
) {
25 ContentViewCore
* cvc
= ContentViewCore::FromWebContents(web_contents
);
28 base::android::ScopedJavaLocalRef
<jobject
> jcvc
= cvc
->GetJavaObject();
31 return new ExternalVideoSurfaceContainerImpl(jcvc
);
34 ExternalVideoSurfaceContainerImpl::ExternalVideoSurfaceContainerImpl(
35 base::android::ScopedJavaLocalRef
<jobject
> java_content_view_core
) {
36 JNIEnv
* env
= AttachCurrentThread();
37 jobject_
.Reset(Java_ExternalVideoSurfaceContainer_create(
38 env
, reinterpret_cast<intptr_t>(this), java_content_view_core
.obj()));
41 ExternalVideoSurfaceContainerImpl::~ExternalVideoSurfaceContainerImpl() {
42 JNIEnv
* env
= AttachCurrentThread();
43 Java_ExternalVideoSurfaceContainer_destroy(env
, jobject_
.obj());
47 void ExternalVideoSurfaceContainerImpl::RequestExternalVideoSurface(
49 const SurfaceCreatedCB
& surface_created_cb
,
50 const SurfaceDestroyedCB
& surface_destroyed_cb
) {
51 surface_created_cb_
= surface_created_cb
;
52 surface_destroyed_cb_
= surface_destroyed_cb
;
54 JNIEnv
* env
= AttachCurrentThread();
55 Java_ExternalVideoSurfaceContainer_requestExternalVideoSurface(
56 env
, jobject_
.obj(), static_cast<jint
>(player_id
));
59 int ExternalVideoSurfaceContainerImpl::GetCurrentPlayerId() {
60 JNIEnv
* env
= AttachCurrentThread();
62 int current_player
= static_cast<int>(
63 Java_ExternalVideoSurfaceContainer_getCurrentPlayerId(
64 env
, jobject_
.obj()));
66 if (current_player
< 0)
67 return kInvalidPlayerId
;
69 return current_player
;
72 void ExternalVideoSurfaceContainerImpl::ReleaseExternalVideoSurface(
74 JNIEnv
* env
= AttachCurrentThread();
75 Java_ExternalVideoSurfaceContainer_releaseExternalVideoSurface(
76 env
, jobject_
.obj(), static_cast<jint
>(player_id
));
78 surface_created_cb_
.Reset();
79 surface_destroyed_cb_
.Reset();
82 void ExternalVideoSurfaceContainerImpl::OnFrameInfoUpdated() {
83 JNIEnv
* env
= AttachCurrentThread();
84 Java_ExternalVideoSurfaceContainer_onFrameInfoUpdated(env
, jobject_
.obj());
87 void ExternalVideoSurfaceContainerImpl::OnExternalVideoSurfacePositionChanged(
88 int player_id
, const gfx::RectF
& rect
) {
89 JNIEnv
* env
= AttachCurrentThread();
90 Java_ExternalVideoSurfaceContainer_onExternalVideoSurfacePositionChanged(
93 static_cast<jint
>(player_id
),
94 static_cast<jfloat
>(rect
.x()),
95 static_cast<jfloat
>(rect
.y()),
96 static_cast<jfloat
>(rect
.x() + rect
.width()),
97 static_cast<jfloat
>(rect
.y() + rect
.height()));
100 // Methods called from Java.
101 void ExternalVideoSurfaceContainerImpl::SurfaceCreated(
102 JNIEnv
* env
, jobject obj
, jint player_id
, jobject jsurface
) {
103 if (!surface_created_cb_
.is_null())
104 surface_created_cb_
.Run(static_cast<int>(player_id
), jsurface
);
107 void ExternalVideoSurfaceContainerImpl::SurfaceDestroyed(
108 JNIEnv
* env
, jobject obj
, jint player_id
) {
109 if (!surface_destroyed_cb_
.is_null())
110 surface_destroyed_cb_
.Run(static_cast<int>(player_id
));
113 } // namespace external_video_surface