blob: 7f875e6c8ead46d281c3131e96c72ba5549278eb [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;
Patrik Höglundbd6ffaf2018-11-16 14:55:16 +010016import org.webrtc.MediaStreamTrack;
Seth Hampsonc384e142018-03-06 15:47:10 -080017
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
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020099 private long nativeRtpTransceiver;
Seth Hampsonc384e142018-03-06 15:47:10 -0800100 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äkiee05e902018-09-28 14:38:21 +0200115 checkRtpTransceiverExists();
Seth Hampsonc384e142018-03-06 15:47:10 -0800116 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äkiee05e902018-09-28 14:38:21 +0200126 checkRtpTransceiverExists();
Seth Hampsonc384e142018-03-06 15:47:10 -0800127 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äkiee05e902018-09-28 14:38:21 +0200158 checkRtpTransceiverExists();
Seth Hampsonc384e142018-03-06 15:47:10 -0800159 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äkiee05e902018-09-28 14:38:21 +0200168 checkRtpTransceiverExists();
Seth Hampsonc384e142018-03-06 15:47:10 -0800169 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äkiee05e902018-09-28 14:38:21 +0200179 checkRtpTransceiverExists();
Seth Hampsonc384e142018-03-06 15:47:10 -0800180 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äkiee05e902018-09-28 14:38:21 +0200191 checkRtpTransceiverExists();
Seth Hampsonc384e142018-03-06 15:47:10 -0800192 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äkiee05e902018-09-28 14:38:21 +0200201 checkRtpTransceiverExists();
Seth Hampsonc384e142018-03-06 15:47:10 -0800202 nativeStop(nativeRtpTransceiver);
203 }
204
Seth Hampson31dbc242018-05-07 09:28:19 -0700205 @CalledByNative
Seth Hampsonc384e142018-03-06 15:47:10 -0800206 public void dispose() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200207 checkRtpTransceiverExists();
Seth Hampsonc384e142018-03-06 15:47:10 -0800208 cachedSender.dispose();
209 cachedReceiver.dispose();
210 JniCommon.nativeReleaseRef(nativeRtpTransceiver);
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200211 nativeRtpTransceiver = 0;
212 }
213
214 private void checkRtpTransceiverExists() {
215 if (nativeRtpTransceiver == 0) {
216 throw new IllegalStateException("RtpTransceiver has been disposed.");
217 }
Seth Hampsonc384e142018-03-06 15:47:10 -0800218 }
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}