blob: 4d9716c89b810a54e46fb1ebfe21a143d45bb38b [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
14#include <string>
15#include <vector>
16
Steve Anton10542f22019-01-11 09:11:00 -080017#include "api/rtp_transceiver_interface.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010018#include "pc/channel_interface.h"
Florent Castelli2d9d82e2019-04-23 19:25:51 +020019#include "pc/channel_manager.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "pc/rtp_receiver.h"
21#include "pc/rtp_sender.h"
Steve Anton6e634bf2017-11-13 10:44:53 -080022
23namespace webrtc {
24
25// Implementation of the public RtpTransceiverInterface.
26//
27// The RtpTransceiverInterface is only intended to be used with a PeerConnection
28// that enables Unified Plan SDP. Thus, the methods that only need to implement
29// public API features and are not used internally can assume exactly one sender
30// and receiver.
31//
32// Since the RtpTransceiver is used internally by PeerConnection for tracking
33// RtpSenders, RtpReceivers, and BaseChannels, and PeerConnection needs to be
34// backwards compatible with Plan B SDP, this implementation is more flexible
35// than that required by the WebRTC specification.
36//
37// With Plan B SDP, an RtpTransceiver can have any number of senders and
38// receivers which map to a=ssrc lines in the m= section.
39// With Unified Plan SDP, an RtpTransceiver will have exactly one sender and one
40// receiver which are encapsulated by the m= section.
41//
42// This class manages the RtpSenders, RtpReceivers, and BaseChannel associated
43// with this m= section. Since the transceiver, senders, and receivers are
44// reference counted and can be referenced from JavaScript (in Chromium), these
45// objects must be ready to live for an arbitrary amount of time. The
46// BaseChannel is not reference counted and is owned by the ChannelManager, so
47// the PeerConnection must take care of creating/deleting the BaseChannel and
48// setting the channel reference in the transceiver to null when it has been
49// deleted.
50//
51// The RtpTransceiver is specialized to either audio or video according to the
52// MediaType specified in the constructor. Audio RtpTransceivers will have
53// AudioRtpSenders, AudioRtpReceivers, and a VoiceChannel. Video RtpTransceivers
54// will have VideoRtpSenders, VideoRtpReceivers, and a VideoChannel.
55class RtpTransceiver final
Steve Anton60776752018-01-10 11:51:34 -080056 : public rtc::RefCountedObject<RtpTransceiverInterface>,
57 public sigslot::has_slots<> {
Steve Anton6e634bf2017-11-13 10:44:53 -080058 public:
Steve Anton79e79602017-11-20 10:25:56 -080059 // Construct a Plan B-style RtpTransceiver with no senders, receivers, or
60 // channel set.
Steve Anton6e634bf2017-11-13 10:44:53 -080061 // |media_type| specifies the type of RtpTransceiver (and, by transitivity,
62 // the type of senders, receivers, and channel). Can either by audio or video.
63 explicit RtpTransceiver(cricket::MediaType media_type);
Steve Anton79e79602017-11-20 10:25:56 -080064 // Construct a Unified Plan-style RtpTransceiver with the given sender and
65 // receiver. The media type will be derived from the media types of the sender
66 // and receiver. The sender and receiver should have the same media type.
Markus Handell0357b3e2020-03-16 13:40:51 +010067 // |HeaderExtensionsToOffer| is used for initializing the return value of
68 // HeaderExtensionsToOffer().
Steve Anton79e79602017-11-20 10:25:56 -080069 RtpTransceiver(
70 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
71 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
Florent Castelli2d9d82e2019-04-23 19:25:51 +020072 receiver,
Markus Handell0357b3e2020-03-16 13:40:51 +010073 cricket::ChannelManager* channel_manager,
Harald Alvestrand280054f2020-11-10 13:12:53 +000074 std::vector<RtpHeaderExtensionCapability> HeaderExtensionsToOffer,
75 std::function<void()> on_negotiation_needed);
Steve Anton6e634bf2017-11-13 10:44:53 -080076 ~RtpTransceiver() override;
77
Steve Anton6e634bf2017-11-13 10:44:53 -080078 // Returns the Voice/VideoChannel set for this transceiver. May be null if
79 // the transceiver is not in the currently set local/remote description.
Amit Hilbuchdd9390c2018-11-13 16:26:05 -080080 cricket::ChannelInterface* channel() const { return channel_; }
Steve Anton6e634bf2017-11-13 10:44:53 -080081
82 // Sets the Voice/VideoChannel. The caller must pass in the correct channel
83 // implementation based on the type of the transceiver.
Amit Hilbuchdd9390c2018-11-13 16:26:05 -080084 void SetChannel(cricket::ChannelInterface* channel);
Steve Anton6e634bf2017-11-13 10:44:53 -080085
86 // Adds an RtpSender of the appropriate type to be owned by this transceiver.
87 // Must not be null.
88 void AddSender(
89 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender);
90
91 // Removes the given RtpSender. Returns false if the sender is not owned by
92 // this transceiver.
93 bool RemoveSender(RtpSenderInterface* sender);
94
95 // Returns a vector of the senders owned by this transceiver.
96 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
97 senders() const {
98 return senders_;
99 }
100
101 // Adds an RtpReceiver of the appropriate type to be owned by this
102 // transceiver. Must not be null.
103 void AddReceiver(
104 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
105 receiver);
106
107 // Removes the given RtpReceiver. Returns false if the sender is not owned by
108 // this transceiver.
109 bool RemoveReceiver(RtpReceiverInterface* receiver);
110
111 // Returns a vector of the receivers owned by this transceiver.
112 std::vector<
113 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
114 receivers() const {
115 return receivers_;
116 }
117
Steve Antonf9381f02017-12-14 10:23:57 -0800118 // Returns the backing object for the transceiver's Unified Plan sender.
119 rtc::scoped_refptr<RtpSenderInternal> sender_internal() const;
120
121 // Returns the backing object for the transceiver's Unified Plan receiver.
122 rtc::scoped_refptr<RtpReceiverInternal> receiver_internal() const;
123
Steve Antondcc3c022017-12-22 16:02:54 -0800124 // RtpTransceivers are not associated until they have a corresponding media
125 // section set in SetLocalDescription or SetRemoteDescription. Therefore,
126 // when setting a local offer we need a way to remember which transceiver was
127 // used to create which media section in the offer. Storing the mline index
128 // in CreateOffer is specified in JSEP to allow us to do that.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200129 absl::optional<size_t> mline_index() const { return mline_index_; }
130 void set_mline_index(absl::optional<size_t> mline_index) {
Steve Antondcc3c022017-12-22 16:02:54 -0800131 mline_index_ = mline_index;
132 }
133
134 // Sets the MID for this transceiver. If the MID is not null, then the
135 // transceiver is considered "associated" with the media section that has the
136 // same MID.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200137 void set_mid(const absl::optional<std::string>& mid) { mid_ = mid; }
Steve Antondcc3c022017-12-22 16:02:54 -0800138
139 // Sets the intended direction for this transceiver. Intended to be used
140 // internally over SetDirection since this does not trigger a negotiation
141 // needed callback.
142 void set_direction(RtpTransceiverDirection direction) {
143 direction_ = direction;
144 }
145
146 // Sets the current direction for this transceiver as negotiated in an offer/
147 // answer exchange. The current direction is null before an answer with this
148 // transceiver has been set.
149 void set_current_direction(RtpTransceiverDirection direction);
150
Steve Anton0f5400a2018-07-17 14:25:36 -0700151 // Sets the fired direction for this transceiver. The fired direction is null
152 // until SetRemoteDescription is called or an answer is set (either local or
153 // remote).
154 void set_fired_direction(RtpTransceiverDirection direction);
155
Steve Antonf9381f02017-12-14 10:23:57 -0800156 // According to JSEP rules for SetRemoteDescription, RtpTransceivers can be
157 // reused only if they were added by AddTrack.
158 void set_created_by_addtrack(bool created_by_addtrack) {
159 created_by_addtrack_ = created_by_addtrack;
160 }
Eldar Rello353a7182019-11-25 18:49:44 +0200161 // If AddTrack has been called then transceiver can't be removed during
162 // rollback.
163 void set_reused_for_addtrack(bool reused_for_addtrack) {
164 reused_for_addtrack_ = reused_for_addtrack;
165 }
166
Steve Antonf9381f02017-12-14 10:23:57 -0800167 bool created_by_addtrack() const { return created_by_addtrack_; }
168
Eldar Rello353a7182019-11-25 18:49:44 +0200169 bool reused_for_addtrack() const { return reused_for_addtrack_; }
170
Steve Antonf9381f02017-12-14 10:23:57 -0800171 // Returns true if this transceiver has ever had the current direction set to
172 // sendonly or sendrecv.
173 bool has_ever_been_used_to_send() const {
174 return has_ever_been_used_to_send_;
175 }
176
Harald Alvestrand6060df52020-08-11 09:54:02 +0200177 // Informs the transceiver that its owning
178 // PeerConnection is closed.
179 void SetPeerConnectionClosed();
180
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000181 // Executes the "stop the RTCRtpTransceiver" procedure from
182 // the webrtc-pc specification, described under the stop() method.
183 void StopTransceiverProcedure();
184
Steve Anton52d86772018-02-20 15:48:12 -0800185 // Fired when the RtpTransceiver state changes such that negotiation is now
186 // needed (e.g., in response to a direction change).
Harald Alvestrand280054f2020-11-10 13:12:53 +0000187 // sigslot::signal0<> SignalNegotiationNeeded;
Steve Anton52d86772018-02-20 15:48:12 -0800188
Steve Anton6e634bf2017-11-13 10:44:53 -0800189 // RtpTransceiverInterface implementation.
Steve Anton69470252018-02-09 11:43:08 -0800190 cricket::MediaType media_type() const override;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200191 absl::optional<std::string> mid() const override;
Steve Anton6e634bf2017-11-13 10:44:53 -0800192 rtc::scoped_refptr<RtpSenderInterface> sender() const override;
193 rtc::scoped_refptr<RtpReceiverInterface> receiver() const override;
194 bool stopped() const override;
Harald Alvestrand6060df52020-08-11 09:54:02 +0200195 bool stopping() const override;
Steve Anton6e634bf2017-11-13 10:44:53 -0800196 RtpTransceiverDirection direction() const override;
Harald Alvestrand6060df52020-08-11 09:54:02 +0200197 RTCError SetDirectionWithError(
198 RtpTransceiverDirection new_direction) override;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200199 absl::optional<RtpTransceiverDirection> current_direction() const override;
Steve Anton0f5400a2018-07-17 14:25:36 -0700200 absl::optional<RtpTransceiverDirection> fired_direction() const override;
Harald Alvestrand6060df52020-08-11 09:54:02 +0200201 RTCError StopStandard() override;
202 void StopInternal() override;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200203 RTCError SetCodecPreferences(
204 rtc::ArrayView<RtpCodecCapability> codecs) override;
205 std::vector<RtpCodecCapability> codec_preferences() const override {
206 return codec_preferences_;
207 }
Markus Handell0357b3e2020-03-16 13:40:51 +0100208 std::vector<RtpHeaderExtensionCapability> HeaderExtensionsToOffer()
209 const override;
Markus Handell755c65d2020-06-24 01:06:10 +0200210 RTCError SetOfferedRtpHeaderExtensions(
211 rtc::ArrayView<const RtpHeaderExtensionCapability>
212 header_extensions_to_offer) override;
Steve Anton6e634bf2017-11-13 10:44:53 -0800213
214 private:
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800215 void OnFirstPacketReceived(cricket::ChannelInterface* channel);
Harald Alvestrand6060df52020-08-11 09:54:02 +0200216 void StopSendingAndReceiving();
Steve Anton60776752018-01-10 11:51:34 -0800217
Harald Alvestrand6060df52020-08-11 09:54:02 +0200218 // Enforce that this object is created, used and destroyed on one thread.
219 const TaskQueueBase* thread_;
Steve Anton6e634bf2017-11-13 10:44:53 -0800220 const bool unified_plan_;
221 const cricket::MediaType media_type_;
222 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
223 senders_;
224 std::vector<
225 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
226 receivers_;
227
228 bool stopped_ = false;
Harald Alvestrand6060df52020-08-11 09:54:02 +0200229 bool stopping_ RTC_GUARDED_BY(thread_) = false;
230 bool is_pc_closed_ = false;
Steve Anton6e634bf2017-11-13 10:44:53 -0800231 RtpTransceiverDirection direction_ = RtpTransceiverDirection::kInactive;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200232 absl::optional<RtpTransceiverDirection> current_direction_;
Steve Anton0f5400a2018-07-17 14:25:36 -0700233 absl::optional<RtpTransceiverDirection> fired_direction_;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200234 absl::optional<std::string> mid_;
235 absl::optional<size_t> mline_index_;
Steve Antonf9381f02017-12-14 10:23:57 -0800236 bool created_by_addtrack_ = false;
Eldar Rello353a7182019-11-25 18:49:44 +0200237 bool reused_for_addtrack_ = false;
Steve Antonf9381f02017-12-14 10:23:57 -0800238 bool has_ever_been_used_to_send_ = false;
Steve Anton6e634bf2017-11-13 10:44:53 -0800239
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800240 cricket::ChannelInterface* channel_ = nullptr;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200241 cricket::ChannelManager* channel_manager_ = nullptr;
242 std::vector<RtpCodecCapability> codec_preferences_;
Markus Handell755c65d2020-06-24 01:06:10 +0200243 std::vector<RtpHeaderExtensionCapability> header_extensions_to_offer_;
Harald Alvestrand280054f2020-11-10 13:12:53 +0000244 const std::function<void()> on_negotiation_needed_;
Steve Anton6e634bf2017-11-13 10:44:53 -0800245};
246
247BEGIN_SIGNALING_PROXY_MAP(RtpTransceiver)
248PROXY_SIGNALING_THREAD_DESTRUCTOR()
Tomas Gunnarssonfc83cdc2020-09-10 13:04:50 +0200249BYPASS_PROXY_CONSTMETHOD0(cricket::MediaType, media_type)
Nico Weber22f99252019-02-20 10:13:16 -0500250PROXY_CONSTMETHOD0(absl::optional<std::string>, mid)
251PROXY_CONSTMETHOD0(rtc::scoped_refptr<RtpSenderInterface>, sender)
252PROXY_CONSTMETHOD0(rtc::scoped_refptr<RtpReceiverInterface>, receiver)
253PROXY_CONSTMETHOD0(bool, stopped)
Harald Alvestrand6060df52020-08-11 09:54:02 +0200254PROXY_CONSTMETHOD0(bool, stopping)
Nico Weber22f99252019-02-20 10:13:16 -0500255PROXY_CONSTMETHOD0(RtpTransceiverDirection, direction)
Harald Alvestrand6060df52020-08-11 09:54:02 +0200256PROXY_METHOD1(webrtc::RTCError, SetDirectionWithError, RtpTransceiverDirection)
Nico Weber22f99252019-02-20 10:13:16 -0500257PROXY_CONSTMETHOD0(absl::optional<RtpTransceiverDirection>, current_direction)
258PROXY_CONSTMETHOD0(absl::optional<RtpTransceiverDirection>, fired_direction)
Harald Alvestrand6060df52020-08-11 09:54:02 +0200259PROXY_METHOD0(webrtc::RTCError, StopStandard)
260PROXY_METHOD0(void, StopInternal)
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200261PROXY_METHOD1(webrtc::RTCError,
262 SetCodecPreferences,
263 rtc::ArrayView<RtpCodecCapability>)
264PROXY_CONSTMETHOD0(std::vector<RtpCodecCapability>, codec_preferences)
Markus Handell0357b3e2020-03-16 13:40:51 +0100265PROXY_CONSTMETHOD0(std::vector<RtpHeaderExtensionCapability>,
266 HeaderExtensionsToOffer)
Markus Handell755c65d2020-06-24 01:06:10 +0200267PROXY_METHOD1(webrtc::RTCError,
268 SetOfferedRtpHeaderExtensions,
269 rtc::ArrayView<const RtpHeaderExtensionCapability>)
Nico Weber22f99252019-02-20 10:13:16 -0500270END_PROXY_MAP()
Steve Anton6e634bf2017-11-13 10:44:53 -0800271
272} // namespace webrtc
273
Steve Anton10542f22019-01-11 09:11:00 -0800274#endif // PC_RTP_TRANSCEIVER_H_