From 4e3832124fe2fb3cf69e9bd20d441e7dd6faedd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sami=20Kalliom=C3=A4ki?= Date: Wed, 15 Nov 2017 14:15:24 +0100 Subject: [PATCH] Do not initialize internal tracer if it has already been initialized. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: b/68989834 Change-Id: I7bb02d58cef5c14c6433d1fc7a95b46ff2b27f6f Reviewed-on: https://webrtc-review.googlesource.com/23280 Reviewed-by: Magnus Jedvert Commit-Queue: Sami Kalliomäki Cr-Commit-Position: refs/heads/master@{#20696} --- sdk/android/BUILD.gn | 1 + .../api/org/webrtc/PeerConnectionFactory.java | 19 +++++- .../org/webrtc/PeerConnectionFactoryTest.java | 62 +++++++++++++++++++ .../src/jni/pc/peerconnectionfactory_jni.cc | 4 +- 4 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 sdk/android/instrumentationtests/src/org/webrtc/PeerConnectionFactoryTest.java diff --git a/sdk/android/BUILD.gn b/sdk/android/BUILD.gn index e1c4df2e64..b3a7894a9e 100644 --- a/sdk/android/BUILD.gn +++ b/sdk/android/BUILD.gn @@ -568,6 +568,7 @@ if (rtc_include_tests) { "instrumentationtests/src/org/webrtc/HardwareVideoEncoderTest.java", "instrumentationtests/src/org/webrtc/MediaCodecVideoEncoderTest.java", "instrumentationtests/src/org/webrtc/NetworkMonitorTest.java", + "instrumentationtests/src/org/webrtc/PeerConnectionFactoryTest.java", "instrumentationtests/src/org/webrtc/PeerConnectionTest.java", "instrumentationtests/src/org/webrtc/RendererCommonTest.java", "instrumentationtests/src/org/webrtc/SurfaceTextureHelperTest.java", diff --git a/sdk/android/api/org/webrtc/PeerConnectionFactory.java b/sdk/android/api/org/webrtc/PeerConnectionFactory.java index d4b158d17f..de09a7c79d 100644 --- a/sdk/android/api/org/webrtc/PeerConnectionFactory.java +++ b/sdk/android/api/org/webrtc/PeerConnectionFactory.java @@ -29,7 +29,9 @@ public class PeerConnectionFactory { private static final String TAG = "PeerConnectionFactory"; private static final String VIDEO_CAPTURER_THREAD_NAME = "VideoCapturerThread"; + private final long nativeFactory; + private static volatile boolean internalTracerInitialized = false; private static Context applicationContext; private static Thread networkThread; private static Thread workerThread; @@ -120,7 +122,7 @@ public class PeerConnectionFactory { NativeLibrary.initialize(options.nativeLibraryLoader); nativeInitializeAndroidGlobals(options.applicationContext, options.enableVideoHwAcceleration); initializeFieldTrials(options.fieldTrials); - if (options.enableInternalTracer) { + if (options.enableInternalTracer && !internalTracerInitialized) { initializeInternalTracer(); } } @@ -154,6 +156,17 @@ public class PeerConnectionFactory { return true; } + @Deprecated + public static void initializeInternalTracer() { + internalTracerInitialized = true; + nativeInitializeInternalTracer(); + } + + public static void shutdownInternalTracer() { + internalTracerInitialized = false; + nativeShutdownInternalTracer(); + } + // Field trial initialization. Must be called before PeerConnectionFactory // is created. // Deprecated, use PeerConnectionFactory.initialize instead. @@ -172,10 +185,10 @@ public class PeerConnectionFactory { // Internal tracing initialization. Must be called before PeerConnectionFactory is created to // prevent racing with tracing code. // Deprecated, use PeerConnectionFactory.initialize instead. - @Deprecated public static native void initializeInternalTracer(); + 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. - public static native void shutdownInternalTracer(); + private static native void nativeShutdownInternalTracer(); // Start/stop internal capturing of internal tracing. public static native boolean startInternalTracingCapture(String tracing_filename); public static native void stopInternalTracingCapture(); diff --git a/sdk/android/instrumentationtests/src/org/webrtc/PeerConnectionFactoryTest.java b/sdk/android/instrumentationtests/src/org/webrtc/PeerConnectionFactoryTest.java new file mode 100644 index 0000000000..01e618dabb --- /dev/null +++ b/sdk/android/instrumentationtests/src/org/webrtc/PeerConnectionFactoryTest.java @@ -0,0 +1,62 @@ +/* + * Copyright 2017 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +package org.webrtc; + +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +public class PeerConnectionFactoryTest { + @SmallTest + @Test + public void testInitialize() { + PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions + .builder(InstrumentationRegistry.getTargetContext()) + .createInitializationOptions()); + } + + @SmallTest + @Test + public void testInitializeTwice() { + PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions + .builder(InstrumentationRegistry.getTargetContext()) + .createInitializationOptions()); + PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions + .builder(InstrumentationRegistry.getTargetContext()) + .createInitializationOptions()); + } + + @SmallTest + @Test + public void testInitializeTwiceWithTracer() { + PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions + .builder(InstrumentationRegistry.getTargetContext()) + .setEnableInternalTracer(true) + .createInitializationOptions()); + PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions + .builder(InstrumentationRegistry.getTargetContext()) + .setEnableInternalTracer(true) + .createInitializationOptions()); + } + + @SmallTest + @Test + public void testInitializeWithTracerAndShutdown() { + PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions + .builder(InstrumentationRegistry.getTargetContext()) + .setEnableInternalTracer(true) + .createInitializationOptions()); + PeerConnectionFactory.shutdownInternalTracer(); + } +} diff --git a/sdk/android/src/jni/pc/peerconnectionfactory_jni.cc b/sdk/android/src/jni/pc/peerconnectionfactory_jni.cc index a1b9b5e85a..e34d1fda97 100644 --- a/sdk/android/src/jni/pc/peerconnectionfactory_jni.cc +++ b/sdk/android/src/jni/pc/peerconnectionfactory_jni.cc @@ -93,7 +93,7 @@ JNI_FUNCTION_DECLARATION(void, } JNI_FUNCTION_DECLARATION(void, - PeerConnectionFactory_initializeInternalTracer, + PeerConnectionFactory_nativeInitializeInternalTracer, JNIEnv* jni, jclass) { rtc::tracing::SetupInternalTracer(); @@ -132,7 +132,7 @@ JNI_FUNCTION_DECLARATION(void, } JNI_FUNCTION_DECLARATION(void, - PeerConnectionFactory_shutdownInternalTracer, + PeerConnectionFactory_nativeShutdownInternalTracer, JNIEnv* jni, jclass) { rtc::tracing::ShutdownInternalTracer();