Android: Use scoped java refs

We currently use raw jobject in our code mixed with sporadic
ScopedLocalRefFrame. This CL moves every jobject into a scoped object,
either local, global, or a parameter. Also, this CL uses the JNI
generation script to generate declaration stubs for the Java->C++
functions so that it no longer becomes possible to mistype them
without getting compilation errors.

TBR=brandt@webrtc.org

Bug: webrtc:8278,webrtc:6969
Change-Id: Ic7bac74a89c11180177d65041086d7db1cdfb516
Reviewed-on: https://webrtc-review.googlesource.com/34655
Commit-Queue: Magnus Jedvert <magjed@webrtc.org>
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21387}
This commit is contained in:
Magnus Jedvert
2017-12-20 15:12:10 +01:00
committed by Commit Bot
parent ec22e3f503
commit 84d8ae5df7
128 changed files with 2324 additions and 1958 deletions

View File

@ -17,6 +17,7 @@ import java.util.List;
* Java wrapper for a C++ PeerConnectionFactoryInterface. Main entry point to
* the PeerConnection API for clients.
*/
@JNINamespace("webrtc::jni")
public class PeerConnectionFactory {
public static final String TRIAL_ENABLED = "Enabled";
public static final String VIDEO_FRAME_EMIT_TRIAL = "VideoFrameEmit";
@ -129,7 +130,7 @@ public class PeerConnectionFactory {
public static void initialize(InitializationOptions options) {
ContextUtils.initialize(options.applicationContext);
NativeLibrary.initialize(options.nativeLibraryLoader);
initializeNativeAndroidGlobals(options.applicationContext, options.enableVideoHwAcceleration);
nativeInitializeAndroidGlobals(options.applicationContext, options.enableVideoHwAcceleration);
initializeFieldTrials(options.fieldTrials);
if (options.enableInternalTracer && !internalTracerInitialized) {
initializeInternalTracer();
@ -144,25 +145,24 @@ public class PeerConnectionFactory {
}
}
// Must be called at least once before creating a PeerConnectionFactory
// (for example, at application startup time).
private static native void initializeNativeAndroidGlobals(
Context context, boolean videoHwAcceleration);
private static void initializeInternalTracer() {
internalTracerInitialized = true;
initializeNativeInternalTracer();
nativeInitializeInternalTracer();
}
public static void shutdownInternalTracer() {
internalTracerInitialized = false;
shutdownNativeInternalTracer();
nativeShutdownInternalTracer();
}
// Field trial initialization. Must be called before PeerConnectionFactory
// is created.
// Deprecated, use PeerConnectionFactory.initialize instead.
@Deprecated public static native void initializeFieldTrials(String fieldTrialsInitString);
@Deprecated
public static void initializeFieldTrials(String fieldTrialsInitString) {
nativeInitializeFieldTrials(fieldTrialsInitString);
}
// Wrapper of webrtc::field_trial::FindFullName. Develop the feature with default behaviour off.
// Example usage:
// if (PeerConnectionFactory.fieldTrialsFindFullName("WebRTCExperiment").equals("Enabled")) {
@ -171,19 +171,16 @@ public class PeerConnectionFactory {
// method2();
// }
public static String fieldTrialsFindFullName(String name) {
return NativeLibrary.isLoaded() ? findNativeFieldTrialsFullName(name) : "";
return NativeLibrary.isLoaded() ? nativeFindFieldTrialsFullName(name) : "";
}
private static native String findNativeFieldTrialsFullName(String name);
// Internal tracing initialization. Must be called before PeerConnectionFactory is created to
// prevent racing with tracing code.
// Deprecated, use PeerConnectionFactory.initialize instead.
private static native void initializeNativeInternalTracer();
// Internal tracing shutdown, called to prevent resource leaks. Must be called after
// PeerConnectionFactory is gone to prevent races with code performing tracing.
private static native void shutdownNativeInternalTracer();
// Start/stop internal capturing of internal tracing.
public static native boolean startInternalTracingCapture(String tracing_filename);
public static native void stopInternalTracingCapture();
public static boolean startInternalTracingCapture(String tracingFilename) {
return nativeStartInternalTracingCapture(tracingFilename);
}
public static void stopInternalTracingCapture() {
nativeStopInternalTracingCapture();
}
@Deprecated
public PeerConnectionFactory() {
@ -199,7 +196,7 @@ public class PeerConnectionFactory {
public PeerConnectionFactory(
Options options, VideoEncoderFactory encoderFactory, VideoDecoderFactory decoderFactory) {
checkInitializeHasBeenCalled();
nativeFactory = createNativePeerConnectionFactory(options, encoderFactory, decoderFactory);
nativeFactory = nativeCreatePeerConnectionFactory(options, encoderFactory, decoderFactory);
if (nativeFactory == 0) {
throw new RuntimeException("Failed to initialize PeerConnectionFactory!");
}
@ -212,7 +209,7 @@ public class PeerConnectionFactory {
throw new NullPointerException(
"PeerConnectionFactory constructor does not accept a null AudioProcessingFactory.");
}
nativeFactory = createNativePeerConnectionFactoryWithAudioProcessing(
nativeFactory = nativeCreatePeerConnectionFactoryWithAudioProcessing(
options, encoderFactory, decoderFactory, audioProcessingFactory.createNative());
if (nativeFactory == 0) {
throw new RuntimeException("Failed to initialize PeerConnectionFactory!");
@ -231,7 +228,7 @@ public class PeerConnectionFactory {
return null;
}
long nativePeerConnection =
createNativePeerConnection(nativeFactory, rtcConfig, constraints, nativeObserver);
nativeCreatePeerConnection(nativeFactory, rtcConfig, constraints, nativeObserver);
if (nativePeerConnection == 0) {
return null;
}
@ -261,7 +258,7 @@ public class PeerConnectionFactory {
}
public MediaStream createLocalMediaStream(String label) {
return new MediaStream(createNativeLocalMediaStream(nativeFactory, label));
return new MediaStream(nativeCreateLocalMediaStream(nativeFactory, label));
}
public VideoSource createVideoSource(VideoCapturer capturer) {
@ -270,7 +267,7 @@ public class PeerConnectionFactory {
final SurfaceTextureHelper surfaceTextureHelper =
SurfaceTextureHelper.create(VIDEO_CAPTURER_THREAD_NAME, eglContext);
long nativeAndroidVideoTrackSource =
createNativeVideoSource(nativeFactory, surfaceTextureHelper, capturer.isScreencast());
nativeCreateVideoSource(nativeFactory, surfaceTextureHelper, capturer.isScreencast());
VideoCapturer.CapturerObserver capturerObserver =
new AndroidVideoTrackSourceObserver(nativeAndroidVideoTrackSource);
capturer.initialize(
@ -279,33 +276,33 @@ public class PeerConnectionFactory {
}
public VideoTrack createVideoTrack(String id, VideoSource source) {
return new VideoTrack(createNativeVideoTrack(nativeFactory, id, source.nativeSource));
return new VideoTrack(nativeCreateVideoTrack(nativeFactory, id, source.nativeSource));
}
public AudioSource createAudioSource(MediaConstraints constraints) {
return new AudioSource(createNativeAudioSource(nativeFactory, constraints));
return new AudioSource(nativeCreateAudioSource(nativeFactory, constraints));
}
public AudioTrack createAudioTrack(String id, AudioSource source) {
return new AudioTrack(createNativeAudioTrack(nativeFactory, id, source.nativeSource));
return new AudioTrack(nativeCreateAudioTrack(nativeFactory, id, source.nativeSource));
}
// Starts recording an AEC dump. Ownership of the file is transfered to the
// native code. If an AEC dump is already in progress, it will be stopped and
// a new one will start using the provided file.
public boolean startAecDump(int file_descriptor, int filesize_limit_bytes) {
return startNativeAecDump(nativeFactory, file_descriptor, filesize_limit_bytes);
return nativeStartAecDump(nativeFactory, file_descriptor, filesize_limit_bytes);
}
// Stops recording an AEC dump. If no AEC dump is currently being recorded,
// this call will have no effect.
public void stopAecDump() {
stopNativeAecDump(nativeFactory);
nativeStopAecDump(nativeFactory);
}
@Deprecated
public void setOptions(Options options) {
setNativeOptions(nativeFactory, options);
nativeSetOptions(nativeFactory, options);
}
/** Set the EGL context used by HW Video encoding and decoding.
@ -326,12 +323,12 @@ public class PeerConnectionFactory {
}
localEglbase = EglBase.create(localEglContext);
remoteEglbase = EglBase.create(remoteEglContext);
setNativeVideoHwAccelerationOptions(
nativeSetVideoHwAccelerationOptions(
nativeFactory, localEglbase.getEglBaseContext(), remoteEglbase.getEglBaseContext());
}
public void dispose() {
freeNativeFactory(nativeFactory);
nativeFreeFactory(nativeFactory);
networkThread = null;
workerThread = null;
signalingThread = null;
@ -342,7 +339,7 @@ public class PeerConnectionFactory {
}
public void threadsCallbacks() {
invokeNativeThreadsCallbacks(nativeFactory);
nativeInvokeThreadsCallbacks(nativeFactory);
}
private static void printStackTrace(Thread thread, String threadName) {
@ -381,46 +378,41 @@ public class PeerConnectionFactory {
Logging.d(TAG, "onSignalingThreadReady");
}
private static native long createNativePeerConnectionFactory(
// Must be called at least once before creating a PeerConnectionFactory
// (for example, at application startup time).
private static native void nativeInitializeAndroidGlobals(
Context context, boolean videoHwAcceleration);
private static native void nativeInitializeFieldTrials(String fieldTrialsInitString);
private static native String nativeFindFieldTrialsFullName(String name);
// Internal tracing initialization. Must be called before PeerConnectionFactory is created to
// prevent racing with tracing code.
// Deprecated, use PeerConnectionFactory.initialize instead.
private static native void nativeInitializeInternalTracer();
// Internal tracing shutdown, called to prevent resource leaks. Must be called after
// PeerConnectionFactory is gone to prevent races with code performing tracing.
private static native void nativeShutdownInternalTracer();
private static native boolean nativeStartInternalTracingCapture(String tracingFilename);
private static native void nativeStopInternalTracingCapture();
private static native long nativeCreatePeerConnectionFactory(
Options options, VideoEncoderFactory encoderFactory, VideoDecoderFactory decoderFactory);
private static native long createNativePeerConnectionFactoryWithAudioProcessing(Options options,
private static native long nativeCreatePeerConnectionFactoryWithAudioProcessing(Options options,
VideoEncoderFactory encoderFactory, VideoDecoderFactory decoderFactory,
long nativeAudioProcessor);
private static native long createNativePeerConnection(long nativeFactory,
private static native long nativeCreatePeerConnection(long factory,
PeerConnection.RTCConfiguration rtcConfig, MediaConstraints constraints, long nativeObserver);
private static native long createNativeLocalMediaStream(long nativeFactory, String label);
private static native long createNativeVideoSource(
long nativeFactory, SurfaceTextureHelper surfaceTextureHelper, boolean is_screencast);
private static native long createNativeVideoTrack(
long nativeFactory, String id, long nativeVideoSource);
private static native long createNativeAudioSource(
long nativeFactory, MediaConstraints constraints);
private static native long createNativeAudioTrack(
long nativeFactory, String id, long nativeSource);
private static native boolean startNativeAecDump(
long nativeFactory, int file_descriptor, int filesize_limit_bytes);
private static native void stopNativeAecDump(long nativeFactory);
@Deprecated
public void nativeSetOptions(long nativeFactory, Options options) {
setNativeOptions(nativeFactory, options);
}
private native void setNativeOptions(long nativeFactory, Options options);
private static native void setNativeVideoHwAccelerationOptions(
long nativeFactory, Object localEGLContext, Object remoteEGLContext);
private static native void invokeNativeThreadsCallbacks(long nativeFactory);
private static native void freeNativeFactory(long nativeFactory);
private static native long nativeCreateLocalMediaStream(long factory, String label);
private static native long nativeCreateVideoSource(
long factory, SurfaceTextureHelper surfaceTextureHelper, boolean is_screencast);
private static native long nativeCreateVideoTrack(
long factory, String id, long nativeVideoSource);
private static native long nativeCreateAudioSource(long factory, MediaConstraints constraints);
private static native long nativeCreateAudioTrack(long factory, String id, long nativeSource);
private static native boolean nativeStartAecDump(
long factory, int file_descriptor, int filesize_limit_bytes);
private static native void nativeStopAecDump(long factory);
@Deprecated public native void nativeSetOptions(long factory, Options options);
private static native void nativeSetVideoHwAccelerationOptions(
long factory, Object localEGLContext, Object remoteEGLContext);
private static native void nativeInvokeThreadsCallbacks(long factory);
private static native void nativeFreeFactory(long factory);
}