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 "chromecast/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 namespace chromecast
{
15 ExternalVideoSurfaceContainerImpl
* ExternalVideoSurfaceContainerImpl::Create(
16 content::WebContents
* web_contents
) {
17 content::ContentViewCore
* cvc
=
18 content::ContentViewCore::FromWebContents(web_contents
);
21 base::android::ScopedJavaLocalRef
<jobject
> jcvc
= cvc
->GetJavaObject();
24 return new ExternalVideoSurfaceContainerImpl(jcvc
);
27 ExternalVideoSurfaceContainerImpl::ExternalVideoSurfaceContainerImpl(
28 base::android::ScopedJavaLocalRef
<jobject
> java_content_view_core
) {
29 JNIEnv
* env
= base::android::AttachCurrentThread();
30 jobject_
.Reset(Java_ExternalVideoSurfaceContainer_create(
31 env
, reinterpret_cast<intptr_t>(this), java_content_view_core
.obj()));
34 ExternalVideoSurfaceContainerImpl::~ExternalVideoSurfaceContainerImpl() {
35 JNIEnv
* env
= base::android::AttachCurrentThread();
36 Java_ExternalVideoSurfaceContainer_destroy(env
, jobject_
.obj());
40 void ExternalVideoSurfaceContainerImpl::RequestExternalVideoSurface(
42 const SurfaceCreatedCB
& surface_created_cb
,
43 const SurfaceDestroyedCB
& surface_destroyed_cb
) {
44 surface_created_cb_
= surface_created_cb
;
45 surface_destroyed_cb_
= surface_destroyed_cb
;
47 JNIEnv
* env
= base::android::AttachCurrentThread();
48 Java_ExternalVideoSurfaceContainer_requestExternalVideoSurface(
49 env
, jobject_
.obj(), static_cast<jint
>(player_id
));
52 int ExternalVideoSurfaceContainerImpl::GetCurrentPlayerId() {
53 JNIEnv
* env
= base::android::AttachCurrentThread();
55 int current_player
= static_cast<int>(
56 Java_ExternalVideoSurfaceContainer_getCurrentPlayerId(
57 env
, jobject_
.obj()));
59 if (current_player
< 0)
60 return kInvalidPlayerId
;
62 return current_player
;
65 void ExternalVideoSurfaceContainerImpl::ReleaseExternalVideoSurface(
67 JNIEnv
* env
= base::android::AttachCurrentThread();
68 Java_ExternalVideoSurfaceContainer_releaseExternalVideoSurface(
69 env
, jobject_
.obj(), static_cast<jint
>(player_id
));
71 surface_created_cb_
.Reset();
72 surface_destroyed_cb_
.Reset();
75 void ExternalVideoSurfaceContainerImpl::OnFrameInfoUpdated() {
76 JNIEnv
* env
= base::android::AttachCurrentThread();
77 Java_ExternalVideoSurfaceContainer_onFrameInfoUpdated(env
, jobject_
.obj());
80 void ExternalVideoSurfaceContainerImpl::OnExternalVideoSurfacePositionChanged(
81 int player_id
, const gfx::RectF
& rect
) {
82 JNIEnv
* env
= base::android::AttachCurrentThread();
83 Java_ExternalVideoSurfaceContainer_onExternalVideoSurfacePositionChanged(
86 static_cast<jint
>(player_id
),
87 static_cast<jfloat
>(rect
.x()),
88 static_cast<jfloat
>(rect
.y()),
89 static_cast<jfloat
>(rect
.x() + rect
.width()),
90 static_cast<jfloat
>(rect
.y() + rect
.height()));
93 // Methods called from Java.
94 void ExternalVideoSurfaceContainerImpl::SurfaceCreated(
95 JNIEnv
* env
, jobject obj
, jint player_id
, jobject jsurface
) {
96 if (!surface_created_cb_
.is_null())
97 surface_created_cb_
.Run(static_cast<int>(player_id
), jsurface
);
100 void ExternalVideoSurfaceContainerImpl::SurfaceDestroyed(
101 JNIEnv
* env
, jobject obj
, jint player_id
) {
102 if (!surface_destroyed_cb_
.is_null())
103 surface_destroyed_cb_
.Run(static_cast<int>(player_id
));
106 bool RegisterExternalVideoSurfaceContainer(JNIEnv
* env
) {
107 return RegisterNativesImpl(env
);
111 } // namespace chromecast