From 0a16916ac8b695b437ee163ce00e060a29647020 Mon Sep 17 00:00:00 2001 From: Paulina Hensman Date: Thu, 28 Feb 2019 13:54:29 +0100 Subject: [PATCH] Use JavaAudioDeviceModule as default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, we have created a Legacy ADM when no ADM is supplied. With this change we will start creating a Java ADM instead. The end goal is to make injection mandatory, and never creating ADMs. This is one step on the way, and will allow us to clean up the Legacy ADM code. Bug: webrtc:7452 Change-Id: Ib99adc50346fe6b748f9435d2fc6321a50c3ee4e Reviewed-on: https://webrtc-review.googlesource.com/c/123887 Reviewed-by: Sami Kalliomäki Reviewed-by: Magnus Jedvert Reviewed-by: Henrik Andreassson Commit-Queue: Paulina Hensman Cr-Commit-Position: refs/heads/master@{#26949} --- sdk/android/BUILD.gn | 6 +++- .../api/org/webrtc/PeerConnectionFactory.java | 10 ++++-- .../application_context_provider.cc | 24 +++++++++++++ .../application_context_provider.h | 23 +++++++++++++ .../audio_device/audio_device_unittest.cc | 34 +++++++------------ .../peer_connection_factory_unittest.cc | 9 +++-- 6 files changed, 78 insertions(+), 28 deletions(-) create mode 100644 sdk/android/native_unittests/application_context_provider.cc create mode 100644 sdk/android/native_unittests/application_context_provider.h diff --git a/sdk/android/BUILD.gn b/sdk/android/BUILD.gn index c116535365..95d81f108e 100644 --- a/sdk/android/BUILD.gn +++ b/sdk/android/BUILD.gn @@ -285,7 +285,6 @@ if (is_android) { rtc_android_library("peerconnection_java") { java_files = [ - "api/org/webrtc/audio/LegacyAudioDeviceModule.java", "api/org/webrtc/AudioProcessingFactory.java", "api/org/webrtc/AudioSource.java", "api/org/webrtc/AudioTrack.java", @@ -338,6 +337,9 @@ if (is_android) { ":base_java", ":builtin_audio_codecs_java", ":default_video_codec_factory_java", + + #TODO(bugs.webrtc.org/7452): Make injection mandatory and remove this dep. + ":java_audio_device_module_java", ":logging_java", ":swcodecs_java", ":video_api_java", @@ -1493,6 +1495,8 @@ if (is_android) { testonly = true sources = [ + "native_unittests/application_context_provider.cc", + "native_unittests/application_context_provider.h", "native_unittests/audio_device/audio_device_unittest.cc", "native_unittests/codecs/wrapper_unittest.cc", "native_unittests/java_types_unittest.cc", diff --git a/sdk/android/api/org/webrtc/PeerConnectionFactory.java b/sdk/android/api/org/webrtc/PeerConnectionFactory.java index 6d00baa2d5..7cab96c526 100644 --- a/sdk/android/api/org/webrtc/PeerConnectionFactory.java +++ b/sdk/android/api/org/webrtc/PeerConnectionFactory.java @@ -17,7 +17,7 @@ import java.util.List; import org.webrtc.Logging.Severity; import org.webrtc.PeerConnection; import org.webrtc.audio.AudioDeviceModule; -import org.webrtc.audio.LegacyAudioDeviceModule; +import org.webrtc.audio.JavaAudioDeviceModule; /** * Java wrapper for a C++ PeerConnectionFactoryInterface. Main entry point to @@ -164,7 +164,7 @@ public class PeerConnectionFactory { public static class Builder { @Nullable private Options options; - @Nullable private AudioDeviceModule audioDeviceModule = new LegacyAudioDeviceModule(); + @Nullable private AudioDeviceModule audioDeviceModule; private AudioEncoderFactoryFactory audioEncoderFactoryFactory = new BuiltinAudioEncoderFactoryFactory(); private AudioDecoderFactoryFactory audioDecoderFactoryFactory = @@ -241,8 +241,12 @@ public class PeerConnectionFactory { public PeerConnectionFactory createPeerConnectionFactory() { checkInitializeHasBeenCalled(); + if (audioDeviceModule == null) { + audioDeviceModule = JavaAudioDeviceModule.builder(ContextUtils.getApplicationContext()) + .createAudioDeviceModule(); + } return nativeCreatePeerConnectionFactory(ContextUtils.getApplicationContext(), options, - audioDeviceModule == null ? 0 : audioDeviceModule.getNativeAudioDeviceModulePointer(), + audioDeviceModule.getNativeAudioDeviceModulePointer(), audioEncoderFactoryFactory.createNativeAudioEncoderFactory(), audioDecoderFactoryFactory.createNativeAudioDecoderFactory(), videoEncoderFactory, videoDecoderFactory, diff --git a/sdk/android/native_unittests/application_context_provider.cc b/sdk/android/native_unittests/application_context_provider.cc new file mode 100644 index 0000000000..885856ea7f --- /dev/null +++ b/sdk/android/native_unittests/application_context_provider.cc @@ -0,0 +1,24 @@ +/* + * Copyright 2019 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. + */ +#include "sdk/android/native_unittests/application_context_provider.h" + +#include "sdk/android/generated_native_unittests_jni/jni/ApplicationContextProvider_jni.h" +#include "sdk/android/src/jni/jni_helpers.h" + +namespace webrtc { +namespace test { + +ScopedJavaLocalRef GetAppContextForTest(JNIEnv* jni) { + return ScopedJavaLocalRef( + jni::Java_ApplicationContextProvider_getApplicationContextForTest(jni)); +} + +} // namespace test +} // namespace webrtc diff --git a/sdk/android/native_unittests/application_context_provider.h b/sdk/android/native_unittests/application_context_provider.h new file mode 100644 index 0000000000..8aace02c32 --- /dev/null +++ b/sdk/android/native_unittests/application_context_provider.h @@ -0,0 +1,23 @@ +/* + * Copyright 2019 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. + */ +#ifndef SDK_ANDROID_NATIVE_UNITTESTS_APPLICATION_CONTEXT_PROVIDER_H_ +#define SDK_ANDROID_NATIVE_UNITTESTS_APPLICATION_CONTEXT_PROVIDER_H_ + +#include "sdk/android/src/jni/jni_helpers.h" + +namespace webrtc { +namespace test { + +ScopedJavaLocalRef GetAppContextForTest(JNIEnv* jni); + +} // namespace test +} // namespace webrtc + +#endif // SDK_ANDROID_NATIVE_UNITTESTS_APPLICATION_CONTEXT_PROVIDER_H_ diff --git a/sdk/android/native_unittests/audio_device/audio_device_unittest.cc b/sdk/android/native_unittests/audio_device/audio_device_unittest.cc index cacb06929f..97f3c59c36 100644 --- a/sdk/android/native_unittests/audio_device/audio_device_unittest.cc +++ b/sdk/android/native_unittests/audio_device/audio_device_unittest.cc @@ -19,9 +19,9 @@ #include "rtc_base/event.h" #include "rtc_base/format_macros.h" #include "rtc_base/time_utils.h" -#include "sdk/android/generated_native_unittests_jni/jni/ApplicationContextProvider_jni.h" #include "sdk/android/generated_native_unittests_jni/jni/BuildInfo_jni.h" #include "sdk/android/native_api/audio_device_module/audio_device_android.h" +#include "sdk/android/native_unittests/application_context_provider.h" #include "sdk/android/src/jni/audio_device/audio_common.h" #include "sdk/android/src/jni/audio_device/audio_device_module.h" #include "sdk/android/src/jni/audio_device/opensles_common.h" @@ -465,34 +465,24 @@ class AudioDeviceTest : public ::testing::Test { // implementations. // Creates an audio device using a default audio layer. jni_ = AttachCurrentThreadIfNeeded(); - audio_device_ = CreateJavaAudioDeviceModule(jni_, context()); + context_ = test::GetAppContextForTest(jni_); + audio_device_ = CreateJavaAudioDeviceModule(jni_, context_.obj()); EXPECT_NE(audio_device_.get(), nullptr); EXPECT_EQ(0, audio_device_->Init()); - audio_manager_ = GetAudioManager(jni_, context_javaref()); + audio_manager_ = GetAudioManager(jni_, context_); UpdateParameters(); } virtual ~AudioDeviceTest() { EXPECT_EQ(0, audio_device_->Terminate()); } int total_delay_ms() const { return 10; } - jobject context() { - return jni::NewGlobalRef( - jni_, Java_ApplicationContextProvider_getApplicationContextForTest(jni_) - .obj()); - } - - ScopedJavaLocalRef context_javaref() { - return ScopedJavaLocalRef( - Java_ApplicationContextProvider_getApplicationContextForTest(jni_)); - } - void UpdateParameters() { int sample_rate = GetDefaultSampleRate(jni_, audio_manager_); bool stereo_playout_is_available; bool stereo_record_is_available; audio_device_->StereoPlayoutIsAvailable(&stereo_playout_is_available); audio_device_->StereoRecordingIsAvailable(&stereo_record_is_available); - GetAudioParameters(jni_, context_javaref(), audio_manager_, sample_rate, + GetAudioParameters(jni_, context_, audio_manager_, sample_rate, stereo_playout_is_available, stereo_record_is_available, &input_parameters_, &output_parameters_); } @@ -524,19 +514,20 @@ class AudioDeviceTest : public ::testing::Test { #if defined(AUDIO_DEVICE_INCLUDE_ANDROID_AAUDIO) if (audio_layer == AudioDeviceModule::kAndroidAAudioAudio) { return rtc::scoped_refptr( - CreateAAudioAudioDeviceModule(jni_, context())); + CreateAAudioAudioDeviceModule(jni_, context_.obj())); } #endif if (audio_layer == AudioDeviceModule::kAndroidJavaAudio) { return rtc::scoped_refptr( - CreateJavaAudioDeviceModule(jni_, context())); + CreateJavaAudioDeviceModule(jni_, context_.obj())); } else if (audio_layer == AudioDeviceModule::kAndroidOpenSLESAudio) { return rtc::scoped_refptr( - CreateOpenSLESAudioDeviceModule(jni_, context())); + CreateOpenSLESAudioDeviceModule(jni_, context_.obj())); } else if (audio_layer == AudioDeviceModule::kAndroidJavaInputAndOpenSLESOutputAudio) { return rtc::scoped_refptr( - CreateJavaInputAndOpenSLESOutputAudioDeviceModule(jni_, context())); + CreateJavaInputAndOpenSLESOutputAudioDeviceModule(jni_, + context_.obj())); } else { return nullptr; } @@ -682,7 +673,7 @@ class AudioDeviceTest : public ::testing::Test { } JNIEnv* jni_; - std::unique_ptr> context_; + ScopedJavaLocalRef context_; rtc::Event test_is_done_; rtc::scoped_refptr audio_device_; ScopedJavaLocalRef audio_manager_; @@ -1136,8 +1127,7 @@ TEST_F(AudioDeviceTest, DISABLED_MeasureLoopbackLatency) { TEST(JavaAudioDeviceTest, TestRunningTwoAdmsSimultaneously) { JNIEnv* jni = AttachCurrentThreadIfNeeded(); - ScopedJavaLocalRef context = - Java_ApplicationContextProvider_getApplicationContextForTest(jni); + ScopedJavaLocalRef context = test::GetAppContextForTest(jni); // Create and start the first ADM. rtc::scoped_refptr adm_1 = diff --git a/sdk/android/native_unittests/peerconnection/peer_connection_factory_unittest.cc b/sdk/android/native_unittests/peerconnection/peer_connection_factory_unittest.cc index de02071bb4..29c1c8e184 100644 --- a/sdk/android/native_unittests/peerconnection/peer_connection_factory_unittest.cc +++ b/sdk/android/native_unittests/peerconnection/peer_connection_factory_unittest.cc @@ -20,7 +20,10 @@ #include "modules/audio_processing/include/audio_processing.h" #include "rtc_base/logging.h" #include "sdk/android/generated_native_unittests_jni/jni/PeerConnectionFactoryInitializationHelper_jni.h" +#include "sdk/android/native_api/audio_device_module/audio_device_android.h" #include "sdk/android/native_api/jni/jvm.h" +#include "sdk/android/native_unittests/application_context_provider.h" +#include "sdk/android/src/jni/jni_helpers.h" #include "test/gtest.h" namespace webrtc { @@ -29,6 +32,7 @@ namespace { // Create native peer connection factory, that will be wrapped by java one rtc::scoped_refptr CreateTestPCF( + JNIEnv* jni, rtc::Thread* network_thread, rtc::Thread* worker_thread, rtc::Thread* signaling_thread) { @@ -38,10 +42,11 @@ rtc::scoped_refptr CreateTestPCF( // webrtc/rtc_base/ are convoluted, we simply wrap here to avoid having to // think about ramifications of auto-wrapping there. rtc::ThreadManager::Instance()->WrapCurrentThread(); + auto adm = CreateJavaAudioDeviceModule(jni, GetAppContextForTest(jni).obj()); std::unique_ptr media_engine = cricket::WebRtcMediaEngineFactory::Create( - nullptr /* adm */, webrtc::CreateBuiltinAudioEncoderFactory(), + adm, webrtc::CreateBuiltinAudioEncoderFactory(), webrtc::CreateBuiltinAudioDecoderFactory(), absl::make_unique(), absl::make_unique(), @@ -81,7 +86,7 @@ TEST(PeerConnectionFactoryTest, NativeToJavaPeerConnectionFactory) { RTC_CHECK(signaling_thread->Start()) << "Failed to start thread"; rtc::scoped_refptr factory = - CreateTestPCF(network_thread.get(), worker_thread.get(), + CreateTestPCF(jni, network_thread.get(), worker_thread.get(), signaling_thread.get()); jobject java_factory = NativeToJavaPeerConnectionFactory(