blob: 23c9ae864ad24fa30180e94971adaad631f085d0 [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
Jonas Olssonf01d8c82018-11-08 15:19:04 +010053 /** Tracks PeerConnectionInterface::PeerConnectionState */
54 public enum PeerConnectionState {
55 NEW,
56 CONNECTING,
57 CONNECTED,
58 DISCONNECTED,
59 FAILED,
60 CLOSED;
61
62 @CalledByNative("PeerConnectionState")
63 static PeerConnectionState fromNativeIndex(int nativeIndex) {
64 return values()[nativeIndex];
65 }
66 }
67
hnsl04833622017-01-09 08:35:45 -080068 /** Tracks PeerConnectionInterface::TlsCertPolicy */
69 public enum TlsCertPolicy {
70 TLS_CERT_POLICY_SECURE,
71 TLS_CERT_POLICY_INSECURE_NO_CHECK,
72 }
73
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 /** Tracks PeerConnectionInterface::SignalingState */
75 public enum SignalingState {
sakalb6760f92016-09-29 04:12:44 -070076 STABLE,
77 HAVE_LOCAL_OFFER,
78 HAVE_LOCAL_PRANSWER,
79 HAVE_REMOTE_OFFER,
80 HAVE_REMOTE_PRANSWER,
Magnus Jedvertba700f62017-12-04 13:43:27 +010081 CLOSED;
82
83 @CalledByNative("SignalingState")
84 static SignalingState fromNativeIndex(int nativeIndex) {
85 return values()[nativeIndex];
86 }
sakalb6760f92016-09-29 04:12:44 -070087 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088
89 /** Java version of PeerConnectionObserver. */
90 public static interface Observer {
91 /** Triggered when the SignalingState changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010092 @CalledByNative("Observer") void onSignalingChange(SignalingState newState);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093
94 /** Triggered when the IceConnectionState changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010095 @CalledByNative("Observer") void onIceConnectionChange(IceConnectionState newState);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096
Jonas Olssonf01d8c82018-11-08 15:19:04 +010097 /** Triggered when the PeerConnectionState changes. */
98 @CalledByNative("Observer")
99 default void onConnectionChange(PeerConnectionState newState) {}
100
Peter Thatcher54360512015-07-08 11:08:35 -0700101 /** Triggered when the ICE connection receiving status changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100102 @CalledByNative("Observer") void onIceConnectionReceivingChange(boolean receiving);
Peter Thatcher54360512015-07-08 11:08:35 -0700103
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 /** Triggered when the IceGatheringState changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100105 @CalledByNative("Observer") void onIceGatheringChange(IceGatheringState newState);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106
107 /** Triggered when a new ICE candidate has been found. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100108 @CalledByNative("Observer") void onIceCandidate(IceCandidate candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700110 /** Triggered when some ICE candidates have been removed. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100111 @CalledByNative("Observer") void onIceCandidatesRemoved(IceCandidate[] candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700112
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 /** Triggered when media is received on a new stream from remote peer. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100114 @CalledByNative("Observer") void onAddStream(MediaStream stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115
116 /** Triggered when a remote peer close a stream. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100117 @CalledByNative("Observer") void onRemoveStream(MediaStream stream);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000118
119 /** Triggered when a remote peer opens a DataChannel. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100120 @CalledByNative("Observer") void onDataChannel(DataChannel dataChannel);
fischman@webrtc.orgd7568a02014-01-13 22:04:12 +0000121
122 /** Triggered when renegotiation is necessary. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100123 @CalledByNative("Observer") void onRenegotiationNeeded();
zhihuangdcccda72016-12-21 14:08:03 -0800124
125 /**
126 * Triggered when a new track is signaled by the remote peer, as a result of
127 * setRemoteDescription.
128 */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100129 @CalledByNative("Observer") void onAddTrack(RtpReceiver receiver, MediaStream[] mediaStreams);
Seth Hampson31dbc242018-05-07 09:28:19 -0700130
131 /**
132 * Triggered when the signaling from SetRemoteDescription indicates that a transceiver
133 * will be receiving media from a remote endpoint. This is only called if UNIFIED_PLAN
134 * semantics are specified. The transceiver will be disposed automatically.
135 */
136 @CalledByNative("Observer") default void onTrack(RtpTransceiver transceiver){};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 }
138
139 /** Java version of PeerConnectionInterface.IceServer. */
140 public static class IceServer {
Emad Omaradab1d2d2017-06-16 15:43:11 -0700141 // List of URIs associated with this server. Valid formats are described
142 // in RFC7064 and RFC7065, and more may be added in the future. The "host"
143 // part of the URI may contain either an IP address or a hostname.
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700144 @Deprecated public final String uri;
145 public final List<String> urls;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 public final String username;
147 public final String password;
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000148 public final TlsCertPolicy tlsCertPolicy;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149
Emad Omaradab1d2d2017-06-16 15:43:11 -0700150 // If the URIs in |urls| only contain IP addresses, this field can be used
151 // to indicate the hostname, which may be necessary for TLS (using the SNI
152 // extension). If |urls| itself contains the hostname, this isn't
153 // necessary.
154 public final String hostname;
155
Diogo Real1dca9d52017-08-29 12:18:32 -0700156 // List of protocols to be used in the TLS ALPN extension.
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000157 public final List<String> tlsAlpnProtocols;
Diogo Real1dca9d52017-08-29 12:18:32 -0700158
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700159 // List of elliptic curves to be used in the TLS elliptic curves extension.
160 // Only curve names supported by OpenSSL should be used (eg. "P-256","X25519").
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000161 public final List<String> tlsEllipticCurves;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700162
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 /** Convenience constructor for STUN servers. */
Diogo Real05ea2b32017-08-31 00:12:58 -0700164 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 public IceServer(String uri) {
166 this(uri, "", "");
167 }
168
Diogo Real05ea2b32017-08-31 00:12:58 -0700169 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170 public IceServer(String uri, String username, String password) {
hnsl04833622017-01-09 08:35:45 -0800171 this(uri, username, password, TlsCertPolicy.TLS_CERT_POLICY_SECURE);
172 }
173
Diogo Real05ea2b32017-08-31 00:12:58 -0700174 @Deprecated
hnsl04833622017-01-09 08:35:45 -0800175 public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy) {
Emad Omaradab1d2d2017-06-16 15:43:11 -0700176 this(uri, username, password, tlsCertPolicy, "");
177 }
178
Diogo Real05ea2b32017-08-31 00:12:58 -0700179 @Deprecated
Emad Omaradab1d2d2017-06-16 15:43:11 -0700180 public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy,
181 String hostname) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700182 this(uri, Collections.singletonList(uri), username, password, tlsCertPolicy, hostname, null,
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000183 null);
Diogo Real1dca9d52017-08-29 12:18:32 -0700184 }
185
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700186 private IceServer(String uri, List<String> urls, String username, String password,
187 TlsCertPolicy tlsCertPolicy, String hostname, List<String> tlsAlpnProtocols,
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000188 List<String> tlsEllipticCurves) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700189 if (uri == null || urls == null || urls.isEmpty()) {
190 throw new IllegalArgumentException("uri == null || urls == null || urls.isEmpty()");
191 }
192 for (String it : urls) {
193 if (it == null) {
194 throw new IllegalArgumentException("urls element is null: " + urls);
195 }
196 }
197 if (username == null) {
198 throw new IllegalArgumentException("username == null");
199 }
200 if (password == null) {
201 throw new IllegalArgumentException("password == null");
202 }
203 if (hostname == null) {
204 throw new IllegalArgumentException("hostname == null");
205 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 this.uri = uri;
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700207 this.urls = urls;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 this.username = username;
209 this.password = password;
hnsl04833622017-01-09 08:35:45 -0800210 this.tlsCertPolicy = tlsCertPolicy;
Emad Omaradab1d2d2017-06-16 15:43:11 -0700211 this.hostname = hostname;
Diogo Real1dca9d52017-08-29 12:18:32 -0700212 this.tlsAlpnProtocols = tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700213 this.tlsEllipticCurves = tlsEllipticCurves;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 }
215
Sami Kalliomäkibde473e2017-10-30 13:34:41 +0100216 @Override
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 public String toString() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700218 return urls + " [" + username + ":" + password + "] [" + tlsCertPolicy + "] [" + hostname
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000219 + "] [" + tlsAlpnProtocols + "] [" + tlsEllipticCurves + "]";
Diogo Real1dca9d52017-08-29 12:18:32 -0700220 }
221
222 public static Builder builder(String uri) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700223 return new Builder(Collections.singletonList(uri));
224 }
225
226 public static Builder builder(List<String> urls) {
227 return new Builder(urls);
Diogo Real1dca9d52017-08-29 12:18:32 -0700228 }
229
230 public static class Builder {
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100231 @Nullable private final List<String> urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700232 private String username = "";
233 private String password = "";
234 private TlsCertPolicy tlsCertPolicy = TlsCertPolicy.TLS_CERT_POLICY_SECURE;
235 private String hostname = "";
236 private List<String> tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700237 private List<String> tlsEllipticCurves;
Diogo Real1dca9d52017-08-29 12:18:32 -0700238
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700239 private Builder(List<String> urls) {
240 if (urls == null || urls.isEmpty()) {
241 throw new IllegalArgumentException("urls == null || urls.isEmpty(): " + urls);
242 }
243 this.urls = urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700244 }
245
246 public Builder setUsername(String username) {
247 this.username = username;
248 return this;
249 }
250
251 public Builder setPassword(String password) {
252 this.password = password;
253 return this;
254 }
255
256 public Builder setTlsCertPolicy(TlsCertPolicy tlsCertPolicy) {
257 this.tlsCertPolicy = tlsCertPolicy;
258 return this;
259 }
260
261 public Builder setHostname(String hostname) {
262 this.hostname = hostname;
263 return this;
264 }
265
266 public Builder setTlsAlpnProtocols(List<String> tlsAlpnProtocols) {
267 this.tlsAlpnProtocols = tlsAlpnProtocols;
268 return this;
269 }
270
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700271 public Builder setTlsEllipticCurves(List<String> tlsEllipticCurves) {
272 this.tlsEllipticCurves = tlsEllipticCurves;
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,
Sergey Silkin9c147dd2018-09-12 10:45:38 +0000278 tlsAlpnProtocols, tlsEllipticCurves);
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 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 }
321
Jiayang Liucac1b382015-04-30 12:35:24 -0700322 /** Java version of PeerConnectionInterface.IceTransportsType */
sakalb6760f92016-09-29 04:12:44 -0700323 public enum IceTransportsType { NONE, RELAY, NOHOST, ALL }
Jiayang Liucac1b382015-04-30 12:35:24 -0700324
325 /** Java version of PeerConnectionInterface.BundlePolicy */
sakalb6760f92016-09-29 04:12:44 -0700326 public enum BundlePolicy { BALANCED, MAXBUNDLE, MAXCOMPAT }
Jiayang Liucac1b382015-04-30 12:35:24 -0700327
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700328 /** Java version of PeerConnectionInterface.RtcpMuxPolicy */
sakalb6760f92016-09-29 04:12:44 -0700329 public enum RtcpMuxPolicy { NEGOTIATE, REQUIRE }
glaznev97579a42015-09-01 11:31:27 -0700330
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700331 /** Java version of PeerConnectionInterface.TcpCandidatePolicy */
sakalb6760f92016-09-29 04:12:44 -0700332 public enum TcpCandidatePolicy { ENABLED, DISABLED }
Jiayang Liucac1b382015-04-30 12:35:24 -0700333
honghaiz60347052016-05-31 18:29:12 -0700334 /** Java version of PeerConnectionInterface.CandidateNetworkPolicy */
sakalb6760f92016-09-29 04:12:44 -0700335 public enum CandidateNetworkPolicy { ALL, LOW_COST }
honghaiz60347052016-05-31 18:29:12 -0700336
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800337 // Keep in sync with webrtc/rtc_base/network_constants.h.
338 public enum AdapterType {
339 UNKNOWN,
340 ETHERNET,
341 WIFI,
342 CELLULAR,
343 VPN,
344 LOOPBACK,
345 }
346
glaznev97579a42015-09-01 11:31:27 -0700347 /** Java version of rtc::KeyType */
sakalb6760f92016-09-29 04:12:44 -0700348 public enum KeyType { RSA, ECDSA }
glaznev97579a42015-09-01 11:31:27 -0700349
honghaiz1f429e32015-09-28 07:57:34 -0700350 /** Java version of PeerConnectionInterface.ContinualGatheringPolicy */
sakalb6760f92016-09-29 04:12:44 -0700351 public enum ContinualGatheringPolicy { GATHER_ONCE, GATHER_CONTINUALLY }
honghaiz1f429e32015-09-28 07:57:34 -0700352
Steve Antond960a0c2017-07-17 12:33:07 -0700353 /** Java version of rtc::IntervalRange */
354 public static class IntervalRange {
355 private final int min;
356 private final int max;
357
358 public IntervalRange(int min, int max) {
359 this.min = min;
360 this.max = max;
361 }
362
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100363 @CalledByNative("IntervalRange")
Steve Antond960a0c2017-07-17 12:33:07 -0700364 public int getMin() {
365 return min;
366 }
367
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100368 @CalledByNative("IntervalRange")
Steve Antond960a0c2017-07-17 12:33:07 -0700369 public int getMax() {
370 return max;
371 }
372 }
373
Seth Hampsonc384e142018-03-06 15:47:10 -0800374 /**
375 * Java version of webrtc::SdpSemantics.
376 *
377 * Configure the SDP semantics used by this PeerConnection. Note that the
378 * WebRTC 1.0 specification requires UNIFIED_PLAN semantics. The
379 * RtpTransceiver API is only available with UNIFIED_PLAN semantics.
380 *
381 * <p>PLAN_B will cause PeerConnection to create offers and answers with at
382 * most one audio and one video m= section with multiple RtpSenders and
383 * RtpReceivers specified as multiple a=ssrc lines within the section. This
384 * will also cause PeerConnection to ignore all but the first m= section of
385 * the same media type.
386 *
387 * <p>UNIFIED_PLAN will cause PeerConnection to create offers and answers with
388 * multiple m= sections where each m= section maps to one RtpSender and one
389 * RtpReceiver (an RtpTransceiver), either both audio or both video. This
390 * will also cause PeerConnection to ignore all but the first a=ssrc lines
391 * that form a Plan B stream.
392 *
393 * <p>For users who wish to send multiple audio/video streams and need to stay
394 * interoperable with legacy WebRTC implementations, specify PLAN_B.
395 *
396 * <p>For users who wish to send multiple audio/video streams and/or wish to
397 * use the new RtpTransceiver API, specify UNIFIED_PLAN.
398 */
399 public enum SdpSemantics { PLAN_B, UNIFIED_PLAN }
400
Jiayang Liucac1b382015-04-30 12:35:24 -0700401 /** Java version of PeerConnectionInterface.RTCConfiguration */
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800402 // TODO(qingsi): Resolve the naming inconsistency of fields with/without units.
Jiayang Liucac1b382015-04-30 12:35:24 -0700403 public static class RTCConfiguration {
404 public IceTransportsType iceTransportsType;
405 public List<IceServer> iceServers;
406 public BundlePolicy bundlePolicy;
Michael Iedema02137862018-10-09 15:30:01 +0200407 @Nullable public RtcCertificatePem certificate;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700408 public RtcpMuxPolicy rtcpMuxPolicy;
Jiayang Liucac1b382015-04-30 12:35:24 -0700409 public TcpCandidatePolicy tcpCandidatePolicy;
honghaiz60347052016-05-31 18:29:12 -0700410 public CandidateNetworkPolicy candidateNetworkPolicy;
Henrik Lundin64dad832015-05-11 12:44:23 +0200411 public int audioJitterBufferMaxPackets;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200412 public boolean audioJitterBufferFastAccelerate;
honghaiz4edc39c2015-09-01 09:53:56 -0700413 public int iceConnectionReceivingTimeout;
Honghai Zhang381b4212015-12-04 12:24:03 -0800414 public int iceBackupCandidatePairPingInterval;
glaznev97579a42015-09-01 11:31:27 -0700415 public KeyType keyType;
honghaiz1f429e32015-09-28 07:57:34 -0700416 public ContinualGatheringPolicy continualGatheringPolicy;
deadbeefbe0c96f2016-05-18 16:20:14 -0700417 public int iceCandidatePoolSize;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700418 public boolean pruneTurnPorts;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700419 public boolean presumeWritableWhenFullyRelayed;
Qingsi Wange6826d22018-03-08 14:55:14 -0800420 // The following fields define intervals in milliseconds at which ICE
421 // connectivity checks are sent.
422 //
423 // We consider ICE is "strongly connected" for an agent when there is at
424 // least one candidate pair that currently succeeds in connectivity check
425 // from its direction i.e. sending a ping and receives a ping response, AND
426 // all candidate pairs have sent a minimum number of pings for connectivity
427 // (this number is implementation-specific). Otherwise, ICE is considered in
428 // "weak connectivity".
429 //
430 // Note that the above notion of strong and weak connectivity is not defined
431 // in RFC 5245, and they apply to our current ICE implementation only.
432 //
433 // 1) iceCheckIntervalStrongConnectivityMs defines the interval applied to
434 // ALL candidate pairs when ICE is strongly connected,
435 // 2) iceCheckIntervalWeakConnectivityMs defines the counterpart for ALL
436 // pairs when ICE is weakly connected, and
437 // 3) iceCheckMinInterval defines the minimal interval (equivalently the
438 // maximum rate) that overrides the above two intervals when either of them
439 // is less.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100440 @Nullable public Integer iceCheckIntervalStrongConnectivityMs;
441 @Nullable public Integer iceCheckIntervalWeakConnectivityMs;
442 @Nullable public Integer iceCheckMinInterval;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700443 // The time period in milliseconds for which a candidate pair must wait for response to
444 // connectivitiy checks before it becomes unwritable.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100445 @Nullable public Integer iceUnwritableTimeMs;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700446 // The minimum number of connectivity checks that a candidate pair must sent without receiving
447 // response before it becomes unwritable.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100448 @Nullable public Integer iceUnwritableMinChecks;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800449 // The interval in milliseconds at which STUN candidates will resend STUN binding requests
450 // to keep NAT bindings open.
451 // The default value in the implementation is used if this field is null.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100452 @Nullable public Integer stunCandidateKeepaliveIntervalMs;
zhihuangb09b3f92017-03-07 14:40:51 -0800453 public boolean disableIPv6OnWifi;
deadbeef28e29192017-07-27 09:14:38 -0700454 // By default, PeerConnection will use a limited number of IPv6 network
455 // interfaces, in order to avoid too many ICE candidate pairs being created
456 // and delaying ICE completion.
457 //
458 // Can be set to Integer.MAX_VALUE to effectively disable the limit.
459 public int maxIPv6Networks;
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100460 @Nullable public IntervalRange iceRegatherIntervalRange;
Jiayang Liucac1b382015-04-30 12:35:24 -0700461
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100462 // These values will be overridden by MediaStream constraints if deprecated constraints-based
463 // create peerconnection interface is used.
464 public boolean disableIpv6;
465 public boolean enableDscp;
466 public boolean enableCpuOveruseDetection;
467 public boolean enableRtpDataChannel;
468 public boolean suspendBelowMinBitrate;
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100469 @Nullable public Integer screencastMinBitrate;
470 @Nullable public Boolean combinedAudioVideoBwe;
471 @Nullable public Boolean enableDtlsSrtp;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800472 // Use "Unknown" to represent no preference of adapter types, not the
473 // preference of adapters of unknown types.
474 public AdapterType networkPreference;
Seth Hampsonc384e142018-03-06 15:47:10 -0800475 public SdpSemantics sdpSemantics;
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100476
Jonas Orelandbdcee282017-10-10 14:01:40 +0200477 // This is an optional wrapper for the C++ webrtc::TurnCustomizer.
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100478 @Nullable public TurnCustomizer turnCustomizer;
Jonas Orelandbdcee282017-10-10 14:01:40 +0200479
Zhi Huangb57e1692018-06-12 11:41:11 -0700480 // Actively reset the SRTP parameters whenever the DTLS transports underneath are reset for
481 // every offer/answer negotiation.This is only intended to be a workaround for crbug.com/835958
482 public boolean activeResetSrtpParams;
483
Piotr (Peter) Slatala09beff22018-10-17 07:22:40 -0700484 /*
485 * Experimental flag that enables a use of media transport. If this is true, the media transport
486 * factory MUST be provided to the PeerConnectionFactory.
487 */
488 public boolean useMediaTransport;
489
Bjorn Mellema9bbd862018-11-02 09:07:48 -0700490 /*
491 * Experimental flag that enables a use of media transport for data channels. If this is true,
492 * the media transport factory MUST be provided to the PeerConnectionFactory.
493 */
494 public boolean useMediaTransportForDataChannels;
495
Benjamin Wright8c27cca2018-10-25 10:16:44 -0700496 /**
497 * Defines advanced optional cryptographic settings related to SRTP and
498 * frame encryption for native WebRTC. Setting this will overwrite any
499 * options set through the PeerConnectionFactory (which is deprecated).
500 */
501 @Nullable public CryptoOptions cryptoOptions;
502
deadbeef28e29192017-07-27 09:14:38 -0700503 // TODO(deadbeef): Instead of duplicating the defaults here, we should do
504 // something to pick up the defaults from C++. The Objective-C equivalent
505 // of RTCConfiguration does that.
Jiayang Liucac1b382015-04-30 12:35:24 -0700506 public RTCConfiguration(List<IceServer> iceServers) {
507 iceTransportsType = IceTransportsType.ALL;
508 bundlePolicy = BundlePolicy.BALANCED;
zhihuang4dfb8ce2016-11-23 10:30:12 -0800509 rtcpMuxPolicy = RtcpMuxPolicy.REQUIRE;
Jiayang Liucac1b382015-04-30 12:35:24 -0700510 tcpCandidatePolicy = TcpCandidatePolicy.ENABLED;
Sami Kalliomäki9828beb2017-10-26 16:21:22 +0200511 candidateNetworkPolicy = CandidateNetworkPolicy.ALL;
Jiayang Liucac1b382015-04-30 12:35:24 -0700512 this.iceServers = iceServers;
Henrik Lundin64dad832015-05-11 12:44:23 +0200513 audioJitterBufferMaxPackets = 50;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200514 audioJitterBufferFastAccelerate = false;
honghaiz4edc39c2015-09-01 09:53:56 -0700515 iceConnectionReceivingTimeout = -1;
Honghai Zhang381b4212015-12-04 12:24:03 -0800516 iceBackupCandidatePairPingInterval = -1;
glaznev97579a42015-09-01 11:31:27 -0700517 keyType = KeyType.ECDSA;
honghaiz1f429e32015-09-28 07:57:34 -0700518 continualGatheringPolicy = ContinualGatheringPolicy.GATHER_ONCE;
deadbeefbe0c96f2016-05-18 16:20:14 -0700519 iceCandidatePoolSize = 0;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700520 pruneTurnPorts = false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700521 presumeWritableWhenFullyRelayed = false;
Qingsi Wange6826d22018-03-08 14:55:14 -0800522 iceCheckIntervalStrongConnectivityMs = null;
523 iceCheckIntervalWeakConnectivityMs = null;
skvlad51072462017-02-02 11:50:14 -0800524 iceCheckMinInterval = null;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700525 iceUnwritableTimeMs = null;
526 iceUnwritableMinChecks = null;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800527 stunCandidateKeepaliveIntervalMs = null;
zhihuangb09b3f92017-03-07 14:40:51 -0800528 disableIPv6OnWifi = false;
deadbeef28e29192017-07-27 09:14:38 -0700529 maxIPv6Networks = 5;
Steve Antond960a0c2017-07-17 12:33:07 -0700530 iceRegatherIntervalRange = null;
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100531 disableIpv6 = false;
532 enableDscp = false;
533 enableCpuOveruseDetection = true;
534 enableRtpDataChannel = false;
535 suspendBelowMinBitrate = false;
536 screencastMinBitrate = null;
537 combinedAudioVideoBwe = null;
538 enableDtlsSrtp = null;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800539 networkPreference = AdapterType.UNKNOWN;
Seth Hampsonc384e142018-03-06 15:47:10 -0800540 sdpSemantics = SdpSemantics.PLAN_B;
Zhi Huangb57e1692018-06-12 11:41:11 -0700541 activeResetSrtpParams = false;
Piotr (Peter) Slatala09beff22018-10-17 07:22:40 -0700542 useMediaTransport = false;
Bjorn Mellema9bbd862018-11-02 09:07:48 -0700543 useMediaTransportForDataChannels = false;
Benjamin Wright8c27cca2018-10-25 10:16:44 -0700544 cryptoOptions = null;
Jiayang Liucac1b382015-04-30 12:35:24 -0700545 }
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100546
547 @CalledByNative("RTCConfiguration")
548 IceTransportsType getIceTransportsType() {
549 return iceTransportsType;
550 }
551
552 @CalledByNative("RTCConfiguration")
553 List<IceServer> getIceServers() {
554 return iceServers;
555 }
556
557 @CalledByNative("RTCConfiguration")
558 BundlePolicy getBundlePolicy() {
559 return bundlePolicy;
560 }
561
Michael Iedema02137862018-10-09 15:30:01 +0200562 @Nullable
563 @CalledByNative("RTCConfiguration")
564 RtcCertificatePem getCertificate() {
565 return certificate;
566 }
567
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100568 @CalledByNative("RTCConfiguration")
569 RtcpMuxPolicy getRtcpMuxPolicy() {
570 return rtcpMuxPolicy;
571 }
572
573 @CalledByNative("RTCConfiguration")
574 TcpCandidatePolicy getTcpCandidatePolicy() {
575 return tcpCandidatePolicy;
576 }
577
578 @CalledByNative("RTCConfiguration")
579 CandidateNetworkPolicy getCandidateNetworkPolicy() {
580 return candidateNetworkPolicy;
581 }
582
583 @CalledByNative("RTCConfiguration")
584 int getAudioJitterBufferMaxPackets() {
585 return audioJitterBufferMaxPackets;
586 }
587
588 @CalledByNative("RTCConfiguration")
589 boolean getAudioJitterBufferFastAccelerate() {
590 return audioJitterBufferFastAccelerate;
591 }
592
593 @CalledByNative("RTCConfiguration")
594 int getIceConnectionReceivingTimeout() {
595 return iceConnectionReceivingTimeout;
596 }
597
598 @CalledByNative("RTCConfiguration")
599 int getIceBackupCandidatePairPingInterval() {
600 return iceBackupCandidatePairPingInterval;
601 }
602
603 @CalledByNative("RTCConfiguration")
604 KeyType getKeyType() {
605 return keyType;
606 }
607
608 @CalledByNative("RTCConfiguration")
609 ContinualGatheringPolicy getContinualGatheringPolicy() {
610 return continualGatheringPolicy;
611 }
612
613 @CalledByNative("RTCConfiguration")
614 int getIceCandidatePoolSize() {
615 return iceCandidatePoolSize;
616 }
617
618 @CalledByNative("RTCConfiguration")
619 boolean getPruneTurnPorts() {
620 return pruneTurnPorts;
621 }
622
623 @CalledByNative("RTCConfiguration")
624 boolean getPresumeWritableWhenFullyRelayed() {
625 return presumeWritableWhenFullyRelayed;
626 }
627
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100628 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100629 @CalledByNative("RTCConfiguration")
Qingsi Wange6826d22018-03-08 14:55:14 -0800630 Integer getIceCheckIntervalStrongConnectivity() {
631 return iceCheckIntervalStrongConnectivityMs;
632 }
633
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100634 @Nullable
Qingsi Wange6826d22018-03-08 14:55:14 -0800635 @CalledByNative("RTCConfiguration")
636 Integer getIceCheckIntervalWeakConnectivity() {
637 return iceCheckIntervalWeakConnectivityMs;
638 }
639
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100640 @Nullable
Qingsi Wange6826d22018-03-08 14:55:14 -0800641 @CalledByNative("RTCConfiguration")
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100642 Integer getIceCheckMinInterval() {
643 return iceCheckMinInterval;
644 }
645
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100646 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100647 @CalledByNative("RTCConfiguration")
Qingsi Wang22e623a2018-03-13 10:53:57 -0700648 Integer getIceUnwritableTimeout() {
649 return iceUnwritableTimeMs;
650 }
651
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100652 @Nullable
Qingsi Wang22e623a2018-03-13 10:53:57 -0700653 @CalledByNative("RTCConfiguration")
654 Integer getIceUnwritableMinChecks() {
655 return iceUnwritableMinChecks;
656 }
657
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100658 @Nullable
Qingsi Wang22e623a2018-03-13 10:53:57 -0700659 @CalledByNative("RTCConfiguration")
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800660 Integer getStunCandidateKeepaliveInterval() {
661 return stunCandidateKeepaliveIntervalMs;
662 }
663
664 @CalledByNative("RTCConfiguration")
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100665 boolean getDisableIPv6OnWifi() {
666 return disableIPv6OnWifi;
667 }
668
669 @CalledByNative("RTCConfiguration")
670 int getMaxIPv6Networks() {
671 return maxIPv6Networks;
672 }
673
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100674 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100675 @CalledByNative("RTCConfiguration")
676 IntervalRange getIceRegatherIntervalRange() {
677 return iceRegatherIntervalRange;
678 }
679
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100680 @Nullable
Magnus Jedvert9060eb12017-12-12 12:52:54 +0100681 @CalledByNative("RTCConfiguration")
682 TurnCustomizer getTurnCustomizer() {
683 return turnCustomizer;
684 }
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100685
686 @CalledByNative("RTCConfiguration")
687 boolean getDisableIpv6() {
688 return disableIpv6;
689 }
690
691 @CalledByNative("RTCConfiguration")
692 boolean getEnableDscp() {
693 return enableDscp;
694 }
695
696 @CalledByNative("RTCConfiguration")
697 boolean getEnableCpuOveruseDetection() {
698 return enableCpuOveruseDetection;
699 }
700
701 @CalledByNative("RTCConfiguration")
702 boolean getEnableRtpDataChannel() {
703 return enableRtpDataChannel;
704 }
705
706 @CalledByNative("RTCConfiguration")
707 boolean getSuspendBelowMinBitrate() {
708 return suspendBelowMinBitrate;
709 }
710
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100711 @Nullable
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100712 @CalledByNative("RTCConfiguration")
713 Integer getScreencastMinBitrate() {
714 return screencastMinBitrate;
715 }
716
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100717 @Nullable
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100718 @CalledByNative("RTCConfiguration")
719 Boolean getCombinedAudioVideoBwe() {
720 return combinedAudioVideoBwe;
721 }
722
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100723 @Nullable
Sami Kalliomäkie8b26cd2017-12-19 12:51:53 +0100724 @CalledByNative("RTCConfiguration")
725 Boolean getEnableDtlsSrtp() {
726 return enableDtlsSrtp;
727 }
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800728
729 @CalledByNative("RTCConfiguration")
730 AdapterType getNetworkPreference() {
731 return networkPreference;
732 }
Seth Hampsonc384e142018-03-06 15:47:10 -0800733
734 @CalledByNative("RTCConfiguration")
735 SdpSemantics getSdpSemantics() {
736 return sdpSemantics;
737 }
Zhi Huangb57e1692018-06-12 11:41:11 -0700738
739 @CalledByNative("RTCConfiguration")
740 boolean getActiveResetSrtpParams() {
741 return activeResetSrtpParams;
742 }
Piotr (Peter) Slatala09beff22018-10-17 07:22:40 -0700743
744 @CalledByNative("RTCConfiguration")
745 boolean getUseMediaTransport() {
746 return useMediaTransport;
747 }
Benjamin Wright8c27cca2018-10-25 10:16:44 -0700748
Bjorn Mellema9bbd862018-11-02 09:07:48 -0700749 @CalledByNative("RTCConfiguration")
750 boolean getUseMediaTransportForDataChannels() {
751 return useMediaTransportForDataChannels;
752 }
753
Benjamin Wright8c27cca2018-10-25 10:16:44 -0700754 @Nullable
755 @CalledByNative("RTCConfiguration")
756 CryptoOptions getCryptoOptions() {
757 return cryptoOptions;
758 }
Jiayang Liucac1b382015-04-30 12:35:24 -0700759 };
760
Magnus Jedvert6062f372017-11-16 16:53:12 +0100761 private final List<MediaStream> localStreams = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 private final long nativePeerConnection;
Magnus Jedvert6062f372017-11-16 16:53:12 +0100763 private List<RtpSender> senders = new ArrayList<>();
764 private List<RtpReceiver> receivers = new ArrayList<>();
Seth Hampsonc384e142018-03-06 15:47:10 -0800765 private List<RtpTransceiver> transceivers = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766
Sami Kalliomäki1ece1ed2017-12-20 11:59:22 +0100767 /**
768 * Wraps a PeerConnection created by the factory. Can be used by clients that want to implement
769 * their PeerConnection creation in JNI.
770 */
771 public PeerConnection(NativePeerConnectionFactory factory) {
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100772 this(factory.createNativePeerConnection());
Sami Kalliomäki1ece1ed2017-12-20 11:59:22 +0100773 }
774
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100775 PeerConnection(long nativePeerConnection) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 this.nativePeerConnection = nativePeerConnection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 }
778
779 // JsepInterface.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100780 public SessionDescription getLocalDescription() {
781 return nativeGetLocalDescription();
782 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100784 public SessionDescription getRemoteDescription() {
785 return nativeGetRemoteDescription();
786 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787
Michael Iedema02137862018-10-09 15:30:01 +0200788 public RtcCertificatePem getCertificate() {
789 return nativeGetCertificate();
790 }
791
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100792 public DataChannel createDataChannel(String label, DataChannel.Init init) {
793 return nativeCreateDataChannel(label, init);
794 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000795
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100796 public void createOffer(SdpObserver observer, MediaConstraints constraints) {
797 nativeCreateOffer(observer, constraints);
798 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100800 public void createAnswer(SdpObserver observer, MediaConstraints constraints) {
801 nativeCreateAnswer(observer, constraints);
802 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100804 public void setLocalDescription(SdpObserver observer, SessionDescription sdp) {
805 nativeSetLocalDescription(observer, sdp);
806 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100808 public void setRemoteDescription(SdpObserver observer, SessionDescription sdp) {
809 nativeSetRemoteDescription(observer, sdp);
810 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811
Seth Hampsonc384e142018-03-06 15:47:10 -0800812 /**
813 * Enables/disables playout of received audio streams. Enabled by default.
814 *
815 * Note that even if playout is enabled, streams will only be played out if
816 * the appropriate SDP is also applied. The main purpose of this API is to
817 * be able to control the exact time when audio playout starts.
818 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100819 public void setAudioPlayout(boolean playout) {
820 nativeSetAudioPlayout(playout);
821 }
henrika5f6bf242017-11-01 11:06:56 +0100822
Seth Hampsonc384e142018-03-06 15:47:10 -0800823 /**
824 * Enables/disables recording of transmitted audio streams. Enabled by default.
825 *
826 * Note that even if recording is enabled, streams will only be recorded if
827 * the appropriate SDP is also applied. The main purpose of this API is to
828 * be able to control the exact time when audio recording starts.
829 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100830 public void setAudioRecording(boolean recording) {
831 nativeSetAudioRecording(recording);
832 }
henrika5f6bf242017-11-01 11:06:56 +0100833
deadbeef5d0b6d82017-01-09 16:05:28 -0800834 public boolean setConfiguration(RTCConfiguration config) {
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +0100835 return nativeSetConfiguration(config);
deadbeef5d0b6d82017-01-09 16:05:28 -0800836 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837
838 public boolean addIceCandidate(IceCandidate candidate) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100839 return nativeAddIceCandidate(candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 }
841
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700842 public boolean removeIceCandidates(final IceCandidate[] candidates) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100843 return nativeRemoveIceCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700844 }
845
Seth Hampsonc384e142018-03-06 15:47:10 -0800846 /**
847 * Adds a new MediaStream to be sent on this peer connection.
848 * Note: This method is not supported with SdpSemantics.UNIFIED_PLAN. Please
849 * use addTrack instead.
850 */
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000851 public boolean addStream(MediaStream stream) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200852 boolean ret = nativeAddLocalStream(stream.getNativeMediaStream());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 if (!ret) {
854 return false;
855 }
856 localStreams.add(stream);
857 return true;
858 }
859
Seth Hampsonc384e142018-03-06 15:47:10 -0800860 /**
861 * Removes the given media stream from this peer connection.
862 * This method is not supported with SdpSemantics.UNIFIED_PLAN. Please use
863 * removeTrack instead.
864 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865 public void removeStream(MediaStream stream) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200866 nativeRemoveLocalStream(stream.getNativeMediaStream());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 localStreams.remove(stream);
868 }
869
deadbeef7a246882017-08-09 08:40:10 -0700870 /**
871 * Creates an RtpSender without a track.
Seth Hampsonc384e142018-03-06 15:47:10 -0800872 *
873 * <p>This method allows an application to cause the PeerConnection to negotiate
deadbeef7a246882017-08-09 08:40:10 -0700874 * sending/receiving a specific media type, but without having a track to
875 * send yet.
Seth Hampsonc384e142018-03-06 15:47:10 -0800876 *
877 * <p>When the application does want to begin sending a track, it can call
deadbeef7a246882017-08-09 08:40:10 -0700878 * RtpSender.setTrack, which doesn't require any additional SDP negotiation.
Seth Hampsonc384e142018-03-06 15:47:10 -0800879 *
880 * <p>Example use:
deadbeef7a246882017-08-09 08:40:10 -0700881 * <pre>
882 * {@code
883 * audioSender = pc.createSender("audio", "stream1");
884 * videoSender = pc.createSender("video", "stream1");
885 * // Do normal SDP offer/answer, which will kick off ICE/DTLS and negotiate
886 * // media parameters....
887 * // Later, when the endpoint is ready to actually begin sending:
888 * audioSender.setTrack(audioTrack, false);
889 * videoSender.setTrack(videoTrack, false);
890 * }
891 * </pre>
Seth Hampsonc384e142018-03-06 15:47:10 -0800892 * <p>Note: This corresponds most closely to "addTransceiver" in the official
deadbeef7a246882017-08-09 08:40:10 -0700893 * WebRTC API, in that it creates a sender without a track. It was
894 * implemented before addTransceiver because it provides useful
895 * functionality, and properly implementing transceivers would have required
896 * a great deal more work.
897 *
Seth Hampsonc384e142018-03-06 15:47:10 -0800898 * <p>Note: This is only available with SdpSemantics.PLAN_B specified. Please use
899 * addTransceiver instead.
900 *
deadbeef7a246882017-08-09 08:40:10 -0700901 * @param kind Corresponds to MediaStreamTrack kinds (must be "audio" or
902 * "video").
903 * @param stream_id The ID of the MediaStream that this sender's track will
904 * be associated with when SDP is applied to the remote
905 * PeerConnection. If createSender is used to create an
906 * audio and video sender that should be synchronized, they
907 * should use the same stream ID.
908 * @return A new RtpSender object if successful, or null otherwise.
909 */
deadbeefbd7d8f72015-12-18 16:58:44 -0800910 public RtpSender createSender(String kind, String stream_id) {
Seth Hampsonc384e142018-03-06 15:47:10 -0800911 RtpSender newSender = nativeCreateSender(kind, stream_id);
912 if (newSender != null) {
913 senders.add(newSender);
deadbeefee524f72015-12-02 11:27:40 -0800914 }
Seth Hampsonc384e142018-03-06 15:47:10 -0800915 return newSender;
deadbeefee524f72015-12-02 11:27:40 -0800916 }
917
Seth Hampsonc384e142018-03-06 15:47:10 -0800918 /**
919 * Gets all RtpSenders associated with this peer connection.
920 * Note that calling getSenders will dispose of the senders previously
921 * returned.
922 */
deadbeef4139c0f2015-10-06 12:29:25 -0700923 public List<RtpSender> getSenders() {
924 for (RtpSender sender : senders) {
925 sender.dispose();
926 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100927 senders = nativeGetSenders();
deadbeef4139c0f2015-10-06 12:29:25 -0700928 return Collections.unmodifiableList(senders);
929 }
930
Seth Hampsonc384e142018-03-06 15:47:10 -0800931 /**
932 * Gets all RtpReceivers associated with this peer connection.
933 * Note that calling getReceivers will dispose of the receivers previously
934 * returned.
935 */
deadbeef4139c0f2015-10-06 12:29:25 -0700936 public List<RtpReceiver> getReceivers() {
937 for (RtpReceiver receiver : receivers) {
938 receiver.dispose();
939 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100940 receivers = nativeGetReceivers();
deadbeef4139c0f2015-10-06 12:29:25 -0700941 return Collections.unmodifiableList(receivers);
942 }
943
Seth Hampsonc384e142018-03-06 15:47:10 -0800944 /**
945 * Gets all RtpTransceivers associated with this peer connection.
946 * Note that calling getTransceivers will dispose of the transceivers previously
947 * returned.
948 * Note: This is only available with SdpSemantics.UNIFIED_PLAN specified.
949 */
950 public List<RtpTransceiver> getTransceivers() {
951 for (RtpTransceiver transceiver : transceivers) {
952 transceiver.dispose();
953 }
954 transceivers = nativeGetTransceivers();
955 return Collections.unmodifiableList(transceivers);
956 }
957
958 /**
959 * Adds a new media stream track to be sent on this peer connection, and returns
960 * the newly created RtpSender. If streamIds are specified, the RtpSender will
961 * be associated with the streams specified in the streamIds list.
962 *
963 * @throws IllegalStateException if an error accors in C++ addTrack.
964 * An error can occur if:
965 * - A sender already exists for the track.
966 * - The peer connection is closed.
967 */
968 public RtpSender addTrack(MediaStreamTrack track) {
969 return addTrack(track, Collections.emptyList());
970 }
971
972 public RtpSender addTrack(MediaStreamTrack track, List<String> streamIds) {
973 if (track == null || streamIds == null) {
974 throw new NullPointerException("No MediaStreamTrack specified in addTrack.");
975 }
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200976 RtpSender newSender = nativeAddTrack(track.getNativeMediaStreamTrack(), streamIds);
Seth Hampsonc384e142018-03-06 15:47:10 -0800977 if (newSender == null) {
978 throw new IllegalStateException("C++ addTrack failed.");
979 }
980 senders.add(newSender);
981 return newSender;
982 }
983
984 /**
985 * Stops sending media from sender. The sender will still appear in getSenders. Future
986 * calls to createOffer will mark the m section for the corresponding transceiver as
987 * receive only or inactive, as defined in JSEP. Returns true on success.
988 */
989 public boolean removeTrack(RtpSender sender) {
990 if (sender == null) {
991 throw new NullPointerException("No RtpSender specified for removeTrack.");
992 }
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200993 return nativeRemoveTrack(sender.getNativeRtpSender());
Seth Hampsonc384e142018-03-06 15:47:10 -0800994 }
995
996 /**
997 * Creates a new RtpTransceiver and adds it to the set of transceivers. Adding a
998 * transceiver will cause future calls to CreateOffer to add a media description
999 * for the corresponding transceiver.
1000 *
1001 * <p>The initial value of |mid| in the returned transceiver is null. Setting a
1002 * new session description may change it to a non-null value.
1003 *
1004 * <p>https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtransceiver
1005 *
1006 * <p>If a MediaStreamTrack is specified then a transceiver will be added with a
1007 * sender set to transmit the given track. The kind
1008 * of the transceiver (and sender/receiver) will be derived from the kind of
1009 * the track.
1010 *
1011 * <p>If MediaType is specified then a transceiver will be added based upon that type.
1012 * This can be either MEDIA_TYPE_AUDIO or MEDIA_TYPE_VIDEO.
1013 *
1014 * <p>Optionally, an RtpTransceiverInit structure can be specified to configure
1015 * the transceiver from construction. If not specified, the transceiver will
1016 * default to having a direction of kSendRecv and not be part of any streams.
1017 *
1018 * <p>Note: These methods are only available with SdpSemantics.UNIFIED_PLAN specified.
1019 * @throws IllegalStateException if an error accors in C++ addTransceiver
1020 */
1021 public RtpTransceiver addTransceiver(MediaStreamTrack track) {
1022 return addTransceiver(track, new RtpTransceiver.RtpTransceiverInit());
1023 }
1024
1025 public RtpTransceiver addTransceiver(
Sami Kalliomäkie7592d82018-03-22 13:32:44 +01001026 MediaStreamTrack track, @Nullable RtpTransceiver.RtpTransceiverInit init) {
Seth Hampsonc384e142018-03-06 15:47:10 -08001027 if (track == null) {
1028 throw new NullPointerException("No MediaStreamTrack specified for addTransceiver.");
1029 }
1030 if (init == null) {
1031 init = new RtpTransceiver.RtpTransceiverInit();
1032 }
Sami Kalliomäkiee05e902018-09-28 14:38:21 +02001033 RtpTransceiver newTransceiver =
1034 nativeAddTransceiverWithTrack(track.getNativeMediaStreamTrack(), init);
Seth Hampsonc384e142018-03-06 15:47:10 -08001035 if (newTransceiver == null) {
1036 throw new IllegalStateException("C++ addTransceiver failed.");
1037 }
1038 transceivers.add(newTransceiver);
1039 return newTransceiver;
1040 }
1041
1042 public RtpTransceiver addTransceiver(MediaStreamTrack.MediaType mediaType) {
1043 return addTransceiver(mediaType, new RtpTransceiver.RtpTransceiverInit());
1044 }
1045
1046 public RtpTransceiver addTransceiver(
Sami Kalliomäkie7592d82018-03-22 13:32:44 +01001047 MediaStreamTrack.MediaType mediaType, @Nullable RtpTransceiver.RtpTransceiverInit init) {
Seth Hampsonc384e142018-03-06 15:47:10 -08001048 if (mediaType == null) {
1049 throw new NullPointerException("No MediaType specified for addTransceiver.");
1050 }
1051 if (init == null) {
1052 init = new RtpTransceiver.RtpTransceiverInit();
1053 }
1054 RtpTransceiver newTransceiver = nativeAddTransceiverOfType(mediaType, init);
1055 if (newTransceiver == null) {
1056 throw new IllegalStateException("C++ addTransceiver failed.");
1057 }
1058 transceivers.add(newTransceiver);
1059 return newTransceiver;
1060 }
1061
deadbeef82215872017-04-18 10:27:51 -07001062 // Older, non-standard implementation of getStats.
1063 @Deprecated
Sami Kalliomäkie7592d82018-03-22 13:32:44 +01001064 public boolean getStats(StatsObserver observer, @Nullable MediaStreamTrack track) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +02001065 return nativeOldGetStats(observer, (track == null) ? 0 : track.getNativeMediaStreamTrack());
deadbeef82215872017-04-18 10:27:51 -07001066 }
1067
Seth Hampsonc384e142018-03-06 15:47:10 -08001068 /**
1069 * Gets stats using the new stats collection API, see webrtc/api/stats/. These
1070 * will replace old stats collection API when the new API has matured enough.
1071 */
deadbeef82215872017-04-18 10:27:51 -07001072 public void getStats(RTCStatsCollectorCallback callback) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001073 nativeNewGetStats(callback);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074 }
1075
Seth Hampsonc384e142018-03-06 15:47:10 -08001076 /**
1077 * Limits the bandwidth allocated for all RTP streams sent by this
1078 * PeerConnection. Pass null to leave a value unchanged.
1079 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001080 public boolean setBitrate(Integer min, Integer current, Integer max) {
1081 return nativeSetBitrate(min, current, max);
1082 }
zsteind89b0bc2017-08-03 11:11:40 -07001083
Seth Hampsonc384e142018-03-06 15:47:10 -08001084 /**
1085 * Starts recording an RTC event log.
1086 *
1087 * Ownership of the file is transfered to the native code. If an RTC event
1088 * log is already being recorded, it will be stopped and a new one will start
1089 * using the provided file. Logging will continue until the stopRtcEventLog
1090 * function is called. The max_size_bytes argument is ignored, it is added
1091 * for future use.
1092 */
ivoc0c6f0f62016-07-06 04:34:23 -07001093 public boolean startRtcEventLog(int file_descriptor, int max_size_bytes) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001094 return nativeStartRtcEventLog(file_descriptor, max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -07001095 }
1096
Seth Hampsonc384e142018-03-06 15:47:10 -08001097 /**
1098 * Stops recording an RTC event log. If no RTC event log is currently being
1099 * recorded, this call will have no effect.
1100 */
ivoc14d5dbe2016-07-04 07:06:55 -07001101 public void stopRtcEventLog() {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001102 nativeStopRtcEventLog();
ivoc14d5dbe2016-07-04 07:06:55 -07001103 }
1104
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001105 // TODO(fischman): add support for DTMF-related methods once that API
1106 // stabilizes.
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001107 public SignalingState signalingState() {
1108 return nativeSignalingState();
1109 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001110
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001111 public IceConnectionState iceConnectionState() {
1112 return nativeIceConnectionState();
1113 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001114
Jonas Olssonf01d8c82018-11-08 15:19:04 +01001115 public PeerConnectionState connectionState() {
1116 return nativeConnectionState();
1117 }
1118
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001119 public IceGatheringState iceGatheringState() {
1120 return nativeIceGatheringState();
1121 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001122
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001123 public void close() {
1124 nativeClose();
1125 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001126
deadbeef43697f62017-09-12 10:52:14 -07001127 /**
1128 * Free native resources associated with this PeerConnection instance.
Seth Hampsonc384e142018-03-06 15:47:10 -08001129 *
deadbeef43697f62017-09-12 10:52:14 -07001130 * This method removes a reference count from the C++ PeerConnection object,
1131 * which should result in it being destroyed. It also calls equivalent
1132 * "dispose" methods on the Java objects attached to this PeerConnection
1133 * (streams, senders, receivers), such that their associated C++ objects
1134 * will also be destroyed.
Seth Hampsonc384e142018-03-06 15:47:10 -08001135 *
1136 * <p>Note that this method cannot be safely called from an observer callback
deadbeef43697f62017-09-12 10:52:14 -07001137 * (PeerConnection.Observer, DataChannel.Observer, etc.). If you want to, for
1138 * example, destroy the PeerConnection after an "ICE failed" callback, you
1139 * must do this asynchronously (in other words, unwind the stack first). See
1140 * <a href="https://bugs.chromium.org/p/webrtc/issues/detail?id=3721">bug
1141 * 3721</a> for more details.
1142 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143 public void dispose() {
1144 close();
1145 for (MediaStream stream : localStreams) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +02001146 nativeRemoveLocalStream(stream.getNativeMediaStream());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001147 stream.dispose();
1148 }
1149 localStreams.clear();
deadbeef4139c0f2015-10-06 12:29:25 -07001150 for (RtpSender sender : senders) {
1151 sender.dispose();
1152 }
1153 senders.clear();
1154 for (RtpReceiver receiver : receivers) {
1155 receiver.dispose();
1156 }
Seth Hampsonc384e142018-03-06 15:47:10 -08001157 for (RtpTransceiver transceiver : transceivers) {
1158 transceiver.dispose();
1159 }
1160 transceivers.clear();
deadbeef4139c0f2015-10-06 12:29:25 -07001161 receivers.clear();
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001162 nativeFreeOwnedPeerConnection(nativePeerConnection);
1163 }
1164
1165 /** Returns a pointer to the native webrtc::PeerConnectionInterface. */
1166 public long getNativePeerConnection() {
1167 return nativeGetNativePeerConnection();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001168 }
1169
Magnus Jedvert9060eb12017-12-12 12:52:54 +01001170 @CalledByNative
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001171 long getNativeOwnedPeerConnection() {
Magnus Jedvert9060eb12017-12-12 12:52:54 +01001172 return nativePeerConnection;
1173 }
1174
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001175 public static long createNativePeerConnectionObserver(Observer observer) {
1176 return nativeCreatePeerConnectionObserver(observer);
1177 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001179 private native long nativeGetNativePeerConnection();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001180 private native SessionDescription nativeGetLocalDescription();
1181 private native SessionDescription nativeGetRemoteDescription();
Michael Iedema02137862018-10-09 15:30:01 +02001182 private native RtcCertificatePem nativeGetCertificate();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001183 private native DataChannel nativeCreateDataChannel(String label, DataChannel.Init init);
1184 private native void nativeCreateOffer(SdpObserver observer, MediaConstraints constraints);
1185 private native void nativeCreateAnswer(SdpObserver observer, MediaConstraints constraints);
1186 private native void nativeSetLocalDescription(SdpObserver observer, SessionDescription sdp);
1187 private native void nativeSetRemoteDescription(SdpObserver observer, SessionDescription sdp);
1188 private native void nativeSetAudioPlayout(boolean playout);
1189 private native void nativeSetAudioRecording(boolean recording);
1190 private native boolean nativeSetBitrate(Integer min, Integer current, Integer max);
1191 private native SignalingState nativeSignalingState();
1192 private native IceConnectionState nativeIceConnectionState();
Jonas Olssonf01d8c82018-11-08 15:19:04 +01001193 private native PeerConnectionState nativeConnectionState();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001194 private native IceGatheringState nativeIceGatheringState();
1195 private native void nativeClose();
1196 private static native long nativeCreatePeerConnectionObserver(Observer observer);
Sami Kalliomäkice5c19a2018-01-15 09:28:34 +01001197 private static native void nativeFreeOwnedPeerConnection(long ownedPeerConnection);
1198 private native boolean nativeSetConfiguration(RTCConfiguration config);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001199 private native boolean nativeAddIceCandidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001200 String sdpMid, int sdpMLineIndex, String iceCandidateSdp);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001201 private native boolean nativeRemoveIceCandidates(final IceCandidate[] candidates);
1202 private native boolean nativeAddLocalStream(long stream);
1203 private native void nativeRemoveLocalStream(long stream);
1204 private native boolean nativeOldGetStats(StatsObserver observer, long nativeTrack);
1205 private native void nativeNewGetStats(RTCStatsCollectorCallback callback);
1206 private native RtpSender nativeCreateSender(String kind, String stream_id);
1207 private native List<RtpSender> nativeGetSenders();
1208 private native List<RtpReceiver> nativeGetReceivers();
Seth Hampsonc384e142018-03-06 15:47:10 -08001209 private native List<RtpTransceiver> nativeGetTransceivers();
1210 private native RtpSender nativeAddTrack(long track, List<String> streamIds);
1211 private native boolean nativeRemoveTrack(long sender);
1212 private native RtpTransceiver nativeAddTransceiverWithTrack(
1213 long track, RtpTransceiver.RtpTransceiverInit init);
1214 private native RtpTransceiver nativeAddTransceiverOfType(
1215 MediaStreamTrack.MediaType mediaType, RtpTransceiver.RtpTransceiverInit init);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001216 private native boolean nativeStartRtcEventLog(int file_descriptor, int max_size_bytes);
1217 private native void nativeStopRtcEventLog();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218}