blob: 8d2d72857d01a3467d7b7a9d82f0d9a45b18790d [file] [log] [blame]
Steve Anton6e634bf2017-11-13 10:44:53 -08001/*
2 * Copyright 2017 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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef PC_RTP_TRANSCEIVER_H_
12#define PC_RTP_TRANSCEIVER_H_
Steve Anton6e634bf2017-11-13 10:44:53 -080013
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000014#include <stddef.h>
15
16#include <algorithm>
17#include <functional>
Steve Anton6e634bf2017-11-13 10:44:53 -080018#include <string>
19#include <vector>
20
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000021#include "absl/types/optional.h"
22#include "api/array_view.h"
23#include "api/media_types.h"
24#include "api/proxy.h"
25#include "api/rtc_error.h"
26#include "api/rtp_parameters.h"
27#include "api/rtp_receiver_interface.h"
28#include "api/rtp_sender_interface.h"
29#include "api/rtp_transceiver_direction.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "api/rtp_transceiver_interface.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000031#include "api/scoped_refptr.h"
32#include "api/task_queue/task_queue_base.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010033#include "pc/channel_interface.h"
Florent Castelli2d9d82e2019-04-23 19:25:51 +020034#include "pc/channel_manager.h"
Steve Anton10542f22019-01-11 09:11:00 -080035#include "pc/rtp_receiver.h"
36#include "pc/rtp_sender.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000037#include "rtc_base/ref_counted_object.h"
38#include "rtc_base/third_party/sigslot/sigslot.h"
39#include "rtc_base/thread_annotations.h"
Steve Anton6e634bf2017-11-13 10:44:53 -080040
41namespace webrtc {
42
43// Implementation of the public RtpTransceiverInterface.
44//
45// The RtpTransceiverInterface is only intended to be used with a PeerConnection
46// that enables Unified Plan SDP. Thus, the methods that only need to implement
47// public API features and are not used internally can assume exactly one sender
48// and receiver.
49//
50// Since the RtpTransceiver is used internally by PeerConnection for tracking
51// RtpSenders, RtpReceivers, and BaseChannels, and PeerConnection needs to be
52// backwards compatible with Plan B SDP, this implementation is more flexible
53// than that required by the WebRTC specification.
54//
55// With Plan B SDP, an RtpTransceiver can have any number of senders and
56// receivers which map to a=ssrc lines in the m= section.
57// With Unified Plan SDP, an RtpTransceiver will have exactly one sender and one
58// receiver which are encapsulated by the m= section.
59//
60// This class manages the RtpSenders, RtpReceivers, and BaseChannel associated
61// with this m= section. Since the transceiver, senders, and receivers are
62// reference counted and can be referenced from JavaScript (in Chromium), these
63// objects must be ready to live for an arbitrary amount of time. The
64// BaseChannel is not reference counted and is owned by the ChannelManager, so
65// the PeerConnection must take care of creating/deleting the BaseChannel and
66// setting the channel reference in the transceiver to null when it has been
67// deleted.
68//
69// The RtpTransceiver is specialized to either audio or video according to the
70// MediaType specified in the constructor. Audio RtpTransceivers will have
71// AudioRtpSenders, AudioRtpReceivers, and a VoiceChannel. Video RtpTransceivers
72// will have VideoRtpSenders, VideoRtpReceivers, and a VideoChannel.
73class RtpTransceiver final
Steve Anton60776752018-01-10 11:51:34 -080074 : public rtc::RefCountedObject<RtpTransceiverInterface>,
75 public sigslot::has_slots<> {
Steve Anton6e634bf2017-11-13 10:44:53 -080076 public:
Steve Anton79e79602017-11-20 10:25:56 -080077 // Construct a Plan B-style RtpTransceiver with no senders, receivers, or
78 // channel set.
Steve Anton6e634bf2017-11-13 10:44:53 -080079 // |media_type| specifies the type of RtpTransceiver (and, by transitivity,
80 // the type of senders, receivers, and channel). Can either by audio or video.
81 explicit RtpTransceiver(cricket::MediaType media_type);
Steve Anton79e79602017-11-20 10:25:56 -080082 // Construct a Unified Plan-style RtpTransceiver with the given sender and
83 // receiver. The media type will be derived from the media types of the sender
84 // and receiver. The sender and receiver should have the same media type.
Markus Handell0357b3e2020-03-16 13:40:51 +010085 // |HeaderExtensionsToOffer| is used for initializing the return value of
86 // HeaderExtensionsToOffer().
Steve Anton79e79602017-11-20 10:25:56 -080087 RtpTransceiver(
88 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
89 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
Florent Castelli2d9d82e2019-04-23 19:25:51 +020090 receiver,
Markus Handell0357b3e2020-03-16 13:40:51 +010091 cricket::ChannelManager* channel_manager,
Harald Alvestrand280054f2020-11-10 13:12:53 +000092 std::vector<RtpHeaderExtensionCapability> HeaderExtensionsToOffer,
93 std::function<void()> on_negotiation_needed);
Steve Anton6e634bf2017-11-13 10:44:53 -080094 ~RtpTransceiver() override;
95
Steve Anton6e634bf2017-11-13 10:44:53 -080096 // Returns the Voice/VideoChannel set for this transceiver. May be null if
97 // the transceiver is not in the currently set local/remote description.
Amit Hilbuchdd9390c2018-11-13 16:26:05 -080098 cricket::ChannelInterface* channel() const { return channel_; }
Steve Anton6e634bf2017-11-13 10:44:53 -080099
100 // Sets the Voice/VideoChannel. The caller must pass in the correct channel
101 // implementation based on the type of the transceiver.
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800102 void SetChannel(cricket::ChannelInterface* channel);
Steve Anton6e634bf2017-11-13 10:44:53 -0800103
104 // Adds an RtpSender of the appropriate type to be owned by this transceiver.
105 // Must not be null.
106 void AddSender(
107 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender);
108
109 // Removes the given RtpSender. Returns false if the sender is not owned by
110 // this transceiver.
111 bool RemoveSender(RtpSenderInterface* sender);
112
113 // Returns a vector of the senders owned by this transceiver.
114 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
115 senders() const {
116 return senders_;
117 }
118
119 // Adds an RtpReceiver of the appropriate type to be owned by this
120 // transceiver. Must not be null.
121 void AddReceiver(
122 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
123 receiver);
124
125 // Removes the given RtpReceiver. Returns false if the sender is not owned by
126 // this transceiver.
127 bool RemoveReceiver(RtpReceiverInterface* receiver);
128
129 // Returns a vector of the receivers owned by this transceiver.
130 std::vector<
131 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
132 receivers() const {
133 return receivers_;
134 }
135
Steve Antonf9381f02017-12-14 10:23:57 -0800136 // Returns the backing object for the transceiver's Unified Plan sender.
137 rtc::scoped_refptr<RtpSenderInternal> sender_internal() const;
138
139 // Returns the backing object for the transceiver's Unified Plan receiver.
140 rtc::scoped_refptr<RtpReceiverInternal> receiver_internal() const;
141
Steve Antondcc3c022017-12-22 16:02:54 -0800142 // RtpTransceivers are not associated until they have a corresponding media
143 // section set in SetLocalDescription or SetRemoteDescription. Therefore,
144 // when setting a local offer we need a way to remember which transceiver was
145 // used to create which media section in the offer. Storing the mline index
146 // in CreateOffer is specified in JSEP to allow us to do that.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200147 absl::optional<size_t> mline_index() const { return mline_index_; }
148 void set_mline_index(absl::optional<size_t> mline_index) {
Steve Antondcc3c022017-12-22 16:02:54 -0800149 mline_index_ = mline_index;
150 }
151
152 // Sets the MID for this transceiver. If the MID is not null, then the
153 // transceiver is considered "associated" with the media section that has the
154 // same MID.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200155 void set_mid(const absl::optional<std::string>& mid) { mid_ = mid; }
Steve Antondcc3c022017-12-22 16:02:54 -0800156
157 // Sets the intended direction for this transceiver. Intended to be used
158 // internally over SetDirection since this does not trigger a negotiation
159 // needed callback.
160 void set_direction(RtpTransceiverDirection direction) {
161 direction_ = direction;
162 }
163
164 // Sets the current direction for this transceiver as negotiated in an offer/
165 // answer exchange. The current direction is null before an answer with this
166 // transceiver has been set.
167 void set_current_direction(RtpTransceiverDirection direction);
168
Steve Anton0f5400a2018-07-17 14:25:36 -0700169 // Sets the fired direction for this transceiver. The fired direction is null
170 // until SetRemoteDescription is called or an answer is set (either local or
171 // remote).
172 void set_fired_direction(RtpTransceiverDirection direction);
173
Steve Antonf9381f02017-12-14 10:23:57 -0800174 // According to JSEP rules for SetRemoteDescription, RtpTransceivers can be
175 // reused only if they were added by AddTrack.
176 void set_created_by_addtrack(bool created_by_addtrack) {
177 created_by_addtrack_ = created_by_addtrack;
178 }
Eldar Rello353a7182019-11-25 18:49:44 +0200179 // If AddTrack has been called then transceiver can't be removed during
180 // rollback.
181 void set_reused_for_addtrack(bool reused_for_addtrack) {
182 reused_for_addtrack_ = reused_for_addtrack;
183 }
184
Steve Antonf9381f02017-12-14 10:23:57 -0800185 bool created_by_addtrack() const { return created_by_addtrack_; }
186
Eldar Rello353a7182019-11-25 18:49:44 +0200187 bool reused_for_addtrack() const { return reused_for_addtrack_; }
188
Steve Antonf9381f02017-12-14 10:23:57 -0800189 // Returns true if this transceiver has ever had the current direction set to
190 // sendonly or sendrecv.
191 bool has_ever_been_used_to_send() const {
192 return has_ever_been_used_to_send_;
193 }
194
Harald Alvestrand6060df52020-08-11 09:54:02 +0200195 // Informs the transceiver that its owning
196 // PeerConnection is closed.
197 void SetPeerConnectionClosed();
198
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000199 // Executes the "stop the RTCRtpTransceiver" procedure from
200 // the webrtc-pc specification, described under the stop() method.
201 void StopTransceiverProcedure();
202
Steve Anton52d86772018-02-20 15:48:12 -0800203 // Fired when the RtpTransceiver state changes such that negotiation is now
204 // needed (e.g., in response to a direction change).
Harald Alvestrand280054f2020-11-10 13:12:53 +0000205 // sigslot::signal0<> SignalNegotiationNeeded;
Steve Anton52d86772018-02-20 15:48:12 -0800206
Steve Anton6e634bf2017-11-13 10:44:53 -0800207 // RtpTransceiverInterface implementation.
Steve Anton69470252018-02-09 11:43:08 -0800208 cricket::MediaType media_type() const override;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200209 absl::optional<std::string> mid() const override;
Steve Anton6e634bf2017-11-13 10:44:53 -0800210 rtc::scoped_refptr<RtpSenderInterface> sender() const override;
211 rtc::scoped_refptr<RtpReceiverInterface> receiver() const override;
212 bool stopped() const override;
Harald Alvestrand6060df52020-08-11 09:54:02 +0200213 bool stopping() const override;
Steve Anton6e634bf2017-11-13 10:44:53 -0800214 RtpTransceiverDirection direction() const override;
Harald Alvestrand6060df52020-08-11 09:54:02 +0200215 RTCError SetDirectionWithError(
216 RtpTransceiverDirection new_direction) override;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200217 absl::optional<RtpTransceiverDirection> current_direction() const override;
Steve Anton0f5400a2018-07-17 14:25:36 -0700218 absl::optional<RtpTransceiverDirection> fired_direction() const override;
Harald Alvestrand6060df52020-08-11 09:54:02 +0200219 RTCError StopStandard() override;
220 void StopInternal() override;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200221 RTCError SetCodecPreferences(
222 rtc::ArrayView<RtpCodecCapability> codecs) override;
223 std::vector<RtpCodecCapability> codec_preferences() const override {
224 return codec_preferences_;
225 }
Markus Handell0357b3e2020-03-16 13:40:51 +0100226 std::vector<RtpHeaderExtensionCapability> HeaderExtensionsToOffer()
227 const override;
Markus Handell5932fe12020-12-17 22:19:40 +0100228 std::vector<RtpHeaderExtensionCapability> HeaderExtensionsNegotiated()
229 const override;
Markus Handell755c65d2020-06-24 01:06:10 +0200230 RTCError SetOfferedRtpHeaderExtensions(
231 rtc::ArrayView<const RtpHeaderExtensionCapability>
232 header_extensions_to_offer) override;
Steve Anton6e634bf2017-11-13 10:44:53 -0800233
234 private:
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800235 void OnFirstPacketReceived(cricket::ChannelInterface* channel);
Harald Alvestrand6060df52020-08-11 09:54:02 +0200236 void StopSendingAndReceiving();
Steve Anton60776752018-01-10 11:51:34 -0800237
Harald Alvestrand6060df52020-08-11 09:54:02 +0200238 // Enforce that this object is created, used and destroyed on one thread.
239 const TaskQueueBase* thread_;
Steve Anton6e634bf2017-11-13 10:44:53 -0800240 const bool unified_plan_;
241 const cricket::MediaType media_type_;
242 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
243 senders_;
244 std::vector<
245 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
246 receivers_;
247
248 bool stopped_ = false;
Harald Alvestrand6060df52020-08-11 09:54:02 +0200249 bool stopping_ RTC_GUARDED_BY(thread_) = false;
250 bool is_pc_closed_ = false;
Steve Anton6e634bf2017-11-13 10:44:53 -0800251 RtpTransceiverDirection direction_ = RtpTransceiverDirection::kInactive;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200252 absl::optional<RtpTransceiverDirection> current_direction_;
Steve Anton0f5400a2018-07-17 14:25:36 -0700253 absl::optional<RtpTransceiverDirection> fired_direction_;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200254 absl::optional<std::string> mid_;
255 absl::optional<size_t> mline_index_;
Steve Antonf9381f02017-12-14 10:23:57 -0800256 bool created_by_addtrack_ = false;
Eldar Rello353a7182019-11-25 18:49:44 +0200257 bool reused_for_addtrack_ = false;
Steve Antonf9381f02017-12-14 10:23:57 -0800258 bool has_ever_been_used_to_send_ = false;
Steve Anton6e634bf2017-11-13 10:44:53 -0800259
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800260 cricket::ChannelInterface* channel_ = nullptr;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200261 cricket::ChannelManager* channel_manager_ = nullptr;
262 std::vector<RtpCodecCapability> codec_preferences_;
Markus Handell755c65d2020-06-24 01:06:10 +0200263 std::vector<RtpHeaderExtensionCapability> header_extensions_to_offer_;
Harald Alvestrand280054f2020-11-10 13:12:53 +0000264 const std::function<void()> on_negotiation_needed_;
Steve Anton6e634bf2017-11-13 10:44:53 -0800265};
266
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +0100267BEGIN_PRIMARY_PROXY_MAP(RtpTransceiver)
Harald Alvestrand5761e7b2021-01-29 14:45:08 +0000268
Mirko Bonadei9d9b8de2021-02-26 09:51:26 +0100269PROXY_PRIMARY_THREAD_DESTRUCTOR()
Tomas Gunnarssonfc83cdc2020-09-10 13:04:50 +0200270BYPASS_PROXY_CONSTMETHOD0(cricket::MediaType, media_type)
Nico Weber22f99252019-02-20 10:13:16 -0500271PROXY_CONSTMETHOD0(absl::optional<std::string>, mid)
272PROXY_CONSTMETHOD0(rtc::scoped_refptr<RtpSenderInterface>, sender)
273PROXY_CONSTMETHOD0(rtc::scoped_refptr<RtpReceiverInterface>, receiver)
274PROXY_CONSTMETHOD0(bool, stopped)
Harald Alvestrand6060df52020-08-11 09:54:02 +0200275PROXY_CONSTMETHOD0(bool, stopping)
Nico Weber22f99252019-02-20 10:13:16 -0500276PROXY_CONSTMETHOD0(RtpTransceiverDirection, direction)
Harald Alvestrand6060df52020-08-11 09:54:02 +0200277PROXY_METHOD1(webrtc::RTCError, SetDirectionWithError, RtpTransceiverDirection)
Nico Weber22f99252019-02-20 10:13:16 -0500278PROXY_CONSTMETHOD0(absl::optional<RtpTransceiverDirection>, current_direction)
279PROXY_CONSTMETHOD0(absl::optional<RtpTransceiverDirection>, fired_direction)
Harald Alvestrand6060df52020-08-11 09:54:02 +0200280PROXY_METHOD0(webrtc::RTCError, StopStandard)
281PROXY_METHOD0(void, StopInternal)
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200282PROXY_METHOD1(webrtc::RTCError,
283 SetCodecPreferences,
284 rtc::ArrayView<RtpCodecCapability>)
285PROXY_CONSTMETHOD0(std::vector<RtpCodecCapability>, codec_preferences)
Markus Handell0357b3e2020-03-16 13:40:51 +0100286PROXY_CONSTMETHOD0(std::vector<RtpHeaderExtensionCapability>,
287 HeaderExtensionsToOffer)
Markus Handell5932fe12020-12-17 22:19:40 +0100288PROXY_CONSTMETHOD0(std::vector<RtpHeaderExtensionCapability>,
289 HeaderExtensionsNegotiated)
Markus Handell755c65d2020-06-24 01:06:10 +0200290PROXY_METHOD1(webrtc::RTCError,
291 SetOfferedRtpHeaderExtensions,
292 rtc::ArrayView<const RtpHeaderExtensionCapability>)
Nico Weber22f99252019-02-20 10:13:16 -0500293END_PROXY_MAP()
Steve Anton6e634bf2017-11-13 10:44:53 -0800294
295} // namespace webrtc
296
Steve Anton10542f22019-01-11 09:11:00 -0800297#endif // PC_RTP_TRANSCEIVER_H_