blob: dfe4b6109c330b353e7c07fc1a9d6b1dbf5e414e [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 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010024@JNINamespace("webrtc::jni")
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025public class PeerConnection {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026 /** Tracks PeerConnectionInterface::IceGatheringState */
Magnus Jedvertba700f62017-12-04 13:43:27 +010027 public enum IceGatheringState {
28 NEW,
29 GATHERING,
30 COMPLETE;
31
32 @CalledByNative("IceGatheringState")
33 static IceGatheringState fromNativeIndex(int nativeIndex) {
34 return values()[nativeIndex];
35 }
36 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037
38 /** Tracks PeerConnectionInterface::IceConnectionState */
39 public enum IceConnectionState {
sakalb6760f92016-09-29 04:12:44 -070040 NEW,
41 CHECKING,
42 CONNECTED,
43 COMPLETED,
44 FAILED,
45 DISCONNECTED,
Magnus Jedvertba700f62017-12-04 13:43:27 +010046 CLOSED;
47
48 @CalledByNative("IceConnectionState")
49 static IceConnectionState fromNativeIndex(int nativeIndex) {
50 return values()[nativeIndex];
51 }
sakalb6760f92016-09-29 04:12:44 -070052 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053
hnsl04833622017-01-09 08:35:45 -080054 /** Tracks PeerConnectionInterface::TlsCertPolicy */
55 public enum TlsCertPolicy {
56 TLS_CERT_POLICY_SECURE,
57 TLS_CERT_POLICY_INSECURE_NO_CHECK,
58 }
59
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 /** Tracks PeerConnectionInterface::SignalingState */
61 public enum SignalingState {
sakalb6760f92016-09-29 04:12:44 -070062 STABLE,
63 HAVE_LOCAL_OFFER,
64 HAVE_LOCAL_PRANSWER,
65 HAVE_REMOTE_OFFER,
66 HAVE_REMOTE_PRANSWER,
Magnus Jedvertba700f62017-12-04 13:43:27 +010067 CLOSED;
68
69 @CalledByNative("SignalingState")
70 static SignalingState fromNativeIndex(int nativeIndex) {
71 return values()[nativeIndex];
72 }
sakalb6760f92016-09-29 04:12:44 -070073 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074
75 /** Java version of PeerConnectionObserver. */
76 public static interface Observer {
77 /** Triggered when the SignalingState changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010078 @CalledByNative("Observer") void onSignalingChange(SignalingState newState);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079
80 /** Triggered when the IceConnectionState changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010081 @CalledByNative("Observer") void onIceConnectionChange(IceConnectionState newState);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082
Peter Thatcher54360512015-07-08 11:08:35 -070083 /** Triggered when the ICE connection receiving status changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010084 @CalledByNative("Observer") void onIceConnectionReceivingChange(boolean receiving);
Peter Thatcher54360512015-07-08 11:08:35 -070085
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 /** Triggered when the IceGatheringState changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010087 @CalledByNative("Observer") void onIceGatheringChange(IceGatheringState newState);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088
89 /** Triggered when a new ICE candidate has been found. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010090 @CalledByNative("Observer") void onIceCandidate(IceCandidate candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091
Honghai Zhang7fb69db2016-03-14 11:59:18 -070092 /** Triggered when some ICE candidates have been removed. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010093 @CalledByNative("Observer") void onIceCandidatesRemoved(IceCandidate[] candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -070094
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 /** Triggered when media is received on a new stream from remote peer. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010096 @CalledByNative("Observer") void onAddStream(MediaStream stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097
98 /** Triggered when a remote peer close a stream. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010099 @CalledByNative("Observer") void onRemoveStream(MediaStream stream);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000100
101 /** Triggered when a remote peer opens a DataChannel. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100102 @CalledByNative("Observer") void onDataChannel(DataChannel dataChannel);
fischman@webrtc.orgd7568a02014-01-13 22:04:12 +0000103
104 /** Triggered when renegotiation is necessary. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100105 @CalledByNative("Observer") void onRenegotiationNeeded();
zhihuangdcccda72016-12-21 14:08:03 -0800106
107 /**
108 * Triggered when a new track is signaled by the remote peer, as a result of
109 * setRemoteDescription.
110 */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100111 @CalledByNative("Observer") void onAddTrack(RtpReceiver receiver, MediaStream[] mediaStreams);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112 }
113
114 /** Java version of PeerConnectionInterface.IceServer. */
115 public static class IceServer {
Emad Omaradab1d2d2017-06-16 15:43:11 -0700116 // List of URIs associated with this server. Valid formats are described
117 // in RFC7064 and RFC7065, and more may be added in the future. The "host"
118 // part of the URI may contain either an IP address or a hostname.
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700119 @Deprecated public final String uri;
120 public final List<String> urls;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 public final String username;
122 public final String password;
hnsl04833622017-01-09 08:35:45 -0800123 public final TlsCertPolicy tlsCertPolicy;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124
Emad Omaradab1d2d2017-06-16 15:43:11 -0700125 // If the URIs in |urls| only contain IP addresses, this field can be used
126 // to indicate the hostname, which may be necessary for TLS (using the SNI
127 // extension). If |urls| itself contains the hostname, this isn't
128 // necessary.
129 public final String hostname;
130
Diogo Real1dca9d52017-08-29 12:18:32 -0700131 // List of protocols to be used in the TLS ALPN extension.
132 public final List<String> tlsAlpnProtocols;
133
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700134 // List of elliptic curves to be used in the TLS elliptic curves extension.
135 // Only curve names supported by OpenSSL should be used (eg. "P-256","X25519").
136 public final List<String> tlsEllipticCurves;
137
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 /** Convenience constructor for STUN servers. */
Diogo Real05ea2b32017-08-31 00:12:58 -0700139 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140 public IceServer(String uri) {
141 this(uri, "", "");
142 }
143
Diogo Real05ea2b32017-08-31 00:12:58 -0700144 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145 public IceServer(String uri, String username, String password) {
hnsl04833622017-01-09 08:35:45 -0800146 this(uri, username, password, TlsCertPolicy.TLS_CERT_POLICY_SECURE);
147 }
148
Diogo Real05ea2b32017-08-31 00:12:58 -0700149 @Deprecated
hnsl04833622017-01-09 08:35:45 -0800150 public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy) {
Emad Omaradab1d2d2017-06-16 15:43:11 -0700151 this(uri, username, password, tlsCertPolicy, "");
152 }
153
Diogo Real05ea2b32017-08-31 00:12:58 -0700154 @Deprecated
Emad Omaradab1d2d2017-06-16 15:43:11 -0700155 public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy,
156 String hostname) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700157 this(uri, Collections.singletonList(uri), username, password, tlsCertPolicy, hostname, null,
158 null);
Diogo Real1dca9d52017-08-29 12:18:32 -0700159 }
160
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700161 private IceServer(String uri, List<String> urls, String username, String password,
162 TlsCertPolicy tlsCertPolicy, String hostname, List<String> tlsAlpnProtocols,
163 List<String> tlsEllipticCurves) {
164 if (uri == null || urls == null || urls.isEmpty()) {
165 throw new IllegalArgumentException("uri == null || urls == null || urls.isEmpty()");
166 }
167 for (String it : urls) {
168 if (it == null) {
169 throw new IllegalArgumentException("urls element is null: " + urls);
170 }
171 }
172 if (username == null) {
173 throw new IllegalArgumentException("username == null");
174 }
175 if (password == null) {
176 throw new IllegalArgumentException("password == null");
177 }
178 if (hostname == null) {
179 throw new IllegalArgumentException("hostname == null");
180 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 this.uri = uri;
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700182 this.urls = urls;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 this.username = username;
184 this.password = password;
hnsl04833622017-01-09 08:35:45 -0800185 this.tlsCertPolicy = tlsCertPolicy;
Emad Omaradab1d2d2017-06-16 15:43:11 -0700186 this.hostname = hostname;
Diogo Real1dca9d52017-08-29 12:18:32 -0700187 this.tlsAlpnProtocols = tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700188 this.tlsEllipticCurves = tlsEllipticCurves;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 }
190
Sami Kalliomäkibde473e2017-10-30 13:34:41 +0100191 @Override
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 public String toString() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700193 return urls + " [" + username + ":" + password + "] [" + tlsCertPolicy + "] [" + hostname
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700194 + "] [" + tlsAlpnProtocols + "] [" + tlsEllipticCurves + "]";
Diogo Real1dca9d52017-08-29 12:18:32 -0700195 }
196
197 public static Builder builder(String uri) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700198 return new Builder(Collections.singletonList(uri));
199 }
200
201 public static Builder builder(List<String> urls) {
202 return new Builder(urls);
Diogo Real1dca9d52017-08-29 12:18:32 -0700203 }
204
205 public static class Builder {
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100206 @Nullable private final List<String> urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700207 private String username = "";
208 private String password = "";
209 private TlsCertPolicy tlsCertPolicy = TlsCertPolicy.TLS_CERT_POLICY_SECURE;
210 private String hostname = "";
211 private List<String> tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700212 private List<String> tlsEllipticCurves;
Diogo Real1dca9d52017-08-29 12:18:32 -0700213
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700214 private Builder(List<String> urls) {
215 if (urls == null || urls.isEmpty()) {
216 throw new IllegalArgumentException("urls == null || urls.isEmpty(): " + urls);
217 }
218 this.urls = urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700219 }
220
221 public Builder setUsername(String username) {
222 this.username = username;
223 return this;
224 }
225
226 public Builder setPassword(String password) {
227 this.password = password;
228 return this;
229 }
230
231 public Builder setTlsCertPolicy(TlsCertPolicy tlsCertPolicy) {
232 this.tlsCertPolicy = tlsCertPolicy;
233 return this;
234 }
235
236 public Builder setHostname(String hostname) {
237 this.hostname = hostname;
238 return this;
239 }
240
241 public Builder setTlsAlpnProtocols(List<String> tlsAlpnProtocols) {
242 this.tlsAlpnProtocols = tlsAlpnProtocols;
243 return this;
244 }
245
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700246 public Builder setTlsEllipticCurves(List<String> tlsEllipticCurves) {
247 this.tlsEllipticCurves = tlsEllipticCurves;
248 return this;
249 }
250
Diogo Real1dca9d52017-08-29 12:18:32 -0700251 public IceServer createIceServer() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700252 return new IceServer(urls.get(0), urls, username, password, tlsCertPolicy, hostname,
253 tlsAlpnProtocols, tlsEllipticCurves);
Diogo Real1dca9d52017-08-29 12:18:32 -0700254 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 }
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100256
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100257 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100258 @CalledByNative("IceServer")
259 List<String> getUrls() {
260 return urls;
261 }
262
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100263 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100264 @CalledByNative("IceServer")
265 String getUsername() {
266 return username;
267 }
268
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100269 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100270 @CalledByNative("IceServer")
271 String getPassword() {
272 return password;
273 }
274
275 @CalledByNative("IceServer")
276 TlsCertPolicy getTlsCertPolicy() {
277 return tlsCertPolicy;
278 }
279
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100280 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100281 @CalledByNative("IceServer")
282 String getHostname() {
283 return hostname;
284 }
285
286 @CalledByNative("IceServer")
287 List<String> getTlsAlpnProtocols() {
288 return tlsAlpnProtocols;
289 }
290
291 @CalledByNative("IceServer")
292 List<String> getTlsEllipticCurves() {
293 return tlsEllipticCurves;
294 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 }
296
Jiayang Liucac1b382015-04-30 12:35:24 -0700297 /** Java version of PeerConnectionInterface.IceTransportsType */
sakalb6760f92016-09-29 04:12:44 -0700298 public enum IceTransportsType { NONE, RELAY, NOHOST, ALL }
Jiayang Liucac1b382015-04-30 12:35:24 -0700299
300 /** Java version of PeerConnectionInterface.BundlePolicy */
sakalb6760f92016-09-29 04:12:44 -0700301 public enum BundlePolicy { BALANCED, MAXBUNDLE, MAXCOMPAT }
Jiayang Liucac1b382015-04-30 12:35:24 -0700302
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700303 /** Java version of PeerConnectionInterface.RtcpMuxPolicy */
sakalb6760f92016-09-29 04:12:44 -0700304 public enum RtcpMuxPolicy { NEGOTIATE, REQUIRE }
glaznev97579a42015-09-01 11:31:27 -0700305
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700306 /** Java version of PeerConnectionInterface.TcpCandidatePolicy */
sakalb6760f92016-09-29 04:12:44 -0700307 public enum TcpCandidatePolicy { ENABLED, DISABLED }
Jiayang Liucac1b382015-04-30 12:35:24 -0700308
honghaiz60347052016-05-31 18:29:12 -0700309 /** Java version of PeerConnectionInterface.CandidateNetworkPolicy */
sakalb6760f92016-09-29 04:12:44 -0700310 public enum CandidateNetworkPolicy { ALL, LOW_COST }
honghaiz60347052016-05-31 18:29:12 -0700311
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800312 // Keep in sync with webrtc/rtc_base/network_constants.h.
313 public enum AdapterType {
314 UNKNOWN,
315 ETHERNET,
316 WIFI,
317 CELLULAR,
318 VPN,
319 LOOPBACK,
320 }
321
glaznev97579a42015-09-01 11:31:27 -0700322 /** Java version of rtc::KeyType */
sakalb6760f92016-09-29 04:12:44 -0700323 public enum KeyType { RSA, ECDSA }
glaznev97579a42015-09-01 11:31:27 -0700324
honghaiz1f429e32015-09-28 07:57:34 -0700325 /** Java version of PeerConnectionInterface.ContinualGatheringPolicy */
sakalb6760f92016-09-29 04:12:44 -0700326 public enum ContinualGatheringPolicy { GATHER_ONCE, GATHER_CONTINUALLY }
honghaiz1f429e32015-09-28 07:57:34 -0700327
Steve Antond960a0c2017-07-17 12:33:07 -0700328 /** Java version of rtc::IntervalRange */
329 public static class IntervalRange {
330 private final int min;
331 private final int max;
332
333 public IntervalRange(int min, int max) {
334 this.min = min;
335 this.max = max;
336 }
337
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100338 @CalledByNative("IntervalRange")
Steve Antond960a0c2017-07-17 12:33:07 -0700339 public int getMin() {
340 return min;
341 }
342
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100343 @CalledByNative("IntervalRange")
Steve Antond960a0c2017-07-17 12:33:07 -0700344 public int getMax() {
345 return max;
346 }
347 }
348
Seth Hampsonc384e142018-03-06 15:47:10 -0800349 /**
350 * Java version of webrtc::SdpSemantics.
351 *
352 * Configure the SDP semantics used by this PeerConnection. Note that the
353 * WebRTC 1.0 specification requires UNIFIED_PLAN semantics. The
354 * RtpTransceiver API is only available with UNIFIED_PLAN semantics.
355 *
356 * <p>PLAN_B will cause PeerConnection to create offers and answers with at
357 * most one audio and one video m= section with multiple RtpSenders and
358 * RtpReceivers specified as multiple a=ssrc lines within the section. This
359 * will also cause PeerConnection to ignore all but the first m= section of
360 * the same media type.
361 *
362 * <p>UNIFIED_PLAN will cause PeerConnection to create offers and answers with
363 * multiple m= sections where each m= section maps to one RtpSender and one
364 * RtpReceiver (an RtpTransceiver), either both audio or both video. This
365 * will also cause PeerConnection to ignore all but the first a=ssrc lines
366 * that form a Plan B stream.
367 *
368 * <p>For users who wish to send multiple audio/video streams and need to stay
369 * interoperable with legacy WebRTC implementations, specify PLAN_B.
370 *
371 * <p>For users who wish to send multiple audio/video streams and/or wish to
372 * use the new RtpTransceiver API, specify UNIFIED_PLAN.
373 */
374 public enum SdpSemantics { PLAN_B, UNIFIED_PLAN }
375
Jiayang Liucac1b382015-04-30 12:35:24 -0700376 /** Java version of PeerConnectionInterface.RTCConfiguration */
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800377 // TODO(qingsi): Resolve the naming inconsistency of fields with/without units.
Jiayang Liucac1b382015-04-30 12:35:24 -0700378 public static class RTCConfiguration {
379 public IceTransportsType iceTransportsType;
380 public List<IceServer> iceServers;
381 public BundlePolicy bundlePolicy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700382 public RtcpMuxPolicy rtcpMuxPolicy;
Jiayang Liucac1b382015-04-30 12:35:24 -0700383 public TcpCandidatePolicy tcpCandidatePolicy;
honghaiz60347052016-05-31 18:29:12 -0700384 public CandidateNetworkPolicy candidateNetworkPolicy;
Henrik Lundin64dad832015-05-11 12:44:23 +0200385 public int audioJitterBufferMaxPackets;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200386 public boolean audioJitterBufferFastAccelerate;
honghaiz4edc39c2015-09-01 09:53:56 -0700387 public int iceConnectionReceivingTimeout;
Honghai Zhang381b4212015-12-04 12:24:03 -0800388 public int iceBackupCandidatePairPingInterval;
glaznev97579a42015-09-01 11:31:27 -0700389 public KeyType keyType;
honghaiz1f429e32015-09-28 07:57:34 -0700390 public ContinualGatheringPolicy continualGatheringPolicy;
deadbeefbe0c96f2016-05-18 16:20:14 -0700391 public int iceCandidatePoolSize;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700392 public boolean pruneTurnPorts;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700393 public boolean presumeWritableWhenFullyRelayed;
Qingsi Wange6826d22018-03-08 14:55:14 -0800394 // The following fields define intervals in milliseconds at which ICE
395 // connectivity checks are sent.
396 //
397 // We consider ICE is "strongly connected" for an agent when there is at
398 // least one candidate pair that currently succeeds in connectivity check
399 // from its direction i.e. sending a ping and receives a ping response, AND
400 // all candidate pairs have sent a minimum number of pings for connectivity
401 // (this number is implementation-specific). Otherwise, ICE is considered in
402 // "weak connectivity".
403 //
404 // Note that the above notion of strong and weak connectivity is not defined
405 // in RFC 5245, and they apply to our current ICE implementation only.
406 //
407 // 1) iceCheckIntervalStrongConnectivityMs defines the interval applied to
408 // ALL candidate pairs when ICE is strongly connected,
409 // 2) iceCheckIntervalWeakConnectivityMs defines the counterpart for ALL
410 // pairs when ICE is weakly connected, and
411 // 3) iceCheckMinInterval defines the minimal interval (equivalently the
412 // maximum rate) that overrides the above two intervals when either of them
413 // is less.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100414 @Nullable public Integer iceCheckIntervalStrongConnectivityMs;
415 @Nullable public Integer iceCheckIntervalWeakConnectivityMs;
416 @Nullable public Integer iceCheckMinInterval;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700417 // The time period in milliseconds for which a candidate pair must wait for response to
418 // connectivitiy checks before it becomes unwritable.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100419 @Nullable public Integer iceUnwritableTimeMs;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700420 // The minimum number of connectivity checks that a candidate pair must sent without receiving
421 // response before it becomes unwritable.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100422 @Nullable public Integer iceUnwritableMinChecks;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800423 // The interval in milliseconds at which STUN candidates will resend STUN binding requests
424 // to keep NAT bindings open.
425 // The default value in the implementation is used if this field is null.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100426 @Nullable public Integer stunCandidateKeepaliveIntervalMs;
zhihuangb09b3f92017-03-07 14:40:51 -0800427 public boolean disableIPv6OnWifi;
deadbeef28e29192017-07-27 09:14:38 -0700428 // By default, PeerConnection will use a limited number of IPv6 network
429 // interfaces, in order to avoid too many ICE candidate pairs being created
430 // and delaying ICE completion.
431 //
432 // Can be set to Integer.MAX_VALUE to effectively disable the limit.
433 public int maxIPv6Networks;
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100434 @Nullable public IntervalRange iceRegatherIntervalRange;
Jiayang Liucac1b382015-04-30 12:35:24 -0700435
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100436 // These values will be overridden by MediaStream constraints if deprecated constraints-based
437 // create peerconnection interface is used.
438 public boolean disableIpv6;
439 public boolean enableDscp;
440 public boolean enableCpuOveruseDetection;
441 public boolean enableRtpDataChannel;
442 public boolean suspendBelowMinBitrate;
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100443 @Nullable public Integer screencastMinBitrate;
444 @Nullable public Boolean combinedAudioVideoBwe;
445 @Nullable public Boolean enableDtlsSrtp;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800446 // Use "Unknown" to represent no preference of adapter types, not the
447 // preference of adapters of unknown types.
448 public AdapterType networkPreference;
Seth Hampsonc384e142018-03-06 15:47:10 -0800449 public SdpSemantics sdpSemantics;
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100450
Jonas Orelandbdcee282017-10-10 14:01:40 +0200451 // This is an optional wrapper for the C++ webrtc::TurnCustomizer.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100452 @Nullable public TurnCustomizer turnCustomizer;
Jonas Orelandbdcee282017-10-10 14:01:40 +0200453
deadbeef28e29192017-07-27 09:14:38 -0700454 // TODO(deadbeef): Instead of duplicating the defaults here, we should do
455 // something to pick up the defaults from C++. The Objective-C equivalent
456 // of RTCConfiguration does that.
Jiayang Liucac1b382015-04-30 12:35:24 -0700457 public RTCConfiguration(List<IceServer> iceServers) {
458 iceTransportsType = IceTransportsType.ALL;
459 bundlePolicy = BundlePolicy.BALANCED;
zhihuang4dfb8ce2016-11-23 10:30:12 -0800460 rtcpMuxPolicy = RtcpMuxPolicy.REQUIRE;
Jiayang Liucac1b382015-04-30 12:35:24 -0700461 tcpCandidatePolicy = TcpCandidatePolicy.ENABLED;
Sami Kalliomäki9828beb2017-10-26 16:21:22 +0200462 candidateNetworkPolicy = CandidateNetworkPolicy.ALL;
Jiayang Liucac1b382015-04-30 12:35:24 -0700463 this.iceServers = iceServers;
Henrik Lundin64dad832015-05-11 12:44:23 +0200464 audioJitterBufferMaxPackets = 50;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200465 audioJitterBufferFastAccelerate = false;
honghaiz4edc39c2015-09-01 09:53:56 -0700466 iceConnectionReceivingTimeout = -1;
Honghai Zhang381b4212015-12-04 12:24:03 -0800467 iceBackupCandidatePairPingInterval = -1;
glaznev97579a42015-09-01 11:31:27 -0700468 keyType = KeyType.ECDSA;
honghaiz1f429e32015-09-28 07:57:34 -0700469 continualGatheringPolicy = ContinualGatheringPolicy.GATHER_ONCE;
deadbeefbe0c96f2016-05-18 16:20:14 -0700470 iceCandidatePoolSize = 0;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700471 pruneTurnPorts = false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700472 presumeWritableWhenFullyRelayed = false;
Qingsi Wange6826d22018-03-08 14:55:14 -0800473 iceCheckIntervalStrongConnectivityMs = null;
474 iceCheckIntervalWeakConnectivityMs = null;
skvlad51072462017-02-02 11:50:14 -0800475 iceCheckMinInterval = null;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700476 iceUnwritableTimeMs = null;
477 iceUnwritableMinChecks = null;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800478 stunCandidateKeepaliveIntervalMs = null;
zhihuangb09b3f92017-03-07 14:40:51 -0800479 disableIPv6OnWifi = false;
deadbeef28e29192017-07-27 09:14:38 -0700480 maxIPv6Networks = 5;
Steve Antond960a0c2017-07-17 12:33:07 -0700481 iceRegatherIntervalRange = null;
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100482 disableIpv6 = false;
483 enableDscp = false;
484 enableCpuOveruseDetection = true;
485 enableRtpDataChannel = false;
486 suspendBelowMinBitrate = false;
487 screencastMinBitrate = null;
488 combinedAudioVideoBwe = null;
489 enableDtlsSrtp = null;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800490 networkPreference = AdapterType.UNKNOWN;
Seth Hampsonc384e142018-03-06 15:47:10 -0800491 sdpSemantics = SdpSemantics.PLAN_B;
Jiayang Liucac1b382015-04-30 12:35:24 -0700492 }
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100493
494 @CalledByNative("RTCConfiguration")
495 IceTransportsType getIceTransportsType() {
496 return iceTransportsType;
497 }
498
499 @CalledByNative("RTCConfiguration")
500 List<IceServer> getIceServers() {
501 return iceServers;
502 }
503
504 @CalledByNative("RTCConfiguration")
505 BundlePolicy getBundlePolicy() {
506 return bundlePolicy;
507 }
508
509 @CalledByNative("RTCConfiguration")
510 RtcpMuxPolicy getRtcpMuxPolicy() {
511 return rtcpMuxPolicy;
512 }
513
514 @CalledByNative("RTCConfiguration")
515 TcpCandidatePolicy getTcpCandidatePolicy() {
516 return tcpCandidatePolicy;
517 }
518
519 @CalledByNative("RTCConfiguration")
520 CandidateNetworkPolicy getCandidateNetworkPolicy() {
521 return candidateNetworkPolicy;
522 }
523
524 @CalledByNative("RTCConfiguration")
525 int getAudioJitterBufferMaxPackets() {
526 return audioJitterBufferMaxPackets;
527 }
528
529 @CalledByNative("RTCConfiguration")
530 boolean getAudioJitterBufferFastAccelerate() {
531 return audioJitterBufferFastAccelerate;
532 }
533
534 @CalledByNative("RTCConfiguration")
535 int getIceConnectionReceivingTimeout() {
536 return iceConnectionReceivingTimeout;
537 }
538
539 @CalledByNative("RTCConfiguration")
540 int getIceBackupCandidatePairPingInterval() {
541 return iceBackupCandidatePairPingInterval;
542 }
543
544 @CalledByNative("RTCConfiguration")
545 KeyType getKeyType() {
546 return keyType;
547 }
548
549 @CalledByNative("RTCConfiguration")
550 ContinualGatheringPolicy getContinualGatheringPolicy() {
551 return continualGatheringPolicy;
552 }
553
554 @CalledByNative("RTCConfiguration")
555 int getIceCandidatePoolSize() {
556 return iceCandidatePoolSize;
557 }
558
559 @CalledByNative("RTCConfiguration")
560 boolean getPruneTurnPorts() {
561 return pruneTurnPorts;
562 }
563
564 @CalledByNative("RTCConfiguration")
565 boolean getPresumeWritableWhenFullyRelayed() {
566 return presumeWritableWhenFullyRelayed;
567 }
568
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100569 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100570 @CalledByNative("RTCConfiguration")
Qingsi Wange6826d22018-03-08 14:55:14 -0800571 Integer getIceCheckIntervalStrongConnectivity() {
572 return iceCheckIntervalStrongConnectivityMs;
573 }
574
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100575 @Nullable
Qingsi Wange6826d22018-03-08 14:55:14 -0800576 @CalledByNative("RTCConfiguration")
577 Integer getIceCheckIntervalWeakConnectivity() {
578 return iceCheckIntervalWeakConnectivityMs;
579 }
580
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100581 @Nullable
Qingsi Wange6826d22018-03-08 14:55:14 -0800582 @CalledByNative("RTCConfiguration")
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100583 Integer getIceCheckMinInterval() {
584 return iceCheckMinInterval;
585 }
586
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100587 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100588 @CalledByNative("RTCConfiguration")
Qingsi Wang22e623a2018-03-13 10:53:57 -0700589 Integer getIceUnwritableTimeout() {
590 return iceUnwritableTimeMs;
591 }
592
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100593 @Nullable
Qingsi Wang22e623a2018-03-13 10:53:57 -0700594 @CalledByNative("RTCConfiguration")
595 Integer getIceUnwritableMinChecks() {
596 return iceUnwritableMinChecks;
597 }
598
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100599 @Nullable
Qingsi Wang22e623a2018-03-13 10:53:57 -0700600 @CalledByNative("RTCConfiguration")
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800601 Integer getStunCandidateKeepaliveInterval() {
602 return stunCandidateKeepaliveIntervalMs;
603 }
604
605 @CalledByNative("RTCConfiguration")
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100606 boolean getDisableIPv6OnWifi() {
607 return disableIPv6OnWifi;
608 }
609
610 @CalledByNative("RTCConfiguration")
611 int getMaxIPv6Networks() {
612 return maxIPv6Networks;
613 }
614
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100615 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100616 @CalledByNative("RTCConfiguration")
617 IntervalRange getIceRegatherIntervalRange() {
618 return iceRegatherIntervalRange;
619 }
620
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100621 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100622 @CalledByNative("RTCConfiguration")
623 TurnCustomizer getTurnCustomizer() {
624 return turnCustomizer;
625 }
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100626
627 @CalledByNative("RTCConfiguration")
628 boolean getDisableIpv6() {
629 return disableIpv6;
630 }
631
632 @CalledByNative("RTCConfiguration")
633 boolean getEnableDscp() {
634 return enableDscp;
635 }
636
637 @CalledByNative("RTCConfiguration")
638 boolean getEnableCpuOveruseDetection() {
639 return enableCpuOveruseDetection;
640 }
641
642 @CalledByNative("RTCConfiguration")
643 boolean getEnableRtpDataChannel() {
644 return enableRtpDataChannel;
645 }
646
647 @CalledByNative("RTCConfiguration")
648 boolean getSuspendBelowMinBitrate() {
649 return suspendBelowMinBitrate;
650 }
651
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100652 @Nullable
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100653 @CalledByNative("RTCConfiguration")
654 Integer getScreencastMinBitrate() {
655 return screencastMinBitrate;
656 }
657
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100658 @Nullable
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100659 @CalledByNative("RTCConfiguration")
660 Boolean getCombinedAudioVideoBwe() {
661 return combinedAudioVideoBwe;
662 }
663
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100664 @Nullable
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100665 @CalledByNative("RTCConfiguration")
666 Boolean getEnableDtlsSrtp() {
667 return enableDtlsSrtp;
668 }
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800669
670 @CalledByNative("RTCConfiguration")
671 AdapterType getNetworkPreference() {
672 return networkPreference;
673 }
Seth Hampsonc384e142018-03-06 15:47:10 -0800674
675 @CalledByNative("RTCConfiguration")
676 SdpSemantics getSdpSemantics() {
677 return sdpSemantics;
678 }
Jiayang Liucac1b382015-04-30 12:35:24 -0700679 };
680
Magnus Jedvert6062f372017-11-16 16:53:12 +0100681 private final List<MediaStream> localStreams = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 private final long nativePeerConnection;
Magnus Jedvert6062f372017-11-16 16:53:12 +0100683 private List<RtpSender> senders = new ArrayList<>();
684 private List<RtpReceiver> receivers = new ArrayList<>();
Seth Hampsonc384e142018-03-06 15:47:10 -0800685 private List<RtpTransceiver> transceivers = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686
Sami Kalliomäki1ece1ed2017-12-20 11:59:22 +0100687 /**
688 * Wraps a PeerConnection created by the factory. Can be used by clients that want to implement
689 * their PeerConnection creation in JNI.
690 */
691 public PeerConnection(NativePeerConnectionFactory factory) {
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100692 this(factory.createNativePeerConnection());
Sami Kalliomäki1ece1ed2017-12-20 11:59:22 +0100693 }
694
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100695 PeerConnection(long nativePeerConnection) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 this.nativePeerConnection = nativePeerConnection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 }
698
699 // JsepInterface.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100700 public SessionDescription getLocalDescription() {
701 return nativeGetLocalDescription();
702 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100704 public SessionDescription getRemoteDescription() {
705 return nativeGetRemoteDescription();
706 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100708 public DataChannel createDataChannel(String label, DataChannel.Init init) {
709 return nativeCreateDataChannel(label, init);
710 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000711
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100712 public void createOffer(SdpObserver observer, MediaConstraints constraints) {
713 nativeCreateOffer(observer, constraints);
714 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100716 public void createAnswer(SdpObserver observer, MediaConstraints constraints) {
717 nativeCreateAnswer(observer, constraints);
718 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100720 public void setLocalDescription(SdpObserver observer, SessionDescription sdp) {
721 nativeSetLocalDescription(observer, sdp);
722 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100724 public void setRemoteDescription(SdpObserver observer, SessionDescription sdp) {
725 nativeSetRemoteDescription(observer, sdp);
726 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727
Seth Hampsonc384e142018-03-06 15:47:10 -0800728 /**
729 * Enables/disables playout of received audio streams. Enabled by default.
730 *
731 * Note that even if playout is enabled, streams will only be played out if
732 * the appropriate SDP is also applied. The main purpose of this API is to
733 * be able to control the exact time when audio playout starts.
734 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100735 public void setAudioPlayout(boolean playout) {
736 nativeSetAudioPlayout(playout);
737 }
henrika5f6bf242017-11-01 11:06:56 +0100738
Seth Hampsonc384e142018-03-06 15:47:10 -0800739 /**
740 * Enables/disables recording of transmitted audio streams. Enabled by default.
741 *
742 * Note that even if recording is enabled, streams will only be recorded if
743 * the appropriate SDP is also applied. The main purpose of this API is to
744 * be able to control the exact time when audio recording starts.
745 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100746 public void setAudioRecording(boolean recording) {
747 nativeSetAudioRecording(recording);
748 }
henrika5f6bf242017-11-01 11:06:56 +0100749
deadbeef5d0b6d82017-01-09 16:05:28 -0800750 public boolean setConfiguration(RTCConfiguration config) {
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100751 return nativeSetConfiguration(config);
deadbeef5d0b6d82017-01-09 16:05:28 -0800752 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753
754 public boolean addIceCandidate(IceCandidate candidate) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100755 return nativeAddIceCandidate(candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 }
757
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700758 public boolean removeIceCandidates(final IceCandidate[] candidates) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100759 return nativeRemoveIceCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700760 }
761
Seth Hampsonc384e142018-03-06 15:47:10 -0800762 /**
763 * Adds a new MediaStream to be sent on this peer connection.
764 * Note: This method is not supported with SdpSemantics.UNIFIED_PLAN. Please
765 * use addTrack instead.
766 */
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000767 public boolean addStream(MediaStream stream) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100768 boolean ret = nativeAddLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 if (!ret) {
770 return false;
771 }
772 localStreams.add(stream);
773 return true;
774 }
775
Seth Hampsonc384e142018-03-06 15:47:10 -0800776 /**
777 * Removes the given media stream from this peer connection.
778 * This method is not supported with SdpSemantics.UNIFIED_PLAN. Please use
779 * removeTrack instead.
780 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 public void removeStream(MediaStream stream) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100782 nativeRemoveLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 localStreams.remove(stream);
784 }
785
deadbeef7a246882017-08-09 08:40:10 -0700786 /**
787 * Creates an RtpSender without a track.
Seth Hampsonc384e142018-03-06 15:47:10 -0800788 *
789 * <p>This method allows an application to cause the PeerConnection to negotiate
deadbeef7a246882017-08-09 08:40:10 -0700790 * sending/receiving a specific media type, but without having a track to
791 * send yet.
Seth Hampsonc384e142018-03-06 15:47:10 -0800792 *
793 * <p>When the application does want to begin sending a track, it can call
deadbeef7a246882017-08-09 08:40:10 -0700794 * RtpSender.setTrack, which doesn't require any additional SDP negotiation.
Seth Hampsonc384e142018-03-06 15:47:10 -0800795 *
796 * <p>Example use:
deadbeef7a246882017-08-09 08:40:10 -0700797 * <pre>
798 * {@code
799 * audioSender = pc.createSender("audio", "stream1");
800 * videoSender = pc.createSender("video", "stream1");
801 * // Do normal SDP offer/answer, which will kick off ICE/DTLS and negotiate
802 * // media parameters....
803 * // Later, when the endpoint is ready to actually begin sending:
804 * audioSender.setTrack(audioTrack, false);
805 * videoSender.setTrack(videoTrack, false);
806 * }
807 * </pre>
Seth Hampsonc384e142018-03-06 15:47:10 -0800808 * <p>Note: This corresponds most closely to "addTransceiver" in the official
deadbeef7a246882017-08-09 08:40:10 -0700809 * WebRTC API, in that it creates a sender without a track. It was
810 * implemented before addTransceiver because it provides useful
811 * functionality, and properly implementing transceivers would have required
812 * a great deal more work.
813 *
Seth Hampsonc384e142018-03-06 15:47:10 -0800814 * <p>Note: This is only available with SdpSemantics.PLAN_B specified. Please use
815 * addTransceiver instead.
816 *
deadbeef7a246882017-08-09 08:40:10 -0700817 * @param kind Corresponds to MediaStreamTrack kinds (must be "audio" or
818 * "video").
819 * @param stream_id The ID of the MediaStream that this sender's track will
820 * be associated with when SDP is applied to the remote
821 * PeerConnection. If createSender is used to create an
822 * audio and video sender that should be synchronized, they
823 * should use the same stream ID.
824 * @return A new RtpSender object if successful, or null otherwise.
825 */
deadbeefbd7d8f72015-12-18 16:58:44 -0800826 public RtpSender createSender(String kind, String stream_id) {
Seth Hampsonc384e142018-03-06 15:47:10 -0800827 RtpSender newSender = nativeCreateSender(kind, stream_id);
828 if (newSender != null) {
829 senders.add(newSender);
deadbeefee524f72015-12-02 11:27:40 -0800830 }
Seth Hampsonc384e142018-03-06 15:47:10 -0800831 return newSender;
deadbeefee524f72015-12-02 11:27:40 -0800832 }
833
Seth Hampsonc384e142018-03-06 15:47:10 -0800834 /**
835 * Gets all RtpSenders associated with this peer connection.
836 * Note that calling getSenders will dispose of the senders previously
837 * returned.
838 */
deadbeef4139c0f2015-10-06 12:29:25 -0700839 public List<RtpSender> getSenders() {
840 for (RtpSender sender : senders) {
841 sender.dispose();
842 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100843 senders = nativeGetSenders();
deadbeef4139c0f2015-10-06 12:29:25 -0700844 return Collections.unmodifiableList(senders);
845 }
846
Seth Hampsonc384e142018-03-06 15:47:10 -0800847 /**
848 * Gets all RtpReceivers associated with this peer connection.
849 * Note that calling getReceivers will dispose of the receivers previously
850 * returned.
851 */
deadbeef4139c0f2015-10-06 12:29:25 -0700852 public List<RtpReceiver> getReceivers() {
853 for (RtpReceiver receiver : receivers) {
854 receiver.dispose();
855 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100856 receivers = nativeGetReceivers();
deadbeef4139c0f2015-10-06 12:29:25 -0700857 return Collections.unmodifiableList(receivers);
858 }
859
Seth Hampsonc384e142018-03-06 15:47:10 -0800860 /**
861 * Gets all RtpTransceivers associated with this peer connection.
862 * Note that calling getTransceivers will dispose of the transceivers previously
863 * returned.
864 * Note: This is only available with SdpSemantics.UNIFIED_PLAN specified.
865 */
866 public List<RtpTransceiver> getTransceivers() {
867 for (RtpTransceiver transceiver : transceivers) {
868 transceiver.dispose();
869 }
870 transceivers = nativeGetTransceivers();
871 return Collections.unmodifiableList(transceivers);
872 }
873
874 /**
875 * Adds a new media stream track to be sent on this peer connection, and returns
876 * the newly created RtpSender. If streamIds are specified, the RtpSender will
877 * be associated with the streams specified in the streamIds list.
878 *
879 * @throws IllegalStateException if an error accors in C++ addTrack.
880 * An error can occur if:
881 * - A sender already exists for the track.
882 * - The peer connection is closed.
883 */
884 public RtpSender addTrack(MediaStreamTrack track) {
885 return addTrack(track, Collections.emptyList());
886 }
887
888 public RtpSender addTrack(MediaStreamTrack track, List<String> streamIds) {
889 if (track == null || streamIds == null) {
890 throw new NullPointerException("No MediaStreamTrack specified in addTrack.");
891 }
892 RtpSender newSender = nativeAddTrack(track.nativeTrack, streamIds);
893 if (newSender == null) {
894 throw new IllegalStateException("C++ addTrack failed.");
895 }
896 senders.add(newSender);
897 return newSender;
898 }
899
900 /**
901 * Stops sending media from sender. The sender will still appear in getSenders. Future
902 * calls to createOffer will mark the m section for the corresponding transceiver as
903 * receive only or inactive, as defined in JSEP. Returns true on success.
904 */
905 public boolean removeTrack(RtpSender sender) {
906 if (sender == null) {
907 throw new NullPointerException("No RtpSender specified for removeTrack.");
908 }
909 return nativeRemoveTrack(sender.nativeRtpSender);
910 }
911
912 /**
913 * Creates a new RtpTransceiver and adds it to the set of transceivers. Adding a
914 * transceiver will cause future calls to CreateOffer to add a media description
915 * for the corresponding transceiver.
916 *
917 * <p>The initial value of |mid| in the returned transceiver is null. Setting a
918 * new session description may change it to a non-null value.
919 *
920 * <p>https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtransceiver
921 *
922 * <p>If a MediaStreamTrack is specified then a transceiver will be added with a
923 * sender set to transmit the given track. The kind
924 * of the transceiver (and sender/receiver) will be derived from the kind of
925 * the track.
926 *
927 * <p>If MediaType is specified then a transceiver will be added based upon that type.
928 * This can be either MEDIA_TYPE_AUDIO or MEDIA_TYPE_VIDEO.
929 *
930 * <p>Optionally, an RtpTransceiverInit structure can be specified to configure
931 * the transceiver from construction. If not specified, the transceiver will
932 * default to having a direction of kSendRecv and not be part of any streams.
933 *
934 * <p>Note: These methods are only available with SdpSemantics.UNIFIED_PLAN specified.
935 * @throws IllegalStateException if an error accors in C++ addTransceiver
936 */
937 public RtpTransceiver addTransceiver(MediaStreamTrack track) {
938 return addTransceiver(track, new RtpTransceiver.RtpTransceiverInit());
939 }
940
941 public RtpTransceiver addTransceiver(
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100942 MediaStreamTrack track, @Nullable RtpTransceiver.RtpTransceiverInit init) {
Seth Hampsonc384e142018-03-06 15:47:10 -0800943 if (track == null) {
944 throw new NullPointerException("No MediaStreamTrack specified for addTransceiver.");
945 }
946 if (init == null) {
947 init = new RtpTransceiver.RtpTransceiverInit();
948 }
949 RtpTransceiver newTransceiver = nativeAddTransceiverWithTrack(track.nativeTrack, init);
950 if (newTransceiver == null) {
951 throw new IllegalStateException("C++ addTransceiver failed.");
952 }
953 transceivers.add(newTransceiver);
954 return newTransceiver;
955 }
956
957 public RtpTransceiver addTransceiver(MediaStreamTrack.MediaType mediaType) {
958 return addTransceiver(mediaType, new RtpTransceiver.RtpTransceiverInit());
959 }
960
961 public RtpTransceiver addTransceiver(
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100962 MediaStreamTrack.MediaType mediaType, @Nullable RtpTransceiver.RtpTransceiverInit init) {
Seth Hampsonc384e142018-03-06 15:47:10 -0800963 if (mediaType == null) {
964 throw new NullPointerException("No MediaType specified for addTransceiver.");
965 }
966 if (init == null) {
967 init = new RtpTransceiver.RtpTransceiverInit();
968 }
969 RtpTransceiver newTransceiver = nativeAddTransceiverOfType(mediaType, init);
970 if (newTransceiver == null) {
971 throw new IllegalStateException("C++ addTransceiver failed.");
972 }
973 transceivers.add(newTransceiver);
974 return newTransceiver;
975 }
976
deadbeef82215872017-04-18 10:27:51 -0700977 // Older, non-standard implementation of getStats.
978 @Deprecated
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100979 public boolean getStats(StatsObserver observer, @Nullable MediaStreamTrack track) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100980 return nativeOldGetStats(observer, (track == null) ? 0 : track.nativeTrack);
deadbeef82215872017-04-18 10:27:51 -0700981 }
982
Seth Hampsonc384e142018-03-06 15:47:10 -0800983 /**
984 * Gets stats using the new stats collection API, see webrtc/api/stats/. These
985 * will replace old stats collection API when the new API has matured enough.
986 */
deadbeef82215872017-04-18 10:27:51 -0700987 public void getStats(RTCStatsCollectorCallback callback) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100988 nativeNewGetStats(callback);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 }
990
Seth Hampsonc384e142018-03-06 15:47:10 -0800991 /**
992 * Limits the bandwidth allocated for all RTP streams sent by this
993 * PeerConnection. Pass null to leave a value unchanged.
994 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100995 public boolean setBitrate(Integer min, Integer current, Integer max) {
996 return nativeSetBitrate(min, current, max);
997 }
zsteind89b0bc2017-08-03 11:11:40 -0700998
Seth Hampsonc384e142018-03-06 15:47:10 -0800999 /**
1000 * Starts recording an RTC event log.
1001 *
1002 * Ownership of the file is transfered to the native code. If an RTC event
1003 * log is already being recorded, it will be stopped and a new one will start
1004 * using the provided file. Logging will continue until the stopRtcEventLog
1005 * function is called. The max_size_bytes argument is ignored, it is added
1006 * for future use.
1007 */
ivoc0c6f0f62016-07-06 04:34:23 -07001008 public boolean startRtcEventLog(int file_descriptor, int max_size_bytes) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001009 return nativeStartRtcEventLog(file_descriptor, max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -07001010 }
1011
Seth Hampsonc384e142018-03-06 15:47:10 -08001012 /**
1013 * Stops recording an RTC event log. If no RTC event log is currently being
1014 * recorded, this call will have no effect.
1015 */
ivoc14d5dbe2016-07-04 07:06:55 -07001016 public void stopRtcEventLog() {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001017 nativeStopRtcEventLog();
ivoc14d5dbe2016-07-04 07:06:55 -07001018 }
1019
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020 // TODO(fischman): add support for DTMF-related methods once that API
1021 // stabilizes.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001022 public SignalingState signalingState() {
1023 return nativeSignalingState();
1024 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001025
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001026 public IceConnectionState iceConnectionState() {
1027 return nativeIceConnectionState();
1028 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001030 public IceGatheringState iceGatheringState() {
1031 return nativeIceGatheringState();
1032 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001034 public void close() {
1035 nativeClose();
1036 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001037
deadbeef43697f62017-09-12 10:52:14 -07001038 /**
1039 * Free native resources associated with this PeerConnection instance.
Seth Hampsonc384e142018-03-06 15:47:10 -08001040 *
deadbeef43697f62017-09-12 10:52:14 -07001041 * This method removes a reference count from the C++ PeerConnection object,
1042 * which should result in it being destroyed. It also calls equivalent
1043 * "dispose" methods on the Java objects attached to this PeerConnection
1044 * (streams, senders, receivers), such that their associated C++ objects
1045 * will also be destroyed.
Seth Hampsonc384e142018-03-06 15:47:10 -08001046 *
1047 * <p>Note that this method cannot be safely called from an observer callback
deadbeef43697f62017-09-12 10:52:14 -07001048 * (PeerConnection.Observer, DataChannel.Observer, etc.). If you want to, for
1049 * example, destroy the PeerConnection after an "ICE failed" callback, you
1050 * must do this asynchronously (in other words, unwind the stack first). See
1051 * <a href="https://bugs.chromium.org/p/webrtc/issues/detail?id=3721">bug
1052 * 3721</a> for more details.
1053 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001054 public void dispose() {
1055 close();
1056 for (MediaStream stream : localStreams) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001057 nativeRemoveLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 stream.dispose();
1059 }
1060 localStreams.clear();
deadbeef4139c0f2015-10-06 12:29:25 -07001061 for (RtpSender sender : senders) {
1062 sender.dispose();
1063 }
1064 senders.clear();
1065 for (RtpReceiver receiver : receivers) {
1066 receiver.dispose();
1067 }
Seth Hampsonc384e142018-03-06 15:47:10 -08001068 for (RtpTransceiver transceiver : transceivers) {
1069 transceiver.dispose();
1070 }
1071 transceivers.clear();
deadbeef4139c0f2015-10-06 12:29:25 -07001072 receivers.clear();
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001073 nativeFreeOwnedPeerConnection(nativePeerConnection);
1074 }
1075
1076 /** Returns a pointer to the native webrtc::PeerConnectionInterface. */
1077 public long getNativePeerConnection() {
1078 return nativeGetNativePeerConnection();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001079 }
1080
Magnus Jedvert9060eb12017-12-12 12:52:54 +01001081 @CalledByNative
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001082 long getNativeOwnedPeerConnection() {
Magnus Jedvert9060eb12017-12-12 12:52:54 +01001083 return nativePeerConnection;
1084 }
1085
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001086 public static long createNativePeerConnectionObserver(Observer observer) {
1087 return nativeCreatePeerConnectionObserver(observer);
1088 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001090 private native long nativeGetNativePeerConnection();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001091 private native SessionDescription nativeGetLocalDescription();
1092 private native SessionDescription nativeGetRemoteDescription();
1093 private native DataChannel nativeCreateDataChannel(String label, DataChannel.Init init);
1094 private native void nativeCreateOffer(SdpObserver observer, MediaConstraints constraints);
1095 private native void nativeCreateAnswer(SdpObserver observer, MediaConstraints constraints);
1096 private native void nativeSetLocalDescription(SdpObserver observer, SessionDescription sdp);
1097 private native void nativeSetRemoteDescription(SdpObserver observer, SessionDescription sdp);
1098 private native void nativeSetAudioPlayout(boolean playout);
1099 private native void nativeSetAudioRecording(boolean recording);
1100 private native boolean nativeSetBitrate(Integer min, Integer current, Integer max);
1101 private native SignalingState nativeSignalingState();
1102 private native IceConnectionState nativeIceConnectionState();
1103 private native IceGatheringState nativeIceGatheringState();
1104 private native void nativeClose();
1105 private static native long nativeCreatePeerConnectionObserver(Observer observer);
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001106 private static native void nativeFreeOwnedPeerConnection(long ownedPeerConnection);
1107 private native boolean nativeSetConfiguration(RTCConfiguration config);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001108 private native boolean nativeAddIceCandidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 String sdpMid, int sdpMLineIndex, String iceCandidateSdp);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001110 private native boolean nativeRemoveIceCandidates(final IceCandidate[] candidates);
1111 private native boolean nativeAddLocalStream(long stream);
1112 private native void nativeRemoveLocalStream(long stream);
1113 private native boolean nativeOldGetStats(StatsObserver observer, long nativeTrack);
1114 private native void nativeNewGetStats(RTCStatsCollectorCallback callback);
1115 private native RtpSender nativeCreateSender(String kind, String stream_id);
1116 private native List<RtpSender> nativeGetSenders();
1117 private native List<RtpReceiver> nativeGetReceivers();
Seth Hampsonc384e142018-03-06 15:47:10 -08001118 private native List<RtpTransceiver> nativeGetTransceivers();
1119 private native RtpSender nativeAddTrack(long track, List<String> streamIds);
1120 private native boolean nativeRemoveTrack(long sender);
1121 private native RtpTransceiver nativeAddTransceiverWithTrack(
1122 long track, RtpTransceiver.RtpTransceiverInit init);
1123 private native RtpTransceiver nativeAddTransceiverOfType(
1124 MediaStreamTrack.MediaType mediaType, RtpTransceiver.RtpTransceiverInit init);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001125 private native boolean nativeStartRtcEventLog(int file_descriptor, int max_size_bytes);
1126 private native void nativeStopRtcEventLog();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127}