blob: c6598b76d11f753860ba54fd9c6b8c1189954090 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
henrike@webrtc.org28e20752013-07-10 00:45:36 +000011package org.webrtc;
12
Magnus Jedvert6062f372017-11-16 16:53:12 +010013import java.util.ArrayList;
Sami Kalliomäki3e189a62017-11-24 11:13:39 +010014import java.util.Collections;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015import java.util.List;
16
17/**
18 * Java-land version of the PeerConnection APIs; wraps the C++ API
19 * http://www.webrtc.org/reference/native-apis, which in turn is inspired by the
20 * JS APIs: http://dev.w3.org/2011/webrtc/editor/webrtc.html and
21 * http://www.w3.org/TR/mediacapture-streams/
22 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010023@JNINamespace("webrtc::jni")
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024public class PeerConnection {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025 /** Tracks PeerConnectionInterface::IceGatheringState */
Magnus Jedvertba700f62017-12-04 13:43:27 +010026 public enum IceGatheringState {
27 NEW,
28 GATHERING,
29 COMPLETE;
30
31 @CalledByNative("IceGatheringState")
32 static IceGatheringState fromNativeIndex(int nativeIndex) {
33 return values()[nativeIndex];
34 }
35 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
37 /** Tracks PeerConnectionInterface::IceConnectionState */
38 public enum IceConnectionState {
sakalb6760f92016-09-29 04:12:44 -070039 NEW,
40 CHECKING,
41 CONNECTED,
42 COMPLETED,
43 FAILED,
44 DISCONNECTED,
Magnus Jedvertba700f62017-12-04 13:43:27 +010045 CLOSED;
46
47 @CalledByNative("IceConnectionState")
48 static IceConnectionState fromNativeIndex(int nativeIndex) {
49 return values()[nativeIndex];
50 }
sakalb6760f92016-09-29 04:12:44 -070051 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052
hnsl04833622017-01-09 08:35:45 -080053 /** Tracks PeerConnectionInterface::TlsCertPolicy */
54 public enum TlsCertPolicy {
55 TLS_CERT_POLICY_SECURE,
56 TLS_CERT_POLICY_INSECURE_NO_CHECK,
57 }
58
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059 /** Tracks PeerConnectionInterface::SignalingState */
60 public enum SignalingState {
sakalb6760f92016-09-29 04:12:44 -070061 STABLE,
62 HAVE_LOCAL_OFFER,
63 HAVE_LOCAL_PRANSWER,
64 HAVE_REMOTE_OFFER,
65 HAVE_REMOTE_PRANSWER,
Magnus Jedvertba700f62017-12-04 13:43:27 +010066 CLOSED;
67
68 @CalledByNative("SignalingState")
69 static SignalingState fromNativeIndex(int nativeIndex) {
70 return values()[nativeIndex];
71 }
sakalb6760f92016-09-29 04:12:44 -070072 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073
74 /** Java version of PeerConnectionObserver. */
75 public static interface Observer {
76 /** Triggered when the SignalingState changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010077 @CalledByNative("Observer") void onSignalingChange(SignalingState newState);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078
79 /** Triggered when the IceConnectionState changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010080 @CalledByNative("Observer") void onIceConnectionChange(IceConnectionState newState);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081
Peter Thatcher54360512015-07-08 11:08:35 -070082 /** Triggered when the ICE connection receiving status changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010083 @CalledByNative("Observer") void onIceConnectionReceivingChange(boolean receiving);
Peter Thatcher54360512015-07-08 11:08:35 -070084
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 /** Triggered when the IceGatheringState changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010086 @CalledByNative("Observer") void onIceGatheringChange(IceGatheringState newState);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087
88 /** Triggered when a new ICE candidate has been found. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010089 @CalledByNative("Observer") void onIceCandidate(IceCandidate candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090
Honghai Zhang7fb69db2016-03-14 11:59:18 -070091 /** Triggered when some ICE candidates have been removed. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010092 @CalledByNative("Observer") void onIceCandidatesRemoved(IceCandidate[] candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -070093
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 /** Triggered when media is received on a new stream from remote peer. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010095 @CalledByNative("Observer") void onAddStream(MediaStream stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096
97 /** Triggered when a remote peer close a stream. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010098 @CalledByNative("Observer") void onRemoveStream(MediaStream stream);
henrike@webrtc.org723d6832013-07-12 16:04:50 +000099
100 /** Triggered when a remote peer opens a DataChannel. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100101 @CalledByNative("Observer") void onDataChannel(DataChannel dataChannel);
fischman@webrtc.orgd7568a02014-01-13 22:04:12 +0000102
103 /** Triggered when renegotiation is necessary. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100104 @CalledByNative("Observer") void onRenegotiationNeeded();
zhihuangdcccda72016-12-21 14:08:03 -0800105
106 /**
107 * Triggered when a new track is signaled by the remote peer, as a result of
108 * setRemoteDescription.
109 */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100110 @CalledByNative("Observer") void onAddTrack(RtpReceiver receiver, MediaStream[] mediaStreams);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 }
112
113 /** Java version of PeerConnectionInterface.IceServer. */
114 public static class IceServer {
Emad Omaradab1d2d2017-06-16 15:43:11 -0700115 // List of URIs associated with this server. Valid formats are described
116 // in RFC7064 and RFC7065, and more may be added in the future. The "host"
117 // part of the URI may contain either an IP address or a hostname.
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700118 @Deprecated public final String uri;
119 public final List<String> urls;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 public final String username;
121 public final String password;
hnsl04833622017-01-09 08:35:45 -0800122 public final TlsCertPolicy tlsCertPolicy;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123
Emad Omaradab1d2d2017-06-16 15:43:11 -0700124 // If the URIs in |urls| only contain IP addresses, this field can be used
125 // to indicate the hostname, which may be necessary for TLS (using the SNI
126 // extension). If |urls| itself contains the hostname, this isn't
127 // necessary.
128 public final String hostname;
129
Diogo Real1dca9d52017-08-29 12:18:32 -0700130 // List of protocols to be used in the TLS ALPN extension.
131 public final List<String> tlsAlpnProtocols;
132
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700133 // List of elliptic curves to be used in the TLS elliptic curves extension.
134 // Only curve names supported by OpenSSL should be used (eg. "P-256","X25519").
135 public final List<String> tlsEllipticCurves;
136
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 /** Convenience constructor for STUN servers. */
Diogo Real05ea2b32017-08-31 00:12:58 -0700138 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 public IceServer(String uri) {
140 this(uri, "", "");
141 }
142
Diogo Real05ea2b32017-08-31 00:12:58 -0700143 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 public IceServer(String uri, String username, String password) {
hnsl04833622017-01-09 08:35:45 -0800145 this(uri, username, password, TlsCertPolicy.TLS_CERT_POLICY_SECURE);
146 }
147
Diogo Real05ea2b32017-08-31 00:12:58 -0700148 @Deprecated
hnsl04833622017-01-09 08:35:45 -0800149 public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy) {
Emad Omaradab1d2d2017-06-16 15:43:11 -0700150 this(uri, username, password, tlsCertPolicy, "");
151 }
152
Diogo Real05ea2b32017-08-31 00:12:58 -0700153 @Deprecated
Emad Omaradab1d2d2017-06-16 15:43:11 -0700154 public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy,
155 String hostname) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700156 this(uri, Collections.singletonList(uri), username, password, tlsCertPolicy, hostname, null,
157 null);
Diogo Real1dca9d52017-08-29 12:18:32 -0700158 }
159
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700160 private IceServer(String uri, List<String> urls, String username, String password,
161 TlsCertPolicy tlsCertPolicy, String hostname, List<String> tlsAlpnProtocols,
162 List<String> tlsEllipticCurves) {
163 if (uri == null || urls == null || urls.isEmpty()) {
164 throw new IllegalArgumentException("uri == null || urls == null || urls.isEmpty()");
165 }
166 for (String it : urls) {
167 if (it == null) {
168 throw new IllegalArgumentException("urls element is null: " + urls);
169 }
170 }
171 if (username == null) {
172 throw new IllegalArgumentException("username == null");
173 }
174 if (password == null) {
175 throw new IllegalArgumentException("password == null");
176 }
177 if (hostname == null) {
178 throw new IllegalArgumentException("hostname == null");
179 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 this.uri = uri;
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700181 this.urls = urls;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 this.username = username;
183 this.password = password;
hnsl04833622017-01-09 08:35:45 -0800184 this.tlsCertPolicy = tlsCertPolicy;
Emad Omaradab1d2d2017-06-16 15:43:11 -0700185 this.hostname = hostname;
Diogo Real1dca9d52017-08-29 12:18:32 -0700186 this.tlsAlpnProtocols = tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700187 this.tlsEllipticCurves = tlsEllipticCurves;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 }
189
Sami Kalliomäkibde473e2017-10-30 13:34:41 +0100190 @Override
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191 public String toString() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700192 return urls + " [" + username + ":" + password + "] [" + tlsCertPolicy + "] [" + hostname
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700193 + "] [" + tlsAlpnProtocols + "] [" + tlsEllipticCurves + "]";
Diogo Real1dca9d52017-08-29 12:18:32 -0700194 }
195
196 public static Builder builder(String uri) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700197 return new Builder(Collections.singletonList(uri));
198 }
199
200 public static Builder builder(List<String> urls) {
201 return new Builder(urls);
Diogo Real1dca9d52017-08-29 12:18:32 -0700202 }
203
204 public static class Builder {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700205 private final List<String> urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700206 private String username = "";
207 private String password = "";
208 private TlsCertPolicy tlsCertPolicy = TlsCertPolicy.TLS_CERT_POLICY_SECURE;
209 private String hostname = "";
210 private List<String> tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700211 private List<String> tlsEllipticCurves;
Diogo Real1dca9d52017-08-29 12:18:32 -0700212
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700213 private Builder(List<String> urls) {
214 if (urls == null || urls.isEmpty()) {
215 throw new IllegalArgumentException("urls == null || urls.isEmpty(): " + urls);
216 }
217 this.urls = urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700218 }
219
220 public Builder setUsername(String username) {
221 this.username = username;
222 return this;
223 }
224
225 public Builder setPassword(String password) {
226 this.password = password;
227 return this;
228 }
229
230 public Builder setTlsCertPolicy(TlsCertPolicy tlsCertPolicy) {
231 this.tlsCertPolicy = tlsCertPolicy;
232 return this;
233 }
234
235 public Builder setHostname(String hostname) {
236 this.hostname = hostname;
237 return this;
238 }
239
240 public Builder setTlsAlpnProtocols(List<String> tlsAlpnProtocols) {
241 this.tlsAlpnProtocols = tlsAlpnProtocols;
242 return this;
243 }
244
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700245 public Builder setTlsEllipticCurves(List<String> tlsEllipticCurves) {
246 this.tlsEllipticCurves = tlsEllipticCurves;
247 return this;
248 }
249
Diogo Real1dca9d52017-08-29 12:18:32 -0700250 public IceServer createIceServer() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700251 return new IceServer(urls.get(0), urls, username, password, tlsCertPolicy, hostname,
252 tlsAlpnProtocols, tlsEllipticCurves);
Diogo Real1dca9d52017-08-29 12:18:32 -0700253 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 }
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100255
256 @CalledByNative("IceServer")
257 List<String> getUrls() {
258 return urls;
259 }
260
261 @CalledByNative("IceServer")
262 String getUsername() {
263 return username;
264 }
265
266 @CalledByNative("IceServer")
267 String getPassword() {
268 return password;
269 }
270
271 @CalledByNative("IceServer")
272 TlsCertPolicy getTlsCertPolicy() {
273 return tlsCertPolicy;
274 }
275
276 @CalledByNative("IceServer")
277 String getHostname() {
278 return hostname;
279 }
280
281 @CalledByNative("IceServer")
282 List<String> getTlsAlpnProtocols() {
283 return tlsAlpnProtocols;
284 }
285
286 @CalledByNative("IceServer")
287 List<String> getTlsEllipticCurves() {
288 return tlsEllipticCurves;
289 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290 }
291
Jiayang Liucac1b382015-04-30 12:35:24 -0700292 /** Java version of PeerConnectionInterface.IceTransportsType */
sakalb6760f92016-09-29 04:12:44 -0700293 public enum IceTransportsType { NONE, RELAY, NOHOST, ALL }
Jiayang Liucac1b382015-04-30 12:35:24 -0700294
295 /** Java version of PeerConnectionInterface.BundlePolicy */
sakalb6760f92016-09-29 04:12:44 -0700296 public enum BundlePolicy { BALANCED, MAXBUNDLE, MAXCOMPAT }
Jiayang Liucac1b382015-04-30 12:35:24 -0700297
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700298 /** Java version of PeerConnectionInterface.RtcpMuxPolicy */
sakalb6760f92016-09-29 04:12:44 -0700299 public enum RtcpMuxPolicy { NEGOTIATE, REQUIRE }
glaznev97579a42015-09-01 11:31:27 -0700300
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700301 /** Java version of PeerConnectionInterface.TcpCandidatePolicy */
sakalb6760f92016-09-29 04:12:44 -0700302 public enum TcpCandidatePolicy { ENABLED, DISABLED }
Jiayang Liucac1b382015-04-30 12:35:24 -0700303
honghaiz60347052016-05-31 18:29:12 -0700304 /** Java version of PeerConnectionInterface.CandidateNetworkPolicy */
sakalb6760f92016-09-29 04:12:44 -0700305 public enum CandidateNetworkPolicy { ALL, LOW_COST }
honghaiz60347052016-05-31 18:29:12 -0700306
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800307 // Keep in sync with webrtc/rtc_base/network_constants.h.
308 public enum AdapterType {
309 UNKNOWN,
310 ETHERNET,
311 WIFI,
312 CELLULAR,
313 VPN,
314 LOOPBACK,
315 }
316
glaznev97579a42015-09-01 11:31:27 -0700317 /** Java version of rtc::KeyType */
sakalb6760f92016-09-29 04:12:44 -0700318 public enum KeyType { RSA, ECDSA }
glaznev97579a42015-09-01 11:31:27 -0700319
honghaiz1f429e32015-09-28 07:57:34 -0700320 /** Java version of PeerConnectionInterface.ContinualGatheringPolicy */
sakalb6760f92016-09-29 04:12:44 -0700321 public enum ContinualGatheringPolicy { GATHER_ONCE, GATHER_CONTINUALLY }
honghaiz1f429e32015-09-28 07:57:34 -0700322
Steve Antond960a0c2017-07-17 12:33:07 -0700323 /** Java version of rtc::IntervalRange */
324 public static class IntervalRange {
325 private final int min;
326 private final int max;
327
328 public IntervalRange(int min, int max) {
329 this.min = min;
330 this.max = max;
331 }
332
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100333 @CalledByNative("IntervalRange")
Steve Antond960a0c2017-07-17 12:33:07 -0700334 public int getMin() {
335 return min;
336 }
337
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100338 @CalledByNative("IntervalRange")
Steve Antond960a0c2017-07-17 12:33:07 -0700339 public int getMax() {
340 return max;
341 }
342 }
343
Jiayang Liucac1b382015-04-30 12:35:24 -0700344 /** Java version of PeerConnectionInterface.RTCConfiguration */
345 public static class RTCConfiguration {
346 public IceTransportsType iceTransportsType;
347 public List<IceServer> iceServers;
348 public BundlePolicy bundlePolicy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700349 public RtcpMuxPolicy rtcpMuxPolicy;
Jiayang Liucac1b382015-04-30 12:35:24 -0700350 public TcpCandidatePolicy tcpCandidatePolicy;
honghaiz60347052016-05-31 18:29:12 -0700351 public CandidateNetworkPolicy candidateNetworkPolicy;
Henrik Lundin64dad832015-05-11 12:44:23 +0200352 public int audioJitterBufferMaxPackets;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200353 public boolean audioJitterBufferFastAccelerate;
honghaiz4edc39c2015-09-01 09:53:56 -0700354 public int iceConnectionReceivingTimeout;
Honghai Zhang381b4212015-12-04 12:24:03 -0800355 public int iceBackupCandidatePairPingInterval;
glaznev97579a42015-09-01 11:31:27 -0700356 public KeyType keyType;
honghaiz1f429e32015-09-28 07:57:34 -0700357 public ContinualGatheringPolicy continualGatheringPolicy;
deadbeefbe0c96f2016-05-18 16:20:14 -0700358 public int iceCandidatePoolSize;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700359 public boolean pruneTurnPorts;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700360 public boolean presumeWritableWhenFullyRelayed;
skvlad51072462017-02-02 11:50:14 -0800361 public Integer iceCheckMinInterval;
zhihuangb09b3f92017-03-07 14:40:51 -0800362 public boolean disableIPv6OnWifi;
deadbeef28e29192017-07-27 09:14:38 -0700363 // By default, PeerConnection will use a limited number of IPv6 network
364 // interfaces, in order to avoid too many ICE candidate pairs being created
365 // and delaying ICE completion.
366 //
367 // Can be set to Integer.MAX_VALUE to effectively disable the limit.
368 public int maxIPv6Networks;
Steve Antond960a0c2017-07-17 12:33:07 -0700369 public IntervalRange iceRegatherIntervalRange;
Jiayang Liucac1b382015-04-30 12:35:24 -0700370
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100371 // These values will be overridden by MediaStream constraints if deprecated constraints-based
372 // create peerconnection interface is used.
373 public boolean disableIpv6;
374 public boolean enableDscp;
375 public boolean enableCpuOveruseDetection;
376 public boolean enableRtpDataChannel;
377 public boolean suspendBelowMinBitrate;
378 public Integer screencastMinBitrate;
379 public Boolean combinedAudioVideoBwe;
380 public Boolean enableDtlsSrtp;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800381 // Use "Unknown" to represent no preference of adapter types, not the
382 // preference of adapters of unknown types.
383 public AdapterType networkPreference;
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100384
Jonas Orelandbdcee282017-10-10 14:01:40 +0200385 // This is an optional wrapper for the C++ webrtc::TurnCustomizer.
386 public TurnCustomizer turnCustomizer;
387
deadbeef28e29192017-07-27 09:14:38 -0700388 // TODO(deadbeef): Instead of duplicating the defaults here, we should do
389 // something to pick up the defaults from C++. The Objective-C equivalent
390 // of RTCConfiguration does that.
Jiayang Liucac1b382015-04-30 12:35:24 -0700391 public RTCConfiguration(List<IceServer> iceServers) {
392 iceTransportsType = IceTransportsType.ALL;
393 bundlePolicy = BundlePolicy.BALANCED;
zhihuang4dfb8ce2016-11-23 10:30:12 -0800394 rtcpMuxPolicy = RtcpMuxPolicy.REQUIRE;
Jiayang Liucac1b382015-04-30 12:35:24 -0700395 tcpCandidatePolicy = TcpCandidatePolicy.ENABLED;
Sami Kalliomäki9828beb2017-10-26 16:21:22 +0200396 candidateNetworkPolicy = CandidateNetworkPolicy.ALL;
Jiayang Liucac1b382015-04-30 12:35:24 -0700397 this.iceServers = iceServers;
Henrik Lundin64dad832015-05-11 12:44:23 +0200398 audioJitterBufferMaxPackets = 50;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200399 audioJitterBufferFastAccelerate = false;
honghaiz4edc39c2015-09-01 09:53:56 -0700400 iceConnectionReceivingTimeout = -1;
Honghai Zhang381b4212015-12-04 12:24:03 -0800401 iceBackupCandidatePairPingInterval = -1;
glaznev97579a42015-09-01 11:31:27 -0700402 keyType = KeyType.ECDSA;
honghaiz1f429e32015-09-28 07:57:34 -0700403 continualGatheringPolicy = ContinualGatheringPolicy.GATHER_ONCE;
deadbeefbe0c96f2016-05-18 16:20:14 -0700404 iceCandidatePoolSize = 0;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700405 pruneTurnPorts = false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700406 presumeWritableWhenFullyRelayed = false;
skvlad51072462017-02-02 11:50:14 -0800407 iceCheckMinInterval = null;
zhihuangb09b3f92017-03-07 14:40:51 -0800408 disableIPv6OnWifi = false;
deadbeef28e29192017-07-27 09:14:38 -0700409 maxIPv6Networks = 5;
Steve Antond960a0c2017-07-17 12:33:07 -0700410 iceRegatherIntervalRange = null;
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100411 disableIpv6 = false;
412 enableDscp = false;
413 enableCpuOveruseDetection = true;
414 enableRtpDataChannel = false;
415 suspendBelowMinBitrate = false;
416 screencastMinBitrate = null;
417 combinedAudioVideoBwe = null;
418 enableDtlsSrtp = null;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800419 networkPreference = AdapterType.UNKNOWN;
Jiayang Liucac1b382015-04-30 12:35:24 -0700420 }
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100421
422 @CalledByNative("RTCConfiguration")
423 IceTransportsType getIceTransportsType() {
424 return iceTransportsType;
425 }
426
427 @CalledByNative("RTCConfiguration")
428 List<IceServer> getIceServers() {
429 return iceServers;
430 }
431
432 @CalledByNative("RTCConfiguration")
433 BundlePolicy getBundlePolicy() {
434 return bundlePolicy;
435 }
436
437 @CalledByNative("RTCConfiguration")
438 RtcpMuxPolicy getRtcpMuxPolicy() {
439 return rtcpMuxPolicy;
440 }
441
442 @CalledByNative("RTCConfiguration")
443 TcpCandidatePolicy getTcpCandidatePolicy() {
444 return tcpCandidatePolicy;
445 }
446
447 @CalledByNative("RTCConfiguration")
448 CandidateNetworkPolicy getCandidateNetworkPolicy() {
449 return candidateNetworkPolicy;
450 }
451
452 @CalledByNative("RTCConfiguration")
453 int getAudioJitterBufferMaxPackets() {
454 return audioJitterBufferMaxPackets;
455 }
456
457 @CalledByNative("RTCConfiguration")
458 boolean getAudioJitterBufferFastAccelerate() {
459 return audioJitterBufferFastAccelerate;
460 }
461
462 @CalledByNative("RTCConfiguration")
463 int getIceConnectionReceivingTimeout() {
464 return iceConnectionReceivingTimeout;
465 }
466
467 @CalledByNative("RTCConfiguration")
468 int getIceBackupCandidatePairPingInterval() {
469 return iceBackupCandidatePairPingInterval;
470 }
471
472 @CalledByNative("RTCConfiguration")
473 KeyType getKeyType() {
474 return keyType;
475 }
476
477 @CalledByNative("RTCConfiguration")
478 ContinualGatheringPolicy getContinualGatheringPolicy() {
479 return continualGatheringPolicy;
480 }
481
482 @CalledByNative("RTCConfiguration")
483 int getIceCandidatePoolSize() {
484 return iceCandidatePoolSize;
485 }
486
487 @CalledByNative("RTCConfiguration")
488 boolean getPruneTurnPorts() {
489 return pruneTurnPorts;
490 }
491
492 @CalledByNative("RTCConfiguration")
493 boolean getPresumeWritableWhenFullyRelayed() {
494 return presumeWritableWhenFullyRelayed;
495 }
496
497 @CalledByNative("RTCConfiguration")
498 Integer getIceCheckMinInterval() {
499 return iceCheckMinInterval;
500 }
501
502 @CalledByNative("RTCConfiguration")
503 boolean getDisableIPv6OnWifi() {
504 return disableIPv6OnWifi;
505 }
506
507 @CalledByNative("RTCConfiguration")
508 int getMaxIPv6Networks() {
509 return maxIPv6Networks;
510 }
511
512 @CalledByNative("RTCConfiguration")
513 IntervalRange getIceRegatherIntervalRange() {
514 return iceRegatherIntervalRange;
515 }
516
517 @CalledByNative("RTCConfiguration")
518 TurnCustomizer getTurnCustomizer() {
519 return turnCustomizer;
520 }
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100521
522 @CalledByNative("RTCConfiguration")
523 boolean getDisableIpv6() {
524 return disableIpv6;
525 }
526
527 @CalledByNative("RTCConfiguration")
528 boolean getEnableDscp() {
529 return enableDscp;
530 }
531
532 @CalledByNative("RTCConfiguration")
533 boolean getEnableCpuOveruseDetection() {
534 return enableCpuOveruseDetection;
535 }
536
537 @CalledByNative("RTCConfiguration")
538 boolean getEnableRtpDataChannel() {
539 return enableRtpDataChannel;
540 }
541
542 @CalledByNative("RTCConfiguration")
543 boolean getSuspendBelowMinBitrate() {
544 return suspendBelowMinBitrate;
545 }
546
547 @CalledByNative("RTCConfiguration")
548 Integer getScreencastMinBitrate() {
549 return screencastMinBitrate;
550 }
551
552 @CalledByNative("RTCConfiguration")
553 Boolean getCombinedAudioVideoBwe() {
554 return combinedAudioVideoBwe;
555 }
556
557 @CalledByNative("RTCConfiguration")
558 Boolean getEnableDtlsSrtp() {
559 return enableDtlsSrtp;
560 }
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800561
562 @CalledByNative("RTCConfiguration")
563 AdapterType getNetworkPreference() {
564 return networkPreference;
565 }
Jiayang Liucac1b382015-04-30 12:35:24 -0700566 };
567
Magnus Jedvert6062f372017-11-16 16:53:12 +0100568 private final List<MediaStream> localStreams = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 private final long nativePeerConnection;
Magnus Jedvert6062f372017-11-16 16:53:12 +0100570 private List<RtpSender> senders = new ArrayList<>();
571 private List<RtpReceiver> receivers = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572
Sami Kalliomäki1ece1ed2017-12-20 11:59:22 +0100573 /**
574 * Wraps a PeerConnection created by the factory. Can be used by clients that want to implement
575 * their PeerConnection creation in JNI.
576 */
577 public PeerConnection(NativePeerConnectionFactory factory) {
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100578 this(factory.createNativePeerConnection());
Sami Kalliomäki1ece1ed2017-12-20 11:59:22 +0100579 }
580
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100581 PeerConnection(long nativePeerConnection) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 this.nativePeerConnection = nativePeerConnection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 }
584
585 // JsepInterface.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100586 public SessionDescription getLocalDescription() {
587 return nativeGetLocalDescription();
588 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100590 public SessionDescription getRemoteDescription() {
591 return nativeGetRemoteDescription();
592 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100594 public DataChannel createDataChannel(String label, DataChannel.Init init) {
595 return nativeCreateDataChannel(label, init);
596 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000597
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100598 public void createOffer(SdpObserver observer, MediaConstraints constraints) {
599 nativeCreateOffer(observer, constraints);
600 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100602 public void createAnswer(SdpObserver observer, MediaConstraints constraints) {
603 nativeCreateAnswer(observer, constraints);
604 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100606 public void setLocalDescription(SdpObserver observer, SessionDescription sdp) {
607 nativeSetLocalDescription(observer, sdp);
608 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100610 public void setRemoteDescription(SdpObserver observer, SessionDescription sdp) {
611 nativeSetRemoteDescription(observer, sdp);
612 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613
henrika5f6bf242017-11-01 11:06:56 +0100614 // True if remote audio should be played out. Defaults to true.
615 // Note that even if playout is enabled, streams will only be played out if
616 // the appropriate SDP is also applied. The main purpose of this API is to
617 // be able to control the exact time when audio playout starts.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100618 public void setAudioPlayout(boolean playout) {
619 nativeSetAudioPlayout(playout);
620 }
henrika5f6bf242017-11-01 11:06:56 +0100621
622 // True if local audio shall be recorded. Defaults to true.
623 // Note that even if recording is enabled, streams will only be recorded if
624 // the appropriate SDP is also applied. The main purpose of this API is to
625 // be able to control the exact time when audio recording starts.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100626 public void setAudioRecording(boolean recording) {
627 nativeSetAudioRecording(recording);
628 }
henrika5f6bf242017-11-01 11:06:56 +0100629
deadbeef5d0b6d82017-01-09 16:05:28 -0800630 public boolean setConfiguration(RTCConfiguration config) {
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100631 return nativeSetConfiguration(config);
deadbeef5d0b6d82017-01-09 16:05:28 -0800632 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633
634 public boolean addIceCandidate(IceCandidate candidate) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100635 return nativeAddIceCandidate(candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636 }
637
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700638 public boolean removeIceCandidates(final IceCandidate[] candidates) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100639 return nativeRemoveIceCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700640 }
641
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000642 public boolean addStream(MediaStream stream) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100643 boolean ret = nativeAddLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000644 if (!ret) {
645 return false;
646 }
647 localStreams.add(stream);
648 return true;
649 }
650
651 public void removeStream(MediaStream stream) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100652 nativeRemoveLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653 localStreams.remove(stream);
654 }
655
deadbeef7a246882017-08-09 08:40:10 -0700656 /**
657 * Creates an RtpSender without a track.
658 * <p>
659 * This method allows an application to cause the PeerConnection to negotiate
660 * sending/receiving a specific media type, but without having a track to
661 * send yet.
662 * <p>
663 * When the application does want to begin sending a track, it can call
664 * RtpSender.setTrack, which doesn't require any additional SDP negotiation.
665 * <p>
666 * Example use:
667 * <pre>
668 * {@code
669 * audioSender = pc.createSender("audio", "stream1");
670 * videoSender = pc.createSender("video", "stream1");
671 * // Do normal SDP offer/answer, which will kick off ICE/DTLS and negotiate
672 * // media parameters....
673 * // Later, when the endpoint is ready to actually begin sending:
674 * audioSender.setTrack(audioTrack, false);
675 * videoSender.setTrack(videoTrack, false);
676 * }
677 * </pre>
678 * Note: This corresponds most closely to "addTransceiver" in the official
679 * WebRTC API, in that it creates a sender without a track. It was
680 * implemented before addTransceiver because it provides useful
681 * functionality, and properly implementing transceivers would have required
682 * a great deal more work.
683 *
684 * @param kind Corresponds to MediaStreamTrack kinds (must be "audio" or
685 * "video").
686 * @param stream_id The ID of the MediaStream that this sender's track will
687 * be associated with when SDP is applied to the remote
688 * PeerConnection. If createSender is used to create an
689 * audio and video sender that should be synchronized, they
690 * should use the same stream ID.
691 * @return A new RtpSender object if successful, or null otherwise.
692 */
deadbeefbd7d8f72015-12-18 16:58:44 -0800693 public RtpSender createSender(String kind, String stream_id) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100694 RtpSender new_sender = nativeCreateSender(kind, stream_id);
deadbeefee524f72015-12-02 11:27:40 -0800695 if (new_sender != null) {
696 senders.add(new_sender);
697 }
698 return new_sender;
699 }
700
deadbeef4139c0f2015-10-06 12:29:25 -0700701 // Note that calling getSenders will dispose of the senders previously
702 // returned (and same goes for getReceivers).
703 public List<RtpSender> getSenders() {
704 for (RtpSender sender : senders) {
705 sender.dispose();
706 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100707 senders = nativeGetSenders();
deadbeef4139c0f2015-10-06 12:29:25 -0700708 return Collections.unmodifiableList(senders);
709 }
710
711 public List<RtpReceiver> getReceivers() {
712 for (RtpReceiver receiver : receivers) {
713 receiver.dispose();
714 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100715 receivers = nativeGetReceivers();
deadbeef4139c0f2015-10-06 12:29:25 -0700716 return Collections.unmodifiableList(receivers);
717 }
718
deadbeef82215872017-04-18 10:27:51 -0700719 // Older, non-standard implementation of getStats.
720 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 public boolean getStats(StatsObserver observer, MediaStreamTrack track) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100722 return nativeOldGetStats(observer, (track == null) ? 0 : track.nativeTrack);
deadbeef82215872017-04-18 10:27:51 -0700723 }
724
725 // Gets stats using the new stats collection API, see webrtc/api/stats/. These
726 // will replace old stats collection API when the new API has matured enough.
727 public void getStats(RTCStatsCollectorCallback callback) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100728 nativeNewGetStats(callback);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 }
730
zsteind89b0bc2017-08-03 11:11:40 -0700731 // Limits the bandwidth allocated for all RTP streams sent by this
732 // PeerConnection. Pass null to leave a value unchanged.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100733 public boolean setBitrate(Integer min, Integer current, Integer max) {
734 return nativeSetBitrate(min, current, max);
735 }
zsteind89b0bc2017-08-03 11:11:40 -0700736
ivoc14d5dbe2016-07-04 07:06:55 -0700737 // Starts recording an RTC event log. Ownership of the file is transfered to
738 // the native code. If an RTC event log is already being recorded, it will be
739 // stopped and a new one will start using the provided file. Logging will
740 // continue until the stopRtcEventLog function is called. The max_size_bytes
741 // argument is ignored, it is added for future use.
ivoc0c6f0f62016-07-06 04:34:23 -0700742 public boolean startRtcEventLog(int file_descriptor, int max_size_bytes) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100743 return nativeStartRtcEventLog(file_descriptor, max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -0700744 }
745
746 // Stops recording an RTC event log. If no RTC event log is currently being
747 // recorded, this call will have no effect.
748 public void stopRtcEventLog() {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100749 nativeStopRtcEventLog();
ivoc14d5dbe2016-07-04 07:06:55 -0700750 }
751
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 // TODO(fischman): add support for DTMF-related methods once that API
753 // stabilizes.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100754 public SignalingState signalingState() {
755 return nativeSignalingState();
756 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100758 public IceConnectionState iceConnectionState() {
759 return nativeIceConnectionState();
760 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100762 public IceGatheringState iceGatheringState() {
763 return nativeIceGatheringState();
764 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100766 public void close() {
767 nativeClose();
768 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769
deadbeef43697f62017-09-12 10:52:14 -0700770 /**
771 * Free native resources associated with this PeerConnection instance.
772 * <p>
773 * This method removes a reference count from the C++ PeerConnection object,
774 * which should result in it being destroyed. It also calls equivalent
775 * "dispose" methods on the Java objects attached to this PeerConnection
776 * (streams, senders, receivers), such that their associated C++ objects
777 * will also be destroyed.
778 * <p>
779 * Note that this method cannot be safely called from an observer callback
780 * (PeerConnection.Observer, DataChannel.Observer, etc.). If you want to, for
781 * example, destroy the PeerConnection after an "ICE failed" callback, you
782 * must do this asynchronously (in other words, unwind the stack first). See
783 * <a href="https://bugs.chromium.org/p/webrtc/issues/detail?id=3721">bug
784 * 3721</a> for more details.
785 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 public void dispose() {
787 close();
788 for (MediaStream stream : localStreams) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100789 nativeRemoveLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 stream.dispose();
791 }
792 localStreams.clear();
deadbeef4139c0f2015-10-06 12:29:25 -0700793 for (RtpSender sender : senders) {
794 sender.dispose();
795 }
796 senders.clear();
797 for (RtpReceiver receiver : receivers) {
798 receiver.dispose();
799 }
800 receivers.clear();
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100801 nativeFreeOwnedPeerConnection(nativePeerConnection);
802 }
803
804 /** Returns a pointer to the native webrtc::PeerConnectionInterface. */
805 public long getNativePeerConnection() {
806 return nativeGetNativePeerConnection();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 }
808
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100809 @CalledByNative
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100810 long getNativeOwnedPeerConnection() {
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100811 return nativePeerConnection;
812 }
813
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100814 public static long createNativePeerConnectionObserver(Observer observer) {
815 return nativeCreatePeerConnectionObserver(observer);
816 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100818 private native long nativeGetNativePeerConnection();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100819 private native SessionDescription nativeGetLocalDescription();
820 private native SessionDescription nativeGetRemoteDescription();
821 private native DataChannel nativeCreateDataChannel(String label, DataChannel.Init init);
822 private native void nativeCreateOffer(SdpObserver observer, MediaConstraints constraints);
823 private native void nativeCreateAnswer(SdpObserver observer, MediaConstraints constraints);
824 private native void nativeSetLocalDescription(SdpObserver observer, SessionDescription sdp);
825 private native void nativeSetRemoteDescription(SdpObserver observer, SessionDescription sdp);
826 private native void nativeSetAudioPlayout(boolean playout);
827 private native void nativeSetAudioRecording(boolean recording);
828 private native boolean nativeSetBitrate(Integer min, Integer current, Integer max);
829 private native SignalingState nativeSignalingState();
830 private native IceConnectionState nativeIceConnectionState();
831 private native IceGatheringState nativeIceGatheringState();
832 private native void nativeClose();
833 private static native long nativeCreatePeerConnectionObserver(Observer observer);
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100834 private static native void nativeFreeOwnedPeerConnection(long ownedPeerConnection);
835 private native boolean nativeSetConfiguration(RTCConfiguration config);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100836 private native boolean nativeAddIceCandidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 String sdpMid, int sdpMLineIndex, String iceCandidateSdp);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100838 private native boolean nativeRemoveIceCandidates(final IceCandidate[] candidates);
839 private native boolean nativeAddLocalStream(long stream);
840 private native void nativeRemoveLocalStream(long stream);
841 private native boolean nativeOldGetStats(StatsObserver observer, long nativeTrack);
842 private native void nativeNewGetStats(RTCStatsCollectorCallback callback);
843 private native RtpSender nativeCreateSender(String kind, String stream_id);
844 private native List<RtpSender> nativeGetSenders();
845 private native List<RtpReceiver> nativeGetReceivers();
846 private native boolean nativeStartRtcEventLog(int file_descriptor, int max_size_bytes);
847 private native void nativeStopRtcEventLog();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848}