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; |
| 16 | import org.webrtc.RtpParameters.Encoding; |
| 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 | */ |
| 34 | @JNINamespace("webrtc::jni") |
| 35 | public class RtpTransceiver { |
| 36 | /** Java version of webrtc::RtpTransceiverDirection - the ordering must be kept in sync. */ |
| 37 | public enum RtpTransceiverDirection { |
| 38 | SEND_RECV(0), |
| 39 | SEND_ONLY(1), |
| 40 | RECV_ONLY(2), |
| 41 | INACTIVE(3); |
| 42 | |
| 43 | private final int nativeIndex; |
| 44 | |
| 45 | private RtpTransceiverDirection(int nativeIndex) { |
| 46 | this.nativeIndex = nativeIndex; |
| 47 | } |
| 48 | |
| 49 | @CalledByNative("RtpTransceiverDirection") |
| 50 | int getNativeIndex() { |
| 51 | return nativeIndex; |
| 52 | } |
| 53 | |
| 54 | @CalledByNative("RtpTransceiverDirection") |
| 55 | static RtpTransceiverDirection fromNativeIndex(int nativeIndex) { |
| 56 | for (RtpTransceiverDirection type : RtpTransceiverDirection.values()) { |
| 57 | if (type.getNativeIndex() == nativeIndex) { |
| 58 | return type; |
| 59 | } |
| 60 | } |
| 61 | throw new IllegalArgumentException( |
| 62 | "Uknown native RtpTransceiverDirection type" + nativeIndex); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Tracks webrtc::RtpTransceiverInit. https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiverinit |
| 68 | * A structure for initializing an RtpTransceiver in a call to addTransceiver. |
| 69 | * Note: This does not contain a list of encoding parameters, because they are currently |
| 70 | * not being used natively. |
| 71 | */ |
| 72 | public static final class RtpTransceiverInit { |
| 73 | private final RtpTransceiverDirection direction; |
| 74 | private final List<String> streamIds; |
| 75 | |
| 76 | public RtpTransceiverInit() { |
| 77 | this(RtpTransceiverDirection.SEND_RECV); |
| 78 | } |
| 79 | |
| 80 | public RtpTransceiverInit(RtpTransceiverDirection direction) { |
| 81 | this(direction, Collections.emptyList()); |
| 82 | } |
| 83 | |
| 84 | public RtpTransceiverInit(RtpTransceiverDirection direction, List<String> streamIds) { |
| 85 | this.direction = direction; |
| 86 | this.streamIds = new ArrayList<String>(streamIds); |
| 87 | } |
| 88 | |
| 89 | @CalledByNative("RtpTransceiverInit") |
| 90 | int getDirectionNativeIndex() { |
| 91 | return direction.getNativeIndex(); |
| 92 | } |
| 93 | |
| 94 | @CalledByNative("RtpTransceiverInit") |
| 95 | List<String> getStreamIds() { |
| 96 | return new ArrayList<String>(this.streamIds); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | private final long nativeRtpTransceiver; |
| 101 | private RtpSender cachedSender; |
| 102 | private RtpReceiver cachedReceiver; |
| 103 | |
| 104 | @CalledByNative |
| 105 | protected RtpTransceiver(long nativeRtpTransceiver) { |
| 106 | this.nativeRtpTransceiver = nativeRtpTransceiver; |
| 107 | cachedSender = nativeGetSender(nativeRtpTransceiver); |
| 108 | cachedReceiver = nativeGetReceiver(nativeRtpTransceiver); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Media type of the transceiver. Any sender(s)/receiver(s) will have this |
| 113 | * type as well. |
| 114 | */ |
| 115 | public MediaStreamTrack.MediaType getMediaType() { |
| 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() { |
| 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() { |
| 157 | return nativeStopped(nativeRtpTransceiver); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * The direction attribute indicates the preferred direction of this |
| 162 | * transceiver, which will be used in calls to CreateOffer and CreateAnswer. |
| 163 | * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-direction |
| 164 | */ |
| 165 | public RtpTransceiverDirection getDirection() { |
| 166 | return nativeDirection(nativeRtpTransceiver); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * The current_direction attribute indicates the current direction negotiated |
| 171 | * for this transceiver. If this transceiver has never been represented in an |
| 172 | * offer/answer exchange, or if the transceiver is stopped, the value is null. |
| 173 | * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-currentdirection |
| 174 | */ |
| 175 | public RtpTransceiverDirection getCurrentDirection() { |
| 176 | return nativeCurrentDirection(nativeRtpTransceiver); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Sets the preferred direction of this transceiver. An update of |
| 181 | * directionality does not take effect immediately. Instead, future calls to |
| 182 | * CreateOffer and CreateAnswer mark the corresponding media descriptions as |
| 183 | * sendrecv, sendonly, recvonly, or inactive. |
| 184 | * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-direction |
| 185 | */ |
| 186 | public void setDirection(RtpTransceiverDirection rtpTransceiverDirection) { |
| 187 | nativeSetDirection(nativeRtpTransceiver, rtpTransceiverDirection); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * The Stop method irreversibly stops the RtpTransceiver. The sender of this |
| 192 | * transceiver will no longer send, the receiver will no longer receive. |
| 193 | * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-stop |
| 194 | */ |
| 195 | public void stop() { |
| 196 | nativeStop(nativeRtpTransceiver); |
| 197 | } |
| 198 | |
Seth Hampson | 31dbc24 | 2018-05-07 09:28:19 -0700 | [diff] [blame^] | 199 | @CalledByNative |
Seth Hampson | c384e14 | 2018-03-06 15:47:10 -0800 | [diff] [blame] | 200 | public void dispose() { |
| 201 | cachedSender.dispose(); |
| 202 | cachedReceiver.dispose(); |
| 203 | JniCommon.nativeReleaseRef(nativeRtpTransceiver); |
| 204 | } |
| 205 | |
| 206 | private static native MediaStreamTrack.MediaType nativeGetMediaType(long rtpTransceiver); |
| 207 | private static native String nativeGetMid(long rtpTransceiver); |
| 208 | private static native RtpSender nativeGetSender(long rtpTransceiver); |
| 209 | private static native RtpReceiver nativeGetReceiver(long rtpTransceiver); |
| 210 | private static native boolean nativeStopped(long rtpTransceiver); |
| 211 | private static native RtpTransceiverDirection nativeDirection(long rtpTransceiver); |
| 212 | private static native RtpTransceiverDirection nativeCurrentDirection(long rtpTransceiver); |
| 213 | private static native void nativeStop(long rtpTransceiver); |
| 214 | private static native void nativeSetDirection( |
| 215 | long rtpTransceiver, RtpTransceiverDirection rtpTransceiverDirection); |
| 216 | } |