2 * Copyright 2017 The WebRTC project authors. All Rights Reserved.
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.
11 #ifndef PC_RTP_TRANSCEIVER_H_
12 #define PC_RTP_TRANSCEIVER_H_
21 #include "absl/strings/string_view.h"
22 #include "absl/types/optional.h"
23 #include "api/array_view.h"
24 #include "api/audio_options.h"
25 #include "api/crypto/crypto_options.h"
27 #include "api/media_types.h"
28 #include "api/rtc_error.h"
29 #include "api/rtp_parameters.h"
30 #include "api/rtp_receiver_interface.h"
31 #include "api/rtp_sender_interface.h"
32 #include "api/rtp_transceiver_direction.h"
33 #include "api/rtp_transceiver_interface.h"
34 #include "api/scoped_refptr.h"
35 #include "api/task_queue/pending_task_safety_flag.h"
36 #include "api/task_queue/task_queue_base.h"
37 #include "api/video/video_bitrate_allocator_factory.h"
38 #include "media/base/media_channel.h"
39 #include "media/base/media_config.h"
40 #include "media/base/media_engine.h"
41 #include "pc/channel_interface.h"
42 #include "pc/connection_context.h"
44 #include "pc/rtp_receiver.h"
45 #include "pc/rtp_receiver_proxy.h"
46 #include "pc/rtp_sender.h"
47 #include "pc/rtp_sender_proxy.h"
48 #include "pc/rtp_transport_internal.h"
49 #include "pc/session_description.h"
50 #include "rtc_base/thread_annotations.h"
53 class MediaEngineInterface
;
58 class PeerConnectionSdpMethods
;
60 // Implementation of the public RtpTransceiverInterface.
62 // The RtpTransceiverInterface is only intended to be used with a PeerConnection
63 // that enables Unified Plan SDP. Thus, the methods that only need to implement
64 // public API features and are not used internally can assume exactly one sender
67 // Since the RtpTransceiver is used internally by PeerConnection for tracking
68 // RtpSenders, RtpReceivers, and BaseChannels, and PeerConnection needs to be
69 // backwards compatible with Plan B SDP, this implementation is more flexible
70 // than that required by the WebRTC specification.
72 // With Plan B SDP, an RtpTransceiver can have any number of senders and
73 // receivers which map to a=ssrc lines in the m= section.
74 // With Unified Plan SDP, an RtpTransceiver will have exactly one sender and one
75 // receiver which are encapsulated by the m= section.
77 // This class manages the RtpSenders, RtpReceivers, and BaseChannel associated
78 // with this m= section. Since the transceiver, senders, and receivers are
79 // reference counted and can be referenced from JavaScript (in Chromium), these
80 // objects must be ready to live for an arbitrary amount of time. The
81 // BaseChannel is not reference counted, so
82 // the PeerConnection must take care of creating/deleting the BaseChannel.
84 // The RtpTransceiver is specialized to either audio or video according to the
85 // MediaType specified in the constructor. Audio RtpTransceivers will have
86 // AudioRtpSenders, AudioRtpReceivers, and a VoiceChannel. Video RtpTransceivers
87 // will have VideoRtpSenders, VideoRtpReceivers, and a VideoChannel.
88 class RtpTransceiver
: public RtpTransceiverInterface
{
90 // Construct a Plan B-style RtpTransceiver with no senders, receivers, or
92 // `media_type` specifies the type of RtpTransceiver (and, by transitivity,
93 // the type of senders, receivers, and channel). Can either by audio or video.
94 RtpTransceiver(cricket::MediaType media_type
, ConnectionContext
* context
);
95 // Construct a Unified Plan-style RtpTransceiver with the given sender and
96 // receiver. The media type will be derived from the media types of the sender
97 // and receiver. The sender and receiver should have the same media type.
98 // `HeaderExtensionsToNegotiate` is used for initializing the return value of
99 // HeaderExtensionsToNegotiate().
101 rtc::scoped_refptr
<RtpSenderProxyWithInternal
<RtpSenderInternal
>> sender
,
102 rtc::scoped_refptr
<RtpReceiverProxyWithInternal
<RtpReceiverInternal
>>
104 ConnectionContext
* context
,
105 std::vector
<RtpHeaderExtensionCapability
> HeaderExtensionsToNegotiate
,
106 std::function
<void()> on_negotiation_needed
);
107 ~RtpTransceiver() override
;
109 // Not copyable or movable.
110 RtpTransceiver(const RtpTransceiver
&) = delete;
111 RtpTransceiver
& operator=(const RtpTransceiver
&) = delete;
112 RtpTransceiver(RtpTransceiver
&&) = delete;
113 RtpTransceiver
& operator=(RtpTransceiver
&&) = delete;
115 // Returns the Voice/VideoChannel set for this transceiver. May be null if
116 // the transceiver is not in the currently set local/remote description.
117 cricket::ChannelInterface
* channel() const { return channel_
.get(); }
119 // Creates the Voice/VideoChannel and sets it.
120 RTCError
CreateChannel(
121 absl::string_view mid
,
123 const cricket::MediaConfig
& media_config
,
125 CryptoOptions crypto_options
,
126 const cricket::AudioOptions
& audio_options
,
127 const cricket::VideoOptions
& video_options
,
128 VideoBitrateAllocatorFactory
* video_bitrate_allocator_factory
,
129 std::function
<RtpTransportInternal
*(absl::string_view
)> transport_lookup
);
131 // Sets the Voice/VideoChannel. The caller must pass in the correct channel
132 // implementation based on the type of the transceiver. The call must
133 // furthermore be made on the signaling thread.
135 // `channel`: The channel instance to be associated with the transceiver.
136 // This must be a valid pointer.
137 // The state of the object
138 // is expected to be newly constructed and not initalized for network
139 // activity (see next parameter for more).
141 // The transceiver takes ownership of `channel`.
143 // `transport_lookup`: This
144 // callback function will be used to look up the `RtpTransport` object
145 // to associate with the channel via `BaseChannel::SetRtpTransport`.
146 // The lookup function will be called on the network thread, synchronously
147 // during the call to `SetChannel`. This means that the caller of
148 // `SetChannel()` may provide a callback function that references state
149 // that exists within the calling scope of SetChannel (e.g. a variable
151 // The reason for this design is to limit the number of times we jump
152 // synchronously to the network thread from the signaling thread.
153 // The callback allows us to combine the transport lookup with network
154 // state initialization of the channel object.
155 // ClearChannel() must be used before calling SetChannel() again.
156 void SetChannel(std::unique_ptr
<cricket::ChannelInterface
> channel
,
157 std::function
<RtpTransportInternal
*(const std::string
&)>
160 // Clear the association between the transceiver and the channel.
163 // Adds an RtpSender of the appropriate type to be owned by this transceiver.
166 rtc::scoped_refptr
<RtpSenderProxyWithInternal
<RtpSenderInternal
>> sender
);
168 // Removes the given RtpSender. Returns false if the sender is not owned by
170 bool RemoveSender(RtpSenderInterface
* sender
);
172 // Returns a vector of the senders owned by this transceiver.
173 std::vector
<rtc::scoped_refptr
<RtpSenderProxyWithInternal
<RtpSenderInternal
>>>
178 // Adds an RtpReceiver of the appropriate type to be owned by this
179 // transceiver. Must not be null.
181 rtc::scoped_refptr
<RtpReceiverProxyWithInternal
<RtpReceiverInternal
>>
184 // Removes the given RtpReceiver. Returns false if the sender is not owned by
186 bool RemoveReceiver(RtpReceiverInterface
* receiver
);
188 // Returns a vector of the receivers owned by this transceiver.
190 rtc::scoped_refptr
<RtpReceiverProxyWithInternal
<RtpReceiverInternal
>>>
195 // Returns the backing object for the transceiver's Unified Plan sender.
196 rtc::scoped_refptr
<RtpSenderInternal
> sender_internal() const;
198 // Returns the backing object for the transceiver's Unified Plan receiver.
199 rtc::scoped_refptr
<RtpReceiverInternal
> receiver_internal() const;
201 // RtpTransceivers are not associated until they have a corresponding media
202 // section set in SetLocalDescription or SetRemoteDescription. Therefore,
203 // when setting a local offer we need a way to remember which transceiver was
204 // used to create which media section in the offer. Storing the mline index
205 // in CreateOffer is specified in JSEP to allow us to do that.
206 absl::optional
<size_t> mline_index() const { return mline_index_
; }
207 void set_mline_index(absl::optional
<size_t> mline_index
) {
208 mline_index_
= mline_index
;
211 // Sets the MID for this transceiver. If the MID is not null, then the
212 // transceiver is considered "associated" with the media section that has the
214 void set_mid(const absl::optional
<std::string
>& mid
) { mid_
= mid
; }
216 // Sets the intended direction for this transceiver. Intended to be used
217 // internally over SetDirection since this does not trigger a negotiation
219 void set_direction(RtpTransceiverDirection direction
) {
220 direction_
= direction
;
223 // Sets the current direction for this transceiver as negotiated in an offer/
224 // answer exchange. The current direction is null before an answer with this
225 // transceiver has been set.
226 void set_current_direction(RtpTransceiverDirection direction
);
228 // Sets the fired direction for this transceiver. The fired direction is null
229 // until SetRemoteDescription is called or an answer is set (either local or
230 // remote) after which the only valid reason to go back to null is rollback.
231 void set_fired_direction(absl::optional
<RtpTransceiverDirection
> direction
);
233 // According to JSEP rules for SetRemoteDescription, RtpTransceivers can be
234 // reused only if they were added by AddTrack.
235 void set_created_by_addtrack(bool created_by_addtrack
) {
236 created_by_addtrack_
= created_by_addtrack
;
238 // If AddTrack has been called then transceiver can't be removed during
240 void set_reused_for_addtrack(bool reused_for_addtrack
) {
241 reused_for_addtrack_
= reused_for_addtrack
;
244 bool created_by_addtrack() const { return created_by_addtrack_
; }
246 bool reused_for_addtrack() const { return reused_for_addtrack_
; }
248 // Returns true if this transceiver has ever had the current direction set to
249 // sendonly or sendrecv.
250 bool has_ever_been_used_to_send() const {
251 return has_ever_been_used_to_send_
;
254 // Informs the transceiver that its owning
255 // PeerConnection is closed.
256 void SetPeerConnectionClosed();
258 // Executes the "stop the RTCRtpTransceiver" procedure from
259 // the webrtc-pc specification, described under the stop() method.
260 void StopTransceiverProcedure();
262 // RtpTransceiverInterface implementation.
263 cricket::MediaType
media_type() const override
;
264 absl::optional
<std::string
> mid() const override
;
265 rtc::scoped_refptr
<RtpSenderInterface
> sender() const override
;
266 rtc::scoped_refptr
<RtpReceiverInterface
> receiver() const override
;
267 bool stopped() const override
;
268 bool stopping() const override
;
269 RtpTransceiverDirection
direction() const override
;
270 RTCError
SetDirectionWithError(
271 RtpTransceiverDirection new_direction
) override
;
272 absl::optional
<RtpTransceiverDirection
> current_direction() const override
;
273 absl::optional
<RtpTransceiverDirection
> fired_direction() const override
;
274 RTCError
StopStandard() override
;
275 void StopInternal() override
;
276 RTCError
SetCodecPreferences(
277 rtc::ArrayView
<RtpCodecCapability
> codecs
) override
;
278 std::vector
<RtpCodecCapability
> codec_preferences() const override
{
279 return codec_preferences_
;
281 std::vector
<RtpHeaderExtensionCapability
> GetHeaderExtensionsToNegotiate()
283 std::vector
<RtpHeaderExtensionCapability
> GetNegotiatedHeaderExtensions()
285 RTCError
SetHeaderExtensionsToNegotiate(
286 rtc::ArrayView
<const RtpHeaderExtensionCapability
> header_extensions
)
289 // Called on the signaling thread when the local or remote content description
290 // is updated. Used to update the negotiated header extensions.
291 // TODO(tommi): The implementation of this method is currently very simple and
292 // only used for updating the negotiated headers. However, we're planning to
293 // move all the updates done on the channel from the transceiver into this
294 // method. This will happen with the ownership of the channel object being
295 // moved into the transceiver.
296 void OnNegotiationUpdate(SdpType sdp_type
,
297 const cricket::MediaContentDescription
* content
);
300 cricket::MediaEngineInterface
* media_engine() const {
301 return context_
->media_engine();
303 ConnectionContext
* context() const { return context_
; }
304 void OnFirstPacketReceived();
305 void StopSendingAndReceiving();
306 // Delete a channel, and ensure that references to its media channel
307 // are updated before deleting it.
308 void PushNewMediaChannelAndDeleteChannel(
309 std::unique_ptr
<cricket::ChannelInterface
> channel_to_delete
);
311 // Enforce that this object is created, used and destroyed on one thread.
312 TaskQueueBase
* const thread_
;
313 const bool unified_plan_
;
314 const cricket::MediaType media_type_
;
315 rtc::scoped_refptr
<PendingTaskSafetyFlag
> signaling_thread_safety_
;
316 std::vector
<rtc::scoped_refptr
<RtpSenderProxyWithInternal
<RtpSenderInternal
>>>
319 rtc::scoped_refptr
<RtpReceiverProxyWithInternal
<RtpReceiverInternal
>>>
322 bool stopped_
RTC_GUARDED_BY(thread_
) = false;
323 bool stopping_
RTC_GUARDED_BY(thread_
) = false;
324 bool is_pc_closed_
= false;
325 RtpTransceiverDirection direction_
= RtpTransceiverDirection::kInactive
;
326 absl::optional
<RtpTransceiverDirection
> current_direction_
;
327 absl::optional
<RtpTransceiverDirection
> fired_direction_
;
328 absl::optional
<std::string
> mid_
;
329 absl::optional
<size_t> mline_index_
;
330 bool created_by_addtrack_
= false;
331 bool reused_for_addtrack_
= false;
332 bool has_ever_been_used_to_send_
= false;
334 // Accessed on both thread_ and the network thread. Considered safe
335 // because all access on the network thread is within an invoke()
337 std::unique_ptr
<cricket::ChannelInterface
> channel_
= nullptr;
338 ConnectionContext
* const context_
;
339 std::vector
<RtpCodecCapability
> codec_preferences_
;
340 std::vector
<RtpHeaderExtensionCapability
> header_extensions_to_negotiate_
;
342 // `negotiated_header_extensions_` is read and written to on the signaling
343 // thread from the SdpOfferAnswerHandler class (e.g.
344 // PushdownMediaDescription().
345 cricket::RtpHeaderExtensions negotiated_header_extensions_
346 RTC_GUARDED_BY(thread_
);
348 const std::function
<void()> on_negotiation_needed_
;
351 BEGIN_PRIMARY_PROXY_MAP(RtpTransceiver
)
353 PROXY_PRIMARY_THREAD_DESTRUCTOR()
354 BYPASS_PROXY_CONSTMETHOD0(cricket::MediaType
, media_type
)
355 PROXY_CONSTMETHOD0(absl::optional
<std::string
>, mid
)
356 PROXY_CONSTMETHOD0(rtc::scoped_refptr
<RtpSenderInterface
>, sender
)
357 PROXY_CONSTMETHOD0(rtc::scoped_refptr
<RtpReceiverInterface
>, receiver
)
358 PROXY_CONSTMETHOD0(bool, stopped
)
359 PROXY_CONSTMETHOD0(bool, stopping
)
360 PROXY_CONSTMETHOD0(RtpTransceiverDirection
, direction
)
361 PROXY_METHOD1(RTCError
, SetDirectionWithError
, RtpTransceiverDirection
)
362 PROXY_CONSTMETHOD0(absl::optional
<RtpTransceiverDirection
>, current_direction
)
363 PROXY_CONSTMETHOD0(absl::optional
<RtpTransceiverDirection
>, fired_direction
)
364 PROXY_METHOD0(RTCError
, StopStandard
)
365 PROXY_METHOD0(void, StopInternal
)
366 PROXY_METHOD1(RTCError
, SetCodecPreferences
, rtc::ArrayView
<RtpCodecCapability
>)
367 PROXY_CONSTMETHOD0(std::vector
<RtpCodecCapability
>, codec_preferences
)
368 PROXY_CONSTMETHOD0(std::vector
<RtpHeaderExtensionCapability
>,
369 GetHeaderExtensionsToNegotiate
)
370 PROXY_CONSTMETHOD0(std::vector
<RtpHeaderExtensionCapability
>,
371 GetNegotiatedHeaderExtensions
)
372 PROXY_METHOD1(RTCError
,
373 SetHeaderExtensionsToNegotiate
,
374 rtc::ArrayView
<const RtpHeaderExtensionCapability
>)
375 END_PROXY_MAP(RtpTransceiver
)
377 } // namespace webrtc
379 #endif // PC_RTP_TRANSCEIVER_H_