blob: 6e25f2d9b7ed5d3083af079afdff8316675a9fc9 [file] [log] [blame]
Harald Alvestrande15fb152020-10-19 13:28:05 +00001/*
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 14:31:24 +010015
Harald Alvestrande15fb152020-10-19 13:28:05 +000016#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 Titovd15a5752021-02-10 14:31:24 +010028#include "api/sequence_checker.h"
Harald Alvestrande15fb152020-10-19 13:28:05 +000029#include "media/base/media_channel.h"
Harald Alvestrande15fb152020-10-19 13:28:05 +000030#include "pc/rtp_receiver.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000031#include "pc/rtp_receiver_proxy.h"
Harald Alvestrande15fb152020-10-19 13:28:05 +000032#include "pc/rtp_sender.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000033#include "pc/rtp_sender_proxy.h"
Harald Alvestrande15fb152020-10-19 13:28:05 +000034#include "pc/rtp_transceiver.h"
35#include "pc/stats_collector_interface.h"
36#include "pc/transceiver_list.h"
37#include "pc/usage_pattern.h"
Harald Alvestrande15fb152020-10-19 13:28:05 +000038#include "rtc_base/third_party/sigslot/sigslot.h"
39#include "rtc_base/thread.h"
40#include "rtc_base/thread_annotations.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000041#include "rtc_base/weak_ptr.h"
Harald Alvestrande15fb152020-10-19 13:28:05 +000042
Harald Alvestrand9e334b72022-05-04 13:38:31 +000043namespace cricket {
44class ChannelManager;
45}
46
Harald Alvestrande15fb152020-10-19 13:28:05 +000047namespace rtc {
48class Thread;
49}
50
51namespace webrtc {
52
53// This class contains information about
54// an RTPSender, used for things like looking it up by SSRC.
55struct RtpSenderInfo {
56 RtpSenderInfo() : first_ssrc(0) {}
57 RtpSenderInfo(const std::string& stream_id,
Ali Tofighb8ef9232022-01-26 14:28:42 +010058 const std::string& sender_id,
Harald Alvestrande15fb152020-10-19 13:28:05 +000059 uint32_t ssrc)
60 : stream_id(stream_id), sender_id(sender_id), first_ssrc(ssrc) {}
61 bool operator==(const RtpSenderInfo& other) {
62 return this->stream_id == other.stream_id &&
63 this->sender_id == other.sender_id &&
64 this->first_ssrc == other.first_ssrc;
65 }
66 std::string stream_id;
67 std::string sender_id;
68 // An RtpSender can have many SSRCs. The first one is used as a sort of ID
69 // for communicating with the lower layers.
70 uint32_t first_ssrc;
71};
72
73// The RtpTransmissionManager class is responsible for managing the lifetime
74// and relationships between objects of type RtpSender, RtpReceiver and
75// RtpTransceiver.
Harald Alvestrand280054f2020-11-10 13:12:53 +000076class RtpTransmissionManager : public RtpSenderBase::SetStreamsObserver {
Harald Alvestrande15fb152020-10-19 13:28:05 +000077 public:
78 RtpTransmissionManager(bool is_unified_plan,
79 rtc::Thread* signaling_thread,
80 rtc::Thread* worker_thread,
81 cricket::ChannelManager* channel_manager,
82 UsagePattern* usage_pattern,
83 PeerConnectionObserver* observer,
84 StatsCollectorInterface* stats_,
85 std::function<void()> on_negotiation_needed);
86
87 // No move or copy permitted.
88 RtpTransmissionManager(const RtpTransmissionManager&) = delete;
89 RtpTransmissionManager& operator=(const RtpTransmissionManager&) = delete;
90
91 // Stop activity. In particular, don't call observer_ any more.
92 void Close();
93
94 // RtpSenderBase::SetStreamsObserver override.
95 void OnSetStreams() override;
96
97 // Add a new track, creating transceiver if required.
98 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrack(
99 rtc::scoped_refptr<MediaStreamTrackInterface> track,
100 const std::vector<std::string>& stream_ids);
101
102 // Create a new RTP sender. Does not associate with a transceiver.
103 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
104 CreateSender(cricket::MediaType media_type,
105 const std::string& id,
106 rtc::scoped_refptr<MediaStreamTrackInterface> track,
107 const std::vector<std::string>& stream_ids,
108 const std::vector<RtpEncodingParameters>& send_encodings);
109
110 // Create a new RTP receiver. Does not associate with a transceiver.
111 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
112 CreateReceiver(cricket::MediaType media_type, const std::string& receiver_id);
113
114 // Create a new RtpTransceiver of the given type and add it to the list of
115 // registered transceivers.
116 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
117 CreateAndAddTransceiver(
118 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
119 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
120 receiver);
121
122 // Returns the first RtpTransceiver suitable for a newly added track, if such
123 // transceiver is available.
124 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
125 FindFirstTransceiverForAddedTrack(
126 rtc::scoped_refptr<MediaStreamTrackInterface> track);
127
128 // Returns the list of senders currently associated with some
129 // registered transceiver
130 std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>>
131 GetSendersInternal() const;
132
133 // Returns the list of receivers currently associated with a transceiver
134 std::vector<
135 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>>
136 GetReceiversInternal() const;
137
138 // Plan B: Get the transceiver containing all audio senders and receivers
139 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
140 GetAudioTransceiver() const;
141 // Plan B: Get the transceiver containing all video senders and receivers
142 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
143 GetVideoTransceiver() const;
144
Harald Alvestrande15fb152020-10-19 13:28:05 +0000145 // Add an audio track, reusing or creating the sender.
146 void AddAudioTrack(AudioTrackInterface* track, MediaStreamInterface* stream);
147 // Plan B: Remove an audio track, removing the sender.
148 void RemoveAudioTrack(AudioTrackInterface* track,
149 MediaStreamInterface* stream);
150 // Add a video track, reusing or creating the sender.
151 void AddVideoTrack(VideoTrackInterface* track, MediaStreamInterface* stream);
152 // Plan B: Remove a video track, removing the sender.
153 void RemoveVideoTrack(VideoTrackInterface* track,
154 MediaStreamInterface* stream);
155
156 // Triggered when a remote sender has been seen for the first time in a remote
157 // session description. It creates a remote MediaStreamTrackInterface
158 // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
159 void OnRemoteSenderAdded(const RtpSenderInfo& sender_info,
160 MediaStreamInterface* stream,
161 cricket::MediaType media_type);
162
163 // Triggered when a remote sender has been removed from a remote session
Artem Titov880fa812021-07-30 22:30:23 +0200164 // description. It removes the remote sender with id `sender_id` from a remote
Harald Alvestrande15fb152020-10-19 13:28:05 +0000165 // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
166 void OnRemoteSenderRemoved(const RtpSenderInfo& sender_info,
167 MediaStreamInterface* stream,
168 cricket::MediaType media_type);
169
170 // Triggered when a local sender has been seen for the first time in a local
171 // session description.
172 // This method triggers CreateAudioSender or CreateVideoSender if the rtp
173 // streams in the local SessionDescription can be mapped to a MediaStreamTrack
Artem Titov880fa812021-07-30 22:30:23 +0200174 // in a MediaStream in `local_streams_`
Harald Alvestrande15fb152020-10-19 13:28:05 +0000175 void OnLocalSenderAdded(const RtpSenderInfo& sender_info,
176 cricket::MediaType media_type);
177
178 // Triggered when a local sender has been removed from a local session
179 // description.
180 // This method triggers DestroyAudioSender or DestroyVideoSender if a stream
181 // has been removed from the local SessionDescription and the stream can be
Artem Titov880fa812021-07-30 22:30:23 +0200182 // mapped to a MediaStreamTrack in a MediaStream in `local_streams_`.
Harald Alvestrande15fb152020-10-19 13:28:05 +0000183 void OnLocalSenderRemoved(const RtpSenderInfo& sender_info,
184 cricket::MediaType media_type);
185
186 std::vector<RtpSenderInfo>* GetRemoteSenderInfos(
187 cricket::MediaType media_type);
188 std::vector<RtpSenderInfo>* GetLocalSenderInfos(
189 cricket::MediaType media_type);
190 const RtpSenderInfo* FindSenderInfo(const std::vector<RtpSenderInfo>& infos,
191 const std::string& stream_id,
Ali Tofighb8ef9232022-01-26 14:28:42 +0100192 const std::string& sender_id) const;
Harald Alvestrande15fb152020-10-19 13:28:05 +0000193
194 // Return the RtpSender with the given track attached.
195 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
196 FindSenderForTrack(MediaStreamTrackInterface* track) const;
197
198 // Return the RtpSender with the given id, or null if none exists.
199 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
200 FindSenderById(const std::string& sender_id) const;
201
202 // Return the RtpReceiver with the given id, or null if none exists.
203 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
204 FindReceiverById(const std::string& receiver_id) const;
205
206 TransceiverList* transceivers() { return &transceivers_; }
207 const TransceiverList* transceivers() const { return &transceivers_; }
208
209 // Plan B helpers for getting the voice/video media channels for the single
210 // audio/video transceiver, if it exists.
211 cricket::VoiceMediaChannel* voice_media_channel() const;
212 cricket::VideoMediaChannel* video_media_channel() const;
213
214 private:
215 rtc::Thread* signaling_thread() const { return signaling_thread_; }
216 rtc::Thread* worker_thread() const { return worker_thread_; }
217 cricket::ChannelManager* channel_manager() const { return channel_manager_; }
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,
226 const std::vector<std::string>& stream_ids);
227 // AddTrack implementation when Plan B is specified.
228 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackPlanB(
229 rtc::scoped_refptr<MediaStreamTrackInterface> track,
230 const std::vector<std::string>& stream_ids);
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
247 TransceiverList transceivers_;
248
249 // These lists store sender info seen in local/remote descriptions.
250 std::vector<RtpSenderInfo> remote_audio_sender_infos_
251 RTC_GUARDED_BY(signaling_thread());
252 std::vector<RtpSenderInfo> remote_video_sender_infos_
253 RTC_GUARDED_BY(signaling_thread());
254 std::vector<RtpSenderInfo> local_audio_sender_infos_
255 RTC_GUARDED_BY(signaling_thread());
256 std::vector<RtpSenderInfo> local_video_sender_infos_
257 RTC_GUARDED_BY(signaling_thread());
258
259 bool closed_ = false;
260 bool const is_unified_plan_;
Harald Alvestrand9e334b72022-05-04 13:38:31 +0000261 rtc::Thread* const signaling_thread_;
262 rtc::Thread* const worker_thread_;
263 cricket::ChannelManager* const channel_manager_;
Harald Alvestrande15fb152020-10-19 13:28:05 +0000264 UsagePattern* usage_pattern_;
265 PeerConnectionObserver* observer_;
266 StatsCollectorInterface* const stats_;
267 std::function<void()> on_negotiation_needed_;
Harald Alvestrand280054f2020-11-10 13:12:53 +0000268 rtc::WeakPtrFactory<RtpTransmissionManager> weak_ptr_factory_
269 RTC_GUARDED_BY(signaling_thread());
Harald Alvestrande15fb152020-10-19 13:28:05 +0000270};
271
272} // namespace webrtc
273
274#endif // PC_RTP_TRANSMISSION_MANAGER_H_