sctp_transport_internal.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2016 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 MEDIA_SCTP_SCTP_TRANSPORT_INTERNAL_H_
  11. #define MEDIA_SCTP_SCTP_TRANSPORT_INTERNAL_H_
  12. // TODO(deadbeef): Move SCTP code out of media/, and make it not depend on
  13. // anything in media/.
  14. #include <memory>
  15. #include <string>
  16. #include <vector>
  17. #include "rtc_base/copy_on_write_buffer.h"
  18. #include "rtc_base/thread.h"
  19. // For SendDataParams/ReceiveDataParams.
  20. // TODO(deadbeef): Use something else for SCTP. It's confusing that we use an
  21. // SSRC field for SID.
  22. #include "media/base/media_channel.h"
  23. #include "p2p/base/packet_transport_internal.h"
  24. namespace cricket {
  25. // Constants that are important to API users
  26. // The size of the SCTP association send buffer. 256kB, the usrsctp default.
  27. constexpr int kSctpSendBufferSize = 256 * 1024;
  28. // The number of outgoing streams that we'll negotiate. Since stream IDs (SIDs)
  29. // are 0-based, the highest usable SID is 1023.
  30. //
  31. // It's recommended to use the maximum of 65535 in:
  32. // https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-6.2
  33. // However, we use 1024 in order to save memory. usrsctp allocates 104 bytes
  34. // for each pair of incoming/outgoing streams (on a 64-bit system), so 65535
  35. // streams would waste ~6MB.
  36. //
  37. // Note: "max" and "min" here are inclusive.
  38. constexpr uint16_t kMaxSctpStreams = 1024;
  39. constexpr uint16_t kMaxSctpSid = kMaxSctpStreams - 1;
  40. constexpr uint16_t kMinSctpSid = 0;
  41. // This is the default SCTP port to use. It is passed along the wire and the
  42. // connectee and connector must be using the same port. It is not related to the
  43. // ports at the IP level. (Corresponds to: sockaddr_conn.sconn_port in
  44. // usrsctp.h)
  45. const int kSctpDefaultPort = 5000;
  46. // Abstract SctpTransport interface for use internally (by PeerConnection etc.).
  47. // Exists to allow mock/fake SctpTransports to be created.
  48. class SctpTransportInternal {
  49. public:
  50. virtual ~SctpTransportInternal() {}
  51. // Changes what underlying DTLS transport is uses. Used when switching which
  52. // bundled transport the SctpTransport uses.
  53. virtual void SetDtlsTransport(rtc::PacketTransportInternal* transport) = 0;
  54. // When Start is called, connects as soon as possible; this can be called
  55. // before DTLS completes, in which case the connection will begin when DTLS
  56. // completes. This method can be called multiple times, though not if either
  57. // of the ports are changed.
  58. //
  59. // |local_sctp_port| and |remote_sctp_port| are passed along the wire and the
  60. // listener and connector must be using the same port. They are not related
  61. // to the ports at the IP level. If set to -1, we default to
  62. // kSctpDefaultPort.
  63. // |max_message_size_| sets the max message size on the connection.
  64. // It must be smaller than or equal to kSctpSendBufferSize.
  65. // It can be changed by a secons Start() call.
  66. //
  67. // TODO(deadbeef): Support calling Start with different local/remote ports
  68. // and create a new association? Not clear if this is something we need to
  69. // support though. See: https://github.com/w3c/webrtc-pc/issues/979
  70. virtual bool Start(int local_sctp_port,
  71. int remote_sctp_port,
  72. int max_message_size) = 0;
  73. // NOTE: Initially there was a "Stop" method here, but it was never used, so
  74. // it was removed.
  75. // Informs SctpTransport that |sid| will start being used. Returns false if
  76. // it is impossible to use |sid|, or if it's already in use.
  77. // Until calling this, can't send data using |sid|.
  78. // TODO(deadbeef): Actually implement the "returns false if |sid| can't be
  79. // used" part. See:
  80. // https://bugs.chromium.org/p/chromium/issues/detail?id=619849
  81. virtual bool OpenStream(int sid) = 0;
  82. // The inverse of OpenStream. Begins the closing procedure, which will
  83. // eventually result in SignalClosingProcedureComplete on the side that
  84. // initiates it, and both SignalClosingProcedureStartedRemotely and
  85. // SignalClosingProcedureComplete on the other side.
  86. virtual bool ResetStream(int sid) = 0;
  87. // Send data down this channel (will be wrapped as SCTP packets then given to
  88. // usrsctp that will then post the network interface).
  89. // Returns true iff successful data somewhere on the send-queue/network.
  90. // Uses |params.ssrc| as the SCTP sid.
  91. virtual bool SendData(const SendDataParams& params,
  92. const rtc::CopyOnWriteBuffer& payload,
  93. SendDataResult* result = nullptr) = 0;
  94. // Indicates when the SCTP socket is created and not blocked by congestion
  95. // control. This changes to false when SDR_BLOCK is returned from SendData,
  96. // and
  97. // changes to true when SignalReadyToSendData is fired. The underlying DTLS/
  98. // ICE channels may be unwritable while ReadyToSendData is true, because data
  99. // can still be queued in usrsctp.
  100. virtual bool ReadyToSendData() = 0;
  101. // Returns the current max message size, set with Start().
  102. virtual int max_message_size() const = 0;
  103. // Returns the current negotiated max # of outbound streams.
  104. // Will return absl::nullopt if negotiation is incomplete.
  105. virtual absl::optional<int> max_outbound_streams() const = 0;
  106. // Returns the current negotiated max # of inbound streams.
  107. virtual absl::optional<int> max_inbound_streams() const = 0;
  108. sigslot::signal0<> SignalReadyToSendData;
  109. sigslot::signal0<> SignalAssociationChangeCommunicationUp;
  110. // ReceiveDataParams includes SID, seq num, timestamp, etc. CopyOnWriteBuffer
  111. // contains message payload.
  112. sigslot::signal2<const ReceiveDataParams&, const rtc::CopyOnWriteBuffer&>
  113. SignalDataReceived;
  114. // Parameter is SID; fired when we receive an incoming stream reset on an
  115. // open stream, indicating that the other side started the closing procedure.
  116. // After resetting the outgoing stream, SignalClosingProcedureComplete will
  117. // fire too.
  118. sigslot::signal1<int> SignalClosingProcedureStartedRemotely;
  119. // Parameter is SID; fired when closing procedure is complete (both incoming
  120. // and outgoing streams reset).
  121. sigslot::signal1<int> SignalClosingProcedureComplete;
  122. // Fired when the underlying DTLS transport has closed due to an error
  123. // or an incoming DTLS disconnect.
  124. sigslot::signal0<> SignalClosedAbruptly;
  125. // Helper for debugging.
  126. virtual void set_debug_name_for_testing(const char* debug_name) = 0;
  127. };
  128. } // namespace cricket
  129. #endif // MEDIA_SCTP_SCTP_TRANSPORT_INTERNAL_H_