Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 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> |
Artem Titov | d15a575 | 2021-02-10 13:31:24 | [diff] [blame] | 15 | |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 16 | #include <functional> |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
Florent Castelli | c5b9a60 | 2024-09-03 09:13:23 | [diff] [blame] | 20 | #include "api/environment/environment.h" |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 21 | #include "api/media_stream_interface.h" |
| 22 | #include "api/media_types.h" |
| 23 | #include "api/peer_connection_interface.h" |
| 24 | #include "api/rtc_error.h" |
| 25 | #include "api/rtp_parameters.h" |
| 26 | #include "api/rtp_receiver_interface.h" |
| 27 | #include "api/rtp_sender_interface.h" |
| 28 | #include "api/scoped_refptr.h" |
Artem Titov | d15a575 | 2021-02-10 13:31:24 | [diff] [blame] | 29 | #include "api/sequence_checker.h" |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 30 | #include "media/base/media_channel.h" |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 31 | #include "pc/legacy_stats_collector_interface.h" |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 32 | #include "pc/rtp_receiver.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 33 | #include "pc/rtp_receiver_proxy.h" |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 34 | #include "pc/rtp_sender.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 35 | #include "pc/rtp_sender_proxy.h" |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 36 | #include "pc/rtp_transceiver.h" |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 37 | #include "pc/transceiver_list.h" |
| 38 | #include "pc/usage_pattern.h" |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 39 | #include "rtc_base/thread.h" |
| 40 | #include "rtc_base/thread_annotations.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 41 | #include "rtc_base/weak_ptr.h" |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 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 { |
| 52 | RtpSenderInfo() : first_ssrc(0) {} |
| 53 | RtpSenderInfo(const std::string& stream_id, |
Ali Tofigh | b8ef923 | 2022-01-26 13:28:42 | [diff] [blame] | 54 | const std::string& sender_id, |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 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. |
Harald Alvestrand | 280054f | 2020-11-10 13:12:53 | [diff] [blame] | 72 | class RtpTransmissionManager : public RtpSenderBase::SetStreamsObserver { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 73 | public: |
Florent Castelli | c5b9a60 | 2024-09-03 09:13:23 | [diff] [blame] | 74 | RtpTransmissionManager(const Environment& env, |
| 75 | bool is_unified_plan, |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 76 | ConnectionContext* context, |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 77 | UsagePattern* usage_pattern, |
| 78 | PeerConnectionObserver* observer, |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 79 | LegacyStatsCollectorInterface* legacy_stats, |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 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, |
Jonas Oreland | 4b2a106 | 2022-10-19 07:24:42 | [diff] [blame] | 95 | const std::vector<std::string>& stream_ids, |
| 96 | const std::vector<RtpEncodingParameters>* init_send_encodings); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 97 | |
| 98 | // Create a new RTP sender. Does not associate with a transceiver. |
| 99 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 100 | CreateSender(cricket::MediaType media_type, |
| 101 | const std::string& id, |
| 102 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 103 | const std::vector<std::string>& stream_ids, |
| 104 | const std::vector<RtpEncodingParameters>& send_encodings); |
| 105 | |
| 106 | // Create a new RTP receiver. Does not associate with a transceiver. |
| 107 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 108 | CreateReceiver(cricket::MediaType media_type, const std::string& receiver_id); |
| 109 | |
| 110 | // Create a new RtpTransceiver of the given type and add it to the list of |
| 111 | // registered transceivers. |
| 112 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 113 | CreateAndAddTransceiver( |
| 114 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 115 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 116 | receiver); |
| 117 | |
| 118 | // Returns the first RtpTransceiver suitable for a newly added track, if such |
| 119 | // transceiver is available. |
| 120 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 121 | FindFirstTransceiverForAddedTrack( |
Jonas Oreland | 4b2a106 | 2022-10-19 07:24:42 | [diff] [blame] | 122 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 123 | const std::vector<RtpEncodingParameters>* init_send_encodings); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 124 | |
| 125 | // Returns the list of senders currently associated with some |
| 126 | // registered transceiver |
| 127 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 128 | GetSendersInternal() const; |
| 129 | |
| 130 | // Returns the list of receivers currently associated with a transceiver |
| 131 | std::vector< |
| 132 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 133 | GetReceiversInternal() const; |
| 134 | |
| 135 | // Plan B: Get the transceiver containing all audio senders and receivers |
| 136 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 137 | GetAudioTransceiver() const; |
| 138 | // Plan B: Get the transceiver containing all video senders and receivers |
| 139 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 140 | GetVideoTransceiver() const; |
| 141 | |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 142 | // Add an audio track, reusing or creating the sender. |
| 143 | void AddAudioTrack(AudioTrackInterface* track, MediaStreamInterface* stream); |
| 144 | // Plan B: Remove an audio track, removing the sender. |
| 145 | void RemoveAudioTrack(AudioTrackInterface* track, |
| 146 | MediaStreamInterface* stream); |
| 147 | // Add a video track, reusing or creating the sender. |
| 148 | void AddVideoTrack(VideoTrackInterface* track, MediaStreamInterface* stream); |
| 149 | // Plan B: Remove a video track, removing the sender. |
| 150 | void RemoveVideoTrack(VideoTrackInterface* track, |
| 151 | MediaStreamInterface* stream); |
| 152 | |
| 153 | // Triggered when a remote sender has been seen for the first time in a remote |
| 154 | // session description. It creates a remote MediaStreamTrackInterface |
| 155 | // implementation and triggers CreateAudioReceiver or CreateVideoReceiver. |
| 156 | void OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 157 | MediaStreamInterface* stream, |
| 158 | cricket::MediaType media_type); |
| 159 | |
| 160 | // Triggered when a remote sender has been removed from a remote session |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 161 | // description. It removes the remote sender with id `sender_id` from a remote |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 162 | // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver. |
| 163 | void OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 164 | MediaStreamInterface* stream, |
| 165 | cricket::MediaType media_type); |
| 166 | |
| 167 | // Triggered when a local sender has been seen for the first time in a local |
| 168 | // session description. |
| 169 | // This method triggers CreateAudioSender or CreateVideoSender if the rtp |
| 170 | // streams in the local SessionDescription can be mapped to a MediaStreamTrack |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 171 | // in a MediaStream in `local_streams_` |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 172 | void OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 173 | cricket::MediaType media_type); |
| 174 | |
| 175 | // Triggered when a local sender has been removed from a local session |
| 176 | // description. |
| 177 | // This method triggers DestroyAudioSender or DestroyVideoSender if a stream |
| 178 | // has been removed from the local SessionDescription and the stream can be |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 179 | // mapped to a MediaStreamTrack in a MediaStream in `local_streams_`. |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 180 | void OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 181 | cricket::MediaType media_type); |
| 182 | |
| 183 | std::vector<RtpSenderInfo>* GetRemoteSenderInfos( |
| 184 | cricket::MediaType media_type); |
| 185 | std::vector<RtpSenderInfo>* GetLocalSenderInfos( |
| 186 | cricket::MediaType media_type); |
| 187 | const RtpSenderInfo* FindSenderInfo(const std::vector<RtpSenderInfo>& infos, |
| 188 | const std::string& stream_id, |
Ali Tofigh | b8ef923 | 2022-01-26 13:28:42 | [diff] [blame] | 189 | const std::string& sender_id) const; |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 190 | |
| 191 | // Return the RtpSender with the given track attached. |
| 192 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 193 | FindSenderForTrack(MediaStreamTrackInterface* track) const; |
| 194 | |
| 195 | // Return the RtpSender with the given id, or null if none exists. |
| 196 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 197 | FindSenderById(const std::string& sender_id) const; |
| 198 | |
| 199 | // Return the RtpReceiver with the given id, or null if none exists. |
| 200 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 201 | FindReceiverById(const std::string& receiver_id) const; |
| 202 | |
| 203 | TransceiverList* transceivers() { return &transceivers_; } |
| 204 | const TransceiverList* transceivers() const { return &transceivers_; } |
| 205 | |
| 206 | // Plan B helpers for getting the voice/video media channels for the single |
| 207 | // audio/video transceiver, if it exists. |
Harald Alvestrand | c0d44d9 | 2022-12-13 12:57:24 | [diff] [blame] | 208 | cricket::VoiceMediaSendChannelInterface* voice_media_send_channel() const; |
| 209 | cricket::VideoMediaSendChannelInterface* video_media_send_channel() const; |
| 210 | cricket::VoiceMediaReceiveChannelInterface* voice_media_receive_channel() |
| 211 | const; |
| 212 | cricket::VideoMediaReceiveChannelInterface* video_media_receive_channel() |
| 213 | const; |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 214 | |
| 215 | private: |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 216 | rtc::Thread* signaling_thread() const { return context_->signaling_thread(); } |
| 217 | rtc::Thread* worker_thread() const { return context_->worker_thread(); } |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 218 | bool IsUnifiedPlan() const { return is_unified_plan_; } |
| 219 | void NoteUsageEvent(UsageEvent event) { |
| 220 | usage_pattern_->NoteUsageEvent(event); |
| 221 | } |
| 222 | |
| 223 | // AddTrack implementation when Unified Plan is specified. |
| 224 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackUnifiedPlan( |
| 225 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Jonas Oreland | 4b2a106 | 2022-10-19 07:24:42 | [diff] [blame] | 226 | const std::vector<std::string>& stream_ids, |
| 227 | const std::vector<RtpEncodingParameters>* init_send_encodings); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 228 | // AddTrack implementation when Plan B is specified. |
| 229 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackPlanB( |
| 230 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Jonas Oreland | 4b2a106 | 2022-10-19 07:24:42 | [diff] [blame] | 231 | const std::vector<std::string>& stream_ids, |
| 232 | const std::vector<RtpEncodingParameters>* init_send_encodings); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 233 | |
| 234 | // Create an RtpReceiver that sources an audio track. |
| 235 | void CreateAudioReceiver(MediaStreamInterface* stream, |
| 236 | const RtpSenderInfo& remote_sender_info) |
| 237 | RTC_RUN_ON(signaling_thread()); |
| 238 | |
| 239 | // Create an RtpReceiver that sources a video track. |
| 240 | void CreateVideoReceiver(MediaStreamInterface* stream, |
| 241 | const RtpSenderInfo& remote_sender_info) |
| 242 | RTC_RUN_ON(signaling_thread()); |
| 243 | rtc::scoped_refptr<RtpReceiverInterface> RemoveAndStopReceiver( |
| 244 | const RtpSenderInfo& remote_sender_info) RTC_RUN_ON(signaling_thread()); |
| 245 | |
| 246 | PeerConnectionObserver* Observer() const; |
| 247 | void OnNegotiationNeeded(); |
| 248 | |
Harald Alvestrand | 35f4b4c | 2022-05-16 10:36:43 | [diff] [blame] | 249 | cricket::MediaEngineInterface* media_engine() const; |
| 250 | |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 251 | rtc::UniqueRandomIdGenerator* ssrc_generator() const { |
| 252 | return context_->ssrc_generator(); |
| 253 | } |
| 254 | |
Florent Castelli | c5b9a60 | 2024-09-03 09:13:23 | [diff] [blame] | 255 | const Environment env_; |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 256 | TransceiverList transceivers_; |
| 257 | |
| 258 | // These lists store sender info seen in local/remote descriptions. |
| 259 | std::vector<RtpSenderInfo> remote_audio_sender_infos_ |
| 260 | RTC_GUARDED_BY(signaling_thread()); |
| 261 | std::vector<RtpSenderInfo> remote_video_sender_infos_ |
| 262 | RTC_GUARDED_BY(signaling_thread()); |
| 263 | std::vector<RtpSenderInfo> local_audio_sender_infos_ |
| 264 | RTC_GUARDED_BY(signaling_thread()); |
| 265 | std::vector<RtpSenderInfo> local_video_sender_infos_ |
| 266 | RTC_GUARDED_BY(signaling_thread()); |
| 267 | |
| 268 | bool closed_ = false; |
| 269 | bool const is_unified_plan_; |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 270 | ConnectionContext* context_; |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 271 | UsagePattern* usage_pattern_; |
| 272 | PeerConnectionObserver* observer_; |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 273 | LegacyStatsCollectorInterface* const legacy_stats_; |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 274 | std::function<void()> on_negotiation_needed_; |
Harald Alvestrand | 280054f | 2020-11-10 13:12:53 | [diff] [blame] | 275 | rtc::WeakPtrFactory<RtpTransmissionManager> weak_ptr_factory_ |
| 276 | RTC_GUARDED_BY(signaling_thread()); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | } // namespace webrtc |
| 280 | |
| 281 | #endif // PC_RTP_TRANSMISSION_MANAGER_H_ |