data_channel_controller.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright 2019 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. #ifndef PC_DATA_CHANNEL_CONTROLLER_H_
  11. #define PC_DATA_CHANNEL_CONTROLLER_H_
  12. #include <map>
  13. #include <memory>
  14. #include <string>
  15. #include <vector>
  16. #include "pc/channel.h"
  17. #include "pc/rtp_data_channel.h"
  18. #include "pc/sctp_data_channel.h"
  19. #include "rtc_base/weak_ptr.h"
  20. namespace webrtc {
  21. class PeerConnection;
  22. class DataChannelController : public RtpDataChannelProviderInterface,
  23. public SctpDataChannelProviderInterface,
  24. public DataChannelSink {
  25. public:
  26. explicit DataChannelController(PeerConnection* pc) : pc_(pc) {}
  27. // Not copyable or movable.
  28. DataChannelController(DataChannelController&) = delete;
  29. DataChannelController& operator=(const DataChannelController& other) = delete;
  30. DataChannelController(DataChannelController&&) = delete;
  31. DataChannelController& operator=(DataChannelController&& other) = delete;
  32. // Implements RtpDataChannelProviderInterface/
  33. // SctpDataChannelProviderInterface.
  34. bool SendData(const cricket::SendDataParams& params,
  35. const rtc::CopyOnWriteBuffer& payload,
  36. cricket::SendDataResult* result) override;
  37. bool ConnectDataChannel(RtpDataChannel* webrtc_data_channel) override;
  38. void DisconnectDataChannel(RtpDataChannel* webrtc_data_channel) override;
  39. bool ConnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
  40. void DisconnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
  41. void AddSctpDataStream(int sid) override;
  42. void RemoveSctpDataStream(int sid) override;
  43. bool ReadyToSendData() const override;
  44. // Implements DataChannelSink.
  45. void OnDataReceived(int channel_id,
  46. DataMessageType type,
  47. const rtc::CopyOnWriteBuffer& buffer) override;
  48. void OnChannelClosing(int channel_id) override;
  49. void OnChannelClosed(int channel_id) override;
  50. void OnReadyToSend() override;
  51. void OnTransportClosed() override;
  52. // Called from PeerConnection::SetupDataChannelTransport_n
  53. void SetupDataChannelTransport_n();
  54. // Called from PeerConnection::TeardownDataChannelTransport_n
  55. void TeardownDataChannelTransport_n();
  56. // Called from PeerConnection::OnTransportChanged
  57. // to make required changes to datachannels' transports.
  58. void OnTransportChanged(
  59. DataChannelTransportInterface* data_channel_transport);
  60. // Called from PeerConnection::GetDataChannelStats on the signaling thread.
  61. std::vector<DataChannelStats> GetDataChannelStats() const;
  62. // Creates channel and adds it to the collection of DataChannels that will
  63. // be offered in a SessionDescription, and wraps it in a proxy object.
  64. rtc::scoped_refptr<DataChannelInterface> InternalCreateDataChannelWithProxy(
  65. const std::string& label,
  66. const InternalDataChannelInit*
  67. config) /* RTC_RUN_ON(signaling_thread()) */;
  68. void AllocateSctpSids(rtc::SSLRole role);
  69. SctpDataChannel* FindDataChannelBySid(int sid) const;
  70. // Checks if any data channel has been added.
  71. bool HasDataChannels() const;
  72. bool HasSctpDataChannels() const {
  73. RTC_DCHECK_RUN_ON(signaling_thread());
  74. return !sctp_data_channels_.empty();
  75. }
  76. bool HasRtpDataChannels() const {
  77. RTC_DCHECK_RUN_ON(signaling_thread());
  78. return !rtp_data_channels_.empty();
  79. }
  80. void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
  81. void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams);
  82. // Accessors
  83. cricket::DataChannelType data_channel_type() const;
  84. void set_data_channel_type(cricket::DataChannelType type);
  85. cricket::RtpDataChannel* rtp_data_channel() const {
  86. return rtp_data_channel_;
  87. }
  88. void set_rtp_data_channel(cricket::RtpDataChannel* channel) {
  89. rtp_data_channel_ = channel;
  90. }
  91. DataChannelTransportInterface* data_channel_transport() const;
  92. void set_data_channel_transport(DataChannelTransportInterface* transport);
  93. const std::map<std::string, rtc::scoped_refptr<RtpDataChannel>>*
  94. rtp_data_channels() const;
  95. sigslot::signal1<RtpDataChannel*>& SignalRtpDataChannelCreated() {
  96. RTC_DCHECK_RUN_ON(signaling_thread());
  97. return SignalRtpDataChannelCreated_;
  98. }
  99. sigslot::signal1<SctpDataChannel*>& SignalSctpDataChannelCreated() {
  100. RTC_DCHECK_RUN_ON(signaling_thread());
  101. return SignalSctpDataChannelCreated_;
  102. }
  103. // Called when the transport for the data channels is closed or destroyed.
  104. void OnTransportChannelClosed();
  105. void OnSctpDataChannelClosed(SctpDataChannel* channel);
  106. private:
  107. rtc::scoped_refptr<RtpDataChannel> InternalCreateRtpDataChannel(
  108. const std::string& label,
  109. const DataChannelInit* config) /* RTC_RUN_ON(signaling_thread()) */;
  110. rtc::scoped_refptr<SctpDataChannel> InternalCreateSctpDataChannel(
  111. const std::string& label,
  112. const InternalDataChannelInit*
  113. config) /* RTC_RUN_ON(signaling_thread()) */;
  114. // Parses and handles open messages. Returns true if the message is an open
  115. // message, false otherwise.
  116. bool HandleOpenMessage_s(const cricket::ReceiveDataParams& params,
  117. const rtc::CopyOnWriteBuffer& buffer)
  118. RTC_RUN_ON(signaling_thread());
  119. // Called when a valid data channel OPEN message is received.
  120. void OnDataChannelOpenMessage(const std::string& label,
  121. const InternalDataChannelInit& config)
  122. RTC_RUN_ON(signaling_thread());
  123. void CreateRemoteRtpDataChannel(const std::string& label,
  124. uint32_t remote_ssrc)
  125. RTC_RUN_ON(signaling_thread());
  126. void UpdateClosingRtpDataChannels(
  127. const std::vector<std::string>& active_channels,
  128. bool is_local_update) RTC_RUN_ON(signaling_thread());
  129. // Called from SendData when data_channel_transport() is true.
  130. bool DataChannelSendData(const cricket::SendDataParams& params,
  131. const rtc::CopyOnWriteBuffer& payload,
  132. cricket::SendDataResult* result);
  133. // Called when all data channels need to be notified of a transport channel
  134. // (calls OnTransportChannelCreated on the signaling thread).
  135. void NotifyDataChannelsOfTransportCreated();
  136. rtc::Thread* network_thread() const;
  137. rtc::Thread* signaling_thread() const;
  138. // Specifies which kind of data channel is allowed. This is controlled
  139. // by the chrome command-line flag and constraints:
  140. // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
  141. // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
  142. // not set or false, SCTP is allowed (DCT_SCTP);
  143. // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
  144. // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
  145. cricket::DataChannelType data_channel_type_ =
  146. cricket::DCT_NONE; // TODO(bugs.webrtc.org/9987): Accessed on both
  147. // signaling and network thread.
  148. // Plugin transport used for data channels. Pointer may be accessed and
  149. // checked from any thread, but the object may only be touched on the
  150. // network thread.
  151. // TODO(bugs.webrtc.org/9987): Accessed on both signaling and network
  152. // thread.
  153. DataChannelTransportInterface* data_channel_transport_ = nullptr;
  154. // Cached value of whether the data channel transport is ready to send.
  155. bool data_channel_transport_ready_to_send_
  156. RTC_GUARDED_BY(signaling_thread()) = false;
  157. // |rtp_data_channel_| is used if in RTP data channel mode,
  158. // |data_channel_transport_| when using SCTP.
  159. cricket::RtpDataChannel* rtp_data_channel_ = nullptr;
  160. // TODO(bugs.webrtc.org/9987): Accessed on both
  161. // signaling and some other thread.
  162. SctpSidAllocator sid_allocator_ /* RTC_GUARDED_BY(signaling_thread()) */;
  163. std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_
  164. RTC_GUARDED_BY(signaling_thread());
  165. std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_to_free_
  166. RTC_GUARDED_BY(signaling_thread());
  167. // Map of label -> DataChannel
  168. std::map<std::string, rtc::scoped_refptr<RtpDataChannel>> rtp_data_channels_
  169. RTC_GUARDED_BY(signaling_thread());
  170. // Signals from |data_channel_transport_|. These are invoked on the
  171. // signaling thread.
  172. // TODO(bugs.webrtc.org/11547): These '_s' signals likely all belong on the
  173. // network thread.
  174. sigslot::signal1<bool> SignalDataChannelTransportWritable_s
  175. RTC_GUARDED_BY(signaling_thread());
  176. sigslot::signal2<const cricket::ReceiveDataParams&,
  177. const rtc::CopyOnWriteBuffer&>
  178. SignalDataChannelTransportReceivedData_s
  179. RTC_GUARDED_BY(signaling_thread());
  180. sigslot::signal1<int> SignalDataChannelTransportChannelClosing_s
  181. RTC_GUARDED_BY(signaling_thread());
  182. sigslot::signal1<int> SignalDataChannelTransportChannelClosed_s
  183. RTC_GUARDED_BY(signaling_thread());
  184. sigslot::signal1<RtpDataChannel*> SignalRtpDataChannelCreated_
  185. RTC_GUARDED_BY(signaling_thread());
  186. sigslot::signal1<SctpDataChannel*> SignalSctpDataChannelCreated_
  187. RTC_GUARDED_BY(signaling_thread());
  188. // Used from the network thread to invoke data channel transport signals on
  189. // the signaling thread.
  190. rtc::AsyncInvoker data_channel_transport_invoker_
  191. RTC_GUARDED_BY(network_thread());
  192. // Owning PeerConnection.
  193. PeerConnection* const pc_;
  194. rtc::WeakPtrFactory<DataChannelController> weak_factory_{this};
  195. };
  196. } // namespace webrtc
  197. #endif // PC_DATA_CHANNEL_CONTROLLER_H_