• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2017 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "sdk/android/src/jni/pc/ice_candidate.h"
12 
13 #include <string>
14 
15 #include "pc/webrtc_sdp.h"
16 #include "sdk/android/generated_peerconnection_jni/IceCandidate_jni.h"
17 #include "sdk/android/native_api/jni/java_types.h"
18 #include "sdk/android/src/jni/pc/media_stream_track.h"
19 #include "sdk/android/src/jni/pc/peer_connection.h"
20 
21 namespace webrtc {
22 namespace jni {
23 
24 namespace {
25 
CreateJavaIceCandidate(JNIEnv * env,const std::string & sdp_mid,int sdp_mline_index,const std::string & sdp,const std::string server_url,int adapterType)26 ScopedJavaLocalRef<jobject> CreateJavaIceCandidate(JNIEnv* env,
27                                                    const std::string& sdp_mid,
28                                                    int sdp_mline_index,
29                                                    const std::string& sdp,
30                                                    const std::string server_url,
31                                                    int adapterType) {
32   return Java_IceCandidate_Constructor(
33       env, NativeToJavaString(env, sdp_mid), sdp_mline_index,
34       NativeToJavaString(env, sdp), NativeToJavaString(env, server_url),
35       NativeToJavaAdapterType(env, adapterType));
36 }
37 
38 }  // namespace
39 
JavaToNativeCandidate(JNIEnv * jni,const JavaRef<jobject> & j_candidate)40 cricket::Candidate JavaToNativeCandidate(JNIEnv* jni,
41                                          const JavaRef<jobject>& j_candidate) {
42   std::string sdp_mid =
43       JavaToStdString(jni, Java_IceCandidate_getSdpMid(jni, j_candidate));
44   std::string sdp =
45       JavaToStdString(jni, Java_IceCandidate_getSdp(jni, j_candidate));
46   cricket::Candidate candidate;
47   if (!SdpDeserializeCandidate(sdp_mid, sdp, &candidate, NULL)) {
48     RTC_LOG(LS_ERROR) << "SdpDescrializeCandidate failed with sdp " << sdp;
49   }
50   return candidate;
51 }
52 
NativeToJavaCandidate(JNIEnv * env,const cricket::Candidate & candidate)53 ScopedJavaLocalRef<jobject> NativeToJavaCandidate(
54     JNIEnv* env,
55     const cricket::Candidate& candidate) {
56   std::string sdp = SdpSerializeCandidate(candidate);
57   RTC_CHECK(!sdp.empty()) << "got an empty ICE candidate";
58   // sdp_mline_index is not used, pass an invalid value -1.
59   return CreateJavaIceCandidate(env, candidate.transport_name(),
60                                 -1 /* sdp_mline_index */, sdp,
61                                 "" /* server_url */, candidate.network_type());
62 }
63 
NativeToJavaIceCandidate(JNIEnv * env,const IceCandidateInterface & candidate)64 ScopedJavaLocalRef<jobject> NativeToJavaIceCandidate(
65     JNIEnv* env,
66     const IceCandidateInterface& candidate) {
67   std::string sdp;
68   RTC_CHECK(candidate.ToString(&sdp)) << "got so far: " << sdp;
69   return CreateJavaIceCandidate(env, candidate.sdp_mid(),
70                                 candidate.sdp_mline_index(), sdp,
71                                 candidate.candidate().url(), 0);
72 }
73 
NativeToJavaCandidateArray(JNIEnv * jni,const std::vector<cricket::Candidate> & candidates)74 ScopedJavaLocalRef<jobjectArray> NativeToJavaCandidateArray(
75     JNIEnv* jni,
76     const std::vector<cricket::Candidate>& candidates) {
77   return NativeToJavaObjectArray(jni, candidates,
78                                  org_webrtc_IceCandidate_clazz(jni),
79                                  &NativeToJavaCandidate);
80 }
81 
JavaToNativeIceTransportsType(JNIEnv * jni,const JavaRef<jobject> & j_ice_transports_type)82 PeerConnectionInterface::IceTransportsType JavaToNativeIceTransportsType(
83     JNIEnv* jni,
84     const JavaRef<jobject>& j_ice_transports_type) {
85   std::string enum_name = GetJavaEnumName(jni, j_ice_transports_type);
86 
87   if (enum_name == "ALL")
88     return PeerConnectionInterface::kAll;
89 
90   if (enum_name == "RELAY")
91     return PeerConnectionInterface::kRelay;
92 
93   if (enum_name == "NOHOST")
94     return PeerConnectionInterface::kNoHost;
95 
96   if (enum_name == "NONE")
97     return PeerConnectionInterface::kNone;
98 
99   RTC_CHECK(false) << "Unexpected IceTransportsType enum_name " << enum_name;
100   return PeerConnectionInterface::kAll;
101 }
102 
JavaToNativeBundlePolicy(JNIEnv * jni,const JavaRef<jobject> & j_bundle_policy)103 PeerConnectionInterface::BundlePolicy JavaToNativeBundlePolicy(
104     JNIEnv* jni,
105     const JavaRef<jobject>& j_bundle_policy) {
106   std::string enum_name = GetJavaEnumName(jni, j_bundle_policy);
107 
108   if (enum_name == "BALANCED")
109     return PeerConnectionInterface::kBundlePolicyBalanced;
110 
111   if (enum_name == "MAXBUNDLE")
112     return PeerConnectionInterface::kBundlePolicyMaxBundle;
113 
114   if (enum_name == "MAXCOMPAT")
115     return PeerConnectionInterface::kBundlePolicyMaxCompat;
116 
117   RTC_CHECK(false) << "Unexpected BundlePolicy enum_name " << enum_name;
118   return PeerConnectionInterface::kBundlePolicyBalanced;
119 }
120 
JavaToNativeRtcpMuxPolicy(JNIEnv * jni,const JavaRef<jobject> & j_rtcp_mux_policy)121 PeerConnectionInterface::RtcpMuxPolicy JavaToNativeRtcpMuxPolicy(
122     JNIEnv* jni,
123     const JavaRef<jobject>& j_rtcp_mux_policy) {
124   std::string enum_name = GetJavaEnumName(jni, j_rtcp_mux_policy);
125 
126   if (enum_name == "NEGOTIATE")
127     return PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
128 
129   if (enum_name == "REQUIRE")
130     return PeerConnectionInterface::kRtcpMuxPolicyRequire;
131 
132   RTC_CHECK(false) << "Unexpected RtcpMuxPolicy enum_name " << enum_name;
133   return PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
134 }
135 
JavaToNativeTcpCandidatePolicy(JNIEnv * jni,const JavaRef<jobject> & j_tcp_candidate_policy)136 PeerConnectionInterface::TcpCandidatePolicy JavaToNativeTcpCandidatePolicy(
137     JNIEnv* jni,
138     const JavaRef<jobject>& j_tcp_candidate_policy) {
139   std::string enum_name = GetJavaEnumName(jni, j_tcp_candidate_policy);
140 
141   if (enum_name == "ENABLED")
142     return PeerConnectionInterface::kTcpCandidatePolicyEnabled;
143 
144   if (enum_name == "DISABLED")
145     return PeerConnectionInterface::kTcpCandidatePolicyDisabled;
146 
147   RTC_CHECK(false) << "Unexpected TcpCandidatePolicy enum_name " << enum_name;
148   return PeerConnectionInterface::kTcpCandidatePolicyEnabled;
149 }
150 
151 PeerConnectionInterface::CandidateNetworkPolicy
JavaToNativeCandidateNetworkPolicy(JNIEnv * jni,const JavaRef<jobject> & j_candidate_network_policy)152 JavaToNativeCandidateNetworkPolicy(
153     JNIEnv* jni,
154     const JavaRef<jobject>& j_candidate_network_policy) {
155   std::string enum_name = GetJavaEnumName(jni, j_candidate_network_policy);
156 
157   if (enum_name == "ALL")
158     return PeerConnectionInterface::kCandidateNetworkPolicyAll;
159 
160   if (enum_name == "LOW_COST")
161     return PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
162 
163   RTC_CHECK(false) << "Unexpected CandidateNetworkPolicy enum_name "
164                    << enum_name;
165   return PeerConnectionInterface::kCandidateNetworkPolicyAll;
166 }
167 
JavaToNativeKeyType(JNIEnv * jni,const JavaRef<jobject> & j_key_type)168 rtc::KeyType JavaToNativeKeyType(JNIEnv* jni,
169                                  const JavaRef<jobject>& j_key_type) {
170   std::string enum_name = GetJavaEnumName(jni, j_key_type);
171 
172   if (enum_name == "RSA")
173     return rtc::KT_RSA;
174   if (enum_name == "ECDSA")
175     return rtc::KT_ECDSA;
176 
177   RTC_CHECK(false) << "Unexpected KeyType enum_name " << enum_name;
178   return rtc::KT_ECDSA;
179 }
180 
181 PeerConnectionInterface::ContinualGatheringPolicy
JavaToNativeContinualGatheringPolicy(JNIEnv * jni,const JavaRef<jobject> & j_gathering_policy)182 JavaToNativeContinualGatheringPolicy(
183     JNIEnv* jni,
184     const JavaRef<jobject>& j_gathering_policy) {
185   std::string enum_name = GetJavaEnumName(jni, j_gathering_policy);
186   if (enum_name == "GATHER_ONCE")
187     return PeerConnectionInterface::GATHER_ONCE;
188 
189   if (enum_name == "GATHER_CONTINUALLY")
190     return PeerConnectionInterface::GATHER_CONTINUALLY;
191 
192   RTC_CHECK(false) << "Unexpected ContinualGatheringPolicy enum name "
193                    << enum_name;
194   return PeerConnectionInterface::GATHER_ONCE;
195 }
196 
JavaToNativePortPrunePolicy(JNIEnv * jni,const JavaRef<jobject> & j_port_prune_policy)197 webrtc::PortPrunePolicy JavaToNativePortPrunePolicy(
198     JNIEnv* jni,
199     const JavaRef<jobject>& j_port_prune_policy) {
200   std::string enum_name = GetJavaEnumName(jni, j_port_prune_policy);
201   if (enum_name == "NO_PRUNE") {
202     return webrtc::NO_PRUNE;
203   }
204   if (enum_name == "PRUNE_BASED_ON_PRIORITY") {
205     return webrtc::PRUNE_BASED_ON_PRIORITY;
206   }
207   if (enum_name == "KEEP_FIRST_READY") {
208     return webrtc::KEEP_FIRST_READY;
209   }
210 
211   RTC_CHECK(false) << " Unexpected PortPrunePolicy enum name " << enum_name;
212 
213   return webrtc::NO_PRUNE;
214 }
215 
JavaToNativeTlsCertPolicy(JNIEnv * jni,const JavaRef<jobject> & j_ice_server_tls_cert_policy)216 PeerConnectionInterface::TlsCertPolicy JavaToNativeTlsCertPolicy(
217     JNIEnv* jni,
218     const JavaRef<jobject>& j_ice_server_tls_cert_policy) {
219   std::string enum_name = GetJavaEnumName(jni, j_ice_server_tls_cert_policy);
220 
221   if (enum_name == "TLS_CERT_POLICY_SECURE")
222     return PeerConnectionInterface::kTlsCertPolicySecure;
223 
224   if (enum_name == "TLS_CERT_POLICY_INSECURE_NO_CHECK")
225     return PeerConnectionInterface::kTlsCertPolicyInsecureNoCheck;
226 
227   RTC_CHECK(false) << "Unexpected TlsCertPolicy enum_name " << enum_name;
228   return PeerConnectionInterface::kTlsCertPolicySecure;
229 }
230 
JavaToNativeNetworkPreference(JNIEnv * jni,const JavaRef<jobject> & j_network_preference)231 absl::optional<rtc::AdapterType> JavaToNativeNetworkPreference(
232     JNIEnv* jni,
233     const JavaRef<jobject>& j_network_preference) {
234   std::string enum_name = GetJavaEnumName(jni, j_network_preference);
235 
236   if (enum_name == "UNKNOWN")
237     return absl::nullopt;
238 
239   if (enum_name == "ETHERNET")
240     return rtc::ADAPTER_TYPE_ETHERNET;
241 
242   if (enum_name == "WIFI")
243     return rtc::ADAPTER_TYPE_WIFI;
244 
245   if (enum_name == "CELLULAR")
246     return rtc::ADAPTER_TYPE_CELLULAR;
247 
248   if (enum_name == "VPN")
249     return rtc::ADAPTER_TYPE_VPN;
250 
251   if (enum_name == "LOOPBACK")
252     return rtc::ADAPTER_TYPE_LOOPBACK;
253 
254   RTC_CHECK(false) << "Unexpected NetworkPreference enum_name " << enum_name;
255   return absl::nullopt;
256 }
257 
258 }  // namespace jni
259 }  // namespace webrtc
260