blob: 5a4bf8352625bd9d805b9368455cbb08333c37e9 [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
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 13:31:2428#include "api/sequence_checker.h"
Harald Alvestrande15fb152020-10-19 13:28:0529#include "media/base/media_channel.h"
Henrik Boströmf7859892022-07-04 12:36:3730#include "pc/legacy_stats_collector_interface.h"
Harald Alvestrande15fb152020-10-19 13:28:0531#include "pc/rtp_receiver.h"
Harald Alvestrandc24a2182022-02-23 13:44:5932#include "pc/rtp_receiver_proxy.h"
Harald Alvestrande15fb152020-10-19 13:28:0533#include "pc/rtp_sender.h"
Harald Alvestrandc24a2182022-02-23 13:44:5934#include "pc/rtp_sender_proxy.h"
Harald Alvestrande15fb152020-10-19 13:28:0535#include "pc/rtp_transceiver.h"
Harald Alvestrande15fb152020-10-19 13:28:0536#include "pc/transceiver_list.h"
37#include "pc/usage_pattern.h"
Harald Alvestrande15fb152020-10-19 13:28:0538#include "rtc_base/thread.h"
39#include "rtc_base/thread_annotations.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0840#include "rtc_base/weak_ptr.h"
Harald Alvestrande15fb152020-10-19 13:28:0541
42namespace rtc {
43class Thread;
44}
45
46namespace webrtc {
47
48// This class contains information about
49// an RTPSender, used for things like looking it up by SSRC.
50struct RtpSenderInfo {
51 RtpSenderInfo() : first_ssrc(0) {}
52 RtpSenderInfo(const std::string& stream_id,
Ali Tofighb8ef9232022-01-26 13:28:4253 const std::string& sender_id,
Harald Alvestrande15fb152020-10-19 13:28:0554 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 Alvestrand280054f2020-11-10 13:12:5371class RtpTransmissionManager : public RtpSenderBase::SetStreamsObserver {
Harald Alvestrande15fb152020-10-19 13:28:0572 public:
73 RtpTransmissionManager(bool is_unified_plan,
Harald Alvestrandc3fa7c32022-05-22 10:57:0174 ConnectionContext* context,
Harald Alvestrande15fb152020-10-19 13:28:0575 UsagePattern* usage_pattern,
76 PeerConnectionObserver* observer,
Henrik Boströmf7859892022-07-04 12:36:3777 LegacyStatsCollectorInterface* legacy_stats,
Harald Alvestrande15fb152020-10-19 13:28:0578 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 Oreland4b2a1062022-10-19 07:24:4293 const std::vector<std::string>& stream_ids,
94 const std::vector<RtpEncodingParameters>* init_send_encodings);
Harald Alvestrande15fb152020-10-19 13:28:0595
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 Oreland4b2a1062022-10-19 07:24:42120 rtc::scoped_refptr<MediaStreamTrackInterface> track,
121 const std::vector<RtpEncodingParameters>* init_send_encodings);
Harald Alvestrande15fb152020-10-19 13:28:05122
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 Alvestrande15fb152020-10-19 13:28:05140 // 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 Titov880fa812021-07-30 20:30:23159 // description. It removes the remote sender with id `sender_id` from a remote
Harald Alvestrande15fb152020-10-19 13:28:05160 // 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 Titov880fa812021-07-30 20:30:23169 // in a MediaStream in `local_streams_`
Harald Alvestrande15fb152020-10-19 13:28:05170 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 Titov880fa812021-07-30 20:30:23177 // mapped to a MediaStreamTrack in a MediaStream in `local_streams_`.
Harald Alvestrande15fb152020-10-19 13:28:05178 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 Tofighb8ef9232022-01-26 13:28:42187 const std::string& sender_id) const;
Harald Alvestrande15fb152020-10-19 13:28:05188
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 Alvestrandc0d44d92022-12-13 12:57:24206 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 Alvestrande15fb152020-10-19 13:28:05212
213 private:
Harald Alvestrandc3fa7c32022-05-22 10:57:01214 rtc::Thread* signaling_thread() const { return context_->signaling_thread(); }
215 rtc::Thread* worker_thread() const { return context_->worker_thread(); }
Harald Alvestrande15fb152020-10-19 13:28:05216 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 Oreland4b2a1062022-10-19 07:24:42224 const std::vector<std::string>& stream_ids,
225 const std::vector<RtpEncodingParameters>* init_send_encodings);
Harald Alvestrande15fb152020-10-19 13:28:05226 // AddTrack implementation when Plan B is specified.
227 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> AddTrackPlanB(
228 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Jonas Oreland4b2a1062022-10-19 07:24:42229 const std::vector<std::string>& stream_ids,
230 const std::vector<RtpEncodingParameters>* init_send_encodings);
Harald Alvestrande15fb152020-10-19 13:28:05231
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 Alvestrand35f4b4c2022-05-16 10:36:43247 cricket::MediaEngineInterface* media_engine() const;
248
Harald Alvestrandc3fa7c32022-05-22 10:57:01249 rtc::UniqueRandomIdGenerator* ssrc_generator() const {
250 return context_->ssrc_generator();
251 }
252
Harald Alvestrande15fb152020-10-19 13:28:05253 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 Alvestrandc3fa7c32022-05-22 10:57:01267 ConnectionContext* context_;
Harald Alvestrande15fb152020-10-19 13:28:05268 UsagePattern* usage_pattern_;
269 PeerConnectionObserver* observer_;
Henrik Boströmf7859892022-07-04 12:36:37270 LegacyStatsCollectorInterface* const legacy_stats_;
Harald Alvestrande15fb152020-10-19 13:28:05271 std::function<void()> on_negotiation_needed_;
Harald Alvestrand280054f2020-11-10 13:12:53272 rtc::WeakPtrFactory<RtpTransmissionManager> weak_ptr_factory_
273 RTC_GUARDED_BY(signaling_thread());
Harald Alvestrande15fb152020-10-19 13:28:05274};
275
276} // namespace webrtc
277
278#endif // PC_RTP_TRANSMISSION_MANAGER_H_