henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 11 | package org.webrtc; |
| 12 | |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 13 | import java.util.Collections; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 14 | import java.util.LinkedList; |
| 15 | import 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 | */ |
| 23 | public class PeerConnection { |
| 24 | static { |
| 25 | System.loadLibrary("jingle_peerconnection_so"); |
| 26 | } |
| 27 | |
| 28 | /** Tracks PeerConnectionInterface::IceGatheringState */ |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 29 | public enum IceGatheringState { NEW, GATHERING, COMPLETE } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 30 | |
| 31 | /** Tracks PeerConnectionInterface::IceConnectionState */ |
| 32 | public enum IceConnectionState { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 33 | NEW, |
| 34 | CHECKING, |
| 35 | CONNECTED, |
| 36 | COMPLETED, |
| 37 | FAILED, |
| 38 | DISCONNECTED, |
| 39 | CLOSED |
| 40 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 41 | |
hnsl | 0483362 | 2017-01-09 08:35:45 -0800 | [diff] [blame] | 42 | /** Tracks PeerConnectionInterface::TlsCertPolicy */ |
| 43 | public enum TlsCertPolicy { |
| 44 | TLS_CERT_POLICY_SECURE, |
| 45 | TLS_CERT_POLICY_INSECURE_NO_CHECK, |
| 46 | } |
| 47 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 48 | /** Tracks PeerConnectionInterface::SignalingState */ |
| 49 | public enum SignalingState { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 50 | STABLE, |
| 51 | HAVE_LOCAL_OFFER, |
| 52 | HAVE_LOCAL_PRANSWER, |
| 53 | HAVE_REMOTE_OFFER, |
| 54 | HAVE_REMOTE_PRANSWER, |
| 55 | CLOSED |
| 56 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 57 | |
| 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 Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 66 | /** Triggered when the ICE connection receiving status changes. */ |
| 67 | public void onIceConnectionReceivingChange(boolean receiving); |
| 68 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | /** 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 Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 75 | /** Triggered when some ICE candidates have been removed. */ |
| 76 | public void onIceCandidatesRemoved(IceCandidate[] candidates); |
| 77 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 78 | /** 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.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 83 | |
| 84 | /** Triggered when a remote peer opens a DataChannel. */ |
| 85 | public void onDataChannel(DataChannel dataChannel); |
fischman@webrtc.org | d7568a0 | 2014-01-13 22:04:12 +0000 | [diff] [blame] | 86 | |
| 87 | /** Triggered when renegotiation is necessary. */ |
| 88 | public void onRenegotiationNeeded(); |
zhihuang | dcccda7 | 2016-12-21 14:08:03 -0800 | [diff] [blame] | 89 | |
| 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** Java version of PeerConnectionInterface.IceServer. */ |
| 98 | public static class IceServer { |
Emad Omara | dab1d2d | 2017-06-16 15:43:11 -0700 | [diff] [blame] | 99 | // 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.anatoly | 0ea0310 | 2017-09-11 06:41:38 -0700 | [diff] [blame] | 102 | @Deprecated public final String uri; |
| 103 | public final List<String> urls; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | public final String username; |
| 105 | public final String password; |
hnsl | 0483362 | 2017-01-09 08:35:45 -0800 | [diff] [blame] | 106 | public final TlsCertPolicy tlsCertPolicy; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | |
Emad Omara | dab1d2d | 2017-06-16 15:43:11 -0700 | [diff] [blame] | 108 | // 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 Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 114 | // List of protocols to be used in the TLS ALPN extension. |
| 115 | public final List<String> tlsAlpnProtocols; |
| 116 | |
Diogo Real | 7bd1f1b | 2017-09-08 12:50:41 -0700 | [diff] [blame] | 117 | // 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 121 | /** Convenience constructor for STUN servers. */ |
Diogo Real | 05ea2b3 | 2017-08-31 00:12:58 -0700 | [diff] [blame] | 122 | @Deprecated |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 123 | public IceServer(String uri) { |
| 124 | this(uri, "", ""); |
| 125 | } |
| 126 | |
Diogo Real | 05ea2b3 | 2017-08-31 00:12:58 -0700 | [diff] [blame] | 127 | @Deprecated |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 128 | public IceServer(String uri, String username, String password) { |
hnsl | 0483362 | 2017-01-09 08:35:45 -0800 | [diff] [blame] | 129 | this(uri, username, password, TlsCertPolicy.TLS_CERT_POLICY_SECURE); |
| 130 | } |
| 131 | |
Diogo Real | 05ea2b3 | 2017-08-31 00:12:58 -0700 | [diff] [blame] | 132 | @Deprecated |
hnsl | 0483362 | 2017-01-09 08:35:45 -0800 | [diff] [blame] | 133 | public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy) { |
Emad Omara | dab1d2d | 2017-06-16 15:43:11 -0700 | [diff] [blame] | 134 | this(uri, username, password, tlsCertPolicy, ""); |
| 135 | } |
| 136 | |
Diogo Real | 05ea2b3 | 2017-08-31 00:12:58 -0700 | [diff] [blame] | 137 | @Deprecated |
Emad Omara | dab1d2d | 2017-06-16 15:43:11 -0700 | [diff] [blame] | 138 | public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy, |
| 139 | String hostname) { |
korniltsev.anatoly | 0ea0310 | 2017-09-11 06:41:38 -0700 | [diff] [blame] | 140 | this(uri, Collections.singletonList(uri), username, password, tlsCertPolicy, hostname, null, |
| 141 | null); |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 142 | } |
| 143 | |
korniltsev.anatoly | 0ea0310 | 2017-09-11 06:41:38 -0700 | [diff] [blame] | 144 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 164 | this.uri = uri; |
korniltsev.anatoly | 0ea0310 | 2017-09-11 06:41:38 -0700 | [diff] [blame] | 165 | this.urls = urls; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 166 | this.username = username; |
| 167 | this.password = password; |
hnsl | 0483362 | 2017-01-09 08:35:45 -0800 | [diff] [blame] | 168 | this.tlsCertPolicy = tlsCertPolicy; |
Emad Omara | dab1d2d | 2017-06-16 15:43:11 -0700 | [diff] [blame] | 169 | this.hostname = hostname; |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 170 | this.tlsAlpnProtocols = tlsAlpnProtocols; |
Diogo Real | 7bd1f1b | 2017-09-08 12:50:41 -0700 | [diff] [blame] | 171 | this.tlsEllipticCurves = tlsEllipticCurves; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Sami Kalliomäki | bde473e | 2017-10-30 13:34:41 +0100 | [diff] [blame] | 174 | @Override |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 175 | public String toString() { |
korniltsev.anatoly | 0ea0310 | 2017-09-11 06:41:38 -0700 | [diff] [blame] | 176 | return urls + " [" + username + ":" + password + "] [" + tlsCertPolicy + "] [" + hostname |
Diogo Real | 7bd1f1b | 2017-09-08 12:50:41 -0700 | [diff] [blame] | 177 | + "] [" + tlsAlpnProtocols + "] [" + tlsEllipticCurves + "]"; |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | public static Builder builder(String uri) { |
korniltsev.anatoly | 0ea0310 | 2017-09-11 06:41:38 -0700 | [diff] [blame] | 181 | return new Builder(Collections.singletonList(uri)); |
| 182 | } |
| 183 | |
| 184 | public static Builder builder(List<String> urls) { |
| 185 | return new Builder(urls); |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | public static class Builder { |
korniltsev.anatoly | 0ea0310 | 2017-09-11 06:41:38 -0700 | [diff] [blame] | 189 | private final List<String> urls; |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 190 | 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 Real | 7bd1f1b | 2017-09-08 12:50:41 -0700 | [diff] [blame] | 195 | private List<String> tlsEllipticCurves; |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 196 | |
korniltsev.anatoly | 0ea0310 | 2017-09-11 06:41:38 -0700 | [diff] [blame] | 197 | 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 Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 202 | } |
| 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 Real | 7bd1f1b | 2017-09-08 12:50:41 -0700 | [diff] [blame] | 229 | public Builder setTlsEllipticCurves(List<String> tlsEllipticCurves) { |
| 230 | this.tlsEllipticCurves = tlsEllipticCurves; |
| 231 | return this; |
| 232 | } |
| 233 | |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 234 | public IceServer createIceServer() { |
korniltsev.anatoly | 0ea0310 | 2017-09-11 06:41:38 -0700 | [diff] [blame] | 235 | return new IceServer(urls.get(0), urls, username, password, tlsCertPolicy, hostname, |
| 236 | tlsAlpnProtocols, tlsEllipticCurves); |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 237 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Jiayang Liu | cac1b38 | 2015-04-30 12:35:24 -0700 | [diff] [blame] | 241 | /** Java version of PeerConnectionInterface.IceTransportsType */ |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 242 | public enum IceTransportsType { NONE, RELAY, NOHOST, ALL } |
Jiayang Liu | cac1b38 | 2015-04-30 12:35:24 -0700 | [diff] [blame] | 243 | |
| 244 | /** Java version of PeerConnectionInterface.BundlePolicy */ |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 245 | public enum BundlePolicy { BALANCED, MAXBUNDLE, MAXCOMPAT } |
Jiayang Liu | cac1b38 | 2015-04-30 12:35:24 -0700 | [diff] [blame] | 246 | |
Peter Thatcher | af55ccc | 2015-05-21 07:48:41 -0700 | [diff] [blame] | 247 | /** Java version of PeerConnectionInterface.RtcpMuxPolicy */ |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 248 | public enum RtcpMuxPolicy { NEGOTIATE, REQUIRE } |
glaznev | 97579a4 | 2015-09-01 11:31:27 -0700 | [diff] [blame] | 249 | |
Peter Thatcher | af55ccc | 2015-05-21 07:48:41 -0700 | [diff] [blame] | 250 | /** Java version of PeerConnectionInterface.TcpCandidatePolicy */ |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 251 | public enum TcpCandidatePolicy { ENABLED, DISABLED } |
Jiayang Liu | cac1b38 | 2015-04-30 12:35:24 -0700 | [diff] [blame] | 252 | |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 253 | /** Java version of PeerConnectionInterface.CandidateNetworkPolicy */ |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 254 | public enum CandidateNetworkPolicy { ALL, LOW_COST } |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 255 | |
glaznev | 97579a4 | 2015-09-01 11:31:27 -0700 | [diff] [blame] | 256 | /** Java version of rtc::KeyType */ |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 257 | public enum KeyType { RSA, ECDSA } |
glaznev | 97579a4 | 2015-09-01 11:31:27 -0700 | [diff] [blame] | 258 | |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 259 | /** Java version of PeerConnectionInterface.ContinualGatheringPolicy */ |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 260 | public enum ContinualGatheringPolicy { GATHER_ONCE, GATHER_CONTINUALLY } |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 261 | |
Steve Anton | d960a0c | 2017-07-17 12:33:07 -0700 | [diff] [blame] | 262 | /** 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 Liu | cac1b38 | 2015-04-30 12:35:24 -0700 | [diff] [blame] | 281 | /** Java version of PeerConnectionInterface.RTCConfiguration */ |
| 282 | public static class RTCConfiguration { |
| 283 | public IceTransportsType iceTransportsType; |
| 284 | public List<IceServer> iceServers; |
| 285 | public BundlePolicy bundlePolicy; |
Peter Thatcher | af55ccc | 2015-05-21 07:48:41 -0700 | [diff] [blame] | 286 | public RtcpMuxPolicy rtcpMuxPolicy; |
Jiayang Liu | cac1b38 | 2015-04-30 12:35:24 -0700 | [diff] [blame] | 287 | public TcpCandidatePolicy tcpCandidatePolicy; |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 288 | public CandidateNetworkPolicy candidateNetworkPolicy; |
Henrik Lundin | 64dad83 | 2015-05-11 12:44:23 +0200 | [diff] [blame] | 289 | public int audioJitterBufferMaxPackets; |
Henrik Lundin | 5263b3c | 2015-06-01 10:29:41 +0200 | [diff] [blame] | 290 | public boolean audioJitterBufferFastAccelerate; |
honghaiz | 4edc39c | 2015-09-01 09:53:56 -0700 | [diff] [blame] | 291 | public int iceConnectionReceivingTimeout; |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 292 | public int iceBackupCandidatePairPingInterval; |
glaznev | 97579a4 | 2015-09-01 11:31:27 -0700 | [diff] [blame] | 293 | public KeyType keyType; |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 294 | public ContinualGatheringPolicy continualGatheringPolicy; |
deadbeef | be0c96f | 2016-05-18 16:20:14 -0700 | [diff] [blame] | 295 | public int iceCandidatePoolSize; |
Honghai Zhang | d78ecf7 | 2016-07-01 14:40:40 -0700 | [diff] [blame] | 296 | public boolean pruneTurnPorts; |
Taylor Brandstetter | e985111 | 2016-07-01 11:11:13 -0700 | [diff] [blame] | 297 | public boolean presumeWritableWhenFullyRelayed; |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 298 | public Integer iceCheckMinInterval; |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 299 | public boolean disableIPv6OnWifi; |
deadbeef | 28e2919 | 2017-07-27 09:14:38 -0700 | [diff] [blame] | 300 | // 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 Anton | d960a0c | 2017-07-17 12:33:07 -0700 | [diff] [blame] | 306 | public IntervalRange iceRegatherIntervalRange; |
Jiayang Liu | cac1b38 | 2015-04-30 12:35:24 -0700 | [diff] [blame] | 307 | |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 308 | // This is an optional wrapper for the C++ webrtc::TurnCustomizer. |
| 309 | public TurnCustomizer turnCustomizer; |
| 310 | |
deadbeef | 28e2919 | 2017-07-27 09:14:38 -0700 | [diff] [blame] | 311 | // 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 Liu | cac1b38 | 2015-04-30 12:35:24 -0700 | [diff] [blame] | 314 | public RTCConfiguration(List<IceServer> iceServers) { |
| 315 | iceTransportsType = IceTransportsType.ALL; |
| 316 | bundlePolicy = BundlePolicy.BALANCED; |
zhihuang | 4dfb8ce | 2016-11-23 10:30:12 -0800 | [diff] [blame] | 317 | rtcpMuxPolicy = RtcpMuxPolicy.REQUIRE; |
Jiayang Liu | cac1b38 | 2015-04-30 12:35:24 -0700 | [diff] [blame] | 318 | tcpCandidatePolicy = TcpCandidatePolicy.ENABLED; |
Sami Kalliomäki | 9828beb | 2017-10-26 16:21:22 +0200 | [diff] [blame] | 319 | candidateNetworkPolicy = CandidateNetworkPolicy.ALL; |
Jiayang Liu | cac1b38 | 2015-04-30 12:35:24 -0700 | [diff] [blame] | 320 | this.iceServers = iceServers; |
Henrik Lundin | 64dad83 | 2015-05-11 12:44:23 +0200 | [diff] [blame] | 321 | audioJitterBufferMaxPackets = 50; |
Henrik Lundin | 5263b3c | 2015-06-01 10:29:41 +0200 | [diff] [blame] | 322 | audioJitterBufferFastAccelerate = false; |
honghaiz | 4edc39c | 2015-09-01 09:53:56 -0700 | [diff] [blame] | 323 | iceConnectionReceivingTimeout = -1; |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 324 | iceBackupCandidatePairPingInterval = -1; |
glaznev | 97579a4 | 2015-09-01 11:31:27 -0700 | [diff] [blame] | 325 | keyType = KeyType.ECDSA; |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 326 | continualGatheringPolicy = ContinualGatheringPolicy.GATHER_ONCE; |
deadbeef | be0c96f | 2016-05-18 16:20:14 -0700 | [diff] [blame] | 327 | iceCandidatePoolSize = 0; |
Honghai Zhang | d78ecf7 | 2016-07-01 14:40:40 -0700 | [diff] [blame] | 328 | pruneTurnPorts = false; |
Taylor Brandstetter | e985111 | 2016-07-01 11:11:13 -0700 | [diff] [blame] | 329 | presumeWritableWhenFullyRelayed = false; |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 330 | iceCheckMinInterval = null; |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 331 | disableIPv6OnWifi = false; |
deadbeef | 28e2919 | 2017-07-27 09:14:38 -0700 | [diff] [blame] | 332 | maxIPv6Networks = 5; |
Steve Anton | d960a0c | 2017-07-17 12:33:07 -0700 | [diff] [blame] | 333 | iceRegatherIntervalRange = null; |
Jiayang Liu | cac1b38 | 2015-04-30 12:35:24 -0700 | [diff] [blame] | 334 | } |
| 335 | }; |
| 336 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 337 | private final List<MediaStream> localStreams; |
| 338 | private final long nativePeerConnection; |
| 339 | private final long nativeObserver; |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 340 | private List<RtpSender> senders; |
| 341 | private List<RtpReceiver> receivers; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 342 | |
| 343 | PeerConnection(long nativePeerConnection, long nativeObserver) { |
| 344 | this.nativePeerConnection = nativePeerConnection; |
| 345 | this.nativeObserver = nativeObserver; |
| 346 | localStreams = new LinkedList<MediaStream>(); |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 347 | senders = new LinkedList<RtpSender>(); |
| 348 | receivers = new LinkedList<RtpReceiver>(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // JsepInterface. |
| 352 | public native SessionDescription getLocalDescription(); |
| 353 | |
| 354 | public native SessionDescription getRemoteDescription(); |
| 355 | |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 356 | public native DataChannel createDataChannel(String label, DataChannel.Init init); |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 357 | |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 358 | public native void createOffer(SdpObserver observer, MediaConstraints constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 359 | |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 360 | public native void createAnswer(SdpObserver observer, MediaConstraints constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 361 | |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 362 | public native void setLocalDescription(SdpObserver observer, SessionDescription sdp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 363 | |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 364 | public native void setRemoteDescription(SdpObserver observer, SessionDescription sdp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 365 | |
deadbeef | 5d0b6d8 | 2017-01-09 16:05:28 -0800 | [diff] [blame] | 366 | public boolean setConfiguration(RTCConfiguration config) { |
| 367 | return nativeSetConfiguration(config, nativeObserver); |
| 368 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 369 | |
| 370 | public boolean addIceCandidate(IceCandidate candidate) { |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 371 | return nativeAddIceCandidate(candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 374 | public boolean removeIceCandidates(final IceCandidate[] candidates) { |
| 375 | return nativeRemoveIceCandidates(candidates); |
| 376 | } |
| 377 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 378 | public boolean addStream(MediaStream stream) { |
| 379 | boolean ret = nativeAddLocalStream(stream.nativeStream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 380 | 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 | |
deadbeef | 7a24688 | 2017-08-09 08:40:10 -0700 | [diff] [blame] | 392 | /** |
| 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 | */ |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 429 | public RtpSender createSender(String kind, String stream_id) { |
| 430 | RtpSender new_sender = nativeCreateSender(kind, stream_id); |
deadbeef | ee524f7 | 2015-12-02 11:27:40 -0800 | [diff] [blame] | 431 | if (new_sender != null) { |
| 432 | senders.add(new_sender); |
| 433 | } |
| 434 | return new_sender; |
| 435 | } |
| 436 | |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 437 | // 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 | |
deadbeef | 8221587 | 2017-04-18 10:27:51 -0700 | [diff] [blame] | 455 | // Older, non-standard implementation of getStats. |
| 456 | @Deprecated |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 457 | public boolean getStats(StatsObserver observer, MediaStreamTrack track) { |
deadbeef | 8221587 | 2017-04-18 10:27:51 -0700 | [diff] [blame] | 458 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 465 | } |
| 466 | |
zstein | d89b0bc | 2017-08-03 11:11:40 -0700 | [diff] [blame] | 467 | // 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 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 471 | // 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. |
ivoc | 0c6f0f6 | 2016-07-06 04:34:23 -0700 | [diff] [blame] | 476 | public boolean startRtcEventLog(int file_descriptor, int max_size_bytes) { |
| 477 | return nativeStartRtcEventLog(file_descriptor, max_size_bytes); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 478 | } |
| 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() { |
ivoc | 0c6f0f6 | 2016-07-06 04:34:23 -0700 | [diff] [blame] | 483 | nativeStopRtcEventLog(); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 484 | } |
| 485 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 486 | // 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 | |
deadbeef | 43697f6 | 2017-09-12 10:52:14 -0700 | [diff] [blame] | 496 | /** |
| 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 512 | public void dispose() { |
| 513 | close(); |
| 514 | for (MediaStream stream : localStreams) { |
fischman@webrtc.org | 32001ef | 2013-08-12 23:26:21 +0000 | [diff] [blame] | 515 | nativeRemoveLocalStream(stream.nativeStream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 516 | stream.dispose(); |
| 517 | } |
| 518 | localStreams.clear(); |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 519 | for (RtpSender sender : senders) { |
| 520 | sender.dispose(); |
| 521 | } |
| 522 | senders.clear(); |
| 523 | for (RtpReceiver receiver : receivers) { |
| 524 | receiver.dispose(); |
| 525 | } |
| 526 | receivers.clear(); |
magjed | b1c7453 | 2017-08-27 13:47:20 -0700 | [diff] [blame] | 527 | JniCommon.nativeReleaseRef(nativePeerConnection); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 528 | freeObserver(nativeObserver); |
| 529 | } |
| 530 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 531 | private static native void freeObserver(long nativeObserver); |
| 532 | |
deadbeef | 5d0b6d8 | 2017-01-09 16:05:28 -0800 | [diff] [blame] | 533 | public native boolean nativeSetConfiguration(RTCConfiguration config, long nativeObserver); |
| 534 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 535 | private native boolean nativeAddIceCandidate( |
| 536 | String sdpMid, int sdpMLineIndex, String iceCandidateSdp); |
| 537 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 538 | private native boolean nativeRemoveIceCandidates(final IceCandidate[] candidates); |
| 539 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 540 | private native boolean nativeAddLocalStream(long nativeStream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 541 | |
| 542 | private native void nativeRemoveLocalStream(long nativeStream); |
| 543 | |
deadbeef | 8221587 | 2017-04-18 10:27:51 -0700 | [diff] [blame] | 544 | private native boolean nativeOldGetStats(StatsObserver observer, long nativeTrack); |
| 545 | |
| 546 | private native void nativeNewGetStats(RTCStatsCollectorCallback callback); |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 547 | |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 548 | private native RtpSender nativeCreateSender(String kind, String stream_id); |
deadbeef | ee524f7 | 2015-12-02 11:27:40 -0800 | [diff] [blame] | 549 | |
deadbeef | 4139c0f | 2015-10-06 12:29:25 -0700 | [diff] [blame] | 550 | private native List<RtpSender> nativeGetSenders(); |
| 551 | |
| 552 | private native List<RtpReceiver> nativeGetReceivers(); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 553 | |
sakal | b6760f9 | 2016-09-29 04:12:44 -0700 | [diff] [blame] | 554 | private native boolean nativeStartRtcEventLog(int file_descriptor, int max_size_bytes); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 555 | |
ivoc | 0c6f0f6 | 2016-07-06 04:34:23 -0700 | [diff] [blame] | 556 | private native void nativeStopRtcEventLog(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 557 | } |