blob: 93326a41a30e061c38303590bf28debadb55f22f [file] [log] [blame]
Seth Hampsonc384e142018-03-06 15:47:10 -08001/*
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
11package org.webrtc;
12
13import java.util.ArrayList;
14import java.util.Collections;
15import java.util.List;
16import 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 */
Seth Hampsonc384e142018-03-06 15:47:10 -080034public 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
99 private final long nativeRtpTransceiver;
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() {
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() {
125 return nativeGetMid(nativeRtpTransceiver);
126 }
127
128 /**
129 * The sender attribute exposes the RtpSender corresponding to the RTP media
130 * that may be sent with the transceiver's mid. The sender is always present,
131 * regardless of the direction of media.
132 * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-sender
133 */
134 public RtpSender getSender() {
135 return cachedSender;
136 }
137
138 /**
139 * The receiver attribute exposes the RtpReceiver corresponding to the RTP
140 * media that may be received with the transceiver's mid. The receiver is
141 * always present, regardless of the direction of media.
142 * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-receiver
143 */
144 public RtpReceiver getReceiver() {
145 return cachedReceiver;
146 }
147
148 /**
149 * The stopped attribute indicates that the sender of this transceiver will no
150 * longer send, and that the receiver will no longer receive. It is true if
151 * either stop has been called or if setting the local or remote description
152 * has caused the RtpTransceiver to be stopped.
153 * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-stopped
154 */
155 public boolean isStopped() {
156 return nativeStopped(nativeRtpTransceiver);
157 }
158
159 /**
160 * The direction attribute indicates the preferred direction of this
161 * transceiver, which will be used in calls to CreateOffer and CreateAnswer.
162 * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-direction
163 */
164 public RtpTransceiverDirection getDirection() {
165 return nativeDirection(nativeRtpTransceiver);
166 }
167
168 /**
169 * The current_direction attribute indicates the current direction negotiated
170 * for this transceiver. If this transceiver has never been represented in an
171 * offer/answer exchange, or if the transceiver is stopped, the value is null.
172 * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-currentdirection
173 */
174 public RtpTransceiverDirection getCurrentDirection() {
175 return nativeCurrentDirection(nativeRtpTransceiver);
176 }
177
178 /**
179 * Sets the preferred direction of this transceiver. An update of
180 * directionality does not take effect immediately. Instead, future calls to
181 * CreateOffer and CreateAnswer mark the corresponding media descriptions as
182 * sendrecv, sendonly, recvonly, or inactive.
183 * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-direction
184 */
185 public void setDirection(RtpTransceiverDirection rtpTransceiverDirection) {
186 nativeSetDirection(nativeRtpTransceiver, rtpTransceiverDirection);
187 }
188
189 /**
190 * The Stop method irreversibly stops the RtpTransceiver. The sender of this
191 * transceiver will no longer send, the receiver will no longer receive.
192 * https://w3c.github.io/webrtc-pc/#dom-rtcrtptransceiver-stop
193 */
194 public void stop() {
195 nativeStop(nativeRtpTransceiver);
196 }
197
Seth Hampson31dbc242018-05-07 09:28:19 -0700198 @CalledByNative
Seth Hampsonc384e142018-03-06 15:47:10 -0800199 public void dispose() {
200 cachedSender.dispose();
201 cachedReceiver.dispose();
202 JniCommon.nativeReleaseRef(nativeRtpTransceiver);
203 }
204
205 private static native MediaStreamTrack.MediaType nativeGetMediaType(long rtpTransceiver);
206 private static native String nativeGetMid(long rtpTransceiver);
207 private static native RtpSender nativeGetSender(long rtpTransceiver);
208 private static native RtpReceiver nativeGetReceiver(long rtpTransceiver);
209 private static native boolean nativeStopped(long rtpTransceiver);
210 private static native RtpTransceiverDirection nativeDirection(long rtpTransceiver);
211 private static native RtpTransceiverDirection nativeCurrentDirection(long rtpTransceiver);
212 private static native void nativeStop(long rtpTransceiver);
213 private static native void nativeSetDirection(
214 long rtpTransceiver, RtpTransceiverDirection rtpTransceiverDirection);
215}