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/rect_f.h" |
| 11 |
| 12 using android_webview::ExternalVideoSurfaceContainerImpl; |
| 13 using base::android::AttachCurrentThread; |
| 14 using content::ContentViewCore; |
| 15 |
| 16 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ExternalVideoSurfaceContainerImpl); |
| 17 |
| 18 namespace content { |
| 19 |
| 20 // static |
| 21 void ExternalVideoSurfaceContainer::CreateForWebContents( |
| 22 WebContents* web_contents) { |
| 23 WebContentsUserData<ExternalVideoSurfaceContainerImpl>::CreateForWebContents( |
| 24 web_contents); |
| 25 } |
| 26 |
| 27 // static |
| 28 ExternalVideoSurfaceContainer* ExternalVideoSurfaceContainer::FromWebContents( |
| 29 WebContents* web_contents) { |
| 30 return WebContentsUserData<ExternalVideoSurfaceContainerImpl>:: |
| 31 FromWebContents(web_contents); |
| 32 } |
| 33 |
| 34 } // namespace content |
| 35 |
| 36 namespace android_webview { |
| 37 |
| 38 ExternalVideoSurfaceContainerImpl::ExternalVideoSurfaceContainerImpl( |
| 39 content::WebContents* web_contents) { |
| 40 ContentViewCore* cvc = ContentViewCore::FromWebContents(web_contents); |
| 41 if (cvc) { |
| 42 JNIEnv* env = AttachCurrentThread(); |
| 43 jobject_.Reset( |
| 44 Java_ExternalVideoSurfaceContainer_create( |
| 45 env, reinterpret_cast<intptr_t>(this), cvc->GetJavaObject().obj())); |
| 46 } |
| 47 } |
| 48 |
| 49 ExternalVideoSurfaceContainerImpl::~ExternalVideoSurfaceContainerImpl() { |
| 50 JNIEnv* env = AttachCurrentThread(); |
| 51 Java_ExternalVideoSurfaceContainer_destroy(env, jobject_.obj()); |
| 52 jobject_.Reset(); |
| 53 } |
| 54 |
| 55 void ExternalVideoSurfaceContainerImpl::RequestExternalVideoSurface( |
| 56 int player_id, |
| 57 const SurfaceCreatedCB& surface_created_cb, |
| 58 const SurfaceDestroyedCB& surface_destroyed_cb) { |
| 59 surface_created_cb_ = surface_created_cb; |
| 60 surface_destroyed_cb_ = surface_destroyed_cb; |
| 61 |
| 62 JNIEnv* env = AttachCurrentThread(); |
| 63 Java_ExternalVideoSurfaceContainer_requestExternalVideoSurface( |
| 64 env, jobject_.obj(), static_cast<jint>(player_id)); |
| 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) >= 0; |
| 110 } |
| 111 |
| 112 } // namespace android_webview |
OLD | NEW |