blob: 21617115753fd89c80353435d24e56237fd13b8e [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
Diogo Real4f085432018-09-11 16:00:22 -070053 // TODO(diogor, webrtc:9673): Remove TlsCertPolicy. It's deprecated, in favor of SslConfig.
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);
Seth Hampson31dbc242018-05-07 09:28:19 -0700112
113 /**
114 * Triggered when the signaling from SetRemoteDescription indicates that a transceiver
115 * will be receiving media from a remote endpoint. This is only called if UNIFIED_PLAN
116 * semantics are specified. The transceiver will be disposed automatically.
117 */
118 @CalledByNative("Observer") default void onTrack(RtpTransceiver transceiver){};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 }
120
121 /** Java version of PeerConnectionInterface.IceServer. */
122 public static class IceServer {
Emad Omaradab1d2d2017-06-16 15:43:11 -0700123 // List of URIs associated with this server. Valid formats are described
124 // in RFC7064 and RFC7065, and more may be added in the future. The "host"
125 // part of the URI may contain either an IP address or a hostname.
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700126 @Deprecated public final String uri;
127 public final List<String> urls;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 public final String username;
129 public final String password;
Diogo Real4f085432018-09-11 16:00:22 -0700130 // TODO(diogor, webrtc:9673): Remove tlsCertPolicy from this API.
131 // This field will be ignored if tlsCertPolicy is also set in SslConfig.
132 @Deprecated public final TlsCertPolicy tlsCertPolicy;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133
Emad Omaradab1d2d2017-06-16 15:43:11 -0700134 // If the URIs in |urls| only contain IP addresses, this field can be used
135 // to indicate the hostname, which may be necessary for TLS (using the SNI
136 // extension). If |urls| itself contains the hostname, this isn't
137 // necessary.
138 public final String hostname;
139
Diogo Real4f085432018-09-11 16:00:22 -0700140 // TODO(diogor, webrtc:9673): Remove tlsAlpnProtocols from this API.
Diogo Real1dca9d52017-08-29 12:18:32 -0700141 // List of protocols to be used in the TLS ALPN extension.
Diogo Real4f085432018-09-11 16:00:22 -0700142 @Deprecated public final List<String> tlsAlpnProtocols;
Diogo Real1dca9d52017-08-29 12:18:32 -0700143
Diogo Real4f085432018-09-11 16:00:22 -0700144 // TODO(diogor, webrtc:9673): Remove tlsEllipticCurves from this API.
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700145 // List of elliptic curves to be used in the TLS elliptic curves extension.
146 // Only curve names supported by OpenSSL should be used (eg. "P-256","X25519").
Diogo Real4f085432018-09-11 16:00:22 -0700147 // This field will be ignored if tlsEllipticCurves is also set in SslConfig.
148 @Deprecated public final List<String> tlsEllipticCurves;
149
150 // SSL configuration options for any SSL/TLS connections to this IceServer.
151 public final SslConfig sslConfig;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700152
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 /** Convenience constructor for STUN servers. */
Diogo Real05ea2b32017-08-31 00:12:58 -0700154 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 public IceServer(String uri) {
156 this(uri, "", "");
157 }
158
Diogo Real05ea2b32017-08-31 00:12:58 -0700159 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 public IceServer(String uri, String username, String password) {
hnsl04833622017-01-09 08:35:45 -0800161 this(uri, username, password, TlsCertPolicy.TLS_CERT_POLICY_SECURE);
162 }
163
Diogo Real05ea2b32017-08-31 00:12:58 -0700164 @Deprecated
hnsl04833622017-01-09 08:35:45 -0800165 public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy) {
Emad Omaradab1d2d2017-06-16 15:43:11 -0700166 this(uri, username, password, tlsCertPolicy, "");
167 }
168
Diogo Real05ea2b32017-08-31 00:12:58 -0700169 @Deprecated
Emad Omaradab1d2d2017-06-16 15:43:11 -0700170 public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy,
171 String hostname) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700172 this(uri, Collections.singletonList(uri), username, password, tlsCertPolicy, hostname, null,
Diogo Real4f085432018-09-11 16:00:22 -0700173 null, SslConfig.builder().createSslConfig());
Diogo Real1dca9d52017-08-29 12:18:32 -0700174 }
175
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700176 private IceServer(String uri, List<String> urls, String username, String password,
177 TlsCertPolicy tlsCertPolicy, String hostname, List<String> tlsAlpnProtocols,
Diogo Real4f085432018-09-11 16:00:22 -0700178 List<String> tlsEllipticCurves, SslConfig sslConfig) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700179 if (uri == null || urls == null || urls.isEmpty()) {
180 throw new IllegalArgumentException("uri == null || urls == null || urls.isEmpty()");
181 }
182 for (String it : urls) {
183 if (it == null) {
184 throw new IllegalArgumentException("urls element is null: " + urls);
185 }
186 }
187 if (username == null) {
188 throw new IllegalArgumentException("username == null");
189 }
190 if (password == null) {
191 throw new IllegalArgumentException("password == null");
192 }
193 if (hostname == null) {
194 throw new IllegalArgumentException("hostname == null");
195 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 this.uri = uri;
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700197 this.urls = urls;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 this.username = username;
199 this.password = password;
hnsl04833622017-01-09 08:35:45 -0800200 this.tlsCertPolicy = tlsCertPolicy;
Emad Omaradab1d2d2017-06-16 15:43:11 -0700201 this.hostname = hostname;
Diogo Real1dca9d52017-08-29 12:18:32 -0700202 this.tlsAlpnProtocols = tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700203 this.tlsEllipticCurves = tlsEllipticCurves;
Diogo Real4f085432018-09-11 16:00:22 -0700204 this.sslConfig = sslConfig;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 }
206
Sami Kalliomäkibde473e2017-10-30 13:34:41 +0100207 @Override
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 public String toString() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700209 return urls + " [" + username + ":" + password + "] [" + tlsCertPolicy + "] [" + hostname
Diogo Real4f085432018-09-11 16:00:22 -0700210 + "] [" + tlsAlpnProtocols + "] [" + tlsEllipticCurves + "] [" + sslConfig + "]";
Diogo Real1dca9d52017-08-29 12:18:32 -0700211 }
212
213 public static Builder builder(String uri) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700214 return new Builder(Collections.singletonList(uri));
215 }
216
217 public static Builder builder(List<String> urls) {
218 return new Builder(urls);
Diogo Real1dca9d52017-08-29 12:18:32 -0700219 }
220
221 public static class Builder {
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100222 @Nullable private final List<String> urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700223 private String username = "";
224 private String password = "";
225 private TlsCertPolicy tlsCertPolicy = TlsCertPolicy.TLS_CERT_POLICY_SECURE;
226 private String hostname = "";
227 private List<String> tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700228 private List<String> tlsEllipticCurves;
Diogo Real4f085432018-09-11 16:00:22 -0700229 private SslConfig sslConfig = SslConfig.builder().createSslConfig();
Diogo Real1dca9d52017-08-29 12:18:32 -0700230
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700231 private Builder(List<String> urls) {
232 if (urls == null || urls.isEmpty()) {
233 throw new IllegalArgumentException("urls == null || urls.isEmpty(): " + urls);
234 }
235 this.urls = urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700236 }
237
238 public Builder setUsername(String username) {
239 this.username = username;
240 return this;
241 }
242
243 public Builder setPassword(String password) {
244 this.password = password;
245 return this;
246 }
247
Diogo Real4f085432018-09-11 16:00:22 -0700248 @Deprecated
Diogo Real1dca9d52017-08-29 12:18:32 -0700249 public Builder setTlsCertPolicy(TlsCertPolicy tlsCertPolicy) {
250 this.tlsCertPolicy = tlsCertPolicy;
251 return this;
252 }
253
254 public Builder setHostname(String hostname) {
255 this.hostname = hostname;
256 return this;
257 }
258
Diogo Real4f085432018-09-11 16:00:22 -0700259 @Deprecated
Diogo Real1dca9d52017-08-29 12:18:32 -0700260 public Builder setTlsAlpnProtocols(List<String> tlsAlpnProtocols) {
261 this.tlsAlpnProtocols = tlsAlpnProtocols;
262 return this;
263 }
264
Diogo Real4f085432018-09-11 16:00:22 -0700265 @Deprecated
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700266 public Builder setTlsEllipticCurves(List<String> tlsEllipticCurves) {
267 this.tlsEllipticCurves = tlsEllipticCurves;
268 return this;
269 }
270
Diogo Real4f085432018-09-11 16:00:22 -0700271 public Builder setSslConfig(SslConfig sslConfig) {
272 this.sslConfig = sslConfig;
273 return this;
274 }
275
Diogo Real1dca9d52017-08-29 12:18:32 -0700276 public IceServer createIceServer() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700277 return new IceServer(urls.get(0), urls, username, password, tlsCertPolicy, hostname,
Diogo Real4f085432018-09-11 16:00:22 -0700278 tlsAlpnProtocols, tlsEllipticCurves, sslConfig);
Diogo Real1dca9d52017-08-29 12:18:32 -0700279 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 }
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100281
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100282 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100283 @CalledByNative("IceServer")
284 List<String> getUrls() {
285 return urls;
286 }
287
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100288 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100289 @CalledByNative("IceServer")
290 String getUsername() {
291 return username;
292 }
293
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100294 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100295 @CalledByNative("IceServer")
296 String getPassword() {
297 return password;
298 }
299
300 @CalledByNative("IceServer")
301 TlsCertPolicy getTlsCertPolicy() {
302 return tlsCertPolicy;
303 }
304
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100305 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100306 @CalledByNative("IceServer")
307 String getHostname() {
308 return hostname;
309 }
310
311 @CalledByNative("IceServer")
312 List<String> getTlsAlpnProtocols() {
313 return tlsAlpnProtocols;
314 }
315
316 @CalledByNative("IceServer")
317 List<String> getTlsEllipticCurves() {
318 return tlsEllipticCurves;
319 }
Diogo Real4f085432018-09-11 16:00:22 -0700320
321 @CalledByNative("IceServer")
322 SslConfig getSslConfig() {
323 return sslConfig;
324 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325 }
326
Jiayang Liucac1b382015-04-30 12:35:24 -0700327 /** Java version of PeerConnectionInterface.IceTransportsType */
sakalb6760f92016-09-29 04:12:44 -0700328 public enum IceTransportsType { NONE, RELAY, NOHOST, ALL }
Jiayang Liucac1b382015-04-30 12:35:24 -0700329
330 /** Java version of PeerConnectionInterface.BundlePolicy */
sakalb6760f92016-09-29 04:12:44 -0700331 public enum BundlePolicy { BALANCED, MAXBUNDLE, MAXCOMPAT }
Jiayang Liucac1b382015-04-30 12:35:24 -0700332
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700333 /** Java version of PeerConnectionInterface.RtcpMuxPolicy */
sakalb6760f92016-09-29 04:12:44 -0700334 public enum RtcpMuxPolicy { NEGOTIATE, REQUIRE }
glaznev97579a42015-09-01 11:31:27 -0700335
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700336 /** Java version of PeerConnectionInterface.TcpCandidatePolicy */
sakalb6760f92016-09-29 04:12:44 -0700337 public enum TcpCandidatePolicy { ENABLED, DISABLED }
Jiayang Liucac1b382015-04-30 12:35:24 -0700338
honghaiz60347052016-05-31 18:29:12 -0700339 /** Java version of PeerConnectionInterface.CandidateNetworkPolicy */
sakalb6760f92016-09-29 04:12:44 -0700340 public enum CandidateNetworkPolicy { ALL, LOW_COST }
honghaiz60347052016-05-31 18:29:12 -0700341
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800342 // Keep in sync with webrtc/rtc_base/network_constants.h.
343 public enum AdapterType {
344 UNKNOWN,
345 ETHERNET,
346 WIFI,
347 CELLULAR,
348 VPN,
349 LOOPBACK,
350 }
351
glaznev97579a42015-09-01 11:31:27 -0700352 /** Java version of rtc::KeyType */
sakalb6760f92016-09-29 04:12:44 -0700353 public enum KeyType { RSA, ECDSA }
glaznev97579a42015-09-01 11:31:27 -0700354
honghaiz1f429e32015-09-28 07:57:34 -0700355 /** Java version of PeerConnectionInterface.ContinualGatheringPolicy */
sakalb6760f92016-09-29 04:12:44 -0700356 public enum ContinualGatheringPolicy { GATHER_ONCE, GATHER_CONTINUALLY }
honghaiz1f429e32015-09-28 07:57:34 -0700357
Steve Antond960a0c2017-07-17 12:33:07 -0700358 /** Java version of rtc::IntervalRange */
359 public static class IntervalRange {
360 private final int min;
361 private final int max;
362
363 public IntervalRange(int min, int max) {
364 this.min = min;
365 this.max = max;
366 }
367
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100368 @CalledByNative("IntervalRange")
Steve Antond960a0c2017-07-17 12:33:07 -0700369 public int getMin() {
370 return min;
371 }
372
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100373 @CalledByNative("IntervalRange")
Steve Antond960a0c2017-07-17 12:33:07 -0700374 public int getMax() {
375 return max;
376 }
377 }
378
Seth Hampsonc384e142018-03-06 15:47:10 -0800379 /**
380 * Java version of webrtc::SdpSemantics.
381 *
382 * Configure the SDP semantics used by this PeerConnection. Note that the
383 * WebRTC 1.0 specification requires UNIFIED_PLAN semantics. The
384 * RtpTransceiver API is only available with UNIFIED_PLAN semantics.
385 *
386 * <p>PLAN_B will cause PeerConnection to create offers and answers with at
387 * most one audio and one video m= section with multiple RtpSenders and
388 * RtpReceivers specified as multiple a=ssrc lines within the section. This
389 * will also cause PeerConnection to ignore all but the first m= section of
390 * the same media type.
391 *
392 * <p>UNIFIED_PLAN will cause PeerConnection to create offers and answers with
393 * multiple m= sections where each m= section maps to one RtpSender and one
394 * RtpReceiver (an RtpTransceiver), either both audio or both video. This
395 * will also cause PeerConnection to ignore all but the first a=ssrc lines
396 * that form a Plan B stream.
397 *
398 * <p>For users who wish to send multiple audio/video streams and need to stay
399 * interoperable with legacy WebRTC implementations, specify PLAN_B.
400 *
401 * <p>For users who wish to send multiple audio/video streams and/or wish to
402 * use the new RtpTransceiver API, specify UNIFIED_PLAN.
403 */
404 public enum SdpSemantics { PLAN_B, UNIFIED_PLAN }
405
Jiayang Liucac1b382015-04-30 12:35:24 -0700406 /** Java version of PeerConnectionInterface.RTCConfiguration */
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800407 // TODO(qingsi): Resolve the naming inconsistency of fields with/without units.
Jiayang Liucac1b382015-04-30 12:35:24 -0700408 public static class RTCConfiguration {
409 public IceTransportsType iceTransportsType;
410 public List<IceServer> iceServers;
411 public BundlePolicy bundlePolicy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700412 public RtcpMuxPolicy rtcpMuxPolicy;
Jiayang Liucac1b382015-04-30 12:35:24 -0700413 public TcpCandidatePolicy tcpCandidatePolicy;
honghaiz60347052016-05-31 18:29:12 -0700414 public CandidateNetworkPolicy candidateNetworkPolicy;
Henrik Lundin64dad832015-05-11 12:44:23 +0200415 public int audioJitterBufferMaxPackets;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200416 public boolean audioJitterBufferFastAccelerate;
honghaiz4edc39c2015-09-01 09:53:56 -0700417 public int iceConnectionReceivingTimeout;
Honghai Zhang381b4212015-12-04 12:24:03 -0800418 public int iceBackupCandidatePairPingInterval;
glaznev97579a42015-09-01 11:31:27 -0700419 public KeyType keyType;
honghaiz1f429e32015-09-28 07:57:34 -0700420 public ContinualGatheringPolicy continualGatheringPolicy;
deadbeefbe0c96f2016-05-18 16:20:14 -0700421 public int iceCandidatePoolSize;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700422 public boolean pruneTurnPorts;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700423 public boolean presumeWritableWhenFullyRelayed;
Qingsi Wange6826d22018-03-08 14:55:14 -0800424 // The following fields define intervals in milliseconds at which ICE
425 // connectivity checks are sent.
426 //
427 // We consider ICE is "strongly connected" for an agent when there is at
428 // least one candidate pair that currently succeeds in connectivity check
429 // from its direction i.e. sending a ping and receives a ping response, AND
430 // all candidate pairs have sent a minimum number of pings for connectivity
431 // (this number is implementation-specific). Otherwise, ICE is considered in
432 // "weak connectivity".
433 //
434 // Note that the above notion of strong and weak connectivity is not defined
435 // in RFC 5245, and they apply to our current ICE implementation only.
436 //
437 // 1) iceCheckIntervalStrongConnectivityMs defines the interval applied to
438 // ALL candidate pairs when ICE is strongly connected,
439 // 2) iceCheckIntervalWeakConnectivityMs defines the counterpart for ALL
440 // pairs when ICE is weakly connected, and
441 // 3) iceCheckMinInterval defines the minimal interval (equivalently the
442 // maximum rate) that overrides the above two intervals when either of them
443 // is less.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100444 @Nullable public Integer iceCheckIntervalStrongConnectivityMs;
445 @Nullable public Integer iceCheckIntervalWeakConnectivityMs;
446 @Nullable public Integer iceCheckMinInterval;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700447 // The time period in milliseconds for which a candidate pair must wait for response to
448 // connectivitiy checks before it becomes unwritable.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100449 @Nullable public Integer iceUnwritableTimeMs;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700450 // The minimum number of connectivity checks that a candidate pair must sent without receiving
451 // response before it becomes unwritable.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100452 @Nullable public Integer iceUnwritableMinChecks;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800453 // The interval in milliseconds at which STUN candidates will resend STUN binding requests
454 // to keep NAT bindings open.
455 // The default value in the implementation is used if this field is null.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100456 @Nullable public Integer stunCandidateKeepaliveIntervalMs;
zhihuangb09b3f92017-03-07 14:40:51 -0800457 public boolean disableIPv6OnWifi;
deadbeef28e29192017-07-27 09:14:38 -0700458 // By default, PeerConnection will use a limited number of IPv6 network
459 // interfaces, in order to avoid too many ICE candidate pairs being created
460 // and delaying ICE completion.
461 //
462 // Can be set to Integer.MAX_VALUE to effectively disable the limit.
463 public int maxIPv6Networks;
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100464 @Nullable public IntervalRange iceRegatherIntervalRange;
Jiayang Liucac1b382015-04-30 12:35:24 -0700465
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100466 // These values will be overridden by MediaStream constraints if deprecated constraints-based
467 // create peerconnection interface is used.
468 public boolean disableIpv6;
469 public boolean enableDscp;
470 public boolean enableCpuOveruseDetection;
471 public boolean enableRtpDataChannel;
472 public boolean suspendBelowMinBitrate;
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100473 @Nullable public Integer screencastMinBitrate;
474 @Nullable public Boolean combinedAudioVideoBwe;
475 @Nullable public Boolean enableDtlsSrtp;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800476 // Use "Unknown" to represent no preference of adapter types, not the
477 // preference of adapters of unknown types.
478 public AdapterType networkPreference;
Seth Hampsonc384e142018-03-06 15:47:10 -0800479 public SdpSemantics sdpSemantics;
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100480
Jonas Orelandbdcee282017-10-10 14:01:40 +0200481 // This is an optional wrapper for the C++ webrtc::TurnCustomizer.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100482 @Nullable public TurnCustomizer turnCustomizer;
Jonas Orelandbdcee282017-10-10 14:01:40 +0200483
Zhi Huangb57e1692018-06-12 11:41:11 -0700484 // Actively reset the SRTP parameters whenever the DTLS transports underneath are reset for
485 // every offer/answer negotiation.This is only intended to be a workaround for crbug.com/835958
486 public boolean activeResetSrtpParams;
487
deadbeef28e29192017-07-27 09:14:38 -0700488 // TODO(deadbeef): Instead of duplicating the defaults here, we should do
489 // something to pick up the defaults from C++. The Objective-C equivalent
490 // of RTCConfiguration does that.
Jiayang Liucac1b382015-04-30 12:35:24 -0700491 public RTCConfiguration(List<IceServer> iceServers) {
492 iceTransportsType = IceTransportsType.ALL;
493 bundlePolicy = BundlePolicy.BALANCED;
zhihuang4dfb8ce2016-11-23 10:30:12 -0800494 rtcpMuxPolicy = RtcpMuxPolicy.REQUIRE;
Jiayang Liucac1b382015-04-30 12:35:24 -0700495 tcpCandidatePolicy = TcpCandidatePolicy.ENABLED;
Sami Kalliomäki9828beb2017-10-26 16:21:22 +0200496 candidateNetworkPolicy = CandidateNetworkPolicy.ALL;
Jiayang Liucac1b382015-04-30 12:35:24 -0700497 this.iceServers = iceServers;
Henrik Lundin64dad832015-05-11 12:44:23 +0200498 audioJitterBufferMaxPackets = 50;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200499 audioJitterBufferFastAccelerate = false;
honghaiz4edc39c2015-09-01 09:53:56 -0700500 iceConnectionReceivingTimeout = -1;
Honghai Zhang381b4212015-12-04 12:24:03 -0800501 iceBackupCandidatePairPingInterval = -1;
glaznev97579a42015-09-01 11:31:27 -0700502 keyType = KeyType.ECDSA;
honghaiz1f429e32015-09-28 07:57:34 -0700503 continualGatheringPolicy = ContinualGatheringPolicy.GATHER_ONCE;
deadbeefbe0c96f2016-05-18 16:20:14 -0700504 iceCandidatePoolSize = 0;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700505 pruneTurnPorts = false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700506 presumeWritableWhenFullyRelayed = false;
Qingsi Wange6826d22018-03-08 14:55:14 -0800507 iceCheckIntervalStrongConnectivityMs = null;
508 iceCheckIntervalWeakConnectivityMs = null;
skvlad51072462017-02-02 11:50:14 -0800509 iceCheckMinInterval = null;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700510 iceUnwritableTimeMs = null;
511 iceUnwritableMinChecks = null;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800512 stunCandidateKeepaliveIntervalMs = null;
zhihuangb09b3f92017-03-07 14:40:51 -0800513 disableIPv6OnWifi = false;
deadbeef28e29192017-07-27 09:14:38 -0700514 maxIPv6Networks = 5;
Steve Antond960a0c2017-07-17 12:33:07 -0700515 iceRegatherIntervalRange = null;
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100516 disableIpv6 = false;
517 enableDscp = false;
518 enableCpuOveruseDetection = true;
519 enableRtpDataChannel = false;
520 suspendBelowMinBitrate = false;
521 screencastMinBitrate = null;
522 combinedAudioVideoBwe = null;
523 enableDtlsSrtp = null;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800524 networkPreference = AdapterType.UNKNOWN;
Seth Hampsonc384e142018-03-06 15:47:10 -0800525 sdpSemantics = SdpSemantics.PLAN_B;
Zhi Huangb57e1692018-06-12 11:41:11 -0700526 activeResetSrtpParams = false;
Jiayang Liucac1b382015-04-30 12:35:24 -0700527 }
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100528
529 @CalledByNative("RTCConfiguration")
530 IceTransportsType getIceTransportsType() {
531 return iceTransportsType;
532 }
533
534 @CalledByNative("RTCConfiguration")
535 List<IceServer> getIceServers() {
536 return iceServers;
537 }
538
539 @CalledByNative("RTCConfiguration")
540 BundlePolicy getBundlePolicy() {
541 return bundlePolicy;
542 }
543
544 @CalledByNative("RTCConfiguration")
545 RtcpMuxPolicy getRtcpMuxPolicy() {
546 return rtcpMuxPolicy;
547 }
548
549 @CalledByNative("RTCConfiguration")
550 TcpCandidatePolicy getTcpCandidatePolicy() {
551 return tcpCandidatePolicy;
552 }
553
554 @CalledByNative("RTCConfiguration")
555 CandidateNetworkPolicy getCandidateNetworkPolicy() {
556 return candidateNetworkPolicy;
557 }
558
559 @CalledByNative("RTCConfiguration")
560 int getAudioJitterBufferMaxPackets() {
561 return audioJitterBufferMaxPackets;
562 }
563
564 @CalledByNative("RTCConfiguration")
565 boolean getAudioJitterBufferFastAccelerate() {
566 return audioJitterBufferFastAccelerate;
567 }
568
569 @CalledByNative("RTCConfiguration")
570 int getIceConnectionReceivingTimeout() {
571 return iceConnectionReceivingTimeout;
572 }
573
574 @CalledByNative("RTCConfiguration")
575 int getIceBackupCandidatePairPingInterval() {
576 return iceBackupCandidatePairPingInterval;
577 }
578
579 @CalledByNative("RTCConfiguration")
580 KeyType getKeyType() {
581 return keyType;
582 }
583
584 @CalledByNative("RTCConfiguration")
585 ContinualGatheringPolicy getContinualGatheringPolicy() {
586 return continualGatheringPolicy;
587 }
588
589 @CalledByNative("RTCConfiguration")
590 int getIceCandidatePoolSize() {
591 return iceCandidatePoolSize;
592 }
593
594 @CalledByNative("RTCConfiguration")
595 boolean getPruneTurnPorts() {
596 return pruneTurnPorts;
597 }
598
599 @CalledByNative("RTCConfiguration")
600 boolean getPresumeWritableWhenFullyRelayed() {
601 return presumeWritableWhenFullyRelayed;
602 }
603
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100604 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100605 @CalledByNative("RTCConfiguration")
Qingsi Wange6826d22018-03-08 14:55:14 -0800606 Integer getIceCheckIntervalStrongConnectivity() {
607 return iceCheckIntervalStrongConnectivityMs;
608 }
609
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100610 @Nullable
Qingsi Wange6826d22018-03-08 14:55:14 -0800611 @CalledByNative("RTCConfiguration")
612 Integer getIceCheckIntervalWeakConnectivity() {
613 return iceCheckIntervalWeakConnectivityMs;
614 }
615
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100616 @Nullable
Qingsi Wange6826d22018-03-08 14:55:14 -0800617 @CalledByNative("RTCConfiguration")
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100618 Integer getIceCheckMinInterval() {
619 return iceCheckMinInterval;
620 }
621
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100622 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100623 @CalledByNative("RTCConfiguration")
Qingsi Wang22e623a2018-03-13 10:53:57 -0700624 Integer getIceUnwritableTimeout() {
625 return iceUnwritableTimeMs;
626 }
627
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100628 @Nullable
Qingsi Wang22e623a2018-03-13 10:53:57 -0700629 @CalledByNative("RTCConfiguration")
630 Integer getIceUnwritableMinChecks() {
631 return iceUnwritableMinChecks;
632 }
633
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100634 @Nullable
Qingsi Wang22e623a2018-03-13 10:53:57 -0700635 @CalledByNative("RTCConfiguration")
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800636 Integer getStunCandidateKeepaliveInterval() {
637 return stunCandidateKeepaliveIntervalMs;
638 }
639
640 @CalledByNative("RTCConfiguration")
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100641 boolean getDisableIPv6OnWifi() {
642 return disableIPv6OnWifi;
643 }
644
645 @CalledByNative("RTCConfiguration")
646 int getMaxIPv6Networks() {
647 return maxIPv6Networks;
648 }
649
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100650 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100651 @CalledByNative("RTCConfiguration")
652 IntervalRange getIceRegatherIntervalRange() {
653 return iceRegatherIntervalRange;
654 }
655
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100656 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100657 @CalledByNative("RTCConfiguration")
658 TurnCustomizer getTurnCustomizer() {
659 return turnCustomizer;
660 }
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100661
662 @CalledByNative("RTCConfiguration")
663 boolean getDisableIpv6() {
664 return disableIpv6;
665 }
666
667 @CalledByNative("RTCConfiguration")
668 boolean getEnableDscp() {
669 return enableDscp;
670 }
671
672 @CalledByNative("RTCConfiguration")
673 boolean getEnableCpuOveruseDetection() {
674 return enableCpuOveruseDetection;
675 }
676
677 @CalledByNative("RTCConfiguration")
678 boolean getEnableRtpDataChannel() {
679 return enableRtpDataChannel;
680 }
681
682 @CalledByNative("RTCConfiguration")
683 boolean getSuspendBelowMinBitrate() {
684 return suspendBelowMinBitrate;
685 }
686
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100687 @Nullable
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100688 @CalledByNative("RTCConfiguration")
689 Integer getScreencastMinBitrate() {
690 return screencastMinBitrate;
691 }
692
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100693 @Nullable
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100694 @CalledByNative("RTCConfiguration")
695 Boolean getCombinedAudioVideoBwe() {
696 return combinedAudioVideoBwe;
697 }
698
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100699 @Nullable
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100700 @CalledByNative("RTCConfiguration")
701 Boolean getEnableDtlsSrtp() {
702 return enableDtlsSrtp;
703 }
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800704
705 @CalledByNative("RTCConfiguration")
706 AdapterType getNetworkPreference() {
707 return networkPreference;
708 }
Seth Hampsonc384e142018-03-06 15:47:10 -0800709
710 @CalledByNative("RTCConfiguration")
711 SdpSemantics getSdpSemantics() {
712 return sdpSemantics;
713 }
Zhi Huangb57e1692018-06-12 11:41:11 -0700714
715 @CalledByNative("RTCConfiguration")
716 boolean getActiveResetSrtpParams() {
717 return activeResetSrtpParams;
718 }
Jiayang Liucac1b382015-04-30 12:35:24 -0700719 };
720
Magnus Jedvert6062f372017-11-16 16:53:12 +0100721 private final List<MediaStream> localStreams = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 private final long nativePeerConnection;
Magnus Jedvert6062f372017-11-16 16:53:12 +0100723 private List<RtpSender> senders = new ArrayList<>();
724 private List<RtpReceiver> receivers = new ArrayList<>();
Seth Hampsonc384e142018-03-06 15:47:10 -0800725 private List<RtpTransceiver> transceivers = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726
Sami Kalliomäki1ece1ed2017-12-20 11:59:22 +0100727 /**
728 * Wraps a PeerConnection created by the factory. Can be used by clients that want to implement
729 * their PeerConnection creation in JNI.
730 */
731 public PeerConnection(NativePeerConnectionFactory factory) {
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100732 this(factory.createNativePeerConnection());
Sami Kalliomäki1ece1ed2017-12-20 11:59:22 +0100733 }
734
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100735 PeerConnection(long nativePeerConnection) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000736 this.nativePeerConnection = nativePeerConnection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 }
738
739 // JsepInterface.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100740 public SessionDescription getLocalDescription() {
741 return nativeGetLocalDescription();
742 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100744 public SessionDescription getRemoteDescription() {
745 return nativeGetRemoteDescription();
746 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100748 public DataChannel createDataChannel(String label, DataChannel.Init init) {
749 return nativeCreateDataChannel(label, init);
750 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000751
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100752 public void createOffer(SdpObserver observer, MediaConstraints constraints) {
753 nativeCreateOffer(observer, constraints);
754 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100756 public void createAnswer(SdpObserver observer, MediaConstraints constraints) {
757 nativeCreateAnswer(observer, constraints);
758 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100760 public void setLocalDescription(SdpObserver observer, SessionDescription sdp) {
761 nativeSetLocalDescription(observer, sdp);
762 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100764 public void setRemoteDescription(SdpObserver observer, SessionDescription sdp) {
765 nativeSetRemoteDescription(observer, sdp);
766 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767
Seth Hampsonc384e142018-03-06 15:47:10 -0800768 /**
769 * Enables/disables playout of received audio streams. Enabled by default.
770 *
771 * Note that even if playout is enabled, streams will only be played out if
772 * the appropriate SDP is also applied. The main purpose of this API is to
773 * be able to control the exact time when audio playout starts.
774 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100775 public void setAudioPlayout(boolean playout) {
776 nativeSetAudioPlayout(playout);
777 }
henrika5f6bf242017-11-01 11:06:56 +0100778
Seth Hampsonc384e142018-03-06 15:47:10 -0800779 /**
780 * Enables/disables recording of transmitted audio streams. Enabled by default.
781 *
782 * Note that even if recording is enabled, streams will only be recorded if
783 * the appropriate SDP is also applied. The main purpose of this API is to
784 * be able to control the exact time when audio recording starts.
785 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100786 public void setAudioRecording(boolean recording) {
787 nativeSetAudioRecording(recording);
788 }
henrika5f6bf242017-11-01 11:06:56 +0100789
deadbeef5d0b6d82017-01-09 16:05:28 -0800790 public boolean setConfiguration(RTCConfiguration config) {
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100791 return nativeSetConfiguration(config);
deadbeef5d0b6d82017-01-09 16:05:28 -0800792 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793
794 public boolean addIceCandidate(IceCandidate candidate) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100795 return nativeAddIceCandidate(candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 }
797
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700798 public boolean removeIceCandidates(final IceCandidate[] candidates) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100799 return nativeRemoveIceCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700800 }
801
Seth Hampsonc384e142018-03-06 15:47:10 -0800802 /**
803 * Adds a new MediaStream to be sent on this peer connection.
804 * Note: This method is not supported with SdpSemantics.UNIFIED_PLAN. Please
805 * use addTrack instead.
806 */
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000807 public boolean addStream(MediaStream stream) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100808 boolean ret = nativeAddLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 if (!ret) {
810 return false;
811 }
812 localStreams.add(stream);
813 return true;
814 }
815
Seth Hampsonc384e142018-03-06 15:47:10 -0800816 /**
817 * Removes the given media stream from this peer connection.
818 * This method is not supported with SdpSemantics.UNIFIED_PLAN. Please use
819 * removeTrack instead.
820 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 public void removeStream(MediaStream stream) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100822 nativeRemoveLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 localStreams.remove(stream);
824 }
825
deadbeef7a246882017-08-09 08:40:10 -0700826 /**
827 * Creates an RtpSender without a track.
Seth Hampsonc384e142018-03-06 15:47:10 -0800828 *
829 * <p>This method allows an application to cause the PeerConnection to negotiate
deadbeef7a246882017-08-09 08:40:10 -0700830 * sending/receiving a specific media type, but without having a track to
831 * send yet.
Seth Hampsonc384e142018-03-06 15:47:10 -0800832 *
833 * <p>When the application does want to begin sending a track, it can call
deadbeef7a246882017-08-09 08:40:10 -0700834 * RtpSender.setTrack, which doesn't require any additional SDP negotiation.
Seth Hampsonc384e142018-03-06 15:47:10 -0800835 *
836 * <p>Example use:
deadbeef7a246882017-08-09 08:40:10 -0700837 * <pre>
838 * {@code
839 * audioSender = pc.createSender("audio", "stream1");
840 * videoSender = pc.createSender("video", "stream1");
841 * // Do normal SDP offer/answer, which will kick off ICE/DTLS and negotiate
842 * // media parameters....
843 * // Later, when the endpoint is ready to actually begin sending:
844 * audioSender.setTrack(audioTrack, false);
845 * videoSender.setTrack(videoTrack, false);
846 * }
847 * </pre>
Seth Hampsonc384e142018-03-06 15:47:10 -0800848 * <p>Note: This corresponds most closely to "addTransceiver" in the official
deadbeef7a246882017-08-09 08:40:10 -0700849 * WebRTC API, in that it creates a sender without a track. It was
850 * implemented before addTransceiver because it provides useful
851 * functionality, and properly implementing transceivers would have required
852 * a great deal more work.
853 *
Seth Hampsonc384e142018-03-06 15:47:10 -0800854 * <p>Note: This is only available with SdpSemantics.PLAN_B specified. Please use
855 * addTransceiver instead.
856 *
deadbeef7a246882017-08-09 08:40:10 -0700857 * @param kind Corresponds to MediaStreamTrack kinds (must be "audio" or
858 * "video").
859 * @param stream_id The ID of the MediaStream that this sender's track will
860 * be associated with when SDP is applied to the remote
861 * PeerConnection. If createSender is used to create an
862 * audio and video sender that should be synchronized, they
863 * should use the same stream ID.
864 * @return A new RtpSender object if successful, or null otherwise.
865 */
deadbeefbd7d8f72015-12-18 16:58:44 -0800866 public RtpSender createSender(String kind, String stream_id) {
Seth Hampsonc384e142018-03-06 15:47:10 -0800867 RtpSender newSender = nativeCreateSender(kind, stream_id);
868 if (newSender != null) {
869 senders.add(newSender);
deadbeefee524f72015-12-02 11:27:40 -0800870 }
Seth Hampsonc384e142018-03-06 15:47:10 -0800871 return newSender;
deadbeefee524f72015-12-02 11:27:40 -0800872 }
873
Seth Hampsonc384e142018-03-06 15:47:10 -0800874 /**
875 * Gets all RtpSenders associated with this peer connection.
876 * Note that calling getSenders will dispose of the senders previously
877 * returned.
878 */
deadbeef4139c0f2015-10-06 12:29:25 -0700879 public List<RtpSender> getSenders() {
880 for (RtpSender sender : senders) {
881 sender.dispose();
882 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100883 senders = nativeGetSenders();
deadbeef4139c0f2015-10-06 12:29:25 -0700884 return Collections.unmodifiableList(senders);
885 }
886
Seth Hampsonc384e142018-03-06 15:47:10 -0800887 /**
888 * Gets all RtpReceivers associated with this peer connection.
889 * Note that calling getReceivers will dispose of the receivers previously
890 * returned.
891 */
deadbeef4139c0f2015-10-06 12:29:25 -0700892 public List<RtpReceiver> getReceivers() {
893 for (RtpReceiver receiver : receivers) {
894 receiver.dispose();
895 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100896 receivers = nativeGetReceivers();
deadbeef4139c0f2015-10-06 12:29:25 -0700897 return Collections.unmodifiableList(receivers);
898 }
899
Seth Hampsonc384e142018-03-06 15:47:10 -0800900 /**
901 * Gets all RtpTransceivers associated with this peer connection.
902 * Note that calling getTransceivers will dispose of the transceivers previously
903 * returned.
904 * Note: This is only available with SdpSemantics.UNIFIED_PLAN specified.
905 */
906 public List<RtpTransceiver> getTransceivers() {
907 for (RtpTransceiver transceiver : transceivers) {
908 transceiver.dispose();
909 }
910 transceivers = nativeGetTransceivers();
911 return Collections.unmodifiableList(transceivers);
912 }
913
914 /**
915 * Adds a new media stream track to be sent on this peer connection, and returns
916 * the newly created RtpSender. If streamIds are specified, the RtpSender will
917 * be associated with the streams specified in the streamIds list.
918 *
919 * @throws IllegalStateException if an error accors in C++ addTrack.
920 * An error can occur if:
921 * - A sender already exists for the track.
922 * - The peer connection is closed.
923 */
924 public RtpSender addTrack(MediaStreamTrack track) {
925 return addTrack(track, Collections.emptyList());
926 }
927
928 public RtpSender addTrack(MediaStreamTrack track, List<String> streamIds) {
929 if (track == null || streamIds == null) {
930 throw new NullPointerException("No MediaStreamTrack specified in addTrack.");
931 }
932 RtpSender newSender = nativeAddTrack(track.nativeTrack, streamIds);
933 if (newSender == null) {
934 throw new IllegalStateException("C++ addTrack failed.");
935 }
936 senders.add(newSender);
937 return newSender;
938 }
939
940 /**
941 * Stops sending media from sender. The sender will still appear in getSenders. Future
942 * calls to createOffer will mark the m section for the corresponding transceiver as
943 * receive only or inactive, as defined in JSEP. Returns true on success.
944 */
945 public boolean removeTrack(RtpSender sender) {
946 if (sender == null) {
947 throw new NullPointerException("No RtpSender specified for removeTrack.");
948 }
949 return nativeRemoveTrack(sender.nativeRtpSender);
950 }
951
952 /**
953 * Creates a new RtpTransceiver and adds it to the set of transceivers. Adding a
954 * transceiver will cause future calls to CreateOffer to add a media description
955 * for the corresponding transceiver.
956 *
957 * <p>The initial value of |mid| in the returned transceiver is null. Setting a
958 * new session description may change it to a non-null value.
959 *
960 * <p>https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtransceiver
961 *
962 * <p>If a MediaStreamTrack is specified then a transceiver will be added with a
963 * sender set to transmit the given track. The kind
964 * of the transceiver (and sender/receiver) will be derived from the kind of
965 * the track.
966 *
967 * <p>If MediaType is specified then a transceiver will be added based upon that type.
968 * This can be either MEDIA_TYPE_AUDIO or MEDIA_TYPE_VIDEO.
969 *
970 * <p>Optionally, an RtpTransceiverInit structure can be specified to configure
971 * the transceiver from construction. If not specified, the transceiver will
972 * default to having a direction of kSendRecv and not be part of any streams.
973 *
974 * <p>Note: These methods are only available with SdpSemantics.UNIFIED_PLAN specified.
975 * @throws IllegalStateException if an error accors in C++ addTransceiver
976 */
977 public RtpTransceiver addTransceiver(MediaStreamTrack track) {
978 return addTransceiver(track, new RtpTransceiver.RtpTransceiverInit());
979 }
980
981 public RtpTransceiver addTransceiver(
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100982 MediaStreamTrack track, @Nullable RtpTransceiver.RtpTransceiverInit init) {
Seth Hampsonc384e142018-03-06 15:47:10 -0800983 if (track == null) {
984 throw new NullPointerException("No MediaStreamTrack specified for addTransceiver.");
985 }
986 if (init == null) {
987 init = new RtpTransceiver.RtpTransceiverInit();
988 }
989 RtpTransceiver newTransceiver = nativeAddTransceiverWithTrack(track.nativeTrack, init);
990 if (newTransceiver == null) {
991 throw new IllegalStateException("C++ addTransceiver failed.");
992 }
993 transceivers.add(newTransceiver);
994 return newTransceiver;
995 }
996
997 public RtpTransceiver addTransceiver(MediaStreamTrack.MediaType mediaType) {
998 return addTransceiver(mediaType, new RtpTransceiver.RtpTransceiverInit());
999 }
1000
1001 public RtpTransceiver addTransceiver(
Sami Kalliomäkie7592d82018-03-22 13:32:44 +01001002 MediaStreamTrack.MediaType mediaType, @Nullable RtpTransceiver.RtpTransceiverInit init) {
Seth Hampsonc384e142018-03-06 15:47:10 -08001003 if (mediaType == null) {
1004 throw new NullPointerException("No MediaType specified for addTransceiver.");
1005 }
1006 if (init == null) {
1007 init = new RtpTransceiver.RtpTransceiverInit();
1008 }
1009 RtpTransceiver newTransceiver = nativeAddTransceiverOfType(mediaType, init);
1010 if (newTransceiver == null) {
1011 throw new IllegalStateException("C++ addTransceiver failed.");
1012 }
1013 transceivers.add(newTransceiver);
1014 return newTransceiver;
1015 }
1016
deadbeef82215872017-04-18 10:27:51 -07001017 // Older, non-standard implementation of getStats.
1018 @Deprecated
Sami Kalliomäkie7592d82018-03-22 13:32:44 +01001019 public boolean getStats(StatsObserver observer, @Nullable MediaStreamTrack track) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001020 return nativeOldGetStats(observer, (track == null) ? 0 : track.nativeTrack);
deadbeef82215872017-04-18 10:27:51 -07001021 }
1022
Seth Hampsonc384e142018-03-06 15:47:10 -08001023 /**
1024 * Gets stats using the new stats collection API, see webrtc/api/stats/. These
1025 * will replace old stats collection API when the new API has matured enough.
1026 */
deadbeef82215872017-04-18 10:27:51 -07001027 public void getStats(RTCStatsCollectorCallback callback) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001028 nativeNewGetStats(callback);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 }
1030
Seth Hampsonc384e142018-03-06 15:47:10 -08001031 /**
1032 * Limits the bandwidth allocated for all RTP streams sent by this
1033 * PeerConnection. Pass null to leave a value unchanged.
1034 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001035 public boolean setBitrate(Integer min, Integer current, Integer max) {
1036 return nativeSetBitrate(min, current, max);
1037 }
zsteind89b0bc2017-08-03 11:11:40 -07001038
Seth Hampsonc384e142018-03-06 15:47:10 -08001039 /**
1040 * Starts recording an RTC event log.
1041 *
1042 * Ownership of the file is transfered to the native code. If an RTC event
1043 * log is already being recorded, it will be stopped and a new one will start
1044 * using the provided file. Logging will continue until the stopRtcEventLog
1045 * function is called. The max_size_bytes argument is ignored, it is added
1046 * for future use.
1047 */
ivoc0c6f0f62016-07-06 04:34:23 -07001048 public boolean startRtcEventLog(int file_descriptor, int max_size_bytes) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001049 return nativeStartRtcEventLog(file_descriptor, max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -07001050 }
1051
Seth Hampsonc384e142018-03-06 15:47:10 -08001052 /**
1053 * Stops recording an RTC event log. If no RTC event log is currently being
1054 * recorded, this call will have no effect.
1055 */
ivoc14d5dbe2016-07-04 07:06:55 -07001056 public void stopRtcEventLog() {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001057 nativeStopRtcEventLog();
ivoc14d5dbe2016-07-04 07:06:55 -07001058 }
1059
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060 // TODO(fischman): add support for DTMF-related methods once that API
1061 // stabilizes.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001062 public SignalingState signalingState() {
1063 return nativeSignalingState();
1064 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001065
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001066 public IceConnectionState iceConnectionState() {
1067 return nativeIceConnectionState();
1068 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001070 public IceGatheringState iceGatheringState() {
1071 return nativeIceGatheringState();
1072 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001074 public void close() {
1075 nativeClose();
1076 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077
deadbeef43697f62017-09-12 10:52:14 -07001078 /**
1079 * Free native resources associated with this PeerConnection instance.
Seth Hampsonc384e142018-03-06 15:47:10 -08001080 *
deadbeef43697f62017-09-12 10:52:14 -07001081 * This method removes a reference count from the C++ PeerConnection object,
1082 * which should result in it being destroyed. It also calls equivalent
1083 * "dispose" methods on the Java objects attached to this PeerConnection
1084 * (streams, senders, receivers), such that their associated C++ objects
1085 * will also be destroyed.
Seth Hampsonc384e142018-03-06 15:47:10 -08001086 *
1087 * <p>Note that this method cannot be safely called from an observer callback
deadbeef43697f62017-09-12 10:52:14 -07001088 * (PeerConnection.Observer, DataChannel.Observer, etc.). If you want to, for
1089 * example, destroy the PeerConnection after an "ICE failed" callback, you
1090 * must do this asynchronously (in other words, unwind the stack first). See
1091 * <a href="https://bugs.chromium.org/p/webrtc/issues/detail?id=3721">bug
1092 * 3721</a> for more details.
1093 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094 public void dispose() {
1095 close();
1096 for (MediaStream stream : localStreams) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001097 nativeRemoveLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098 stream.dispose();
1099 }
1100 localStreams.clear();
deadbeef4139c0f2015-10-06 12:29:25 -07001101 for (RtpSender sender : senders) {
1102 sender.dispose();
1103 }
1104 senders.clear();
1105 for (RtpReceiver receiver : receivers) {
1106 receiver.dispose();
1107 }
Seth Hampsonc384e142018-03-06 15:47:10 -08001108 for (RtpTransceiver transceiver : transceivers) {
1109 transceiver.dispose();
1110 }
1111 transceivers.clear();
deadbeef4139c0f2015-10-06 12:29:25 -07001112 receivers.clear();
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001113 nativeFreeOwnedPeerConnection(nativePeerConnection);
1114 }
1115
1116 /** Returns a pointer to the native webrtc::PeerConnectionInterface. */
1117 public long getNativePeerConnection() {
1118 return nativeGetNativePeerConnection();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001119 }
1120
Magnus Jedvert9060eb12017-12-12 12:52:54 +01001121 @CalledByNative
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001122 long getNativeOwnedPeerConnection() {
Magnus Jedvert9060eb12017-12-12 12:52:54 +01001123 return nativePeerConnection;
1124 }
1125
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001126 public static long createNativePeerConnectionObserver(Observer observer) {
1127 return nativeCreatePeerConnectionObserver(observer);
1128 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001130 private native long nativeGetNativePeerConnection();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001131 private native SessionDescription nativeGetLocalDescription();
1132 private native SessionDescription nativeGetRemoteDescription();
1133 private native DataChannel nativeCreateDataChannel(String label, DataChannel.Init init);
1134 private native void nativeCreateOffer(SdpObserver observer, MediaConstraints constraints);
1135 private native void nativeCreateAnswer(SdpObserver observer, MediaConstraints constraints);
1136 private native void nativeSetLocalDescription(SdpObserver observer, SessionDescription sdp);
1137 private native void nativeSetRemoteDescription(SdpObserver observer, SessionDescription sdp);
1138 private native void nativeSetAudioPlayout(boolean playout);
1139 private native void nativeSetAudioRecording(boolean recording);
1140 private native boolean nativeSetBitrate(Integer min, Integer current, Integer max);
1141 private native SignalingState nativeSignalingState();
1142 private native IceConnectionState nativeIceConnectionState();
1143 private native IceGatheringState nativeIceGatheringState();
1144 private native void nativeClose();
1145 private static native long nativeCreatePeerConnectionObserver(Observer observer);
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001146 private static native void nativeFreeOwnedPeerConnection(long ownedPeerConnection);
1147 private native boolean nativeSetConfiguration(RTCConfiguration config);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001148 private native boolean nativeAddIceCandidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001149 String sdpMid, int sdpMLineIndex, String iceCandidateSdp);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001150 private native boolean nativeRemoveIceCandidates(final IceCandidate[] candidates);
1151 private native boolean nativeAddLocalStream(long stream);
1152 private native void nativeRemoveLocalStream(long stream);
1153 private native boolean nativeOldGetStats(StatsObserver observer, long nativeTrack);
1154 private native void nativeNewGetStats(RTCStatsCollectorCallback callback);
1155 private native RtpSender nativeCreateSender(String kind, String stream_id);
1156 private native List<RtpSender> nativeGetSenders();
1157 private native List<RtpReceiver> nativeGetReceivers();
Seth Hampsonc384e142018-03-06 15:47:10 -08001158 private native List<RtpTransceiver> nativeGetTransceivers();
1159 private native RtpSender nativeAddTrack(long track, List<String> streamIds);
1160 private native boolean nativeRemoveTrack(long sender);
1161 private native RtpTransceiver nativeAddTransceiverWithTrack(
1162 long track, RtpTransceiver.RtpTransceiverInit init);
1163 private native RtpTransceiver nativeAddTransceiverOfType(
1164 MediaStreamTrack.MediaType mediaType, RtpTransceiver.RtpTransceiverInit init);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001165 private native boolean nativeStartRtcEventLog(int file_descriptor, int max_size_bytes);
1166 private native void nativeStopRtcEventLog();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167}