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