blob: db973f2fed2d76e9c63b57a1eab98483d5a017f3 [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
Artem Titarenko69540f42018-12-10 12:30:46 +010013import android.support.annotation.Nullable;
Magnus Jedvert6062f372017-11-16 16:53:12 +010014import java.util.ArrayList;
Sami Kalliomäki3e189a62017-11-24 11:13:39 +010015import java.util.Collections;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016import java.util.List;
Patrik Höglundbd6ffaf2018-11-16 14:55:16 +010017import org.webrtc.DataChannel;
18import org.webrtc.MediaStreamTrack;
19import org.webrtc.RtpTransceiver;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020
21/**
22 * Java-land version of the PeerConnection APIs; wraps the C++ API
23 * http://www.webrtc.org/reference/native-apis, which in turn is inspired by the
24 * JS APIs: http://dev.w3.org/2011/webrtc/editor/webrtc.html and
25 * http://www.w3.org/TR/mediacapture-streams/
26 */
27public class PeerConnection {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028 /** Tracks PeerConnectionInterface::IceGatheringState */
Magnus Jedvertba700f62017-12-04 13:43:27 +010029 public enum IceGatheringState {
30 NEW,
31 GATHERING,
32 COMPLETE;
33
34 @CalledByNative("IceGatheringState")
35 static IceGatheringState fromNativeIndex(int nativeIndex) {
36 return values()[nativeIndex];
37 }
38 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039
40 /** Tracks PeerConnectionInterface::IceConnectionState */
41 public enum IceConnectionState {
sakalb6760f92016-09-29 04:12:44 -070042 NEW,
43 CHECKING,
44 CONNECTED,
45 COMPLETED,
46 FAILED,
47 DISCONNECTED,
Magnus Jedvertba700f62017-12-04 13:43:27 +010048 CLOSED;
49
50 @CalledByNative("IceConnectionState")
51 static IceConnectionState fromNativeIndex(int nativeIndex) {
52 return values()[nativeIndex];
53 }
sakalb6760f92016-09-29 04:12:44 -070054 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055
Jonas Olssonf01d8c82018-11-08 15:19:04 +010056 /** Tracks PeerConnectionInterface::PeerConnectionState */
57 public enum PeerConnectionState {
58 NEW,
59 CONNECTING,
60 CONNECTED,
61 DISCONNECTED,
62 FAILED,
63 CLOSED;
64
65 @CalledByNative("PeerConnectionState")
66 static PeerConnectionState fromNativeIndex(int nativeIndex) {
67 return values()[nativeIndex];
68 }
69 }
70
hnsl04833622017-01-09 08:35:45 -080071 /** Tracks PeerConnectionInterface::TlsCertPolicy */
72 public enum TlsCertPolicy {
73 TLS_CERT_POLICY_SECURE,
74 TLS_CERT_POLICY_INSECURE_NO_CHECK,
75 }
76
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077 /** Tracks PeerConnectionInterface::SignalingState */
78 public enum SignalingState {
sakalb6760f92016-09-29 04:12:44 -070079 STABLE,
80 HAVE_LOCAL_OFFER,
81 HAVE_LOCAL_PRANSWER,
82 HAVE_REMOTE_OFFER,
83 HAVE_REMOTE_PRANSWER,
Magnus Jedvertba700f62017-12-04 13:43:27 +010084 CLOSED;
85
86 @CalledByNative("SignalingState")
87 static SignalingState fromNativeIndex(int nativeIndex) {
88 return values()[nativeIndex];
89 }
sakalb6760f92016-09-29 04:12:44 -070090 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091
92 /** Java version of PeerConnectionObserver. */
93 public static interface Observer {
94 /** Triggered when the SignalingState changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010095 @CalledByNative("Observer") void onSignalingChange(SignalingState newState);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096
97 /** Triggered when the IceConnectionState changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010098 @CalledByNative("Observer") void onIceConnectionChange(IceConnectionState newState);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099
Jonas Olssonf01d8c82018-11-08 15:19:04 +0100100 /** Triggered when the PeerConnectionState changes. */
101 @CalledByNative("Observer")
102 default void onConnectionChange(PeerConnectionState newState) {}
103
Peter Thatcher54360512015-07-08 11:08:35 -0700104 /** Triggered when the ICE connection receiving status changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100105 @CalledByNative("Observer") void onIceConnectionReceivingChange(boolean receiving);
Peter Thatcher54360512015-07-08 11:08:35 -0700106
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 /** Triggered when the IceGatheringState changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100108 @CalledByNative("Observer") void onIceGatheringChange(IceGatheringState newState);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109
110 /** Triggered when a new ICE candidate has been found. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100111 @CalledByNative("Observer") void onIceCandidate(IceCandidate candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700113 /** Triggered when some ICE candidates have been removed. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100114 @CalledByNative("Observer") void onIceCandidatesRemoved(IceCandidate[] candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700115
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 /** Triggered when media is received on a new stream from remote peer. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100117 @CalledByNative("Observer") void onAddStream(MediaStream stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118
119 /** Triggered when a remote peer close a stream. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100120 @CalledByNative("Observer") void onRemoveStream(MediaStream stream);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000121
122 /** Triggered when a remote peer opens a DataChannel. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100123 @CalledByNative("Observer") void onDataChannel(DataChannel dataChannel);
fischman@webrtc.orgd7568a02014-01-13 22:04:12 +0000124
125 /** Triggered when renegotiation is necessary. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100126 @CalledByNative("Observer") void onRenegotiationNeeded();
zhihuangdcccda72016-12-21 14:08:03 -0800127
128 /**
129 * Triggered when a new track is signaled by the remote peer, as a result of
130 * setRemoteDescription.
131 */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100132 @CalledByNative("Observer") void onAddTrack(RtpReceiver receiver, MediaStream[] mediaStreams);
Seth Hampson31dbc242018-05-07 09:28:19 -0700133
134 /**
135 * Triggered when the signaling from SetRemoteDescription indicates that a transceiver
136 * will be receiving media from a remote endpoint. This is only called if UNIFIED_PLAN
137 * semantics are specified. The transceiver will be disposed automatically.
138 */
139 @CalledByNative("Observer") default void onTrack(RtpTransceiver transceiver){};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140 }
141
142 /** Java version of PeerConnectionInterface.IceServer. */
143 public static class IceServer {
Emad Omaradab1d2d2017-06-16 15:43:11 -0700144 // List of URIs associated with this server. Valid formats are described
145 // in RFC7064 and RFC7065, and more may be added in the future. The "host"
146 // part of the URI may contain either an IP address or a hostname.
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700147 @Deprecated public final String uri;
148 public final List<String> urls;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149 public final String username;
150 public final String password;
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000151 public final TlsCertPolicy tlsCertPolicy;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152
Emad Omaradab1d2d2017-06-16 15:43:11 -0700153 // If the URIs in |urls| only contain IP addresses, this field can be used
154 // to indicate the hostname, which may be necessary for TLS (using the SNI
155 // extension). If |urls| itself contains the hostname, this isn't
156 // necessary.
157 public final String hostname;
158
Diogo Real1dca9d52017-08-29 12:18:32 -0700159 // List of protocols to be used in the TLS ALPN extension.
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000160 public final List<String> tlsAlpnProtocols;
Diogo Real1dca9d52017-08-29 12:18:32 -0700161
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700162 // List of elliptic curves to be used in the TLS elliptic curves extension.
163 // Only curve names supported by OpenSSL should be used (eg. "P-256","X25519").
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000164 public final List<String> tlsEllipticCurves;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700165
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 /** Convenience constructor for STUN servers. */
Diogo Real05ea2b32017-08-31 00:12:58 -0700167 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 public IceServer(String uri) {
169 this(uri, "", "");
170 }
171
Diogo Real05ea2b32017-08-31 00:12:58 -0700172 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 public IceServer(String uri, String username, String password) {
hnsl04833622017-01-09 08:35:45 -0800174 this(uri, username, password, TlsCertPolicy.TLS_CERT_POLICY_SECURE);
175 }
176
Diogo Real05ea2b32017-08-31 00:12:58 -0700177 @Deprecated
hnsl04833622017-01-09 08:35:45 -0800178 public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy) {
Emad Omaradab1d2d2017-06-16 15:43:11 -0700179 this(uri, username, password, tlsCertPolicy, "");
180 }
181
Diogo Real05ea2b32017-08-31 00:12:58 -0700182 @Deprecated
Emad Omaradab1d2d2017-06-16 15:43:11 -0700183 public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy,
184 String hostname) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700185 this(uri, Collections.singletonList(uri), username, password, tlsCertPolicy, hostname, null,
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000186 null);
Diogo Real1dca9d52017-08-29 12:18:32 -0700187 }
188
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700189 private IceServer(String uri, List<String> urls, String username, String password,
190 TlsCertPolicy tlsCertPolicy, String hostname, List<String> tlsAlpnProtocols,
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000191 List<String> tlsEllipticCurves) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700192 if (uri == null || urls == null || urls.isEmpty()) {
193 throw new IllegalArgumentException("uri == null || urls == null || urls.isEmpty()");
194 }
195 for (String it : urls) {
196 if (it == null) {
197 throw new IllegalArgumentException("urls element is null: " + urls);
198 }
199 }
200 if (username == null) {
201 throw new IllegalArgumentException("username == null");
202 }
203 if (password == null) {
204 throw new IllegalArgumentException("password == null");
205 }
206 if (hostname == null) {
207 throw new IllegalArgumentException("hostname == null");
208 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209 this.uri = uri;
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700210 this.urls = urls;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 this.username = username;
212 this.password = password;
hnsl04833622017-01-09 08:35:45 -0800213 this.tlsCertPolicy = tlsCertPolicy;
Emad Omaradab1d2d2017-06-16 15:43:11 -0700214 this.hostname = hostname;
Diogo Real1dca9d52017-08-29 12:18:32 -0700215 this.tlsAlpnProtocols = tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700216 this.tlsEllipticCurves = tlsEllipticCurves;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 }
218
Sami Kalliomäkibde473e2017-10-30 13:34:41 +0100219 @Override
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 public String toString() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700221 return urls + " [" + username + ":" + password + "] [" + tlsCertPolicy + "] [" + hostname
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000222 + "] [" + tlsAlpnProtocols + "] [" + tlsEllipticCurves + "]";
Diogo Real1dca9d52017-08-29 12:18:32 -0700223 }
224
225 public static Builder builder(String uri) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700226 return new Builder(Collections.singletonList(uri));
227 }
228
229 public static Builder builder(List<String> urls) {
230 return new Builder(urls);
Diogo Real1dca9d52017-08-29 12:18:32 -0700231 }
232
233 public static class Builder {
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100234 @Nullable private final List<String> urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700235 private String username = "";
236 private String password = "";
237 private TlsCertPolicy tlsCertPolicy = TlsCertPolicy.TLS_CERT_POLICY_SECURE;
238 private String hostname = "";
239 private List<String> tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700240 private List<String> tlsEllipticCurves;
Diogo Real1dca9d52017-08-29 12:18:32 -0700241
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700242 private Builder(List<String> urls) {
243 if (urls == null || urls.isEmpty()) {
244 throw new IllegalArgumentException("urls == null || urls.isEmpty(): " + urls);
245 }
246 this.urls = urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700247 }
248
249 public Builder setUsername(String username) {
250 this.username = username;
251 return this;
252 }
253
254 public Builder setPassword(String password) {
255 this.password = password;
256 return this;
257 }
258
259 public Builder setTlsCertPolicy(TlsCertPolicy tlsCertPolicy) {
260 this.tlsCertPolicy = tlsCertPolicy;
261 return this;
262 }
263
264 public Builder setHostname(String hostname) {
265 this.hostname = hostname;
266 return this;
267 }
268
269 public Builder setTlsAlpnProtocols(List<String> tlsAlpnProtocols) {
270 this.tlsAlpnProtocols = tlsAlpnProtocols;
271 return this;
272 }
273
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700274 public Builder setTlsEllipticCurves(List<String> tlsEllipticCurves) {
275 this.tlsEllipticCurves = tlsEllipticCurves;
276 return this;
277 }
278
Diogo Real1dca9d52017-08-29 12:18:32 -0700279 public IceServer createIceServer() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700280 return new IceServer(urls.get(0), urls, username, password, tlsCertPolicy, hostname,
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000281 tlsAlpnProtocols, tlsEllipticCurves);
Diogo Real1dca9d52017-08-29 12:18:32 -0700282 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283 }
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100284
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100285 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100286 @CalledByNative("IceServer")
287 List<String> getUrls() {
288 return urls;
289 }
290
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100291 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100292 @CalledByNative("IceServer")
293 String getUsername() {
294 return username;
295 }
296
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100297 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100298 @CalledByNative("IceServer")
299 String getPassword() {
300 return password;
301 }
302
303 @CalledByNative("IceServer")
304 TlsCertPolicy getTlsCertPolicy() {
305 return tlsCertPolicy;
306 }
307
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100308 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100309 @CalledByNative("IceServer")
310 String getHostname() {
311 return hostname;
312 }
313
314 @CalledByNative("IceServer")
315 List<String> getTlsAlpnProtocols() {
316 return tlsAlpnProtocols;
317 }
318
319 @CalledByNative("IceServer")
320 List<String> getTlsEllipticCurves() {
321 return tlsEllipticCurves;
322 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 }
324
Jiayang Liucac1b382015-04-30 12:35:24 -0700325 /** Java version of PeerConnectionInterface.IceTransportsType */
sakalb6760f92016-09-29 04:12:44 -0700326 public enum IceTransportsType { NONE, RELAY, NOHOST, ALL }
Jiayang Liucac1b382015-04-30 12:35:24 -0700327
328 /** Java version of PeerConnectionInterface.BundlePolicy */
sakalb6760f92016-09-29 04:12:44 -0700329 public enum BundlePolicy { BALANCED, MAXBUNDLE, MAXCOMPAT }
Jiayang Liucac1b382015-04-30 12:35:24 -0700330
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700331 /** Java version of PeerConnectionInterface.RtcpMuxPolicy */
sakalb6760f92016-09-29 04:12:44 -0700332 public enum RtcpMuxPolicy { NEGOTIATE, REQUIRE }
glaznev97579a42015-09-01 11:31:27 -0700333
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700334 /** Java version of PeerConnectionInterface.TcpCandidatePolicy */
sakalb6760f92016-09-29 04:12:44 -0700335 public enum TcpCandidatePolicy { ENABLED, DISABLED }
Jiayang Liucac1b382015-04-30 12:35:24 -0700336
honghaiz60347052016-05-31 18:29:12 -0700337 /** Java version of PeerConnectionInterface.CandidateNetworkPolicy */
sakalb6760f92016-09-29 04:12:44 -0700338 public enum CandidateNetworkPolicy { ALL, LOW_COST }
honghaiz60347052016-05-31 18:29:12 -0700339
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800340 // Keep in sync with webrtc/rtc_base/network_constants.h.
341 public enum AdapterType {
342 UNKNOWN,
343 ETHERNET,
344 WIFI,
345 CELLULAR,
346 VPN,
347 LOOPBACK,
348 }
349
glaznev97579a42015-09-01 11:31:27 -0700350 /** Java version of rtc::KeyType */
sakalb6760f92016-09-29 04:12:44 -0700351 public enum KeyType { RSA, ECDSA }
glaznev97579a42015-09-01 11:31:27 -0700352
honghaiz1f429e32015-09-28 07:57:34 -0700353 /** Java version of PeerConnectionInterface.ContinualGatheringPolicy */
sakalb6760f92016-09-29 04:12:44 -0700354 public enum ContinualGatheringPolicy { GATHER_ONCE, GATHER_CONTINUALLY }
honghaiz1f429e32015-09-28 07:57:34 -0700355
Steve Antond960a0c2017-07-17 12:33:07 -0700356 /** Java version of rtc::IntervalRange */
357 public static class IntervalRange {
358 private final int min;
359 private final int max;
360
361 public IntervalRange(int min, int max) {
362 this.min = min;
363 this.max = max;
364 }
365
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100366 @CalledByNative("IntervalRange")
Steve Antond960a0c2017-07-17 12:33:07 -0700367 public int getMin() {
368 return min;
369 }
370
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100371 @CalledByNative("IntervalRange")
Steve Antond960a0c2017-07-17 12:33:07 -0700372 public int getMax() {
373 return max;
374 }
375 }
376
Seth Hampsonc384e142018-03-06 15:47:10 -0800377 /**
378 * Java version of webrtc::SdpSemantics.
379 *
380 * Configure the SDP semantics used by this PeerConnection. Note that the
381 * WebRTC 1.0 specification requires UNIFIED_PLAN semantics. The
382 * RtpTransceiver API is only available with UNIFIED_PLAN semantics.
383 *
384 * <p>PLAN_B will cause PeerConnection to create offers and answers with at
385 * most one audio and one video m= section with multiple RtpSenders and
386 * RtpReceivers specified as multiple a=ssrc lines within the section. This
387 * will also cause PeerConnection to ignore all but the first m= section of
388 * the same media type.
389 *
390 * <p>UNIFIED_PLAN will cause PeerConnection to create offers and answers with
391 * multiple m= sections where each m= section maps to one RtpSender and one
392 * RtpReceiver (an RtpTransceiver), either both audio or both video. This
393 * will also cause PeerConnection to ignore all but the first a=ssrc lines
394 * that form a Plan B stream.
395 *
396 * <p>For users who wish to send multiple audio/video streams and need to stay
397 * interoperable with legacy WebRTC implementations, specify PLAN_B.
398 *
399 * <p>For users who wish to send multiple audio/video streams and/or wish to
400 * use the new RtpTransceiver API, specify UNIFIED_PLAN.
401 */
402 public enum SdpSemantics { PLAN_B, UNIFIED_PLAN }
403
Jiayang Liucac1b382015-04-30 12:35:24 -0700404 /** Java version of PeerConnectionInterface.RTCConfiguration */
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800405 // TODO(qingsi): Resolve the naming inconsistency of fields with/without units.
Jiayang Liucac1b382015-04-30 12:35:24 -0700406 public static class RTCConfiguration {
407 public IceTransportsType iceTransportsType;
408 public List<IceServer> iceServers;
409 public BundlePolicy bundlePolicy;
Michael Iedema02137862018-10-09 15:30:01 +0200410 @Nullable public RtcCertificatePem certificate;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700411 public RtcpMuxPolicy rtcpMuxPolicy;
Jiayang Liucac1b382015-04-30 12:35:24 -0700412 public TcpCandidatePolicy tcpCandidatePolicy;
honghaiz60347052016-05-31 18:29:12 -0700413 public CandidateNetworkPolicy candidateNetworkPolicy;
Henrik Lundin64dad832015-05-11 12:44:23 +0200414 public int audioJitterBufferMaxPackets;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200415 public boolean audioJitterBufferFastAccelerate;
honghaiz4edc39c2015-09-01 09:53:56 -0700416 public int iceConnectionReceivingTimeout;
Honghai Zhang381b4212015-12-04 12:24:03 -0800417 public int iceBackupCandidatePairPingInterval;
glaznev97579a42015-09-01 11:31:27 -0700418 public KeyType keyType;
honghaiz1f429e32015-09-28 07:57:34 -0700419 public ContinualGatheringPolicy continualGatheringPolicy;
deadbeefbe0c96f2016-05-18 16:20:14 -0700420 public int iceCandidatePoolSize;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700421 public boolean pruneTurnPorts;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700422 public boolean presumeWritableWhenFullyRelayed;
Qingsi Wange6826d22018-03-08 14:55:14 -0800423 // The following fields define intervals in milliseconds at which ICE
424 // connectivity checks are sent.
425 //
426 // We consider ICE is "strongly connected" for an agent when there is at
427 // least one candidate pair that currently succeeds in connectivity check
428 // from its direction i.e. sending a ping and receives a ping response, AND
429 // all candidate pairs have sent a minimum number of pings for connectivity
430 // (this number is implementation-specific). Otherwise, ICE is considered in
431 // "weak connectivity".
432 //
433 // Note that the above notion of strong and weak connectivity is not defined
434 // in RFC 5245, and they apply to our current ICE implementation only.
435 //
436 // 1) iceCheckIntervalStrongConnectivityMs defines the interval applied to
437 // ALL candidate pairs when ICE is strongly connected,
438 // 2) iceCheckIntervalWeakConnectivityMs defines the counterpart for ALL
439 // pairs when ICE is weakly connected, and
440 // 3) iceCheckMinInterval defines the minimal interval (equivalently the
441 // maximum rate) that overrides the above two intervals when either of them
442 // is less.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100443 @Nullable public Integer iceCheckIntervalStrongConnectivityMs;
444 @Nullable public Integer iceCheckIntervalWeakConnectivityMs;
445 @Nullable public Integer iceCheckMinInterval;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700446 // The time period in milliseconds for which a candidate pair must wait for response to
447 // connectivitiy checks before it becomes unwritable.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100448 @Nullable public Integer iceUnwritableTimeMs;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700449 // The minimum number of connectivity checks that a candidate pair must sent without receiving
450 // response before it becomes unwritable.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100451 @Nullable public Integer iceUnwritableMinChecks;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800452 // The interval in milliseconds at which STUN candidates will resend STUN binding requests
453 // to keep NAT bindings open.
454 // The default value in the implementation is used if this field is null.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100455 @Nullable public Integer stunCandidateKeepaliveIntervalMs;
zhihuangb09b3f92017-03-07 14:40:51 -0800456 public boolean disableIPv6OnWifi;
deadbeef28e29192017-07-27 09:14:38 -0700457 // By default, PeerConnection will use a limited number of IPv6 network
458 // interfaces, in order to avoid too many ICE candidate pairs being created
459 // and delaying ICE completion.
460 //
461 // Can be set to Integer.MAX_VALUE to effectively disable the limit.
462 public int maxIPv6Networks;
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100463 @Nullable public IntervalRange iceRegatherIntervalRange;
Jiayang Liucac1b382015-04-30 12:35:24 -0700464
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100465 // These values will be overridden by MediaStream constraints if deprecated constraints-based
466 // create peerconnection interface is used.
467 public boolean disableIpv6;
468 public boolean enableDscp;
469 public boolean enableCpuOveruseDetection;
470 public boolean enableRtpDataChannel;
471 public boolean suspendBelowMinBitrate;
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100472 @Nullable public Integer screencastMinBitrate;
473 @Nullable public Boolean combinedAudioVideoBwe;
474 @Nullable public Boolean enableDtlsSrtp;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800475 // Use "Unknown" to represent no preference of adapter types, not the
476 // preference of adapters of unknown types.
477 public AdapterType networkPreference;
Seth Hampsonc384e142018-03-06 15:47:10 -0800478 public SdpSemantics sdpSemantics;
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100479
Jonas Orelandbdcee282017-10-10 14:01:40 +0200480 // This is an optional wrapper for the C++ webrtc::TurnCustomizer.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100481 @Nullable public TurnCustomizer turnCustomizer;
Jonas Orelandbdcee282017-10-10 14:01:40 +0200482
Zhi Huangb57e1692018-06-12 11:41:11 -0700483 // Actively reset the SRTP parameters whenever the DTLS transports underneath are reset for
484 // every offer/answer negotiation.This is only intended to be a workaround for crbug.com/835958
485 public boolean activeResetSrtpParams;
486
Piotr (Peter) Slatala09beff22018-10-17 07:22:40 -0700487 /*
488 * Experimental flag that enables a use of media transport. If this is true, the media transport
489 * factory MUST be provided to the PeerConnectionFactory.
490 */
491 public boolean useMediaTransport;
492
Bjorn Mellema9bbd862018-11-02 09:07:48 -0700493 /*
494 * Experimental flag that enables a use of media transport for data channels. If this is true,
495 * the media transport factory MUST be provided to the PeerConnectionFactory.
496 */
497 public boolean useMediaTransportForDataChannels;
498
Benjamin Wright8c27cca2018-10-25 10:16:44 -0700499 /**
500 * Defines advanced optional cryptographic settings related to SRTP and
501 * frame encryption for native WebRTC. Setting this will overwrite any
502 * options set through the PeerConnectionFactory (which is deprecated).
503 */
504 @Nullable public CryptoOptions cryptoOptions;
505
deadbeef28e29192017-07-27 09:14:38 -0700506 // TODO(deadbeef): Instead of duplicating the defaults here, we should do
507 // something to pick up the defaults from C++. The Objective-C equivalent
508 // of RTCConfiguration does that.
Jiayang Liucac1b382015-04-30 12:35:24 -0700509 public RTCConfiguration(List<IceServer> iceServers) {
510 iceTransportsType = IceTransportsType.ALL;
511 bundlePolicy = BundlePolicy.BALANCED;
zhihuang4dfb8ce2016-11-23 10:30:12 -0800512 rtcpMuxPolicy = RtcpMuxPolicy.REQUIRE;
Jiayang Liucac1b382015-04-30 12:35:24 -0700513 tcpCandidatePolicy = TcpCandidatePolicy.ENABLED;
Sami Kalliomäki9828beb2017-10-26 16:21:22 +0200514 candidateNetworkPolicy = CandidateNetworkPolicy.ALL;
Jiayang Liucac1b382015-04-30 12:35:24 -0700515 this.iceServers = iceServers;
Henrik Lundin64dad832015-05-11 12:44:23 +0200516 audioJitterBufferMaxPackets = 50;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200517 audioJitterBufferFastAccelerate = false;
honghaiz4edc39c2015-09-01 09:53:56 -0700518 iceConnectionReceivingTimeout = -1;
Honghai Zhang381b4212015-12-04 12:24:03 -0800519 iceBackupCandidatePairPingInterval = -1;
glaznev97579a42015-09-01 11:31:27 -0700520 keyType = KeyType.ECDSA;
honghaiz1f429e32015-09-28 07:57:34 -0700521 continualGatheringPolicy = ContinualGatheringPolicy.GATHER_ONCE;
deadbeefbe0c96f2016-05-18 16:20:14 -0700522 iceCandidatePoolSize = 0;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700523 pruneTurnPorts = false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700524 presumeWritableWhenFullyRelayed = false;
Qingsi Wange6826d22018-03-08 14:55:14 -0800525 iceCheckIntervalStrongConnectivityMs = null;
526 iceCheckIntervalWeakConnectivityMs = null;
skvlad51072462017-02-02 11:50:14 -0800527 iceCheckMinInterval = null;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700528 iceUnwritableTimeMs = null;
529 iceUnwritableMinChecks = null;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800530 stunCandidateKeepaliveIntervalMs = null;
zhihuangb09b3f92017-03-07 14:40:51 -0800531 disableIPv6OnWifi = false;
deadbeef28e29192017-07-27 09:14:38 -0700532 maxIPv6Networks = 5;
Steve Antond960a0c2017-07-17 12:33:07 -0700533 iceRegatherIntervalRange = null;
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100534 disableIpv6 = false;
535 enableDscp = false;
536 enableCpuOveruseDetection = true;
537 enableRtpDataChannel = false;
538 suspendBelowMinBitrate = false;
539 screencastMinBitrate = null;
540 combinedAudioVideoBwe = null;
541 enableDtlsSrtp = null;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800542 networkPreference = AdapterType.UNKNOWN;
Seth Hampsonc384e142018-03-06 15:47:10 -0800543 sdpSemantics = SdpSemantics.PLAN_B;
Zhi Huangb57e1692018-06-12 11:41:11 -0700544 activeResetSrtpParams = false;
Piotr (Peter) Slatala09beff22018-10-17 07:22:40 -0700545 useMediaTransport = false;
Bjorn Mellema9bbd862018-11-02 09:07:48 -0700546 useMediaTransportForDataChannels = false;
Benjamin Wright8c27cca2018-10-25 10:16:44 -0700547 cryptoOptions = null;
Jiayang Liucac1b382015-04-30 12:35:24 -0700548 }
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100549
550 @CalledByNative("RTCConfiguration")
551 IceTransportsType getIceTransportsType() {
552 return iceTransportsType;
553 }
554
555 @CalledByNative("RTCConfiguration")
556 List<IceServer> getIceServers() {
557 return iceServers;
558 }
559
560 @CalledByNative("RTCConfiguration")
561 BundlePolicy getBundlePolicy() {
562 return bundlePolicy;
563 }
564
Michael Iedema02137862018-10-09 15:30:01 +0200565 @Nullable
566 @CalledByNative("RTCConfiguration")
567 RtcCertificatePem getCertificate() {
568 return certificate;
569 }
570
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100571 @CalledByNative("RTCConfiguration")
572 RtcpMuxPolicy getRtcpMuxPolicy() {
573 return rtcpMuxPolicy;
574 }
575
576 @CalledByNative("RTCConfiguration")
577 TcpCandidatePolicy getTcpCandidatePolicy() {
578 return tcpCandidatePolicy;
579 }
580
581 @CalledByNative("RTCConfiguration")
582 CandidateNetworkPolicy getCandidateNetworkPolicy() {
583 return candidateNetworkPolicy;
584 }
585
586 @CalledByNative("RTCConfiguration")
587 int getAudioJitterBufferMaxPackets() {
588 return audioJitterBufferMaxPackets;
589 }
590
591 @CalledByNative("RTCConfiguration")
592 boolean getAudioJitterBufferFastAccelerate() {
593 return audioJitterBufferFastAccelerate;
594 }
595
596 @CalledByNative("RTCConfiguration")
597 int getIceConnectionReceivingTimeout() {
598 return iceConnectionReceivingTimeout;
599 }
600
601 @CalledByNative("RTCConfiguration")
602 int getIceBackupCandidatePairPingInterval() {
603 return iceBackupCandidatePairPingInterval;
604 }
605
606 @CalledByNative("RTCConfiguration")
607 KeyType getKeyType() {
608 return keyType;
609 }
610
611 @CalledByNative("RTCConfiguration")
612 ContinualGatheringPolicy getContinualGatheringPolicy() {
613 return continualGatheringPolicy;
614 }
615
616 @CalledByNative("RTCConfiguration")
617 int getIceCandidatePoolSize() {
618 return iceCandidatePoolSize;
619 }
620
621 @CalledByNative("RTCConfiguration")
622 boolean getPruneTurnPorts() {
623 return pruneTurnPorts;
624 }
625
626 @CalledByNative("RTCConfiguration")
627 boolean getPresumeWritableWhenFullyRelayed() {
628 return presumeWritableWhenFullyRelayed;
629 }
630
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100631 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100632 @CalledByNative("RTCConfiguration")
Qingsi Wange6826d22018-03-08 14:55:14 -0800633 Integer getIceCheckIntervalStrongConnectivity() {
634 return iceCheckIntervalStrongConnectivityMs;
635 }
636
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100637 @Nullable
Qingsi Wange6826d22018-03-08 14:55:14 -0800638 @CalledByNative("RTCConfiguration")
639 Integer getIceCheckIntervalWeakConnectivity() {
640 return iceCheckIntervalWeakConnectivityMs;
641 }
642
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100643 @Nullable
Qingsi Wange6826d22018-03-08 14:55:14 -0800644 @CalledByNative("RTCConfiguration")
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100645 Integer getIceCheckMinInterval() {
646 return iceCheckMinInterval;
647 }
648
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100649 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100650 @CalledByNative("RTCConfiguration")
Qingsi Wang22e623a2018-03-13 10:53:57 -0700651 Integer getIceUnwritableTimeout() {
652 return iceUnwritableTimeMs;
653 }
654
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100655 @Nullable
Qingsi Wang22e623a2018-03-13 10:53:57 -0700656 @CalledByNative("RTCConfiguration")
657 Integer getIceUnwritableMinChecks() {
658 return iceUnwritableMinChecks;
659 }
660
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100661 @Nullable
Qingsi Wang22e623a2018-03-13 10:53:57 -0700662 @CalledByNative("RTCConfiguration")
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800663 Integer getStunCandidateKeepaliveInterval() {
664 return stunCandidateKeepaliveIntervalMs;
665 }
666
667 @CalledByNative("RTCConfiguration")
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100668 boolean getDisableIPv6OnWifi() {
669 return disableIPv6OnWifi;
670 }
671
672 @CalledByNative("RTCConfiguration")
673 int getMaxIPv6Networks() {
674 return maxIPv6Networks;
675 }
676
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100677 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100678 @CalledByNative("RTCConfiguration")
679 IntervalRange getIceRegatherIntervalRange() {
680 return iceRegatherIntervalRange;
681 }
682
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100683 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100684 @CalledByNative("RTCConfiguration")
685 TurnCustomizer getTurnCustomizer() {
686 return turnCustomizer;
687 }
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100688
689 @CalledByNative("RTCConfiguration")
690 boolean getDisableIpv6() {
691 return disableIpv6;
692 }
693
694 @CalledByNative("RTCConfiguration")
695 boolean getEnableDscp() {
696 return enableDscp;
697 }
698
699 @CalledByNative("RTCConfiguration")
700 boolean getEnableCpuOveruseDetection() {
701 return enableCpuOveruseDetection;
702 }
703
704 @CalledByNative("RTCConfiguration")
705 boolean getEnableRtpDataChannel() {
706 return enableRtpDataChannel;
707 }
708
709 @CalledByNative("RTCConfiguration")
710 boolean getSuspendBelowMinBitrate() {
711 return suspendBelowMinBitrate;
712 }
713
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100714 @Nullable
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100715 @CalledByNative("RTCConfiguration")
716 Integer getScreencastMinBitrate() {
717 return screencastMinBitrate;
718 }
719
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100720 @Nullable
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100721 @CalledByNative("RTCConfiguration")
722 Boolean getCombinedAudioVideoBwe() {
723 return combinedAudioVideoBwe;
724 }
725
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100726 @Nullable
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100727 @CalledByNative("RTCConfiguration")
728 Boolean getEnableDtlsSrtp() {
729 return enableDtlsSrtp;
730 }
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800731
732 @CalledByNative("RTCConfiguration")
733 AdapterType getNetworkPreference() {
734 return networkPreference;
735 }
Seth Hampsonc384e142018-03-06 15:47:10 -0800736
737 @CalledByNative("RTCConfiguration")
738 SdpSemantics getSdpSemantics() {
739 return sdpSemantics;
740 }
Zhi Huangb57e1692018-06-12 11:41:11 -0700741
742 @CalledByNative("RTCConfiguration")
743 boolean getActiveResetSrtpParams() {
744 return activeResetSrtpParams;
745 }
Piotr (Peter) Slatala09beff22018-10-17 07:22:40 -0700746
747 @CalledByNative("RTCConfiguration")
748 boolean getUseMediaTransport() {
749 return useMediaTransport;
750 }
Benjamin Wright8c27cca2018-10-25 10:16:44 -0700751
Bjorn Mellema9bbd862018-11-02 09:07:48 -0700752 @CalledByNative("RTCConfiguration")
753 boolean getUseMediaTransportForDataChannels() {
754 return useMediaTransportForDataChannels;
755 }
756
Benjamin Wright8c27cca2018-10-25 10:16:44 -0700757 @Nullable
758 @CalledByNative("RTCConfiguration")
759 CryptoOptions getCryptoOptions() {
760 return cryptoOptions;
761 }
Jiayang Liucac1b382015-04-30 12:35:24 -0700762 };
763
Magnus Jedvert6062f372017-11-16 16:53:12 +0100764 private final List<MediaStream> localStreams = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 private final long nativePeerConnection;
Magnus Jedvert6062f372017-11-16 16:53:12 +0100766 private List<RtpSender> senders = new ArrayList<>();
767 private List<RtpReceiver> receivers = new ArrayList<>();
Seth Hampsonc384e142018-03-06 15:47:10 -0800768 private List<RtpTransceiver> transceivers = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769
Sami Kalliomäki1ece1ed2017-12-20 11:59:22 +0100770 /**
771 * Wraps a PeerConnection created by the factory. Can be used by clients that want to implement
772 * their PeerConnection creation in JNI.
773 */
774 public PeerConnection(NativePeerConnectionFactory factory) {
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100775 this(factory.createNativePeerConnection());
Sami Kalliomäki1ece1ed2017-12-20 11:59:22 +0100776 }
777
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100778 PeerConnection(long nativePeerConnection) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779 this.nativePeerConnection = nativePeerConnection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 }
781
782 // JsepInterface.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100783 public SessionDescription getLocalDescription() {
784 return nativeGetLocalDescription();
785 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100787 public SessionDescription getRemoteDescription() {
788 return nativeGetRemoteDescription();
789 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790
Michael Iedema02137862018-10-09 15:30:01 +0200791 public RtcCertificatePem getCertificate() {
792 return nativeGetCertificate();
793 }
794
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100795 public DataChannel createDataChannel(String label, DataChannel.Init init) {
796 return nativeCreateDataChannel(label, init);
797 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000798
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100799 public void createOffer(SdpObserver observer, MediaConstraints constraints) {
800 nativeCreateOffer(observer, constraints);
801 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100803 public void createAnswer(SdpObserver observer, MediaConstraints constraints) {
804 nativeCreateAnswer(observer, constraints);
805 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100807 public void setLocalDescription(SdpObserver observer, SessionDescription sdp) {
808 nativeSetLocalDescription(observer, sdp);
809 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100811 public void setRemoteDescription(SdpObserver observer, SessionDescription sdp) {
812 nativeSetRemoteDescription(observer, sdp);
813 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814
Seth Hampsonc384e142018-03-06 15:47:10 -0800815 /**
816 * Enables/disables playout of received audio streams. Enabled by default.
817 *
818 * Note that even if playout is enabled, streams will only be played out if
819 * the appropriate SDP is also applied. The main purpose of this API is to
820 * be able to control the exact time when audio playout starts.
821 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100822 public void setAudioPlayout(boolean playout) {
823 nativeSetAudioPlayout(playout);
824 }
henrika5f6bf242017-11-01 11:06:56 +0100825
Seth Hampsonc384e142018-03-06 15:47:10 -0800826 /**
827 * Enables/disables recording of transmitted audio streams. Enabled by default.
828 *
829 * Note that even if recording is enabled, streams will only be recorded if
830 * the appropriate SDP is also applied. The main purpose of this API is to
831 * be able to control the exact time when audio recording starts.
832 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100833 public void setAudioRecording(boolean recording) {
834 nativeSetAudioRecording(recording);
835 }
henrika5f6bf242017-11-01 11:06:56 +0100836
deadbeef5d0b6d82017-01-09 16:05:28 -0800837 public boolean setConfiguration(RTCConfiguration config) {
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100838 return nativeSetConfiguration(config);
deadbeef5d0b6d82017-01-09 16:05:28 -0800839 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840
841 public boolean addIceCandidate(IceCandidate candidate) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100842 return nativeAddIceCandidate(candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 }
844
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700845 public boolean removeIceCandidates(final IceCandidate[] candidates) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100846 return nativeRemoveIceCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700847 }
848
Seth Hampsonc384e142018-03-06 15:47:10 -0800849 /**
850 * Adds a new MediaStream to be sent on this peer connection.
851 * Note: This method is not supported with SdpSemantics.UNIFIED_PLAN. Please
852 * use addTrack instead.
853 */
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000854 public boolean addStream(MediaStream stream) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200855 boolean ret = nativeAddLocalStream(stream.getNativeMediaStream());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 if (!ret) {
857 return false;
858 }
859 localStreams.add(stream);
860 return true;
861 }
862
Seth Hampsonc384e142018-03-06 15:47:10 -0800863 /**
864 * Removes the given media stream from this peer connection.
865 * This method is not supported with SdpSemantics.UNIFIED_PLAN. Please use
866 * removeTrack instead.
867 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 public void removeStream(MediaStream stream) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200869 nativeRemoveLocalStream(stream.getNativeMediaStream());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 localStreams.remove(stream);
871 }
872
deadbeef7a246882017-08-09 08:40:10 -0700873 /**
874 * Creates an RtpSender without a track.
Seth Hampsonc384e142018-03-06 15:47:10 -0800875 *
876 * <p>This method allows an application to cause the PeerConnection to negotiate
deadbeef7a246882017-08-09 08:40:10 -0700877 * sending/receiving a specific media type, but without having a track to
878 * send yet.
Seth Hampsonc384e142018-03-06 15:47:10 -0800879 *
880 * <p>When the application does want to begin sending a track, it can call
deadbeef7a246882017-08-09 08:40:10 -0700881 * RtpSender.setTrack, which doesn't require any additional SDP negotiation.
Seth Hampsonc384e142018-03-06 15:47:10 -0800882 *
883 * <p>Example use:
deadbeef7a246882017-08-09 08:40:10 -0700884 * <pre>
885 * {@code
886 * audioSender = pc.createSender("audio", "stream1");
887 * videoSender = pc.createSender("video", "stream1");
888 * // Do normal SDP offer/answer, which will kick off ICE/DTLS and negotiate
889 * // media parameters....
890 * // Later, when the endpoint is ready to actually begin sending:
891 * audioSender.setTrack(audioTrack, false);
892 * videoSender.setTrack(videoTrack, false);
893 * }
894 * </pre>
Seth Hampsonc384e142018-03-06 15:47:10 -0800895 * <p>Note: This corresponds most closely to "addTransceiver" in the official
deadbeef7a246882017-08-09 08:40:10 -0700896 * WebRTC API, in that it creates a sender without a track. It was
897 * implemented before addTransceiver because it provides useful
898 * functionality, and properly implementing transceivers would have required
899 * a great deal more work.
900 *
Seth Hampsonc384e142018-03-06 15:47:10 -0800901 * <p>Note: This is only available with SdpSemantics.PLAN_B specified. Please use
902 * addTransceiver instead.
903 *
deadbeef7a246882017-08-09 08:40:10 -0700904 * @param kind Corresponds to MediaStreamTrack kinds (must be "audio" or
905 * "video").
906 * @param stream_id The ID of the MediaStream that this sender's track will
907 * be associated with when SDP is applied to the remote
908 * PeerConnection. If createSender is used to create an
909 * audio and video sender that should be synchronized, they
910 * should use the same stream ID.
911 * @return A new RtpSender object if successful, or null otherwise.
912 */
deadbeefbd7d8f72015-12-18 16:58:44 -0800913 public RtpSender createSender(String kind, String stream_id) {
Seth Hampsonc384e142018-03-06 15:47:10 -0800914 RtpSender newSender = nativeCreateSender(kind, stream_id);
915 if (newSender != null) {
916 senders.add(newSender);
deadbeefee524f72015-12-02 11:27:40 -0800917 }
Seth Hampsonc384e142018-03-06 15:47:10 -0800918 return newSender;
deadbeefee524f72015-12-02 11:27:40 -0800919 }
920
Seth Hampsonc384e142018-03-06 15:47:10 -0800921 /**
922 * Gets all RtpSenders associated with this peer connection.
923 * Note that calling getSenders will dispose of the senders previously
924 * returned.
925 */
deadbeef4139c0f2015-10-06 12:29:25 -0700926 public List<RtpSender> getSenders() {
927 for (RtpSender sender : senders) {
928 sender.dispose();
929 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100930 senders = nativeGetSenders();
deadbeef4139c0f2015-10-06 12:29:25 -0700931 return Collections.unmodifiableList(senders);
932 }
933
Seth Hampsonc384e142018-03-06 15:47:10 -0800934 /**
935 * Gets all RtpReceivers associated with this peer connection.
936 * Note that calling getReceivers will dispose of the receivers previously
937 * returned.
938 */
deadbeef4139c0f2015-10-06 12:29:25 -0700939 public List<RtpReceiver> getReceivers() {
940 for (RtpReceiver receiver : receivers) {
941 receiver.dispose();
942 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100943 receivers = nativeGetReceivers();
deadbeef4139c0f2015-10-06 12:29:25 -0700944 return Collections.unmodifiableList(receivers);
945 }
946
Seth Hampsonc384e142018-03-06 15:47:10 -0800947 /**
948 * Gets all RtpTransceivers associated with this peer connection.
949 * Note that calling getTransceivers will dispose of the transceivers previously
950 * returned.
951 * Note: This is only available with SdpSemantics.UNIFIED_PLAN specified.
952 */
953 public List<RtpTransceiver> getTransceivers() {
954 for (RtpTransceiver transceiver : transceivers) {
955 transceiver.dispose();
956 }
957 transceivers = nativeGetTransceivers();
958 return Collections.unmodifiableList(transceivers);
959 }
960
961 /**
962 * Adds a new media stream track to be sent on this peer connection, and returns
963 * the newly created RtpSender. If streamIds are specified, the RtpSender will
964 * be associated with the streams specified in the streamIds list.
965 *
966 * @throws IllegalStateException if an error accors in C++ addTrack.
967 * An error can occur if:
968 * - A sender already exists for the track.
969 * - The peer connection is closed.
970 */
971 public RtpSender addTrack(MediaStreamTrack track) {
972 return addTrack(track, Collections.emptyList());
973 }
974
975 public RtpSender addTrack(MediaStreamTrack track, List<String> streamIds) {
976 if (track == null || streamIds == null) {
977 throw new NullPointerException("No MediaStreamTrack specified in addTrack.");
978 }
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200979 RtpSender newSender = nativeAddTrack(track.getNativeMediaStreamTrack(), streamIds);
Seth Hampsonc384e142018-03-06 15:47:10 -0800980 if (newSender == null) {
981 throw new IllegalStateException("C++ addTrack failed.");
982 }
983 senders.add(newSender);
984 return newSender;
985 }
986
987 /**
988 * Stops sending media from sender. The sender will still appear in getSenders. Future
989 * calls to createOffer will mark the m section for the corresponding transceiver as
990 * receive only or inactive, as defined in JSEP. Returns true on success.
991 */
992 public boolean removeTrack(RtpSender sender) {
993 if (sender == null) {
994 throw new NullPointerException("No RtpSender specified for removeTrack.");
995 }
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200996 return nativeRemoveTrack(sender.getNativeRtpSender());
Seth Hampsonc384e142018-03-06 15:47:10 -0800997 }
998
999 /**
1000 * Creates a new RtpTransceiver and adds it to the set of transceivers. Adding a
1001 * transceiver will cause future calls to CreateOffer to add a media description
1002 * for the corresponding transceiver.
1003 *
1004 * <p>The initial value of |mid| in the returned transceiver is null. Setting a
1005 * new session description may change it to a non-null value.
1006 *
1007 * <p>https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtransceiver
1008 *
1009 * <p>If a MediaStreamTrack is specified then a transceiver will be added with a
1010 * sender set to transmit the given track. The kind
1011 * of the transceiver (and sender/receiver) will be derived from the kind of
1012 * the track.
1013 *
1014 * <p>If MediaType is specified then a transceiver will be added based upon that type.
1015 * This can be either MEDIA_TYPE_AUDIO or MEDIA_TYPE_VIDEO.
1016 *
1017 * <p>Optionally, an RtpTransceiverInit structure can be specified to configure
1018 * the transceiver from construction. If not specified, the transceiver will
1019 * default to having a direction of kSendRecv and not be part of any streams.
1020 *
1021 * <p>Note: These methods are only available with SdpSemantics.UNIFIED_PLAN specified.
1022 * @throws IllegalStateException if an error accors in C++ addTransceiver
1023 */
1024 public RtpTransceiver addTransceiver(MediaStreamTrack track) {
1025 return addTransceiver(track, new RtpTransceiver.RtpTransceiverInit());
1026 }
1027
1028 public RtpTransceiver addTransceiver(
Sami Kalliomäkie7592d82018-03-22 13:32:44 +01001029 MediaStreamTrack track, @Nullable RtpTransceiver.RtpTransceiverInit init) {
Seth Hampsonc384e142018-03-06 15:47:10 -08001030 if (track == null) {
1031 throw new NullPointerException("No MediaStreamTrack specified for addTransceiver.");
1032 }
1033 if (init == null) {
1034 init = new RtpTransceiver.RtpTransceiverInit();
1035 }
Sami Kalliomäkiee05e902018-09-28 14:38:21 +02001036 RtpTransceiver newTransceiver =
1037 nativeAddTransceiverWithTrack(track.getNativeMediaStreamTrack(), init);
Seth Hampsonc384e142018-03-06 15:47:10 -08001038 if (newTransceiver == null) {
1039 throw new IllegalStateException("C++ addTransceiver failed.");
1040 }
1041 transceivers.add(newTransceiver);
1042 return newTransceiver;
1043 }
1044
1045 public RtpTransceiver addTransceiver(MediaStreamTrack.MediaType mediaType) {
1046 return addTransceiver(mediaType, new RtpTransceiver.RtpTransceiverInit());
1047 }
1048
1049 public RtpTransceiver addTransceiver(
Sami Kalliomäkie7592d82018-03-22 13:32:44 +01001050 MediaStreamTrack.MediaType mediaType, @Nullable RtpTransceiver.RtpTransceiverInit init) {
Seth Hampsonc384e142018-03-06 15:47:10 -08001051 if (mediaType == null) {
1052 throw new NullPointerException("No MediaType specified for addTransceiver.");
1053 }
1054 if (init == null) {
1055 init = new RtpTransceiver.RtpTransceiverInit();
1056 }
1057 RtpTransceiver newTransceiver = nativeAddTransceiverOfType(mediaType, init);
1058 if (newTransceiver == null) {
1059 throw new IllegalStateException("C++ addTransceiver failed.");
1060 }
1061 transceivers.add(newTransceiver);
1062 return newTransceiver;
1063 }
1064
deadbeef82215872017-04-18 10:27:51 -07001065 // Older, non-standard implementation of getStats.
1066 @Deprecated
Sami Kalliomäkie7592d82018-03-22 13:32:44 +01001067 public boolean getStats(StatsObserver observer, @Nullable MediaStreamTrack track) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +02001068 return nativeOldGetStats(observer, (track == null) ? 0 : track.getNativeMediaStreamTrack());
deadbeef82215872017-04-18 10:27:51 -07001069 }
1070
Seth Hampsonc384e142018-03-06 15:47:10 -08001071 /**
1072 * Gets stats using the new stats collection API, see webrtc/api/stats/. These
1073 * will replace old stats collection API when the new API has matured enough.
1074 */
deadbeef82215872017-04-18 10:27:51 -07001075 public void getStats(RTCStatsCollectorCallback callback) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001076 nativeNewGetStats(callback);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077 }
1078
Seth Hampsonc384e142018-03-06 15:47:10 -08001079 /**
1080 * Limits the bandwidth allocated for all RTP streams sent by this
1081 * PeerConnection. Pass null to leave a value unchanged.
1082 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001083 public boolean setBitrate(Integer min, Integer current, Integer max) {
1084 return nativeSetBitrate(min, current, max);
1085 }
zsteind89b0bc2017-08-03 11:11:40 -07001086
Seth Hampsonc384e142018-03-06 15:47:10 -08001087 /**
1088 * Starts recording an RTC event log.
1089 *
1090 * Ownership of the file is transfered to the native code. If an RTC event
1091 * log is already being recorded, it will be stopped and a new one will start
1092 * using the provided file. Logging will continue until the stopRtcEventLog
1093 * function is called. The max_size_bytes argument is ignored, it is added
1094 * for future use.
1095 */
ivoc0c6f0f62016-07-06 04:34:23 -07001096 public boolean startRtcEventLog(int file_descriptor, int max_size_bytes) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001097 return nativeStartRtcEventLog(file_descriptor, max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -07001098 }
1099
Seth Hampsonc384e142018-03-06 15:47:10 -08001100 /**
1101 * Stops recording an RTC event log. If no RTC event log is currently being
1102 * recorded, this call will have no effect.
1103 */
ivoc14d5dbe2016-07-04 07:06:55 -07001104 public void stopRtcEventLog() {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001105 nativeStopRtcEventLog();
ivoc14d5dbe2016-07-04 07:06:55 -07001106 }
1107
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001108 // TODO(fischman): add support for DTMF-related methods once that API
1109 // stabilizes.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001110 public SignalingState signalingState() {
1111 return nativeSignalingState();
1112 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001114 public IceConnectionState iceConnectionState() {
1115 return nativeIceConnectionState();
1116 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001117
Jonas Olssonf01d8c82018-11-08 15:19:04 +01001118 public PeerConnectionState connectionState() {
1119 return nativeConnectionState();
1120 }
1121
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001122 public IceGatheringState iceGatheringState() {
1123 return nativeIceGatheringState();
1124 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001125
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001126 public void close() {
1127 nativeClose();
1128 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129
deadbeef43697f62017-09-12 10:52:14 -07001130 /**
1131 * Free native resources associated with this PeerConnection instance.
Seth Hampsonc384e142018-03-06 15:47:10 -08001132 *
deadbeef43697f62017-09-12 10:52:14 -07001133 * This method removes a reference count from the C++ PeerConnection object,
1134 * which should result in it being destroyed. It also calls equivalent
1135 * "dispose" methods on the Java objects attached to this PeerConnection
1136 * (streams, senders, receivers), such that their associated C++ objects
1137 * will also be destroyed.
Seth Hampsonc384e142018-03-06 15:47:10 -08001138 *
1139 * <p>Note that this method cannot be safely called from an observer callback
deadbeef43697f62017-09-12 10:52:14 -07001140 * (PeerConnection.Observer, DataChannel.Observer, etc.). If you want to, for
1141 * example, destroy the PeerConnection after an "ICE failed" callback, you
1142 * must do this asynchronously (in other words, unwind the stack first). See
1143 * <a href="https://bugs.chromium.org/p/webrtc/issues/detail?id=3721">bug
1144 * 3721</a> for more details.
1145 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001146 public void dispose() {
1147 close();
1148 for (MediaStream stream : localStreams) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +02001149 nativeRemoveLocalStream(stream.getNativeMediaStream());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 stream.dispose();
1151 }
1152 localStreams.clear();
deadbeef4139c0f2015-10-06 12:29:25 -07001153 for (RtpSender sender : senders) {
1154 sender.dispose();
1155 }
1156 senders.clear();
1157 for (RtpReceiver receiver : receivers) {
1158 receiver.dispose();
1159 }
Seth Hampsonc384e142018-03-06 15:47:10 -08001160 for (RtpTransceiver transceiver : transceivers) {
1161 transceiver.dispose();
1162 }
1163 transceivers.clear();
deadbeef4139c0f2015-10-06 12:29:25 -07001164 receivers.clear();
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001165 nativeFreeOwnedPeerConnection(nativePeerConnection);
1166 }
1167
1168 /** Returns a pointer to the native webrtc::PeerConnectionInterface. */
1169 public long getNativePeerConnection() {
1170 return nativeGetNativePeerConnection();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171 }
1172
Magnus Jedvert9060eb12017-12-12 12:52:54 +01001173 @CalledByNative
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001174 long getNativeOwnedPeerConnection() {
Magnus Jedvert9060eb12017-12-12 12:52:54 +01001175 return nativePeerConnection;
1176 }
1177
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001178 public static long createNativePeerConnectionObserver(Observer observer) {
1179 return nativeCreatePeerConnectionObserver(observer);
1180 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001182 private native long nativeGetNativePeerConnection();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001183 private native SessionDescription nativeGetLocalDescription();
1184 private native SessionDescription nativeGetRemoteDescription();
Michael Iedema02137862018-10-09 15:30:01 +02001185 private native RtcCertificatePem nativeGetCertificate();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001186 private native DataChannel nativeCreateDataChannel(String label, DataChannel.Init init);
1187 private native void nativeCreateOffer(SdpObserver observer, MediaConstraints constraints);
1188 private native void nativeCreateAnswer(SdpObserver observer, MediaConstraints constraints);
1189 private native void nativeSetLocalDescription(SdpObserver observer, SessionDescription sdp);
1190 private native void nativeSetRemoteDescription(SdpObserver observer, SessionDescription sdp);
1191 private native void nativeSetAudioPlayout(boolean playout);
1192 private native void nativeSetAudioRecording(boolean recording);
1193 private native boolean nativeSetBitrate(Integer min, Integer current, Integer max);
1194 private native SignalingState nativeSignalingState();
1195 private native IceConnectionState nativeIceConnectionState();
Jonas Olssonf01d8c82018-11-08 15:19:04 +01001196 private native PeerConnectionState nativeConnectionState();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001197 private native IceGatheringState nativeIceGatheringState();
1198 private native void nativeClose();
1199 private static native long nativeCreatePeerConnectionObserver(Observer observer);
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001200 private static native void nativeFreeOwnedPeerConnection(long ownedPeerConnection);
1201 private native boolean nativeSetConfiguration(RTCConfiguration config);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001202 private native boolean nativeAddIceCandidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203 String sdpMid, int sdpMLineIndex, String iceCandidateSdp);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001204 private native boolean nativeRemoveIceCandidates(final IceCandidate[] candidates);
1205 private native boolean nativeAddLocalStream(long stream);
1206 private native void nativeRemoveLocalStream(long stream);
1207 private native boolean nativeOldGetStats(StatsObserver observer, long nativeTrack);
1208 private native void nativeNewGetStats(RTCStatsCollectorCallback callback);
1209 private native RtpSender nativeCreateSender(String kind, String stream_id);
1210 private native List<RtpSender> nativeGetSenders();
1211 private native List<RtpReceiver> nativeGetReceivers();
Seth Hampsonc384e142018-03-06 15:47:10 -08001212 private native List<RtpTransceiver> nativeGetTransceivers();
1213 private native RtpSender nativeAddTrack(long track, List<String> streamIds);
1214 private native boolean nativeRemoveTrack(long sender);
1215 private native RtpTransceiver nativeAddTransceiverWithTrack(
1216 long track, RtpTransceiver.RtpTransceiverInit init);
1217 private native RtpTransceiver nativeAddTransceiverOfType(
1218 MediaStreamTrack.MediaType mediaType, RtpTransceiver.RtpTransceiverInit init);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001219 private native boolean nativeStartRtcEventLog(int file_descriptor, int max_size_bytes);
1220 private native void nativeStopRtcEventLog();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221}