blob: 881255d9aa5bec78b08d81c1aca6ff0a250496ec [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
Jonas Orelandbdcee282017-10-10 14:01:40 +0200307 // This is an optional wrapper for the C++ webrtc::TurnCustomizer.
308 public TurnCustomizer turnCustomizer;
309
deadbeef28e29192017-07-27 09:14:38 -0700310 // TODO(deadbeef): Instead of duplicating the defaults here, we should do
311 // something to pick up the defaults from C++. The Objective-C equivalent
312 // of RTCConfiguration does that.
Jiayang Liucac1b382015-04-30 12:35:24 -0700313 public RTCConfiguration(List<IceServer> iceServers) {
314 iceTransportsType = IceTransportsType.ALL;
315 bundlePolicy = BundlePolicy.BALANCED;
zhihuang4dfb8ce2016-11-23 10:30:12 -0800316 rtcpMuxPolicy = RtcpMuxPolicy.REQUIRE;
Jiayang Liucac1b382015-04-30 12:35:24 -0700317 tcpCandidatePolicy = TcpCandidatePolicy.ENABLED;
honghaiz60347052016-05-31 18:29:12 -0700318 candidateNetworkPolicy = candidateNetworkPolicy.ALL;
Jiayang Liucac1b382015-04-30 12:35:24 -0700319 this.iceServers = iceServers;
Henrik Lundin64dad832015-05-11 12:44:23 +0200320 audioJitterBufferMaxPackets = 50;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200321 audioJitterBufferFastAccelerate = false;
honghaiz4edc39c2015-09-01 09:53:56 -0700322 iceConnectionReceivingTimeout = -1;
Honghai Zhang381b4212015-12-04 12:24:03 -0800323 iceBackupCandidatePairPingInterval = -1;
glaznev97579a42015-09-01 11:31:27 -0700324 keyType = KeyType.ECDSA;
honghaiz1f429e32015-09-28 07:57:34 -0700325 continualGatheringPolicy = ContinualGatheringPolicy.GATHER_ONCE;
deadbeefbe0c96f2016-05-18 16:20:14 -0700326 iceCandidatePoolSize = 0;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700327 pruneTurnPorts = false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700328 presumeWritableWhenFullyRelayed = false;
skvlad51072462017-02-02 11:50:14 -0800329 iceCheckMinInterval = null;
zhihuangb09b3f92017-03-07 14:40:51 -0800330 disableIPv6OnWifi = false;
deadbeef28e29192017-07-27 09:14:38 -0700331 maxIPv6Networks = 5;
Steve Antond960a0c2017-07-17 12:33:07 -0700332 iceRegatherIntervalRange = null;
Jiayang Liucac1b382015-04-30 12:35:24 -0700333 }
334 };
335
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 private final List<MediaStream> localStreams;
337 private final long nativePeerConnection;
338 private final long nativeObserver;
deadbeef4139c0f2015-10-06 12:29:25 -0700339 private List<RtpSender> senders;
340 private List<RtpReceiver> receivers;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341
342 PeerConnection(long nativePeerConnection, long nativeObserver) {
343 this.nativePeerConnection = nativePeerConnection;
344 this.nativeObserver = nativeObserver;
345 localStreams = new LinkedList<MediaStream>();
deadbeef4139c0f2015-10-06 12:29:25 -0700346 senders = new LinkedList<RtpSender>();
347 receivers = new LinkedList<RtpReceiver>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348 }
349
350 // JsepInterface.
351 public native SessionDescription getLocalDescription();
352
353 public native SessionDescription getRemoteDescription();
354
sakalb6760f92016-09-29 04:12:44 -0700355 public native DataChannel createDataChannel(String label, DataChannel.Init init);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000356
sakalb6760f92016-09-29 04:12:44 -0700357 public native void createOffer(SdpObserver observer, MediaConstraints constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358
sakalb6760f92016-09-29 04:12:44 -0700359 public native void createAnswer(SdpObserver observer, MediaConstraints constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360
sakalb6760f92016-09-29 04:12:44 -0700361 public native void setLocalDescription(SdpObserver observer, SessionDescription sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362
sakalb6760f92016-09-29 04:12:44 -0700363 public native void setRemoteDescription(SdpObserver observer, SessionDescription sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364
deadbeef5d0b6d82017-01-09 16:05:28 -0800365 public boolean setConfiguration(RTCConfiguration config) {
366 return nativeSetConfiguration(config, nativeObserver);
367 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368
369 public boolean addIceCandidate(IceCandidate candidate) {
sakalb6760f92016-09-29 04:12:44 -0700370 return nativeAddIceCandidate(candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371 }
372
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700373 public boolean removeIceCandidates(final IceCandidate[] candidates) {
374 return nativeRemoveIceCandidates(candidates);
375 }
376
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000377 public boolean addStream(MediaStream stream) {
378 boolean ret = nativeAddLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379 if (!ret) {
380 return false;
381 }
382 localStreams.add(stream);
383 return true;
384 }
385
386 public void removeStream(MediaStream stream) {
387 nativeRemoveLocalStream(stream.nativeStream);
388 localStreams.remove(stream);
389 }
390
deadbeef7a246882017-08-09 08:40:10 -0700391 /**
392 * Creates an RtpSender without a track.
393 * <p>
394 * This method allows an application to cause the PeerConnection to negotiate
395 * sending/receiving a specific media type, but without having a track to
396 * send yet.
397 * <p>
398 * When the application does want to begin sending a track, it can call
399 * RtpSender.setTrack, which doesn't require any additional SDP negotiation.
400 * <p>
401 * Example use:
402 * <pre>
403 * {@code
404 * audioSender = pc.createSender("audio", "stream1");
405 * videoSender = pc.createSender("video", "stream1");
406 * // Do normal SDP offer/answer, which will kick off ICE/DTLS and negotiate
407 * // media parameters....
408 * // Later, when the endpoint is ready to actually begin sending:
409 * audioSender.setTrack(audioTrack, false);
410 * videoSender.setTrack(videoTrack, false);
411 * }
412 * </pre>
413 * Note: This corresponds most closely to "addTransceiver" in the official
414 * WebRTC API, in that it creates a sender without a track. It was
415 * implemented before addTransceiver because it provides useful
416 * functionality, and properly implementing transceivers would have required
417 * a great deal more work.
418 *
419 * @param kind Corresponds to MediaStreamTrack kinds (must be "audio" or
420 * "video").
421 * @param stream_id The ID of the MediaStream that this sender's track will
422 * be associated with when SDP is applied to the remote
423 * PeerConnection. If createSender is used to create an
424 * audio and video sender that should be synchronized, they
425 * should use the same stream ID.
426 * @return A new RtpSender object if successful, or null otherwise.
427 */
deadbeefbd7d8f72015-12-18 16:58:44 -0800428 public RtpSender createSender(String kind, String stream_id) {
429 RtpSender new_sender = nativeCreateSender(kind, stream_id);
deadbeefee524f72015-12-02 11:27:40 -0800430 if (new_sender != null) {
431 senders.add(new_sender);
432 }
433 return new_sender;
434 }
435
deadbeef4139c0f2015-10-06 12:29:25 -0700436 // Note that calling getSenders will dispose of the senders previously
437 // returned (and same goes for getReceivers).
438 public List<RtpSender> getSenders() {
439 for (RtpSender sender : senders) {
440 sender.dispose();
441 }
442 senders = nativeGetSenders();
443 return Collections.unmodifiableList(senders);
444 }
445
446 public List<RtpReceiver> getReceivers() {
447 for (RtpReceiver receiver : receivers) {
448 receiver.dispose();
449 }
450 receivers = nativeGetReceivers();
451 return Collections.unmodifiableList(receivers);
452 }
453
deadbeef82215872017-04-18 10:27:51 -0700454 // Older, non-standard implementation of getStats.
455 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456 public boolean getStats(StatsObserver observer, MediaStreamTrack track) {
deadbeef82215872017-04-18 10:27:51 -0700457 return nativeOldGetStats(observer, (track == null) ? 0 : track.nativeTrack);
458 }
459
460 // Gets stats using the new stats collection API, see webrtc/api/stats/. These
461 // will replace old stats collection API when the new API has matured enough.
462 public void getStats(RTCStatsCollectorCallback callback) {
463 nativeNewGetStats(callback);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464 }
465
zsteind89b0bc2017-08-03 11:11:40 -0700466 // Limits the bandwidth allocated for all RTP streams sent by this
467 // PeerConnection. Pass null to leave a value unchanged.
468 public native boolean setBitrate(Integer min, Integer current, Integer max);
469
ivoc14d5dbe2016-07-04 07:06:55 -0700470 // Starts recording an RTC event log. Ownership of the file is transfered to
471 // the native code. If an RTC event log is already being recorded, it will be
472 // stopped and a new one will start using the provided file. Logging will
473 // continue until the stopRtcEventLog function is called. The max_size_bytes
474 // argument is ignored, it is added for future use.
ivoc0c6f0f62016-07-06 04:34:23 -0700475 public boolean startRtcEventLog(int file_descriptor, int max_size_bytes) {
476 return nativeStartRtcEventLog(file_descriptor, max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -0700477 }
478
479 // Stops recording an RTC event log. If no RTC event log is currently being
480 // recorded, this call will have no effect.
481 public void stopRtcEventLog() {
ivoc0c6f0f62016-07-06 04:34:23 -0700482 nativeStopRtcEventLog();
ivoc14d5dbe2016-07-04 07:06:55 -0700483 }
484
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 // TODO(fischman): add support for DTMF-related methods once that API
486 // stabilizes.
487 public native SignalingState signalingState();
488
489 public native IceConnectionState iceConnectionState();
490
491 public native IceGatheringState iceGatheringState();
492
493 public native void close();
494
deadbeef43697f62017-09-12 10:52:14 -0700495 /**
496 * Free native resources associated with this PeerConnection instance.
497 * <p>
498 * This method removes a reference count from the C++ PeerConnection object,
499 * which should result in it being destroyed. It also calls equivalent
500 * "dispose" methods on the Java objects attached to this PeerConnection
501 * (streams, senders, receivers), such that their associated C++ objects
502 * will also be destroyed.
503 * <p>
504 * Note that this method cannot be safely called from an observer callback
505 * (PeerConnection.Observer, DataChannel.Observer, etc.). If you want to, for
506 * example, destroy the PeerConnection after an "ICE failed" callback, you
507 * must do this asynchronously (in other words, unwind the stack first). See
508 * <a href="https://bugs.chromium.org/p/webrtc/issues/detail?id=3721">bug
509 * 3721</a> for more details.
510 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 public void dispose() {
512 close();
513 for (MediaStream stream : localStreams) {
fischman@webrtc.org32001ef2013-08-12 23:26:21 +0000514 nativeRemoveLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 stream.dispose();
516 }
517 localStreams.clear();
deadbeef4139c0f2015-10-06 12:29:25 -0700518 for (RtpSender sender : senders) {
519 sender.dispose();
520 }
521 senders.clear();
522 for (RtpReceiver receiver : receivers) {
523 receiver.dispose();
524 }
525 receivers.clear();
magjedb1c74532017-08-27 13:47:20 -0700526 JniCommon.nativeReleaseRef(nativePeerConnection);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 freeObserver(nativeObserver);
528 }
529
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530 private static native void freeObserver(long nativeObserver);
531
deadbeef5d0b6d82017-01-09 16:05:28 -0800532 public native boolean nativeSetConfiguration(RTCConfiguration config, long nativeObserver);
533
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 private native boolean nativeAddIceCandidate(
535 String sdpMid, int sdpMLineIndex, String iceCandidateSdp);
536
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700537 private native boolean nativeRemoveIceCandidates(final IceCandidate[] candidates);
538
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000539 private native boolean nativeAddLocalStream(long nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540
541 private native void nativeRemoveLocalStream(long nativeStream);
542
deadbeef82215872017-04-18 10:27:51 -0700543 private native boolean nativeOldGetStats(StatsObserver observer, long nativeTrack);
544
545 private native void nativeNewGetStats(RTCStatsCollectorCallback callback);
deadbeef4139c0f2015-10-06 12:29:25 -0700546
deadbeefbd7d8f72015-12-18 16:58:44 -0800547 private native RtpSender nativeCreateSender(String kind, String stream_id);
deadbeefee524f72015-12-02 11:27:40 -0800548
deadbeef4139c0f2015-10-06 12:29:25 -0700549 private native List<RtpSender> nativeGetSenders();
550
551 private native List<RtpReceiver> nativeGetReceivers();
ivoc14d5dbe2016-07-04 07:06:55 -0700552
sakalb6760f92016-09-29 04:12:44 -0700553 private native boolean nativeStartRtcEventLog(int file_descriptor, int max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -0700554
ivoc0c6f0f62016-07-06 04:34:23 -0700555 private native void nativeStopRtcEventLog();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556}