blob: 24425f09fb27c38b7e68d5bee0b29fb084c446ef [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;
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 {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024 /** Tracks PeerConnectionInterface::IceGatheringState */
Magnus Jedvertba700f62017-12-04 13:43:27 +010025 public enum IceGatheringState {
26 NEW,
27 GATHERING,
28 COMPLETE;
29
30 @CalledByNative("IceGatheringState")
31 static IceGatheringState fromNativeIndex(int nativeIndex) {
32 return values()[nativeIndex];
33 }
34 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
36 /** Tracks PeerConnectionInterface::IceConnectionState */
37 public enum IceConnectionState {
sakalb6760f92016-09-29 04:12:44 -070038 NEW,
39 CHECKING,
40 CONNECTED,
41 COMPLETED,
42 FAILED,
43 DISCONNECTED,
Magnus Jedvertba700f62017-12-04 13:43:27 +010044 CLOSED;
45
46 @CalledByNative("IceConnectionState")
47 static IceConnectionState fromNativeIndex(int nativeIndex) {
48 return values()[nativeIndex];
49 }
sakalb6760f92016-09-29 04:12:44 -070050 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
hnsl04833622017-01-09 08:35:45 -080052 /** Tracks PeerConnectionInterface::TlsCertPolicy */
53 public enum TlsCertPolicy {
54 TLS_CERT_POLICY_SECURE,
55 TLS_CERT_POLICY_INSECURE_NO_CHECK,
56 }
57
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 /** Tracks PeerConnectionInterface::SignalingState */
59 public enum SignalingState {
sakalb6760f92016-09-29 04:12:44 -070060 STABLE,
61 HAVE_LOCAL_OFFER,
62 HAVE_LOCAL_PRANSWER,
63 HAVE_REMOTE_OFFER,
64 HAVE_REMOTE_PRANSWER,
Magnus Jedvertba700f62017-12-04 13:43:27 +010065 CLOSED;
66
67 @CalledByNative("SignalingState")
68 static SignalingState fromNativeIndex(int nativeIndex) {
69 return values()[nativeIndex];
70 }
sakalb6760f92016-09-29 04:12:44 -070071 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072
73 /** Java version of PeerConnectionObserver. */
74 public static interface Observer {
75 /** Triggered when the SignalingState changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010076 @CalledByNative("Observer") void onSignalingChange(SignalingState newState);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077
78 /** Triggered when the IceConnectionState changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010079 @CalledByNative("Observer") void onIceConnectionChange(IceConnectionState newState);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080
Peter Thatcher54360512015-07-08 11:08:35 -070081 /** Triggered when the ICE connection receiving status changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010082 @CalledByNative("Observer") void onIceConnectionReceivingChange(boolean receiving);
Peter Thatcher54360512015-07-08 11:08:35 -070083
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 /** Triggered when the IceGatheringState changes. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010085 @CalledByNative("Observer") void onIceGatheringChange(IceGatheringState newState);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086
87 /** Triggered when a new ICE candidate has been found. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010088 @CalledByNative("Observer") void onIceCandidate(IceCandidate candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089
Honghai Zhang7fb69db2016-03-14 11:59:18 -070090 /** Triggered when some ICE candidates have been removed. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010091 @CalledByNative("Observer") void onIceCandidatesRemoved(IceCandidate[] candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -070092
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 /** Triggered when media is received on a new stream from remote peer. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010094 @CalledByNative("Observer") void onAddStream(MediaStream stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
96 /** Triggered when a remote peer close a stream. */
Magnus Jedvertba700f62017-12-04 13:43:27 +010097 @CalledByNative("Observer") void onRemoveStream(MediaStream stream);
henrike@webrtc.org723d6832013-07-12 16:04:50 +000098
99 /** Triggered when a remote peer opens a DataChannel. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100100 @CalledByNative("Observer") void onDataChannel(DataChannel dataChannel);
fischman@webrtc.orgd7568a02014-01-13 22:04:12 +0000101
102 /** Triggered when renegotiation is necessary. */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100103 @CalledByNative("Observer") void onRenegotiationNeeded();
zhihuangdcccda72016-12-21 14:08:03 -0800104
105 /**
106 * Triggered when a new track is signaled by the remote peer, as a result of
107 * setRemoteDescription.
108 */
Magnus Jedvertba700f62017-12-04 13:43:27 +0100109 @CalledByNative("Observer") void onAddTrack(RtpReceiver receiver, MediaStream[] mediaStreams);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 }
111
112 /** Java version of PeerConnectionInterface.IceServer. */
113 public static class IceServer {
Emad Omaradab1d2d2017-06-16 15:43:11 -0700114 // List of URIs associated with this server. Valid formats are described
115 // in RFC7064 and RFC7065, and more may be added in the future. The "host"
116 // part of the URI may contain either an IP address or a hostname.
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700117 @Deprecated public final String uri;
118 public final List<String> urls;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 public final String username;
120 public final String password;
hnsl04833622017-01-09 08:35:45 -0800121 public final TlsCertPolicy tlsCertPolicy;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122
Emad Omaradab1d2d2017-06-16 15:43:11 -0700123 // If the URIs in |urls| only contain IP addresses, this field can be used
124 // to indicate the hostname, which may be necessary for TLS (using the SNI
125 // extension). If |urls| itself contains the hostname, this isn't
126 // necessary.
127 public final String hostname;
128
Diogo Real1dca9d52017-08-29 12:18:32 -0700129 // List of protocols to be used in the TLS ALPN extension.
130 public final List<String> tlsAlpnProtocols;
131
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700132 // List of elliptic curves to be used in the TLS elliptic curves extension.
133 // Only curve names supported by OpenSSL should be used (eg. "P-256","X25519").
134 public final List<String> tlsEllipticCurves;
135
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 /** Convenience constructor for STUN servers. */
Diogo Real05ea2b32017-08-31 00:12:58 -0700137 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 public IceServer(String uri) {
139 this(uri, "", "");
140 }
141
Diogo Real05ea2b32017-08-31 00:12:58 -0700142 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 public IceServer(String uri, String username, String password) {
hnsl04833622017-01-09 08:35:45 -0800144 this(uri, username, password, TlsCertPolicy.TLS_CERT_POLICY_SECURE);
145 }
146
Diogo Real05ea2b32017-08-31 00:12:58 -0700147 @Deprecated
hnsl04833622017-01-09 08:35:45 -0800148 public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy) {
Emad Omaradab1d2d2017-06-16 15:43:11 -0700149 this(uri, username, password, tlsCertPolicy, "");
150 }
151
Diogo Real05ea2b32017-08-31 00:12:58 -0700152 @Deprecated
Emad Omaradab1d2d2017-06-16 15:43:11 -0700153 public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy,
154 String hostname) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700155 this(uri, Collections.singletonList(uri), username, password, tlsCertPolicy, hostname, null,
156 null);
Diogo Real1dca9d52017-08-29 12:18:32 -0700157 }
158
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700159 private IceServer(String uri, List<String> urls, String username, String password,
160 TlsCertPolicy tlsCertPolicy, String hostname, List<String> tlsAlpnProtocols,
161 List<String> tlsEllipticCurves) {
162 if (uri == null || urls == null || urls.isEmpty()) {
163 throw new IllegalArgumentException("uri == null || urls == null || urls.isEmpty()");
164 }
165 for (String it : urls) {
166 if (it == null) {
167 throw new IllegalArgumentException("urls element is null: " + urls);
168 }
169 }
170 if (username == null) {
171 throw new IllegalArgumentException("username == null");
172 }
173 if (password == null) {
174 throw new IllegalArgumentException("password == null");
175 }
176 if (hostname == null) {
177 throw new IllegalArgumentException("hostname == null");
178 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 this.uri = uri;
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700180 this.urls = urls;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 this.username = username;
182 this.password = password;
hnsl04833622017-01-09 08:35:45 -0800183 this.tlsCertPolicy = tlsCertPolicy;
Emad Omaradab1d2d2017-06-16 15:43:11 -0700184 this.hostname = hostname;
Diogo Real1dca9d52017-08-29 12:18:32 -0700185 this.tlsAlpnProtocols = tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700186 this.tlsEllipticCurves = tlsEllipticCurves;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 }
188
Sami Kalliomäkibde473e2017-10-30 13:34:41 +0100189 @Override
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 public String toString() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700191 return urls + " [" + username + ":" + password + "] [" + tlsCertPolicy + "] [" + hostname
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700192 + "] [" + tlsAlpnProtocols + "] [" + tlsEllipticCurves + "]";
Diogo Real1dca9d52017-08-29 12:18:32 -0700193 }
194
195 public static Builder builder(String uri) {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700196 return new Builder(Collections.singletonList(uri));
197 }
198
199 public static Builder builder(List<String> urls) {
200 return new Builder(urls);
Diogo Real1dca9d52017-08-29 12:18:32 -0700201 }
202
203 public static class Builder {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700204 private final List<String> urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700205 private String username = "";
206 private String password = "";
207 private TlsCertPolicy tlsCertPolicy = TlsCertPolicy.TLS_CERT_POLICY_SECURE;
208 private String hostname = "";
209 private List<String> tlsAlpnProtocols;
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700210 private List<String> tlsEllipticCurves;
Diogo Real1dca9d52017-08-29 12:18:32 -0700211
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700212 private Builder(List<String> urls) {
213 if (urls == null || urls.isEmpty()) {
214 throw new IllegalArgumentException("urls == null || urls.isEmpty(): " + urls);
215 }
216 this.urls = urls;
Diogo Real1dca9d52017-08-29 12:18:32 -0700217 }
218
219 public Builder setUsername(String username) {
220 this.username = username;
221 return this;
222 }
223
224 public Builder setPassword(String password) {
225 this.password = password;
226 return this;
227 }
228
229 public Builder setTlsCertPolicy(TlsCertPolicy tlsCertPolicy) {
230 this.tlsCertPolicy = tlsCertPolicy;
231 return this;
232 }
233
234 public Builder setHostname(String hostname) {
235 this.hostname = hostname;
236 return this;
237 }
238
239 public Builder setTlsAlpnProtocols(List<String> tlsAlpnProtocols) {
240 this.tlsAlpnProtocols = tlsAlpnProtocols;
241 return this;
242 }
243
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700244 public Builder setTlsEllipticCurves(List<String> tlsEllipticCurves) {
245 this.tlsEllipticCurves = tlsEllipticCurves;
246 return this;
247 }
248
Diogo Real1dca9d52017-08-29 12:18:32 -0700249 public IceServer createIceServer() {
korniltsev.anatoly0ea03102017-09-11 06:41:38 -0700250 return new IceServer(urls.get(0), urls, username, password, tlsCertPolicy, hostname,
251 tlsAlpnProtocols, tlsEllipticCurves);
Diogo Real1dca9d52017-08-29 12:18:32 -0700252 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 }
254 }
255
Jiayang Liucac1b382015-04-30 12:35:24 -0700256 /** Java version of PeerConnectionInterface.IceTransportsType */
sakalb6760f92016-09-29 04:12:44 -0700257 public enum IceTransportsType { NONE, RELAY, NOHOST, ALL }
Jiayang Liucac1b382015-04-30 12:35:24 -0700258
259 /** Java version of PeerConnectionInterface.BundlePolicy */
sakalb6760f92016-09-29 04:12:44 -0700260 public enum BundlePolicy { BALANCED, MAXBUNDLE, MAXCOMPAT }
Jiayang Liucac1b382015-04-30 12:35:24 -0700261
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700262 /** Java version of PeerConnectionInterface.RtcpMuxPolicy */
sakalb6760f92016-09-29 04:12:44 -0700263 public enum RtcpMuxPolicy { NEGOTIATE, REQUIRE }
glaznev97579a42015-09-01 11:31:27 -0700264
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700265 /** Java version of PeerConnectionInterface.TcpCandidatePolicy */
sakalb6760f92016-09-29 04:12:44 -0700266 public enum TcpCandidatePolicy { ENABLED, DISABLED }
Jiayang Liucac1b382015-04-30 12:35:24 -0700267
honghaiz60347052016-05-31 18:29:12 -0700268 /** Java version of PeerConnectionInterface.CandidateNetworkPolicy */
sakalb6760f92016-09-29 04:12:44 -0700269 public enum CandidateNetworkPolicy { ALL, LOW_COST }
honghaiz60347052016-05-31 18:29:12 -0700270
glaznev97579a42015-09-01 11:31:27 -0700271 /** Java version of rtc::KeyType */
sakalb6760f92016-09-29 04:12:44 -0700272 public enum KeyType { RSA, ECDSA }
glaznev97579a42015-09-01 11:31:27 -0700273
honghaiz1f429e32015-09-28 07:57:34 -0700274 /** Java version of PeerConnectionInterface.ContinualGatheringPolicy */
sakalb6760f92016-09-29 04:12:44 -0700275 public enum ContinualGatheringPolicy { GATHER_ONCE, GATHER_CONTINUALLY }
honghaiz1f429e32015-09-28 07:57:34 -0700276
Steve Antond960a0c2017-07-17 12:33:07 -0700277 /** Java version of rtc::IntervalRange */
278 public static class IntervalRange {
279 private final int min;
280 private final int max;
281
282 public IntervalRange(int min, int max) {
283 this.min = min;
284 this.max = max;
285 }
286
287 public int getMin() {
288 return min;
289 }
290
291 public int getMax() {
292 return max;
293 }
294 }
295
Jiayang Liucac1b382015-04-30 12:35:24 -0700296 /** Java version of PeerConnectionInterface.RTCConfiguration */
297 public static class RTCConfiguration {
298 public IceTransportsType iceTransportsType;
299 public List<IceServer> iceServers;
300 public BundlePolicy bundlePolicy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700301 public RtcpMuxPolicy rtcpMuxPolicy;
Jiayang Liucac1b382015-04-30 12:35:24 -0700302 public TcpCandidatePolicy tcpCandidatePolicy;
honghaiz60347052016-05-31 18:29:12 -0700303 public CandidateNetworkPolicy candidateNetworkPolicy;
Henrik Lundin64dad832015-05-11 12:44:23 +0200304 public int audioJitterBufferMaxPackets;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200305 public boolean audioJitterBufferFastAccelerate;
honghaiz4edc39c2015-09-01 09:53:56 -0700306 public int iceConnectionReceivingTimeout;
Honghai Zhang381b4212015-12-04 12:24:03 -0800307 public int iceBackupCandidatePairPingInterval;
glaznev97579a42015-09-01 11:31:27 -0700308 public KeyType keyType;
honghaiz1f429e32015-09-28 07:57:34 -0700309 public ContinualGatheringPolicy continualGatheringPolicy;
deadbeefbe0c96f2016-05-18 16:20:14 -0700310 public int iceCandidatePoolSize;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700311 public boolean pruneTurnPorts;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700312 public boolean presumeWritableWhenFullyRelayed;
skvlad51072462017-02-02 11:50:14 -0800313 public Integer iceCheckMinInterval;
zhihuangb09b3f92017-03-07 14:40:51 -0800314 public boolean disableIPv6OnWifi;
deadbeef28e29192017-07-27 09:14:38 -0700315 // By default, PeerConnection will use a limited number of IPv6 network
316 // interfaces, in order to avoid too many ICE candidate pairs being created
317 // and delaying ICE completion.
318 //
319 // Can be set to Integer.MAX_VALUE to effectively disable the limit.
320 public int maxIPv6Networks;
Steve Antond960a0c2017-07-17 12:33:07 -0700321 public IntervalRange iceRegatherIntervalRange;
Jiayang Liucac1b382015-04-30 12:35:24 -0700322
Jonas Orelandbdcee282017-10-10 14:01:40 +0200323 // This is an optional wrapper for the C++ webrtc::TurnCustomizer.
324 public TurnCustomizer turnCustomizer;
325
deadbeef28e29192017-07-27 09:14:38 -0700326 // TODO(deadbeef): Instead of duplicating the defaults here, we should do
327 // something to pick up the defaults from C++. The Objective-C equivalent
328 // of RTCConfiguration does that.
Jiayang Liucac1b382015-04-30 12:35:24 -0700329 public RTCConfiguration(List<IceServer> iceServers) {
330 iceTransportsType = IceTransportsType.ALL;
331 bundlePolicy = BundlePolicy.BALANCED;
zhihuang4dfb8ce2016-11-23 10:30:12 -0800332 rtcpMuxPolicy = RtcpMuxPolicy.REQUIRE;
Jiayang Liucac1b382015-04-30 12:35:24 -0700333 tcpCandidatePolicy = TcpCandidatePolicy.ENABLED;
Sami Kalliomäki9828beb2017-10-26 16:21:22 +0200334 candidateNetworkPolicy = CandidateNetworkPolicy.ALL;
Jiayang Liucac1b382015-04-30 12:35:24 -0700335 this.iceServers = iceServers;
Henrik Lundin64dad832015-05-11 12:44:23 +0200336 audioJitterBufferMaxPackets = 50;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200337 audioJitterBufferFastAccelerate = false;
honghaiz4edc39c2015-09-01 09:53:56 -0700338 iceConnectionReceivingTimeout = -1;
Honghai Zhang381b4212015-12-04 12:24:03 -0800339 iceBackupCandidatePairPingInterval = -1;
glaznev97579a42015-09-01 11:31:27 -0700340 keyType = KeyType.ECDSA;
honghaiz1f429e32015-09-28 07:57:34 -0700341 continualGatheringPolicy = ContinualGatheringPolicy.GATHER_ONCE;
deadbeefbe0c96f2016-05-18 16:20:14 -0700342 iceCandidatePoolSize = 0;
Honghai Zhangd78ecf72016-07-01 14:40:40 -0700343 pruneTurnPorts = false;
Taylor Brandstettere9851112016-07-01 11:11:13 -0700344 presumeWritableWhenFullyRelayed = false;
skvlad51072462017-02-02 11:50:14 -0800345 iceCheckMinInterval = null;
zhihuangb09b3f92017-03-07 14:40:51 -0800346 disableIPv6OnWifi = false;
deadbeef28e29192017-07-27 09:14:38 -0700347 maxIPv6Networks = 5;
Steve Antond960a0c2017-07-17 12:33:07 -0700348 iceRegatherIntervalRange = null;
Jiayang Liucac1b382015-04-30 12:35:24 -0700349 }
350 };
351
Magnus Jedvert6062f372017-11-16 16:53:12 +0100352 private final List<MediaStream> localStreams = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 private final long nativePeerConnection;
354 private final long nativeObserver;
Magnus Jedvert6062f372017-11-16 16:53:12 +0100355 private List<RtpSender> senders = new ArrayList<>();
356 private List<RtpReceiver> receivers = new ArrayList<>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357
358 PeerConnection(long nativePeerConnection, long nativeObserver) {
359 this.nativePeerConnection = nativePeerConnection;
360 this.nativeObserver = nativeObserver;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361 }
362
363 // JsepInterface.
364 public native SessionDescription getLocalDescription();
365
366 public native SessionDescription getRemoteDescription();
367
sakalb6760f92016-09-29 04:12:44 -0700368 public native DataChannel createDataChannel(String label, DataChannel.Init init);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000369
sakalb6760f92016-09-29 04:12:44 -0700370 public native void createOffer(SdpObserver observer, MediaConstraints constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371
sakalb6760f92016-09-29 04:12:44 -0700372 public native void createAnswer(SdpObserver observer, MediaConstraints constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373
sakalb6760f92016-09-29 04:12:44 -0700374 public native void setLocalDescription(SdpObserver observer, SessionDescription sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375
sakalb6760f92016-09-29 04:12:44 -0700376 public native void setRemoteDescription(SdpObserver observer, SessionDescription sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377
henrika5f6bf242017-11-01 11:06:56 +0100378 // True if remote audio should be played out. Defaults to true.
379 // Note that even if playout is enabled, streams will only be played out if
380 // the appropriate SDP is also applied. The main purpose of this API is to
381 // be able to control the exact time when audio playout starts.
382 public native void setAudioPlayout(boolean playout);
383
384 // True if local audio shall be recorded. Defaults to true.
385 // Note that even if recording is enabled, streams will only be recorded if
386 // the appropriate SDP is also applied. The main purpose of this API is to
387 // be able to control the exact time when audio recording starts.
388 public native void setAudioRecording(boolean recording);
389
deadbeef5d0b6d82017-01-09 16:05:28 -0800390 public boolean setConfiguration(RTCConfiguration config) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100391 return setNativeConfiguration(config, nativeObserver);
deadbeef5d0b6d82017-01-09 16:05:28 -0800392 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393
394 public boolean addIceCandidate(IceCandidate candidate) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100395 return addNativeIceCandidate(candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396 }
397
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700398 public boolean removeIceCandidates(final IceCandidate[] candidates) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100399 return removeNativeIceCandidates(candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700400 }
401
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000402 public boolean addStream(MediaStream stream) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100403 boolean ret = addNativeLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 if (!ret) {
405 return false;
406 }
407 localStreams.add(stream);
408 return true;
409 }
410
411 public void removeStream(MediaStream stream) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100412 removeNativeLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 localStreams.remove(stream);
414 }
415
deadbeef7a246882017-08-09 08:40:10 -0700416 /**
417 * Creates an RtpSender without a track.
418 * <p>
419 * This method allows an application to cause the PeerConnection to negotiate
420 * sending/receiving a specific media type, but without having a track to
421 * send yet.
422 * <p>
423 * When the application does want to begin sending a track, it can call
424 * RtpSender.setTrack, which doesn't require any additional SDP negotiation.
425 * <p>
426 * Example use:
427 * <pre>
428 * {@code
429 * audioSender = pc.createSender("audio", "stream1");
430 * videoSender = pc.createSender("video", "stream1");
431 * // Do normal SDP offer/answer, which will kick off ICE/DTLS and negotiate
432 * // media parameters....
433 * // Later, when the endpoint is ready to actually begin sending:
434 * audioSender.setTrack(audioTrack, false);
435 * videoSender.setTrack(videoTrack, false);
436 * }
437 * </pre>
438 * Note: This corresponds most closely to "addTransceiver" in the official
439 * WebRTC API, in that it creates a sender without a track. It was
440 * implemented before addTransceiver because it provides useful
441 * functionality, and properly implementing transceivers would have required
442 * a great deal more work.
443 *
444 * @param kind Corresponds to MediaStreamTrack kinds (must be "audio" or
445 * "video").
446 * @param stream_id The ID of the MediaStream that this sender's track will
447 * be associated with when SDP is applied to the remote
448 * PeerConnection. If createSender is used to create an
449 * audio and video sender that should be synchronized, they
450 * should use the same stream ID.
451 * @return A new RtpSender object if successful, or null otherwise.
452 */
deadbeefbd7d8f72015-12-18 16:58:44 -0800453 public RtpSender createSender(String kind, String stream_id) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100454 RtpSender new_sender = createNativeSender(kind, stream_id);
deadbeefee524f72015-12-02 11:27:40 -0800455 if (new_sender != null) {
456 senders.add(new_sender);
457 }
458 return new_sender;
459 }
460
deadbeef4139c0f2015-10-06 12:29:25 -0700461 // Note that calling getSenders will dispose of the senders previously
462 // returned (and same goes for getReceivers).
463 public List<RtpSender> getSenders() {
464 for (RtpSender sender : senders) {
465 sender.dispose();
466 }
Magnus Jedvertba700f62017-12-04 13:43:27 +0100467 senders = getNativeSenders();
deadbeef4139c0f2015-10-06 12:29:25 -0700468 return Collections.unmodifiableList(senders);
469 }
470
471 public List<RtpReceiver> getReceivers() {
472 for (RtpReceiver receiver : receivers) {
473 receiver.dispose();
474 }
Magnus Jedvertba700f62017-12-04 13:43:27 +0100475 receivers = getNativeReceivers();
deadbeef4139c0f2015-10-06 12:29:25 -0700476 return Collections.unmodifiableList(receivers);
477 }
478
deadbeef82215872017-04-18 10:27:51 -0700479 // Older, non-standard implementation of getStats.
480 @Deprecated
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 public boolean getStats(StatsObserver observer, MediaStreamTrack track) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100482 return oldGetNativeStats(observer, (track == null) ? 0 : track.nativeTrack);
deadbeef82215872017-04-18 10:27:51 -0700483 }
484
485 // Gets stats using the new stats collection API, see webrtc/api/stats/. These
486 // will replace old stats collection API when the new API has matured enough.
487 public void getStats(RTCStatsCollectorCallback callback) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100488 newGetNativeStats(callback);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 }
490
zsteind89b0bc2017-08-03 11:11:40 -0700491 // Limits the bandwidth allocated for all RTP streams sent by this
492 // PeerConnection. Pass null to leave a value unchanged.
493 public native boolean setBitrate(Integer min, Integer current, Integer max);
494
ivoc14d5dbe2016-07-04 07:06:55 -0700495 // Starts recording an RTC event log. Ownership of the file is transfered to
496 // the native code. If an RTC event log is already being recorded, it will be
497 // stopped and a new one will start using the provided file. Logging will
498 // continue until the stopRtcEventLog function is called. The max_size_bytes
499 // argument is ignored, it is added for future use.
ivoc0c6f0f62016-07-06 04:34:23 -0700500 public boolean startRtcEventLog(int file_descriptor, int max_size_bytes) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100501 return startNativeRtcEventLog(file_descriptor, max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -0700502 }
503
504 // Stops recording an RTC event log. If no RTC event log is currently being
505 // recorded, this call will have no effect.
506 public void stopRtcEventLog() {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100507 stopNativeRtcEventLog();
ivoc14d5dbe2016-07-04 07:06:55 -0700508 }
509
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510 // TODO(fischman): add support for DTMF-related methods once that API
511 // stabilizes.
512 public native SignalingState signalingState();
513
514 public native IceConnectionState iceConnectionState();
515
516 public native IceGatheringState iceGatheringState();
517
518 public native void close();
519
deadbeef43697f62017-09-12 10:52:14 -0700520 /**
521 * Free native resources associated with this PeerConnection instance.
522 * <p>
523 * This method removes a reference count from the C++ PeerConnection object,
524 * which should result in it being destroyed. It also calls equivalent
525 * "dispose" methods on the Java objects attached to this PeerConnection
526 * (streams, senders, receivers), such that their associated C++ objects
527 * will also be destroyed.
528 * <p>
529 * Note that this method cannot be safely called from an observer callback
530 * (PeerConnection.Observer, DataChannel.Observer, etc.). If you want to, for
531 * example, destroy the PeerConnection after an "ICE failed" callback, you
532 * must do this asynchronously (in other words, unwind the stack first). See
533 * <a href="https://bugs.chromium.org/p/webrtc/issues/detail?id=3721">bug
534 * 3721</a> for more details.
535 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 public void dispose() {
537 close();
538 for (MediaStream stream : localStreams) {
Magnus Jedvertba700f62017-12-04 13:43:27 +0100539 removeNativeLocalStream(stream.nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540 stream.dispose();
541 }
542 localStreams.clear();
deadbeef4139c0f2015-10-06 12:29:25 -0700543 for (RtpSender sender : senders) {
544 sender.dispose();
545 }
546 senders.clear();
547 for (RtpReceiver receiver : receivers) {
548 receiver.dispose();
549 }
550 receivers.clear();
magjedb1c74532017-08-27 13:47:20 -0700551 JniCommon.nativeReleaseRef(nativePeerConnection);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552 freeObserver(nativeObserver);
553 }
554
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 private static native void freeObserver(long nativeObserver);
556
Magnus Jedvertba700f62017-12-04 13:43:27 +0100557 private native boolean setNativeConfiguration(RTCConfiguration config, long nativeObserver);
deadbeef5d0b6d82017-01-09 16:05:28 -0800558
Magnus Jedvertba700f62017-12-04 13:43:27 +0100559 private native boolean addNativeIceCandidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 String sdpMid, int sdpMLineIndex, String iceCandidateSdp);
561
Magnus Jedvertba700f62017-12-04 13:43:27 +0100562 private native boolean removeNativeIceCandidates(final IceCandidate[] candidates);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700563
Magnus Jedvertba700f62017-12-04 13:43:27 +0100564 private native boolean addNativeLocalStream(long nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565
Magnus Jedvertba700f62017-12-04 13:43:27 +0100566 private native void removeNativeLocalStream(long nativeStream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567
Magnus Jedvertba700f62017-12-04 13:43:27 +0100568 private native boolean oldGetNativeStats(StatsObserver observer, long nativeTrack);
deadbeef82215872017-04-18 10:27:51 -0700569
Magnus Jedvertba700f62017-12-04 13:43:27 +0100570 private native void newGetNativeStats(RTCStatsCollectorCallback callback);
deadbeef4139c0f2015-10-06 12:29:25 -0700571
Magnus Jedvertba700f62017-12-04 13:43:27 +0100572 private native RtpSender createNativeSender(String kind, String stream_id);
deadbeefee524f72015-12-02 11:27:40 -0800573
Magnus Jedvertba700f62017-12-04 13:43:27 +0100574 private native List<RtpSender> getNativeSenders();
deadbeef4139c0f2015-10-06 12:29:25 -0700575
Magnus Jedvertba700f62017-12-04 13:43:27 +0100576 private native List<RtpReceiver> getNativeReceivers();
ivoc14d5dbe2016-07-04 07:06:55 -0700577
Magnus Jedvertba700f62017-12-04 13:43:27 +0100578 private native boolean startNativeRtcEventLog(int file_descriptor, int max_size_bytes);
ivoc14d5dbe2016-07-04 07:06:55 -0700579
Magnus Jedvertba700f62017-12-04 13:43:27 +0100580 private native void stopNativeRtcEventLog();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581}