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