From ad148a36ea0b73ccf64ffcfedff03a37d43a540c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sami=20Kalliom=C3=A4ki?= Date: Fri, 16 Feb 2018 12:55:55 +0100 Subject: [PATCH] Add native API for creating AndroidNetworkMonitorFactory. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Moves AndroidNetworkMonitor out of pc folder. Even clients not using PeerConnection seem to be using it and it doesn't have any dependencies to the PeerConnection API. Bug: webrtc:8769 Change-Id: I2bdeff9f5c9925e13388fbc77aa9b264a7583548 Reviewed-on: https://webrtc-review.googlesource.com/53260 Commit-Queue: Sami Kalliomäki Reviewed-by: Taylor Brandstetter Cr-Commit-Position: refs/heads/master@{#22051} --- sdk/android/BUILD.gn | 9 +- sdk/android/native_api/base/networkmonitor.cc | 25 ++++ sdk/android/native_api/base/networkmonitor.h | 29 +++++ .../src/jni/{pc => }/androidnetworkmonitor.cc | 12 +- sdk/android/src/jni/androidnetworkmonitor.h | 119 ++++++++++++++++++ .../src/jni/pc/androidnetworkmonitor.h | 107 +--------------- 6 files changed, 190 insertions(+), 111 deletions(-) create mode 100644 sdk/android/native_api/base/networkmonitor.cc create mode 100644 sdk/android/native_api/base/networkmonitor.h rename sdk/android/src/jni/{pc => }/androidnetworkmonitor.cc (97%) create mode 100644 sdk/android/src/jni/androidnetworkmonitor.h diff --git a/sdk/android/BUILD.gn b/sdk/android/BUILD.gn index 18d9987d1e..a73bed2514 100644 --- a/sdk/android/BUILD.gn +++ b/sdk/android/BUILD.gn @@ -36,6 +36,8 @@ config("libjingle_peerconnection_jni_warnings_config") { generate_jni("generated_base_jni") { sources = [ + "api/org/webrtc/NetworkMonitor.java", + "api/org/webrtc/NetworkMonitorAutoDetect.java", "src/java/org/webrtc/Histogram.java", "src/java/org/webrtc/JniCommon.java", ] @@ -87,6 +89,8 @@ rtc_source_set("base_jni") { visibility = [ "*" ] sources = [ "src/jni/androidhistogram.cc", + "src/jni/androidnetworkmonitor.cc", + "src/jni/androidnetworkmonitor.h", "src/jni/class_loader.h", "src/jni/classreferenceholder.h", "src/jni/jni_common.cc", @@ -411,8 +415,6 @@ generate_jni("generated_peerconnection_jni") { "api/org/webrtc/MediaSource.java", "api/org/webrtc/MediaStream.java", "api/org/webrtc/MediaStreamTrack.java", - "api/org/webrtc/NetworkMonitor.java", - "api/org/webrtc/NetworkMonitorAutoDetect.java", "api/org/webrtc/PeerConnection.java", "api/org/webrtc/PeerConnectionFactory.java", "api/org/webrtc/RTCStats.java", @@ -439,7 +441,6 @@ rtc_static_library("peerconnection_jni") { sources = [ "src/jni/androidnetworkmonitor_jni.h", - "src/jni/pc/androidnetworkmonitor.cc", "src/jni/pc/androidnetworkmonitor.h", "src/jni/pc/audiotrack.cc", "src/jni/pc/callsessionfilerotatinglogsink.cc", @@ -818,6 +819,8 @@ rtc_static_library("native_api_base") { sources = [ "native_api/base/init.cc", "native_api/base/init.h", + "native_api/base/networkmonitor.cc", + "native_api/base/networkmonitor.h", ] deps = [ diff --git a/sdk/android/native_api/base/networkmonitor.cc b/sdk/android/native_api/base/networkmonitor.cc new file mode 100644 index 0000000000..786c533c40 --- /dev/null +++ b/sdk/android/native_api/base/networkmonitor.cc @@ -0,0 +1,25 @@ +/* + * Copyright 2018 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_api/base/networkmonitor.h" + +#include + +#include "rtc_base/ptr_util.h" +#include "sdk/android/src/jni/androidnetworkmonitor.h" + +namespace webrtc { + +std::unique_ptr +CreateAndroidNetworkMonitorFactory() { + return rtc::MakeUnique(); +} + +} // namespace webrtc diff --git a/sdk/android/native_api/base/networkmonitor.h b/sdk/android/native_api/base/networkmonitor.h new file mode 100644 index 0000000000..3301bff7dd --- /dev/null +++ b/sdk/android/native_api/base/networkmonitor.h @@ -0,0 +1,29 @@ +/* + * Copyright 2018 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_API_BASE_NETWORKMONITOR_H_ +#define SDK_ANDROID_NATIVE_API_BASE_NETWORKMONITOR_H_ + +#include + +#include "rtc_base/networkmonitor.h" + +namespace webrtc { + +// Creates an Android-specific network monitor, which is capable of detecting +// network changes as soon as they occur, requesting a cellular interface +// (dependent on permissions), and binding sockets to network interfaces (more +// reliable than binding to IP addresses on Android). +std::unique_ptr +CreateAndroidNetworkMonitorFactory(); + +} // namespace webrtc + +#endif // SDK_ANDROID_NATIVE_API_BASE_NETWORKMONITOR_H_ diff --git a/sdk/android/src/jni/pc/androidnetworkmonitor.cc b/sdk/android/src/jni/androidnetworkmonitor.cc similarity index 97% rename from sdk/android/src/jni/pc/androidnetworkmonitor.cc rename to sdk/android/src/jni/androidnetworkmonitor.cc index 742acc0e2d..9a4eb18bfc 100644 --- a/sdk/android/src/jni/pc/androidnetworkmonitor.cc +++ b/sdk/android/src/jni/androidnetworkmonitor.cc @@ -8,7 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ -#include "sdk/android/src/jni/pc/androidnetworkmonitor.h" +#include "sdk/android/src/jni/androidnetworkmonitor.h" #include #ifndef RTLD_NOLOAD @@ -19,8 +19,8 @@ #include "rtc_base/bind.h" #include "rtc_base/checks.h" #include "rtc_base/ipaddress.h" -#include "sdk/android/generated_peerconnection_jni/jni/NetworkMonitorAutoDetect_jni.h" -#include "sdk/android/generated_peerconnection_jni/jni/NetworkMonitor_jni.h" +#include "sdk/android/generated_base_jni/jni/NetworkMonitorAutoDetect_jni.h" +#include "sdk/android/generated_base_jni/jni/NetworkMonitor_jni.h" #include "sdk/android/native_api/jni/java_types.h" #include "sdk/android/src/jni/jni_helpers.h" @@ -130,6 +130,10 @@ static NetworkInformation GetNetworkInformationFromJava( return network_info; } +NetworkInformation::NetworkInformation() = default; + +NetworkInformation::~NetworkInformation() = default; + std::string NetworkInformation::ToString() const { std::stringstream ss; ss << "NetInfo[name " << interface_name << "; handle " << handle << "; type " @@ -145,6 +149,8 @@ AndroidNetworkMonitor::AndroidNetworkMonitor(JNIEnv* env) : android_sdk_int_(Java_NetworkMonitor_androidSdkInt(env)), j_network_monitor_(env, Java_NetworkMonitor_getInstance(env)) {} +AndroidNetworkMonitor::~AndroidNetworkMonitor() = default; + void AndroidNetworkMonitor::Start() { RTC_CHECK(thread_checker_.CalledOnValidThread()); if (started_) { diff --git a/sdk/android/src/jni/androidnetworkmonitor.h b/sdk/android/src/jni/androidnetworkmonitor.h new file mode 100644 index 0000000000..ae052bfa70 --- /dev/null +++ b/sdk/android/src/jni/androidnetworkmonitor.h @@ -0,0 +1,119 @@ +/* + * Copyright 2015 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_SRC_JNI_ANDROIDNETWORKMONITOR_H_ +#define SDK_ANDROID_SRC_JNI_ANDROIDNETWORKMONITOR_H_ + +#include +#include +#include +#include + +#include "rtc_base/networkmonitor.h" +#include "rtc_base/thread_checker.h" +#include "sdk/android/src/jni/jni_helpers.h" + +namespace webrtc { +namespace jni { + +typedef int64_t NetworkHandle; + +// c++ equivalent of java NetworkMonitorAutoDetect.ConnectionType. +enum NetworkType { + NETWORK_UNKNOWN, + NETWORK_ETHERNET, + NETWORK_WIFI, + NETWORK_4G, + NETWORK_3G, + NETWORK_2G, + NETWORK_UNKNOWN_CELLULAR, + NETWORK_BLUETOOTH, + NETWORK_NONE +}; + +// The information is collected from Android OS so that the native code can get +// the network type and handle (Android network ID) for each interface. +struct NetworkInformation { + std::string interface_name; + NetworkHandle handle; + NetworkType type; + std::vector ip_addresses; + + NetworkInformation(); + ~NetworkInformation(); + + std::string ToString() const; +}; + +class AndroidNetworkMonitor : public rtc::NetworkMonitorBase, + public rtc::NetworkBinderInterface { + public: + explicit AndroidNetworkMonitor(JNIEnv* env); + ~AndroidNetworkMonitor() override; + + // TODO(sakal): Remove once down stream dependencies have been updated. + static void SetAndroidContext(JNIEnv* jni, jobject context) {} + + void Start() override; + void Stop() override; + + rtc::NetworkBindingResult BindSocketToNetwork( + int socket_fd, + const rtc::IPAddress& address) override; + rtc::AdapterType GetAdapterType(const std::string& if_name) override; + void OnNetworkConnected(const NetworkInformation& network_info); + void OnNetworkDisconnected(NetworkHandle network_handle); + // Always expected to be called on the network thread. + void SetNetworkInfos(const std::vector& network_infos); + + void NotifyConnectionTypeChanged(JNIEnv* env, + const JavaRef& j_caller); + void NotifyOfNetworkConnect(JNIEnv* env, + const JavaRef& j_caller, + const JavaRef& j_network_info); + void NotifyOfNetworkDisconnect(JNIEnv* env, + const JavaRef& j_caller, + jlong network_handle); + void NotifyOfActiveNetworkList(JNIEnv* env, + const JavaRef& j_caller, + const JavaRef& j_network_infos); + + private: + void OnNetworkConnected_w(const NetworkInformation& network_info); + void OnNetworkDisconnected_w(NetworkHandle network_handle); + + const int android_sdk_int_; + ScopedJavaGlobalRef j_network_monitor_; + rtc::ThreadChecker thread_checker_; + bool started_ = false; + std::map adapter_type_by_name_; + std::map network_handle_by_address_; + std::map network_info_by_handle_; +}; + +class AndroidNetworkMonitorFactory : public rtc::NetworkMonitorFactory { + public: + AndroidNetworkMonitorFactory() {} + + rtc::NetworkMonitorInterface* CreateNetworkMonitor() override; +}; + +} // namespace jni +} // namespace webrtc + +// TODO(magjed): Remove once external clients are updated. +namespace webrtc_jni { + +using webrtc::jni::AndroidNetworkMonitor; +using webrtc::jni::AndroidNetworkMonitorFactory; + +} // namespace webrtc_jni + +#endif // SDK_ANDROID_SRC_JNI_ANDROIDNETWORKMONITOR_H_ diff --git a/sdk/android/src/jni/pc/androidnetworkmonitor.h b/sdk/android/src/jni/pc/androidnetworkmonitor.h index 0d99a13e70..3eee11b3a5 100644 --- a/sdk/android/src/jni/pc/androidnetworkmonitor.h +++ b/sdk/android/src/jni/pc/androidnetworkmonitor.h @@ -8,108 +8,5 @@ * be found in the AUTHORS file in the root of the source tree. */ -#ifndef SDK_ANDROID_SRC_JNI_PC_ANDROIDNETWORKMONITOR_H_ -#define SDK_ANDROID_SRC_JNI_PC_ANDROIDNETWORKMONITOR_H_ - -#include -#include -#include -#include - -#include "rtc_base/networkmonitor.h" -#include "rtc_base/thread_checker.h" -#include "sdk/android/src/jni/jni_helpers.h" - -namespace webrtc { -namespace jni { - -typedef int64_t NetworkHandle; - -// c++ equivalent of java NetworkMonitorAutoDetect.ConnectionType. -enum NetworkType { - NETWORK_UNKNOWN, - NETWORK_ETHERNET, - NETWORK_WIFI, - NETWORK_4G, - NETWORK_3G, - NETWORK_2G, - NETWORK_UNKNOWN_CELLULAR, - NETWORK_BLUETOOTH, - NETWORK_NONE -}; - -// The information is collected from Android OS so that the native code can get -// the network type and handle (Android network ID) for each interface. -struct NetworkInformation { - std::string interface_name; - NetworkHandle handle; - NetworkType type; - std::vector ip_addresses; - - std::string ToString() const; -}; - -class AndroidNetworkMonitor : public rtc::NetworkMonitorBase, - public rtc::NetworkBinderInterface { - public: - explicit AndroidNetworkMonitor(JNIEnv* env); - - // TODO(sakal): Remove once down stream dependencies have been updated. - static void SetAndroidContext(JNIEnv* jni, jobject context) {} - - void Start() override; - void Stop() override; - - rtc::NetworkBindingResult BindSocketToNetwork( - int socket_fd, - const rtc::IPAddress& address) override; - rtc::AdapterType GetAdapterType(const std::string& if_name) override; - void OnNetworkConnected(const NetworkInformation& network_info); - void OnNetworkDisconnected(NetworkHandle network_handle); - // Always expected to be called on the network thread. - void SetNetworkInfos(const std::vector& network_infos); - - void NotifyConnectionTypeChanged(JNIEnv* env, - const JavaRef& j_caller); - void NotifyOfNetworkConnect(JNIEnv* env, - const JavaRef& j_caller, - const JavaRef& j_network_info); - void NotifyOfNetworkDisconnect(JNIEnv* env, - const JavaRef& j_caller, - jlong network_handle); - void NotifyOfActiveNetworkList(JNIEnv* env, - const JavaRef& j_caller, - const JavaRef& j_network_infos); - - private: - void OnNetworkConnected_w(const NetworkInformation& network_info); - void OnNetworkDisconnected_w(NetworkHandle network_handle); - - const int android_sdk_int_; - ScopedJavaGlobalRef j_network_monitor_; - rtc::ThreadChecker thread_checker_; - bool started_ = false; - std::map adapter_type_by_name_; - std::map network_handle_by_address_; - std::map network_info_by_handle_; -}; - -class AndroidNetworkMonitorFactory : public rtc::NetworkMonitorFactory { - public: - AndroidNetworkMonitorFactory() {} - - rtc::NetworkMonitorInterface* CreateNetworkMonitor() override; -}; - -} // namespace jni -} // namespace webrtc - -// TODO(magjed): Remove once external clients are updated. -namespace webrtc_jni { - -using webrtc::jni::AndroidNetworkMonitor; -using webrtc::jni::AndroidNetworkMonitorFactory; - -} // namespace webrtc_jni - -#endif // SDK_ANDROID_SRC_JNI_PC_ANDROIDNETWORKMONITOR_H_ +// TODO(sakal): Remove this file once clients have update to the native API. +#include "sdk/android/src/jni/androidnetworkmonitor.h"