1 /* 2 * Copyright 2020 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_TRANSMISSION_MANAGER_H_ 12 #define PC_RTP_TRANSMISSION_MANAGER_H_ 13 14 #include <stdint.h> 15 16 #include <functional> 17 #include <string> 18 #include <vector> 19 20 #include "api/media_stream_interface.h" 21 #include "api/media_types.h" 22 #include "api/peer_connection_interface.h" 23 #include "api/rtc_error.h" 24 #include "api/rtp_parameters.h" 25 #include "api/rtp_receiver_interface.h" 26 #include "api/rtp_sender_interface.h" 27 #include "api/scoped_refptr.h" 28 #include "api/sequence_checker.h" 29 #include "media/base/media_channel.h" 30 #include "pc/legacy_stats_collector_interface.h" 31 #include "pc/rtp_receiver.h" 32 #include "pc/rtp_receiver_proxy.h" 33 #include "pc/rtp_sender.h" 34 #include "pc/rtp_sender_proxy.h" 35 #include "pc/rtp_transceiver.h" 36 #include "pc/transceiver_list.h" 37 #include "pc/usage_pattern.h" 38 #include "rtc_base/third_party/sigslot/sigslot.h" 39 #include "rtc_base/thread.h" 40 #include "rtc_base/thread_annotations.h" 41 #include "rtc_base/weak_ptr.h" 42 43 namespace rtc { 44 class Thread; 45 } 46 47 namespace webrtc { 48 49 // This class contains information about 50 // an RTPSender, used for things like looking it up by SSRC. 51 struct RtpSenderInfo { RtpSenderInfoRtpSenderInfo52 RtpSenderInfo() : first_ssrc(0) {} RtpSenderInfoRtpSenderInfo53 RtpSenderInfo(const std::string& stream_id, 54 const std::string& sender_id, 55 uint32_t ssrc) 56 : stream_id(stream_id), sender_id(sender_id), first_ssrc(ssrc) {} 57 bool operator==(const RtpSenderInfo& other) { 58 return this->stream_id == other.stream_id && 59 this->sender_id == other.sender_id && 60 this->first_ssrc == other.first_ssrc; 61 } 62 std::string stream_id; 63 std::string sender_id; 64 // An RtpSender can have many SSRCs. The first one is used as a sort of ID 65 // for communicating with the lower layers. 66 uint32_t first_ssrc; 67 }; 68 69 // The RtpTransmissionManager class is responsible for managing the lifetime 70 // and relationships between objects of type RtpSender, RtpReceiver and 71 // RtpTransceiver. 72 class RtpTransmissionManager : public RtpSenderBase::SetStreamsObserver { 73 public: 74 RtpTransmissionManager(bool is_unified_plan, 75 ConnectionContext* context, 76 UsagePattern* usage_pattern, 77 PeerConnectionObserver* observer, 78 LegacyStatsCollectorInterface* legacy_stats, 79 std::function<void()> on_negotiation_needed); 80 81 // No move or copy permitted. 82 RtpTransmissionManager(const RtpTransmissionManager&) = delete; 83 RtpTransmissionManager& operator=(const RtpTransmissionManager&) = delete; 84 85 // Stop activity. In particular, don't call observer_ any more. 86 void Close(); 87 88 // RtpSenderBase::SetStreamsObserver override. 89 void OnSetStreams() override; 90 91 // Add a new track, creating transceiver if required. 92 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrack( 93 rtc::scoped_refptr<MediaStreamTrackInterface> track, 94 const std::vector<std::string>& stream_ids, 95 const std::vector<RtpEncodingParameters>* init_send_encodings); 96 97 // Create a new RTP sender. Does not associate with a transceiver. 98 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> 99 CreateSender(cricket::MediaType media_type, 100 const std::string& id, 101 rtc::scoped_refptr<MediaStreamTrackInterface> track, 102 const std::vector<std::string>& stream_ids, 103 const std::vector<RtpEncodingParameters>& send_encodings); 104 105 // Create a new RTP receiver. Does not associate with a transceiver. 106 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> 107 CreateReceiver(cricket::MediaType media_type, const std::string& receiver_id); 108 109 // Create a new RtpTransceiver of the given type and add it to the list of 110 // registered transceivers. 111 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> 112 CreateAndAddTransceiver( 113 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, 114 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> 115 receiver); 116 117 // Returns the first RtpTransceiver suitable for a newly added track, if such 118 // transceiver is available. 119 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> 120 FindFirstTransceiverForAddedTrack( 121 rtc::scoped_refptr<MediaStreamTrackInterface> track, 122 const std::vector<RtpEncodingParameters>* init_send_encodings); 123 124 // Returns the list of senders currently associated with some 125 // registered transceiver 126 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> 127 GetSendersInternal() const; 128 129 // Returns the list of receivers currently associated with a transceiver 130 std::vector< 131 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> 132 GetReceiversInternal() const; 133 134 // Plan B: Get the transceiver containing all audio senders and receivers 135 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> 136 GetAudioTransceiver() const; 137 // Plan B: Get the transceiver containing all video senders and receivers 138 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> 139 GetVideoTransceiver() const; 140 141 // Add an audio track, reusing or creating the sender. 142 void AddAudioTrack(AudioTrackInterface* track, MediaStreamInterface* stream); 143 // Plan B: Remove an audio track, removing the sender. 144 void RemoveAudioTrack(AudioTrackInterface* track, 145 MediaStreamInterface* stream); 146 // Add a video track, reusing or creating the sender. 147 void AddVideoTrack(VideoTrackInterface* track, MediaStreamInterface* stream); 148 // Plan B: Remove a video track, removing the sender. 149 void RemoveVideoTrack(VideoTrackInterface* track, 150 MediaStreamInterface* stream); 151 152 // Triggered when a remote sender has been seen for the first time in a remote 153 // session description. It creates a remote MediaStreamTrackInterface 154 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver. 155 void OnRemoteSenderAdded(const RtpSenderInfo& sender_info, 156 MediaStreamInterface* stream, 157 cricket::MediaType media_type); 158 159 // Triggered when a remote sender has been removed from a remote session 160 // description. It removes the remote sender with id `sender_id` from a remote 161 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver. 162 void OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, 163 MediaStreamInterface* stream, 164 cricket::MediaType media_type); 165 166 // Triggered when a local sender has been seen for the first time in a local 167 // session description. 168 // This method triggers CreateAudioSender or CreateVideoSender if the rtp 169 // streams in the local SessionDescription can be mapped to a MediaStreamTrack 170 // in a MediaStream in `local_streams_` 171 void OnLocalSenderAdded(const RtpSenderInfo& sender_info, 172 cricket::MediaType media_type); 173 174 // Triggered when a local sender has been removed from a local session 175 // description. 176 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream 177 // has been removed from the local SessionDescription and the stream can be 178 // mapped to a MediaStreamTrack in a MediaStream in `local_streams_`. 179 void OnLocalSenderRemoved(const RtpSenderInfo& sender_info, 180 cricket::MediaType media_type); 181 182 std::vector<RtpSenderInfo>* GetRemoteSenderInfos( 183 cricket::MediaType media_type); 184 std::vector<RtpSenderInfo>* GetLocalSenderInfos( 185 cricket::MediaType media_type); 186 const RtpSenderInfo* FindSenderInfo(const std::vector<RtpSenderInfo>& infos, 187 const std::string& stream_id, 188 const std::string& sender_id) const; 189 190 // Return the RtpSender with the given track attached. 191 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> 192 FindSenderForTrack(MediaStreamTrackInterface* track) const; 193 194 // Return the RtpSender with the given id, or null if none exists. 195 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> 196 FindSenderById(const std::string& sender_id) const; 197 198 // Return the RtpReceiver with the given id, or null if none exists. 199 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> 200 FindReceiverById(const std::string& receiver_id) const; 201 transceivers()202 TransceiverList* transceivers() { return &transceivers_; } transceivers()203 const TransceiverList* transceivers() const { return &transceivers_; } 204 205 // Plan B helpers for getting the voice/video media channels for the single 206 // audio/video transceiver, if it exists. 207 cricket::VoiceMediaChannel* voice_media_channel() const; 208 cricket::VideoMediaChannel* video_media_channel() const; 209 210 private: signaling_thread()211 rtc::Thread* signaling_thread() const { return context_->signaling_thread(); } worker_thread()212 rtc::Thread* worker_thread() const { return context_->worker_thread(); } IsUnifiedPlan()213 bool IsUnifiedPlan() const { return is_unified_plan_; } NoteUsageEvent(UsageEvent event)214 void NoteUsageEvent(UsageEvent event) { 215 usage_pattern_->NoteUsageEvent(event); 216 } 217 218 // AddTrack implementation when Unified Plan is specified. 219 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackUnifiedPlan( 220 rtc::scoped_refptr<MediaStreamTrackInterface> track, 221 const std::vector<std::string>& stream_ids, 222 const std::vector<RtpEncodingParameters>* init_send_encodings); 223 // AddTrack implementation when Plan B is specified. 224 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackPlanB( 225 rtc::scoped_refptr<MediaStreamTrackInterface> track, 226 const std::vector<std::string>& stream_ids, 227 const std::vector<RtpEncodingParameters>* init_send_encodings); 228 229 // Create an RtpReceiver that sources an audio track. 230 void CreateAudioReceiver(MediaStreamInterface* stream, 231 const RtpSenderInfo& remote_sender_info) 232 RTC_RUN_ON(signaling_thread()); 233 234 // Create an RtpReceiver that sources a video track. 235 void CreateVideoReceiver(MediaStreamInterface* stream, 236 const RtpSenderInfo& remote_sender_info) 237 RTC_RUN_ON(signaling_thread()); 238 rtc::scoped_refptr<RtpReceiverInterface> RemoveAndStopReceiver( 239 const RtpSenderInfo& remote_sender_info) RTC_RUN_ON(signaling_thread()); 240 241 PeerConnectionObserver* Observer() const; 242 void OnNegotiationNeeded(); 243 244 cricket::MediaEngineInterface* media_engine() const; 245 ssrc_generator()246 rtc::UniqueRandomIdGenerator* ssrc_generator() const { 247 return context_->ssrc_generator(); 248 } 249 250 TransceiverList transceivers_; 251 252 // These lists store sender info seen in local/remote descriptions. 253 std::vector<RtpSenderInfo> remote_audio_sender_infos_ 254 RTC_GUARDED_BY(signaling_thread()); 255 std::vector<RtpSenderInfo> remote_video_sender_infos_ 256 RTC_GUARDED_BY(signaling_thread()); 257 std::vector<RtpSenderInfo> local_audio_sender_infos_ 258 RTC_GUARDED_BY(signaling_thread()); 259 std::vector<RtpSenderInfo> local_video_sender_infos_ 260 RTC_GUARDED_BY(signaling_thread()); 261 262 bool closed_ = false; 263 bool const is_unified_plan_; 264 ConnectionContext* context_; 265 UsagePattern* usage_pattern_; 266 PeerConnectionObserver* observer_; 267 LegacyStatsCollectorInterface* const legacy_stats_; 268 std::function<void()> on_negotiation_needed_; 269 rtc::WeakPtrFactory<RtpTransmissionManager> weak_ptr_factory_ 270 RTC_GUARDED_BY(signaling_thread()); 271 }; 272 273 } // namespace webrtc 274 275 #endif // PC_RTP_TRANSMISSION_MANAGER_H_ 276