blob: 66e8075c9767a79d7ac566e65cd6c82413acca03 [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
Sami Kalliomäkibde473e2017-10-30 13:34:41 +0100174 @Override
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 public String toString() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700176 return urls + " [" + username + ":" + password + "] [" + tlsCertPolicy + "] [" + hostname
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700177 + "] [" + tlsAlpnProtocols + "] [" + tlsEllipticCurves + "]";
Diogo Real1dca9d52017-08-29 12:18:32 -0700178 }
179
180 public static Builder builder(String uri) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700181 return new Builder(Collections.singletonList(uri));
182 }
183
184 public static Builder builder(List<String> urls) {
185 return new Builder(urls);
Diogo Real1dca9d52017-08-29 12:18:32 -0700186 }
187
188 public static class Builder {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700189 private final List<String> urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700190 private String username = "";
191 private String password = "";
192 private TlsCertPolicy tlsCertPolicy = TlsCertPolicy.TLS_CERT_POLICY_SECURE;
193 private String hostname = "";
194 private List<String> tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700195 private List<String> tlsEllipticCurves;
Diogo Real1dca9d52017-08-29 12:18:32 -0700196
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700197 private Builder(List<String> urls) {
198 if (urls == null || urls.isEmpty()) {
199 throw new IllegalArgumentException("urls == null || urls.isEmpty(): " + urls);
200 }
201 this.urls = urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700202 }
203
204 public Builder setUsername(String username) {
205 this.username = username;
206 return this;
207 }
208
209 public Builder setPassword(String password) {
210 this.password = password;
211 return this;
212 }
213
214 public Builder setTlsCertPolicy(TlsCertPolicy tlsCertPolicy) {
215 this.tlsCertPolicy = tlsCertPolicy;
216 return this;
217 }
218
219 public Builder setHostname(String hostname) {
220 this.hostname = hostname;
221 return this;
222 }
223
224 public Builder setTlsAlpnProtocols(List<String> tlsAlpnProtocols) {
225 this.tlsAlpnProtocols = tlsAlpnProtocols;
226 return this;
227 }
228
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700229 public Builder setTlsEllipticCurves(List<String> tlsEllipticCurves) {
230 this.tlsEllipticCurves = tlsEllipticCurves;
231 return this;
232 }
233
Diogo Real1dca9d52017-08-29 12:18:32 -0700234 public IceServer createIceServer() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700235 return new IceServer(urls.get(0), urls, username, password, tlsCertPolicy, hostname,
236 tlsAlpnProtocols, tlsEllipticCurves);
Diogo Real1dca9d52017-08-29 12:18:32 -0700237 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238 }
239 }
240
Jiayang Liucac1b382015-04-30 12:35:24 -0700241 /** Java version of PeerConnectionInterface.IceTransportsType */
sakalb6760f92016-09-29 04:12:44 -0700242 public enum IceTransportsType { NONE, RELAY, NOHOST, ALL }
Jiayang Liucac1b382015-04-30 12:35:24 -0700243
244 /** Java version of PeerConnectionInterface.BundlePolicy */
sakalb6760f92016-09-29 04:12:44 -0700245 public enum BundlePolicy { BALANCED, MAXBUNDLE, MAXCOMPAT }
Jiayang Liucac1b382015-04-30 12:35:24 -0700246
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700247 /** Java version of PeerConnectionInterface.RtcpMuxPolicy */
sakalb6760f92016-09-29 04:12:44 -0700248 public enum RtcpMuxPolicy { NEGOTIATE, REQUIRE }
glaznev97579a42015-09-01 11:31:27 -0700249
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700250 /** Java version of PeerConnectionInterface.TcpCandidatePolicy */
sakalb6760f92016-09-29 04:12:44 -0700251 public enum TcpCandidatePolicy { ENABLED, DISABLED }
Jiayang Liucac1b382015-04-30 12:35:24 -0700252
honghaiz60347052016-05-31 18:29:12 -0700253 /** Java version of PeerConnectionInterface.CandidateNetworkPolicy */
sakalb6760f92016-09-29 04:12:44 -0700254 public enum CandidateNetworkPolicy { ALL, LOW_COST }
honghaiz60347052016-05-31 18:29:12 -0700255
glaznev97579a42015-09-01 11:31:27 -0700256 /** Java version of rtc::KeyType */
sakalb6760f92016-09-29 04:12:44 -0700257 public enum KeyType { RSA, ECDSA }
glaznev97579a42015-09-01 11:31:27 -0700258
honghaiz1f429e32015-09-28 07:57:34 -0700259 /** Java version of PeerConnectionInterface.ContinualGatheringPolicy */
sakalb6760f92016-09-29 04:12:44 -0700260 public enum ContinualGatheringPolicy { GATHER_ONCE, GATHER_CONTINUALLY }
honghaiz1f429e32015-09-28 07:57:34 -0700261
Steve Antond960a0c2017-07-17 12:33:07 -0700262 /** Java version of rtc::IntervalRange */
263 public static class IntervalRange {
264 private final int min;
265 private final int max;
266
267 public IntervalRange(int min, int max) {
268 this.min = min;
269 this.max = max;
270 }
271
272 public int getMin() {
273 return min;
274 }
275
276 public int getMax() {
277 return max;
278 }
279 }
280
Jiayang Liucac1b382015-04-30 12:35:24 -0700281 /** Java version of PeerConnectionInterface.RTCConfiguration */
282 public static class RTCConfiguration {
283 public IceTransportsType iceTransportsType;
284 public List<IceServer> iceServers;
285 public BundlePolicy bundlePolicy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700286 public RtcpMuxPolicy rtcpMuxPolicy;
Jiayang Liucac1b382015-04-30 12:35:24 -0700287 public TcpCandidatePolicy tcpCandidatePolicy;
honghaiz60347052016-05-31 18:29:12 -0700288 public CandidateNetworkPolicy candidateNetworkPolicy;
Henrik Lundin64dad832015-05-11 12:44:23 +0200289 public int audioJitterBufferMaxPackets;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200290 public boolean audioJitterBufferFastAccelerate;
honghaiz4edc39c2015-09-01 09:53:56 -0700291 public int iceConnectionReceivingTimeout;
Honghai Zhang381b4212015-12-04 12:24:03 -0800292 public int iceBackupCandidatePairPingInterval;
glaznev97579a42015-09-01 11:31:27 -0700293 public KeyType keyType;
honghaiz1f429e32015-09-28 07:57:34 -0700294 public ContinualGatheringPolicy continualGatheringPolicy;
deadbeefbe0c96f2016-05-18 16:20:14 -0700295 public int iceCandidatePoolSize;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700296 public boolean pruneTurnPorts;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700297 public boolean presumeWritableWhenFullyRelayed;
skvlad51072462017-02-02 11:50:14 -0800298 public Integer iceCheckMinInterval;
zhihuangb09b3f92017-03-07 14:40:51 -0800299 public boolean disableIPv6OnWifi;
deadbeef28e29192017-07-27 09:14:38 -0700300 // By default, PeerConnection will use a limited number of IPv6 network
301 // interfaces, in order to avoid too many ICE candidate pairs being created
302 // and delaying ICE completion.
303 //
304 // Can be set to Integer.MAX_VALUE to effectively disable the limit.
305 public int maxIPv6Networks;
Steve Antond960a0c2017-07-17 12:33:07 -0700306 public IntervalRange iceRegatherIntervalRange;
Jiayang Liucac1b382015-04-30 12:35:24 -0700307
Jonas Orelandbdcee282017-10-10 14:01:40 +0200308 // This is an optional wrapper for the C++ webrtc::TurnCustomizer.
309 public TurnCustomizer turnCustomizer;
310
deadbeef28e29192017-07-27 09:14:38 -0700311 // TODO(deadbeef): Instead of duplicating the defaults here, we should do
312 // something to pick up the defaults from C++. The Objective-C equivalent
313 // of RTCConfiguration does that.
Jiayang Liucac1b382015-04-30 12:35:24 -0700314 public RTCConfiguration(List<IceServer> iceServers) {
315 iceTransportsType = IceTransportsType.ALL;
316 bundlePolicy = BundlePolicy.BALANCED;
zhihuang4dfb8ce2016-11-23 10:30:12 -0800317 rtcpMuxPolicy = RtcpMuxPolicy.REQUIRE;
Jiayang Liucac1b382015-04-30 12:35:24 -0700318 tcpCandidatePolicy = TcpCandidatePolicy.ENABLED;
Sami Kalliomäki9828beb2017-10-26 16:21:22 +0200319 candidateNetworkPolicy = CandidateNetworkPolicy.ALL;
Jiayang Liucac1b382015-04-30 12:35:24 -0700320 this.iceServers = iceServers;
Henrik Lundin64dad832015-05-11 12:44:23 +0200321 audioJitterBufferMaxPackets = 50;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200322 audioJitterBufferFastAccelerate = false;
honghaiz4edc39c2015-09-01 09:53:56 -0700323 iceConnectionReceivingTimeout = -1;
Honghai Zhang381b4212015-12-04 12:24:03 -0800324 iceBackupCandidatePairPingInterval = -1;
glaznev97579a42015-09-01 11:31:27 -0700325 keyType = KeyType.ECDSA;
honghaiz1f429e32015-09-28 07:57:34 -0700326 continualGatheringPolicy = ContinualGatheringPolicy.GATHER_ONCE;
deadbeefbe0c96f2016-05-18 16:20:14 -0700327 iceCandidatePoolSize = 0;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700328 pruneTurnPorts = false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700329 presumeWritableWhenFullyRelayed = false;
skvlad51072462017-02-02 11:50:14 -0800330 iceCheckMinInterval = null;
zhihuangb09b3f92017-03-07 14:40:51 -0800331 disableIPv6OnWifi = false;
deadbeef28e29192017-07-27 09:14:38 -0700332 maxIPv6Networks = 5;
Steve Antond960a0c2017-07-17 12:33:07 -0700333 iceRegatherIntervalRange = null;
Jiayang Liucac1b382015-04-30 12:35:24 -0700334 }
335 };
336
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337 private final List<MediaStream> localStreams;
338 private final long nativePeerConnection;
339 private final long nativeObserver;
deadbeef4139c0f2015-10-06 12:29:25 -0700340 private List<RtpSender> senders;
341 private List<RtpReceiver> receivers;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342
343 PeerConnection(long nativePeerConnection, long nativeObserver) {
344 this.nativePeerConnection = nativePeerConnection;
345 this.nativeObserver = nativeObserver;
346 localStreams = new LinkedList<MediaStream>();
deadbeef4139c0f2015-10-06 12:29:25 -0700347 senders = new LinkedList<RtpSender>();
348 receivers = new LinkedList<RtpReceiver>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349 }
350
351 // JsepInterface.
352 public native SessionDescription getLocalDescription();
353
354 public native SessionDescription getRemoteDescription();
355
sakalb6760f92016-09-29 04:12:44 -0700356 public native DataChannel createDataChannel(String label, DataChannel.Init init);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000357
sakalb6760f92016-09-29 04:12:44 -0700358 public native void createOffer(SdpObserver observer, MediaConstraints constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359
sakalb6760f92016-09-29 04:12:44 -0700360 public native void createAnswer(SdpObserver observer, MediaConstraints constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361
sakalb6760f92016-09-29 04:12:44 -0700362 public native void setLocalDescription(SdpObserver observer, SessionDescription sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363
sakalb6760f92016-09-29 04:12:44 -0700364 public native void setRemoteDescription(SdpObserver observer, SessionDescription sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365
deadbeef5d0b6d82017-01-09 16:05:28 -0800366 public boolean setConfiguration(RTCConfiguration config) {
367 return nativeSetConfiguration(config, nativeObserver);
368 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369
370 public boolean addIceCandidate(IceCandidate candidate) {
sakalb6760f92016-09-29 04:12:44 -0700371 return nativeAddIceCandidate(candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000372 }
373
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700374 public boolean removeIceCandidates(final IceCandidate[] candidates) {
375 return nativeRemoveIceCandidates(candidates);
376 }
377
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000378 public boolean addStream(MediaStream stream) {
379 boolean ret = nativeAddLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380 if (!ret) {
381 return false;
382 }
383 localStreams.add(stream);
384 return true;
385 }
386
387 public void removeStream(MediaStream stream) {
388 nativeRemoveLocalStream(stream.nativeStream);
389 localStreams.remove(stream);
390 }
391
deadbeef7a246882017-08-09 08:40:10 -0700392 /**
393 * Creates an RtpSender without a track.
394 * <p>
395 * This method allows an application to cause the PeerConnection to negotiate
396 * sending/receiving a specific media type, but without having a track to
397 * send yet.
398 * <p>
399 * When the application does want to begin sending a track, it can call
400 * RtpSender.setTrack, which doesn't require any additional SDP negotiation.
401 * <p>
402 * Example use:
403 * <pre>
404 * {@code
405 * audioSender = pc.createSender("audio", "stream1");
406 * videoSender = pc.createSender("video", "stream1");
407 * // Do normal SDP offer/answer, which will kick off ICE/DTLS and negotiate
408 * // media parameters....
409 * // Later, when the endpoint is ready to actually begin sending:
410 * audioSender.setTrack(audioTrack, false);
411 * videoSender.setTrack(videoTrack, false);
412 * }
413 * </pre>
414 * Note: This corresponds most closely to "addTransceiver" in the official
415 * WebRTC API, in that it creates a sender without a track. It was
416 * implemented before addTransceiver because it provides useful
417 * functionality, and properly implementing transceivers would have required
418 * a great deal more work.
419 *
420 * @param kind Corresponds to MediaStreamTrack kinds (must be "audio" or
421 * "video").
422 * @param stream_id The ID of the MediaStream that this sender's track will
423 * be associated with when SDP is applied to the remote
424 * PeerConnection. If createSender is used to create an
425 * audio and video sender that should be synchronized, they
426 * should use the same stream ID.
427 * @return A new RtpSender object if successful, or null otherwise.
428 */
deadbeefbd7d8f72015-12-18 16:58:44 -0800429 public RtpSender createSender(String kind, String stream_id) {
430 RtpSender new_sender = nativeCreateSender(kind, stream_id);
deadbeefee524f72015-12-02 11:27:40 -0800431 if (new_sender != null) {
432 senders.add(new_sender);
433 }
434 return new_sender;
435 }
436
deadbeef4139c0f2015-10-06 12:29:25 -0700437 // Note that calling getSenders will dispose of the senders previously
438 // returned (and same goes for getReceivers).
439 public List<RtpSender> getSenders() {
440 for (RtpSender sender : senders) {
441 sender.dispose();
442 }
443 senders = nativeGetSenders();
444 return Collections.unmodifiableList(senders);
445 }
446
447 public List<RtpReceiver> getReceivers() {
448 for (RtpReceiver receiver : receivers) {
449 receiver.dispose();
450 }
451 receivers = nativeGetReceivers();
452 return Collections.unmodifiableList(receivers);
453 }
454
deadbeef82215872017-04-18 10:27:51 -0700455 // Older, non-standard implementation of getStats.
456 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457 public boolean getStats(StatsObserver observer, MediaStreamTrack track) {
deadbeef82215872017-04-18 10:27:51 -0700458 return nativeOldGetStats(observer, (track == null) ? 0 : track.nativeTrack);
459 }
460
461 // Gets stats using the new stats collection API, see webrtc/api/stats/. These
462 // will replace old stats collection API when the new API has matured enough.
463 public void getStats(RTCStatsCollectorCallback callback) {
464 nativeNewGetStats(callback);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 }
466
zsteind89b0bc2017-08-03 11:11:40 -0700467 // Limits the bandwidth allocated for all RTP streams sent by this
468 // PeerConnection. Pass null to leave a value unchanged.
469 public native boolean setBitrate(Integer min, Integer current, Integer max);
470
ivoc14d5dbe2016-07-04 07:06:55 -0700471 // Starts recording an RTC event log. Ownership of the file is transfered to
472 // the native code. If an RTC event log is already being recorded, it will be
473 // stopped and a new one will start using the provided file. Logging will
474 // continue until the stopRtcEventLog function is called. The max_size_bytes
475 // argument is ignored, it is added for future use.
ivoc0c6f0f62016-07-06 04:34:23 -0700476 public boolean startRtcEventLog(int file_descriptor, int max_size_bytes) {
477 return nativeStartRtcEventLog(file_descriptor, max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -0700478 }
479
480 // Stops recording an RTC event log. If no RTC event log is currently being
481 // recorded, this call will have no effect.
482 public void stopRtcEventLog() {
ivoc0c6f0f62016-07-06 04:34:23 -0700483 nativeStopRtcEventLog();
ivoc14d5dbe2016-07-04 07:06:55 -0700484 }
485
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 // TODO(fischman): add support for DTMF-related methods once that API
487 // stabilizes.
488 public native SignalingState signalingState();
489
490 public native IceConnectionState iceConnectionState();
491
492 public native IceGatheringState iceGatheringState();
493
494 public native void close();
495
deadbeef43697f62017-09-12 10:52:14 -0700496 /**
497 * Free native resources associated with this PeerConnection instance.
498 * <p>
499 * This method removes a reference count from the C++ PeerConnection object,
500 * which should result in it being destroyed. It also calls equivalent
501 * "dispose" methods on the Java objects attached to this PeerConnection
502 * (streams, senders, receivers), such that their associated C++ objects
503 * will also be destroyed.
504 * <p>
505 * Note that this method cannot be safely called from an observer callback
506 * (PeerConnection.Observer, DataChannel.Observer, etc.). If you want to, for
507 * example, destroy the PeerConnection after an "ICE failed" callback, you
508 * must do this asynchronously (in other words, unwind the stack first). See
509 * <a href="https://bugs.chromium.org/p/webrtc/issues/detail?id=3721">bug
510 * 3721</a> for more details.
511 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 public void dispose() {
513 close();
514 for (MediaStream stream : localStreams) {
fischman@webrtc.org32001ef2013-08-12 23:26:21 +0000515 nativeRemoveLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516 stream.dispose();
517 }
518 localStreams.clear();
deadbeef4139c0f2015-10-06 12:29:25 -0700519 for (RtpSender sender : senders) {
520 sender.dispose();
521 }
522 senders.clear();
523 for (RtpReceiver receiver : receivers) {
524 receiver.dispose();
525 }
526 receivers.clear();
magjedb1c74532017-08-27 13:47:20 -0700527 JniCommon.nativeReleaseRef(nativePeerConnection);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 freeObserver(nativeObserver);
529 }
530
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 private static native void freeObserver(long nativeObserver);
532
deadbeef5d0b6d82017-01-09 16:05:28 -0800533 public native boolean nativeSetConfiguration(RTCConfiguration config, long nativeObserver);
534
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 private native boolean nativeAddIceCandidate(
536 String sdpMid, int sdpMLineIndex, String iceCandidateSdp);
537
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700538 private native boolean nativeRemoveIceCandidates(final IceCandidate[] candidates);
539
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000540 private native boolean nativeAddLocalStream(long nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541
542 private native void nativeRemoveLocalStream(long nativeStream);
543
deadbeef82215872017-04-18 10:27:51 -0700544 private native boolean nativeOldGetStats(StatsObserver observer, long nativeTrack);
545
546 private native void nativeNewGetStats(RTCStatsCollectorCallback callback);
deadbeef4139c0f2015-10-06 12:29:25 -0700547
deadbeefbd7d8f72015-12-18 16:58:44 -0800548 private native RtpSender nativeCreateSender(String kind, String stream_id);
deadbeefee524f72015-12-02 11:27:40 -0800549
deadbeef4139c0f2015-10-06 12:29:25 -0700550 private native List<RtpSender> nativeGetSenders();
551
552 private native List<RtpReceiver> nativeGetReceivers();
ivoc14d5dbe2016-07-04 07:06:55 -0700553
sakalb6760f92016-09-29 04:12:44 -0700554 private native boolean nativeStartRtcEventLog(int file_descriptor, int max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -0700555
ivoc0c6f0f62016-07-06 04:34:23 -0700556 private native void nativeStopRtcEventLog();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557}