xref: /aosp_15_r20/external/webrtc/pc/channel.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2004 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_CHANNEL_H_
12 #define PC_CHANNEL_H_
13 
14 #include <stdint.h>
15 
16 #include <functional>
17 #include <memory>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
22 #include "absl/strings/string_view.h"
23 #include "absl/types/optional.h"
24 #include "api/crypto/crypto_options.h"
25 #include "api/jsep.h"
26 #include "api/media_types.h"
27 #include "api/rtp_parameters.h"
28 #include "api/rtp_transceiver_direction.h"
29 #include "api/scoped_refptr.h"
30 #include "api/sequence_checker.h"
31 #include "api/task_queue/pending_task_safety_flag.h"
32 #include "call/rtp_demuxer.h"
33 #include "call/rtp_packet_sink_interface.h"
34 #include "media/base/media_channel.h"
35 #include "media/base/stream_params.h"
36 #include "modules/rtp_rtcp/source/rtp_packet_received.h"
37 #include "pc/channel_interface.h"
38 #include "pc/rtp_transport_internal.h"
39 #include "pc/session_description.h"
40 #include "rtc_base/async_packet_socket.h"
41 #include "rtc_base/checks.h"
42 #include "rtc_base/containers/flat_set.h"
43 #include "rtc_base/copy_on_write_buffer.h"
44 #include "rtc_base/network/sent_packet.h"
45 #include "rtc_base/network_route.h"
46 #include "rtc_base/socket.h"
47 #include "rtc_base/third_party/sigslot/sigslot.h"
48 #include "rtc_base/thread.h"
49 #include "rtc_base/thread_annotations.h"
50 #include "rtc_base/unique_id_generator.h"
51 
52 namespace cricket {
53 
54 // BaseChannel contains logic common to voice and video, including enable,
55 // marshaling calls to a worker and network threads, and connection and media
56 // monitors.
57 //
58 // BaseChannel assumes signaling and other threads are allowed to make
59 // synchronous calls to the worker thread, the worker thread makes synchronous
60 // calls only to the network thread, and the network thread can't be blocked by
61 // other threads.
62 // All methods with _n suffix must be called on network thread,
63 //     methods with _w suffix on worker thread
64 // and methods with _s suffix on signaling thread.
65 // Network and worker threads may be the same thread.
66 //
67 
68 class BaseChannel : public ChannelInterface,
69                     // TODO(tommi): Remove has_slots inheritance.
70                     public sigslot::has_slots<>,
71                     // TODO(tommi): Consider implementing these interfaces
72                     // via composition.
73                     public MediaChannel::NetworkInterface,
74                     public webrtc::RtpPacketSinkInterface {
75  public:
76   // If `srtp_required` is true, the channel will not send or receive any
77   // RTP/RTCP packets without using SRTP (either using SDES or DTLS-SRTP).
78   // The BaseChannel does not own the UniqueRandomIdGenerator so it is the
79   // responsibility of the user to ensure it outlives this object.
80   // TODO(zhihuang:) Create a BaseChannel::Config struct for the parameter lists
81   // which will make it easier to change the constructor.
82   BaseChannel(rtc::Thread* worker_thread,
83               rtc::Thread* network_thread,
84               rtc::Thread* signaling_thread,
85               std::unique_ptr<MediaChannel> media_channel,
86               absl::string_view mid,
87               bool srtp_required,
88               webrtc::CryptoOptions crypto_options,
89               rtc::UniqueRandomIdGenerator* ssrc_generator);
90   virtual ~BaseChannel();
91 
worker_thread()92   rtc::Thread* worker_thread() const { return worker_thread_; }
network_thread()93   rtc::Thread* network_thread() const { return network_thread_; }
mid()94   const std::string& mid() const override { return demuxer_criteria_.mid(); }
95   // TODO(deadbeef): This is redundant; remove this.
transport_name()96   absl::string_view transport_name() const override {
97     RTC_DCHECK_RUN_ON(network_thread());
98     if (rtp_transport_)
99       return rtp_transport_->transport_name();
100     return "";
101   }
102 
103   // This function returns true if using SRTP (DTLS-based keying or SDES).
srtp_active()104   bool srtp_active() const {
105     RTC_DCHECK_RUN_ON(network_thread());
106     return rtp_transport_ && rtp_transport_->IsSrtpActive();
107   }
108 
109   // Set an RTP level transport which could be an RtpTransport without
110   // encryption, an SrtpTransport for SDES or a DtlsSrtpTransport for DTLS-SRTP.
111   // This can be called from any thread and it hops to the network thread
112   // internally. It would replace the `SetTransports` and its variants.
113   bool SetRtpTransport(webrtc::RtpTransportInternal* rtp_transport) override;
114 
rtp_transport()115   webrtc::RtpTransportInternal* rtp_transport() const {
116     RTC_DCHECK_RUN_ON(network_thread());
117     return rtp_transport_;
118   }
119 
120   // Channel control
121   bool SetLocalContent(const MediaContentDescription* content,
122                        webrtc::SdpType type,
123                        std::string& error_desc) override;
124   bool SetRemoteContent(const MediaContentDescription* content,
125                         webrtc::SdpType type,
126                         std::string& error_desc) override;
127   // Controls whether this channel will receive packets on the basis of
128   // matching payload type alone. This is needed for legacy endpoints that
129   // don't signal SSRCs or use MID/RID, but doesn't make sense if there is
130   // more than channel of specific media type, As that creates an ambiguity.
131   //
132   // This method will also remove any existing streams that were bound to this
133   // channel on the basis of payload type, since one of these streams might
134   // actually belong to a new channel. See: crbug.com/webrtc/11477
135   bool SetPayloadTypeDemuxingEnabled(bool enabled) override;
136 
137   void Enable(bool enable) override;
138 
local_streams()139   const std::vector<StreamParams>& local_streams() const override {
140     return local_streams_;
141   }
remote_streams()142   const std::vector<StreamParams>& remote_streams() const override {
143     return remote_streams_;
144   }
145 
146   // Used for latency measurements.
147   void SetFirstPacketReceivedCallback(std::function<void()> callback) override;
148 
149   // From RtpTransport - public for testing only
150   void OnTransportReadyToSend(bool ready);
151 
152   // Only public for unit tests.  Otherwise, consider protected.
153   int SetOption(SocketType type, rtc::Socket::Option o, int val) override;
154 
155   // RtpPacketSinkInterface overrides.
156   void OnRtpPacket(const webrtc::RtpPacketReceived& packet) override;
157 
media_channel()158   MediaChannel* media_channel() const override {
159     return media_channel_.get();
160   }
video_media_channel()161   VideoMediaChannel* video_media_channel() const override {
162     RTC_CHECK(false) << "Attempt to fetch video channel from non-video";
163     return nullptr;
164   }
voice_media_channel()165   VoiceMediaChannel* voice_media_channel() const override {
166     RTC_CHECK(false) << "Attempt to fetch voice channel from non-voice";
167     return nullptr;
168   }
169 
170  protected:
set_local_content_direction(webrtc::RtpTransceiverDirection direction)171   void set_local_content_direction(webrtc::RtpTransceiverDirection direction)
172       RTC_RUN_ON(worker_thread()) {
173     local_content_direction_ = direction;
174   }
175 
local_content_direction()176   webrtc::RtpTransceiverDirection local_content_direction() const
177       RTC_RUN_ON(worker_thread()) {
178     return local_content_direction_;
179   }
180 
set_remote_content_direction(webrtc::RtpTransceiverDirection direction)181   void set_remote_content_direction(webrtc::RtpTransceiverDirection direction)
182       RTC_RUN_ON(worker_thread()) {
183     remote_content_direction_ = direction;
184   }
185 
remote_content_direction()186   webrtc::RtpTransceiverDirection remote_content_direction() const
187       RTC_RUN_ON(worker_thread()) {
188     return remote_content_direction_;
189   }
190 
extensions_filter()191   webrtc::RtpExtension::Filter extensions_filter() const {
192     return extensions_filter_;
193   }
194 
network_initialized()195   bool network_initialized() RTC_RUN_ON(network_thread()) {
196     return media_channel_->HasNetworkInterface();
197   }
198 
enabled()199   bool enabled() const RTC_RUN_ON(worker_thread()) { return enabled_; }
signaling_thread()200   rtc::Thread* signaling_thread() const { return signaling_thread_; }
201 
202   // Call to verify that:
203   // * The required content description directions have been set.
204   // * The channel is enabled.
205   // * The SRTP filter is active if it's needed.
206   // * The transport has been writable before, meaning it should be at least
207   //   possible to succeed in sending a packet.
208   //
209   // When any of these properties change, UpdateMediaSendRecvState_w should be
210   // called.
211   bool IsReadyToSendMedia_w() const RTC_RUN_ON(worker_thread());
212 
213   // NetworkInterface implementation, called by MediaEngine
214   bool SendPacket(rtc::CopyOnWriteBuffer* packet,
215                   const rtc::PacketOptions& options) override;
216   bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
217                 const rtc::PacketOptions& options) override;
218 
219   // From RtpTransportInternal
220   void OnWritableState(bool writable);
221 
222   void OnNetworkRouteChanged(absl::optional<rtc::NetworkRoute> network_route);
223 
224   bool SendPacket(bool rtcp,
225                   rtc::CopyOnWriteBuffer* packet,
226                   const rtc::PacketOptions& options);
227 
228   void EnableMedia_w() RTC_RUN_ON(worker_thread());
229   void DisableMedia_w() RTC_RUN_ON(worker_thread());
230 
231   // Performs actions if the RTP/RTCP writable state changed. This should
232   // be called whenever a channel's writable state changes or when RTCP muxing
233   // becomes active/inactive.
234   void UpdateWritableState_n() RTC_RUN_ON(network_thread());
235   void ChannelWritable_n() RTC_RUN_ON(network_thread());
236   void ChannelNotWritable_n() RTC_RUN_ON(network_thread());
237 
238   bool SetPayloadTypeDemuxingEnabled_w(bool enabled)
239       RTC_RUN_ON(worker_thread());
240 
241   // Should be called whenever the conditions for
242   // IsReadyToReceiveMedia/IsReadyToSendMedia are satisfied (or unsatisfied).
243   // Updates the send/recv state of the media channel.
244   virtual void UpdateMediaSendRecvState_w() RTC_RUN_ON(worker_thread()) = 0;
245 
246   bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
247                             webrtc::SdpType type,
248                             std::string& error_desc)
249       RTC_RUN_ON(worker_thread());
250   bool UpdateRemoteStreams_w(const MediaContentDescription* content,
251                              webrtc::SdpType type,
252                              std::string& error_desc)
253       RTC_RUN_ON(worker_thread());
254   virtual bool SetLocalContent_w(const MediaContentDescription* content,
255                                  webrtc::SdpType type,
256                                  std::string& error_desc)
257       RTC_RUN_ON(worker_thread()) = 0;
258   virtual bool SetRemoteContent_w(const MediaContentDescription* content,
259                                   webrtc::SdpType type,
260                                   std::string& error_desc)
261       RTC_RUN_ON(worker_thread()) = 0;
262 
263   // Returns a list of RTP header extensions where any extension URI is unique.
264   // Encrypted extensions will be either preferred or discarded, depending on
265   // the current crypto_options_.
266   RtpHeaderExtensions GetDeduplicatedRtpHeaderExtensions(
267       const RtpHeaderExtensions& extensions);
268 
269   // Add `payload_type` to `demuxer_criteria_` if payload type demuxing is
270   // enabled.
271   // Returns true if the demuxer payload type changed and a re-registration
272   // is needed.
273   bool MaybeAddHandledPayloadType(int payload_type) RTC_RUN_ON(worker_thread());
274 
275   // Returns true if the demuxer payload type criteria was non-empty before
276   // clearing.
277   bool ClearHandledPayloadTypes() RTC_RUN_ON(worker_thread());
278 
279   // Hops to the network thread to update the transport if an update is
280   // requested. If `update_demuxer` is false and `extensions` is not set, the
281   // function simply returns. If either of these is set, the function updates
282   // the transport with either or both of the demuxer criteria and the supplied
283   // rtp header extensions.
284   // Returns `true` if either an update wasn't needed or one was successfully
285   // applied. If the return value is `false`, then updating the demuxer criteria
286   // failed, which needs to be treated as an error.
287   bool MaybeUpdateDemuxerAndRtpExtensions_w(
288       bool update_demuxer,
289       absl::optional<RtpHeaderExtensions> extensions,
290       std::string& error_desc) RTC_RUN_ON(worker_thread());
291 
292   bool RegisterRtpDemuxerSink_w() RTC_RUN_ON(worker_thread());
293 
294   // Return description of media channel to facilitate logging
295   std::string ToString() const;
296 
297  private:
298   bool ConnectToRtpTransport_n() RTC_RUN_ON(network_thread());
299   void DisconnectFromRtpTransport_n() RTC_RUN_ON(network_thread());
300   void SignalSentPacket_n(const rtc::SentPacket& sent_packet);
301 
302   rtc::Thread* const worker_thread_;
303   rtc::Thread* const network_thread_;
304   rtc::Thread* const signaling_thread_;
305   rtc::scoped_refptr<webrtc::PendingTaskSafetyFlag> alive_;
306 
307   std::function<void()> on_first_packet_received_
308       RTC_GUARDED_BY(network_thread());
309 
310   webrtc::RtpTransportInternal* rtp_transport_
311       RTC_GUARDED_BY(network_thread()) = nullptr;
312 
313   std::vector<std::pair<rtc::Socket::Option, int> > socket_options_
314       RTC_GUARDED_BY(network_thread());
315   std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_
316       RTC_GUARDED_BY(network_thread());
317   bool writable_ RTC_GUARDED_BY(network_thread()) = false;
318   bool was_ever_writable_n_ RTC_GUARDED_BY(network_thread()) = false;
319   bool was_ever_writable_ RTC_GUARDED_BY(worker_thread()) = false;
320   const bool srtp_required_ = true;
321 
322   // Set to either kPreferEncryptedExtension or kDiscardEncryptedExtension
323   // based on the supplied CryptoOptions.
324   const webrtc::RtpExtension::Filter extensions_filter_;
325 
326   // MediaChannel related members that should be accessed from the worker
327   // thread.
328   const std::unique_ptr<MediaChannel> media_channel_;
329   // Currently the `enabled_` flag is accessed from the signaling thread as
330   // well, but it can be changed only when signaling thread does a synchronous
331   // call to the worker thread, so it should be safe.
332   bool enabled_ RTC_GUARDED_BY(worker_thread()) = false;
333   bool enabled_s_ RTC_GUARDED_BY(signaling_thread()) = false;
334   bool payload_type_demuxing_enabled_ RTC_GUARDED_BY(worker_thread()) = true;
335   std::vector<StreamParams> local_streams_ RTC_GUARDED_BY(worker_thread());
336   std::vector<StreamParams> remote_streams_ RTC_GUARDED_BY(worker_thread());
337   webrtc::RtpTransceiverDirection local_content_direction_ RTC_GUARDED_BY(
338       worker_thread()) = webrtc::RtpTransceiverDirection::kInactive;
339   webrtc::RtpTransceiverDirection remote_content_direction_ RTC_GUARDED_BY(
340       worker_thread()) = webrtc::RtpTransceiverDirection::kInactive;
341 
342   // Cached list of payload types, used if payload type demuxing is re-enabled.
343   webrtc::flat_set<uint8_t> payload_types_ RTC_GUARDED_BY(worker_thread());
344   // A stored copy of the rtp header extensions as applied to the transport.
345   RtpHeaderExtensions rtp_header_extensions_ RTC_GUARDED_BY(worker_thread());
346   // TODO(bugs.webrtc.org/12239): Modified on worker thread, accessed
347   // on network thread in RegisterRtpDemuxerSink_n (called from Init_w)
348   webrtc::RtpDemuxerCriteria demuxer_criteria_;
349   // This generator is used to generate SSRCs for local streams.
350   // This is needed in cases where SSRCs are not negotiated or set explicitly
351   // like in Simulcast.
352   // This object is not owned by the channel so it must outlive it.
353   rtc::UniqueRandomIdGenerator* const ssrc_generator_;
354 };
355 
356 // VoiceChannel is a specialization that adds support for early media, DTMF,
357 // and input/output level monitoring.
358 class VoiceChannel : public BaseChannel {
359  public:
360   VoiceChannel(rtc::Thread* worker_thread,
361                rtc::Thread* network_thread,
362                rtc::Thread* signaling_thread,
363                std::unique_ptr<VoiceMediaChannel> channel,
364                absl::string_view mid,
365                bool srtp_required,
366                webrtc::CryptoOptions crypto_options,
367                rtc::UniqueRandomIdGenerator* ssrc_generator);
368   ~VoiceChannel();
369 
370   // downcasts a MediaChannel
media_channel()371   VoiceMediaChannel* media_channel() const override {
372     return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
373   }
374 
voice_media_channel()375   VoiceMediaChannel* voice_media_channel() const override {
376     return static_cast<VoiceMediaChannel*>(media_channel());
377   }
378 
media_type()379   cricket::MediaType media_type() const override {
380     return cricket::MEDIA_TYPE_AUDIO;
381   }
382 
383  private:
384   // overrides from BaseChannel
385   void UpdateMediaSendRecvState_w() RTC_RUN_ON(worker_thread()) override;
386   bool SetLocalContent_w(const MediaContentDescription* content,
387                          webrtc::SdpType type,
388                          std::string& error_desc)
389       RTC_RUN_ON(worker_thread()) override;
390   bool SetRemoteContent_w(const MediaContentDescription* content,
391                           webrtc::SdpType type,
392                           std::string& error_desc)
393       RTC_RUN_ON(worker_thread()) override;
394 
395   // Last AudioSendParameters sent down to the media_channel() via
396   // SetSendParameters.
397   AudioSendParameters last_send_params_ RTC_GUARDED_BY(worker_thread());
398   // Last AudioRecvParameters sent down to the media_channel() via
399   // SetRecvParameters.
400   AudioRecvParameters last_recv_params_ RTC_GUARDED_BY(worker_thread());
401 };
402 
403 // VideoChannel is a specialization for video.
404 class VideoChannel : public BaseChannel {
405  public:
406   VideoChannel(rtc::Thread* worker_thread,
407                rtc::Thread* network_thread,
408                rtc::Thread* signaling_thread,
409                std::unique_ptr<VideoMediaChannel> media_channel,
410                absl::string_view mid,
411                bool srtp_required,
412                webrtc::CryptoOptions crypto_options,
413                rtc::UniqueRandomIdGenerator* ssrc_generator);
414   ~VideoChannel();
415 
416   // downcasts a MediaChannel
media_channel()417   VideoMediaChannel* media_channel() const override {
418     return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
419   }
420 
video_media_channel()421   VideoMediaChannel* video_media_channel() const override {
422     return static_cast<cricket::VideoMediaChannel*>(media_channel());
423   }
424 
media_type()425   cricket::MediaType media_type() const override {
426     return cricket::MEDIA_TYPE_VIDEO;
427   }
428 
429  private:
430   // overrides from BaseChannel
431   void UpdateMediaSendRecvState_w() RTC_RUN_ON(worker_thread()) override;
432   bool SetLocalContent_w(const MediaContentDescription* content,
433                          webrtc::SdpType type,
434                          std::string& error_desc)
435       RTC_RUN_ON(worker_thread()) override;
436   bool SetRemoteContent_w(const MediaContentDescription* content,
437                           webrtc::SdpType type,
438                           std::string& error_desc)
439       RTC_RUN_ON(worker_thread()) override;
440 
441   // Last VideoSendParameters sent down to the media_channel() via
442   // SetSendParameters.
443   VideoSendParameters last_send_params_ RTC_GUARDED_BY(worker_thread());
444   // Last VideoRecvParameters sent down to the media_channel() via
445   // SetRecvParameters.
446   VideoRecvParameters last_recv_params_ RTC_GUARDED_BY(worker_thread());
447 };
448 
449 }  // namespace cricket
450 
451 #endif  // PC_CHANNEL_H_
452