| 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. |
| 9 | */ |
| 10 |
| 11 | package org.webrtc; |
| 12 |
| 13 | import androidx.annotation.Nullable; |
| 14 | import java.util.ArrayList; |
| 15 | import java.util.Arrays; |
| 16 | import java.util.Collections; |
| 17 | import java.util.HashMap; |
| 18 | import java.util.List; |
| 19 | import java.util.Map; |
| 20 | import org.webrtc.CandidatePairChangeEvent; |
| 21 | import org.webrtc.DataChannel; |
| 22 | import org.webrtc.MediaStreamTrack; |
| 23 | import org.webrtc.RtpTransceiver; |
| 24 |
| 25 | /** |
| 26 | * Java-land version of the PeerConnection APIs; wraps the C++ API |
| 27 | * http://www.webrtc.org/reference/native-apis, which in turn is inspired by the |
| 28 | * JS APIs: http://dev.w3.org/2011/webrtc/editor/webrtc.html and |
| 29 | * http://www.w3.org/TR/mediacapture-streams/ |
| 30 | */ |
| 31 | public class PeerConnection { |
| 32 | /** Tracks PeerConnectionInterface::IceGatheringState */ |
| 33 | public enum IceGatheringState { |
| 34 | NEW, |
| 35 | GATHERING, |
| 36 | COMPLETE; |
| 37 |
| 38 | @CalledByNative("IceGatheringState") |
| 39 | static IceGatheringState fromNativeIndex(int nativeIndex) { |
| 40 | return values()[nativeIndex]; |
| 41 | } |
| 42 | } |
| 43 |
| 44 | /** Tracks PeerConnectionInterface::IceConnectionState */ |
| 45 | public enum IceConnectionState { |
| 46 | NEW, |
| 47 | CHECKING, |
| 48 | CONNECTED, |
| 49 | COMPLETED, |
| 50 | FAILED, |
| 51 | DISCONNECTED, |
| 52 | CLOSED; |
| 53 |
| 54 | @CalledByNative("IceConnectionState") |
| 55 | static IceConnectionState fromNativeIndex(int nativeIndex) { |
| 56 | return values()[nativeIndex]; |
| 57 | } |
| 58 | } |
| 59 |
| 60 | /** Tracks PeerConnectionInterface::PeerConnectionState */ |
| 61 | public enum PeerConnectionState { |
| 62 | NEW, |
| 63 | CONNECTING, |
| 64 | CONNECTED, |
| 65 | DISCONNECTED, |
| 66 | FAILED, |
| 67 | CLOSED; |
| 68 |
| 69 | @CalledByNative("PeerConnectionState") |
| 70 | static PeerConnectionState fromNativeIndex(int nativeIndex) { |
| 71 | return values()[nativeIndex]; |
| 72 | } |
| 73 | } |
| 74 |
| 75 | /** Tracks PeerConnectionInterface::TlsCertPolicy */ |
| 76 | public enum TlsCertPolicy { |
| 77 | TLS_CERT_POLICY_SECURE, |
| 78 | TLS_CERT_POLICY_INSECURE_NO_CHECK, |
| 79 | } |
| 80 |
| 81 | /** Tracks PeerConnectionInterface::SignalingState */ |
| 82 | public enum SignalingState { |
| 83 | STABLE, |
| 84 | HAVE_LOCAL_OFFER, |
| 85 | HAVE_LOCAL_PRANSWER, |
| 86 | HAVE_REMOTE_OFFER, |
| 87 | HAVE_REMOTE_PRANSWER, |
| 88 | CLOSED; |
| 89 |
| 90 | @CalledByNative("SignalingState") |
| 91 | static SignalingState fromNativeIndex(int nativeIndex) { |
| 92 | return values()[nativeIndex]; |
| 93 | } |
| 94 | } |
| 95 |
| 96 | /** Java version of PeerConnectionObserver. */ |
| 97 | public static interface Observer { |
| 98 | /** Triggered when the SignalingState changes. */ |
| 99 | @CalledByNative("Observer") void onSignalingChange(SignalingState newState); |
| 100 |
| 101 | /** Triggered when the IceConnectionState changes. */ |
| 102 | @CalledByNative("Observer") void onIceConnectionChange(IceConnectionState newState); |
| 103 |
| 104 | /* Triggered when the standard-compliant state transition of IceConnectionState happens. */ |
| 105 | @CalledByNative("Observer") |
| 106 | default void onStandardizedIceConnectionChange(IceConnectionState newState) {} |
| 107 |
| 108 | /** Triggered when the PeerConnectionState changes. */ |
| 109 | @CalledByNative("Observer") |
| 110 | default void onConnectionChange(PeerConnectionState newState) {} |
| 111 |
| 112 | /** Triggered when the ICE connection receiving status changes. */ |
| 113 | @CalledByNative("Observer") void onIceConnectionReceivingChange(boolean receiving); |
| 114 |
| 115 | /** Triggered when the IceGatheringState changes. */ |
| 116 | @CalledByNative("Observer") void onIceGatheringChange(IceGatheringState newState); |
| 117 |
| 118 | /** Triggered when a new ICE candidate has been found. */ |
| 119 | @CalledByNative("Observer") void onIceCandidate(IceCandidate candidate); |
| 120 |
| 121 | /** Triggered when gathering of an ICE candidate failed. */ |
| 122 | default @CalledByNative("Observer") void onIceCandidateError(IceCandidateErrorEvent event) {} |
| 123 |
| 124 | /** Triggered when some ICE candidates have been removed. */ |
| 125 | @CalledByNative("Observer") void onIceCandidatesRemoved(IceCandidate[] candidates); |
| 126 |
| 127 | /** Triggered when the ICE candidate pair is changed. */ |
| 128 | @CalledByNative("Observer") |
| 129 | default void onSelectedCandidatePairChanged(CandidatePairChangeEvent event) {} |
| 130 |
| 131 | /** Triggered when media is received on a new stream from remote peer. */ |
| 132 | @CalledByNative("Observer") void onAddStream(MediaStream stream); |
| 133 |
| 134 | /** Triggered when a remote peer close a stream. */ |
| 135 | @CalledByNative("Observer") void onRemoveStream(MediaStream stream); |
| 136 |
| 137 | /** Triggered when a remote peer opens a DataChannel. */ |
| 138 | @CalledByNative("Observer") void onDataChannel(DataChannel dataChannel); |
| 139 |
| 140 | /** Triggered when renegotiation is necessary. */ |
| 141 | @CalledByNative("Observer") void onRenegotiationNeeded(); |
| 142 |
| 143 | /** |
| 144 | * Triggered when a new track is signaled by the remote peer, as a result of |
| 145 | * setRemoteDescription. |
| 146 | */ |
| 147 | @CalledByNative("Observer") |
| 148 | default void onAddTrack(RtpReceiver receiver, MediaStream[] mediaStreams){}; |
| 149 |
| 150 | /** |
| 151 | * Triggered when a previously added remote track is removed by the remote |
| 152 | * peer, as a result of setRemoteDescription. |
| 153 | */ |
| 154 | @CalledByNative("Observer") default void onRemoveTrack(RtpReceiver receiver){}; |
| 155 |
| 156 | /** |
| 157 | * Triggered when the signaling from SetRemoteDescription indicates that a transceiver |
| 158 | * will be receiving media from a remote endpoint. This is only called if UNIFIED_PLAN |
| 159 | * semantics are specified. The transceiver will be disposed automatically. |
| 160 | */ |
| 161 | @CalledByNative("Observer") default void onTrack(RtpTransceiver transceiver){}; |
| 162 | } |
| 163 |
| 164 | /** Java version of PeerConnectionInterface.IceServer. */ |
| 165 | public static class IceServer { |
| 166 | // List of URIs associated with this server. Valid formats are described |
| 167 | // in RFC7064 and RFC7065, and more may be added in the future. The "host" |
| 168 | // part of the URI may contain either an IP address or a hostname. |
| 169 | @Deprecated public final String uri; |
| 170 | public final List<String> urls; |
| 171 | public final String username; |
| 172 | public final String password; |
| 173 | public final TlsCertPolicy tlsCertPolicy; |
| 174 |
| 175 | // If the URIs in `urls` only contain IP addresses, this field can be used |
| 176 | // to indicate the hostname, which may be necessary for TLS (using the SNI |
| 177 | // extension). If `urls` itself contains the hostname, this isn't |
| 178 | // necessary. |
| 179 | public final String hostname; |
| 180 |
| 181 | // List of protocols to be used in the TLS ALPN extension. |
| 182 | public final List<String> tlsAlpnProtocols; |
| 183 |
| 184 | // List of elliptic curves to be used in the TLS elliptic curves extension. |
| 185 | // Only curve names supported by OpenSSL should be used (eg. "P-256","X25519"). |
| 186 | public final List<String> tlsEllipticCurves; |
| 187 |
| 188 | /** Convenience constructor for STUN servers. */ |
| 189 | @Deprecated |
| 190 | public IceServer(String uri) { |
| 191 | this(uri, "", ""); |
| 192 | } |
| 193 |
| 194 | @Deprecated |
| 195 | public IceServer(String uri, String username, String password) { |
| 196 | this(uri, username, password, TlsCertPolicy.TLS_CERT_POLICY_SECURE); |
| 197 | } |
| 198 |
| 199 | @Deprecated |
| 200 | public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy) { |
| 201 | this(uri, username, password, tlsCertPolicy, ""); |
| 202 | } |
| 203 |
| 204 | @Deprecated |
| 205 | public IceServer(String uri, String username, String password, TlsCertPolicy tlsCertPolicy, |
| 206 | String hostname) { |
| 207 | this(uri, Collections.singletonList(uri), username, password, tlsCertPolicy, hostname, null, |
| 208 | null); |
| 209 | } |
| 210 |
| 211 | private IceServer(String uri, List<String> urls, String username, String password, |
| 212 | TlsCertPolicy tlsCertPolicy, String hostname, List<String> tlsAlpnProtocols, |
| 213 | List<String> tlsEllipticCurves) { |
| 214 | if (uri == null || urls == null || urls.isEmpty()) { |
| 215 | throw new IllegalArgumentException("uri == null || urls == null || urls.isEmpty()"); |
| 216 | } |
| 217 | for (String it : urls) { |
| 218 | if (it == null) { |
| 219 | throw new IllegalArgumentException("urls element is null: " + urls); |
| 220 | } |
| 221 | } |
| 222 | if (username == null) { |
| 223 | throw new IllegalArgumentException("username == null"); |
| 224 | } |
| 225 | if (password == null) { |
| 226 | throw new IllegalArgumentException("password == null"); |
| 227 | } |
| 228 | if (hostname == null) { |
| 229 | throw new IllegalArgumentException("hostname == null"); |
| 230 | } |
| 231 | this.uri = uri; |
| 232 | this.urls = urls; |
| 233 | this.username = username; |
| 234 | this.password = password; |
| 235 | this.tlsCertPolicy = tlsCertPolicy; |
| 236 | this.hostname = hostname; |
| 237 | this.tlsAlpnProtocols = tlsAlpnProtocols; |
| 238 | this.tlsEllipticCurves = tlsEllipticCurves; |
| 239 | } |
| 240 |
| 241 | @Override |
| 242 | public String toString() { |
| 243 | return urls + " [" + username + ":" + password + "] [" + tlsCertPolicy + "] [" + hostname |
| 244 | + "] [" + tlsAlpnProtocols + "] [" + tlsEllipticCurves + "]"; |
| 245 | } |
| 246 |
| 247 | @Override |
| 248 | public boolean equals(@Nullable Object obj) { |
| 249 | if (obj == null) { |
| 250 | return false; |
| 251 | } |
| 252 | if (obj == this) { |
| 253 | return true; |
| 254 | } |
| 255 | if (!(obj instanceof IceServer)) { |
| 256 | return false; |
| 257 | } |
| 258 | IceServer other = (IceServer) obj; |
| 259 | return (uri.equals(other.uri) && urls.equals(other.urls) && username.equals(other.username) |
| 260 | && password.equals(other.password) && tlsCertPolicy.equals(other.tlsCertPolicy) |
| 261 | && hostname.equals(other.hostname) && tlsAlpnProtocols.equals(other.tlsAlpnProtocols) |
| 262 | && tlsEllipticCurves.equals(other.tlsEllipticCurves)); |
| 263 | } |
| 264 |
| 265 | @Override |
| 266 | public int hashCode() { |
| 267 | Object[] values = {uri, urls, username, password, tlsCertPolicy, hostname, tlsAlpnProtocols, |
| 268 | tlsEllipticCurves}; |
| 269 | return Arrays.hashCode(values); |
| 270 | } |
| 271 |
| 272 | public static Builder builder(String uri) { |
| 273 | return new Builder(Collections.singletonList(uri)); |
| 274 | } |
| 275 |
| 276 | public static Builder builder(List<String> urls) { |
| 277 | return new Builder(urls); |
| 278 | } |
| 279 |
| 280 | public static class Builder { |
| 281 | @Nullable private final List<String> urls; |
| 282 | private String username = ""; |
| 283 | private String password = ""; |
| 284 | private TlsCertPolicy tlsCertPolicy = TlsCertPolicy.TLS_CERT_POLICY_SECURE; |
| 285 | private String hostname = ""; |
| 286 | private List<String> tlsAlpnProtocols; |
| 287 | private List<String> tlsEllipticCurves; |
| 288 |
| 289 | private Builder(List<String> urls) { |
| 290 | if (urls == null || urls.isEmpty()) { |
| 291 | throw new IllegalArgumentException("urls == null || urls.isEmpty(): " + urls); |
| 292 | } |
| 293 | this.urls = urls; |
| 294 | } |
| 295 |
| 296 | public Builder setUsername(String username) { |
| 297 | this.username = username; |
| 298 | return this; |
| 299 | } |
| 300 |
| 301 | public Builder setPassword(String password) { |
| 302 | this.password = password; |
| 303 | return this; |
| 304 | } |
| 305 |
| 306 | public Builder setTlsCertPolicy(TlsCertPolicy tlsCertPolicy) { |
| 307 | this.tlsCertPolicy = tlsCertPolicy; |
| 308 | return this; |
| 309 | } |
| 310 |
| 311 | public Builder setHostname(String hostname) { |
| 312 | this.hostname = hostname; |
| 313 | return this; |
| 314 | } |
| 315 |
| 316 | public Builder setTlsAlpnProtocols(List<String> tlsAlpnProtocols) { |
| 317 | this.tlsAlpnProtocols = tlsAlpnProtocols; |
| 318 | return this; |
| 319 | } |
| 320 |
| 321 | public Builder setTlsEllipticCurves(List<String> tlsEllipticCurves) { |
| 322 | this.tlsEllipticCurves = tlsEllipticCurves; |
| 323 | return this; |
| 324 | } |
| 325 |
| 326 | public IceServer createIceServer() { |
| 327 | return new IceServer(urls.get(0), urls, username, password, tlsCertPolicy, hostname, |
| 328 | tlsAlpnProtocols, tlsEllipticCurves); |
| 329 | } |
| 330 | } |
| 331 |
| 332 | @Nullable |
| 333 | @CalledByNative("IceServer") |
| 334 | List<String> getUrls() { |
| 335 | return urls; |
| 336 | } |
| 337 |
| 338 | @Nullable |
| 339 | @CalledByNative("IceServer") |
| 340 | String getUsername() { |
| 341 | return username; |
| 342 | } |
| 343 |
| 344 | @Nullable |
| 345 | @CalledByNative("IceServer") |
| 346 | String getPassword() { |
| 347 | return password; |
| 348 | } |
| 349 |
| 350 | @CalledByNative("IceServer") |
| 351 | TlsCertPolicy getTlsCertPolicy() { |
| 352 | return tlsCertPolicy; |
| 353 | } |
| 354 |
| 355 | @Nullable |
| 356 | @CalledByNative("IceServer") |
| 357 | String getHostname() { |
| 358 | return hostname; |
| 359 | } |
| 360 |
| 361 | @CalledByNative("IceServer") |
| 362 | List<String> getTlsAlpnProtocols() { |
| 363 | return tlsAlpnProtocols; |
| 364 | } |
| 365 |
| 366 | @CalledByNative("IceServer") |
| 367 | List<String> getTlsEllipticCurves() { |
| 368 | return tlsEllipticCurves; |
| 369 | } |
| 370 | } |
| 371 |
| 372 | /** Java version of PeerConnectionInterface.IceTransportsType */ |
| 373 | public enum IceTransportsType { NONE, RELAY, NOHOST, ALL } |
| 374 |
| 375 | /** Java version of PeerConnectionInterface.BundlePolicy */ |
| 376 | public enum BundlePolicy { BALANCED, MAXBUNDLE, MAXCOMPAT } |
| 377 |
| 378 | /** Java version of PeerConnectionInterface.RtcpMuxPolicy */ |
| 379 | public enum RtcpMuxPolicy { NEGOTIATE, REQUIRE } |
| 380 |
| 381 | /** Java version of PeerConnectionInterface.TcpCandidatePolicy */ |
| 382 | public enum TcpCandidatePolicy { ENABLED, DISABLED } |
| 383 |
| 384 | /** Java version of PeerConnectionInterface.CandidateNetworkPolicy */ |
| 385 | public enum CandidateNetworkPolicy { ALL, LOW_COST } |
| 386 |
| 387 | // Keep in sync with webrtc/rtc_base/network_constants.h. |
| 388 | public enum AdapterType { |
| 389 | UNKNOWN(0), |
| 390 | ETHERNET(1 << 0), |
| 391 | WIFI(1 << 1), |
| 392 | CELLULAR(1 << 2), |
| 393 | VPN(1 << 3), |
| 394 | LOOPBACK(1 << 4), |
| 395 | ADAPTER_TYPE_ANY(1 << 5), |
| 396 | CELLULAR_2G(1 << 6), |
| 397 | CELLULAR_3G(1 << 7), |
| 398 | CELLULAR_4G(1 << 8), |
| 399 | CELLULAR_5G(1 << 9); |
| 400 |
| 401 | public final Integer bitMask; |
| 402 | private AdapterType(Integer bitMask) { |
| 403 | this.bitMask = bitMask; |
| 404 | } |
| 405 | private static final Map<Integer, AdapterType> BY_BITMASK = new HashMap<>(); |
| 406 | static { |
| 407 | for (AdapterType t : values()) { |
| 408 | BY_BITMASK.put(t.bitMask, t); |
| 409 | } |
| 410 | } |
| 411 |
| 412 | @Nullable |
| 413 | @CalledByNative("AdapterType") |
| 414 | static AdapterType fromNativeIndex(int nativeIndex) { |
| 415 | return BY_BITMASK.get(nativeIndex); |
| 416 | } |
| 417 | } |
| 418 |
| 419 | /** Java version of rtc::KeyType */ |
| 420 | public enum KeyType { RSA, ECDSA } |
| 421 |
| 422 | /** Java version of PeerConnectionInterface.ContinualGatheringPolicy */ |
| 423 | public enum ContinualGatheringPolicy { GATHER_ONCE, GATHER_CONTINUALLY } |
| 424 |
| 425 | /** Java version of webrtc::PortPrunePolicy */ |
| 426 | public enum PortPrunePolicy { |
| 427 | NO_PRUNE, // Do not prune turn port. |
| 428 | PRUNE_BASED_ON_PRIORITY, // Prune turn port based the priority on the same network |
| 429 | KEEP_FIRST_READY // Keep the first ready port and prune the rest on the same network. |
| 430 | } |
| 431 |
| 432 | /** |
| 433 | * Java version of webrtc::SdpSemantics. |
| 434 | * |
| 435 | * Configure the SDP semantics used by this PeerConnection. By default, this |
| 436 | * is UNIFIED_PLAN which is compliant to the WebRTC 1.0 specification. It is |
| 437 | * possible to overrwite this to the deprecated PLAN_B SDP format, but note |
| 438 | * that PLAN_B will be deleted at some future date, see |
| 439 | * https://crbug.com/webrtc/13528. |
| 440 | * |
| 441 | * UNIFIED_PLAN will cause PeerConnection to create offers and answers with |
| 442 | * multiple m= sections where each m= section maps to one RtpSender and one |
| 443 | * RtpReceiver (an RtpTransceiver), either both audio or both video. This |
| 444 | * will also cause PeerConnection to ignore all but the first a=ssrc lines |
| 445 | * that form a Plan B stream. |
| 446 | * |
| 447 | * PLAN_B will cause PeerConnection to create offers and answers with at most |
| 448 | * one audio and one video m= section with multiple RtpSenders and |
| 449 | * RtpReceivers specified as multiple a=ssrc lines within the section. This |
| 450 | * will also cause PeerConnection to ignore all but the first m= section of |
| 451 | * the same media type. |
| 452 | */ |
| 453 | public enum SdpSemantics { |
| 454 | // TODO(https://crbug.com/webrtc/13528): Remove support for PLAN_B. |
| 455 | @Deprecated PLAN_B, |
| 456 | UNIFIED_PLAN |
| 457 | } |
| 458 |
| 459 | /** Java version of PeerConnectionInterface.RTCConfiguration */ |
| 460 | // TODO(qingsi): Resolve the naming inconsistency of fields with/without units. |
| 461 | public static class RTCConfiguration { |
| 462 | public IceTransportsType iceTransportsType; |
| 463 | public List<IceServer> iceServers; |
| 464 | public BundlePolicy bundlePolicy; |
| 465 | @Nullable public RtcCertificatePem certificate; |
| 466 | public RtcpMuxPolicy rtcpMuxPolicy; |
| 467 | public TcpCandidatePolicy tcpCandidatePolicy; |
| 468 | public CandidateNetworkPolicy candidateNetworkPolicy; |
| 469 | public int audioJitterBufferMaxPackets; |
| 470 | public boolean audioJitterBufferFastAccelerate; |
| 471 | public int iceConnectionReceivingTimeout; |
| 472 | public int iceBackupCandidatePairPingInterval; |
| 473 | public KeyType keyType; |
| 474 | public ContinualGatheringPolicy continualGatheringPolicy; |
| 475 | public int iceCandidatePoolSize; |
| 476 | @Deprecated // by the turnPortPrunePolicy. See bugs.webrtc.org/11026 |
| 477 | public boolean pruneTurnPorts; |
| 478 | public PortPrunePolicy turnPortPrunePolicy; |
| 479 | public boolean presumeWritableWhenFullyRelayed; |
| 480 | public boolean surfaceIceCandidatesOnIceTransportTypeChanged; |
| 481 | // The following fields define intervals in milliseconds at which ICE |
| 482 | // connectivity checks are sent. |
| 483 | // |
| 484 | // We consider ICE is "strongly connected" for an agent when there is at |
| 485 | // least one candidate pair that currently succeeds in connectivity check |
| 486 | // from its direction i.e. sending a ping and receives a ping response, AND |
| 487 | // all candidate pairs have sent a minimum number of pings for connectivity |
| 488 | // (this number is implementation-specific). Otherwise, ICE is considered in |
| 489 | // "weak connectivity". |
| 490 | // |
| 491 | // Note that the above notion of strong and weak connectivity is not defined |
| 492 | // in RFC 5245, and they apply to our current ICE implementation only. |
| 493 | // |
| 494 | // 1) iceCheckIntervalStrongConnectivityMs defines the interval applied to |
| 495 | // ALL candidate pairs when ICE is strongly connected, |
| 496 | // 2) iceCheckIntervalWeakConnectivityMs defines the counterpart for ALL |
| 497 | // pairs when ICE is weakly connected, and |
| 498 | // 3) iceCheckMinInterval defines the minimal interval (equivalently the |
| 499 | // maximum rate) that overrides the above two intervals when either of them |
| 500 | // is less. |
| 501 | @Nullable public Integer iceCheckIntervalStrongConnectivityMs; |
| 502 | @Nullable public Integer iceCheckIntervalWeakConnectivityMs; |
| 503 | @Nullable public Integer iceCheckMinInterval; |
| 504 | // The time period in milliseconds for which a candidate pair must wait for response to |
| 505 | // connectivitiy checks before it becomes unwritable. |
| 506 | @Nullable public Integer iceUnwritableTimeMs; |
| 507 | // The minimum number of connectivity checks that a candidate pair must sent without receiving |
| 508 | // response before it becomes unwritable. |
| 509 | @Nullable public Integer iceUnwritableMinChecks; |
| 510 | // The interval in milliseconds at which STUN candidates will resend STUN binding requests |
| 511 | // to keep NAT bindings open. |
| 512 | // The default value in the implementation is used if this field is null. |
| 513 | @Nullable public Integer stunCandidateKeepaliveIntervalMs; |
| 514 | // The interval in milliseconds of pings sent when the connection is stable and writable. |
| 515 | // The default value in the implementation is used if this field is null. |
| 516 | @Nullable public Integer stableWritableConnectionPingIntervalMs; |
| 517 | public boolean disableIPv6OnWifi; |
| 518 | // By default, PeerConnection will use a limited number of IPv6 network |
| 519 | // interfaces, in order to avoid too many ICE candidate pairs being created |
| 520 | // and delaying ICE completion. |
| 521 | // |
| 522 | // Can be set to Integer.MAX_VALUE to effectively disable the limit. |
| 523 | public int maxIPv6Networks; |
| 524 |
| 525 | // These values will be overridden by MediaStream constraints if deprecated constraints-based |
| 526 | // create peerconnection interface is used. |
| 527 | public boolean enableDscp; |
| 528 | public boolean enableCpuOveruseDetection; |
| 529 | public boolean suspendBelowMinBitrate; |
| 530 | @Nullable public Integer screencastMinBitrate; |
| 531 | // Use "Unknown" to represent no preference of adapter types, not the |
| 532 | // preference of adapters of unknown types. |
| 533 | public AdapterType networkPreference; |
| 534 | public SdpSemantics sdpSemantics; |
| 535 |
| 536 | // This is an optional wrapper for the C++ webrtc::TurnCustomizer. |
| 537 | @Nullable public TurnCustomizer turnCustomizer; |
| 538 |
| 539 | // Actively reset the SRTP parameters whenever the DTLS transports underneath are reset for |
| 540 | // every offer/answer negotiation.This is only intended to be a workaround for crbug.com/835958 |
| 541 | public boolean activeResetSrtpParams; |
| 542 |
| 543 | /** |
| 544 | * Defines advanced optional cryptographic settings related to SRTP and |
| 545 | * frame encryption for native WebRTC. Setting this will overwrite any |
| 546 | * options set through the PeerConnectionFactory (which is deprecated). |
| 547 | */ |
| 548 | @Nullable public CryptoOptions cryptoOptions; |
| 549 |
| 550 | /** |
| 551 | * An optional string that if set will be attached to the |
| 552 | * TURN_ALLOCATE_REQUEST which can be used to correlate client |
| 553 | * logs with backend logs |
| 554 | */ |
| 555 | @Nullable public String turnLoggingId; |
| 556 |
| 557 | /** |
| 558 | * Allow implicit rollback of local description when remote description |
| 559 | * conflicts with local description. |
| 560 | * See: https://w3c.github.io/webrtc-pc/#dom-peerconnection-setremotedescription |
| 561 | */ |
| 562 | public boolean enableImplicitRollback; |
| 563 |
| 564 | /** |
| 565 | * Control if "a=extmap-allow-mixed" is included in the offer. |
| 566 | * See: https://www.chromestatus.com/feature/6269234631933952 |
| 567 | */ |
| 568 | public boolean offerExtmapAllowMixed; |
| 569 |
| 570 | // TODO(deadbeef): Instead of duplicating the defaults here, we should do |
| 571 | // something to pick up the defaults from C++. The Objective-C equivalent |
| 572 | // of RTCConfiguration does that. |
| 573 | public RTCConfiguration(List<IceServer> iceServers) { |
| 574 | iceTransportsType = IceTransportsType.ALL; |
| 575 | bundlePolicy = BundlePolicy.BALANCED; |
| 576 | rtcpMuxPolicy = RtcpMuxPolicy.REQUIRE; |
| 577 | tcpCandidatePolicy = TcpCandidatePolicy.ENABLED; |
| 578 | candidateNetworkPolicy = CandidateNetworkPolicy.ALL; |
| 579 | this.iceServers = iceServers; |
| 580 | audioJitterBufferMaxPackets = 50; |
| 581 | audioJitterBufferFastAccelerate = false; |
| 582 | iceConnectionReceivingTimeout = -1; |
| 583 | iceBackupCandidatePairPingInterval = -1; |
| 584 | keyType = KeyType.ECDSA; |
| 585 | continualGatheringPolicy = ContinualGatheringPolicy.GATHER_ONCE; |
| 586 | iceCandidatePoolSize = 0; |
| 587 | pruneTurnPorts = false; |
| 588 | turnPortPrunePolicy = PortPrunePolicy.NO_PRUNE; |
| 589 | presumeWritableWhenFullyRelayed = false; |
| 590 | surfaceIceCandidatesOnIceTransportTypeChanged = false; |
| 591 | iceCheckIntervalStrongConnectivityMs = null; |
| 592 | iceCheckIntervalWeakConnectivityMs = null; |
| 593 | iceCheckMinInterval = null; |
| 594 | iceUnwritableTimeMs = null; |
| 595 | iceUnwritableMinChecks = null; |
| 596 | stunCandidateKeepaliveIntervalMs = null; |
| 597 | stableWritableConnectionPingIntervalMs = null; |
| 598 | disableIPv6OnWifi = false; |
| 599 | maxIPv6Networks = 5; |
| 600 | enableDscp = false; |
| 601 | enableCpuOveruseDetection = true; |
| 602 | suspendBelowMinBitrate = false; |
| 603 | screencastMinBitrate = null; |
| 604 | networkPreference = AdapterType.UNKNOWN; |
| 605 | sdpSemantics = SdpSemantics.UNIFIED_PLAN; |
| 606 | activeResetSrtpParams = false; |
| 607 | cryptoOptions = null; |
| 608 | turnLoggingId = null; |
| 609 | enableImplicitRollback = false; |
| 610 | offerExtmapAllowMixed = true; |
| 611 | } |
| 612 |
| 613 | @CalledByNative("RTCConfiguration") |
| 614 | IceTransportsType getIceTransportsType() { |
| 615 | return iceTransportsType; |
| 616 | } |
| 617 |
| 618 | @CalledByNative("RTCConfiguration") |
| 619 | List<IceServer> getIceServers() { |
| 620 | return iceServers; |
| 621 | } |
| 622 |
| 623 | @CalledByNative("RTCConfiguration") |
| 624 | BundlePolicy getBundlePolicy() { |
| 625 | return bundlePolicy; |
| 626 | } |
| 627 |
| 628 | @CalledByNative("RTCConfiguration") |
| 629 | PortPrunePolicy getTurnPortPrunePolicy() { |
| 630 | return turnPortPrunePolicy; |
| 631 | } |
| 632 |
| 633 | @Nullable |
| 634 | @CalledByNative("RTCConfiguration") |
| 635 | RtcCertificatePem getCertificate() { |
| 636 | return certificate; |
| 637 | } |
| 638 |
| 639 | @CalledByNative("RTCConfiguration") |
| 640 | RtcpMuxPolicy getRtcpMuxPolicy() { |
| 641 | return rtcpMuxPolicy; |
| 642 | } |
| 643 |
| 644 | @CalledByNative("RTCConfiguration") |
| 645 | TcpCandidatePolicy getTcpCandidatePolicy() { |
| 646 | return tcpCandidatePolicy; |
| 647 | } |
| 648 |
| 649 | @CalledByNative("RTCConfiguration") |
| 650 | CandidateNetworkPolicy getCandidateNetworkPolicy() { |
| 651 | return candidateNetworkPolicy; |
| 652 | } |
| 653 |
| 654 | @CalledByNative("RTCConfiguration") |
| 655 | int getAudioJitterBufferMaxPackets() { |
| 656 | return audioJitterBufferMaxPackets; |
| 657 | } |
| 658 |
| 659 | @CalledByNative("RTCConfiguration") |
| 660 | boolean getAudioJitterBufferFastAccelerate() { |
| 661 | return audioJitterBufferFastAccelerate; |
| 662 | } |
| 663 |
| 664 | @CalledByNative("RTCConfiguration") |
| 665 | int getIceConnectionReceivingTimeout() { |
| 666 | return iceConnectionReceivingTimeout; |
| 667 | } |
| 668 |
| 669 | @CalledByNative("RTCConfiguration") |
| 670 | int getIceBackupCandidatePairPingInterval() { |
| 671 | return iceBackupCandidatePairPingInterval; |
| 672 | } |
| 673 |
| 674 | @CalledByNative("RTCConfiguration") |
| 675 | KeyType getKeyType() { |
| 676 | return keyType; |
| 677 | } |
| 678 |
| 679 | @CalledByNative("RTCConfiguration") |
| 680 | ContinualGatheringPolicy getContinualGatheringPolicy() { |
| 681 | return continualGatheringPolicy; |
| 682 | } |
| 683 |
| 684 | @CalledByNative("RTCConfiguration") |
| 685 | int getIceCandidatePoolSize() { |
| 686 | return iceCandidatePoolSize; |
| 687 | } |
| 688 |
| 689 | @CalledByNative("RTCConfiguration") |
| 690 | boolean getPruneTurnPorts() { |
| 691 | return pruneTurnPorts; |
| 692 | } |
| 693 |
| 694 | @CalledByNative("RTCConfiguration") |
| 695 | boolean getPresumeWritableWhenFullyRelayed() { |
| 696 | return presumeWritableWhenFullyRelayed; |
| 697 | } |
| 698 |
| 699 | @CalledByNative("RTCConfiguration") |
| 700 | boolean getSurfaceIceCandidatesOnIceTransportTypeChanged() { |
| 701 | return surfaceIceCandidatesOnIceTransportTypeChanged; |
| 702 | } |
| 703 |
| 704 | @Nullable |
| 705 | @CalledByNative("RTCConfiguration") |
| 706 | Integer getIceCheckIntervalStrongConnectivity() { |
| 707 | return iceCheckIntervalStrongConnectivityMs; |
| 708 | } |
| 709 |
| 710 | @Nullable |
| 711 | @CalledByNative("RTCConfiguration") |
| 712 | Integer getIceCheckIntervalWeakConnectivity() { |
| 713 | return iceCheckIntervalWeakConnectivityMs; |
| 714 | } |
| 715 |
| 716 | @Nullable |
| 717 | @CalledByNative("RTCConfiguration") |
| 718 | Integer getIceCheckMinInterval() { |
| 719 | return iceCheckMinInterval; |
| 720 | } |
| 721 |
| 722 | @Nullable |
| 723 | @CalledByNative("RTCConfiguration") |
| 724 | Integer getIceUnwritableTimeout() { |
| 725 | return iceUnwritableTimeMs; |
| 726 | } |
| 727 |
| 728 | @Nullable |
| 729 | @CalledByNative("RTCConfiguration") |
| 730 | Integer getIceUnwritableMinChecks() { |
| 731 | return iceUnwritableMinChecks; |
| 732 | } |
| 733 |
| 734 | @Nullable |
| 735 | @CalledByNative("RTCConfiguration") |
| 736 | Integer getStunCandidateKeepaliveInterval() { |
| 737 | return stunCandidateKeepaliveIntervalMs; |
| 738 | } |
| 739 |
| 740 | @Nullable |
| 741 | @CalledByNative("RTCConfiguration") |
| 742 | Integer getStableWritableConnectionPingIntervalMs() { |
| 743 | return stableWritableConnectionPingIntervalMs; |
| 744 | } |
| 745 |
| 746 | @CalledByNative("RTCConfiguration") |
| 747 | boolean getDisableIPv6OnWifi() { |
| 748 | return disableIPv6OnWifi; |
| 749 | } |
| 750 |
| 751 | @CalledByNative("RTCConfiguration") |
| 752 | int getMaxIPv6Networks() { |
| 753 | return maxIPv6Networks; |
| 754 | } |
| 755 |
| 756 | @Nullable |
| 757 | @CalledByNative("RTCConfiguration") |
| 758 | TurnCustomizer getTurnCustomizer() { |
| 759 | return turnCustomizer; |
| 760 | } |
| 761 |
| 762 | @CalledByNative("RTCConfiguration") |
| 763 | boolean getEnableDscp() { |
| 764 | return enableDscp; |
| 765 | } |
| 766 |
| 767 | @CalledByNative("RTCConfiguration") |
| 768 | boolean getEnableCpuOveruseDetection() { |
| 769 | return enableCpuOveruseDetection; |
| 770 | } |
| 771 |
| 772 | @CalledByNative("RTCConfiguration") |
| 773 | boolean getSuspendBelowMinBitrate() { |
| 774 | return suspendBelowMinBitrate; |
| 775 | } |
| 776 |
| 777 | @Nullable |
| 778 | @CalledByNative("RTCConfiguration") |
| 779 | Integer getScreencastMinBitrate() { |
| 780 | return screencastMinBitrate; |
| 781 | } |
| 782 |
| 783 | @CalledByNative("RTCConfiguration") |
| 784 | AdapterType getNetworkPreference() { |
| 785 | return networkPreference; |
| 786 | } |
| 787 |
| 788 | @CalledByNative("RTCConfiguration") |
| 789 | SdpSemantics getSdpSemantics() { |
| 790 | return sdpSemantics; |
| 791 | } |
| 792 |
| 793 | @CalledByNative("RTCConfiguration") |
| 794 | boolean getActiveResetSrtpParams() { |
| 795 | return activeResetSrtpParams; |
| 796 | } |
| 797 |
| 798 | @Nullable |
| 799 | @CalledByNative("RTCConfiguration") |
| 800 | CryptoOptions getCryptoOptions() { |
| 801 | return cryptoOptions; |
| 802 | } |
| 803 |
| 804 | @Nullable |
| 805 | @CalledByNative("RTCConfiguration") |
| 806 | String getTurnLoggingId() { |
| 807 | return turnLoggingId; |
| 808 | } |
| 809 |
| 810 | @CalledByNative("RTCConfiguration") |
| 811 | boolean getEnableImplicitRollback() { |
| 812 | return enableImplicitRollback; |
| 813 | } |
| 814 |
| 815 | @CalledByNative("RTCConfiguration") |
| 816 | boolean getOfferExtmapAllowMixed() { |
| 817 | return offerExtmapAllowMixed; |
| 818 | } |
| 819 | }; |
| 820 |
| 821 | private final List<MediaStream> localStreams = new ArrayList<>(); |
| 822 | private final long nativePeerConnection; |
| 823 | private List<RtpSender> senders = new ArrayList<>(); |
| 824 | private List<RtpReceiver> receivers = new ArrayList<>(); |
| 825 | private List<RtpTransceiver> transceivers = new ArrayList<>(); |
| 826 |
| 827 | /** |
| 828 | * Wraps a PeerConnection created by the factory. Can be used by clients that want to implement |
| 829 | * their PeerConnection creation in JNI. |
| 830 | */ |
| 831 | public PeerConnection(NativePeerConnectionFactory factory) { |
| 832 | this(factory.createNativePeerConnection()); |
| 833 | } |
| 834 |
| 835 | PeerConnection(long nativePeerConnection) { |
| 836 | this.nativePeerConnection = nativePeerConnection; |
| 837 | } |
| 838 |
| 839 | // JsepInterface. |
| 840 | public SessionDescription getLocalDescription() { |
| 841 | return nativeGetLocalDescription(); |
| 842 | } |
| 843 |
| 844 | public SessionDescription getRemoteDescription() { |
| 845 | return nativeGetRemoteDescription(); |
| 846 | } |
| 847 |
| 848 | public RtcCertificatePem getCertificate() { |
| 849 | return nativeGetCertificate(); |
| 850 | } |
| 851 |
| 852 | public DataChannel createDataChannel(String label, DataChannel.Init init) { |
| 853 | return nativeCreateDataChannel(label, init); |
| 854 | } |
| 855 |
| 856 | public void createOffer(SdpObserver observer, MediaConstraints constraints) { |
| 857 | nativeCreateOffer(observer, constraints); |
| 858 | } |
| 859 |
| 860 | public void createAnswer(SdpObserver observer, MediaConstraints constraints) { |
| 861 | nativeCreateAnswer(observer, constraints); |
| 862 | } |
| 863 |
| 864 | public void setLocalDescription(SdpObserver observer) { |
| 865 | nativeSetLocalDescriptionAutomatically(observer); |
| 866 | } |
| 867 |
| 868 | public void setLocalDescription(SdpObserver observer, SessionDescription sdp) { |
| 869 | nativeSetLocalDescription(observer, sdp); |
| 870 | } |
| 871 |
| 872 | public void setRemoteDescription(SdpObserver observer, SessionDescription sdp) { |
| 873 | nativeSetRemoteDescription(observer, sdp); |
| 874 | } |
| 875 |
| 876 | /** |
| 877 | * Tells the PeerConnection that ICE should be restarted. |
| 878 | */ |
| 879 | public void restartIce() { |
| 880 | nativeRestartIce(); |
| 881 | } |
| 882 |
| 883 | /** |
| 884 | * Enables/disables playout of received audio streams. Enabled by default. |
| 885 | * |
| 886 | * Note that even if playout is enabled, streams will only be played out if |
| 887 | * the appropriate SDP is also applied. The main purpose of this API is to |
| 888 | * be able to control the exact time when audio playout starts. |
| 889 | */ |
| 890 | public void setAudioPlayout(boolean playout) { |
| 891 | nativeSetAudioPlayout(playout); |
| 892 | } |
| 893 |
| 894 | /** |
| 895 | * Enables/disables recording of transmitted audio streams. Enabled by default. |
| 896 | * |
| 897 | * Note that even if recording is enabled, streams will only be recorded if |
| 898 | * the appropriate SDP is also applied. The main purpose of this API is to |
| 899 | * be able to control the exact time when audio recording starts. |
| 900 | */ |
| 901 | public void setAudioRecording(boolean recording) { |
| 902 | nativeSetAudioRecording(recording); |
| 903 | } |
| 904 |
| 905 | public boolean setConfiguration(RTCConfiguration config) { |
| 906 | return nativeSetConfiguration(config); |
| 907 | } |
| 908 |
| 909 | public boolean addIceCandidate(IceCandidate candidate) { |
| 910 | return nativeAddIceCandidate(candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp); |
| 911 | } |
| 912 |
| 913 | public void addIceCandidate(IceCandidate candidate, AddIceObserver observer) { |
| 914 | nativeAddIceCandidateWithObserver( |
| 915 | candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp, observer); |
| 916 | } |
| 917 |
| 918 | public boolean removeIceCandidates(final IceCandidate[] candidates) { |
| 919 | return nativeRemoveIceCandidates(candidates); |
| 920 | } |
| 921 |
| 922 | /** |
| 923 | * Adds a new MediaStream to be sent on this peer connection. |
| 924 | * Note: This method is not supported with SdpSemantics.UNIFIED_PLAN. Please |
| 925 | * use addTrack instead. |
| 926 | */ |
| 927 | public boolean addStream(MediaStream stream) { |
| 928 | boolean ret = nativeAddLocalStream(stream.getNativeMediaStream()); |
| 929 | if (!ret) { |
| 930 | return false; |
| 931 | } |
| 932 | localStreams.add(stream); |
| 933 | return true; |
| 934 | } |
| 935 |
| 936 | /** |
| 937 | * Removes the given media stream from this peer connection. |
| 938 | * This method is not supported with SdpSemantics.UNIFIED_PLAN. Please use |
| 939 | * removeTrack instead. |
| 940 | */ |
| 941 | public void removeStream(MediaStream stream) { |
| 942 | nativeRemoveLocalStream(stream.getNativeMediaStream()); |
| 943 | localStreams.remove(stream); |
| 944 | } |
| 945 |
| 946 | /** |
| 947 | * Creates an RtpSender without a track. |
| 948 | * |
| 949 | * <p>This method allows an application to cause the PeerConnection to negotiate |
| 950 | * sending/receiving a specific media type, but without having a track to |
| 951 | * send yet. |
| 952 | * |
| 953 | * <p>When the application does want to begin sending a track, it can call |
| 954 | * RtpSender.setTrack, which doesn't require any additional SDP negotiation. |
| 955 | * |
| 956 | * <p>Example use: |
| 957 | * <pre> |
| 958 | * {@code |
| 959 | * audioSender = pc.createSender("audio", "stream1"); |
| 960 | * videoSender = pc.createSender("video", "stream1"); |
| 961 | * // Do normal SDP offer/answer, which will kick off ICE/DTLS and negotiate |
| 962 | * // media parameters.... |
| 963 | * // Later, when the endpoint is ready to actually begin sending: |
| 964 | * audioSender.setTrack(audioTrack, false); |
| 965 | * videoSender.setTrack(videoTrack, false); |
| 966 | * } |
| 967 | * </pre> |
| 968 | * <p>Note: This corresponds most closely to "addTransceiver" in the official |
| 969 | * WebRTC API, in that it creates a sender without a track. It was |
| 970 | * implemented before addTransceiver because it provides useful |
| 971 | * functionality, and properly implementing transceivers would have required |
| 972 | * a great deal more work. |
| 973 | * |
| 974 | * <p>Note: This is only available with SdpSemantics.PLAN_B specified. Please use |
| 975 | * addTransceiver instead. |
| 976 | * |
| 977 | * @param kind Corresponds to MediaStreamTrack kinds (must be "audio" or |
| 978 | * "video"). |
| 979 | * @param stream_id The ID of the MediaStream that this sender's track will |
| 980 | * be associated with when SDP is applied to the remote |
| 981 | * PeerConnection. If createSender is used to create an |
| 982 | * audio and video sender that should be synchronized, they |
| 983 | * should use the same stream ID. |
| 984 | * @return A new RtpSender object if successful, or null otherwise. |
| 985 | */ |
| 986 | public RtpSender createSender(String kind, String stream_id) { |
| 987 | RtpSender newSender = nativeCreateSender(kind, stream_id); |
| 988 | if (newSender != null) { |
| 989 | senders.add(newSender); |
| 990 | } |
| 991 | return newSender; |
| 992 | } |
| 993 |
| 994 | /** |
| 995 | * Gets all RtpSenders associated with this peer connection. |
| 996 | * Note that calling getSenders will dispose of the senders previously |
| 997 | * returned. |
| 998 | */ |
| 999 | public List<RtpSender> getSenders() { |
| 1000 | for (RtpSender sender : senders) { |
| 1001 | sender.dispose(); |
| 1002 | } |
| 1003 | senders = nativeGetSenders(); |
| 1004 | return Collections.unmodifiableList(senders); |
| 1005 | } |
| 1006 |
| 1007 | /** |
| 1008 | * Gets all RtpReceivers associated with this peer connection. |
| 1009 | * Note that calling getReceivers will dispose of the receivers previously |
| 1010 | * returned. |
| 1011 | */ |
| 1012 | public List<RtpReceiver> getReceivers() { |
| 1013 | for (RtpReceiver receiver : receivers) { |
| 1014 | receiver.dispose(); |
| 1015 | } |
| 1016 | receivers = nativeGetReceivers(); |
| 1017 | return Collections.unmodifiableList(receivers); |
| 1018 | } |
| 1019 |
| 1020 | /** |
| 1021 | * Gets all RtpTransceivers associated with this peer connection. |
| 1022 | * Note that calling getTransceivers will dispose of the transceivers previously |
| 1023 | * returned. |
| 1024 | * Note: This is only available with SdpSemantics.UNIFIED_PLAN specified. |
| 1025 | */ |
| 1026 | public List<RtpTransceiver> getTransceivers() { |
| 1027 | for (RtpTransceiver transceiver : transceivers) { |
| 1028 | transceiver.dispose(); |
| 1029 | } |
| 1030 | transceivers = nativeGetTransceivers(); |
| 1031 | return Collections.unmodifiableList(transceivers); |
| 1032 | } |
| 1033 |
| 1034 | /** |
| 1035 | * Adds a new media stream track to be sent on this peer connection, and returns |
| 1036 | * the newly created RtpSender. If streamIds are specified, the RtpSender will |
| 1037 | * be associated with the streams specified in the streamIds list. |
| 1038 | * |
| 1039 | * @throws IllegalStateException if an error accors in C++ addTrack. |
| 1040 | * An error can occur if: |
| 1041 | * - A sender already exists for the track. |
| 1042 | * - The peer connection is closed. |
| 1043 | */ |
| 1044 | public RtpSender addTrack(MediaStreamTrack track) { |
| 1045 | return addTrack(track, Collections.emptyList()); |
| 1046 | } |
| 1047 |
| 1048 | public RtpSender addTrack(MediaStreamTrack track, List<String> streamIds) { |
| 1049 | if (track == null || streamIds == null) { |
| 1050 | throw new NullPointerException("No MediaStreamTrack specified in addTrack."); |
| 1051 | } |
| 1052 | RtpSender newSender = nativeAddTrack(track.getNativeMediaStreamTrack(), streamIds); |
| 1053 | if (newSender == null) { |
| 1054 | throw new IllegalStateException("C++ addTrack failed."); |
| 1055 | } |
| 1056 | senders.add(newSender); |
| 1057 | return newSender; |
| 1058 | } |
| 1059 |
| 1060 | /** |
| 1061 | * Stops sending media from sender. The sender will still appear in getSenders. Future |
| 1062 | * calls to createOffer will mark the m section for the corresponding transceiver as |
| 1063 | * receive only or inactive, as defined in JSEP. Returns true on success. |
| 1064 | */ |
| 1065 | public boolean removeTrack(RtpSender sender) { |
| 1066 | if (sender == null) { |
| 1067 | throw new NullPointerException("No RtpSender specified for removeTrack."); |
| 1068 | } |
| 1069 | return nativeRemoveTrack(sender.getNativeRtpSender()); |
| 1070 | } |
| 1071 |
| 1072 | /** |
| 1073 | * Creates a new RtpTransceiver and adds it to the set of transceivers. Adding a |
| 1074 | * transceiver will cause future calls to CreateOffer to add a media description |
| 1075 | * for the corresponding transceiver. |
| 1076 | * |
| 1077 | * <p>The initial value of `mid` in the returned transceiver is null. Setting a |
| 1078 | * new session description may change it to a non-null value. |
| 1079 | * |
| 1080 | * <p>https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtransceiver |
| 1081 | * |
| 1082 | * <p>If a MediaStreamTrack is specified then a transceiver will be added with a |
| 1083 | * sender set to transmit the given track. The kind |
| 1084 | * of the transceiver (and sender/receiver) will be derived from the kind of |
| 1085 | * the track. |
| 1086 | * |
| 1087 | * <p>If MediaType is specified then a transceiver will be added based upon that type. |
| 1088 | * This can be either MEDIA_TYPE_AUDIO or MEDIA_TYPE_VIDEO. |
| 1089 | * |
| 1090 | * <p>Optionally, an RtpTransceiverInit structure can be specified to configure |
| 1091 | * the transceiver from construction. If not specified, the transceiver will |
| 1092 | * default to having a direction of kSendRecv and not be part of any streams. |
| 1093 | * |
| 1094 | * <p>Note: These methods are only available with SdpSemantics.UNIFIED_PLAN specified. |
| 1095 | * @throws IllegalStateException if an error accors in C++ addTransceiver |
| 1096 | */ |
| 1097 | public RtpTransceiver addTransceiver(MediaStreamTrack track) { |
| 1098 | return addTransceiver(track, new RtpTransceiver.RtpTransceiverInit()); |
| 1099 | } |
| 1100 |
| 1101 | public RtpTransceiver addTransceiver( |
| 1102 | MediaStreamTrack track, @Nullable RtpTransceiver.RtpTransceiverInit init) { |
| 1103 | if (track == null) { |
| 1104 | throw new NullPointerException("No MediaStreamTrack specified for addTransceiver."); |
| 1105 | } |
| 1106 | if (init == null) { |
| 1107 | init = new RtpTransceiver.RtpTransceiverInit(); |
| 1108 | } |
| 1109 | RtpTransceiver newTransceiver = |
| 1110 | nativeAddTransceiverWithTrack(track.getNativeMediaStreamTrack(), init); |
| 1111 | if (newTransceiver == null) { |
| 1112 | throw new IllegalStateException("C++ addTransceiver failed."); |
| 1113 | } |
| 1114 | transceivers.add(newTransceiver); |
| 1115 | return newTransceiver; |
| 1116 | } |
| 1117 |
| 1118 | public RtpTransceiver addTransceiver(MediaStreamTrack.MediaType mediaType) { |
| 1119 | return addTransceiver(mediaType, new RtpTransceiver.RtpTransceiverInit()); |
| 1120 | } |
| 1121 |
| 1122 | public RtpTransceiver addTransceiver( |
| 1123 | MediaStreamTrack.MediaType mediaType, @Nullable RtpTransceiver.RtpTransceiverInit init) { |
| 1124 | if (mediaType == null) { |
| 1125 | throw new NullPointerException("No MediaType specified for addTransceiver."); |
| 1126 | } |
| 1127 | if (init == null) { |
| 1128 | init = new RtpTransceiver.RtpTransceiverInit(); |
| 1129 | } |
| 1130 | RtpTransceiver newTransceiver = nativeAddTransceiverOfType(mediaType, init); |
| 1131 | if (newTransceiver == null) { |
| 1132 | throw new IllegalStateException("C++ addTransceiver failed."); |
| 1133 | } |
| 1134 | transceivers.add(newTransceiver); |
| 1135 | return newTransceiver; |
| 1136 | } |
| 1137 |
| 1138 | // Older, non-standard implementation of getStats. |
| 1139 | @Deprecated |
| 1140 | public boolean getStats(StatsObserver observer, @Nullable MediaStreamTrack track) { |
| 1141 | return nativeOldGetStats(observer, (track == null) ? 0 : track.getNativeMediaStreamTrack()); |
| 1142 | } |
| 1143 |
| 1144 | /** |
| 1145 | * Gets stats using the new stats collection API, see webrtc/api/stats/. These |
| 1146 | * will replace old stats collection API when the new API has matured enough. |
| 1147 | */ |
| 1148 | public void getStats(RTCStatsCollectorCallback callback) { |
| 1149 | nativeNewGetStats(callback); |
| 1150 | } |
| 1151 |
| 1152 | /** |
| 1153 | * Gets stats using the new stats collection API, see webrtc/api/stats/. These |
| 1154 | * will replace old stats collection API when the new API has matured enough. |
| 1155 | */ |
| 1156 | public void getStats(RtpSender sender, RTCStatsCollectorCallback callback) { |
| 1157 | nativeNewGetStatsSender(sender.getNativeRtpSender(), callback); |
| 1158 | } |
| 1159 |
| 1160 | /** |
| 1161 | * Gets stats using the new stats collection API, see webrtc/api/stats/. These |
| 1162 | * will replace old stats collection API when the new API has matured enough. |
| 1163 | */ |
| 1164 | public void getStats(RtpReceiver receiver, RTCStatsCollectorCallback callback) { |
| 1165 | nativeNewGetStatsReceiver(receiver.getNativeRtpReceiver(), callback); |
| 1166 | } |
| 1167 |
| 1168 | /** |
| 1169 | * Limits the bandwidth allocated for all RTP streams sent by this |
| 1170 | * PeerConnection. Pass null to leave a value unchanged. |
| 1171 | */ |
| 1172 | public boolean setBitrate(Integer min, Integer current, Integer max) { |
| 1173 | return nativeSetBitrate(min, current, max); |
| 1174 | } |
| 1175 |
| 1176 | /** |
| 1177 | * Starts recording an RTC event log. |
| 1178 | * |
| 1179 | * Ownership of the file is transfered to the native code. If an RTC event |
| 1180 | * log is already being recorded, it will be stopped and a new one will start |
| 1181 | * using the provided file. Logging will continue until the stopRtcEventLog |
| 1182 | * function is called. The max_size_bytes argument is ignored, it is added |
| 1183 | * for future use. |
| 1184 | */ |
| 1185 | public boolean startRtcEventLog(int file_descriptor, int max_size_bytes) { |
| 1186 | return nativeStartRtcEventLog(file_descriptor, max_size_bytes); |
| 1187 | } |
| 1188 |
| 1189 | /** |
| 1190 | * Stops recording an RTC event log. If no RTC event log is currently being |
| 1191 | * recorded, this call will have no effect. |
| 1192 | */ |
| 1193 | public void stopRtcEventLog() { |
| 1194 | nativeStopRtcEventLog(); |
| 1195 | } |
| 1196 |
| 1197 | // TODO(fischman): add support for DTMF-related methods once that API |
| 1198 | // stabilizes. |
| 1199 | public SignalingState signalingState() { |
| 1200 | return nativeSignalingState(); |
| 1201 | } |
| 1202 |
| 1203 | public IceConnectionState iceConnectionState() { |
| 1204 | return nativeIceConnectionState(); |
| 1205 | } |
| 1206 |
| 1207 | public PeerConnectionState connectionState() { |
| 1208 | return nativeConnectionState(); |
| 1209 | } |
| 1210 |
| 1211 | public IceGatheringState iceGatheringState() { |
| 1212 | return nativeIceGatheringState(); |
| 1213 | } |
| 1214 |
| 1215 | public void close() { |
| 1216 | nativeClose(); |
| 1217 | } |
| 1218 |
| 1219 | /** |
| 1220 | * Free native resources associated with this PeerConnection instance. |
| 1221 | * |
| 1222 | * This method removes a reference count from the C++ PeerConnection object, |
| 1223 | * which should result in it being destroyed. It also calls equivalent |
| 1224 | * "dispose" methods on the Java objects attached to this PeerConnection |
| 1225 | * (streams, senders, receivers), such that their associated C++ objects |
| 1226 | * will also be destroyed. |
| 1227 | * |
| 1228 | * <p>Note that this method cannot be safely called from an observer callback |
| 1229 | * (PeerConnection.Observer, DataChannel.Observer, etc.). If you want to, for |
| 1230 | * example, destroy the PeerConnection after an "ICE failed" callback, you |
| 1231 | * must do this asynchronously (in other words, unwind the stack first). See |
| 1232 | * <a href="https://bugs.chromium.org/p/webrtc/issues/detail?id=3721">bug |
| 1233 | * 3721</a> for more details. |
| 1234 | */ |
| 1235 | public void dispose() { |
| 1236 | close(); |
| 1237 | for (MediaStream stream : localStreams) { |
| 1238 | nativeRemoveLocalStream(stream.getNativeMediaStream()); |
| 1239 | stream.dispose(); |
| 1240 | } |
| 1241 | localStreams.clear(); |
| 1242 | for (RtpSender sender : senders) { |
| 1243 | sender.dispose(); |
| 1244 | } |
| 1245 | senders.clear(); |
| 1246 | for (RtpReceiver receiver : receivers) { |
| 1247 | receiver.dispose(); |
| 1248 | } |
| 1249 | for (RtpTransceiver transceiver : transceivers) { |
| 1250 | transceiver.dispose(); |
| 1251 | } |
| 1252 | transceivers.clear(); |
| 1253 | receivers.clear(); |
| 1254 | nativeFreeOwnedPeerConnection(nativePeerConnection); |
| 1255 | } |
| 1256 |
| 1257 | /** Returns a pointer to the native webrtc::PeerConnectionInterface. */ |
| 1258 | public long getNativePeerConnection() { |
| 1259 | return nativeGetNativePeerConnection(); |
| 1260 | } |
| 1261 |
| 1262 | @CalledByNative |
| 1263 | long getNativeOwnedPeerConnection() { |
| 1264 | return nativePeerConnection; |
| 1265 | } |
| 1266 |
| 1267 | public static long createNativePeerConnectionObserver(Observer observer) { |
| 1268 | return nativeCreatePeerConnectionObserver(observer); |
| 1269 | } |
| 1270 |
| 1271 | private native long nativeGetNativePeerConnection(); |
| 1272 | private native SessionDescription nativeGetLocalDescription(); |
| 1273 | private native SessionDescription nativeGetRemoteDescription(); |
| 1274 | private native RtcCertificatePem nativeGetCertificate(); |
| 1275 | private native DataChannel nativeCreateDataChannel(String label, DataChannel.Init init); |
| 1276 | private native void nativeCreateOffer(SdpObserver observer, MediaConstraints constraints); |
| 1277 | private native void nativeCreateAnswer(SdpObserver observer, MediaConstraints constraints); |
| 1278 | private native void nativeSetLocalDescriptionAutomatically(SdpObserver observer); |
| 1279 | private native void nativeSetLocalDescription(SdpObserver observer, SessionDescription sdp); |
| 1280 | private native void nativeSetRemoteDescription(SdpObserver observer, SessionDescription sdp); |
| 1281 | private native void nativeRestartIce(); |
| 1282 | private native void nativeSetAudioPlayout(boolean playout); |
| 1283 | private native void nativeSetAudioRecording(boolean recording); |
| 1284 | private native boolean nativeSetBitrate(Integer min, Integer current, Integer max); |
| 1285 | private native SignalingState nativeSignalingState(); |
| 1286 | private native IceConnectionState nativeIceConnectionState(); |
| 1287 | private native PeerConnectionState nativeConnectionState(); |
| 1288 | private native IceGatheringState nativeIceGatheringState(); |
| 1289 | private native void nativeClose(); |
| 1290 | private static native long nativeCreatePeerConnectionObserver(Observer observer); |
| 1291 | private static native void nativeFreeOwnedPeerConnection(long ownedPeerConnection); |
| 1292 | private native boolean nativeSetConfiguration(RTCConfiguration config); |
| 1293 | private native boolean nativeAddIceCandidate( |
| 1294 | String sdpMid, int sdpMLineIndex, String iceCandidateSdp); |
| 1295 | private native void nativeAddIceCandidateWithObserver( |
| 1296 | String sdpMid, int sdpMLineIndex, String iceCandidateSdp, AddIceObserver observer); |
| 1297 | private native boolean nativeRemoveIceCandidates(final IceCandidate[] candidates); |
| 1298 | private native boolean nativeAddLocalStream(long stream); |
| 1299 | private native void nativeRemoveLocalStream(long stream); |
| 1300 | private native boolean nativeOldGetStats(StatsObserver observer, long nativeTrack); |
| 1301 | private native void nativeNewGetStats(RTCStatsCollectorCallback callback); |
| 1302 | private native void nativeNewGetStatsSender(long sender, RTCStatsCollectorCallback callback); |
| 1303 | private native void nativeNewGetStatsReceiver(long receiver, RTCStatsCollectorCallback callback); |
| 1304 | private native RtpSender nativeCreateSender(String kind, String stream_id); |
| 1305 | private native List<RtpSender> nativeGetSenders(); |
| 1306 | private native List<RtpReceiver> nativeGetReceivers(); |
| 1307 | private native List<RtpTransceiver> nativeGetTransceivers(); |
| 1308 | private native RtpSender nativeAddTrack(long track, List<String> streamIds); |
| 1309 | private native boolean nativeRemoveTrack(long sender); |
| 1310 | private native RtpTransceiver nativeAddTransceiverWithTrack( |
| 1311 | long track, RtpTransceiver.RtpTransceiverInit init); |
| 1312 | private native RtpTransceiver nativeAddTransceiverOfType( |
| 1313 | MediaStreamTrack.MediaType mediaType, RtpTransceiver.RtpTransceiverInit init); |
| 1314 | private native boolean nativeStartRtcEventLog(int file_descriptor, int max_size_bytes); |
| 1315 | private native void nativeStopRtcEventLog(); |
| 1316 | } |
| 1317 |