Seth Hampson | c384e14 | 2018-03-06 15:47:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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. |
| 9 | */ |
| 10 | |
| 11 | package org.webrtc; |
| 12 | |
| 13 | import java.util.ArrayList; |
| 14 | import java.util.Collections; |
| 15 | import java.util.List; |
Seth Hampson | c384e14 | 2018-03-06 15:47:10 -0800 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * Java wrapper for a C++ RtpTransceiverInterface. |
| 19 | * |
| 20 | * <p>The RTCRtpTransceiver maps to the RTCRtpTransceiver defined by the WebRTC |
| 21 | * specification. A transceiver represents a combination of an RTCRtpSender |
| 22 | * and an RTCRtpReceiver that share a common mid. As defined in JSEP, an |
| 23 | * RTCRtpTransceiver is said to be associated with a media description if its |
| 24 | * mid property is non-nil; otherwise, it is said to be disassociated. |
| 25 | * JSEP: https://tools.ietf.org/html/draft-ietf-rtcweb-jsep-24 |
| 26 | * |
| 27 | * <p>Note that RTCRtpTransceivers are only supported when using |
| 28 | * RTCPeerConnection with Unified Plan SDP. |
| 29 | * |
| 30 | * <p>WebRTC specification for RTCRtpTransceiver, the JavaScript analog: |
| 31 | * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver |
| 32 | */ |
Seth Hampson | c384e14 | 2018-03-06 15:47:10 -0800 | [diff] [blame] | 33 | public class RtpTransceiver { |
| 34 | /** Java version of webrtc::RtpTransceiverDirection - the ordering must be kept in sync. */ |
| 35 | public enum RtpTransceiverDirection { |
| 36 | SEND_RECV(0), |
| 37 | SEND_ONLY(1), |
| 38 | RECV_ONLY(2), |
| 39 | INACTIVE(3); |
| 40 | |
| 41 | private final int nativeIndex; |
| 42 | |
| 43 | private RtpTransceiverDirection(int nativeIndex) { |
| 44 | this.nativeIndex = nativeIndex; |
| 45 | } |
| 46 | |
| 47 | @CalledByNative("RtpTransceiverDirection") |
| 48 | int getNativeIndex() { |
| 49 | return nativeIndex; |
| 50 | } |
| 51 | |
| 52 | @CalledByNative("RtpTransceiverDirection") |
| 53 | static RtpTransceiverDirection fromNativeIndex(int nativeIndex) { |
| 54 | for (RtpTransceiverDirection type : RtpTransceiverDirection.values()) { |
| 55 | if (type.getNativeIndex() == nativeIndex) { |
| 56 | return type; |
| 57 | } |
| 58 | } |
| 59 | throw new IllegalArgumentException( |
| 60 | "Uknown native RtpTransceiverDirection type" + nativeIndex); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Tracks webrtc::RtpTransceiverInit. https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiverinit |
| 66 | * A structure for initializing an RtpTransceiver in a call to addTransceiver. |
| 67 | * Note: This does not contain a list of encoding parameters, because they are currently |
| 68 | * not being used natively. |
| 69 | */ |
| 70 | public static final class RtpTransceiverInit { |
| 71 | private final RtpTransceiverDirection direction; |
| 72 | private final List<String> streamIds; |
| 73 | |
| 74 | public RtpTransceiverInit() { |
| 75 | this(RtpTransceiverDirection.SEND_RECV); |
| 76 | } |
| 77 | |
| 78 | public RtpTransceiverInit(RtpTransceiverDirection direction) { |
| 79 | this(direction, Collections.emptyList()); |
| 80 | } |
| 81 | |
| 82 | public RtpTransceiverInit(RtpTransceiverDirection direction, List<String> streamIds) { |
| 83 | this.direction = direction; |
| 84 | this.streamIds = new ArrayList<String>(streamIds); |
| 85 | } |
| 86 | |
| 87 | @CalledByNative("RtpTransceiverInit") |
| 88 | int getDirectionNativeIndex() { |
| 89 | return direction.getNativeIndex(); |
| 90 | } |
| 91 | |
| 92 | @CalledByNative("RtpTransceiverInit") |
| 93 | List<String> getStreamIds() { |
| 94 | return new ArrayList<String>(this.streamIds); |
| 95 | } |
| 96 | } |
| 97 | |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame^] | 98 | private long nativeRtpTransceiver; |
Seth Hampson | c384e14 | 2018-03-06 15:47:10 -0800 | [diff] [blame] | 99 | private RtpSender cachedSender; |
| 100 | private RtpReceiver cachedReceiver; |
| 101 | |
| 102 | @CalledByNative |
| 103 | protected RtpTransceiver(long nativeRtpTransceiver) { |
| 104 | this.nativeRtpTransceiver = nativeRtpTransceiver; |
| 105 | cachedSender = nativeGetSender(nativeRtpTransceiver); |
| 106 | cachedReceiver = nativeGetReceiver(nativeRtpTransceiver); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Media type of the transceiver. Any sender(s)/receiver(s) will have this |
| 111 | * type as well. |
| 112 | */ |
| 113 | public MediaStreamTrack.MediaType getMediaType() { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame^] | 114 | checkRtpTransceiverExists(); |
Seth Hampson | c384e14 | 2018-03-06 15:47:10 -0800 | [diff] [blame] | 115 | return nativeGetMediaType(nativeRtpTransceiver); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * The mid attribute is the mid negotiated and present in the local and |
| 120 | * remote descriptions. Before negotiation is complete, the mid value may be |
| 121 | * null. After rollbacks, the value may change from a non-null value to null. |
| 122 | * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-mid |
| 123 | */ |
| 124 | public String getMid() { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame^] | 125 | checkRtpTransceiverExists(); |
Seth Hampson | c384e14 | 2018-03-06 15:47:10 -0800 | [diff] [blame] | 126 | return nativeGetMid(nativeRtpTransceiver); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * The sender attribute exposes the RtpSender corresponding to the RTP media |
| 131 | * that may be sent with the transceiver's mid. The sender is always present, |
| 132 | * regardless of the direction of media. |
| 133 | * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-sender |
| 134 | */ |
| 135 | public RtpSender getSender() { |
| 136 | return cachedSender; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * The receiver attribute exposes the RtpReceiver corresponding to the RTP |
| 141 | * media that may be received with the transceiver's mid. The receiver is |
| 142 | * always present, regardless of the direction of media. |
| 143 | * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-receiver |
| 144 | */ |
| 145 | public RtpReceiver getReceiver() { |
| 146 | return cachedReceiver; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * The stopped attribute indicates that the sender of this transceiver will no |
| 151 | * longer send, and that the receiver will no longer receive. It is true if |
| 152 | * either stop has been called or if setting the local or remote description |
| 153 | * has caused the RtpTransceiver to be stopped. |
| 154 | * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-stopped |
| 155 | */ |
| 156 | public boolean isStopped() { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame^] | 157 | checkRtpTransceiverExists(); |
Seth Hampson | c384e14 | 2018-03-06 15:47:10 -0800 | [diff] [blame] | 158 | return nativeStopped(nativeRtpTransceiver); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * The direction attribute indicates the preferred direction of this |
| 163 | * transceiver, which will be used in calls to CreateOffer and CreateAnswer. |
| 164 | * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-direction |
| 165 | */ |
| 166 | public RtpTransceiverDirection getDirection() { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame^] | 167 | checkRtpTransceiverExists(); |
Seth Hampson | c384e14 | 2018-03-06 15:47:10 -0800 | [diff] [blame] | 168 | return nativeDirection(nativeRtpTransceiver); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * The current_direction attribute indicates the current direction negotiated |
| 173 | * for this transceiver. If this transceiver has never been represented in an |
| 174 | * offer/answer exchange, or if the transceiver is stopped, the value is null. |
| 175 | * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-currentdirection |
| 176 | */ |
| 177 | public RtpTransceiverDirection getCurrentDirection() { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame^] | 178 | checkRtpTransceiverExists(); |
Seth Hampson | c384e14 | 2018-03-06 15:47:10 -0800 | [diff] [blame] | 179 | return nativeCurrentDirection(nativeRtpTransceiver); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Sets the preferred direction of this transceiver. An update of |
| 184 | * directionality does not take effect immediately. Instead, future calls to |
| 185 | * CreateOffer and CreateAnswer mark the corresponding media descriptions as |
| 186 | * sendrecv, sendonly, recvonly, or inactive. |
| 187 | * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-direction |
| 188 | */ |
| 189 | public void setDirection(RtpTransceiverDirection rtpTransceiverDirection) { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame^] | 190 | checkRtpTransceiverExists(); |
Seth Hampson | c384e14 | 2018-03-06 15:47:10 -0800 | [diff] [blame] | 191 | nativeSetDirection(nativeRtpTransceiver, rtpTransceiverDirection); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * The Stop method irreversibly stops the RtpTransceiver. The sender of this |
| 196 | * transceiver will no longer send, the receiver will no longer receive. |
| 197 | * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-stop |
| 198 | */ |
| 199 | public void stop() { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame^] | 200 | checkRtpTransceiverExists(); |
Seth Hampson | c384e14 | 2018-03-06 15:47:10 -0800 | [diff] [blame] | 201 | nativeStop(nativeRtpTransceiver); |
| 202 | } |
| 203 | |
Seth Hampson | 31dbc24 | 2018-05-07 09:28:19 -0700 | [diff] [blame] | 204 | @CalledByNative |
Seth Hampson | c384e14 | 2018-03-06 15:47:10 -0800 | [diff] [blame] | 205 | public void dispose() { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame^] | 206 | checkRtpTransceiverExists(); |
Seth Hampson | c384e14 | 2018-03-06 15:47:10 -0800 | [diff] [blame] | 207 | cachedSender.dispose(); |
| 208 | cachedReceiver.dispose(); |
| 209 | JniCommon.nativeReleaseRef(nativeRtpTransceiver); |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame^] | 210 | nativeRtpTransceiver = 0; |
| 211 | } |
| 212 | |
| 213 | private void checkRtpTransceiverExists() { |
| 214 | if (nativeRtpTransceiver == 0) { |
| 215 | throw new IllegalStateException("RtpTransceiver has been disposed."); |
| 216 | } |
Seth Hampson | c384e14 | 2018-03-06 15:47:10 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | private static native MediaStreamTrack.MediaType nativeGetMediaType(long rtpTransceiver); |
| 220 | private static native String nativeGetMid(long rtpTransceiver); |
| 221 | private static native RtpSender nativeGetSender(long rtpTransceiver); |
| 222 | private static native RtpReceiver nativeGetReceiver(long rtpTransceiver); |
| 223 | private static native boolean nativeStopped(long rtpTransceiver); |
| 224 | private static native RtpTransceiverDirection nativeDirection(long rtpTransceiver); |
| 225 | private static native RtpTransceiverDirection nativeCurrentDirection(long rtpTransceiver); |
| 226 | private static native void nativeStop(long rtpTransceiver); |
| 227 | private static native void nativeSetDirection( |
| 228 | long rtpTransceiver, RtpTransceiverDirection rtpTransceiverDirection); |
| 229 | } |