blob: 1b63ef3d294e228d602447120f9a124a3a6f8069 [file] [log] [blame]
Harald Alvestrande15fb152020-10-19 13:28:051/*
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 Titovd15a5752021-02-10 13:31:2415
Harald Alvestrande15fb152020-10-19 13:28:0516#include <functional>
17#include <string>
18#include <vector>
19
Florent Castellic5b9a602024-09-03 09:13:2320#include "api/environment/environment.h"
Harald Alvestrande15fb152020-10-19 13:28:0521#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 Titovd15a5752021-02-10 13:31:2429#include "api/sequence_checker.h"
Harald Alvestrande15fb152020-10-19 13:28:0530#include "media/base/media_channel.h"
Henrik Boströmf7859892022-07-04 12:36:3731#include "pc/legacy_stats_collector_interface.h"
Harald Alvestrande15fb152020-10-19 13:28:0532#include "pc/rtp_receiver.h"
Harald Alvestrandc24a2182022-02-23 13:44:5933#include "pc/rtp_receiver_proxy.h"
Harald Alvestrande15fb152020-10-19 13:28:0534#include "pc/rtp_sender.h"
Harald Alvestrandc24a2182022-02-23 13:44:5935#include "pc/rtp_sender_proxy.h"
Harald Alvestrande15fb152020-10-19 13:28:0536#include "pc/rtp_transceiver.h"
Harald Alvestrande15fb152020-10-19 13:28:0537#include "pc/transceiver_list.h"
38#include "pc/usage_pattern.h"
Harald Alvestrande15fb152020-10-19 13:28:0539#include "rtc_base/thread.h"
40#include "rtc_base/thread_annotations.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0841#include "rtc_base/weak_ptr.h"
Harald Alvestrande15fb152020-10-19 13:28:0542
43namespace rtc {
44class Thread;
45}
46
47namespace webrtc {
48
49// This class contains information about
50// an RTPSender, used for things like looking it up by SSRC.
51struct RtpSenderInfo {
52 RtpSenderInfo() : first_ssrc(0) {}
53 RtpSenderInfo(const std::string& stream_id,
Ali Tofighb8ef9232022-01-26 13:28:4254 const std::string& sender_id,
Harald Alvestrande15fb152020-10-19 13:28:0555 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 Alvestrand280054f2020-11-10 13:12:5372class RtpTransmissionManager : public RtpSenderBase::SetStreamsObserver {
Harald Alvestrande15fb152020-10-19 13:28:0573 public:
Florent Castellic5b9a602024-09-03 09:13:2374 RtpTransmissionManager(const Environment& env,
75 bool is_unified_plan,
Harald Alvestrandc3fa7c32022-05-22 10:57:0176 ConnectionContext* context,
Harald Alvestrande15fb152020-10-19 13:28:0577 UsagePattern* usage_pattern,
78 PeerConnectionObserver* observer,
Henrik Boströmf7859892022-07-04 12:36:3779 LegacyStatsCollectorInterface* legacy_stats,
Harald Alvestrande15fb152020-10-19 13:28:0580 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 Oreland4b2a1062022-10-19 07:24:4295 const std::vector<std::string>& stream_ids,
96 const std::vector<RtpEncodingParameters>* init_send_encodings);
Harald Alvestrande15fb152020-10-19 13:28:0597
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 Oreland4b2a1062022-10-19 07:24:42122 rtc::scoped_refptr<MediaStreamTrackInterface> track,
123 const std::vector<RtpEncodingParameters>* init_send_encodings);
Harald Alvestrande15fb152020-10-19 13:28:05124
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 Alvestrande15fb152020-10-19 13:28:05142 // 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 Titov880fa812021-07-30 20:30:23161 // description. It removes the remote sender with id `sender_id` from a remote
Harald Alvestrande15fb152020-10-19 13:28:05162 // 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 Titov880fa812021-07-30 20:30:23171 // in a MediaStream in `local_streams_`
Harald Alvestrande15fb152020-10-19 13:28:05172 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 Titov880fa812021-07-30 20:30:23179 // mapped to a MediaStreamTrack in a MediaStream in `local_streams_`.
Harald Alvestrande15fb152020-10-19 13:28:05180 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 Tofighb8ef9232022-01-26 13:28:42189 const std::string& sender_id) const;
Harald Alvestrande15fb152020-10-19 13:28:05190
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 Alvestrandc0d44d92022-12-13 12:57:24208 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 Alvestrande15fb152020-10-19 13:28:05214
215 private:
Harald Alvestrandc3fa7c32022-05-22 10:57:01216 rtc::Thread* signaling_thread() const { return context_->signaling_thread(); }
217 rtc::Thread* worker_thread() const { return context_->worker_thread(); }
Harald Alvestrande15fb152020-10-19 13:28:05218 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 Oreland4b2a1062022-10-19 07:24:42226 const std::vector<std::string>& stream_ids,
227 const std::vector<RtpEncodingParameters>* init_send_encodings);
Harald Alvestrande15fb152020-10-19 13:28:05228 // AddTrack implementation when Plan B is specified.
229 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackPlanB(
230 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Jonas Oreland4b2a1062022-10-19 07:24:42231 const std::vector<std::string>& stream_ids,
232 const std::vector<RtpEncodingParameters>* init_send_encodings);
Harald Alvestrande15fb152020-10-19 13:28:05233
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 Alvestrand35f4b4c2022-05-16 10:36:43249 cricket::MediaEngineInterface* media_engine() const;
250
Harald Alvestrandc3fa7c32022-05-22 10:57:01251 rtc::UniqueRandomIdGenerator* ssrc_generator() const {
252 return context_->ssrc_generator();
253 }
254
Florent Castellic5b9a602024-09-03 09:13:23255 const Environment env_;
Harald Alvestrande15fb152020-10-19 13:28:05256 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 Alvestrandc3fa7c32022-05-22 10:57:01270 ConnectionContext* context_;
Harald Alvestrande15fb152020-10-19 13:28:05271 UsagePattern* usage_pattern_;
272 PeerConnectionObserver* observer_;
Henrik Boströmf7859892022-07-04 12:36:37273 LegacyStatsCollectorInterface* const legacy_stats_;
Harald Alvestrande15fb152020-10-19 13:28:05274 std::function<void()> on_negotiation_needed_;
Harald Alvestrand280054f2020-11-10 13:12:53275 rtc::WeakPtrFactory<RtpTransmissionManager> weak_ptr_factory_
276 RTC_GUARDED_BY(signaling_thread());
Harald Alvestrande15fb152020-10-19 13:28:05277};
278
279} // namespace webrtc
280
281#endif // PC_RTP_TRANSMISSION_MANAGER_H_