blob: 9ddfdfdc06f2a8cb2b81958a331ff5c943d0fd41 [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
deadbeef4139c0f2015-10-06 12:29:25 -070013import java.util.Collections;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014import java.util.LinkedList;
15import java.util.List;
16
17/**
18 * Java-land version of the PeerConnection APIs; wraps the C++ API
19 * http://www.webrtc.org/reference/native-apis, which in turn is inspired by the
20 * JS APIs: http://dev.w3.org/2011/webrtc/editor/webrtc.html and
21 * http://www.w3.org/TR/mediacapture-streams/
22 */
23public class PeerConnection {
24 static {
25 System.loadLibrary("jingle_peerconnection_so");
26 }
27
28 /** Tracks PeerConnectionInterface::IceGatheringState */
sakalb6760f92016-09-29 04:12:44 -070029 public enum IceGatheringState { NEW, GATHERING, COMPLETE }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030
31 /** Tracks PeerConnectionInterface::IceConnectionState */
32 public enum IceConnectionState {
sakalb6760f92016-09-29 04:12:44 -070033 NEW,
34 CHECKING,
35 CONNECTED,
36 COMPLETED,
37 FAILED,
38 DISCONNECTED,
39 CLOSED
40 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
hnsl04833622017-01-09 08:35:45 -080042 /** Tracks PeerConnectionInterface::TlsCertPolicy */
43 public enum TlsCertPolicy {
44 TLS_CERT_POLICY_SECURE,
45 TLS_CERT_POLICY_INSECURE_NO_CHECK,
46 }
47
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048 /** Tracks PeerConnectionInterface::SignalingState */
49 public enum SignalingState {
sakalb6760f92016-09-29 04:12:44 -070050 STABLE,
51 HAVE_LOCAL_OFFER,
52 HAVE_LOCAL_PRANSWER,
53 HAVE_REMOTE_OFFER,
54 HAVE_REMOTE_PRANSWER,
55 CLOSED
56 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057
58 /** Java version of PeerConnectionObserver. */
59 public static interface Observer {
60 /** Triggered when the SignalingState changes. */
61 public void onSignalingChange(SignalingState newState);
62
63 /** Triggered when the IceConnectionState changes. */
64 public void onIceConnectionChange(IceConnectionState newState);
65
Peter Thatcher54360512015-07-08 11:08:35 -070066 /** Triggered when the ICE connection receiving status changes. */
67 public void onIceConnectionReceivingChange(boolean receiving);
68
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 /** Triggered when the IceGatheringState changes. */
70 public void onIceGatheringChange(IceGatheringState newState);
71
72 /** Triggered when a new ICE candidate has been found. */
73 public void onIceCandidate(IceCandidate candidate);
74
Honghai Zhang7fb69db2016-03-14 11:59:18 -070075 /** Triggered when some ICE candidates have been removed. */
76 public void onIceCandidatesRemoved(IceCandidate[] candidates);
77
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 /** Triggered when media is received on a new stream from remote peer. */
79 public void onAddStream(MediaStream stream);
80
81 /** Triggered when a remote peer close a stream. */
82 public void onRemoveStream(MediaStream stream);
henrike@webrtc.org723d6832013-07-12 16:04:50 +000083
84 /** Triggered when a remote peer opens a DataChannel. */
85 public void onDataChannel(DataChannel dataChannel);
fischman@webrtc.orgd7568a02014-01-13 22:04:12 +000086
87 /** Triggered when renegotiation is necessary. */
88 public void onRenegotiationNeeded();
zhihuangdcccda72016-12-21 14:08:03 -080089
90 /**
91 * Triggered when a new track is signaled by the remote peer, as a result of
92 * setRemoteDescription.
93 */
94 public void onAddTrack(RtpReceiver receiver, MediaStream[] mediaStreams);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 }
96
97 /** Java version of PeerConnectionInterface.IceServer. */
98 public static class IceServer {
Emad Omaradab1d2d2017-06-16 15:43:11 -070099 // List of URIs associated with this server. Valid formats are described
100 // in RFC7064 and RFC7065, and more may be added in the future. The "host"
101 // part of the URI may contain either an IP address or a hostname.
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700102 @Deprecated public final String uri;
103 public final List<String> urls;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 public final String username;
105 public final String password;
hnsl04833622017-01-09 08:35:45 -0800106 public final TlsCertPolicy tlsCertPolicy;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107
Emad Omaradab1d2d2017-06-16 15:43:11 -0700108 // If the URIs in |urls| only contain IP addresses, this field can be used
109 // to indicate the hostname, which may be necessary for TLS (using the SNI
110 // extension). If |urls| itself contains the hostname, this isn't
111 // necessary.
112 public final String hostname;
113
Diogo Real1dca9d52017-08-29 12:18:32 -0700114 // List of protocols to be used in the TLS ALPN extension.
115 public final List<String> tlsAlpnProtocols;
116
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700117 // List of elliptic curves to be used in the TLS elliptic curves extension.
118 // Only curve names supported by OpenSSL should be used (eg. "P-256","X25519").
119 public final List<String> tlsEllipticCurves;
120
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 /** Convenience constructor for STUN servers. */
Diogo Real05ea2b32017-08-31 00:12:58 -0700122 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 public IceServer(String uri) {
124 this(uri, "", "");
125 }
126
Diogo Real05ea2b32017-08-31 00:12:58 -0700127 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 public IceServer(String uri, String username, String password) {
hnsl04833622017-01-09 08:35:45 -0800129 this(uri, username, password, TlsCertPolicy.TLS_CERT_POLICY_SECURE);
130 }
131
Diogo Real05ea2b32017-08-31 00:12:58 -0700132 @Deprecated
hnsl04833622017-01-09 08:35:45 -0800133 public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy) {
Emad Omaradab1d2d2017-06-16 15:43:11 -0700134 this(uri, username, password, tlsCertPolicy, "");
135 }
136
Diogo Real05ea2b32017-08-31 00:12:58 -0700137 @Deprecated
Emad Omaradab1d2d2017-06-16 15:43:11 -0700138 public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy,
139 String hostname) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700140 this(uri, Collections.singletonList(uri), username, password, tlsCertPolicy, hostname, null,
141 null);
Diogo Real1dca9d52017-08-29 12:18:32 -0700142 }
143
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700144 private IceServer(String uri, List<String> urls, String username, String password,
145 TlsCertPolicy tlsCertPolicy, String hostname, List<String> tlsAlpnProtocols,
146 List<String> tlsEllipticCurves) {
147 if (uri == null || urls == null || urls.isEmpty()) {
148 throw new IllegalArgumentException("uri == null || urls == null || urls.isEmpty()");
149 }
150 for (String it : urls) {
151 if (it == null) {
152 throw new IllegalArgumentException("urls element is null: " + urls);
153 }
154 }
155 if (username == null) {
156 throw new IllegalArgumentException("username == null");
157 }
158 if (password == null) {
159 throw new IllegalArgumentException("password == null");
160 }
161 if (hostname == null) {
162 throw new IllegalArgumentException("hostname == null");
163 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 this.uri = uri;
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700165 this.urls = urls;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 this.username = username;
167 this.password = password;
hnsl04833622017-01-09 08:35:45 -0800168 this.tlsCertPolicy = tlsCertPolicy;
Emad Omaradab1d2d2017-06-16 15:43:11 -0700169 this.hostname = hostname;
Diogo Real1dca9d52017-08-29 12:18:32 -0700170 this.tlsAlpnProtocols = tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700171 this.tlsEllipticCurves = tlsEllipticCurves;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 }
173
174 public String toString() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700175 return urls + " [" + username + ":" + password + "] [" + tlsCertPolicy + "] [" + hostname
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700176 + "] [" + tlsAlpnProtocols + "] [" + tlsEllipticCurves + "]";
Diogo Real1dca9d52017-08-29 12:18:32 -0700177 }
178
179 public static Builder builder(String uri) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700180 return new Builder(Collections.singletonList(uri));
181 }
182
183 public static Builder builder(List<String> urls) {
184 return new Builder(urls);
Diogo Real1dca9d52017-08-29 12:18:32 -0700185 }
186
187 public static class Builder {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700188 private final List<String> urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700189 private String username = "";
190 private String password = "";
191 private TlsCertPolicy tlsCertPolicy = TlsCertPolicy.TLS_CERT_POLICY_SECURE;
192 private String hostname = "";
193 private List<String> tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700194 private List<String> tlsEllipticCurves;
Diogo Real1dca9d52017-08-29 12:18:32 -0700195
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700196 private Builder(List<String> urls) {
197 if (urls == null || urls.isEmpty()) {
198 throw new IllegalArgumentException("urls == null || urls.isEmpty(): " + urls);
199 }
200 this.urls = urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700201 }
202
203 public Builder setUsername(String username) {
204 this.username = username;
205 return this;
206 }
207
208 public Builder setPassword(String password) {
209 this.password = password;
210 return this;
211 }
212
213 public Builder setTlsCertPolicy(TlsCertPolicy tlsCertPolicy) {
214 this.tlsCertPolicy = tlsCertPolicy;
215 return this;
216 }
217
218 public Builder setHostname(String hostname) {
219 this.hostname = hostname;
220 return this;
221 }
222
223 public Builder setTlsAlpnProtocols(List<String> tlsAlpnProtocols) {
224 this.tlsAlpnProtocols = tlsAlpnProtocols;
225 return this;
226 }
227
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700228 public Builder setTlsEllipticCurves(List<String> tlsEllipticCurves) {
229 this.tlsEllipticCurves = tlsEllipticCurves;
230 return this;
231 }
232
Diogo Real1dca9d52017-08-29 12:18:32 -0700233 public IceServer createIceServer() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700234 return new IceServer(urls.get(0), urls, username, password, tlsCertPolicy, hostname,
235 tlsAlpnProtocols, tlsEllipticCurves);
Diogo Real1dca9d52017-08-29 12:18:32 -0700236 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237 }
238 }
239
Jiayang Liucac1b382015-04-30 12:35:24 -0700240 /** Java version of PeerConnectionInterface.IceTransportsType */
sakalb6760f92016-09-29 04:12:44 -0700241 public enum IceTransportsType { NONE, RELAY, NOHOST, ALL }
Jiayang Liucac1b382015-04-30 12:35:24 -0700242
243 /** Java version of PeerConnectionInterface.BundlePolicy */
sakalb6760f92016-09-29 04:12:44 -0700244 public enum BundlePolicy { BALANCED, MAXBUNDLE, MAXCOMPAT }
Jiayang Liucac1b382015-04-30 12:35:24 -0700245
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700246 /** Java version of PeerConnectionInterface.RtcpMuxPolicy */
sakalb6760f92016-09-29 04:12:44 -0700247 public enum RtcpMuxPolicy { NEGOTIATE, REQUIRE }
glaznev97579a42015-09-01 11:31:27 -0700248
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700249 /** Java version of PeerConnectionInterface.TcpCandidatePolicy */
sakalb6760f92016-09-29 04:12:44 -0700250 public enum TcpCandidatePolicy { ENABLED, DISABLED }
Jiayang Liucac1b382015-04-30 12:35:24 -0700251
honghaiz60347052016-05-31 18:29:12 -0700252 /** Java version of PeerConnectionInterface.CandidateNetworkPolicy */
sakalb6760f92016-09-29 04:12:44 -0700253 public enum CandidateNetworkPolicy { ALL, LOW_COST }
honghaiz60347052016-05-31 18:29:12 -0700254
glaznev97579a42015-09-01 11:31:27 -0700255 /** Java version of rtc::KeyType */
sakalb6760f92016-09-29 04:12:44 -0700256 public enum KeyType { RSA, ECDSA }
glaznev97579a42015-09-01 11:31:27 -0700257
honghaiz1f429e32015-09-28 07:57:34 -0700258 /** Java version of PeerConnectionInterface.ContinualGatheringPolicy */
sakalb6760f92016-09-29 04:12:44 -0700259 public enum ContinualGatheringPolicy { GATHER_ONCE, GATHER_CONTINUALLY }
honghaiz1f429e32015-09-28 07:57:34 -0700260
Steve Antond960a0c2017-07-17 12:33:07 -0700261 /** Java version of rtc::IntervalRange */
262 public static class IntervalRange {
263 private final int min;
264 private final int max;
265
266 public IntervalRange(int min, int max) {
267 this.min = min;
268 this.max = max;
269 }
270
271 public int getMin() {
272 return min;
273 }
274
275 public int getMax() {
276 return max;
277 }
278 }
279
Jiayang Liucac1b382015-04-30 12:35:24 -0700280 /** Java version of PeerConnectionInterface.RTCConfiguration */
281 public static class RTCConfiguration {
282 public IceTransportsType iceTransportsType;
283 public List<IceServer> iceServers;
284 public BundlePolicy bundlePolicy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700285 public RtcpMuxPolicy rtcpMuxPolicy;
Jiayang Liucac1b382015-04-30 12:35:24 -0700286 public TcpCandidatePolicy tcpCandidatePolicy;
honghaiz60347052016-05-31 18:29:12 -0700287 public CandidateNetworkPolicy candidateNetworkPolicy;
Henrik Lundin64dad832015-05-11 12:44:23 +0200288 public int audioJitterBufferMaxPackets;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200289 public boolean audioJitterBufferFastAccelerate;
honghaiz4edc39c2015-09-01 09:53:56 -0700290 public int iceConnectionReceivingTimeout;
Honghai Zhang381b4212015-12-04 12:24:03 -0800291 public int iceBackupCandidatePairPingInterval;
glaznev97579a42015-09-01 11:31:27 -0700292 public KeyType keyType;
honghaiz1f429e32015-09-28 07:57:34 -0700293 public ContinualGatheringPolicy continualGatheringPolicy;
deadbeefbe0c96f2016-05-18 16:20:14 -0700294 public int iceCandidatePoolSize;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700295 public boolean pruneTurnPorts;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700296 public boolean presumeWritableWhenFullyRelayed;
skvlad51072462017-02-02 11:50:14 -0800297 public Integer iceCheckMinInterval;
zhihuangb09b3f92017-03-07 14:40:51 -0800298 public boolean disableIPv6OnWifi;
deadbeef28e29192017-07-27 09:14:38 -0700299 // By default, PeerConnection will use a limited number of IPv6 network
300 // interfaces, in order to avoid too many ICE candidate pairs being created
301 // and delaying ICE completion.
302 //
303 // Can be set to Integer.MAX_VALUE to effectively disable the limit.
304 public int maxIPv6Networks;
Steve Antond960a0c2017-07-17 12:33:07 -0700305 public IntervalRange iceRegatherIntervalRange;
Jiayang Liucac1b382015-04-30 12:35:24 -0700306
deadbeef28e29192017-07-27 09:14:38 -0700307 // TODO(deadbeef): Instead of duplicating the defaults here, we should do
308 // something to pick up the defaults from C++. The Objective-C equivalent
309 // of RTCConfiguration does that.
Jiayang Liucac1b382015-04-30 12:35:24 -0700310 public RTCConfiguration(List<IceServer> iceServers) {
311 iceTransportsType = IceTransportsType.ALL;
312 bundlePolicy = BundlePolicy.BALANCED;
zhihuang4dfb8ce2016-11-23 10:30:12 -0800313 rtcpMuxPolicy = RtcpMuxPolicy.REQUIRE;
Jiayang Liucac1b382015-04-30 12:35:24 -0700314 tcpCandidatePolicy = TcpCandidatePolicy.ENABLED;
honghaiz60347052016-05-31 18:29:12 -0700315 candidateNetworkPolicy = candidateNetworkPolicy.ALL;
Jiayang Liucac1b382015-04-30 12:35:24 -0700316 this.iceServers = iceServers;
Henrik Lundin64dad832015-05-11 12:44:23 +0200317 audioJitterBufferMaxPackets = 50;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200318 audioJitterBufferFastAccelerate = false;
honghaiz4edc39c2015-09-01 09:53:56 -0700319 iceConnectionReceivingTimeout = -1;
Honghai Zhang381b4212015-12-04 12:24:03 -0800320 iceBackupCandidatePairPingInterval = -1;
glaznev97579a42015-09-01 11:31:27 -0700321 keyType = KeyType.ECDSA;
honghaiz1f429e32015-09-28 07:57:34 -0700322 continualGatheringPolicy = ContinualGatheringPolicy.GATHER_ONCE;
deadbeefbe0c96f2016-05-18 16:20:14 -0700323 iceCandidatePoolSize = 0;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700324 pruneTurnPorts = false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700325 presumeWritableWhenFullyRelayed = false;
skvlad51072462017-02-02 11:50:14 -0800326 iceCheckMinInterval = null;
zhihuangb09b3f92017-03-07 14:40:51 -0800327 disableIPv6OnWifi = false;
deadbeef28e29192017-07-27 09:14:38 -0700328 maxIPv6Networks = 5;
Steve Antond960a0c2017-07-17 12:33:07 -0700329 iceRegatherIntervalRange = null;
Jiayang Liucac1b382015-04-30 12:35:24 -0700330 }
331 };
332
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333 private final List<MediaStream> localStreams;
334 private final long nativePeerConnection;
335 private final long nativeObserver;
deadbeef4139c0f2015-10-06 12:29:25 -0700336 private List<RtpSender> senders;
337 private List<RtpReceiver> receivers;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338
339 PeerConnection(long nativePeerConnection, long nativeObserver) {
340 this.nativePeerConnection = nativePeerConnection;
341 this.nativeObserver = nativeObserver;
342 localStreams = new LinkedList<MediaStream>();
deadbeef4139c0f2015-10-06 12:29:25 -0700343 senders = new LinkedList<RtpSender>();
344 receivers = new LinkedList<RtpReceiver>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 }
346
347 // JsepInterface.
348 public native SessionDescription getLocalDescription();
349
350 public native SessionDescription getRemoteDescription();
351
sakalb6760f92016-09-29 04:12:44 -0700352 public native DataChannel createDataChannel(String label, DataChannel.Init init);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000353
sakalb6760f92016-09-29 04:12:44 -0700354 public native void createOffer(SdpObserver observer, MediaConstraints constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355
sakalb6760f92016-09-29 04:12:44 -0700356 public native void createAnswer(SdpObserver observer, MediaConstraints constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357
sakalb6760f92016-09-29 04:12:44 -0700358 public native void setLocalDescription(SdpObserver observer, SessionDescription sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359
sakalb6760f92016-09-29 04:12:44 -0700360 public native void setRemoteDescription(SdpObserver observer, SessionDescription sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361
deadbeef5d0b6d82017-01-09 16:05:28 -0800362 public boolean setConfiguration(RTCConfiguration config) {
363 return nativeSetConfiguration(config, nativeObserver);
364 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365
366 public boolean addIceCandidate(IceCandidate candidate) {
sakalb6760f92016-09-29 04:12:44 -0700367 return nativeAddIceCandidate(candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368 }
369
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700370 public boolean removeIceCandidates(final IceCandidate[] candidates) {
371 return nativeRemoveIceCandidates(candidates);
372 }
373
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000374 public boolean addStream(MediaStream stream) {
375 boolean ret = nativeAddLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376 if (!ret) {
377 return false;
378 }
379 localStreams.add(stream);
380 return true;
381 }
382
383 public void removeStream(MediaStream stream) {
384 nativeRemoveLocalStream(stream.nativeStream);
385 localStreams.remove(stream);
386 }
387
deadbeef7a246882017-08-09 08:40:10 -0700388 /**
389 * Creates an RtpSender without a track.
390 * <p>
391 * This method allows an application to cause the PeerConnection to negotiate
392 * sending/receiving a specific media type, but without having a track to
393 * send yet.
394 * <p>
395 * When the application does want to begin sending a track, it can call
396 * RtpSender.setTrack, which doesn't require any additional SDP negotiation.
397 * <p>
398 * Example use:
399 * <pre>
400 * {@code
401 * audioSender = pc.createSender("audio", "stream1");
402 * videoSender = pc.createSender("video", "stream1");
403 * // Do normal SDP offer/answer, which will kick off ICE/DTLS and negotiate
404 * // media parameters....
405 * // Later, when the endpoint is ready to actually begin sending:
406 * audioSender.setTrack(audioTrack, false);
407 * videoSender.setTrack(videoTrack, false);
408 * }
409 * </pre>
410 * Note: This corresponds most closely to "addTransceiver" in the official
411 * WebRTC API, in that it creates a sender without a track. It was
412 * implemented before addTransceiver because it provides useful
413 * functionality, and properly implementing transceivers would have required
414 * a great deal more work.
415 *
416 * @param kind Corresponds to MediaStreamTrack kinds (must be "audio" or
417 * "video").
418 * @param stream_id The ID of the MediaStream that this sender's track will
419 * be associated with when SDP is applied to the remote
420 * PeerConnection. If createSender is used to create an
421 * audio and video sender that should be synchronized, they
422 * should use the same stream ID.
423 * @return A new RtpSender object if successful, or null otherwise.
424 */
deadbeefbd7d8f72015-12-18 16:58:44 -0800425 public RtpSender createSender(String kind, String stream_id) {
426 RtpSender new_sender = nativeCreateSender(kind, stream_id);
deadbeefee524f72015-12-02 11:27:40 -0800427 if (new_sender != null) {
428 senders.add(new_sender);
429 }
430 return new_sender;
431 }
432
deadbeef4139c0f2015-10-06 12:29:25 -0700433 // Note that calling getSenders will dispose of the senders previously
434 // returned (and same goes for getReceivers).
435 public List<RtpSender> getSenders() {
436 for (RtpSender sender : senders) {
437 sender.dispose();
438 }
439 senders = nativeGetSenders();
440 return Collections.unmodifiableList(senders);
441 }
442
443 public List<RtpReceiver> getReceivers() {
444 for (RtpReceiver receiver : receivers) {
445 receiver.dispose();
446 }
447 receivers = nativeGetReceivers();
448 return Collections.unmodifiableList(receivers);
449 }
450
deadbeef82215872017-04-18 10:27:51 -0700451 // Older, non-standard implementation of getStats.
452 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453 public boolean getStats(StatsObserver observer, MediaStreamTrack track) {
deadbeef82215872017-04-18 10:27:51 -0700454 return nativeOldGetStats(observer, (track == null) ? 0 : track.nativeTrack);
455 }
456
457 // Gets stats using the new stats collection API, see webrtc/api/stats/. These
458 // will replace old stats collection API when the new API has matured enough.
459 public void getStats(RTCStatsCollectorCallback callback) {
460 nativeNewGetStats(callback);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461 }
462
zsteind89b0bc2017-08-03 11:11:40 -0700463 // Limits the bandwidth allocated for all RTP streams sent by this
464 // PeerConnection. Pass null to leave a value unchanged.
465 public native boolean setBitrate(Integer min, Integer current, Integer max);
466
ivoc14d5dbe2016-07-04 07:06:55 -0700467 // Starts recording an RTC event log. Ownership of the file is transfered to
468 // the native code. If an RTC event log is already being recorded, it will be
469 // stopped and a new one will start using the provided file. Logging will
470 // continue until the stopRtcEventLog function is called. The max_size_bytes
471 // argument is ignored, it is added for future use.
ivoc0c6f0f62016-07-06 04:34:23 -0700472 public boolean startRtcEventLog(int file_descriptor, int max_size_bytes) {
473 return nativeStartRtcEventLog(file_descriptor, max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -0700474 }
475
476 // Stops recording an RTC event log. If no RTC event log is currently being
477 // recorded, this call will have no effect.
478 public void stopRtcEventLog() {
ivoc0c6f0f62016-07-06 04:34:23 -0700479 nativeStopRtcEventLog();
ivoc14d5dbe2016-07-04 07:06:55 -0700480 }
481
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 // TODO(fischman): add support for DTMF-related methods once that API
483 // stabilizes.
484 public native SignalingState signalingState();
485
486 public native IceConnectionState iceConnectionState();
487
488 public native IceGatheringState iceGatheringState();
489
490 public native void close();
491
deadbeef43697f62017-09-12 10:52:14 -0700492 /**
493 * Free native resources associated with this PeerConnection instance.
494 * <p>
495 * This method removes a reference count from the C++ PeerConnection object,
496 * which should result in it being destroyed. It also calls equivalent
497 * "dispose" methods on the Java objects attached to this PeerConnection
498 * (streams, senders, receivers), such that their associated C++ objects
499 * will also be destroyed.
500 * <p>
501 * Note that this method cannot be safely called from an observer callback
502 * (PeerConnection.Observer, DataChannel.Observer, etc.). If you want to, for
503 * example, destroy the PeerConnection after an "ICE failed" callback, you
504 * must do this asynchronously (in other words, unwind the stack first). See
505 * <a href="https://bugs.chromium.org/p/webrtc/issues/detail?id=3721">bug
506 * 3721</a> for more details.
507 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508 public void dispose() {
509 close();
510 for (MediaStream stream : localStreams) {
fischman@webrtc.org32001ef2013-08-12 23:26:21 +0000511 nativeRemoveLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 stream.dispose();
513 }
514 localStreams.clear();
deadbeef4139c0f2015-10-06 12:29:25 -0700515 for (RtpSender sender : senders) {
516 sender.dispose();
517 }
518 senders.clear();
519 for (RtpReceiver receiver : receivers) {
520 receiver.dispose();
521 }
522 receivers.clear();
magjedb1c74532017-08-27 13:47:20 -0700523 JniCommon.nativeReleaseRef(nativePeerConnection);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 freeObserver(nativeObserver);
525 }
526
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 private static native void freeObserver(long nativeObserver);
528
deadbeef5d0b6d82017-01-09 16:05:28 -0800529 public native boolean nativeSetConfiguration(RTCConfiguration config, long nativeObserver);
530
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 private native boolean nativeAddIceCandidate(
532 String sdpMid, int sdpMLineIndex, String iceCandidateSdp);
533
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700534 private native boolean nativeRemoveIceCandidates(final IceCandidate[] candidates);
535
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000536 private native boolean nativeAddLocalStream(long nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537
538 private native void nativeRemoveLocalStream(long nativeStream);
539
deadbeef82215872017-04-18 10:27:51 -0700540 private native boolean nativeOldGetStats(StatsObserver observer, long nativeTrack);
541
542 private native void nativeNewGetStats(RTCStatsCollectorCallback callback);
deadbeef4139c0f2015-10-06 12:29:25 -0700543
deadbeefbd7d8f72015-12-18 16:58:44 -0800544 private native RtpSender nativeCreateSender(String kind, String stream_id);
deadbeefee524f72015-12-02 11:27:40 -0800545
deadbeef4139c0f2015-10-06 12:29:25 -0700546 private native List<RtpSender> nativeGetSenders();
547
548 private native List<RtpReceiver> nativeGetReceivers();
ivoc14d5dbe2016-07-04 07:06:55 -0700549
sakalb6760f92016-09-29 04:12:44 -0700550 private native boolean nativeStartRtcEventLog(int file_descriptor, int max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -0700551
ivoc0c6f0f62016-07-06 04:34:23 -0700552 private native void nativeStopRtcEventLog();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553}