blob: 6c278725aa1dc0cbe9b3ccbfe1518d6a155e1856 [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 */
23public class PeerConnection {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024 /** Tracks PeerConnectionInterface::IceGatheringState */
Magnus Jedvertba700f62017-12-04 13:43:27 +010025 public enum IceGatheringState {
26 NEW,
27 GATHERING,
28 COMPLETE;
29
30 @CalledByNative("IceGatheringState")
31 static IceGatheringState fromNativeIndex(int nativeIndex) {
32 return values()[nativeIndex];
33 }
34 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
36 /** Tracks PeerConnectionInterface::IceConnectionState */
37 public enum IceConnectionState {
sakalb6760f92016-09-29 04:12:44 -070038 NEW,
39 CHECKING,
40 CONNECTED,
41 COMPLETED,
42 FAILED,
43 DISCONNECTED,
Magnus Jedvertba700f62017-12-04 13:43:27 +010044 CLOSED;
45
46 @CalledByNative("IceConnectionState")
47 static IceConnectionState fromNativeIndex(int nativeIndex) {
48 return values()[nativeIndex];
49 }
sakalb6760f92016-09-29 04:12:44 -070050 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
hnsl04833622017-01-09 08:35:45 -080052 /** Tracks PeerConnectionInterface::TlsCertPolicy */
53 public enum TlsCertPolicy {
54 TLS_CERT_POLICY_SECURE,
55 TLS_CERT_POLICY_INSECURE_NO_CHECK,
56 }
57
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 /** Tracks PeerConnectionInterface::SignalingState */
59 public enum SignalingState {
sakalb6760f92016-09-29 04:12:44 -070060 STABLE,
61 HAVE_LOCAL_OFFER,
62 HAVE_LOCAL_PRANSWER,
63 HAVE_REMOTE_OFFER,
64 HAVE_REMOTE_PRANSWER,
Magnus Jedvertba700f62017-12-04 13:43:27 +010065 CLOSED;
66
67 @CalledByNative("SignalingState")
68 static SignalingState fromNativeIndex(int nativeIndex) {
69 return values()[nativeIndex];
70 }
sakalb6760f92016-09-29 04:12:44 -070071 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072
73 /** Java version of PeerConnectionObserver. */
74 public static interface Observer {
75 /** Triggered when the SignalingState changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010076 @CalledByNative("Observer") void onSignalingChange(SignalingState newState);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077
78 /** Triggered when the IceConnectionState changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010079 @CalledByNative("Observer") void onIceConnectionChange(IceConnectionState newState);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080
Peter Thatcher54360512015-07-08 11:08:35 -070081 /** Triggered when the ICE connection receiving status changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010082 @CalledByNative("Observer") void onIceConnectionReceivingChange(boolean receiving);
Peter Thatcher54360512015-07-08 11:08:35 -070083
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 /** Triggered when the IceGatheringState changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010085 @CalledByNative("Observer") void onIceGatheringChange(IceGatheringState newState);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086
87 /** Triggered when a new ICE candidate has been found. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010088 @CalledByNative("Observer") void onIceCandidate(IceCandidate candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089
Honghai Zhang7fb69db2016-03-14 11:59:18 -070090 /** Triggered when some ICE candidates have been removed. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010091 @CalledByNative("Observer") void onIceCandidatesRemoved(IceCandidate[] candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -070092
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 /** Triggered when media is received on a new stream from remote peer. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010094 @CalledByNative("Observer") void onAddStream(MediaStream stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
96 /** Triggered when a remote peer close a stream. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010097 @CalledByNative("Observer") void onRemoveStream(MediaStream stream);
henrike@webrtc.org723d6832013-07-12 16:04:50 +000098
99 /** Triggered when a remote peer opens a DataChannel. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100100 @CalledByNative("Observer") void onDataChannel(DataChannel dataChannel);
fischman@webrtc.orgd7568a02014-01-13 22:04:12 +0000101
102 /** Triggered when renegotiation is necessary. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100103 @CalledByNative("Observer") void onRenegotiationNeeded();
zhihuangdcccda72016-12-21 14:08:03 -0800104
105 /**
106 * Triggered when a new track is signaled by the remote peer, as a result of
107 * setRemoteDescription.
108 */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100109 @CalledByNative("Observer") void onAddTrack(RtpReceiver receiver, MediaStream[] mediaStreams);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 }
111
112 /** Java version of PeerConnectionInterface.IceServer. */
113 public static class IceServer {
Emad Omaradab1d2d2017-06-16 15:43:11 -0700114 // List of URIs associated with this server. Valid formats are described
115 // in RFC7064 and RFC7065, and more may be added in the future. The "host"
116 // part of the URI may contain either an IP address or a hostname.
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700117 @Deprecated public final String uri;
118 public final List<String> urls;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 public final String username;
120 public final String password;
hnsl04833622017-01-09 08:35:45 -0800121 public final TlsCertPolicy tlsCertPolicy;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122
Emad Omaradab1d2d2017-06-16 15:43:11 -0700123 // If the URIs in |urls| only contain IP addresses, this field can be used
124 // to indicate the hostname, which may be necessary for TLS (using the SNI
125 // extension). If |urls| itself contains the hostname, this isn't
126 // necessary.
127 public final String hostname;
128
Diogo Real1dca9d52017-08-29 12:18:32 -0700129 // List of protocols to be used in the TLS ALPN extension.
130 public final List<String> tlsAlpnProtocols;
131
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700132 // List of elliptic curves to be used in the TLS elliptic curves extension.
133 // Only curve names supported by OpenSSL should be used (eg. "P-256","X25519").
134 public final List<String> tlsEllipticCurves;
135
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 /** Convenience constructor for STUN servers. */
Diogo Real05ea2b32017-08-31 00:12:58 -0700137 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 public IceServer(String uri) {
139 this(uri, "", "");
140 }
141
Diogo Real05ea2b32017-08-31 00:12:58 -0700142 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 public IceServer(String uri, String username, String password) {
hnsl04833622017-01-09 08:35:45 -0800144 this(uri, username, password, TlsCertPolicy.TLS_CERT_POLICY_SECURE);
145 }
146
Diogo Real05ea2b32017-08-31 00:12:58 -0700147 @Deprecated
hnsl04833622017-01-09 08:35:45 -0800148 public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy) {
Emad Omaradab1d2d2017-06-16 15:43:11 -0700149 this(uri, username, password, tlsCertPolicy, "");
150 }
151
Diogo Real05ea2b32017-08-31 00:12:58 -0700152 @Deprecated
Emad Omaradab1d2d2017-06-16 15:43:11 -0700153 public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy,
154 String hostname) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700155 this(uri, Collections.singletonList(uri), username, password, tlsCertPolicy, hostname, null,
156 null);
Diogo Real1dca9d52017-08-29 12:18:32 -0700157 }
158
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700159 private IceServer(String uri, List<String> urls, String username, String password,
160 TlsCertPolicy tlsCertPolicy, String hostname, List<String> tlsAlpnProtocols,
161 List<String> tlsEllipticCurves) {
162 if (uri == null || urls == null || urls.isEmpty()) {
163 throw new IllegalArgumentException("uri == null || urls == null || urls.isEmpty()");
164 }
165 for (String it : urls) {
166 if (it == null) {
167 throw new IllegalArgumentException("urls element is null: " + urls);
168 }
169 }
170 if (username == null) {
171 throw new IllegalArgumentException("username == null");
172 }
173 if (password == null) {
174 throw new IllegalArgumentException("password == null");
175 }
176 if (hostname == null) {
177 throw new IllegalArgumentException("hostname == null");
178 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 this.uri = uri;
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700180 this.urls = urls;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 this.username = username;
182 this.password = password;
hnsl04833622017-01-09 08:35:45 -0800183 this.tlsCertPolicy = tlsCertPolicy;
Emad Omaradab1d2d2017-06-16 15:43:11 -0700184 this.hostname = hostname;
Diogo Real1dca9d52017-08-29 12:18:32 -0700185 this.tlsAlpnProtocols = tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700186 this.tlsEllipticCurves = tlsEllipticCurves;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 }
188
Sami Kalliomäkibde473e2017-10-30 13:34:41 +0100189 @Override
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 public String toString() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700191 return urls + " [" + username + ":" + password + "] [" + tlsCertPolicy + "] [" + hostname
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700192 + "] [" + tlsAlpnProtocols + "] [" + tlsEllipticCurves + "]";
Diogo Real1dca9d52017-08-29 12:18:32 -0700193 }
194
195 public static Builder builder(String uri) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700196 return new Builder(Collections.singletonList(uri));
197 }
198
199 public static Builder builder(List<String> urls) {
200 return new Builder(urls);
Diogo Real1dca9d52017-08-29 12:18:32 -0700201 }
202
203 public static class Builder {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700204 private final List<String> urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700205 private String username = "";
206 private String password = "";
207 private TlsCertPolicy tlsCertPolicy = TlsCertPolicy.TLS_CERT_POLICY_SECURE;
208 private String hostname = "";
209 private List<String> tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700210 private List<String> tlsEllipticCurves;
Diogo Real1dca9d52017-08-29 12:18:32 -0700211
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700212 private Builder(List<String> urls) {
213 if (urls == null || urls.isEmpty()) {
214 throw new IllegalArgumentException("urls == null || urls.isEmpty(): " + urls);
215 }
216 this.urls = urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700217 }
218
219 public Builder setUsername(String username) {
220 this.username = username;
221 return this;
222 }
223
224 public Builder setPassword(String password) {
225 this.password = password;
226 return this;
227 }
228
229 public Builder setTlsCertPolicy(TlsCertPolicy tlsCertPolicy) {
230 this.tlsCertPolicy = tlsCertPolicy;
231 return this;
232 }
233
234 public Builder setHostname(String hostname) {
235 this.hostname = hostname;
236 return this;
237 }
238
239 public Builder setTlsAlpnProtocols(List<String> tlsAlpnProtocols) {
240 this.tlsAlpnProtocols = tlsAlpnProtocols;
241 return this;
242 }
243
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700244 public Builder setTlsEllipticCurves(List<String> tlsEllipticCurves) {
245 this.tlsEllipticCurves = tlsEllipticCurves;
246 return this;
247 }
248
Diogo Real1dca9d52017-08-29 12:18:32 -0700249 public IceServer createIceServer() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700250 return new IceServer(urls.get(0), urls, username, password, tlsCertPolicy, hostname,
251 tlsAlpnProtocols, tlsEllipticCurves);
Diogo Real1dca9d52017-08-29 12:18:32 -0700252 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 }
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100254
255 @CalledByNative("IceServer")
256 List<String> getUrls() {
257 return urls;
258 }
259
260 @CalledByNative("IceServer")
261 String getUsername() {
262 return username;
263 }
264
265 @CalledByNative("IceServer")
266 String getPassword() {
267 return password;
268 }
269
270 @CalledByNative("IceServer")
271 TlsCertPolicy getTlsCertPolicy() {
272 return tlsCertPolicy;
273 }
274
275 @CalledByNative("IceServer")
276 String getHostname() {
277 return hostname;
278 }
279
280 @CalledByNative("IceServer")
281 List<String> getTlsAlpnProtocols() {
282 return tlsAlpnProtocols;
283 }
284
285 @CalledByNative("IceServer")
286 List<String> getTlsEllipticCurves() {
287 return tlsEllipticCurves;
288 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289 }
290
Jiayang Liucac1b382015-04-30 12:35:24 -0700291 /** Java version of PeerConnectionInterface.IceTransportsType */
sakalb6760f92016-09-29 04:12:44 -0700292 public enum IceTransportsType { NONE, RELAY, NOHOST, ALL }
Jiayang Liucac1b382015-04-30 12:35:24 -0700293
294 /** Java version of PeerConnectionInterface.BundlePolicy */
sakalb6760f92016-09-29 04:12:44 -0700295 public enum BundlePolicy { BALANCED, MAXBUNDLE, MAXCOMPAT }
Jiayang Liucac1b382015-04-30 12:35:24 -0700296
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700297 /** Java version of PeerConnectionInterface.RtcpMuxPolicy */
sakalb6760f92016-09-29 04:12:44 -0700298 public enum RtcpMuxPolicy { NEGOTIATE, REQUIRE }
glaznev97579a42015-09-01 11:31:27 -0700299
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700300 /** Java version of PeerConnectionInterface.TcpCandidatePolicy */
sakalb6760f92016-09-29 04:12:44 -0700301 public enum TcpCandidatePolicy { ENABLED, DISABLED }
Jiayang Liucac1b382015-04-30 12:35:24 -0700302
honghaiz60347052016-05-31 18:29:12 -0700303 /** Java version of PeerConnectionInterface.CandidateNetworkPolicy */
sakalb6760f92016-09-29 04:12:44 -0700304 public enum CandidateNetworkPolicy { ALL, LOW_COST }
honghaiz60347052016-05-31 18:29:12 -0700305
glaznev97579a42015-09-01 11:31:27 -0700306 /** Java version of rtc::KeyType */
sakalb6760f92016-09-29 04:12:44 -0700307 public enum KeyType { RSA, ECDSA }
glaznev97579a42015-09-01 11:31:27 -0700308
honghaiz1f429e32015-09-28 07:57:34 -0700309 /** Java version of PeerConnectionInterface.ContinualGatheringPolicy */
sakalb6760f92016-09-29 04:12:44 -0700310 public enum ContinualGatheringPolicy { GATHER_ONCE, GATHER_CONTINUALLY }
honghaiz1f429e32015-09-28 07:57:34 -0700311
Steve Antond960a0c2017-07-17 12:33:07 -0700312 /** Java version of rtc::IntervalRange */
313 public static class IntervalRange {
314 private final int min;
315 private final int max;
316
317 public IntervalRange(int min, int max) {
318 this.min = min;
319 this.max = max;
320 }
321
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100322 @CalledByNative("IntervalRange")
Steve Antond960a0c2017-07-17 12:33:07 -0700323 public int getMin() {
324 return min;
325 }
326
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100327 @CalledByNative("IntervalRange")
Steve Antond960a0c2017-07-17 12:33:07 -0700328 public int getMax() {
329 return max;
330 }
331 }
332
Jiayang Liucac1b382015-04-30 12:35:24 -0700333 /** Java version of PeerConnectionInterface.RTCConfiguration */
334 public static class RTCConfiguration {
335 public IceTransportsType iceTransportsType;
336 public List<IceServer> iceServers;
337 public BundlePolicy bundlePolicy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700338 public RtcpMuxPolicy rtcpMuxPolicy;
Jiayang Liucac1b382015-04-30 12:35:24 -0700339 public TcpCandidatePolicy tcpCandidatePolicy;
honghaiz60347052016-05-31 18:29:12 -0700340 public CandidateNetworkPolicy candidateNetworkPolicy;
Henrik Lundin64dad832015-05-11 12:44:23 +0200341 public int audioJitterBufferMaxPackets;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200342 public boolean audioJitterBufferFastAccelerate;
honghaiz4edc39c2015-09-01 09:53:56 -0700343 public int iceConnectionReceivingTimeout;
Honghai Zhang381b4212015-12-04 12:24:03 -0800344 public int iceBackupCandidatePairPingInterval;
glaznev97579a42015-09-01 11:31:27 -0700345 public KeyType keyType;
honghaiz1f429e32015-09-28 07:57:34 -0700346 public ContinualGatheringPolicy continualGatheringPolicy;
deadbeefbe0c96f2016-05-18 16:20:14 -0700347 public int iceCandidatePoolSize;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700348 public boolean pruneTurnPorts;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700349 public boolean presumeWritableWhenFullyRelayed;
skvlad51072462017-02-02 11:50:14 -0800350 public Integer iceCheckMinInterval;
zhihuangb09b3f92017-03-07 14:40:51 -0800351 public boolean disableIPv6OnWifi;
deadbeef28e29192017-07-27 09:14:38 -0700352 // By default, PeerConnection will use a limited number of IPv6 network
353 // interfaces, in order to avoid too many ICE candidate pairs being created
354 // and delaying ICE completion.
355 //
356 // Can be set to Integer.MAX_VALUE to effectively disable the limit.
357 public int maxIPv6Networks;
Steve Antond960a0c2017-07-17 12:33:07 -0700358 public IntervalRange iceRegatherIntervalRange;
Jiayang Liucac1b382015-04-30 12:35:24 -0700359
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100360 // These values will be overridden by MediaStream constraints if deprecated constraints-based
361 // create peerconnection interface is used.
362 public boolean disableIpv6;
363 public boolean enableDscp;
364 public boolean enableCpuOveruseDetection;
365 public boolean enableRtpDataChannel;
366 public boolean suspendBelowMinBitrate;
367 public Integer screencastMinBitrate;
368 public Boolean combinedAudioVideoBwe;
369 public Boolean enableDtlsSrtp;
370
Jonas Orelandbdcee282017-10-10 14:01:40 +0200371 // This is an optional wrapper for the C++ webrtc::TurnCustomizer.
372 public TurnCustomizer turnCustomizer;
373
deadbeef28e29192017-07-27 09:14:38 -0700374 // TODO(deadbeef): Instead of duplicating the defaults here, we should do
375 // something to pick up the defaults from C++. The Objective-C equivalent
376 // of RTCConfiguration does that.
Jiayang Liucac1b382015-04-30 12:35:24 -0700377 public RTCConfiguration(List<IceServer> iceServers) {
378 iceTransportsType = IceTransportsType.ALL;
379 bundlePolicy = BundlePolicy.BALANCED;
zhihuang4dfb8ce2016-11-23 10:30:12 -0800380 rtcpMuxPolicy = RtcpMuxPolicy.REQUIRE;
Jiayang Liucac1b382015-04-30 12:35:24 -0700381 tcpCandidatePolicy = TcpCandidatePolicy.ENABLED;
Sami Kalliomäki9828beb2017-10-26 16:21:22 +0200382 candidateNetworkPolicy = CandidateNetworkPolicy.ALL;
Jiayang Liucac1b382015-04-30 12:35:24 -0700383 this.iceServers = iceServers;
Henrik Lundin64dad832015-05-11 12:44:23 +0200384 audioJitterBufferMaxPackets = 50;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200385 audioJitterBufferFastAccelerate = false;
honghaiz4edc39c2015-09-01 09:53:56 -0700386 iceConnectionReceivingTimeout = -1;
Honghai Zhang381b4212015-12-04 12:24:03 -0800387 iceBackupCandidatePairPingInterval = -1;
glaznev97579a42015-09-01 11:31:27 -0700388 keyType = KeyType.ECDSA;
honghaiz1f429e32015-09-28 07:57:34 -0700389 continualGatheringPolicy = ContinualGatheringPolicy.GATHER_ONCE;
deadbeefbe0c96f2016-05-18 16:20:14 -0700390 iceCandidatePoolSize = 0;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700391 pruneTurnPorts = false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700392 presumeWritableWhenFullyRelayed = false;
skvlad51072462017-02-02 11:50:14 -0800393 iceCheckMinInterval = null;
zhihuangb09b3f92017-03-07 14:40:51 -0800394 disableIPv6OnWifi = false;
deadbeef28e29192017-07-27 09:14:38 -0700395 maxIPv6Networks = 5;
Steve Antond960a0c2017-07-17 12:33:07 -0700396 iceRegatherIntervalRange = null;
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100397 disableIpv6 = false;
398 enableDscp = false;
399 enableCpuOveruseDetection = true;
400 enableRtpDataChannel = false;
401 suspendBelowMinBitrate = false;
402 screencastMinBitrate = null;
403 combinedAudioVideoBwe = null;
404 enableDtlsSrtp = null;
Jiayang Liucac1b382015-04-30 12:35:24 -0700405 }
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100406
407 @CalledByNative("RTCConfiguration")
408 IceTransportsType getIceTransportsType() {
409 return iceTransportsType;
410 }
411
412 @CalledByNative("RTCConfiguration")
413 List<IceServer> getIceServers() {
414 return iceServers;
415 }
416
417 @CalledByNative("RTCConfiguration")
418 BundlePolicy getBundlePolicy() {
419 return bundlePolicy;
420 }
421
422 @CalledByNative("RTCConfiguration")
423 RtcpMuxPolicy getRtcpMuxPolicy() {
424 return rtcpMuxPolicy;
425 }
426
427 @CalledByNative("RTCConfiguration")
428 TcpCandidatePolicy getTcpCandidatePolicy() {
429 return tcpCandidatePolicy;
430 }
431
432 @CalledByNative("RTCConfiguration")
433 CandidateNetworkPolicy getCandidateNetworkPolicy() {
434 return candidateNetworkPolicy;
435 }
436
437 @CalledByNative("RTCConfiguration")
438 int getAudioJitterBufferMaxPackets() {
439 return audioJitterBufferMaxPackets;
440 }
441
442 @CalledByNative("RTCConfiguration")
443 boolean getAudioJitterBufferFastAccelerate() {
444 return audioJitterBufferFastAccelerate;
445 }
446
447 @CalledByNative("RTCConfiguration")
448 int getIceConnectionReceivingTimeout() {
449 return iceConnectionReceivingTimeout;
450 }
451
452 @CalledByNative("RTCConfiguration")
453 int getIceBackupCandidatePairPingInterval() {
454 return iceBackupCandidatePairPingInterval;
455 }
456
457 @CalledByNative("RTCConfiguration")
458 KeyType getKeyType() {
459 return keyType;
460 }
461
462 @CalledByNative("RTCConfiguration")
463 ContinualGatheringPolicy getContinualGatheringPolicy() {
464 return continualGatheringPolicy;
465 }
466
467 @CalledByNative("RTCConfiguration")
468 int getIceCandidatePoolSize() {
469 return iceCandidatePoolSize;
470 }
471
472 @CalledByNative("RTCConfiguration")
473 boolean getPruneTurnPorts() {
474 return pruneTurnPorts;
475 }
476
477 @CalledByNative("RTCConfiguration")
478 boolean getPresumeWritableWhenFullyRelayed() {
479 return presumeWritableWhenFullyRelayed;
480 }
481
482 @CalledByNative("RTCConfiguration")
483 Integer getIceCheckMinInterval() {
484 return iceCheckMinInterval;
485 }
486
487 @CalledByNative("RTCConfiguration")
488 boolean getDisableIPv6OnWifi() {
489 return disableIPv6OnWifi;
490 }
491
492 @CalledByNative("RTCConfiguration")
493 int getMaxIPv6Networks() {
494 return maxIPv6Networks;
495 }
496
497 @CalledByNative("RTCConfiguration")
498 IntervalRange getIceRegatherIntervalRange() {
499 return iceRegatherIntervalRange;
500 }
501
502 @CalledByNative("RTCConfiguration")
503 TurnCustomizer getTurnCustomizer() {
504 return turnCustomizer;
505 }
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100506
507 @CalledByNative("RTCConfiguration")
508 boolean getDisableIpv6() {
509 return disableIpv6;
510 }
511
512 @CalledByNative("RTCConfiguration")
513 boolean getEnableDscp() {
514 return enableDscp;
515 }
516
517 @CalledByNative("RTCConfiguration")
518 boolean getEnableCpuOveruseDetection() {
519 return enableCpuOveruseDetection;
520 }
521
522 @CalledByNative("RTCConfiguration")
523 boolean getEnableRtpDataChannel() {
524 return enableRtpDataChannel;
525 }
526
527 @CalledByNative("RTCConfiguration")
528 boolean getSuspendBelowMinBitrate() {
529 return suspendBelowMinBitrate;
530 }
531
532 @CalledByNative("RTCConfiguration")
533 Integer getScreencastMinBitrate() {
534 return screencastMinBitrate;
535 }
536
537 @CalledByNative("RTCConfiguration")
538 Boolean getCombinedAudioVideoBwe() {
539 return combinedAudioVideoBwe;
540 }
541
542 @CalledByNative("RTCConfiguration")
543 Boolean getEnableDtlsSrtp() {
544 return enableDtlsSrtp;
545 }
Jiayang Liucac1b382015-04-30 12:35:24 -0700546 };
547
Magnus Jedvert6062f372017-11-16 16:53:12 +0100548 private final List<MediaStream> localStreams = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549 private final long nativePeerConnection;
550 private final long nativeObserver;
Magnus Jedvert6062f372017-11-16 16:53:12 +0100551 private List<RtpSender> senders = new ArrayList<>();
552 private List<RtpReceiver> receivers = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553
554 PeerConnection(long nativePeerConnection, long nativeObserver) {
555 this.nativePeerConnection = nativePeerConnection;
556 this.nativeObserver = nativeObserver;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 }
558
559 // JsepInterface.
560 public native SessionDescription getLocalDescription();
561
562 public native SessionDescription getRemoteDescription();
563
sakalb6760f92016-09-29 04:12:44 -0700564 public native DataChannel createDataChannel(String label, DataChannel.Init init);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000565
sakalb6760f92016-09-29 04:12:44 -0700566 public native void createOffer(SdpObserver observer, MediaConstraints constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567
sakalb6760f92016-09-29 04:12:44 -0700568 public native void createAnswer(SdpObserver observer, MediaConstraints constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569
sakalb6760f92016-09-29 04:12:44 -0700570 public native void setLocalDescription(SdpObserver observer, SessionDescription sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571
sakalb6760f92016-09-29 04:12:44 -0700572 public native void setRemoteDescription(SdpObserver observer, SessionDescription sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573
henrika5f6bf242017-11-01 11:06:56 +0100574 // True if remote audio should be played out. Defaults to true.
575 // Note that even if playout is enabled, streams will only be played out if
576 // the appropriate SDP is also applied. The main purpose of this API is to
577 // be able to control the exact time when audio playout starts.
578 public native void setAudioPlayout(boolean playout);
579
580 // True if local audio shall be recorded. Defaults to true.
581 // Note that even if recording is enabled, streams will only be recorded if
582 // the appropriate SDP is also applied. The main purpose of this API is to
583 // be able to control the exact time when audio recording starts.
584 public native void setAudioRecording(boolean recording);
585
deadbeef5d0b6d82017-01-09 16:05:28 -0800586 public boolean setConfiguration(RTCConfiguration config) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100587 return setNativeConfiguration(config, nativeObserver);
deadbeef5d0b6d82017-01-09 16:05:28 -0800588 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589
590 public boolean addIceCandidate(IceCandidate candidate) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100591 return addNativeIceCandidate(candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592 }
593
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700594 public boolean removeIceCandidates(final IceCandidate[] candidates) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100595 return removeNativeIceCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700596 }
597
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000598 public boolean addStream(MediaStream stream) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100599 boolean ret = addNativeLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600 if (!ret) {
601 return false;
602 }
603 localStreams.add(stream);
604 return true;
605 }
606
607 public void removeStream(MediaStream stream) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100608 removeNativeLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 localStreams.remove(stream);
610 }
611
deadbeef7a246882017-08-09 08:40:10 -0700612 /**
613 * Creates an RtpSender without a track.
614 * <p>
615 * This method allows an application to cause the PeerConnection to negotiate
616 * sending/receiving a specific media type, but without having a track to
617 * send yet.
618 * <p>
619 * When the application does want to begin sending a track, it can call
620 * RtpSender.setTrack, which doesn't require any additional SDP negotiation.
621 * <p>
622 * Example use:
623 * <pre>
624 * {@code
625 * audioSender = pc.createSender("audio", "stream1");
626 * videoSender = pc.createSender("video", "stream1");
627 * // Do normal SDP offer/answer, which will kick off ICE/DTLS and negotiate
628 * // media parameters....
629 * // Later, when the endpoint is ready to actually begin sending:
630 * audioSender.setTrack(audioTrack, false);
631 * videoSender.setTrack(videoTrack, false);
632 * }
633 * </pre>
634 * Note: This corresponds most closely to "addTransceiver" in the official
635 * WebRTC API, in that it creates a sender without a track. It was
636 * implemented before addTransceiver because it provides useful
637 * functionality, and properly implementing transceivers would have required
638 * a great deal more work.
639 *
640 * @param kind Corresponds to MediaStreamTrack kinds (must be "audio" or
641 * "video").
642 * @param stream_id The ID of the MediaStream that this sender's track will
643 * be associated with when SDP is applied to the remote
644 * PeerConnection. If createSender is used to create an
645 * audio and video sender that should be synchronized, they
646 * should use the same stream ID.
647 * @return A new RtpSender object if successful, or null otherwise.
648 */
deadbeefbd7d8f72015-12-18 16:58:44 -0800649 public RtpSender createSender(String kind, String stream_id) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100650 RtpSender new_sender = createNativeSender(kind, stream_id);
deadbeefee524f72015-12-02 11:27:40 -0800651 if (new_sender != null) {
652 senders.add(new_sender);
653 }
654 return new_sender;
655 }
656
deadbeef4139c0f2015-10-06 12:29:25 -0700657 // Note that calling getSenders will dispose of the senders previously
658 // returned (and same goes for getReceivers).
659 public List<RtpSender> getSenders() {
660 for (RtpSender sender : senders) {
661 sender.dispose();
662 }
Magnus Jedvertba700f62017-12-04 13:43:27 +0100663 senders = getNativeSenders();
deadbeef4139c0f2015-10-06 12:29:25 -0700664 return Collections.unmodifiableList(senders);
665 }
666
667 public List<RtpReceiver> getReceivers() {
668 for (RtpReceiver receiver : receivers) {
669 receiver.dispose();
670 }
Magnus Jedvertba700f62017-12-04 13:43:27 +0100671 receivers = getNativeReceivers();
deadbeef4139c0f2015-10-06 12:29:25 -0700672 return Collections.unmodifiableList(receivers);
673 }
674
deadbeef82215872017-04-18 10:27:51 -0700675 // Older, non-standard implementation of getStats.
676 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 public boolean getStats(StatsObserver observer, MediaStreamTrack track) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100678 return oldGetNativeStats(observer, (track == null) ? 0 : track.nativeTrack);
deadbeef82215872017-04-18 10:27:51 -0700679 }
680
681 // Gets stats using the new stats collection API, see webrtc/api/stats/. These
682 // will replace old stats collection API when the new API has matured enough.
683 public void getStats(RTCStatsCollectorCallback callback) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100684 newGetNativeStats(callback);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 }
686
zsteind89b0bc2017-08-03 11:11:40 -0700687 // Limits the bandwidth allocated for all RTP streams sent by this
688 // PeerConnection. Pass null to leave a value unchanged.
689 public native boolean setBitrate(Integer min, Integer current, Integer max);
690
ivoc14d5dbe2016-07-04 07:06:55 -0700691 // Starts recording an RTC event log. Ownership of the file is transfered to
692 // the native code. If an RTC event log is already being recorded, it will be
693 // stopped and a new one will start using the provided file. Logging will
694 // continue until the stopRtcEventLog function is called. The max_size_bytes
695 // argument is ignored, it is added for future use.
ivoc0c6f0f62016-07-06 04:34:23 -0700696 public boolean startRtcEventLog(int file_descriptor, int max_size_bytes) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100697 return startNativeRtcEventLog(file_descriptor, max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -0700698 }
699
700 // Stops recording an RTC event log. If no RTC event log is currently being
701 // recorded, this call will have no effect.
702 public void stopRtcEventLog() {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100703 stopNativeRtcEventLog();
ivoc14d5dbe2016-07-04 07:06:55 -0700704 }
705
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 // TODO(fischman): add support for DTMF-related methods once that API
707 // stabilizes.
708 public native SignalingState signalingState();
709
710 public native IceConnectionState iceConnectionState();
711
712 public native IceGatheringState iceGatheringState();
713
714 public native void close();
715
deadbeef43697f62017-09-12 10:52:14 -0700716 /**
717 * Free native resources associated with this PeerConnection instance.
718 * <p>
719 * This method removes a reference count from the C++ PeerConnection object,
720 * which should result in it being destroyed. It also calls equivalent
721 * "dispose" methods on the Java objects attached to this PeerConnection
722 * (streams, senders, receivers), such that their associated C++ objects
723 * will also be destroyed.
724 * <p>
725 * Note that this method cannot be safely called from an observer callback
726 * (PeerConnection.Observer, DataChannel.Observer, etc.). If you want to, for
727 * example, destroy the PeerConnection after an "ICE failed" callback, you
728 * must do this asynchronously (in other words, unwind the stack first). See
729 * <a href="https://bugs.chromium.org/p/webrtc/issues/detail?id=3721">bug
730 * 3721</a> for more details.
731 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732 public void dispose() {
733 close();
734 for (MediaStream stream : localStreams) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100735 removeNativeLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000736 stream.dispose();
737 }
738 localStreams.clear();
deadbeef4139c0f2015-10-06 12:29:25 -0700739 for (RtpSender sender : senders) {
740 sender.dispose();
741 }
742 senders.clear();
743 for (RtpReceiver receiver : receivers) {
744 receiver.dispose();
745 }
746 receivers.clear();
magjedb1c74532017-08-27 13:47:20 -0700747 JniCommon.nativeReleaseRef(nativePeerConnection);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748 freeObserver(nativeObserver);
749 }
750
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100751 @CalledByNative
752 long getNativePeerConnection() {
753 return nativePeerConnection;
754 }
755
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 private static native void freeObserver(long nativeObserver);
757
Magnus Jedvertba700f62017-12-04 13:43:27 +0100758 private native boolean setNativeConfiguration(RTCConfiguration config, long nativeObserver);
deadbeef5d0b6d82017-01-09 16:05:28 -0800759
Magnus Jedvertba700f62017-12-04 13:43:27 +0100760 private native boolean addNativeIceCandidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761 String sdpMid, int sdpMLineIndex, String iceCandidateSdp);
762
Magnus Jedvertba700f62017-12-04 13:43:27 +0100763 private native boolean removeNativeIceCandidates(final IceCandidate[] candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700764
Magnus Jedvertba700f62017-12-04 13:43:27 +0100765 private native boolean addNativeLocalStream(long nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766
Magnus Jedvertba700f62017-12-04 13:43:27 +0100767 private native void removeNativeLocalStream(long nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768
Magnus Jedvertba700f62017-12-04 13:43:27 +0100769 private native boolean oldGetNativeStats(StatsObserver observer, long nativeTrack);
deadbeef82215872017-04-18 10:27:51 -0700770
Magnus Jedvertba700f62017-12-04 13:43:27 +0100771 private native void newGetNativeStats(RTCStatsCollectorCallback callback);
deadbeef4139c0f2015-10-06 12:29:25 -0700772
Magnus Jedvertba700f62017-12-04 13:43:27 +0100773 private native RtpSender createNativeSender(String kind, String stream_id);
deadbeefee524f72015-12-02 11:27:40 -0800774
Magnus Jedvertba700f62017-12-04 13:43:27 +0100775 private native List<RtpSender> getNativeSenders();
deadbeef4139c0f2015-10-06 12:29:25 -0700776
Magnus Jedvertba700f62017-12-04 13:43:27 +0100777 private native List<RtpReceiver> getNativeReceivers();
ivoc14d5dbe2016-07-04 07:06:55 -0700778
Magnus Jedvertba700f62017-12-04 13:43:27 +0100779 private native boolean startNativeRtcEventLog(int file_descriptor, int max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -0700780
Magnus Jedvertba700f62017-12-04 13:43:27 +0100781 private native void stopNativeRtcEventLog();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782}