OLD | NEW |
| (Empty) |
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/geometry/rect_f.h" | |
11 | |
12 using base::android::AttachCurrentThread; | |
13 using content::ContentViewCore; | |
14 | |
15 namespace android_webview { | |
16 | |
17 // static | |
18 ExternalVideoSurfaceContainerImpl* ExternalVideoSurfaceContainerImpl::Create( | |
19 content::WebContents* web_contents) { | |
20 ContentViewCore* cvc = ContentViewCore::FromWebContents(web_contents); | |
21 if (!cvc) | |
22 return nullptr; | |
23 base::android::ScopedJavaLocalRef<jobject> jcvc = cvc->GetJavaObject(); | |
24 if (jcvc.is_null()) | |
25 return nullptr; | |
26 return new ExternalVideoSurfaceContainerImpl(jcvc); | |
27 } | |
28 | |
29 ExternalVideoSurfaceContainerImpl::ExternalVideoSurfaceContainerImpl( | |
30 base::android::ScopedJavaLocalRef<jobject> java_content_view_core) { | |
31 JNIEnv* env = AttachCurrentThread(); | |
32 jobject_.Reset(Java_ExternalVideoSurfaceContainer_create( | |
33 env, reinterpret_cast<intptr_t>(this), java_content_view_core.obj())); | |
34 } | |
35 | |
36 ExternalVideoSurfaceContainerImpl::~ExternalVideoSurfaceContainerImpl() { | |
37 JNIEnv* env = AttachCurrentThread(); | |
38 Java_ExternalVideoSurfaceContainer_destroy(env, jobject_.obj()); | |
39 jobject_.Reset(); | |
40 } | |
41 | |
42 void ExternalVideoSurfaceContainerImpl::RequestExternalVideoSurface( | |
43 int player_id, | |
44 const SurfaceCreatedCB& surface_created_cb, | |
45 const SurfaceDestroyedCB& surface_destroyed_cb) { | |
46 surface_created_cb_ = surface_created_cb; | |
47 surface_destroyed_cb_ = surface_destroyed_cb; | |
48 | |
49 JNIEnv* env = AttachCurrentThread(); | |
50 Java_ExternalVideoSurfaceContainer_requestExternalVideoSurface( | |
51 env, jobject_.obj(), static_cast<jint>(player_id)); | |
52 } | |
53 | |
54 int ExternalVideoSurfaceContainerImpl::GetCurrentPlayerId() { | |
55 JNIEnv* env = AttachCurrentThread(); | |
56 | |
57 int current_player = static_cast<int>( | |
58 Java_ExternalVideoSurfaceContainer_getCurrentPlayerId( | |
59 env, jobject_.obj())); | |
60 | |
61 if (current_player < 0) | |
62 return kInvalidPlayerId; | |
63 else | |
64 return current_player; | |
65 } | |
66 | |
67 void ExternalVideoSurfaceContainerImpl::ReleaseExternalVideoSurface( | |
68 int player_id) { | |
69 JNIEnv* env = AttachCurrentThread(); | |
70 Java_ExternalVideoSurfaceContainer_releaseExternalVideoSurface( | |
71 env, jobject_.obj(), static_cast<jint>(player_id)); | |
72 | |
73 surface_created_cb_.Reset(); | |
74 surface_destroyed_cb_.Reset(); | |
75 } | |
76 | |
77 void ExternalVideoSurfaceContainerImpl::OnFrameInfoUpdated() { | |
78 JNIEnv* env = AttachCurrentThread(); | |
79 Java_ExternalVideoSurfaceContainer_onFrameInfoUpdated(env, jobject_.obj()); | |
80 } | |
81 | |
82 void ExternalVideoSurfaceContainerImpl::OnExternalVideoSurfacePositionChanged( | |
83 int player_id, const gfx::RectF& rect) { | |
84 JNIEnv* env = AttachCurrentThread(); | |
85 Java_ExternalVideoSurfaceContainer_onExternalVideoSurfacePositionChanged( | |
86 env, | |
87 jobject_.obj(), | |
88 static_cast<jint>(player_id), | |
89 static_cast<jfloat>(rect.x()), | |
90 static_cast<jfloat>(rect.y()), | |
91 static_cast<jfloat>(rect.x() + rect.width()), | |
92 static_cast<jfloat>(rect.y() + rect.height())); | |
93 } | |
94 | |
95 // Methods called from Java. | |
96 void ExternalVideoSurfaceContainerImpl::SurfaceCreated( | |
97 JNIEnv* env, jobject obj, jint player_id, jobject jsurface) { | |
98 if (!surface_created_cb_.is_null()) | |
99 surface_created_cb_.Run(static_cast<int>(player_id), jsurface); | |
100 } | |
101 | |
102 void ExternalVideoSurfaceContainerImpl::SurfaceDestroyed( | |
103 JNIEnv* env, jobject obj, jint player_id) { | |
104 if (!surface_destroyed_cb_.is_null()) | |
105 surface_destroyed_cb_.Run(static_cast<int>(player_id)); | |
106 } | |
107 | |
108 bool RegisterExternalVideoSurfaceContainer(JNIEnv* env) { | |
109 return RegisterNativesImpl(env); | |
110 } | |
111 | |
112 } // namespace android_webview | |
OLD | NEW |