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(
16 content::WebContents
* web_contents
) {
17 content::ContentViewCore
* cvc
=
18 content::ContentViewCore::FromWebContents(web_contents
);
20 JNIEnv
* env
= base::android::AttachCurrentThread();
22 Java_ExternalVideoSurfaceContainer_create(
23 env
, reinterpret_cast<intptr_t>(this), cvc
->GetJavaObject().obj()));
27 ExternalVideoSurfaceContainerImpl::~ExternalVideoSurfaceContainerImpl() {
28 JNIEnv
* env
= base::android::AttachCurrentThread();
29 Java_ExternalVideoSurfaceContainer_destroy(env
, jobject_
.obj());
33 void ExternalVideoSurfaceContainerImpl::RequestExternalVideoSurface(
35 const SurfaceCreatedCB
& surface_created_cb
,
36 const SurfaceDestroyedCB
& surface_destroyed_cb
) {
37 surface_created_cb_
= surface_created_cb
;
38 surface_destroyed_cb_
= surface_destroyed_cb
;
40 JNIEnv
* env
= base::android::AttachCurrentThread();
41 Java_ExternalVideoSurfaceContainer_requestExternalVideoSurface(
42 env
, jobject_
.obj(), static_cast<jint
>(player_id
));
45 int ExternalVideoSurfaceContainerImpl::GetCurrentPlayerId() {
46 JNIEnv
* env
= base::android::AttachCurrentThread();
48 int current_player
= static_cast<int>(
49 Java_ExternalVideoSurfaceContainer_getCurrentPlayerId(
50 env
, jobject_
.obj()));
52 if (current_player
< 0)
53 return kInvalidPlayerId
;
55 return current_player
;
58 void ExternalVideoSurfaceContainerImpl::ReleaseExternalVideoSurface(
60 JNIEnv
* env
= base::android::AttachCurrentThread();
61 Java_ExternalVideoSurfaceContainer_releaseExternalVideoSurface(
62 env
, jobject_
.obj(), static_cast<jint
>(player_id
));
64 surface_created_cb_
.Reset();
65 surface_destroyed_cb_
.Reset();
68 void ExternalVideoSurfaceContainerImpl::OnFrameInfoUpdated() {
69 JNIEnv
* env
= base::android::AttachCurrentThread();
70 Java_ExternalVideoSurfaceContainer_onFrameInfoUpdated(env
, jobject_
.obj());
73 void ExternalVideoSurfaceContainerImpl::OnExternalVideoSurfacePositionChanged(
74 int player_id
, const gfx::RectF
& rect
) {
75 JNIEnv
* env
= base::android::AttachCurrentThread();
76 Java_ExternalVideoSurfaceContainer_onExternalVideoSurfacePositionChanged(
79 static_cast<jint
>(player_id
),
80 static_cast<jfloat
>(rect
.x()),
81 static_cast<jfloat
>(rect
.y()),
82 static_cast<jfloat
>(rect
.x() + rect
.width()),
83 static_cast<jfloat
>(rect
.y() + rect
.height()));
86 // Methods called from Java.
87 void ExternalVideoSurfaceContainerImpl::SurfaceCreated(
88 JNIEnv
* env
, jobject obj
, jint player_id
, jobject jsurface
) {
89 if (!surface_created_cb_
.is_null())
90 surface_created_cb_
.Run(static_cast<int>(player_id
), jsurface
);
93 void ExternalVideoSurfaceContainerImpl::SurfaceDestroyed(
94 JNIEnv
* env
, jobject obj
, jint player_id
) {
95 if (!surface_destroyed_cb_
.is_null())
96 surface_destroyed_cb_
.Run(static_cast<int>(player_id
));
99 bool RegisterExternalVideoSurfaceContainer(JNIEnv
* env
) {
100 return RegisterNativesImpl(env
);
104 } // namespace chromecast