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 <map>
15 #include <memory>
16 #include <set>
17 #include <string>
18 #include <utility>
19 #include <vector>
20 
21 #include "api/call/audio_sink.h"
22 #include "api/function_view.h"
23 #include "api/jsep.h"
24 #include "api/rtp_receiver_interface.h"
25 #include "api/transport/media/media_transport_config.h"
26 #include "api/video/video_sink_interface.h"
27 #include "api/video/video_source_interface.h"
28 #include "call/rtp_packet_sink_interface.h"
29 #include "media/base/media_channel.h"
30 #include "media/base/media_engine.h"
31 #include "media/base/stream_params.h"
32 #include "p2p/base/dtls_transport_internal.h"
33 #include "p2p/base/packet_transport_internal.h"
34 #include "pc/channel_interface.h"
35 #include "pc/dtls_srtp_transport.h"
36 #include "pc/media_session.h"
37 #include "pc/rtp_transport.h"
38 #include "pc/srtp_filter.h"
39 #include "pc/srtp_transport.h"
40 #include "rtc_base/async_invoker.h"
41 #include "rtc_base/async_udp_socket.h"
42 #include "rtc_base/critical_section.h"
43 #include "rtc_base/network.h"
44 #include "rtc_base/third_party/sigslot/sigslot.h"
45 #include "rtc_base/unique_id_generator.h"
46 
47 namespace webrtc {
48 class AudioSinkInterface;
49 class MediaTransportInterface;
50 }  // namespace webrtc
51 
52 namespace cricket {
53 
54 struct CryptoParams;
55 
56 // BaseChannel contains logic common to voice and video, including enable,
57 // marshaling calls to a worker and network threads, and connection and media
58 // monitors.
59 //
60 // BaseChannel assumes signaling and other threads are allowed to make
61 // synchronous calls to the worker thread, the worker thread makes synchronous
62 // calls only to the network thread, and the network thread can't be blocked by
63 // other threads.
64 // All methods with _n suffix must be called on network thread,
65 //     methods with _w suffix on worker thread
66 // and methods with _s suffix on signaling thread.
67 // Network and worker threads may be the same thread.
68 //
69 // WARNING! SUBCLASSES MUST CALL Deinit() IN THEIR DESTRUCTORS!
70 // This is required to avoid a data race between the destructor modifying the
71 // vtable, and the media channel's thread using BaseChannel as the
72 // NetworkInterface.
73 
74 class BaseChannel : public ChannelInterface,
75                     public rtc::MessageHandler,
76                     public sigslot::has_slots<>,
77                     public MediaChannel::NetworkInterface,
78                     public webrtc::RtpPacketSinkInterface {
79  public:
80   // If |srtp_required| is true, the channel will not send or receive any
81   // RTP/RTCP packets without using SRTP (either using SDES or DTLS-SRTP).
82   // The BaseChannel does not own the UniqueRandomIdGenerator so it is the
83   // responsibility of the user to ensure it outlives this object.
84   // TODO(zhihuang:) Create a BaseChannel::Config struct for the parameter lists
85   // which will make it easier to change the constructor.
86   BaseChannel(rtc::Thread* worker_thread,
87               rtc::Thread* network_thread,
88               rtc::Thread* signaling_thread,
89               std::unique_ptr<MediaChannel> media_channel,
90               const std::string& content_name,
91               bool srtp_required,
92               webrtc::CryptoOptions crypto_options,
93               rtc::UniqueRandomIdGenerator* ssrc_generator);
94   virtual ~BaseChannel();
95   virtual void Init_w(
96       webrtc::RtpTransportInternal* rtp_transport,
97       const webrtc::MediaTransportConfig& media_transport_config);
98 
99   // Deinit may be called multiple times and is simply ignored if it's already
100   // done.
101   void Deinit();
102 
worker_thread()103   rtc::Thread* worker_thread() const { return worker_thread_; }
network_thread()104   rtc::Thread* network_thread() const { return network_thread_; }
content_name()105   const std::string& content_name() const override { return content_name_; }
106   // TODO(deadbeef): This is redundant; remove this.
transport_name()107   const std::string& transport_name() const override { return transport_name_; }
enabled()108   bool enabled() const override { return enabled_; }
109 
110   // This function returns true if using SRTP (DTLS-based keying or SDES).
srtp_active()111   bool srtp_active() const {
112     return rtp_transport_ && rtp_transport_->IsSrtpActive();
113   }
114 
writable()115   bool writable() const { return writable_; }
116 
117   // Set an RTP level transport which could be an RtpTransport without
118   // encryption, an SrtpTransport for SDES or a DtlsSrtpTransport for DTLS-SRTP.
119   // This can be called from any thread and it hops to the network thread
120   // internally. It would replace the |SetTransports| and its variants.
121   bool SetRtpTransport(webrtc::RtpTransportInternal* rtp_transport) override;
122 
rtp_transport()123   webrtc::RtpTransportInternal* rtp_transport() const { return rtp_transport_; }
124 
125   // Channel control
126   bool SetLocalContent(const MediaContentDescription* content,
127                        webrtc::SdpType type,
128                        std::string* error_desc) override;
129   bool SetRemoteContent(const MediaContentDescription* content,
130                         webrtc::SdpType type,
131                         std::string* error_desc) override;
132 
133   bool Enable(bool enable) override;
134 
local_streams()135   const std::vector<StreamParams>& local_streams() const override {
136     return local_streams_;
137   }
remote_streams()138   const std::vector<StreamParams>& remote_streams() const override {
139     return remote_streams_;
140   }
141 
142   sigslot::signal2<BaseChannel*, bool> SignalDtlsSrtpSetupFailure;
143   void SignalDtlsSrtpSetupFailure_n(bool rtcp);
144   void SignalDtlsSrtpSetupFailure_s(bool rtcp);
145 
146   // Used for latency measurements.
SignalFirstPacketReceived()147   sigslot::signal1<ChannelInterface*>& SignalFirstPacketReceived() override {
148     return SignalFirstPacketReceived_;
149   }
150 
151   // Forward SignalSentPacket to worker thread.
152   sigslot::signal1<const rtc::SentPacket&> SignalSentPacket;
153 
154   // Emitted whenever rtcp-mux is fully negotiated and the rtcp-transport can
155   // be destroyed.
156   // Fired on the network thread.
157   sigslot::signal1<const std::string&> SignalRtcpMuxFullyActive;
158 
159   // From RtpTransport - public for testing only
160   void OnTransportReadyToSend(bool ready);
161 
162   // Only public for unit tests.  Otherwise, consider protected.
163   int SetOption(SocketType type, rtc::Socket::Option o, int val) override;
164   int SetOption_n(SocketType type, rtc::Socket::Option o, int val);
165 
166   // RtpPacketSinkInterface overrides.
167   void OnRtpPacket(const webrtc::RtpPacketReceived& packet) override;
168 
169   // Used by the RTCStatsCollector tests to set the transport name without
170   // creating RtpTransports.
set_transport_name_for_testing(const std::string & transport_name)171   void set_transport_name_for_testing(const std::string& transport_name) {
172     transport_name_ = transport_name;
173   }
174 
media_channel()175   MediaChannel* media_channel() const override { return media_channel_.get(); }
176 
177  protected:
was_ever_writable()178   bool was_ever_writable() const { return was_ever_writable_; }
set_local_content_direction(webrtc::RtpTransceiverDirection direction)179   void set_local_content_direction(webrtc::RtpTransceiverDirection direction) {
180     local_content_direction_ = direction;
181   }
set_remote_content_direction(webrtc::RtpTransceiverDirection direction)182   void set_remote_content_direction(webrtc::RtpTransceiverDirection direction) {
183     remote_content_direction_ = direction;
184   }
185   // These methods verify that:
186   // * The required content description directions have been set.
187   // * The channel is enabled.
188   // * And for sending:
189   //   - The SRTP filter is active if it's needed.
190   //   - The transport has been writable before, meaning it should be at least
191   //     possible to succeed in sending a packet.
192   //
193   // When any of these properties change, UpdateMediaSendRecvState_w should be
194   // called.
195   bool IsReadyToReceiveMedia_w() const;
196   bool IsReadyToSendMedia_w() const;
signaling_thread()197   rtc::Thread* signaling_thread() { return signaling_thread_; }
198 
199   void FlushRtcpMessages_n();
200 
201   // NetworkInterface implementation, called by MediaEngine
202   bool SendPacket(rtc::CopyOnWriteBuffer* packet,
203                   const rtc::PacketOptions& options) override;
204   bool SendRtcp(rtc::CopyOnWriteBuffer* packet,
205                 const rtc::PacketOptions& options) override;
206 
207   // From RtpTransportInternal
208   void OnWritableState(bool writable);
209 
210   void OnNetworkRouteChanged(absl::optional<rtc::NetworkRoute> network_route);
211 
212   bool PacketIsRtcp(const rtc::PacketTransportInternal* transport,
213                     const char* data,
214                     size_t len);
215   bool SendPacket(bool rtcp,
216                   rtc::CopyOnWriteBuffer* packet,
217                   const rtc::PacketOptions& options);
218 
219   void EnableMedia_w();
220   void DisableMedia_w();
221 
222   // Performs actions if the RTP/RTCP writable state changed. This should
223   // be called whenever a channel's writable state changes or when RTCP muxing
224   // becomes active/inactive.
225   void UpdateWritableState_n();
226   void ChannelWritable_n();
227   void ChannelNotWritable_n();
228 
229   bool AddRecvStream_w(const StreamParams& sp);
230   bool RemoveRecvStream_w(uint32_t ssrc);
231   void ResetUnsignaledRecvStream_w();
232   bool AddSendStream_w(const StreamParams& sp);
233   bool RemoveSendStream_w(uint32_t ssrc);
234 
235   // Should be called whenever the conditions for
236   // IsReadyToReceiveMedia/IsReadyToSendMedia are satisfied (or unsatisfied).
237   // Updates the send/recv state of the media channel.
238   void UpdateMediaSendRecvState();
239   virtual void UpdateMediaSendRecvState_w() = 0;
240 
241   bool UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
242                             webrtc::SdpType type,
243                             std::string* error_desc);
244   bool UpdateRemoteStreams_w(const std::vector<StreamParams>& streams,
245                              webrtc::SdpType type,
246                              std::string* error_desc);
247   virtual bool SetLocalContent_w(const MediaContentDescription* content,
248                                  webrtc::SdpType type,
249                                  std::string* error_desc) = 0;
250   virtual bool SetRemoteContent_w(const MediaContentDescription* content,
251                                   webrtc::SdpType type,
252                                   std::string* error_desc) = 0;
253   // Return a list of RTP header extensions with the non-encrypted extensions
254   // removed depending on the current crypto_options_ and only if both the
255   // non-encrypted and encrypted extension is present for the same URI.
256   RtpHeaderExtensions GetFilteredRtpHeaderExtensions(
257       const RtpHeaderExtensions& extensions);
258 
259   // From MessageHandler
260   void OnMessage(rtc::Message* pmsg) override;
261 
262   // Helper function template for invoking methods on the worker thread.
263   template <class T>
InvokeOnWorker(const rtc::Location & posted_from,rtc::FunctionView<T ()> functor)264   T InvokeOnWorker(const rtc::Location& posted_from,
265                    rtc::FunctionView<T()> functor) {
266     return worker_thread_->Invoke<T>(posted_from, functor);
267   }
268 
269   void AddHandledPayloadType(int payload_type);
270 
271   void ClearHandledPayloadTypes();
272 
273   void UpdateRtpHeaderExtensionMap(
274       const RtpHeaderExtensions& header_extensions);
275 
276   bool RegisterRtpDemuxerSink();
277 
278   bool has_received_packet_ = false;
279 
280  private:
281   bool ConnectToRtpTransport();
282   void DisconnectFromRtpTransport();
283   void SignalSentPacket_n(const rtc::SentPacket& sent_packet);
284   bool IsReadyToSendMedia_n() const;
285 
286   rtc::Thread* const worker_thread_;
287   rtc::Thread* const network_thread_;
288   rtc::Thread* const signaling_thread_;
289   rtc::AsyncInvoker invoker_;
290   sigslot::signal1<ChannelInterface*> SignalFirstPacketReceived_;
291 
292   const std::string content_name_;
293 
294   // Won't be set when using raw packet transports. SDP-specific thing.
295   std::string transport_name_;
296 
297   webrtc::RtpTransportInternal* rtp_transport_ = nullptr;
298 
299   // Optional media transport configuration (experimental).
300   webrtc::MediaTransportConfig media_transport_config_;
301 
302   std::vector<std::pair<rtc::Socket::Option, int> > socket_options_;
303   std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_;
304   bool writable_ = false;
305   bool was_ever_writable_ = false;
306   const bool srtp_required_ = true;
307   webrtc::CryptoOptions crypto_options_;
308 
309   // MediaChannel related members that should be accessed from the worker
310   // thread.
311   std::unique_ptr<MediaChannel> media_channel_;
312   // Currently the |enabled_| flag is accessed from the signaling thread as
313   // well, but it can be changed only when signaling thread does a synchronous
314   // call to the worker thread, so it should be safe.
315   bool enabled_ = false;
316   std::vector<StreamParams> local_streams_;
317   std::vector<StreamParams> remote_streams_;
318   webrtc::RtpTransceiverDirection local_content_direction_ =
319       webrtc::RtpTransceiverDirection::kInactive;
320   webrtc::RtpTransceiverDirection remote_content_direction_ =
321       webrtc::RtpTransceiverDirection::kInactive;
322 
323   webrtc::RtpDemuxerCriteria demuxer_criteria_;
324   // This generator is used to generate SSRCs for local streams.
325   // This is needed in cases where SSRCs are not negotiated or set explicitly
326   // like in Simulcast.
327   // This object is not owned by the channel so it must outlive it.
328   rtc::UniqueRandomIdGenerator* const ssrc_generator_;
329 };
330 
331 // VoiceChannel is a specialization that adds support for early media, DTMF,
332 // and input/output level monitoring.
333 class VoiceChannel : public BaseChannel {
334  public:
335   VoiceChannel(rtc::Thread* worker_thread,
336                rtc::Thread* network_thread,
337                rtc::Thread* signaling_thread,
338                std::unique_ptr<VoiceMediaChannel> channel,
339                const std::string& content_name,
340                bool srtp_required,
341                webrtc::CryptoOptions crypto_options,
342                rtc::UniqueRandomIdGenerator* ssrc_generator);
343   ~VoiceChannel();
344 
345   // downcasts a MediaChannel
media_channel()346   VoiceMediaChannel* media_channel() const override {
347     return static_cast<VoiceMediaChannel*>(BaseChannel::media_channel());
348   }
349 
media_type()350   cricket::MediaType media_type() const override {
351     return cricket::MEDIA_TYPE_AUDIO;
352   }
353   void Init_w(
354       webrtc::RtpTransportInternal* rtp_transport,
355       const webrtc::MediaTransportConfig& media_transport_config) override;
356 
357  private:
358   // overrides from BaseChannel
359   void UpdateMediaSendRecvState_w() override;
360   bool SetLocalContent_w(const MediaContentDescription* content,
361                          webrtc::SdpType type,
362                          std::string* error_desc) override;
363   bool SetRemoteContent_w(const MediaContentDescription* content,
364                           webrtc::SdpType type,
365                           std::string* error_desc) override;
366 
367   // Last AudioSendParameters sent down to the media_channel() via
368   // SetSendParameters.
369   AudioSendParameters last_send_params_;
370   // Last AudioRecvParameters sent down to the media_channel() via
371   // SetRecvParameters.
372   AudioRecvParameters last_recv_params_;
373 };
374 
375 // VideoChannel is a specialization for video.
376 class VideoChannel : public BaseChannel {
377  public:
378   VideoChannel(rtc::Thread* worker_thread,
379                rtc::Thread* network_thread,
380                rtc::Thread* signaling_thread,
381                std::unique_ptr<VideoMediaChannel> media_channel,
382                const std::string& content_name,
383                bool srtp_required,
384                webrtc::CryptoOptions crypto_options,
385                rtc::UniqueRandomIdGenerator* ssrc_generator);
386   ~VideoChannel();
387 
388   // downcasts a MediaChannel
media_channel()389   VideoMediaChannel* media_channel() const override {
390     return static_cast<VideoMediaChannel*>(BaseChannel::media_channel());
391   }
392 
393   void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
394 
media_type()395   cricket::MediaType media_type() const override {
396     return cricket::MEDIA_TYPE_VIDEO;
397   }
398 
399  private:
400   // overrides from BaseChannel
401   void UpdateMediaSendRecvState_w() override;
402   bool SetLocalContent_w(const MediaContentDescription* content,
403                          webrtc::SdpType type,
404                          std::string* error_desc) override;
405   bool SetRemoteContent_w(const MediaContentDescription* content,
406                           webrtc::SdpType type,
407                           std::string* error_desc) override;
408 
409   // Last VideoSendParameters sent down to the media_channel() via
410   // SetSendParameters.
411   VideoSendParameters last_send_params_;
412   // Last VideoRecvParameters sent down to the media_channel() via
413   // SetRecvParameters.
414   VideoRecvParameters last_recv_params_;
415 };
416 
417 // RtpDataChannel is a specialization for data.
418 class RtpDataChannel : public BaseChannel {
419  public:
420   RtpDataChannel(rtc::Thread* worker_thread,
421                  rtc::Thread* network_thread,
422                  rtc::Thread* signaling_thread,
423                  std::unique_ptr<DataMediaChannel> channel,
424                  const std::string& content_name,
425                  bool srtp_required,
426                  webrtc::CryptoOptions crypto_options,
427                  rtc::UniqueRandomIdGenerator* ssrc_generator);
428   ~RtpDataChannel();
429   // TODO(zhihuang): Remove this once the RtpTransport can be shared between
430   // BaseChannels.
431   void Init_w(DtlsTransportInternal* rtp_dtls_transport,
432               DtlsTransportInternal* rtcp_dtls_transport,
433               rtc::PacketTransportInternal* rtp_packet_transport,
434               rtc::PacketTransportInternal* rtcp_packet_transport);
435   void Init_w(
436       webrtc::RtpTransportInternal* rtp_transport,
437       const webrtc::MediaTransportConfig& media_transport_config) override;
438 
439   virtual bool SendData(const SendDataParams& params,
440                         const rtc::CopyOnWriteBuffer& payload,
441                         SendDataResult* result);
442 
443   // Should be called on the signaling thread only.
ready_to_send_data()444   bool ready_to_send_data() const { return ready_to_send_data_; }
445 
446   sigslot::signal2<const ReceiveDataParams&, const rtc::CopyOnWriteBuffer&>
447       SignalDataReceived;
448   // Signal for notifying when the channel becomes ready to send data.
449   // That occurs when the channel is enabled, the transport is writable,
450   // both local and remote descriptions are set, and the channel is unblocked.
451   sigslot::signal1<bool> SignalReadyToSendData;
media_type()452   cricket::MediaType media_type() const override {
453     return cricket::MEDIA_TYPE_DATA;
454   }
455 
456  protected:
457   // downcasts a MediaChannel.
media_channel()458   DataMediaChannel* media_channel() const override {
459     return static_cast<DataMediaChannel*>(BaseChannel::media_channel());
460   }
461 
462  private:
463   struct SendDataMessageData : public rtc::MessageData {
SendDataMessageDataSendDataMessageData464     SendDataMessageData(const SendDataParams& params,
465                         const rtc::CopyOnWriteBuffer* payload,
466                         SendDataResult* result)
467         : params(params), payload(payload), result(result), succeeded(false) {}
468 
469     const SendDataParams& params;
470     const rtc::CopyOnWriteBuffer* payload;
471     SendDataResult* result;
472     bool succeeded;
473   };
474 
475   struct DataReceivedMessageData : public rtc::MessageData {
476     // We copy the data because the data will become invalid after we
477     // handle DataMediaChannel::SignalDataReceived but before we fire
478     // SignalDataReceived.
DataReceivedMessageDataDataReceivedMessageData479     DataReceivedMessageData(const ReceiveDataParams& params,
480                             const char* data,
481                             size_t len)
482         : params(params), payload(data, len) {}
483     const ReceiveDataParams params;
484     const rtc::CopyOnWriteBuffer payload;
485   };
486 
487   typedef rtc::TypedMessageData<bool> DataChannelReadyToSendMessageData;
488 
489   // overrides from BaseChannel
490   // Checks that data channel type is RTP.
491   bool CheckDataChannelTypeFromContent(const MediaContentDescription* content,
492                                        std::string* error_desc);
493   bool SetLocalContent_w(const MediaContentDescription* content,
494                          webrtc::SdpType type,
495                          std::string* error_desc) override;
496   bool SetRemoteContent_w(const MediaContentDescription* content,
497                           webrtc::SdpType type,
498                           std::string* error_desc) override;
499   void UpdateMediaSendRecvState_w() override;
500 
501   void OnMessage(rtc::Message* pmsg) override;
502   void OnDataReceived(const ReceiveDataParams& params,
503                       const char* data,
504                       size_t len);
505   void OnDataChannelReadyToSend(bool writable);
506 
507   bool ready_to_send_data_ = false;
508 
509   // Last DataSendParameters sent down to the media_channel() via
510   // SetSendParameters.
511   DataSendParameters last_send_params_;
512   // Last DataRecvParameters sent down to the media_channel() via
513   // SetRecvParameters.
514   DataRecvParameters last_recv_params_;
515 };
516 
517 }  // namespace cricket
518 
519 #endif  // PC_CHANNEL_H_
520