Call VideoCapturer.initialize directly from Java.

Passing the call through JNI is unnecessary.

Bug: webrtc:7730
Change-Id: Icf1ecd7e2ea54033342120311c70d47b4a4f7c9a
Reviewed-on: https://chromium-review.googlesource.com/521050
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Reviewed-by: Magnus Jedvert <magjed@webrtc.org>
Commit-Queue: Sami Kalliomäki <sakal@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#18408}
This commit is contained in:
Sami Kalliomäki
2017-06-02 09:51:39 +02:00
committed by Commit Bot
parent dbb497af84
commit 58c742ce7d
4 changed files with 24 additions and 51 deletions

View File

@ -30,6 +30,7 @@ public class PeerConnectionFactory {
} }
private static final String TAG = "PeerConnectionFactory"; private static final String TAG = "PeerConnectionFactory";
private static final String VIDEO_CAPTURER_THREAD_NAME = "VideoCapturerThread";
private final long nativeFactory; private final long nativeFactory;
private static Context applicationContext; private static Context applicationContext;
private static Thread networkThread; private static Thread networkThread;
@ -135,12 +136,14 @@ public class PeerConnectionFactory {
public VideoSource createVideoSource(VideoCapturer capturer) { public VideoSource createVideoSource(VideoCapturer capturer) {
final EglBase.Context eglContext = final EglBase.Context eglContext =
localEglbase == null ? null : localEglbase.getEglBaseContext(); localEglbase == null ? null : localEglbase.getEglBaseContext();
final SurfaceTextureHelper surfaceTextureHelper =
SurfaceTextureHelper.create(VIDEO_CAPTURER_THREAD_NAME, eglContext);
long nativeAndroidVideoTrackSource = long nativeAndroidVideoTrackSource =
nativeCreateVideoSource(nativeFactory, eglContext, capturer.isScreencast()); nativeCreateVideoSource(nativeFactory, surfaceTextureHelper, capturer.isScreencast());
VideoCapturer.CapturerObserver capturerObserver = VideoCapturer.CapturerObserver capturerObserver =
new AndroidVideoTrackSourceObserver(nativeAndroidVideoTrackSource); new AndroidVideoTrackSourceObserver(nativeAndroidVideoTrackSource);
nativeInitializeVideoCapturer( capturer.initialize(
nativeFactory, capturer, nativeAndroidVideoTrackSource, capturerObserver); surfaceTextureHelper, ContextUtils.getApplicationContext(), capturerObserver);
return new VideoSource(nativeAndroidVideoTrackSource); return new VideoSource(nativeAndroidVideoTrackSource);
} }
@ -254,11 +257,7 @@ public class PeerConnectionFactory {
private static native long nativeCreateLocalMediaStream(long nativeFactory, String label); private static native long nativeCreateLocalMediaStream(long nativeFactory, String label);
private static native long nativeCreateVideoSource( private static native long nativeCreateVideoSource(
long nativeFactory, EglBase.Context eglContext, boolean is_screencast); long nativeFactory, SurfaceTextureHelper surfaceTextureHelper, boolean is_screencast);
private static native void nativeInitializeVideoCapturer(long native_factory,
VideoCapturer j_video_capturer, long native_source,
VideoCapturer.CapturerObserver j_frame_observer);
private static native long nativeCreateVideoTrack( private static native long nativeCreateVideoTrack(
long nativeFactory, String id, long nativeVideoSource); long nativeFactory, String id, long nativeVideoSource);

View File

@ -19,16 +19,17 @@ const int kRequiredResolutionAlignment = 2;
namespace webrtc { namespace webrtc {
AndroidVideoTrackSource::AndroidVideoTrackSource(rtc::Thread* signaling_thread, AndroidVideoTrackSource::AndroidVideoTrackSource(
JNIEnv* jni, rtc::Thread* signaling_thread,
jobject j_egl_context, JNIEnv* jni,
bool is_screencast) jobject j_surface_texture_helper,
bool is_screencast)
: AdaptedVideoTrackSource(kRequiredResolutionAlignment), : AdaptedVideoTrackSource(kRequiredResolutionAlignment),
signaling_thread_(signaling_thread), signaling_thread_(signaling_thread),
surface_texture_helper_(webrtc_jni::SurfaceTextureHelper::create( surface_texture_helper_(
jni, new rtc::RefCountedObject<webrtc_jni::SurfaceTextureHelper>(
"Camera SurfaceTextureHelper", jni,
j_egl_context)), j_surface_texture_helper)),
is_screencast_(is_screencast) { is_screencast_(is_screencast) {
LOG(LS_INFO) << "AndroidVideoTrackSource ctor"; LOG(LS_INFO) << "AndroidVideoTrackSource ctor";
camera_thread_checker_.DetachFromThread(); camera_thread_checker_.DetachFromThread();

View File

@ -27,7 +27,7 @@ class AndroidVideoTrackSource : public rtc::AdaptedVideoTrackSource {
public: public:
AndroidVideoTrackSource(rtc::Thread* signaling_thread, AndroidVideoTrackSource(rtc::Thread* signaling_thread,
JNIEnv* jni, JNIEnv* jni,
jobject j_egl_context, jobject j_surface_texture_helper,
bool is_screencast = false); bool is_screencast = false);
bool is_screencast() const override { return is_screencast_; } bool is_screencast() const override { return is_screencast_; }

View File

@ -126,7 +126,6 @@ static char *field_trials_init_string = NULL;
// Set in PeerConnectionFactory_initializeAndroidGlobals(). // Set in PeerConnectionFactory_initializeAndroidGlobals().
static bool factory_static_initialized = false; static bool factory_static_initialized = false;
static bool video_hw_acceleration_enabled = true; static bool video_hw_acceleration_enabled = true;
static jobject j_application_context = nullptr;
// Return the (singleton) Java Enum object corresponding to |index|; // Return the (singleton) Java Enum object corresponding to |index|;
// |state_class_fragment| is something like "MediaSource$State". // |state_class_fragment| is something like "MediaSource$State".
@ -1146,8 +1145,6 @@ JOW(void, PeerConnectionFactory_nativeInitializeAndroidGlobals)
jboolean video_hw_acceleration) { jboolean video_hw_acceleration) {
video_hw_acceleration_enabled = video_hw_acceleration; video_hw_acceleration_enabled = video_hw_acceleration;
if (!factory_static_initialized) { if (!factory_static_initialized) {
RTC_DCHECK(j_application_context == nullptr);
j_application_context = NewGlobalRef(jni, context);
webrtc::JVM::Initialize(GetJVM()); webrtc::JVM::Initialize(GetJVM());
factory_static_initialized = true; factory_static_initialized = true;
} }
@ -1416,14 +1413,18 @@ JOW(jlong, PeerConnectionFactory_nativeCreateLocalMediaStream)(
} }
JOW(jlong, PeerConnectionFactory_nativeCreateVideoSource) JOW(jlong, PeerConnectionFactory_nativeCreateVideoSource)
(JNIEnv* jni, jclass, jlong native_factory, jobject j_egl_context, (JNIEnv* jni,
jboolean is_screencast) { jclass,
jlong native_factory,
jobject j_surface_texture_helper,
jboolean is_screencast) {
OwnedFactoryAndThreads* factory = OwnedFactoryAndThreads* factory =
reinterpret_cast<OwnedFactoryAndThreads*>(native_factory); reinterpret_cast<OwnedFactoryAndThreads*>(native_factory);
rtc::scoped_refptr<webrtc::AndroidVideoTrackSource> source( rtc::scoped_refptr<webrtc::AndroidVideoTrackSource> source(
new rtc::RefCountedObject<webrtc::AndroidVideoTrackSource>( new rtc::RefCountedObject<webrtc::AndroidVideoTrackSource>(
factory->signaling_thread(), jni, j_egl_context, is_screencast)); factory->signaling_thread(), jni, j_surface_texture_helper,
is_screencast));
rtc::scoped_refptr<webrtc::VideoTrackSourceProxy> proxy_source = rtc::scoped_refptr<webrtc::VideoTrackSourceProxy> proxy_source =
webrtc::VideoTrackSourceProxy::Create(factory->signaling_thread(), webrtc::VideoTrackSourceProxy::Create(factory->signaling_thread(),
factory->worker_thread(), source); factory->worker_thread(), source);
@ -1431,34 +1432,6 @@ JOW(jlong, PeerConnectionFactory_nativeCreateVideoSource)
return (jlong)proxy_source.release(); return (jlong)proxy_source.release();
} }
JOW(void, PeerConnectionFactory_nativeInitializeVideoCapturer)
(JNIEnv* jni,
jclass,
jlong native_factory,
jobject j_video_capturer,
jlong native_source,
jobject j_frame_observer) {
LOG(LS_INFO) << "PeerConnectionFactory_nativeInitializeVideoCapturer";
rtc::scoped_refptr<PeerConnectionFactoryInterface> factory(
factoryFromJava(native_factory));
auto proxy_source =
reinterpret_cast<webrtc::VideoTrackSourceProxy*>(native_source);
auto source = reinterpret_cast<webrtc::AndroidVideoTrackSource*>(
proxy_source->internal());
rtc::scoped_refptr<SurfaceTextureHelper> surface_texture_helper =
source->surface_texture_helper();
jni->CallVoidMethod(
j_video_capturer,
GetMethodID(jni, FindClass(jni, "org/webrtc/VideoCapturer"), "initialize",
"(Lorg/webrtc/SurfaceTextureHelper;Landroid/content/"
"Context;Lorg/webrtc/VideoCapturer$CapturerObserver;)V"),
surface_texture_helper
? surface_texture_helper->GetJavaSurfaceTextureHelper()
: nullptr,
j_application_context, j_frame_observer);
CHECK_EXCEPTION(jni) << "error during VideoCapturer.initialize()";
}
JOW(jlong, PeerConnectionFactory_nativeCreateVideoTrack)( JOW(jlong, PeerConnectionFactory_nativeCreateVideoTrack)(
JNIEnv* jni, jclass, jlong native_factory, jstring id, JNIEnv* jni, jclass, jlong native_factory, jstring id,
jlong native_source) { jlong native_source) {