data_channel_controller.h 8.6 KB

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