blob: 7c4e91d3f11dcc78cf7b41df2cfe175ec4128f56 [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
Jonas Orelandbdcee282017-10-10 14:01:40 +0200360 // This is an optional wrapper for the C++ webrtc::TurnCustomizer.
361 public TurnCustomizer turnCustomizer;
362
deadbeef28e29192017-07-27 09:14:38 -0700363 // TODO(deadbeef): Instead of duplicating the defaults here, we should do
364 // something to pick up the defaults from C++. The Objective-C equivalent
365 // of RTCConfiguration does that.
Jiayang Liucac1b382015-04-30 12:35:24 -0700366 public RTCConfiguration(List<IceServer> iceServers) {
367 iceTransportsType = IceTransportsType.ALL;
368 bundlePolicy = BundlePolicy.BALANCED;
zhihuang4dfb8ce2016-11-23 10:30:12 -0800369 rtcpMuxPolicy = RtcpMuxPolicy.REQUIRE;
Jiayang Liucac1b382015-04-30 12:35:24 -0700370 tcpCandidatePolicy = TcpCandidatePolicy.ENABLED;
Sami Kalliomäki9828beb2017-10-26 16:21:22 +0200371 candidateNetworkPolicy = CandidateNetworkPolicy.ALL;
Jiayang Liucac1b382015-04-30 12:35:24 -0700372 this.iceServers = iceServers;
Henrik Lundin64dad832015-05-11 12:44:23 +0200373 audioJitterBufferMaxPackets = 50;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200374 audioJitterBufferFastAccelerate = false;
honghaiz4edc39c2015-09-01 09:53:56 -0700375 iceConnectionReceivingTimeout = -1;
Honghai Zhang381b4212015-12-04 12:24:03 -0800376 iceBackupCandidatePairPingInterval = -1;
glaznev97579a42015-09-01 11:31:27 -0700377 keyType = KeyType.ECDSA;
honghaiz1f429e32015-09-28 07:57:34 -0700378 continualGatheringPolicy = ContinualGatheringPolicy.GATHER_ONCE;
deadbeefbe0c96f2016-05-18 16:20:14 -0700379 iceCandidatePoolSize = 0;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700380 pruneTurnPorts = false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700381 presumeWritableWhenFullyRelayed = false;
skvlad51072462017-02-02 11:50:14 -0800382 iceCheckMinInterval = null;
zhihuangb09b3f92017-03-07 14:40:51 -0800383 disableIPv6OnWifi = false;
deadbeef28e29192017-07-27 09:14:38 -0700384 maxIPv6Networks = 5;
Steve Antond960a0c2017-07-17 12:33:07 -0700385 iceRegatherIntervalRange = null;
Jiayang Liucac1b382015-04-30 12:35:24 -0700386 }
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100387
388 @CalledByNative("RTCConfiguration")
389 IceTransportsType getIceTransportsType() {
390 return iceTransportsType;
391 }
392
393 @CalledByNative("RTCConfiguration")
394 List<IceServer> getIceServers() {
395 return iceServers;
396 }
397
398 @CalledByNative("RTCConfiguration")
399 BundlePolicy getBundlePolicy() {
400 return bundlePolicy;
401 }
402
403 @CalledByNative("RTCConfiguration")
404 RtcpMuxPolicy getRtcpMuxPolicy() {
405 return rtcpMuxPolicy;
406 }
407
408 @CalledByNative("RTCConfiguration")
409 TcpCandidatePolicy getTcpCandidatePolicy() {
410 return tcpCandidatePolicy;
411 }
412
413 @CalledByNative("RTCConfiguration")
414 CandidateNetworkPolicy getCandidateNetworkPolicy() {
415 return candidateNetworkPolicy;
416 }
417
418 @CalledByNative("RTCConfiguration")
419 int getAudioJitterBufferMaxPackets() {
420 return audioJitterBufferMaxPackets;
421 }
422
423 @CalledByNative("RTCConfiguration")
424 boolean getAudioJitterBufferFastAccelerate() {
425 return audioJitterBufferFastAccelerate;
426 }
427
428 @CalledByNative("RTCConfiguration")
429 int getIceConnectionReceivingTimeout() {
430 return iceConnectionReceivingTimeout;
431 }
432
433 @CalledByNative("RTCConfiguration")
434 int getIceBackupCandidatePairPingInterval() {
435 return iceBackupCandidatePairPingInterval;
436 }
437
438 @CalledByNative("RTCConfiguration")
439 KeyType getKeyType() {
440 return keyType;
441 }
442
443 @CalledByNative("RTCConfiguration")
444 ContinualGatheringPolicy getContinualGatheringPolicy() {
445 return continualGatheringPolicy;
446 }
447
448 @CalledByNative("RTCConfiguration")
449 int getIceCandidatePoolSize() {
450 return iceCandidatePoolSize;
451 }
452
453 @CalledByNative("RTCConfiguration")
454 boolean getPruneTurnPorts() {
455 return pruneTurnPorts;
456 }
457
458 @CalledByNative("RTCConfiguration")
459 boolean getPresumeWritableWhenFullyRelayed() {
460 return presumeWritableWhenFullyRelayed;
461 }
462
463 @CalledByNative("RTCConfiguration")
464 Integer getIceCheckMinInterval() {
465 return iceCheckMinInterval;
466 }
467
468 @CalledByNative("RTCConfiguration")
469 boolean getDisableIPv6OnWifi() {
470 return disableIPv6OnWifi;
471 }
472
473 @CalledByNative("RTCConfiguration")
474 int getMaxIPv6Networks() {
475 return maxIPv6Networks;
476 }
477
478 @CalledByNative("RTCConfiguration")
479 IntervalRange getIceRegatherIntervalRange() {
480 return iceRegatherIntervalRange;
481 }
482
483 @CalledByNative("RTCConfiguration")
484 TurnCustomizer getTurnCustomizer() {
485 return turnCustomizer;
486 }
Jiayang Liucac1b382015-04-30 12:35:24 -0700487 };
488
Magnus Jedvert6062f372017-11-16 16:53:12 +0100489 private final List<MediaStream> localStreams = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 private final long nativePeerConnection;
491 private final long nativeObserver;
Magnus Jedvert6062f372017-11-16 16:53:12 +0100492 private List<RtpSender> senders = new ArrayList<>();
493 private List<RtpReceiver> receivers = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494
495 PeerConnection(long nativePeerConnection, long nativeObserver) {
496 this.nativePeerConnection = nativePeerConnection;
497 this.nativeObserver = nativeObserver;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 }
499
500 // JsepInterface.
501 public native SessionDescription getLocalDescription();
502
503 public native SessionDescription getRemoteDescription();
504
sakalb6760f92016-09-29 04:12:44 -0700505 public native DataChannel createDataChannel(String label, DataChannel.Init init);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000506
sakalb6760f92016-09-29 04:12:44 -0700507 public native void createOffer(SdpObserver observer, MediaConstraints constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508
sakalb6760f92016-09-29 04:12:44 -0700509 public native void createAnswer(SdpObserver observer, MediaConstraints constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510
sakalb6760f92016-09-29 04:12:44 -0700511 public native void setLocalDescription(SdpObserver observer, SessionDescription sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512
sakalb6760f92016-09-29 04:12:44 -0700513 public native void setRemoteDescription(SdpObserver observer, SessionDescription sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514
henrika5f6bf242017-11-01 11:06:56 +0100515 // True if remote audio should be played out. Defaults to true.
516 // Note that even if playout is enabled, streams will only be played out if
517 // the appropriate SDP is also applied. The main purpose of this API is to
518 // be able to control the exact time when audio playout starts.
519 public native void setAudioPlayout(boolean playout);
520
521 // True if local audio shall be recorded. Defaults to true.
522 // Note that even if recording is enabled, streams will only be recorded if
523 // the appropriate SDP is also applied. The main purpose of this API is to
524 // be able to control the exact time when audio recording starts.
525 public native void setAudioRecording(boolean recording);
526
deadbeef5d0b6d82017-01-09 16:05:28 -0800527 public boolean setConfiguration(RTCConfiguration config) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100528 return setNativeConfiguration(config, nativeObserver);
deadbeef5d0b6d82017-01-09 16:05:28 -0800529 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530
531 public boolean addIceCandidate(IceCandidate candidate) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100532 return addNativeIceCandidate(candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533 }
534
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700535 public boolean removeIceCandidates(final IceCandidate[] candidates) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100536 return removeNativeIceCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700537 }
538
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000539 public boolean addStream(MediaStream stream) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100540 boolean ret = addNativeLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 if (!ret) {
542 return false;
543 }
544 localStreams.add(stream);
545 return true;
546 }
547
548 public void removeStream(MediaStream stream) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100549 removeNativeLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 localStreams.remove(stream);
551 }
552
deadbeef7a246882017-08-09 08:40:10 -0700553 /**
554 * Creates an RtpSender without a track.
555 * <p>
556 * This method allows an application to cause the PeerConnection to negotiate
557 * sending/receiving a specific media type, but without having a track to
558 * send yet.
559 * <p>
560 * When the application does want to begin sending a track, it can call
561 * RtpSender.setTrack, which doesn't require any additional SDP negotiation.
562 * <p>
563 * Example use:
564 * <pre>
565 * {@code
566 * audioSender = pc.createSender("audio", "stream1");
567 * videoSender = pc.createSender("video", "stream1");
568 * // Do normal SDP offer/answer, which will kick off ICE/DTLS and negotiate
569 * // media parameters....
570 * // Later, when the endpoint is ready to actually begin sending:
571 * audioSender.setTrack(audioTrack, false);
572 * videoSender.setTrack(videoTrack, false);
573 * }
574 * </pre>
575 * Note: This corresponds most closely to "addTransceiver" in the official
576 * WebRTC API, in that it creates a sender without a track. It was
577 * implemented before addTransceiver because it provides useful
578 * functionality, and properly implementing transceivers would have required
579 * a great deal more work.
580 *
581 * @param kind Corresponds to MediaStreamTrack kinds (must be "audio" or
582 * "video").
583 * @param stream_id The ID of the MediaStream that this sender's track will
584 * be associated with when SDP is applied to the remote
585 * PeerConnection. If createSender is used to create an
586 * audio and video sender that should be synchronized, they
587 * should use the same stream ID.
588 * @return A new RtpSender object if successful, or null otherwise.
589 */
deadbeefbd7d8f72015-12-18 16:58:44 -0800590 public RtpSender createSender(String kind, String stream_id) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100591 RtpSender new_sender = createNativeSender(kind, stream_id);
deadbeefee524f72015-12-02 11:27:40 -0800592 if (new_sender != null) {
593 senders.add(new_sender);
594 }
595 return new_sender;
596 }
597
deadbeef4139c0f2015-10-06 12:29:25 -0700598 // Note that calling getSenders will dispose of the senders previously
599 // returned (and same goes for getReceivers).
600 public List<RtpSender> getSenders() {
601 for (RtpSender sender : senders) {
602 sender.dispose();
603 }
Magnus Jedvertba700f62017-12-04 13:43:27 +0100604 senders = getNativeSenders();
deadbeef4139c0f2015-10-06 12:29:25 -0700605 return Collections.unmodifiableList(senders);
606 }
607
608 public List<RtpReceiver> getReceivers() {
609 for (RtpReceiver receiver : receivers) {
610 receiver.dispose();
611 }
Magnus Jedvertba700f62017-12-04 13:43:27 +0100612 receivers = getNativeReceivers();
deadbeef4139c0f2015-10-06 12:29:25 -0700613 return Collections.unmodifiableList(receivers);
614 }
615
deadbeef82215872017-04-18 10:27:51 -0700616 // Older, non-standard implementation of getStats.
617 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 public boolean getStats(StatsObserver observer, MediaStreamTrack track) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100619 return oldGetNativeStats(observer, (track == null) ? 0 : track.nativeTrack);
deadbeef82215872017-04-18 10:27:51 -0700620 }
621
622 // Gets stats using the new stats collection API, see webrtc/api/stats/. These
623 // will replace old stats collection API when the new API has matured enough.
624 public void getStats(RTCStatsCollectorCallback callback) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100625 newGetNativeStats(callback);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 }
627
zsteind89b0bc2017-08-03 11:11:40 -0700628 // Limits the bandwidth allocated for all RTP streams sent by this
629 // PeerConnection. Pass null to leave a value unchanged.
630 public native boolean setBitrate(Integer min, Integer current, Integer max);
631
ivoc14d5dbe2016-07-04 07:06:55 -0700632 // Starts recording an RTC event log. Ownership of the file is transfered to
633 // the native code. If an RTC event log is already being recorded, it will be
634 // stopped and a new one will start using the provided file. Logging will
635 // continue until the stopRtcEventLog function is called. The max_size_bytes
636 // argument is ignored, it is added for future use.
ivoc0c6f0f62016-07-06 04:34:23 -0700637 public boolean startRtcEventLog(int file_descriptor, int max_size_bytes) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100638 return startNativeRtcEventLog(file_descriptor, max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -0700639 }
640
641 // Stops recording an RTC event log. If no RTC event log is currently being
642 // recorded, this call will have no effect.
643 public void stopRtcEventLog() {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100644 stopNativeRtcEventLog();
ivoc14d5dbe2016-07-04 07:06:55 -0700645 }
646
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 // TODO(fischman): add support for DTMF-related methods once that API
648 // stabilizes.
649 public native SignalingState signalingState();
650
651 public native IceConnectionState iceConnectionState();
652
653 public native IceGatheringState iceGatheringState();
654
655 public native void close();
656
deadbeef43697f62017-09-12 10:52:14 -0700657 /**
658 * Free native resources associated with this PeerConnection instance.
659 * <p>
660 * This method removes a reference count from the C++ PeerConnection object,
661 * which should result in it being destroyed. It also calls equivalent
662 * "dispose" methods on the Java objects attached to this PeerConnection
663 * (streams, senders, receivers), such that their associated C++ objects
664 * will also be destroyed.
665 * <p>
666 * Note that this method cannot be safely called from an observer callback
667 * (PeerConnection.Observer, DataChannel.Observer, etc.). If you want to, for
668 * example, destroy the PeerConnection after an "ICE failed" callback, you
669 * must do this asynchronously (in other words, unwind the stack first). See
670 * <a href="https://bugs.chromium.org/p/webrtc/issues/detail?id=3721">bug
671 * 3721</a> for more details.
672 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673 public void dispose() {
674 close();
675 for (MediaStream stream : localStreams) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100676 removeNativeLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 stream.dispose();
678 }
679 localStreams.clear();
deadbeef4139c0f2015-10-06 12:29:25 -0700680 for (RtpSender sender : senders) {
681 sender.dispose();
682 }
683 senders.clear();
684 for (RtpReceiver receiver : receivers) {
685 receiver.dispose();
686 }
687 receivers.clear();
magjedb1c74532017-08-27 13:47:20 -0700688 JniCommon.nativeReleaseRef(nativePeerConnection);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 freeObserver(nativeObserver);
690 }
691
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100692 @CalledByNative
693 long getNativePeerConnection() {
694 return nativePeerConnection;
695 }
696
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 private static native void freeObserver(long nativeObserver);
698
Magnus Jedvertba700f62017-12-04 13:43:27 +0100699 private native boolean setNativeConfiguration(RTCConfiguration config, long nativeObserver);
deadbeef5d0b6d82017-01-09 16:05:28 -0800700
Magnus Jedvertba700f62017-12-04 13:43:27 +0100701 private native boolean addNativeIceCandidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 String sdpMid, int sdpMLineIndex, String iceCandidateSdp);
703
Magnus Jedvertba700f62017-12-04 13:43:27 +0100704 private native boolean removeNativeIceCandidates(final IceCandidate[] candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700705
Magnus Jedvertba700f62017-12-04 13:43:27 +0100706 private native boolean addNativeLocalStream(long nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707
Magnus Jedvertba700f62017-12-04 13:43:27 +0100708 private native void removeNativeLocalStream(long nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709
Magnus Jedvertba700f62017-12-04 13:43:27 +0100710 private native boolean oldGetNativeStats(StatsObserver observer, long nativeTrack);
deadbeef82215872017-04-18 10:27:51 -0700711
Magnus Jedvertba700f62017-12-04 13:43:27 +0100712 private native void newGetNativeStats(RTCStatsCollectorCallback callback);
deadbeef4139c0f2015-10-06 12:29:25 -0700713
Magnus Jedvertba700f62017-12-04 13:43:27 +0100714 private native RtpSender createNativeSender(String kind, String stream_id);
deadbeefee524f72015-12-02 11:27:40 -0800715
Magnus Jedvertba700f62017-12-04 13:43:27 +0100716 private native List<RtpSender> getNativeSenders();
deadbeef4139c0f2015-10-06 12:29:25 -0700717
Magnus Jedvertba700f62017-12-04 13:43:27 +0100718 private native List<RtpReceiver> getNativeReceivers();
ivoc14d5dbe2016-07-04 07:06:55 -0700719
Magnus Jedvertba700f62017-12-04 13:43:27 +0100720 private native boolean startNativeRtcEventLog(int file_descriptor, int max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -0700721
Magnus Jedvertba700f62017-12-04 13:43:27 +0100722 private native void stopNativeRtcEventLog();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723}