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 | |
| 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" |
Artem Titov | d15a575 | 2021-02-10 13:31:24 | [diff] [blame] | 28 | #include "api/sequence_checker.h" |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 29 | #include "media/base/media_channel.h" |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 30 | #include "pc/legacy_stats_collector_interface.h" |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 31 | #include "pc/rtp_receiver.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 32 | #include "pc/rtp_receiver_proxy.h" |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 33 | #include "pc/rtp_sender.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 34 | #include "pc/rtp_sender_proxy.h" |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 35 | #include "pc/rtp_transceiver.h" |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 36 | #include "pc/transceiver_list.h" |
| 37 | #include "pc/usage_pattern.h" |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 38 | #include "rtc_base/thread.h" |
| 39 | #include "rtc_base/thread_annotations.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 40 | #include "rtc_base/weak_ptr.h" |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 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 { |
| 51 | RtpSenderInfo() : first_ssrc(0) {} |
| 52 | RtpSenderInfo(const std::string& stream_id, |
Ali Tofigh | b8ef923 | 2022-01-26 13:28:42 | [diff] [blame] | 53 | const std::string& sender_id, |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 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. |
Harald Alvestrand | 280054f | 2020-11-10 13:12:53 | [diff] [blame] | 71 | class RtpTransmissionManager : public RtpSenderBase::SetStreamsObserver { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 72 | public: |
| 73 | RtpTransmissionManager(bool is_unified_plan, |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 74 | ConnectionContext* context, |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 75 | UsagePattern* usage_pattern, |
| 76 | PeerConnectionObserver* observer, |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 77 | LegacyStatsCollectorInterface* legacy_stats, |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 78 | std::function<void()> on_negotiation_needed); |
| 79 | |
| 80 | // No move or copy permitted. |
| 81 | RtpTransmissionManager(const RtpTransmissionManager&) = delete; |
| 82 | RtpTransmissionManager& operator=(const RtpTransmissionManager&) = delete; |
| 83 | |
| 84 | // Stop activity. In particular, don't call observer_ any more. |
| 85 | void Close(); |
| 86 | |
| 87 | // RtpSenderBase::SetStreamsObserver override. |
| 88 | void OnSetStreams() override; |
| 89 | |
| 90 | // Add a new track, creating transceiver if required. |
| 91 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrack( |
| 92 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Jonas Oreland | 4b2a106 | 2022-10-19 07:24:42 | [diff] [blame] | 93 | const std::vector<std::string>& stream_ids, |
| 94 | const std::vector<RtpEncodingParameters>* init_send_encodings); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 95 | |
| 96 | // Create a new RTP sender. Does not associate with a transceiver. |
| 97 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 98 | CreateSender(cricket::MediaType media_type, |
| 99 | const std::string& id, |
| 100 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 101 | const std::vector<std::string>& stream_ids, |
| 102 | const std::vector<RtpEncodingParameters>& send_encodings); |
| 103 | |
| 104 | // Create a new RTP receiver. Does not associate with a transceiver. |
| 105 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 106 | CreateReceiver(cricket::MediaType media_type, const std::string& receiver_id); |
| 107 | |
| 108 | // Create a new RtpTransceiver of the given type and add it to the list of |
| 109 | // registered transceivers. |
| 110 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 111 | CreateAndAddTransceiver( |
| 112 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 113 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 114 | receiver); |
| 115 | |
| 116 | // Returns the first RtpTransceiver suitable for a newly added track, if such |
| 117 | // transceiver is available. |
| 118 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 119 | FindFirstTransceiverForAddedTrack( |
Jonas Oreland | 4b2a106 | 2022-10-19 07:24:42 | [diff] [blame] | 120 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 121 | const std::vector<RtpEncodingParameters>* init_send_encodings); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 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 | |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 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 |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 159 | // description. It removes the remote sender with id `sender_id` from a remote |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 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 |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 169 | // in a MediaStream in `local_streams_` |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 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 |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 177 | // mapped to a MediaStreamTrack in a MediaStream in `local_streams_`. |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 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, |
Ali Tofigh | b8ef923 | 2022-01-26 13:28:42 | [diff] [blame] | 187 | const std::string& sender_id) const; |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 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 | |
| 201 | TransceiverList* transceivers() { return &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. |
Harald Alvestrand | c0d44d9 | 2022-12-13 12:57:24 | [diff] [blame] | 206 | cricket::VoiceMediaSendChannelInterface* voice_media_send_channel() const; |
| 207 | cricket::VideoMediaSendChannelInterface* video_media_send_channel() const; |
| 208 | cricket::VoiceMediaReceiveChannelInterface* voice_media_receive_channel() |
| 209 | const; |
| 210 | cricket::VideoMediaReceiveChannelInterface* video_media_receive_channel() |
| 211 | const; |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 212 | |
| 213 | private: |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 214 | rtc::Thread* signaling_thread() const { return context_->signaling_thread(); } |
| 215 | rtc::Thread* worker_thread() const { return context_->worker_thread(); } |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 216 | bool IsUnifiedPlan() const { return is_unified_plan_; } |
| 217 | void NoteUsageEvent(UsageEvent event) { |
| 218 | usage_pattern_->NoteUsageEvent(event); |
| 219 | } |
| 220 | |
| 221 | // AddTrack implementation when Unified Plan is specified. |
| 222 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackUnifiedPlan( |
| 223 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Jonas Oreland | 4b2a106 | 2022-10-19 07:24:42 | [diff] [blame] | 224 | const std::vector<std::string>& stream_ids, |
| 225 | const std::vector<RtpEncodingParameters>* init_send_encodings); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 226 | // AddTrack implementation when Plan B is specified. |
| 227 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackPlanB( |
| 228 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Jonas Oreland | 4b2a106 | 2022-10-19 07:24:42 | [diff] [blame] | 229 | const std::vector<std::string>& stream_ids, |
| 230 | const std::vector<RtpEncodingParameters>* init_send_encodings); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 231 | |
| 232 | // Create an RtpReceiver that sources an audio track. |
| 233 | void CreateAudioReceiver(MediaStreamInterface* stream, |
| 234 | const RtpSenderInfo& remote_sender_info) |
| 235 | RTC_RUN_ON(signaling_thread()); |
| 236 | |
| 237 | // Create an RtpReceiver that sources a video track. |
| 238 | void CreateVideoReceiver(MediaStreamInterface* stream, |
| 239 | const RtpSenderInfo& remote_sender_info) |
| 240 | RTC_RUN_ON(signaling_thread()); |
| 241 | rtc::scoped_refptr<RtpReceiverInterface> RemoveAndStopReceiver( |
| 242 | const RtpSenderInfo& remote_sender_info) RTC_RUN_ON(signaling_thread()); |
| 243 | |
| 244 | PeerConnectionObserver* Observer() const; |
| 245 | void OnNegotiationNeeded(); |
| 246 | |
Harald Alvestrand | 35f4b4c | 2022-05-16 10:36:43 | [diff] [blame] | 247 | cricket::MediaEngineInterface* media_engine() const; |
| 248 | |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 249 | rtc::UniqueRandomIdGenerator* ssrc_generator() const { |
| 250 | return context_->ssrc_generator(); |
| 251 | } |
| 252 | |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 253 | TransceiverList transceivers_; |
| 254 | |
| 255 | // These lists store sender info seen in local/remote descriptions. |
| 256 | std::vector<RtpSenderInfo> remote_audio_sender_infos_ |
| 257 | RTC_GUARDED_BY(signaling_thread()); |
| 258 | std::vector<RtpSenderInfo> remote_video_sender_infos_ |
| 259 | RTC_GUARDED_BY(signaling_thread()); |
| 260 | std::vector<RtpSenderInfo> local_audio_sender_infos_ |
| 261 | RTC_GUARDED_BY(signaling_thread()); |
| 262 | std::vector<RtpSenderInfo> local_video_sender_infos_ |
| 263 | RTC_GUARDED_BY(signaling_thread()); |
| 264 | |
| 265 | bool closed_ = false; |
| 266 | bool const is_unified_plan_; |
Harald Alvestrand | c3fa7c3 | 2022-05-22 10:57:01 | [diff] [blame] | 267 | ConnectionContext* context_; |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 268 | UsagePattern* usage_pattern_; |
| 269 | PeerConnectionObserver* observer_; |
Henrik Boström | f785989 | 2022-07-04 12:36:37 | [diff] [blame] | 270 | LegacyStatsCollectorInterface* const legacy_stats_; |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 271 | std::function<void()> on_negotiation_needed_; |
Harald Alvestrand | 280054f | 2020-11-10 13:12:53 | [diff] [blame] | 272 | rtc::WeakPtrFactory<RtpTransmissionManager> weak_ptr_factory_ |
| 273 | RTC_GUARDED_BY(signaling_thread()); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | } // namespace webrtc |
| 277 | |
| 278 | #endif // PC_RTP_TRANSMISSION_MANAGER_H_ |