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/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 void ExternalVideoSurfaceContainerImpl::ReleaseExternalVideoSurface(
47 JNIEnv
* env
= base::android::AttachCurrentThread();
48 Java_ExternalVideoSurfaceContainer_releaseExternalVideoSurface(
49 env
, jobject_
.obj(), static_cast<jint
>(player_id
));
51 surface_created_cb_
.Reset();
52 surface_destroyed_cb_
.Reset();
55 void ExternalVideoSurfaceContainerImpl::OnFrameInfoUpdated() {
56 JNIEnv
* env
= base::android::AttachCurrentThread();
57 Java_ExternalVideoSurfaceContainer_onFrameInfoUpdated(env
, jobject_
.obj());
60 void ExternalVideoSurfaceContainerImpl::OnExternalVideoSurfacePositionChanged(
61 int player_id
, const gfx::RectF
& rect
) {
62 JNIEnv
* env
= base::android::AttachCurrentThread();
63 Java_ExternalVideoSurfaceContainer_onExternalVideoSurfacePositionChanged(
66 static_cast<jint
>(player_id
),
67 static_cast<jfloat
>(rect
.x()),
68 static_cast<jfloat
>(rect
.y()),
69 static_cast<jfloat
>(rect
.x() + rect
.width()),
70 static_cast<jfloat
>(rect
.y() + rect
.height()));
73 // Methods called from Java.
74 void ExternalVideoSurfaceContainerImpl::SurfaceCreated(
75 JNIEnv
* env
, jobject obj
, jint player_id
, jobject jsurface
) {
76 if (!surface_created_cb_
.is_null())
77 surface_created_cb_
.Run(static_cast<int>(player_id
), jsurface
);
80 void ExternalVideoSurfaceContainerImpl::SurfaceDestroyed(
81 JNIEnv
* env
, jobject obj
, jint player_id
) {
82 if (!surface_destroyed_cb_
.is_null())
83 surface_destroyed_cb_
.Run(static_cast<int>(player_id
));
86 bool RegisterExternalVideoSurfaceContainer(JNIEnv
* env
) {
87 return RegisterNativesImpl(env
);
91 } // namespace chromecast