blob: 2f9adcf962103781b29b2e61fb99f4693f37dcf4 [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;
Sami Kalliomäkie7592d82018-03-22 13:32:44 +010016import javax.annotation.Nullable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017
18/**
19 * Java-land version of the PeerConnection APIs; wraps the C++ API
20 * http://www.webrtc.org/reference/native-apis, which in turn is inspired by the
21 * JS APIs: http://dev.w3.org/2011/webrtc/editor/webrtc.html and
22 * http://www.w3.org/TR/mediacapture-streams/
23 */
24public 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);
Seth Hampson31dbc242018-05-07 09:28:19 -0700111
112 /**
113 * Triggered when the signaling from SetRemoteDescription indicates that a transceiver
114 * will be receiving media from a remote endpoint. This is only called if UNIFIED_PLAN
115 * semantics are specified. The transceiver will be disposed automatically.
116 */
117 @CalledByNative("Observer") default void onTrack(RtpTransceiver transceiver){};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 }
119
120 /** Java version of PeerConnectionInterface.IceServer. */
121 public static class IceServer {
Emad Omaradab1d2d2017-06-16 15:43:11 -0700122 // List of URIs associated with this server. Valid formats are described
123 // in RFC7064 and RFC7065, and more may be added in the future. The "host"
124 // part of the URI may contain either an IP address or a hostname.
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700125 @Deprecated public final String uri;
126 public final List<String> urls;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 public final String username;
128 public final String password;
hnsl04833622017-01-09 08:35:45 -0800129 public final TlsCertPolicy tlsCertPolicy;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130
Emad Omaradab1d2d2017-06-16 15:43:11 -0700131 // If the URIs in |urls| only contain IP addresses, this field can be used
132 // to indicate the hostname, which may be necessary for TLS (using the SNI
133 // extension). If |urls| itself contains the hostname, this isn't
134 // necessary.
135 public final String hostname;
136
Diogo Real1dca9d52017-08-29 12:18:32 -0700137 // List of protocols to be used in the TLS ALPN extension.
138 public final List<String> tlsAlpnProtocols;
139
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700140 // List of elliptic curves to be used in the TLS elliptic curves extension.
141 // Only curve names supported by OpenSSL should be used (eg. "P-256","X25519").
142 public final List<String> tlsEllipticCurves;
143
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 /** Convenience constructor for STUN servers. */
Diogo Real05ea2b32017-08-31 00:12:58 -0700145 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 public IceServer(String uri) {
147 this(uri, "", "");
148 }
149
Diogo Real05ea2b32017-08-31 00:12:58 -0700150 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 public IceServer(String uri, String username, String password) {
hnsl04833622017-01-09 08:35:45 -0800152 this(uri, username, password, TlsCertPolicy.TLS_CERT_POLICY_SECURE);
153 }
154
Diogo Real05ea2b32017-08-31 00:12:58 -0700155 @Deprecated
hnsl04833622017-01-09 08:35:45 -0800156 public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy) {
Emad Omaradab1d2d2017-06-16 15:43:11 -0700157 this(uri, username, password, tlsCertPolicy, "");
158 }
159
Diogo Real05ea2b32017-08-31 00:12:58 -0700160 @Deprecated
Emad Omaradab1d2d2017-06-16 15:43:11 -0700161 public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy,
162 String hostname) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700163 this(uri, Collections.singletonList(uri), username, password, tlsCertPolicy, hostname, null,
164 null);
Diogo Real1dca9d52017-08-29 12:18:32 -0700165 }
166
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700167 private IceServer(String uri, List<String> urls, String username, String password,
168 TlsCertPolicy tlsCertPolicy, String hostname, List<String> tlsAlpnProtocols,
169 List<String> tlsEllipticCurves) {
170 if (uri == null || urls == null || urls.isEmpty()) {
171 throw new IllegalArgumentException("uri == null || urls == null || urls.isEmpty()");
172 }
173 for (String it : urls) {
174 if (it == null) {
175 throw new IllegalArgumentException("urls element is null: " + urls);
176 }
177 }
178 if (username == null) {
179 throw new IllegalArgumentException("username == null");
180 }
181 if (password == null) {
182 throw new IllegalArgumentException("password == null");
183 }
184 if (hostname == null) {
185 throw new IllegalArgumentException("hostname == null");
186 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 this.uri = uri;
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700188 this.urls = urls;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 this.username = username;
190 this.password = password;
hnsl04833622017-01-09 08:35:45 -0800191 this.tlsCertPolicy = tlsCertPolicy;
Emad Omaradab1d2d2017-06-16 15:43:11 -0700192 this.hostname = hostname;
Diogo Real1dca9d52017-08-29 12:18:32 -0700193 this.tlsAlpnProtocols = tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700194 this.tlsEllipticCurves = tlsEllipticCurves;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 }
196
Sami Kalliomäkibde473e2017-10-30 13:34:41 +0100197 @Override
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 public String toString() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700199 return urls + " [" + username + ":" + password + "] [" + tlsCertPolicy + "] [" + hostname
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700200 + "] [" + tlsAlpnProtocols + "] [" + tlsEllipticCurves + "]";
Diogo Real1dca9d52017-08-29 12:18:32 -0700201 }
202
203 public static Builder builder(String uri) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700204 return new Builder(Collections.singletonList(uri));
205 }
206
207 public static Builder builder(List<String> urls) {
208 return new Builder(urls);
Diogo Real1dca9d52017-08-29 12:18:32 -0700209 }
210
211 public static class Builder {
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100212 @Nullable private final List<String> urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700213 private String username = "";
214 private String password = "";
215 private TlsCertPolicy tlsCertPolicy = TlsCertPolicy.TLS_CERT_POLICY_SECURE;
216 private String hostname = "";
217 private List<String> tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700218 private List<String> tlsEllipticCurves;
Diogo Real1dca9d52017-08-29 12:18:32 -0700219
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700220 private Builder(List<String> urls) {
221 if (urls == null || urls.isEmpty()) {
222 throw new IllegalArgumentException("urls == null || urls.isEmpty(): " + urls);
223 }
224 this.urls = urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700225 }
226
227 public Builder setUsername(String username) {
228 this.username = username;
229 return this;
230 }
231
232 public Builder setPassword(String password) {
233 this.password = password;
234 return this;
235 }
236
237 public Builder setTlsCertPolicy(TlsCertPolicy tlsCertPolicy) {
238 this.tlsCertPolicy = tlsCertPolicy;
239 return this;
240 }
241
242 public Builder setHostname(String hostname) {
243 this.hostname = hostname;
244 return this;
245 }
246
247 public Builder setTlsAlpnProtocols(List<String> tlsAlpnProtocols) {
248 this.tlsAlpnProtocols = tlsAlpnProtocols;
249 return this;
250 }
251
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700252 public Builder setTlsEllipticCurves(List<String> tlsEllipticCurves) {
253 this.tlsEllipticCurves = tlsEllipticCurves;
254 return this;
255 }
256
Diogo Real1dca9d52017-08-29 12:18:32 -0700257 public IceServer createIceServer() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700258 return new IceServer(urls.get(0), urls, username, password, tlsCertPolicy, hostname,
259 tlsAlpnProtocols, tlsEllipticCurves);
Diogo Real1dca9d52017-08-29 12:18:32 -0700260 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 }
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100262
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100263 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100264 @CalledByNative("IceServer")
265 List<String> getUrls() {
266 return urls;
267 }
268
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100269 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100270 @CalledByNative("IceServer")
271 String getUsername() {
272 return username;
273 }
274
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100275 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100276 @CalledByNative("IceServer")
277 String getPassword() {
278 return password;
279 }
280
281 @CalledByNative("IceServer")
282 TlsCertPolicy getTlsCertPolicy() {
283 return tlsCertPolicy;
284 }
285
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100286 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100287 @CalledByNative("IceServer")
288 String getHostname() {
289 return hostname;
290 }
291
292 @CalledByNative("IceServer")
293 List<String> getTlsAlpnProtocols() {
294 return tlsAlpnProtocols;
295 }
296
297 @CalledByNative("IceServer")
298 List<String> getTlsEllipticCurves() {
299 return tlsEllipticCurves;
300 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301 }
302
Jiayang Liucac1b382015-04-30 12:35:24 -0700303 /** Java version of PeerConnectionInterface.IceTransportsType */
sakalb6760f92016-09-29 04:12:44 -0700304 public enum IceTransportsType { NONE, RELAY, NOHOST, ALL }
Jiayang Liucac1b382015-04-30 12:35:24 -0700305
306 /** Java version of PeerConnectionInterface.BundlePolicy */
sakalb6760f92016-09-29 04:12:44 -0700307 public enum BundlePolicy { BALANCED, MAXBUNDLE, MAXCOMPAT }
Jiayang Liucac1b382015-04-30 12:35:24 -0700308
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700309 /** Java version of PeerConnectionInterface.RtcpMuxPolicy */
sakalb6760f92016-09-29 04:12:44 -0700310 public enum RtcpMuxPolicy { NEGOTIATE, REQUIRE }
glaznev97579a42015-09-01 11:31:27 -0700311
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700312 /** Java version of PeerConnectionInterface.TcpCandidatePolicy */
sakalb6760f92016-09-29 04:12:44 -0700313 public enum TcpCandidatePolicy { ENABLED, DISABLED }
Jiayang Liucac1b382015-04-30 12:35:24 -0700314
honghaiz60347052016-05-31 18:29:12 -0700315 /** Java version of PeerConnectionInterface.CandidateNetworkPolicy */
sakalb6760f92016-09-29 04:12:44 -0700316 public enum CandidateNetworkPolicy { ALL, LOW_COST }
honghaiz60347052016-05-31 18:29:12 -0700317
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800318 // Keep in sync with webrtc/rtc_base/network_constants.h.
319 public enum AdapterType {
320 UNKNOWN,
321 ETHERNET,
322 WIFI,
323 CELLULAR,
324 VPN,
325 LOOPBACK,
326 }
327
glaznev97579a42015-09-01 11:31:27 -0700328 /** Java version of rtc::KeyType */
sakalb6760f92016-09-29 04:12:44 -0700329 public enum KeyType { RSA, ECDSA }
glaznev97579a42015-09-01 11:31:27 -0700330
honghaiz1f429e32015-09-28 07:57:34 -0700331 /** Java version of PeerConnectionInterface.ContinualGatheringPolicy */
sakalb6760f92016-09-29 04:12:44 -0700332 public enum ContinualGatheringPolicy { GATHER_ONCE, GATHER_CONTINUALLY }
honghaiz1f429e32015-09-28 07:57:34 -0700333
Steve Antond960a0c2017-07-17 12:33:07 -0700334 /** Java version of rtc::IntervalRange */
335 public static class IntervalRange {
336 private final int min;
337 private final int max;
338
339 public IntervalRange(int min, int max) {
340 this.min = min;
341 this.max = max;
342 }
343
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100344 @CalledByNative("IntervalRange")
Steve Antond960a0c2017-07-17 12:33:07 -0700345 public int getMin() {
346 return min;
347 }
348
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100349 @CalledByNative("IntervalRange")
Steve Antond960a0c2017-07-17 12:33:07 -0700350 public int getMax() {
351 return max;
352 }
353 }
354
Seth Hampsonc384e142018-03-06 15:47:10 -0800355 /**
356 * Java version of webrtc::SdpSemantics.
357 *
358 * Configure the SDP semantics used by this PeerConnection. Note that the
359 * WebRTC 1.0 specification requires UNIFIED_PLAN semantics. The
360 * RtpTransceiver API is only available with UNIFIED_PLAN semantics.
361 *
362 * <p>PLAN_B will cause PeerConnection to create offers and answers with at
363 * most one audio and one video m= section with multiple RtpSenders and
364 * RtpReceivers specified as multiple a=ssrc lines within the section. This
365 * will also cause PeerConnection to ignore all but the first m= section of
366 * the same media type.
367 *
368 * <p>UNIFIED_PLAN will cause PeerConnection to create offers and answers with
369 * multiple m= sections where each m= section maps to one RtpSender and one
370 * RtpReceiver (an RtpTransceiver), either both audio or both video. This
371 * will also cause PeerConnection to ignore all but the first a=ssrc lines
372 * that form a Plan B stream.
373 *
374 * <p>For users who wish to send multiple audio/video streams and need to stay
375 * interoperable with legacy WebRTC implementations, specify PLAN_B.
376 *
377 * <p>For users who wish to send multiple audio/video streams and/or wish to
378 * use the new RtpTransceiver API, specify UNIFIED_PLAN.
379 */
380 public enum SdpSemantics { PLAN_B, UNIFIED_PLAN }
381
Jiayang Liucac1b382015-04-30 12:35:24 -0700382 /** Java version of PeerConnectionInterface.RTCConfiguration */
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800383 // TODO(qingsi): Resolve the naming inconsistency of fields with/without units.
Jiayang Liucac1b382015-04-30 12:35:24 -0700384 public static class RTCConfiguration {
385 public IceTransportsType iceTransportsType;
386 public List<IceServer> iceServers;
387 public BundlePolicy bundlePolicy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700388 public RtcpMuxPolicy rtcpMuxPolicy;
Jiayang Liucac1b382015-04-30 12:35:24 -0700389 public TcpCandidatePolicy tcpCandidatePolicy;
honghaiz60347052016-05-31 18:29:12 -0700390 public CandidateNetworkPolicy candidateNetworkPolicy;
Henrik Lundin64dad832015-05-11 12:44:23 +0200391 public int audioJitterBufferMaxPackets;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200392 public boolean audioJitterBufferFastAccelerate;
honghaiz4edc39c2015-09-01 09:53:56 -0700393 public int iceConnectionReceivingTimeout;
Honghai Zhang381b4212015-12-04 12:24:03 -0800394 public int iceBackupCandidatePairPingInterval;
glaznev97579a42015-09-01 11:31:27 -0700395 public KeyType keyType;
honghaiz1f429e32015-09-28 07:57:34 -0700396 public ContinualGatheringPolicy continualGatheringPolicy;
deadbeefbe0c96f2016-05-18 16:20:14 -0700397 public int iceCandidatePoolSize;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700398 public boolean pruneTurnPorts;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700399 public boolean presumeWritableWhenFullyRelayed;
Qingsi Wange6826d22018-03-08 14:55:14 -0800400 // The following fields define intervals in milliseconds at which ICE
401 // connectivity checks are sent.
402 //
403 // We consider ICE is "strongly connected" for an agent when there is at
404 // least one candidate pair that currently succeeds in connectivity check
405 // from its direction i.e. sending a ping and receives a ping response, AND
406 // all candidate pairs have sent a minimum number of pings for connectivity
407 // (this number is implementation-specific). Otherwise, ICE is considered in
408 // "weak connectivity".
409 //
410 // Note that the above notion of strong and weak connectivity is not defined
411 // in RFC 5245, and they apply to our current ICE implementation only.
412 //
413 // 1) iceCheckIntervalStrongConnectivityMs defines the interval applied to
414 // ALL candidate pairs when ICE is strongly connected,
415 // 2) iceCheckIntervalWeakConnectivityMs defines the counterpart for ALL
416 // pairs when ICE is weakly connected, and
417 // 3) iceCheckMinInterval defines the minimal interval (equivalently the
418 // maximum rate) that overrides the above two intervals when either of them
419 // is less.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100420 @Nullable public Integer iceCheckIntervalStrongConnectivityMs;
421 @Nullable public Integer iceCheckIntervalWeakConnectivityMs;
422 @Nullable public Integer iceCheckMinInterval;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700423 // The time period in milliseconds for which a candidate pair must wait for response to
424 // connectivitiy checks before it becomes unwritable.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100425 @Nullable public Integer iceUnwritableTimeMs;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700426 // The minimum number of connectivity checks that a candidate pair must sent without receiving
427 // response before it becomes unwritable.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100428 @Nullable public Integer iceUnwritableMinChecks;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800429 // The interval in milliseconds at which STUN candidates will resend STUN binding requests
430 // to keep NAT bindings open.
431 // The default value in the implementation is used if this field is null.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100432 @Nullable public Integer stunCandidateKeepaliveIntervalMs;
zhihuangb09b3f92017-03-07 14:40:51 -0800433 public boolean disableIPv6OnWifi;
deadbeef28e29192017-07-27 09:14:38 -0700434 // By default, PeerConnection will use a limited number of IPv6 network
435 // interfaces, in order to avoid too many ICE candidate pairs being created
436 // and delaying ICE completion.
437 //
438 // Can be set to Integer.MAX_VALUE to effectively disable the limit.
439 public int maxIPv6Networks;
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100440 @Nullable public IntervalRange iceRegatherIntervalRange;
Jiayang Liucac1b382015-04-30 12:35:24 -0700441
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100442 // These values will be overridden by MediaStream constraints if deprecated constraints-based
443 // create peerconnection interface is used.
444 public boolean disableIpv6;
445 public boolean enableDscp;
446 public boolean enableCpuOveruseDetection;
447 public boolean enableRtpDataChannel;
448 public boolean suspendBelowMinBitrate;
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100449 @Nullable public Integer screencastMinBitrate;
450 @Nullable public Boolean combinedAudioVideoBwe;
451 @Nullable public Boolean enableDtlsSrtp;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800452 // Use "Unknown" to represent no preference of adapter types, not the
453 // preference of adapters of unknown types.
454 public AdapterType networkPreference;
Seth Hampsonc384e142018-03-06 15:47:10 -0800455 public SdpSemantics sdpSemantics;
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100456
Jonas Orelandbdcee282017-10-10 14:01:40 +0200457 // This is an optional wrapper for the C++ webrtc::TurnCustomizer.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100458 @Nullable public TurnCustomizer turnCustomizer;
Jonas Orelandbdcee282017-10-10 14:01:40 +0200459
Zhi Huangb57e1692018-06-12 11:41:11 -0700460 // Actively reset the SRTP parameters whenever the DTLS transports underneath are reset for
461 // every offer/answer negotiation.This is only intended to be a workaround for crbug.com/835958
462 public boolean activeResetSrtpParams;
463
deadbeef28e29192017-07-27 09:14:38 -0700464 // TODO(deadbeef): Instead of duplicating the defaults here, we should do
465 // something to pick up the defaults from C++. The Objective-C equivalent
466 // of RTCConfiguration does that.
Jiayang Liucac1b382015-04-30 12:35:24 -0700467 public RTCConfiguration(List<IceServer> iceServers) {
468 iceTransportsType = IceTransportsType.ALL;
469 bundlePolicy = BundlePolicy.BALANCED;
zhihuang4dfb8ce2016-11-23 10:30:12 -0800470 rtcpMuxPolicy = RtcpMuxPolicy.REQUIRE;
Jiayang Liucac1b382015-04-30 12:35:24 -0700471 tcpCandidatePolicy = TcpCandidatePolicy.ENABLED;
Sami Kalliomäki9828beb2017-10-26 16:21:22 +0200472 candidateNetworkPolicy = CandidateNetworkPolicy.ALL;
Jiayang Liucac1b382015-04-30 12:35:24 -0700473 this.iceServers = iceServers;
Henrik Lundin64dad832015-05-11 12:44:23 +0200474 audioJitterBufferMaxPackets = 50;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200475 audioJitterBufferFastAccelerate = false;
honghaiz4edc39c2015-09-01 09:53:56 -0700476 iceConnectionReceivingTimeout = -1;
Honghai Zhang381b4212015-12-04 12:24:03 -0800477 iceBackupCandidatePairPingInterval = -1;
glaznev97579a42015-09-01 11:31:27 -0700478 keyType = KeyType.ECDSA;
honghaiz1f429e32015-09-28 07:57:34 -0700479 continualGatheringPolicy = ContinualGatheringPolicy.GATHER_ONCE;
deadbeefbe0c96f2016-05-18 16:20:14 -0700480 iceCandidatePoolSize = 0;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700481 pruneTurnPorts = false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700482 presumeWritableWhenFullyRelayed = false;
Qingsi Wange6826d22018-03-08 14:55:14 -0800483 iceCheckIntervalStrongConnectivityMs = null;
484 iceCheckIntervalWeakConnectivityMs = null;
skvlad51072462017-02-02 11:50:14 -0800485 iceCheckMinInterval = null;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700486 iceUnwritableTimeMs = null;
487 iceUnwritableMinChecks = null;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800488 stunCandidateKeepaliveIntervalMs = null;
zhihuangb09b3f92017-03-07 14:40:51 -0800489 disableIPv6OnWifi = false;
deadbeef28e29192017-07-27 09:14:38 -0700490 maxIPv6Networks = 5;
Steve Antond960a0c2017-07-17 12:33:07 -0700491 iceRegatherIntervalRange = null;
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100492 disableIpv6 = false;
493 enableDscp = false;
494 enableCpuOveruseDetection = true;
495 enableRtpDataChannel = false;
496 suspendBelowMinBitrate = false;
497 screencastMinBitrate = null;
498 combinedAudioVideoBwe = null;
499 enableDtlsSrtp = null;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800500 networkPreference = AdapterType.UNKNOWN;
Seth Hampsonc384e142018-03-06 15:47:10 -0800501 sdpSemantics = SdpSemantics.PLAN_B;
Zhi Huangb57e1692018-06-12 11:41:11 -0700502 activeResetSrtpParams = false;
Jiayang Liucac1b382015-04-30 12:35:24 -0700503 }
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100504
505 @CalledByNative("RTCConfiguration")
506 IceTransportsType getIceTransportsType() {
507 return iceTransportsType;
508 }
509
510 @CalledByNative("RTCConfiguration")
511 List<IceServer> getIceServers() {
512 return iceServers;
513 }
514
515 @CalledByNative("RTCConfiguration")
516 BundlePolicy getBundlePolicy() {
517 return bundlePolicy;
518 }
519
520 @CalledByNative("RTCConfiguration")
521 RtcpMuxPolicy getRtcpMuxPolicy() {
522 return rtcpMuxPolicy;
523 }
524
525 @CalledByNative("RTCConfiguration")
526 TcpCandidatePolicy getTcpCandidatePolicy() {
527 return tcpCandidatePolicy;
528 }
529
530 @CalledByNative("RTCConfiguration")
531 CandidateNetworkPolicy getCandidateNetworkPolicy() {
532 return candidateNetworkPolicy;
533 }
534
535 @CalledByNative("RTCConfiguration")
536 int getAudioJitterBufferMaxPackets() {
537 return audioJitterBufferMaxPackets;
538 }
539
540 @CalledByNative("RTCConfiguration")
541 boolean getAudioJitterBufferFastAccelerate() {
542 return audioJitterBufferFastAccelerate;
543 }
544
545 @CalledByNative("RTCConfiguration")
546 int getIceConnectionReceivingTimeout() {
547 return iceConnectionReceivingTimeout;
548 }
549
550 @CalledByNative("RTCConfiguration")
551 int getIceBackupCandidatePairPingInterval() {
552 return iceBackupCandidatePairPingInterval;
553 }
554
555 @CalledByNative("RTCConfiguration")
556 KeyType getKeyType() {
557 return keyType;
558 }
559
560 @CalledByNative("RTCConfiguration")
561 ContinualGatheringPolicy getContinualGatheringPolicy() {
562 return continualGatheringPolicy;
563 }
564
565 @CalledByNative("RTCConfiguration")
566 int getIceCandidatePoolSize() {
567 return iceCandidatePoolSize;
568 }
569
570 @CalledByNative("RTCConfiguration")
571 boolean getPruneTurnPorts() {
572 return pruneTurnPorts;
573 }
574
575 @CalledByNative("RTCConfiguration")
576 boolean getPresumeWritableWhenFullyRelayed() {
577 return presumeWritableWhenFullyRelayed;
578 }
579
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100580 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100581 @CalledByNative("RTCConfiguration")
Qingsi Wange6826d22018-03-08 14:55:14 -0800582 Integer getIceCheckIntervalStrongConnectivity() {
583 return iceCheckIntervalStrongConnectivityMs;
584 }
585
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100586 @Nullable
Qingsi Wange6826d22018-03-08 14:55:14 -0800587 @CalledByNative("RTCConfiguration")
588 Integer getIceCheckIntervalWeakConnectivity() {
589 return iceCheckIntervalWeakConnectivityMs;
590 }
591
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100592 @Nullable
Qingsi Wange6826d22018-03-08 14:55:14 -0800593 @CalledByNative("RTCConfiguration")
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100594 Integer getIceCheckMinInterval() {
595 return iceCheckMinInterval;
596 }
597
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100598 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100599 @CalledByNative("RTCConfiguration")
Qingsi Wang22e623a2018-03-13 10:53:57 -0700600 Integer getIceUnwritableTimeout() {
601 return iceUnwritableTimeMs;
602 }
603
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100604 @Nullable
Qingsi Wang22e623a2018-03-13 10:53:57 -0700605 @CalledByNative("RTCConfiguration")
606 Integer getIceUnwritableMinChecks() {
607 return iceUnwritableMinChecks;
608 }
609
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100610 @Nullable
Qingsi Wang22e623a2018-03-13 10:53:57 -0700611 @CalledByNative("RTCConfiguration")
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800612 Integer getStunCandidateKeepaliveInterval() {
613 return stunCandidateKeepaliveIntervalMs;
614 }
615
616 @CalledByNative("RTCConfiguration")
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100617 boolean getDisableIPv6OnWifi() {
618 return disableIPv6OnWifi;
619 }
620
621 @CalledByNative("RTCConfiguration")
622 int getMaxIPv6Networks() {
623 return maxIPv6Networks;
624 }
625
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100626 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100627 @CalledByNative("RTCConfiguration")
628 IntervalRange getIceRegatherIntervalRange() {
629 return iceRegatherIntervalRange;
630 }
631
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100632 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100633 @CalledByNative("RTCConfiguration")
634 TurnCustomizer getTurnCustomizer() {
635 return turnCustomizer;
636 }
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100637
638 @CalledByNative("RTCConfiguration")
639 boolean getDisableIpv6() {
640 return disableIpv6;
641 }
642
643 @CalledByNative("RTCConfiguration")
644 boolean getEnableDscp() {
645 return enableDscp;
646 }
647
648 @CalledByNative("RTCConfiguration")
649 boolean getEnableCpuOveruseDetection() {
650 return enableCpuOveruseDetection;
651 }
652
653 @CalledByNative("RTCConfiguration")
654 boolean getEnableRtpDataChannel() {
655 return enableRtpDataChannel;
656 }
657
658 @CalledByNative("RTCConfiguration")
659 boolean getSuspendBelowMinBitrate() {
660 return suspendBelowMinBitrate;
661 }
662
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100663 @Nullable
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100664 @CalledByNative("RTCConfiguration")
665 Integer getScreencastMinBitrate() {
666 return screencastMinBitrate;
667 }
668
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100669 @Nullable
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100670 @CalledByNative("RTCConfiguration")
671 Boolean getCombinedAudioVideoBwe() {
672 return combinedAudioVideoBwe;
673 }
674
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100675 @Nullable
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100676 @CalledByNative("RTCConfiguration")
677 Boolean getEnableDtlsSrtp() {
678 return enableDtlsSrtp;
679 }
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800680
681 @CalledByNative("RTCConfiguration")
682 AdapterType getNetworkPreference() {
683 return networkPreference;
684 }
Seth Hampsonc384e142018-03-06 15:47:10 -0800685
686 @CalledByNative("RTCConfiguration")
687 SdpSemantics getSdpSemantics() {
688 return sdpSemantics;
689 }
Zhi Huangb57e1692018-06-12 11:41:11 -0700690
691 @CalledByNative("RTCConfiguration")
692 boolean getActiveResetSrtpParams() {
693 return activeResetSrtpParams;
694 }
Jiayang Liucac1b382015-04-30 12:35:24 -0700695 };
696
Magnus Jedvert6062f372017-11-16 16:53:12 +0100697 private final List<MediaStream> localStreams = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 private final long nativePeerConnection;
Magnus Jedvert6062f372017-11-16 16:53:12 +0100699 private List<RtpSender> senders = new ArrayList<>();
700 private List<RtpReceiver> receivers = new ArrayList<>();
Seth Hampsonc384e142018-03-06 15:47:10 -0800701 private List<RtpTransceiver> transceivers = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702
Sami Kalliomäki1ece1ed2017-12-20 11:59:22 +0100703 /**
704 * Wraps a PeerConnection created by the factory. Can be used by clients that want to implement
705 * their PeerConnection creation in JNI.
706 */
707 public PeerConnection(NativePeerConnectionFactory factory) {
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100708 this(factory.createNativePeerConnection());
Sami Kalliomäki1ece1ed2017-12-20 11:59:22 +0100709 }
710
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100711 PeerConnection(long nativePeerConnection) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 this.nativePeerConnection = nativePeerConnection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 }
714
715 // JsepInterface.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100716 public SessionDescription getLocalDescription() {
717 return nativeGetLocalDescription();
718 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100720 public SessionDescription getRemoteDescription() {
721 return nativeGetRemoteDescription();
722 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100724 public DataChannel createDataChannel(String label, DataChannel.Init init) {
725 return nativeCreateDataChannel(label, init);
726 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000727
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100728 public void createOffer(SdpObserver observer, MediaConstraints constraints) {
729 nativeCreateOffer(observer, constraints);
730 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100732 public void createAnswer(SdpObserver observer, MediaConstraints constraints) {
733 nativeCreateAnswer(observer, constraints);
734 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100736 public void setLocalDescription(SdpObserver observer, SessionDescription sdp) {
737 nativeSetLocalDescription(observer, sdp);
738 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100740 public void setRemoteDescription(SdpObserver observer, SessionDescription sdp) {
741 nativeSetRemoteDescription(observer, sdp);
742 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743
Seth Hampsonc384e142018-03-06 15:47:10 -0800744 /**
745 * Enables/disables playout of received audio streams. Enabled by default.
746 *
747 * Note that even if playout is enabled, streams will only be played out if
748 * the appropriate SDP is also applied. The main purpose of this API is to
749 * be able to control the exact time when audio playout starts.
750 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100751 public void setAudioPlayout(boolean playout) {
752 nativeSetAudioPlayout(playout);
753 }
henrika5f6bf242017-11-01 11:06:56 +0100754
Seth Hampsonc384e142018-03-06 15:47:10 -0800755 /**
756 * Enables/disables recording of transmitted audio streams. Enabled by default.
757 *
758 * Note that even if recording is enabled, streams will only be recorded if
759 * the appropriate SDP is also applied. The main purpose of this API is to
760 * be able to control the exact time when audio recording starts.
761 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100762 public void setAudioRecording(boolean recording) {
763 nativeSetAudioRecording(recording);
764 }
henrika5f6bf242017-11-01 11:06:56 +0100765
deadbeef5d0b6d82017-01-09 16:05:28 -0800766 public boolean setConfiguration(RTCConfiguration config) {
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100767 return nativeSetConfiguration(config);
deadbeef5d0b6d82017-01-09 16:05:28 -0800768 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769
770 public boolean addIceCandidate(IceCandidate candidate) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100771 return nativeAddIceCandidate(candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 }
773
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700774 public boolean removeIceCandidates(final IceCandidate[] candidates) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100775 return nativeRemoveIceCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700776 }
777
Seth Hampsonc384e142018-03-06 15:47:10 -0800778 /**
779 * Adds a new MediaStream to be sent on this peer connection.
780 * Note: This method is not supported with SdpSemantics.UNIFIED_PLAN. Please
781 * use addTrack instead.
782 */
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000783 public boolean addStream(MediaStream stream) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100784 boolean ret = nativeAddLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 if (!ret) {
786 return false;
787 }
788 localStreams.add(stream);
789 return true;
790 }
791
Seth Hampsonc384e142018-03-06 15:47:10 -0800792 /**
793 * Removes the given media stream from this peer connection.
794 * This method is not supported with SdpSemantics.UNIFIED_PLAN. Please use
795 * removeTrack instead.
796 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 public void removeStream(MediaStream stream) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100798 nativeRemoveLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 localStreams.remove(stream);
800 }
801
deadbeef7a246882017-08-09 08:40:10 -0700802 /**
803 * Creates an RtpSender without a track.
Seth Hampsonc384e142018-03-06 15:47:10 -0800804 *
805 * <p>This method allows an application to cause the PeerConnection to negotiate
deadbeef7a246882017-08-09 08:40:10 -0700806 * sending/receiving a specific media type, but without having a track to
807 * send yet.
Seth Hampsonc384e142018-03-06 15:47:10 -0800808 *
809 * <p>When the application does want to begin sending a track, it can call
deadbeef7a246882017-08-09 08:40:10 -0700810 * RtpSender.setTrack, which doesn't require any additional SDP negotiation.
Seth Hampsonc384e142018-03-06 15:47:10 -0800811 *
812 * <p>Example use:
deadbeef7a246882017-08-09 08:40:10 -0700813 * <pre>
814 * {@code
815 * audioSender = pc.createSender("audio", "stream1");
816 * videoSender = pc.createSender("video", "stream1");
817 * // Do normal SDP offer/answer, which will kick off ICE/DTLS and negotiate
818 * // media parameters....
819 * // Later, when the endpoint is ready to actually begin sending:
820 * audioSender.setTrack(audioTrack, false);
821 * videoSender.setTrack(videoTrack, false);
822 * }
823 * </pre>
Seth Hampsonc384e142018-03-06 15:47:10 -0800824 * <p>Note: This corresponds most closely to "addTransceiver" in the official
deadbeef7a246882017-08-09 08:40:10 -0700825 * WebRTC API, in that it creates a sender without a track. It was
826 * implemented before addTransceiver because it provides useful
827 * functionality, and properly implementing transceivers would have required
828 * a great deal more work.
829 *
Seth Hampsonc384e142018-03-06 15:47:10 -0800830 * <p>Note: This is only available with SdpSemantics.PLAN_B specified. Please use
831 * addTransceiver instead.
832 *
deadbeef7a246882017-08-09 08:40:10 -0700833 * @param kind Corresponds to MediaStreamTrack kinds (must be "audio" or
834 * "video").
835 * @param stream_id The ID of the MediaStream that this sender's track will
836 * be associated with when SDP is applied to the remote
837 * PeerConnection. If createSender is used to create an
838 * audio and video sender that should be synchronized, they
839 * should use the same stream ID.
840 * @return A new RtpSender object if successful, or null otherwise.
841 */
deadbeefbd7d8f72015-12-18 16:58:44 -0800842 public RtpSender createSender(String kind, String stream_id) {
Seth Hampsonc384e142018-03-06 15:47:10 -0800843 RtpSender newSender = nativeCreateSender(kind, stream_id);
844 if (newSender != null) {
845 senders.add(newSender);
deadbeefee524f72015-12-02 11:27:40 -0800846 }
Seth Hampsonc384e142018-03-06 15:47:10 -0800847 return newSender;
deadbeefee524f72015-12-02 11:27:40 -0800848 }
849
Seth Hampsonc384e142018-03-06 15:47:10 -0800850 /**
851 * Gets all RtpSenders associated with this peer connection.
852 * Note that calling getSenders will dispose of the senders previously
853 * returned.
854 */
deadbeef4139c0f2015-10-06 12:29:25 -0700855 public List<RtpSender> getSenders() {
856 for (RtpSender sender : senders) {
857 sender.dispose();
858 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100859 senders = nativeGetSenders();
deadbeef4139c0f2015-10-06 12:29:25 -0700860 return Collections.unmodifiableList(senders);
861 }
862
Seth Hampsonc384e142018-03-06 15:47:10 -0800863 /**
864 * Gets all RtpReceivers associated with this peer connection.
865 * Note that calling getReceivers will dispose of the receivers previously
866 * returned.
867 */
deadbeef4139c0f2015-10-06 12:29:25 -0700868 public List<RtpReceiver> getReceivers() {
869 for (RtpReceiver receiver : receivers) {
870 receiver.dispose();
871 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100872 receivers = nativeGetReceivers();
deadbeef4139c0f2015-10-06 12:29:25 -0700873 return Collections.unmodifiableList(receivers);
874 }
875
Seth Hampsonc384e142018-03-06 15:47:10 -0800876 /**
877 * Gets all RtpTransceivers associated with this peer connection.
878 * Note that calling getTransceivers will dispose of the transceivers previously
879 * returned.
880 * Note: This is only available with SdpSemantics.UNIFIED_PLAN specified.
881 */
882 public List<RtpTransceiver> getTransceivers() {
883 for (RtpTransceiver transceiver : transceivers) {
884 transceiver.dispose();
885 }
886 transceivers = nativeGetTransceivers();
887 return Collections.unmodifiableList(transceivers);
888 }
889
890 /**
891 * Adds a new media stream track to be sent on this peer connection, and returns
892 * the newly created RtpSender. If streamIds are specified, the RtpSender will
893 * be associated with the streams specified in the streamIds list.
894 *
895 * @throws IllegalStateException if an error accors in C++ addTrack.
896 * An error can occur if:
897 * - A sender already exists for the track.
898 * - The peer connection is closed.
899 */
900 public RtpSender addTrack(MediaStreamTrack track) {
901 return addTrack(track, Collections.emptyList());
902 }
903
904 public RtpSender addTrack(MediaStreamTrack track, List<String> streamIds) {
905 if (track == null || streamIds == null) {
906 throw new NullPointerException("No MediaStreamTrack specified in addTrack.");
907 }
908 RtpSender newSender = nativeAddTrack(track.nativeTrack, streamIds);
909 if (newSender == null) {
910 throw new IllegalStateException("C++ addTrack failed.");
911 }
912 senders.add(newSender);
913 return newSender;
914 }
915
916 /**
917 * Stops sending media from sender. The sender will still appear in getSenders. Future
918 * calls to createOffer will mark the m section for the corresponding transceiver as
919 * receive only or inactive, as defined in JSEP. Returns true on success.
920 */
921 public boolean removeTrack(RtpSender sender) {
922 if (sender == null) {
923 throw new NullPointerException("No RtpSender specified for removeTrack.");
924 }
925 return nativeRemoveTrack(sender.nativeRtpSender);
926 }
927
928 /**
929 * Creates a new RtpTransceiver and adds it to the set of transceivers. Adding a
930 * transceiver will cause future calls to CreateOffer to add a media description
931 * for the corresponding transceiver.
932 *
933 * <p>The initial value of |mid| in the returned transceiver is null. Setting a
934 * new session description may change it to a non-null value.
935 *
936 * <p>https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtransceiver
937 *
938 * <p>If a MediaStreamTrack is specified then a transceiver will be added with a
939 * sender set to transmit the given track. The kind
940 * of the transceiver (and sender/receiver) will be derived from the kind of
941 * the track.
942 *
943 * <p>If MediaType is specified then a transceiver will be added based upon that type.
944 * This can be either MEDIA_TYPE_AUDIO or MEDIA_TYPE_VIDEO.
945 *
946 * <p>Optionally, an RtpTransceiverInit structure can be specified to configure
947 * the transceiver from construction. If not specified, the transceiver will
948 * default to having a direction of kSendRecv and not be part of any streams.
949 *
950 * <p>Note: These methods are only available with SdpSemantics.UNIFIED_PLAN specified.
951 * @throws IllegalStateException if an error accors in C++ addTransceiver
952 */
953 public RtpTransceiver addTransceiver(MediaStreamTrack track) {
954 return addTransceiver(track, new RtpTransceiver.RtpTransceiverInit());
955 }
956
957 public RtpTransceiver addTransceiver(
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100958 MediaStreamTrack track, @Nullable RtpTransceiver.RtpTransceiverInit init) {
Seth Hampsonc384e142018-03-06 15:47:10 -0800959 if (track == null) {
960 throw new NullPointerException("No MediaStreamTrack specified for addTransceiver.");
961 }
962 if (init == null) {
963 init = new RtpTransceiver.RtpTransceiverInit();
964 }
965 RtpTransceiver newTransceiver = nativeAddTransceiverWithTrack(track.nativeTrack, init);
966 if (newTransceiver == null) {
967 throw new IllegalStateException("C++ addTransceiver failed.");
968 }
969 transceivers.add(newTransceiver);
970 return newTransceiver;
971 }
972
973 public RtpTransceiver addTransceiver(MediaStreamTrack.MediaType mediaType) {
974 return addTransceiver(mediaType, new RtpTransceiver.RtpTransceiverInit());
975 }
976
977 public RtpTransceiver addTransceiver(
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100978 MediaStreamTrack.MediaType mediaType, @Nullable RtpTransceiver.RtpTransceiverInit init) {
Seth Hampsonc384e142018-03-06 15:47:10 -0800979 if (mediaType == null) {
980 throw new NullPointerException("No MediaType specified for addTransceiver.");
981 }
982 if (init == null) {
983 init = new RtpTransceiver.RtpTransceiverInit();
984 }
985 RtpTransceiver newTransceiver = nativeAddTransceiverOfType(mediaType, init);
986 if (newTransceiver == null) {
987 throw new IllegalStateException("C++ addTransceiver failed.");
988 }
989 transceivers.add(newTransceiver);
990 return newTransceiver;
991 }
992
deadbeef82215872017-04-18 10:27:51 -0700993 // Older, non-standard implementation of getStats.
994 @Deprecated
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100995 public boolean getStats(StatsObserver observer, @Nullable MediaStreamTrack track) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100996 return nativeOldGetStats(observer, (track == null) ? 0 : track.nativeTrack);
deadbeef82215872017-04-18 10:27:51 -0700997 }
998
Seth Hampsonc384e142018-03-06 15:47:10 -0800999 /**
1000 * Gets stats using the new stats collection API, see webrtc/api/stats/. These
1001 * will replace old stats collection API when the new API has matured enough.
1002 */
deadbeef82215872017-04-18 10:27:51 -07001003 public void getStats(RTCStatsCollectorCallback callback) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001004 nativeNewGetStats(callback);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005 }
1006
Seth Hampsonc384e142018-03-06 15:47:10 -08001007 /**
1008 * Limits the bandwidth allocated for all RTP streams sent by this
1009 * PeerConnection. Pass null to leave a value unchanged.
1010 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001011 public boolean setBitrate(Integer min, Integer current, Integer max) {
1012 return nativeSetBitrate(min, current, max);
1013 }
zsteind89b0bc2017-08-03 11:11:40 -07001014
Seth Hampsonc384e142018-03-06 15:47:10 -08001015 /**
1016 * Starts recording an RTC event log.
1017 *
1018 * Ownership of the file is transfered to the native code. If an RTC event
1019 * log is already being recorded, it will be stopped and a new one will start
1020 * using the provided file. Logging will continue until the stopRtcEventLog
1021 * function is called. The max_size_bytes argument is ignored, it is added
1022 * for future use.
1023 */
ivoc0c6f0f62016-07-06 04:34:23 -07001024 public boolean startRtcEventLog(int file_descriptor, int max_size_bytes) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001025 return nativeStartRtcEventLog(file_descriptor, max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -07001026 }
1027
Seth Hampsonc384e142018-03-06 15:47:10 -08001028 /**
1029 * Stops recording an RTC event log. If no RTC event log is currently being
1030 * recorded, this call will have no effect.
1031 */
ivoc14d5dbe2016-07-04 07:06:55 -07001032 public void stopRtcEventLog() {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001033 nativeStopRtcEventLog();
ivoc14d5dbe2016-07-04 07:06:55 -07001034 }
1035
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036 // TODO(fischman): add support for DTMF-related methods once that API
1037 // stabilizes.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001038 public SignalingState signalingState() {
1039 return nativeSignalingState();
1040 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001042 public IceConnectionState iceConnectionState() {
1043 return nativeIceConnectionState();
1044 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001046 public IceGatheringState iceGatheringState() {
1047 return nativeIceGatheringState();
1048 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001050 public void close() {
1051 nativeClose();
1052 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053
deadbeef43697f62017-09-12 10:52:14 -07001054 /**
1055 * Free native resources associated with this PeerConnection instance.
Seth Hampsonc384e142018-03-06 15:47:10 -08001056 *
deadbeef43697f62017-09-12 10:52:14 -07001057 * This method removes a reference count from the C++ PeerConnection object,
1058 * which should result in it being destroyed. It also calls equivalent
1059 * "dispose" methods on the Java objects attached to this PeerConnection
1060 * (streams, senders, receivers), such that their associated C++ objects
1061 * will also be destroyed.
Seth Hampsonc384e142018-03-06 15:47:10 -08001062 *
1063 * <p>Note that this method cannot be safely called from an observer callback
deadbeef43697f62017-09-12 10:52:14 -07001064 * (PeerConnection.Observer, DataChannel.Observer, etc.). If you want to, for
1065 * example, destroy the PeerConnection after an "ICE failed" callback, you
1066 * must do this asynchronously (in other words, unwind the stack first). See
1067 * <a href="https://bugs.chromium.org/p/webrtc/issues/detail?id=3721">bug
1068 * 3721</a> for more details.
1069 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070 public void dispose() {
1071 close();
1072 for (MediaStream stream : localStreams) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001073 nativeRemoveLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074 stream.dispose();
1075 }
1076 localStreams.clear();
deadbeef4139c0f2015-10-06 12:29:25 -07001077 for (RtpSender sender : senders) {
1078 sender.dispose();
1079 }
1080 senders.clear();
1081 for (RtpReceiver receiver : receivers) {
1082 receiver.dispose();
1083 }
Seth Hampsonc384e142018-03-06 15:47:10 -08001084 for (RtpTransceiver transceiver : transceivers) {
1085 transceiver.dispose();
1086 }
1087 transceivers.clear();
deadbeef4139c0f2015-10-06 12:29:25 -07001088 receivers.clear();
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001089 nativeFreeOwnedPeerConnection(nativePeerConnection);
1090 }
1091
1092 /** Returns a pointer to the native webrtc::PeerConnectionInterface. */
1093 public long getNativePeerConnection() {
1094 return nativeGetNativePeerConnection();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095 }
1096
Magnus Jedvert9060eb12017-12-12 12:52:54 +01001097 @CalledByNative
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001098 long getNativeOwnedPeerConnection() {
Magnus Jedvert9060eb12017-12-12 12:52:54 +01001099 return nativePeerConnection;
1100 }
1101
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001102 public static long createNativePeerConnectionObserver(Observer observer) {
1103 return nativeCreatePeerConnectionObserver(observer);
1104 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001105
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001106 private native long nativeGetNativePeerConnection();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001107 private native SessionDescription nativeGetLocalDescription();
1108 private native SessionDescription nativeGetRemoteDescription();
1109 private native DataChannel nativeCreateDataChannel(String label, DataChannel.Init init);
1110 private native void nativeCreateOffer(SdpObserver observer, MediaConstraints constraints);
1111 private native void nativeCreateAnswer(SdpObserver observer, MediaConstraints constraints);
1112 private native void nativeSetLocalDescription(SdpObserver observer, SessionDescription sdp);
1113 private native void nativeSetRemoteDescription(SdpObserver observer, SessionDescription sdp);
1114 private native void nativeSetAudioPlayout(boolean playout);
1115 private native void nativeSetAudioRecording(boolean recording);
1116 private native boolean nativeSetBitrate(Integer min, Integer current, Integer max);
1117 private native SignalingState nativeSignalingState();
1118 private native IceConnectionState nativeIceConnectionState();
1119 private native IceGatheringState nativeIceGatheringState();
1120 private native void nativeClose();
1121 private static native long nativeCreatePeerConnectionObserver(Observer observer);
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001122 private static native void nativeFreeOwnedPeerConnection(long ownedPeerConnection);
1123 private native boolean nativeSetConfiguration(RTCConfiguration config);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001124 private native boolean nativeAddIceCandidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001125 String sdpMid, int sdpMLineIndex, String iceCandidateSdp);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001126 private native boolean nativeRemoveIceCandidates(final IceCandidate[] candidates);
1127 private native boolean nativeAddLocalStream(long stream);
1128 private native void nativeRemoveLocalStream(long stream);
1129 private native boolean nativeOldGetStats(StatsObserver observer, long nativeTrack);
1130 private native void nativeNewGetStats(RTCStatsCollectorCallback callback);
1131 private native RtpSender nativeCreateSender(String kind, String stream_id);
1132 private native List<RtpSender> nativeGetSenders();
1133 private native List<RtpReceiver> nativeGetReceivers();
Seth Hampsonc384e142018-03-06 15:47:10 -08001134 private native List<RtpTransceiver> nativeGetTransceivers();
1135 private native RtpSender nativeAddTrack(long track, List<String> streamIds);
1136 private native boolean nativeRemoveTrack(long sender);
1137 private native RtpTransceiver nativeAddTransceiverWithTrack(
1138 long track, RtpTransceiver.RtpTransceiverInit init);
1139 private native RtpTransceiver nativeAddTransceiverOfType(
1140 MediaStreamTrack.MediaType mediaType, RtpTransceiver.RtpTransceiverInit init);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001141 private native boolean nativeStartRtcEventLog(int file_descriptor, int max_size_bytes);
1142 private native void nativeStopRtcEventLog();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143}