blob: 1cb5717b4eb2514f3e130b7c8cbb633eaad3525a [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;
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000129 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.
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000138 public final List<String> tlsAlpnProtocols;
Diogo Real1dca9d52017-08-29 12:18:32 -0700139
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").
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000142 public final List<String> tlsEllipticCurves;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700143
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,
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000164 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,
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000169 List<String> tlsEllipticCurves) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700170 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
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000200 + "] [" + 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,
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000259 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;
Michael Iedema02137862018-10-09 15:30:01 +0200388 @Nullable public RtcCertificatePem certificate;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700389 public RtcpMuxPolicy rtcpMuxPolicy;
Jiayang Liucac1b382015-04-30 12:35:24 -0700390 public TcpCandidatePolicy tcpCandidatePolicy;
honghaiz60347052016-05-31 18:29:12 -0700391 public CandidateNetworkPolicy candidateNetworkPolicy;
Henrik Lundin64dad832015-05-11 12:44:23 +0200392 public int audioJitterBufferMaxPackets;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200393 public boolean audioJitterBufferFastAccelerate;
honghaiz4edc39c2015-09-01 09:53:56 -0700394 public int iceConnectionReceivingTimeout;
Honghai Zhang381b4212015-12-04 12:24:03 -0800395 public int iceBackupCandidatePairPingInterval;
glaznev97579a42015-09-01 11:31:27 -0700396 public KeyType keyType;
honghaiz1f429e32015-09-28 07:57:34 -0700397 public ContinualGatheringPolicy continualGatheringPolicy;
deadbeefbe0c96f2016-05-18 16:20:14 -0700398 public int iceCandidatePoolSize;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700399 public boolean pruneTurnPorts;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700400 public boolean presumeWritableWhenFullyRelayed;
Qingsi Wange6826d22018-03-08 14:55:14 -0800401 // The following fields define intervals in milliseconds at which ICE
402 // connectivity checks are sent.
403 //
404 // We consider ICE is "strongly connected" for an agent when there is at
405 // least one candidate pair that currently succeeds in connectivity check
406 // from its direction i.e. sending a ping and receives a ping response, AND
407 // all candidate pairs have sent a minimum number of pings for connectivity
408 // (this number is implementation-specific). Otherwise, ICE is considered in
409 // "weak connectivity".
410 //
411 // Note that the above notion of strong and weak connectivity is not defined
412 // in RFC 5245, and they apply to our current ICE implementation only.
413 //
414 // 1) iceCheckIntervalStrongConnectivityMs defines the interval applied to
415 // ALL candidate pairs when ICE is strongly connected,
416 // 2) iceCheckIntervalWeakConnectivityMs defines the counterpart for ALL
417 // pairs when ICE is weakly connected, and
418 // 3) iceCheckMinInterval defines the minimal interval (equivalently the
419 // maximum rate) that overrides the above two intervals when either of them
420 // is less.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100421 @Nullable public Integer iceCheckIntervalStrongConnectivityMs;
422 @Nullable public Integer iceCheckIntervalWeakConnectivityMs;
423 @Nullable public Integer iceCheckMinInterval;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700424 // The time period in milliseconds for which a candidate pair must wait for response to
425 // connectivitiy checks before it becomes unwritable.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100426 @Nullable public Integer iceUnwritableTimeMs;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700427 // The minimum number of connectivity checks that a candidate pair must sent without receiving
428 // response before it becomes unwritable.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100429 @Nullable public Integer iceUnwritableMinChecks;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800430 // The interval in milliseconds at which STUN candidates will resend STUN binding requests
431 // to keep NAT bindings open.
432 // The default value in the implementation is used if this field is null.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100433 @Nullable public Integer stunCandidateKeepaliveIntervalMs;
zhihuangb09b3f92017-03-07 14:40:51 -0800434 public boolean disableIPv6OnWifi;
deadbeef28e29192017-07-27 09:14:38 -0700435 // By default, PeerConnection will use a limited number of IPv6 network
436 // interfaces, in order to avoid too many ICE candidate pairs being created
437 // and delaying ICE completion.
438 //
439 // Can be set to Integer.MAX_VALUE to effectively disable the limit.
440 public int maxIPv6Networks;
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100441 @Nullable public IntervalRange iceRegatherIntervalRange;
Jiayang Liucac1b382015-04-30 12:35:24 -0700442
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100443 // These values will be overridden by MediaStream constraints if deprecated constraints-based
444 // create peerconnection interface is used.
445 public boolean disableIpv6;
446 public boolean enableDscp;
447 public boolean enableCpuOveruseDetection;
448 public boolean enableRtpDataChannel;
449 public boolean suspendBelowMinBitrate;
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100450 @Nullable public Integer screencastMinBitrate;
451 @Nullable public Boolean combinedAudioVideoBwe;
452 @Nullable public Boolean enableDtlsSrtp;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800453 // Use "Unknown" to represent no preference of adapter types, not the
454 // preference of adapters of unknown types.
455 public AdapterType networkPreference;
Seth Hampsonc384e142018-03-06 15:47:10 -0800456 public SdpSemantics sdpSemantics;
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100457
Jonas Orelandbdcee282017-10-10 14:01:40 +0200458 // This is an optional wrapper for the C++ webrtc::TurnCustomizer.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100459 @Nullable public TurnCustomizer turnCustomizer;
Jonas Orelandbdcee282017-10-10 14:01:40 +0200460
Zhi Huangb57e1692018-06-12 11:41:11 -0700461 // Actively reset the SRTP parameters whenever the DTLS transports underneath are reset for
462 // every offer/answer negotiation.This is only intended to be a workaround for crbug.com/835958
463 public boolean activeResetSrtpParams;
464
Piotr (Peter) Slatala09beff22018-10-17 07:22:40 -0700465 /*
466 * Experimental flag that enables a use of media transport. If this is true, the media transport
467 * factory MUST be provided to the PeerConnectionFactory.
468 */
469 public boolean useMediaTransport;
470
Benjamin Wright8c27cca2018-10-25 10:16:44 -0700471 /**
472 * Defines advanced optional cryptographic settings related to SRTP and
473 * frame encryption for native WebRTC. Setting this will overwrite any
474 * options set through the PeerConnectionFactory (which is deprecated).
475 */
476 @Nullable public CryptoOptions cryptoOptions;
477
deadbeef28e29192017-07-27 09:14:38 -0700478 // TODO(deadbeef): Instead of duplicating the defaults here, we should do
479 // something to pick up the defaults from C++. The Objective-C equivalent
480 // of RTCConfiguration does that.
Jiayang Liucac1b382015-04-30 12:35:24 -0700481 public RTCConfiguration(List<IceServer> iceServers) {
482 iceTransportsType = IceTransportsType.ALL;
483 bundlePolicy = BundlePolicy.BALANCED;
zhihuang4dfb8ce2016-11-23 10:30:12 -0800484 rtcpMuxPolicy = RtcpMuxPolicy.REQUIRE;
Jiayang Liucac1b382015-04-30 12:35:24 -0700485 tcpCandidatePolicy = TcpCandidatePolicy.ENABLED;
Sami Kalliomäki9828beb2017-10-26 16:21:22 +0200486 candidateNetworkPolicy = CandidateNetworkPolicy.ALL;
Jiayang Liucac1b382015-04-30 12:35:24 -0700487 this.iceServers = iceServers;
Henrik Lundin64dad832015-05-11 12:44:23 +0200488 audioJitterBufferMaxPackets = 50;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200489 audioJitterBufferFastAccelerate = false;
honghaiz4edc39c2015-09-01 09:53:56 -0700490 iceConnectionReceivingTimeout = -1;
Honghai Zhang381b4212015-12-04 12:24:03 -0800491 iceBackupCandidatePairPingInterval = -1;
glaznev97579a42015-09-01 11:31:27 -0700492 keyType = KeyType.ECDSA;
honghaiz1f429e32015-09-28 07:57:34 -0700493 continualGatheringPolicy = ContinualGatheringPolicy.GATHER_ONCE;
deadbeefbe0c96f2016-05-18 16:20:14 -0700494 iceCandidatePoolSize = 0;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700495 pruneTurnPorts = false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700496 presumeWritableWhenFullyRelayed = false;
Qingsi Wange6826d22018-03-08 14:55:14 -0800497 iceCheckIntervalStrongConnectivityMs = null;
498 iceCheckIntervalWeakConnectivityMs = null;
skvlad51072462017-02-02 11:50:14 -0800499 iceCheckMinInterval = null;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700500 iceUnwritableTimeMs = null;
501 iceUnwritableMinChecks = null;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800502 stunCandidateKeepaliveIntervalMs = null;
zhihuangb09b3f92017-03-07 14:40:51 -0800503 disableIPv6OnWifi = false;
deadbeef28e29192017-07-27 09:14:38 -0700504 maxIPv6Networks = 5;
Steve Antond960a0c2017-07-17 12:33:07 -0700505 iceRegatherIntervalRange = null;
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100506 disableIpv6 = false;
507 enableDscp = false;
508 enableCpuOveruseDetection = true;
509 enableRtpDataChannel = false;
510 suspendBelowMinBitrate = false;
511 screencastMinBitrate = null;
512 combinedAudioVideoBwe = null;
513 enableDtlsSrtp = null;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800514 networkPreference = AdapterType.UNKNOWN;
Seth Hampsonc384e142018-03-06 15:47:10 -0800515 sdpSemantics = SdpSemantics.PLAN_B;
Zhi Huangb57e1692018-06-12 11:41:11 -0700516 activeResetSrtpParams = false;
Piotr (Peter) Slatala09beff22018-10-17 07:22:40 -0700517 useMediaTransport = false;
Benjamin Wright8c27cca2018-10-25 10:16:44 -0700518 cryptoOptions = null;
Jiayang Liucac1b382015-04-30 12:35:24 -0700519 }
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100520
521 @CalledByNative("RTCConfiguration")
522 IceTransportsType getIceTransportsType() {
523 return iceTransportsType;
524 }
525
526 @CalledByNative("RTCConfiguration")
527 List<IceServer> getIceServers() {
528 return iceServers;
529 }
530
531 @CalledByNative("RTCConfiguration")
532 BundlePolicy getBundlePolicy() {
533 return bundlePolicy;
534 }
535
Michael Iedema02137862018-10-09 15:30:01 +0200536 @Nullable
537 @CalledByNative("RTCConfiguration")
538 RtcCertificatePem getCertificate() {
539 return certificate;
540 }
541
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100542 @CalledByNative("RTCConfiguration")
543 RtcpMuxPolicy getRtcpMuxPolicy() {
544 return rtcpMuxPolicy;
545 }
546
547 @CalledByNative("RTCConfiguration")
548 TcpCandidatePolicy getTcpCandidatePolicy() {
549 return tcpCandidatePolicy;
550 }
551
552 @CalledByNative("RTCConfiguration")
553 CandidateNetworkPolicy getCandidateNetworkPolicy() {
554 return candidateNetworkPolicy;
555 }
556
557 @CalledByNative("RTCConfiguration")
558 int getAudioJitterBufferMaxPackets() {
559 return audioJitterBufferMaxPackets;
560 }
561
562 @CalledByNative("RTCConfiguration")
563 boolean getAudioJitterBufferFastAccelerate() {
564 return audioJitterBufferFastAccelerate;
565 }
566
567 @CalledByNative("RTCConfiguration")
568 int getIceConnectionReceivingTimeout() {
569 return iceConnectionReceivingTimeout;
570 }
571
572 @CalledByNative("RTCConfiguration")
573 int getIceBackupCandidatePairPingInterval() {
574 return iceBackupCandidatePairPingInterval;
575 }
576
577 @CalledByNative("RTCConfiguration")
578 KeyType getKeyType() {
579 return keyType;
580 }
581
582 @CalledByNative("RTCConfiguration")
583 ContinualGatheringPolicy getContinualGatheringPolicy() {
584 return continualGatheringPolicy;
585 }
586
587 @CalledByNative("RTCConfiguration")
588 int getIceCandidatePoolSize() {
589 return iceCandidatePoolSize;
590 }
591
592 @CalledByNative("RTCConfiguration")
593 boolean getPruneTurnPorts() {
594 return pruneTurnPorts;
595 }
596
597 @CalledByNative("RTCConfiguration")
598 boolean getPresumeWritableWhenFullyRelayed() {
599 return presumeWritableWhenFullyRelayed;
600 }
601
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100602 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100603 @CalledByNative("RTCConfiguration")
Qingsi Wange6826d22018-03-08 14:55:14 -0800604 Integer getIceCheckIntervalStrongConnectivity() {
605 return iceCheckIntervalStrongConnectivityMs;
606 }
607
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100608 @Nullable
Qingsi Wange6826d22018-03-08 14:55:14 -0800609 @CalledByNative("RTCConfiguration")
610 Integer getIceCheckIntervalWeakConnectivity() {
611 return iceCheckIntervalWeakConnectivityMs;
612 }
613
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100614 @Nullable
Qingsi Wange6826d22018-03-08 14:55:14 -0800615 @CalledByNative("RTCConfiguration")
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100616 Integer getIceCheckMinInterval() {
617 return iceCheckMinInterval;
618 }
619
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100620 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100621 @CalledByNative("RTCConfiguration")
Qingsi Wang22e623a2018-03-13 10:53:57 -0700622 Integer getIceUnwritableTimeout() {
623 return iceUnwritableTimeMs;
624 }
625
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100626 @Nullable
Qingsi Wang22e623a2018-03-13 10:53:57 -0700627 @CalledByNative("RTCConfiguration")
628 Integer getIceUnwritableMinChecks() {
629 return iceUnwritableMinChecks;
630 }
631
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100632 @Nullable
Qingsi Wang22e623a2018-03-13 10:53:57 -0700633 @CalledByNative("RTCConfiguration")
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800634 Integer getStunCandidateKeepaliveInterval() {
635 return stunCandidateKeepaliveIntervalMs;
636 }
637
638 @CalledByNative("RTCConfiguration")
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100639 boolean getDisableIPv6OnWifi() {
640 return disableIPv6OnWifi;
641 }
642
643 @CalledByNative("RTCConfiguration")
644 int getMaxIPv6Networks() {
645 return maxIPv6Networks;
646 }
647
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100648 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100649 @CalledByNative("RTCConfiguration")
650 IntervalRange getIceRegatherIntervalRange() {
651 return iceRegatherIntervalRange;
652 }
653
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100654 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100655 @CalledByNative("RTCConfiguration")
656 TurnCustomizer getTurnCustomizer() {
657 return turnCustomizer;
658 }
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100659
660 @CalledByNative("RTCConfiguration")
661 boolean getDisableIpv6() {
662 return disableIpv6;
663 }
664
665 @CalledByNative("RTCConfiguration")
666 boolean getEnableDscp() {
667 return enableDscp;
668 }
669
670 @CalledByNative("RTCConfiguration")
671 boolean getEnableCpuOveruseDetection() {
672 return enableCpuOveruseDetection;
673 }
674
675 @CalledByNative("RTCConfiguration")
676 boolean getEnableRtpDataChannel() {
677 return enableRtpDataChannel;
678 }
679
680 @CalledByNative("RTCConfiguration")
681 boolean getSuspendBelowMinBitrate() {
682 return suspendBelowMinBitrate;
683 }
684
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100685 @Nullable
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100686 @CalledByNative("RTCConfiguration")
687 Integer getScreencastMinBitrate() {
688 return screencastMinBitrate;
689 }
690
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100691 @Nullable
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100692 @CalledByNative("RTCConfiguration")
693 Boolean getCombinedAudioVideoBwe() {
694 return combinedAudioVideoBwe;
695 }
696
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100697 @Nullable
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100698 @CalledByNative("RTCConfiguration")
699 Boolean getEnableDtlsSrtp() {
700 return enableDtlsSrtp;
701 }
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800702
703 @CalledByNative("RTCConfiguration")
704 AdapterType getNetworkPreference() {
705 return networkPreference;
706 }
Seth Hampsonc384e142018-03-06 15:47:10 -0800707
708 @CalledByNative("RTCConfiguration")
709 SdpSemantics getSdpSemantics() {
710 return sdpSemantics;
711 }
Zhi Huangb57e1692018-06-12 11:41:11 -0700712
713 @CalledByNative("RTCConfiguration")
714 boolean getActiveResetSrtpParams() {
715 return activeResetSrtpParams;
716 }
Piotr (Peter) Slatala09beff22018-10-17 07:22:40 -0700717
718 @CalledByNative("RTCConfiguration")
719 boolean getUseMediaTransport() {
720 return useMediaTransport;
721 }
Benjamin Wright8c27cca2018-10-25 10:16:44 -0700722
723 @Nullable
724 @CalledByNative("RTCConfiguration")
725 CryptoOptions getCryptoOptions() {
726 return cryptoOptions;
727 }
Jiayang Liucac1b382015-04-30 12:35:24 -0700728 };
729
Magnus Jedvert6062f372017-11-16 16:53:12 +0100730 private final List<MediaStream> localStreams = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 private final long nativePeerConnection;
Magnus Jedvert6062f372017-11-16 16:53:12 +0100732 private List<RtpSender> senders = new ArrayList<>();
733 private List<RtpReceiver> receivers = new ArrayList<>();
Seth Hampsonc384e142018-03-06 15:47:10 -0800734 private List<RtpTransceiver> transceivers = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735
Sami Kalliomäki1ece1ed2017-12-20 11:59:22 +0100736 /**
737 * Wraps a PeerConnection created by the factory. Can be used by clients that want to implement
738 * their PeerConnection creation in JNI.
739 */
740 public PeerConnection(NativePeerConnectionFactory factory) {
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100741 this(factory.createNativePeerConnection());
Sami Kalliomäki1ece1ed2017-12-20 11:59:22 +0100742 }
743
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100744 PeerConnection(long nativePeerConnection) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 this.nativePeerConnection = nativePeerConnection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 }
747
748 // JsepInterface.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100749 public SessionDescription getLocalDescription() {
750 return nativeGetLocalDescription();
751 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100753 public SessionDescription getRemoteDescription() {
754 return nativeGetRemoteDescription();
755 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756
Michael Iedema02137862018-10-09 15:30:01 +0200757 public RtcCertificatePem getCertificate() {
758 return nativeGetCertificate();
759 }
760
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100761 public DataChannel createDataChannel(String label, DataChannel.Init init) {
762 return nativeCreateDataChannel(label, init);
763 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000764
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100765 public void createOffer(SdpObserver observer, MediaConstraints constraints) {
766 nativeCreateOffer(observer, constraints);
767 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100769 public void createAnswer(SdpObserver observer, MediaConstraints constraints) {
770 nativeCreateAnswer(observer, constraints);
771 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100773 public void setLocalDescription(SdpObserver observer, SessionDescription sdp) {
774 nativeSetLocalDescription(observer, sdp);
775 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100777 public void setRemoteDescription(SdpObserver observer, SessionDescription sdp) {
778 nativeSetRemoteDescription(observer, sdp);
779 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780
Seth Hampsonc384e142018-03-06 15:47:10 -0800781 /**
782 * Enables/disables playout of received audio streams. Enabled by default.
783 *
784 * Note that even if playout is enabled, streams will only be played out if
785 * the appropriate SDP is also applied. The main purpose of this API is to
786 * be able to control the exact time when audio playout starts.
787 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100788 public void setAudioPlayout(boolean playout) {
789 nativeSetAudioPlayout(playout);
790 }
henrika5f6bf242017-11-01 11:06:56 +0100791
Seth Hampsonc384e142018-03-06 15:47:10 -0800792 /**
793 * Enables/disables recording of transmitted audio streams. Enabled by default.
794 *
795 * Note that even if recording is enabled, streams will only be recorded if
796 * the appropriate SDP is also applied. The main purpose of this API is to
797 * be able to control the exact time when audio recording starts.
798 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100799 public void setAudioRecording(boolean recording) {
800 nativeSetAudioRecording(recording);
801 }
henrika5f6bf242017-11-01 11:06:56 +0100802
deadbeef5d0b6d82017-01-09 16:05:28 -0800803 public boolean setConfiguration(RTCConfiguration config) {
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100804 return nativeSetConfiguration(config);
deadbeef5d0b6d82017-01-09 16:05:28 -0800805 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806
807 public boolean addIceCandidate(IceCandidate candidate) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100808 return nativeAddIceCandidate(candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 }
810
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700811 public boolean removeIceCandidates(final IceCandidate[] candidates) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100812 return nativeRemoveIceCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700813 }
814
Seth Hampsonc384e142018-03-06 15:47:10 -0800815 /**
816 * Adds a new MediaStream to be sent on this peer connection.
817 * Note: This method is not supported with SdpSemantics.UNIFIED_PLAN. Please
818 * use addTrack instead.
819 */
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000820 public boolean addStream(MediaStream stream) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200821 boolean ret = nativeAddLocalStream(stream.getNativeMediaStream());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 if (!ret) {
823 return false;
824 }
825 localStreams.add(stream);
826 return true;
827 }
828
Seth Hampsonc384e142018-03-06 15:47:10 -0800829 /**
830 * Removes the given media stream from this peer connection.
831 * This method is not supported with SdpSemantics.UNIFIED_PLAN. Please use
832 * removeTrack instead.
833 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 public void removeStream(MediaStream stream) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200835 nativeRemoveLocalStream(stream.getNativeMediaStream());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 localStreams.remove(stream);
837 }
838
deadbeef7a246882017-08-09 08:40:10 -0700839 /**
840 * Creates an RtpSender without a track.
Seth Hampsonc384e142018-03-06 15:47:10 -0800841 *
842 * <p>This method allows an application to cause the PeerConnection to negotiate
deadbeef7a246882017-08-09 08:40:10 -0700843 * sending/receiving a specific media type, but without having a track to
844 * send yet.
Seth Hampsonc384e142018-03-06 15:47:10 -0800845 *
846 * <p>When the application does want to begin sending a track, it can call
deadbeef7a246882017-08-09 08:40:10 -0700847 * RtpSender.setTrack, which doesn't require any additional SDP negotiation.
Seth Hampsonc384e142018-03-06 15:47:10 -0800848 *
849 * <p>Example use:
deadbeef7a246882017-08-09 08:40:10 -0700850 * <pre>
851 * {@code
852 * audioSender = pc.createSender("audio", "stream1");
853 * videoSender = pc.createSender("video", "stream1");
854 * // Do normal SDP offer/answer, which will kick off ICE/DTLS and negotiate
855 * // media parameters....
856 * // Later, when the endpoint is ready to actually begin sending:
857 * audioSender.setTrack(audioTrack, false);
858 * videoSender.setTrack(videoTrack, false);
859 * }
860 * </pre>
Seth Hampsonc384e142018-03-06 15:47:10 -0800861 * <p>Note: This corresponds most closely to "addTransceiver" in the official
deadbeef7a246882017-08-09 08:40:10 -0700862 * WebRTC API, in that it creates a sender without a track. It was
863 * implemented before addTransceiver because it provides useful
864 * functionality, and properly implementing transceivers would have required
865 * a great deal more work.
866 *
Seth Hampsonc384e142018-03-06 15:47:10 -0800867 * <p>Note: This is only available with SdpSemantics.PLAN_B specified. Please use
868 * addTransceiver instead.
869 *
deadbeef7a246882017-08-09 08:40:10 -0700870 * @param kind Corresponds to MediaStreamTrack kinds (must be "audio" or
871 * "video").
872 * @param stream_id The ID of the MediaStream that this sender's track will
873 * be associated with when SDP is applied to the remote
874 * PeerConnection. If createSender is used to create an
875 * audio and video sender that should be synchronized, they
876 * should use the same stream ID.
877 * @return A new RtpSender object if successful, or null otherwise.
878 */
deadbeefbd7d8f72015-12-18 16:58:44 -0800879 public RtpSender createSender(String kind, String stream_id) {
Seth Hampsonc384e142018-03-06 15:47:10 -0800880 RtpSender newSender = nativeCreateSender(kind, stream_id);
881 if (newSender != null) {
882 senders.add(newSender);
deadbeefee524f72015-12-02 11:27:40 -0800883 }
Seth Hampsonc384e142018-03-06 15:47:10 -0800884 return newSender;
deadbeefee524f72015-12-02 11:27:40 -0800885 }
886
Seth Hampsonc384e142018-03-06 15:47:10 -0800887 /**
888 * Gets all RtpSenders associated with this peer connection.
889 * Note that calling getSenders will dispose of the senders previously
890 * returned.
891 */
deadbeef4139c0f2015-10-06 12:29:25 -0700892 public List<RtpSender> getSenders() {
893 for (RtpSender sender : senders) {
894 sender.dispose();
895 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100896 senders = nativeGetSenders();
deadbeef4139c0f2015-10-06 12:29:25 -0700897 return Collections.unmodifiableList(senders);
898 }
899
Seth Hampsonc384e142018-03-06 15:47:10 -0800900 /**
901 * Gets all RtpReceivers associated with this peer connection.
902 * Note that calling getReceivers will dispose of the receivers previously
903 * returned.
904 */
deadbeef4139c0f2015-10-06 12:29:25 -0700905 public List<RtpReceiver> getReceivers() {
906 for (RtpReceiver receiver : receivers) {
907 receiver.dispose();
908 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100909 receivers = nativeGetReceivers();
deadbeef4139c0f2015-10-06 12:29:25 -0700910 return Collections.unmodifiableList(receivers);
911 }
912
Seth Hampsonc384e142018-03-06 15:47:10 -0800913 /**
914 * Gets all RtpTransceivers associated with this peer connection.
915 * Note that calling getTransceivers will dispose of the transceivers previously
916 * returned.
917 * Note: This is only available with SdpSemantics.UNIFIED_PLAN specified.
918 */
919 public List<RtpTransceiver> getTransceivers() {
920 for (RtpTransceiver transceiver : transceivers) {
921 transceiver.dispose();
922 }
923 transceivers = nativeGetTransceivers();
924 return Collections.unmodifiableList(transceivers);
925 }
926
927 /**
928 * Adds a new media stream track to be sent on this peer connection, and returns
929 * the newly created RtpSender. If streamIds are specified, the RtpSender will
930 * be associated with the streams specified in the streamIds list.
931 *
932 * @throws IllegalStateException if an error accors in C++ addTrack.
933 * An error can occur if:
934 * - A sender already exists for the track.
935 * - The peer connection is closed.
936 */
937 public RtpSender addTrack(MediaStreamTrack track) {
938 return addTrack(track, Collections.emptyList());
939 }
940
941 public RtpSender addTrack(MediaStreamTrack track, List<String> streamIds) {
942 if (track == null || streamIds == null) {
943 throw new NullPointerException("No MediaStreamTrack specified in addTrack.");
944 }
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200945 RtpSender newSender = nativeAddTrack(track.getNativeMediaStreamTrack(), streamIds);
Seth Hampsonc384e142018-03-06 15:47:10 -0800946 if (newSender == null) {
947 throw new IllegalStateException("C++ addTrack failed.");
948 }
949 senders.add(newSender);
950 return newSender;
951 }
952
953 /**
954 * Stops sending media from sender. The sender will still appear in getSenders. Future
955 * calls to createOffer will mark the m section for the corresponding transceiver as
956 * receive only or inactive, as defined in JSEP. Returns true on success.
957 */
958 public boolean removeTrack(RtpSender sender) {
959 if (sender == null) {
960 throw new NullPointerException("No RtpSender specified for removeTrack.");
961 }
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200962 return nativeRemoveTrack(sender.getNativeRtpSender());
Seth Hampsonc384e142018-03-06 15:47:10 -0800963 }
964
965 /**
966 * Creates a new RtpTransceiver and adds it to the set of transceivers. Adding a
967 * transceiver will cause future calls to CreateOffer to add a media description
968 * for the corresponding transceiver.
969 *
970 * <p>The initial value of |mid| in the returned transceiver is null. Setting a
971 * new session description may change it to a non-null value.
972 *
973 * <p>https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtransceiver
974 *
975 * <p>If a MediaStreamTrack is specified then a transceiver will be added with a
976 * sender set to transmit the given track. The kind
977 * of the transceiver (and sender/receiver) will be derived from the kind of
978 * the track.
979 *
980 * <p>If MediaType is specified then a transceiver will be added based upon that type.
981 * This can be either MEDIA_TYPE_AUDIO or MEDIA_TYPE_VIDEO.
982 *
983 * <p>Optionally, an RtpTransceiverInit structure can be specified to configure
984 * the transceiver from construction. If not specified, the transceiver will
985 * default to having a direction of kSendRecv and not be part of any streams.
986 *
987 * <p>Note: These methods are only available with SdpSemantics.UNIFIED_PLAN specified.
988 * @throws IllegalStateException if an error accors in C++ addTransceiver
989 */
990 public RtpTransceiver addTransceiver(MediaStreamTrack track) {
991 return addTransceiver(track, new RtpTransceiver.RtpTransceiverInit());
992 }
993
994 public RtpTransceiver addTransceiver(
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100995 MediaStreamTrack track, @Nullable RtpTransceiver.RtpTransceiverInit init) {
Seth Hampsonc384e142018-03-06 15:47:10 -0800996 if (track == null) {
997 throw new NullPointerException("No MediaStreamTrack specified for addTransceiver.");
998 }
999 if (init == null) {
1000 init = new RtpTransceiver.RtpTransceiverInit();
1001 }
Sami Kalliomäkiee05e902018-09-28 14:38:21 +02001002 RtpTransceiver newTransceiver =
1003 nativeAddTransceiverWithTrack(track.getNativeMediaStreamTrack(), init);
Seth Hampsonc384e142018-03-06 15:47:10 -08001004 if (newTransceiver == null) {
1005 throw new IllegalStateException("C++ addTransceiver failed.");
1006 }
1007 transceivers.add(newTransceiver);
1008 return newTransceiver;
1009 }
1010
1011 public RtpTransceiver addTransceiver(MediaStreamTrack.MediaType mediaType) {
1012 return addTransceiver(mediaType, new RtpTransceiver.RtpTransceiverInit());
1013 }
1014
1015 public RtpTransceiver addTransceiver(
Sami Kalliomäkie7592d82018-03-22 13:32:44 +01001016 MediaStreamTrack.MediaType mediaType, @Nullable RtpTransceiver.RtpTransceiverInit init) {
Seth Hampsonc384e142018-03-06 15:47:10 -08001017 if (mediaType == null) {
1018 throw new NullPointerException("No MediaType specified for addTransceiver.");
1019 }
1020 if (init == null) {
1021 init = new RtpTransceiver.RtpTransceiverInit();
1022 }
1023 RtpTransceiver newTransceiver = nativeAddTransceiverOfType(mediaType, init);
1024 if (newTransceiver == null) {
1025 throw new IllegalStateException("C++ addTransceiver failed.");
1026 }
1027 transceivers.add(newTransceiver);
1028 return newTransceiver;
1029 }
1030
deadbeef82215872017-04-18 10:27:51 -07001031 // Older, non-standard implementation of getStats.
1032 @Deprecated
Sami Kalliomäkie7592d82018-03-22 13:32:44 +01001033 public boolean getStats(StatsObserver observer, @Nullable MediaStreamTrack track) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +02001034 return nativeOldGetStats(observer, (track == null) ? 0 : track.getNativeMediaStreamTrack());
deadbeef82215872017-04-18 10:27:51 -07001035 }
1036
Seth Hampsonc384e142018-03-06 15:47:10 -08001037 /**
1038 * Gets stats using the new stats collection API, see webrtc/api/stats/. These
1039 * will replace old stats collection API when the new API has matured enough.
1040 */
deadbeef82215872017-04-18 10:27:51 -07001041 public void getStats(RTCStatsCollectorCallback callback) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001042 nativeNewGetStats(callback);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001043 }
1044
Seth Hampsonc384e142018-03-06 15:47:10 -08001045 /**
1046 * Limits the bandwidth allocated for all RTP streams sent by this
1047 * PeerConnection. Pass null to leave a value unchanged.
1048 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001049 public boolean setBitrate(Integer min, Integer current, Integer max) {
1050 return nativeSetBitrate(min, current, max);
1051 }
zsteind89b0bc2017-08-03 11:11:40 -07001052
Seth Hampsonc384e142018-03-06 15:47:10 -08001053 /**
1054 * Starts recording an RTC event log.
1055 *
1056 * Ownership of the file is transfered to the native code. If an RTC event
1057 * log is already being recorded, it will be stopped and a new one will start
1058 * using the provided file. Logging will continue until the stopRtcEventLog
1059 * function is called. The max_size_bytes argument is ignored, it is added
1060 * for future use.
1061 */
ivoc0c6f0f62016-07-06 04:34:23 -07001062 public boolean startRtcEventLog(int file_descriptor, int max_size_bytes) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001063 return nativeStartRtcEventLog(file_descriptor, max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -07001064 }
1065
Seth Hampsonc384e142018-03-06 15:47:10 -08001066 /**
1067 * Stops recording an RTC event log. If no RTC event log is currently being
1068 * recorded, this call will have no effect.
1069 */
ivoc14d5dbe2016-07-04 07:06:55 -07001070 public void stopRtcEventLog() {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001071 nativeStopRtcEventLog();
ivoc14d5dbe2016-07-04 07:06:55 -07001072 }
1073
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074 // TODO(fischman): add support for DTMF-related methods once that API
1075 // stabilizes.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001076 public SignalingState signalingState() {
1077 return nativeSignalingState();
1078 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001079
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001080 public IceConnectionState iceConnectionState() {
1081 return nativeIceConnectionState();
1082 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001083
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001084 public IceGatheringState iceGatheringState() {
1085 return nativeIceGatheringState();
1086 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001087
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001088 public void close() {
1089 nativeClose();
1090 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001091
deadbeef43697f62017-09-12 10:52:14 -07001092 /**
1093 * Free native resources associated with this PeerConnection instance.
Seth Hampsonc384e142018-03-06 15:47:10 -08001094 *
deadbeef43697f62017-09-12 10:52:14 -07001095 * This method removes a reference count from the C++ PeerConnection object,
1096 * which should result in it being destroyed. It also calls equivalent
1097 * "dispose" methods on the Java objects attached to this PeerConnection
1098 * (streams, senders, receivers), such that their associated C++ objects
1099 * will also be destroyed.
Seth Hampsonc384e142018-03-06 15:47:10 -08001100 *
1101 * <p>Note that this method cannot be safely called from an observer callback
deadbeef43697f62017-09-12 10:52:14 -07001102 * (PeerConnection.Observer, DataChannel.Observer, etc.). If you want to, for
1103 * example, destroy the PeerConnection after an "ICE failed" callback, you
1104 * must do this asynchronously (in other words, unwind the stack first). See
1105 * <a href="https://bugs.chromium.org/p/webrtc/issues/detail?id=3721">bug
1106 * 3721</a> for more details.
1107 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001108 public void dispose() {
1109 close();
1110 for (MediaStream stream : localStreams) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +02001111 nativeRemoveLocalStream(stream.getNativeMediaStream());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001112 stream.dispose();
1113 }
1114 localStreams.clear();
deadbeef4139c0f2015-10-06 12:29:25 -07001115 for (RtpSender sender : senders) {
1116 sender.dispose();
1117 }
1118 senders.clear();
1119 for (RtpReceiver receiver : receivers) {
1120 receiver.dispose();
1121 }
Seth Hampsonc384e142018-03-06 15:47:10 -08001122 for (RtpTransceiver transceiver : transceivers) {
1123 transceiver.dispose();
1124 }
1125 transceivers.clear();
deadbeef4139c0f2015-10-06 12:29:25 -07001126 receivers.clear();
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001127 nativeFreeOwnedPeerConnection(nativePeerConnection);
1128 }
1129
1130 /** Returns a pointer to the native webrtc::PeerConnectionInterface. */
1131 public long getNativePeerConnection() {
1132 return nativeGetNativePeerConnection();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133 }
1134
Magnus Jedvert9060eb12017-12-12 12:52:54 +01001135 @CalledByNative
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001136 long getNativeOwnedPeerConnection() {
Magnus Jedvert9060eb12017-12-12 12:52:54 +01001137 return nativePeerConnection;
1138 }
1139
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001140 public static long createNativePeerConnectionObserver(Observer observer) {
1141 return nativeCreatePeerConnectionObserver(observer);
1142 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001144 private native long nativeGetNativePeerConnection();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001145 private native SessionDescription nativeGetLocalDescription();
1146 private native SessionDescription nativeGetRemoteDescription();
Michael Iedema02137862018-10-09 15:30:01 +02001147 private native RtcCertificatePem nativeGetCertificate();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001148 private native DataChannel nativeCreateDataChannel(String label, DataChannel.Init init);
1149 private native void nativeCreateOffer(SdpObserver observer, MediaConstraints constraints);
1150 private native void nativeCreateAnswer(SdpObserver observer, MediaConstraints constraints);
1151 private native void nativeSetLocalDescription(SdpObserver observer, SessionDescription sdp);
1152 private native void nativeSetRemoteDescription(SdpObserver observer, SessionDescription sdp);
1153 private native void nativeSetAudioPlayout(boolean playout);
1154 private native void nativeSetAudioRecording(boolean recording);
1155 private native boolean nativeSetBitrate(Integer min, Integer current, Integer max);
1156 private native SignalingState nativeSignalingState();
1157 private native IceConnectionState nativeIceConnectionState();
1158 private native IceGatheringState nativeIceGatheringState();
1159 private native void nativeClose();
1160 private static native long nativeCreatePeerConnectionObserver(Observer observer);
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001161 private static native void nativeFreeOwnedPeerConnection(long ownedPeerConnection);
1162 private native boolean nativeSetConfiguration(RTCConfiguration config);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001163 private native boolean nativeAddIceCandidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001164 String sdpMid, int sdpMLineIndex, String iceCandidateSdp);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001165 private native boolean nativeRemoveIceCandidates(final IceCandidate[] candidates);
1166 private native boolean nativeAddLocalStream(long stream);
1167 private native void nativeRemoveLocalStream(long stream);
1168 private native boolean nativeOldGetStats(StatsObserver observer, long nativeTrack);
1169 private native void nativeNewGetStats(RTCStatsCollectorCallback callback);
1170 private native RtpSender nativeCreateSender(String kind, String stream_id);
1171 private native List<RtpSender> nativeGetSenders();
1172 private native List<RtpReceiver> nativeGetReceivers();
Seth Hampsonc384e142018-03-06 15:47:10 -08001173 private native List<RtpTransceiver> nativeGetTransceivers();
1174 private native RtpSender nativeAddTrack(long track, List<String> streamIds);
1175 private native boolean nativeRemoveTrack(long sender);
1176 private native RtpTransceiver nativeAddTransceiverWithTrack(
1177 long track, RtpTransceiver.RtpTransceiverInit init);
1178 private native RtpTransceiver nativeAddTransceiverOfType(
1179 MediaStreamTrack.MediaType mediaType, RtpTransceiver.RtpTransceiverInit init);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001180 private native boolean nativeStartRtcEventLog(int file_descriptor, int max_size_bytes);
1181 private native void nativeStopRtcEventLog();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182}