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