Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / android_webview / native / external_video_surface_container_impl.cc
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.
4
5 #include "android_webview/native/external_video_surface_container_impl.h"
6
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"
11
12 using base::android::AttachCurrentThread;
13 using content::ContentViewCore;
14
15 namespace android_webview {
16
17 ExternalVideoSurfaceContainerImpl::ExternalVideoSurfaceContainerImpl(
18     content::WebContents* web_contents) {
19   ContentViewCore* cvc = ContentViewCore::FromWebContents(web_contents);
20   if (cvc) {
21     JNIEnv* env = AttachCurrentThread();
22     jobject_.Reset(
23         Java_ExternalVideoSurfaceContainer_create(
24             env, reinterpret_cast<intptr_t>(this), cvc->GetJavaObject().obj()));
25   }
26 }
27
28 ExternalVideoSurfaceContainerImpl::~ExternalVideoSurfaceContainerImpl() {
29   JNIEnv* env = AttachCurrentThread();
30   Java_ExternalVideoSurfaceContainer_destroy(env, jobject_.obj());
31   jobject_.Reset();
32 }
33
34 void ExternalVideoSurfaceContainerImpl::RequestExternalVideoSurface(
35     int player_id,
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;
40
41   JNIEnv* env = AttachCurrentThread();
42   Java_ExternalVideoSurfaceContainer_requestExternalVideoSurface(
43       env, jobject_.obj(), static_cast<jint>(player_id));
44 }
45
46 void ExternalVideoSurfaceContainerImpl::ReleaseExternalVideoSurface(
47     int player_id) {
48   JNIEnv* env = AttachCurrentThread();
49   Java_ExternalVideoSurfaceContainer_releaseExternalVideoSurface(
50       env, jobject_.obj(), static_cast<jint>(player_id));
51
52   surface_created_cb_.Reset();
53   surface_destroyed_cb_.Reset();
54 }
55
56 void ExternalVideoSurfaceContainerImpl::OnFrameInfoUpdated() {
57   JNIEnv* env = AttachCurrentThread();
58   Java_ExternalVideoSurfaceContainer_onFrameInfoUpdated(env, jobject_.obj());
59 }
60
61 void ExternalVideoSurfaceContainerImpl::OnExternalVideoSurfacePositionChanged(
62     int player_id, const gfx::RectF& rect) {
63   JNIEnv* env = AttachCurrentThread();
64   Java_ExternalVideoSurfaceContainer_onExternalVideoSurfacePositionChanged(
65       env,
66       jobject_.obj(),
67       static_cast<jint>(player_id),
68       static_cast<jfloat>(rect.x()),
69       static_cast<jfloat>(rect.y()),
70       static_cast<jfloat>(rect.x() + rect.width()),
71       static_cast<jfloat>(rect.y() + rect.height()));
72 }
73
74 // Methods called from Java.
75 void ExternalVideoSurfaceContainerImpl::SurfaceCreated(
76     JNIEnv* env, jobject obj, jint player_id, jobject jsurface) {
77   if (!surface_created_cb_.is_null())
78     surface_created_cb_.Run(static_cast<int>(player_id), jsurface);
79 }
80
81 void ExternalVideoSurfaceContainerImpl::SurfaceDestroyed(
82     JNIEnv* env, jobject obj, jint player_id) {
83   if (!surface_destroyed_cb_.is_null())
84     surface_destroyed_cb_.Run(static_cast<int>(player_id));
85 }
86
87 bool RegisterExternalVideoSurfaceContainer(JNIEnv* env) {
88   return RegisterNativesImpl(env);
89 }
90
91 }  // namespace android_webview