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