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/channel_manager.h" 31 #include "pc/rtp_receiver.h" 32 #include "pc/rtp_sender.h" 33 #include "pc/rtp_transceiver.h" 34 #include "pc/stats_collector_interface.h" 35 #include "pc/transceiver_list.h" 36 #include "pc/usage_pattern.h" 37 #include "rtc_base/third_party/sigslot/sigslot.h" 38 #include "rtc_base/thread.h" 39 #include "rtc_base/thread_annotations.h" 40 #include "rtc_base/weak_ptr.h" 41 42 namespace rtc { 43 class Thread; 44 } 45 46 namespace webrtc { 47 48 // This class contains information about 49 // an RTPSender, used for things like looking it up by SSRC. 50 struct RtpSenderInfo { RtpSenderInfoRtpSenderInfo51 RtpSenderInfo() : first_ssrc(0) {} RtpSenderInfoRtpSenderInfo52 RtpSenderInfo(const std::string& stream_id, 53 const std::string sender_id, 54 uint32_t ssrc) 55 : stream_id(stream_id), sender_id(sender_id), first_ssrc(ssrc) {} 56 bool operator==(const RtpSenderInfo& other) { 57 return this->stream_id == other.stream_id && 58 this->sender_id == other.sender_id && 59 this->first_ssrc == other.first_ssrc; 60 } 61 std::string stream_id; 62 std::string sender_id; 63 // An RtpSender can have many SSRCs. The first one is used as a sort of ID 64 // for communicating with the lower layers. 65 uint32_t first_ssrc; 66 }; 67 68 // The RtpTransmissionManager class is responsible for managing the lifetime 69 // and relationships between objects of type RtpSender, RtpReceiver and 70 // RtpTransceiver. 71 class RtpTransmissionManager : public RtpSenderBase::SetStreamsObserver { 72 public: 73 RtpTransmissionManager(bool is_unified_plan, 74 rtc::Thread* signaling_thread, 75 rtc::Thread* worker_thread, 76 cricket::ChannelManager* channel_manager, 77 UsagePattern* usage_pattern, 78 PeerConnectionObserver* observer, 79 StatsCollectorInterface* stats_, 80 std::function<void()> on_negotiation_needed); 81 82 // No move or copy permitted. 83 RtpTransmissionManager(const RtpTransmissionManager&) = delete; 84 RtpTransmissionManager& operator=(const RtpTransmissionManager&) = delete; 85 86 // Stop activity. In particular, don't call observer_ any more. 87 void Close(); 88 89 // RtpSenderBase::SetStreamsObserver override. 90 void OnSetStreams() override; 91 92 // Add a new track, creating transceiver if required. 93 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrack( 94 rtc::scoped_refptr<MediaStreamTrackInterface> track, 95 const std::vector<std::string>& stream_ids); 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 123 // Returns the list of senders currently associated with some 124 // registered transceiver 125 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> 126 GetSendersInternal() const; 127 128 // Returns the list of receivers currently associated with a transceiver 129 std::vector< 130 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> 131 GetReceiversInternal() const; 132 133 // Plan B: Get the transceiver containing all audio senders and receivers 134 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> 135 GetAudioTransceiver() const; 136 // Plan B: Get the transceiver containing all video senders and receivers 137 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> 138 GetVideoTransceiver() const; 139 140 // Add an audio track, reusing or creating the sender. 141 void AddAudioTrack(AudioTrackInterface* track, MediaStreamInterface* stream); 142 // Plan B: Remove an audio track, removing the sender. 143 void RemoveAudioTrack(AudioTrackInterface* track, 144 MediaStreamInterface* stream); 145 // Add a video track, reusing or creating the sender. 146 void AddVideoTrack(VideoTrackInterface* track, MediaStreamInterface* stream); 147 // Plan B: Remove a video track, removing the sender. 148 void RemoveVideoTrack(VideoTrackInterface* track, 149 MediaStreamInterface* stream); 150 151 // Triggered when a remote sender has been seen for the first time in a remote 152 // session description. It creates a remote MediaStreamTrackInterface 153 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver. 154 void OnRemoteSenderAdded(const RtpSenderInfo& sender_info, 155 MediaStreamInterface* stream, 156 cricket::MediaType media_type); 157 158 // Triggered when a remote sender has been removed from a remote session 159 // description. It removes the remote sender with id |sender_id| from a remote 160 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver. 161 void OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, 162 MediaStreamInterface* stream, 163 cricket::MediaType media_type); 164 165 // Triggered when a local sender has been seen for the first time in a local 166 // session description. 167 // This method triggers CreateAudioSender or CreateVideoSender if the rtp 168 // streams in the local SessionDescription can be mapped to a MediaStreamTrack 169 // in a MediaStream in |local_streams_| 170 void OnLocalSenderAdded(const RtpSenderInfo& sender_info, 171 cricket::MediaType media_type); 172 173 // Triggered when a local sender has been removed from a local session 174 // description. 175 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream 176 // has been removed from the local SessionDescription and the stream can be 177 // mapped to a MediaStreamTrack in a MediaStream in |local_streams_|. 178 void OnLocalSenderRemoved(const RtpSenderInfo& sender_info, 179 cricket::MediaType media_type); 180 181 std::vector<RtpSenderInfo>* GetRemoteSenderInfos( 182 cricket::MediaType media_type); 183 std::vector<RtpSenderInfo>* GetLocalSenderInfos( 184 cricket::MediaType media_type); 185 const RtpSenderInfo* FindSenderInfo(const std::vector<RtpSenderInfo>& infos, 186 const std::string& stream_id, 187 const std::string sender_id) const; 188 189 // Return the RtpSender with the given track attached. 190 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> 191 FindSenderForTrack(MediaStreamTrackInterface* track) const; 192 193 // Return the RtpSender with the given id, or null if none exists. 194 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> 195 FindSenderById(const std::string& sender_id) const; 196 197 // Return the RtpReceiver with the given id, or null if none exists. 198 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> 199 FindReceiverById(const std::string& receiver_id) const; 200 transceivers()201 TransceiverList* transceivers() { return &transceivers_; } transceivers()202 const TransceiverList* transceivers() const { return &transceivers_; } 203 204 // Plan B helpers for getting the voice/video media channels for the single 205 // audio/video transceiver, if it exists. 206 cricket::VoiceMediaChannel* voice_media_channel() const; 207 cricket::VideoMediaChannel* video_media_channel() const; 208 209 private: signaling_thread()210 rtc::Thread* signaling_thread() const { return signaling_thread_; } worker_thread()211 rtc::Thread* worker_thread() const { return worker_thread_; } channel_manager()212 cricket::ChannelManager* channel_manager() const { return channel_manager_; } 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 // AddTrack implementation when Plan B is specified. 223 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackPlanB( 224 rtc::scoped_refptr<MediaStreamTrackInterface> track, 225 const std::vector<std::string>& stream_ids); 226 227 // Create an RtpReceiver that sources an audio track. 228 void CreateAudioReceiver(MediaStreamInterface* stream, 229 const RtpSenderInfo& remote_sender_info) 230 RTC_RUN_ON(signaling_thread()); 231 232 // Create an RtpReceiver that sources a video track. 233 void CreateVideoReceiver(MediaStreamInterface* stream, 234 const RtpSenderInfo& remote_sender_info) 235 RTC_RUN_ON(signaling_thread()); 236 rtc::scoped_refptr<RtpReceiverInterface> RemoveAndStopReceiver( 237 const RtpSenderInfo& remote_sender_info) RTC_RUN_ON(signaling_thread()); 238 239 PeerConnectionObserver* Observer() const; 240 void OnNegotiationNeeded(); 241 242 TransceiverList transceivers_; 243 244 // These lists store sender info seen in local/remote descriptions. 245 std::vector<RtpSenderInfo> remote_audio_sender_infos_ 246 RTC_GUARDED_BY(signaling_thread()); 247 std::vector<RtpSenderInfo> remote_video_sender_infos_ 248 RTC_GUARDED_BY(signaling_thread()); 249 std::vector<RtpSenderInfo> local_audio_sender_infos_ 250 RTC_GUARDED_BY(signaling_thread()); 251 std::vector<RtpSenderInfo> local_video_sender_infos_ 252 RTC_GUARDED_BY(signaling_thread()); 253 254 bool closed_ = false; 255 bool const is_unified_plan_; 256 rtc::Thread* signaling_thread_; 257 rtc::Thread* worker_thread_; 258 cricket::ChannelManager* channel_manager_; 259 UsagePattern* usage_pattern_; 260 PeerConnectionObserver* observer_; 261 StatsCollectorInterface* const stats_; 262 std::function<void()> on_negotiation_needed_; 263 rtc::WeakPtrFactory<RtpTransmissionManager> weak_ptr_factory_ 264 RTC_GUARDED_BY(signaling_thread()); 265 }; 266 267 } // namespace webrtc 268 269 #endif // PC_RTP_TRANSMISSION_MANAGER_H_ 270