blob: 238814d60177e7bec192ac835e6e0c58d1119639 [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
glaznev97579a42015-09-01 11:31:27 -0700307 /** Java version of rtc::KeyType */
sakalb6760f92016-09-29 04:12:44 -0700308 public enum KeyType { RSA, ECDSA }
glaznev97579a42015-09-01 11:31:27 -0700309
honghaiz1f429e32015-09-28 07:57:34 -0700310 /** Java version of PeerConnectionInterface.ContinualGatheringPolicy */
sakalb6760f92016-09-29 04:12:44 -0700311 public enum ContinualGatheringPolicy { GATHER_ONCE, GATHER_CONTINUALLY }
honghaiz1f429e32015-09-28 07:57:34 -0700312
Steve Antond960a0c2017-07-17 12:33:07 -0700313 /** Java version of rtc::IntervalRange */
314 public static class IntervalRange {
315 private final int min;
316 private final int max;
317
318 public IntervalRange(int min, int max) {
319 this.min = min;
320 this.max = max;
321 }
322
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100323 @CalledByNative("IntervalRange")
Steve Antond960a0c2017-07-17 12:33:07 -0700324 public int getMin() {
325 return min;
326 }
327
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100328 @CalledByNative("IntervalRange")
Steve Antond960a0c2017-07-17 12:33:07 -0700329 public int getMax() {
330 return max;
331 }
332 }
333
Jiayang Liucac1b382015-04-30 12:35:24 -0700334 /** Java version of PeerConnectionInterface.RTCConfiguration */
335 public static class RTCConfiguration {
336 public IceTransportsType iceTransportsType;
337 public List<IceServer> iceServers;
338 public BundlePolicy bundlePolicy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700339 public RtcpMuxPolicy rtcpMuxPolicy;
Jiayang Liucac1b382015-04-30 12:35:24 -0700340 public TcpCandidatePolicy tcpCandidatePolicy;
honghaiz60347052016-05-31 18:29:12 -0700341 public CandidateNetworkPolicy candidateNetworkPolicy;
Henrik Lundin64dad832015-05-11 12:44:23 +0200342 public int audioJitterBufferMaxPackets;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200343 public boolean audioJitterBufferFastAccelerate;
honghaiz4edc39c2015-09-01 09:53:56 -0700344 public int iceConnectionReceivingTimeout;
Honghai Zhang381b4212015-12-04 12:24:03 -0800345 public int iceBackupCandidatePairPingInterval;
glaznev97579a42015-09-01 11:31:27 -0700346 public KeyType keyType;
honghaiz1f429e32015-09-28 07:57:34 -0700347 public ContinualGatheringPolicy continualGatheringPolicy;
deadbeefbe0c96f2016-05-18 16:20:14 -0700348 public int iceCandidatePoolSize;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700349 public boolean pruneTurnPorts;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700350 public boolean presumeWritableWhenFullyRelayed;
skvlad51072462017-02-02 11:50:14 -0800351 public Integer iceCheckMinInterval;
zhihuangb09b3f92017-03-07 14:40:51 -0800352 public boolean disableIPv6OnWifi;
deadbeef28e29192017-07-27 09:14:38 -0700353 // By default, PeerConnection will use a limited number of IPv6 network
354 // interfaces, in order to avoid too many ICE candidate pairs being created
355 // and delaying ICE completion.
356 //
357 // Can be set to Integer.MAX_VALUE to effectively disable the limit.
358 public int maxIPv6Networks;
Steve Antond960a0c2017-07-17 12:33:07 -0700359 public IntervalRange iceRegatherIntervalRange;
Jiayang Liucac1b382015-04-30 12:35:24 -0700360
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100361 // These values will be overridden by MediaStream constraints if deprecated constraints-based
362 // create peerconnection interface is used.
363 public boolean disableIpv6;
364 public boolean enableDscp;
365 public boolean enableCpuOveruseDetection;
366 public boolean enableRtpDataChannel;
367 public boolean suspendBelowMinBitrate;
368 public Integer screencastMinBitrate;
369 public Boolean combinedAudioVideoBwe;
370 public Boolean enableDtlsSrtp;
371
Jonas Orelandbdcee282017-10-10 14:01:40 +0200372 // This is an optional wrapper for the C++ webrtc::TurnCustomizer.
373 public TurnCustomizer turnCustomizer;
374
deadbeef28e29192017-07-27 09:14:38 -0700375 // TODO(deadbeef): Instead of duplicating the defaults here, we should do
376 // something to pick up the defaults from C++. The Objective-C equivalent
377 // of RTCConfiguration does that.
Jiayang Liucac1b382015-04-30 12:35:24 -0700378 public RTCConfiguration(List<IceServer> iceServers) {
379 iceTransportsType = IceTransportsType.ALL;
380 bundlePolicy = BundlePolicy.BALANCED;
zhihuang4dfb8ce2016-11-23 10:30:12 -0800381 rtcpMuxPolicy = RtcpMuxPolicy.REQUIRE;
Jiayang Liucac1b382015-04-30 12:35:24 -0700382 tcpCandidatePolicy = TcpCandidatePolicy.ENABLED;
Sami Kalliomäki9828beb2017-10-26 16:21:22 +0200383 candidateNetworkPolicy = CandidateNetworkPolicy.ALL;
Jiayang Liucac1b382015-04-30 12:35:24 -0700384 this.iceServers = iceServers;
Henrik Lundin64dad832015-05-11 12:44:23 +0200385 audioJitterBufferMaxPackets = 50;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200386 audioJitterBufferFastAccelerate = false;
honghaiz4edc39c2015-09-01 09:53:56 -0700387 iceConnectionReceivingTimeout = -1;
Honghai Zhang381b4212015-12-04 12:24:03 -0800388 iceBackupCandidatePairPingInterval = -1;
glaznev97579a42015-09-01 11:31:27 -0700389 keyType = KeyType.ECDSA;
honghaiz1f429e32015-09-28 07:57:34 -0700390 continualGatheringPolicy = ContinualGatheringPolicy.GATHER_ONCE;
deadbeefbe0c96f2016-05-18 16:20:14 -0700391 iceCandidatePoolSize = 0;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700392 pruneTurnPorts = false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700393 presumeWritableWhenFullyRelayed = false;
skvlad51072462017-02-02 11:50:14 -0800394 iceCheckMinInterval = null;
zhihuangb09b3f92017-03-07 14:40:51 -0800395 disableIPv6OnWifi = false;
deadbeef28e29192017-07-27 09:14:38 -0700396 maxIPv6Networks = 5;
Steve Antond960a0c2017-07-17 12:33:07 -0700397 iceRegatherIntervalRange = null;
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100398 disableIpv6 = false;
399 enableDscp = false;
400 enableCpuOveruseDetection = true;
401 enableRtpDataChannel = false;
402 suspendBelowMinBitrate = false;
403 screencastMinBitrate = null;
404 combinedAudioVideoBwe = null;
405 enableDtlsSrtp = null;
Jiayang Liucac1b382015-04-30 12:35:24 -0700406 }
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100407
408 @CalledByNative("RTCConfiguration")
409 IceTransportsType getIceTransportsType() {
410 return iceTransportsType;
411 }
412
413 @CalledByNative("RTCConfiguration")
414 List<IceServer> getIceServers() {
415 return iceServers;
416 }
417
418 @CalledByNative("RTCConfiguration")
419 BundlePolicy getBundlePolicy() {
420 return bundlePolicy;
421 }
422
423 @CalledByNative("RTCConfiguration")
424 RtcpMuxPolicy getRtcpMuxPolicy() {
425 return rtcpMuxPolicy;
426 }
427
428 @CalledByNative("RTCConfiguration")
429 TcpCandidatePolicy getTcpCandidatePolicy() {
430 return tcpCandidatePolicy;
431 }
432
433 @CalledByNative("RTCConfiguration")
434 CandidateNetworkPolicy getCandidateNetworkPolicy() {
435 return candidateNetworkPolicy;
436 }
437
438 @CalledByNative("RTCConfiguration")
439 int getAudioJitterBufferMaxPackets() {
440 return audioJitterBufferMaxPackets;
441 }
442
443 @CalledByNative("RTCConfiguration")
444 boolean getAudioJitterBufferFastAccelerate() {
445 return audioJitterBufferFastAccelerate;
446 }
447
448 @CalledByNative("RTCConfiguration")
449 int getIceConnectionReceivingTimeout() {
450 return iceConnectionReceivingTimeout;
451 }
452
453 @CalledByNative("RTCConfiguration")
454 int getIceBackupCandidatePairPingInterval() {
455 return iceBackupCandidatePairPingInterval;
456 }
457
458 @CalledByNative("RTCConfiguration")
459 KeyType getKeyType() {
460 return keyType;
461 }
462
463 @CalledByNative("RTCConfiguration")
464 ContinualGatheringPolicy getContinualGatheringPolicy() {
465 return continualGatheringPolicy;
466 }
467
468 @CalledByNative("RTCConfiguration")
469 int getIceCandidatePoolSize() {
470 return iceCandidatePoolSize;
471 }
472
473 @CalledByNative("RTCConfiguration")
474 boolean getPruneTurnPorts() {
475 return pruneTurnPorts;
476 }
477
478 @CalledByNative("RTCConfiguration")
479 boolean getPresumeWritableWhenFullyRelayed() {
480 return presumeWritableWhenFullyRelayed;
481 }
482
483 @CalledByNative("RTCConfiguration")
484 Integer getIceCheckMinInterval() {
485 return iceCheckMinInterval;
486 }
487
488 @CalledByNative("RTCConfiguration")
489 boolean getDisableIPv6OnWifi() {
490 return disableIPv6OnWifi;
491 }
492
493 @CalledByNative("RTCConfiguration")
494 int getMaxIPv6Networks() {
495 return maxIPv6Networks;
496 }
497
498 @CalledByNative("RTCConfiguration")
499 IntervalRange getIceRegatherIntervalRange() {
500 return iceRegatherIntervalRange;
501 }
502
503 @CalledByNative("RTCConfiguration")
504 TurnCustomizer getTurnCustomizer() {
505 return turnCustomizer;
506 }
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100507
508 @CalledByNative("RTCConfiguration")
509 boolean getDisableIpv6() {
510 return disableIpv6;
511 }
512
513 @CalledByNative("RTCConfiguration")
514 boolean getEnableDscp() {
515 return enableDscp;
516 }
517
518 @CalledByNative("RTCConfiguration")
519 boolean getEnableCpuOveruseDetection() {
520 return enableCpuOveruseDetection;
521 }
522
523 @CalledByNative("RTCConfiguration")
524 boolean getEnableRtpDataChannel() {
525 return enableRtpDataChannel;
526 }
527
528 @CalledByNative("RTCConfiguration")
529 boolean getSuspendBelowMinBitrate() {
530 return suspendBelowMinBitrate;
531 }
532
533 @CalledByNative("RTCConfiguration")
534 Integer getScreencastMinBitrate() {
535 return screencastMinBitrate;
536 }
537
538 @CalledByNative("RTCConfiguration")
539 Boolean getCombinedAudioVideoBwe() {
540 return combinedAudioVideoBwe;
541 }
542
543 @CalledByNative("RTCConfiguration")
544 Boolean getEnableDtlsSrtp() {
545 return enableDtlsSrtp;
546 }
Jiayang Liucac1b382015-04-30 12:35:24 -0700547 };
548
Magnus Jedvert6062f372017-11-16 16:53:12 +0100549 private final List<MediaStream> localStreams = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 private final long nativePeerConnection;
551 private final long nativeObserver;
Magnus Jedvert6062f372017-11-16 16:53:12 +0100552 private List<RtpSender> senders = new ArrayList<>();
553 private List<RtpReceiver> receivers = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554
Sami Kalliomäki1ece1ed2017-12-20 11:59:22 +0100555 /**
556 * Wraps a PeerConnection created by the factory. Can be used by clients that want to implement
557 * their PeerConnection creation in JNI.
558 */
559 public PeerConnection(NativePeerConnectionFactory factory) {
560 this(factory.createNativePeerConnection(), 0 /* nativeObserver */);
561 }
562
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563 PeerConnection(long nativePeerConnection, long nativeObserver) {
564 this.nativePeerConnection = nativePeerConnection;
565 this.nativeObserver = nativeObserver;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 }
567
568 // JsepInterface.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100569 public SessionDescription getLocalDescription() {
570 return nativeGetLocalDescription();
571 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100573 public SessionDescription getRemoteDescription() {
574 return nativeGetRemoteDescription();
575 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100577 public DataChannel createDataChannel(String label, DataChannel.Init init) {
578 return nativeCreateDataChannel(label, init);
579 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000580
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100581 public void createOffer(SdpObserver observer, MediaConstraints constraints) {
582 nativeCreateOffer(observer, constraints);
583 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100585 public void createAnswer(SdpObserver observer, MediaConstraints constraints) {
586 nativeCreateAnswer(observer, constraints);
587 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100589 public void setLocalDescription(SdpObserver observer, SessionDescription sdp) {
590 nativeSetLocalDescription(observer, sdp);
591 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100593 public void setRemoteDescription(SdpObserver observer, SessionDescription sdp) {
594 nativeSetRemoteDescription(observer, sdp);
595 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596
henrika5f6bf242017-11-01 11:06:56 +0100597 // True if remote audio should be played out. Defaults to true.
598 // Note that even if playout is enabled, streams will only be played out if
599 // the appropriate SDP is also applied. The main purpose of this API is to
600 // be able to control the exact time when audio playout starts.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100601 public void setAudioPlayout(boolean playout) {
602 nativeSetAudioPlayout(playout);
603 }
henrika5f6bf242017-11-01 11:06:56 +0100604
605 // True if local audio shall be recorded. Defaults to true.
606 // Note that even if recording is enabled, streams will only be recorded if
607 // the appropriate SDP is also applied. The main purpose of this API is to
608 // be able to control the exact time when audio recording starts.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100609 public void setAudioRecording(boolean recording) {
610 nativeSetAudioRecording(recording);
611 }
henrika5f6bf242017-11-01 11:06:56 +0100612
deadbeef5d0b6d82017-01-09 16:05:28 -0800613 public boolean setConfiguration(RTCConfiguration config) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100614 return nativeSetConfiguration(config, nativeObserver);
deadbeef5d0b6d82017-01-09 16:05:28 -0800615 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616
617 public boolean addIceCandidate(IceCandidate candidate) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100618 return nativeAddIceCandidate(candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 }
620
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700621 public boolean removeIceCandidates(final IceCandidate[] candidates) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100622 return nativeRemoveIceCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700623 }
624
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000625 public boolean addStream(MediaStream stream) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100626 boolean ret = nativeAddLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627 if (!ret) {
628 return false;
629 }
630 localStreams.add(stream);
631 return true;
632 }
633
634 public void removeStream(MediaStream stream) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100635 nativeRemoveLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636 localStreams.remove(stream);
637 }
638
deadbeef7a246882017-08-09 08:40:10 -0700639 /**
640 * Creates an RtpSender without a track.
641 * <p>
642 * This method allows an application to cause the PeerConnection to negotiate
643 * sending/receiving a specific media type, but without having a track to
644 * send yet.
645 * <p>
646 * When the application does want to begin sending a track, it can call
647 * RtpSender.setTrack, which doesn't require any additional SDP negotiation.
648 * <p>
649 * Example use:
650 * <pre>
651 * {@code
652 * audioSender = pc.createSender("audio", "stream1");
653 * videoSender = pc.createSender("video", "stream1");
654 * // Do normal SDP offer/answer, which will kick off ICE/DTLS and negotiate
655 * // media parameters....
656 * // Later, when the endpoint is ready to actually begin sending:
657 * audioSender.setTrack(audioTrack, false);
658 * videoSender.setTrack(videoTrack, false);
659 * }
660 * </pre>
661 * Note: This corresponds most closely to "addTransceiver" in the official
662 * WebRTC API, in that it creates a sender without a track. It was
663 * implemented before addTransceiver because it provides useful
664 * functionality, and properly implementing transceivers would have required
665 * a great deal more work.
666 *
667 * @param kind Corresponds to MediaStreamTrack kinds (must be "audio" or
668 * "video").
669 * @param stream_id The ID of the MediaStream that this sender's track will
670 * be associated with when SDP is applied to the remote
671 * PeerConnection. If createSender is used to create an
672 * audio and video sender that should be synchronized, they
673 * should use the same stream ID.
674 * @return A new RtpSender object if successful, or null otherwise.
675 */
deadbeefbd7d8f72015-12-18 16:58:44 -0800676 public RtpSender createSender(String kind, String stream_id) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100677 RtpSender new_sender = nativeCreateSender(kind, stream_id);
deadbeefee524f72015-12-02 11:27:40 -0800678 if (new_sender != null) {
679 senders.add(new_sender);
680 }
681 return new_sender;
682 }
683
deadbeef4139c0f2015-10-06 12:29:25 -0700684 // Note that calling getSenders will dispose of the senders previously
685 // returned (and same goes for getReceivers).
686 public List<RtpSender> getSenders() {
687 for (RtpSender sender : senders) {
688 sender.dispose();
689 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100690 senders = nativeGetSenders();
deadbeef4139c0f2015-10-06 12:29:25 -0700691 return Collections.unmodifiableList(senders);
692 }
693
694 public List<RtpReceiver> getReceivers() {
695 for (RtpReceiver receiver : receivers) {
696 receiver.dispose();
697 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100698 receivers = nativeGetReceivers();
deadbeef4139c0f2015-10-06 12:29:25 -0700699 return Collections.unmodifiableList(receivers);
700 }
701
deadbeef82215872017-04-18 10:27:51 -0700702 // Older, non-standard implementation of getStats.
703 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704 public boolean getStats(StatsObserver observer, MediaStreamTrack track) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100705 return nativeOldGetStats(observer, (track == null) ? 0 : track.nativeTrack);
deadbeef82215872017-04-18 10:27:51 -0700706 }
707
708 // Gets stats using the new stats collection API, see webrtc/api/stats/. These
709 // will replace old stats collection API when the new API has matured enough.
710 public void getStats(RTCStatsCollectorCallback callback) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100711 nativeNewGetStats(callback);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 }
713
zsteind89b0bc2017-08-03 11:11:40 -0700714 // Limits the bandwidth allocated for all RTP streams sent by this
715 // PeerConnection. Pass null to leave a value unchanged.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100716 public boolean setBitrate(Integer min, Integer current, Integer max) {
717 return nativeSetBitrate(min, current, max);
718 }
zsteind89b0bc2017-08-03 11:11:40 -0700719
ivoc14d5dbe2016-07-04 07:06:55 -0700720 // Starts recording an RTC event log. Ownership of the file is transfered to
721 // the native code. If an RTC event log is already being recorded, it will be
722 // stopped and a new one will start using the provided file. Logging will
723 // continue until the stopRtcEventLog function is called. The max_size_bytes
724 // argument is ignored, it is added for future use.
ivoc0c6f0f62016-07-06 04:34:23 -0700725 public boolean startRtcEventLog(int file_descriptor, int max_size_bytes) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100726 return nativeStartRtcEventLog(file_descriptor, max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -0700727 }
728
729 // Stops recording an RTC event log. If no RTC event log is currently being
730 // recorded, this call will have no effect.
731 public void stopRtcEventLog() {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100732 nativeStopRtcEventLog();
ivoc14d5dbe2016-07-04 07:06:55 -0700733 }
734
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 // TODO(fischman): add support for DTMF-related methods once that API
736 // stabilizes.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100737 public SignalingState signalingState() {
738 return nativeSignalingState();
739 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100741 public IceConnectionState iceConnectionState() {
742 return nativeIceConnectionState();
743 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100745 public IceGatheringState iceGatheringState() {
746 return nativeIceGatheringState();
747 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100749 public void close() {
750 nativeClose();
751 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752
deadbeef43697f62017-09-12 10:52:14 -0700753 /**
754 * Free native resources associated with this PeerConnection instance.
755 * <p>
756 * This method removes a reference count from the C++ PeerConnection object,
757 * which should result in it being destroyed. It also calls equivalent
758 * "dispose" methods on the Java objects attached to this PeerConnection
759 * (streams, senders, receivers), such that their associated C++ objects
760 * will also be destroyed.
761 * <p>
762 * Note that this method cannot be safely called from an observer callback
763 * (PeerConnection.Observer, DataChannel.Observer, etc.). If you want to, for
764 * example, destroy the PeerConnection after an "ICE failed" callback, you
765 * must do this asynchronously (in other words, unwind the stack first). See
766 * <a href="https://bugs.chromium.org/p/webrtc/issues/detail?id=3721">bug
767 * 3721</a> for more details.
768 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 public void dispose() {
770 close();
771 for (MediaStream stream : localStreams) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100772 nativeRemoveLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 stream.dispose();
774 }
775 localStreams.clear();
deadbeef4139c0f2015-10-06 12:29:25 -0700776 for (RtpSender sender : senders) {
777 sender.dispose();
778 }
779 senders.clear();
780 for (RtpReceiver receiver : receivers) {
781 receiver.dispose();
782 }
783 receivers.clear();
magjedb1c74532017-08-27 13:47:20 -0700784 JniCommon.nativeReleaseRef(nativePeerConnection);
Sami Kalliomäki1ece1ed2017-12-20 11:59:22 +0100785 if (nativeObserver != 0) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100786 nativeFreePeerConnectionObserver(nativeObserver);
Sami Kalliomäki1ece1ed2017-12-20 11:59:22 +0100787 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 }
789
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100790 @CalledByNative
Sami Kalliomäki84804b02017-12-21 13:35:53 +0100791 public long getNativePeerConnection() {
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100792 return nativePeerConnection;
793 }
794
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100795 public static long createNativePeerConnectionObserver(Observer observer) {
796 return nativeCreatePeerConnectionObserver(observer);
797 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100799 public static void freeNativePeerConnectionObserver(long observer) {
800 nativeFreePeerConnectionObserver(observer);
801 }
deadbeef5d0b6d82017-01-09 16:05:28 -0800802
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100803 private native SessionDescription nativeGetLocalDescription();
804 private native SessionDescription nativeGetRemoteDescription();
805 private native DataChannel nativeCreateDataChannel(String label, DataChannel.Init init);
806 private native void nativeCreateOffer(SdpObserver observer, MediaConstraints constraints);
807 private native void nativeCreateAnswer(SdpObserver observer, MediaConstraints constraints);
808 private native void nativeSetLocalDescription(SdpObserver observer, SessionDescription sdp);
809 private native void nativeSetRemoteDescription(SdpObserver observer, SessionDescription sdp);
810 private native void nativeSetAudioPlayout(boolean playout);
811 private native void nativeSetAudioRecording(boolean recording);
812 private native boolean nativeSetBitrate(Integer min, Integer current, Integer max);
813 private native SignalingState nativeSignalingState();
814 private native IceConnectionState nativeIceConnectionState();
815 private native IceGatheringState nativeIceGatheringState();
816 private native void nativeClose();
817 private static native long nativeCreatePeerConnectionObserver(Observer observer);
818 private static native void nativeFreePeerConnectionObserver(long observer);
819 private native boolean nativeSetConfiguration(RTCConfiguration config, long nativeObserver);
820 private native boolean nativeAddIceCandidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 String sdpMid, int sdpMLineIndex, String iceCandidateSdp);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100822 private native boolean nativeRemoveIceCandidates(final IceCandidate[] candidates);
823 private native boolean nativeAddLocalStream(long stream);
824 private native void nativeRemoveLocalStream(long stream);
825 private native boolean nativeOldGetStats(StatsObserver observer, long nativeTrack);
826 private native void nativeNewGetStats(RTCStatsCollectorCallback callback);
827 private native RtpSender nativeCreateSender(String kind, String stream_id);
828 private native List<RtpSender> nativeGetSenders();
829 private native List<RtpReceiver> nativeGetReceivers();
830 private native boolean nativeStartRtcEventLog(int file_descriptor, int max_size_bytes);
831 private native void nativeStopRtcEventLog();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832}