blob: c8c606ed1154debef7c1f4f9ba55fa898652dd7c [file] [log] [blame]
deadbeef953c2ce2017-01-09 14:53:41 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MEDIA_SCTP_SCTPTRANSPORTINTERNAL_H_
12#define MEDIA_SCTP_SCTPTRANSPORTINTERNAL_H_
deadbeef953c2ce2017-01-09 14:53:41 -080013
14// TODO(deadbeef): Move SCTP code out of media/, and make it not depend on
15// anything in media/.
16
17#include <memory> // for unique_ptr
18#include <string>
19#include <vector>
20
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/copyonwritebuffer.h"
22#include "rtc_base/thread.h"
deadbeef953c2ce2017-01-09 14:53:41 -080023// For SendDataParams/ReceiveDataParams.
24// TODO(deadbeef): Use something else for SCTP. It's confusing that we use an
25// SSRC field for SID.
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "media/base/mediachannel.h"
27#include "p2p/base/packettransportinternal.h"
deadbeef953c2ce2017-01-09 14:53:41 -080028
29namespace cricket {
30
31// The number of outgoing streams that we'll negotiate. Since stream IDs (SIDs)
32// are 0-based, the highest usable SID is 1023.
33//
34// It's recommended to use the maximum of 65535 in:
35// https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-6.2
36// However, we use 1024 in order to save memory. usrsctp allocates 104 bytes
37// for each pair of incoming/outgoing streams (on a 64-bit system), so 65535
38// streams would waste ~6MB.
39//
40// Note: "max" and "min" here are inclusive.
41constexpr uint16_t kMaxSctpStreams = 1024;
42constexpr uint16_t kMaxSctpSid = kMaxSctpStreams - 1;
43constexpr uint16_t kMinSctpSid = 0;
44
45// This is the default SCTP port to use. It is passed along the wire and the
46// connectee and connector must be using the same port. It is not related to the
47// ports at the IP level. (Corresponds to: sockaddr_conn.sconn_port in
48// usrsctp.h)
49const int kSctpDefaultPort = 5000;
50
51// Abstract SctpTransport interface for use internally (by
52// PeerConnection/WebRtcSession/etc.). Exists to allow mock/fake SctpTransports
53// to be created.
54class SctpTransportInternal {
55 public:
56 virtual ~SctpTransportInternal() {}
57
58 // Changes what underlying DTLS channel is uses. Used when switching which
59 // bundled transport the SctpTransport uses.
60 // Assumes |channel| is non-null.
deadbeef5bd5ca32017-02-10 11:31:50 -080061 virtual void SetTransportChannel(rtc::PacketTransportInternal* channel) = 0;
deadbeef953c2ce2017-01-09 14:53:41 -080062
63 // When Start is called, connects as soon as possible; this can be called
64 // before DTLS completes, in which case the connection will begin when DTLS
65 // completes. This method can be called multiple times, though not if either
66 // of the ports are changed.
67 //
68 // |local_sctp_port| and |remote_sctp_port| are passed along the wire and the
69 // listener and connector must be using the same port. They are not related
70 // to the ports at the IP level. If set to -1, we default to
71 // kSctpDefaultPort.
72 //
73 // TODO(deadbeef): Add remote max message size as parameter to Start, once we
74 // start supporting it.
75 // TODO(deadbeef): Support calling Start with different local/remote ports
76 // and create a new association? Not clear if this is something we need to
77 // support though. See: https://github.com/w3c/webrtc-pc/issues/979
78 virtual bool Start(int local_sctp_port, int remote_sctp_port) = 0;
79
80 // NOTE: Initially there was a "Stop" method here, but it was never used, so
81 // it was removed.
82
83 // Informs SctpTransport that |sid| will start being used. Returns false if
84 // it is impossible to use |sid|, or if it's already in use.
85 // Until calling this, can't send data using |sid|.
86 // TODO(deadbeef): Actually implement the "returns false if |sid| can't be
87 // used" part. See:
88 // https://bugs.chromium.org/p/chromium/issues/detail?id=619849
89 virtual bool OpenStream(int sid) = 0;
90 // The inverse of OpenStream. When this method returns, the reset process may
91 // have not finished but it will have begun.
92 // TODO(deadbeef): We need a way to tell when it's done. See:
93 // https://bugs.chromium.org/p/webrtc/issues/detail?id=4453
94 virtual bool ResetStream(int sid) = 0;
95 // Send data down this channel (will be wrapped as SCTP packets then given to
96 // usrsctp that will then post the network interface).
97 // Returns true iff successful data somewhere on the send-queue/network.
98 // Uses |params.ssrc| as the SCTP sid.
99 virtual bool SendData(const SendDataParams& params,
100 const rtc::CopyOnWriteBuffer& payload,
101 SendDataResult* result = nullptr) = 0;
102
103 // Indicates when the SCTP socket is created and not blocked by congestion
104 // control. This changes to false when SDR_BLOCK is returned from SendData,
105 // and
106 // changes to true when SignalReadyToSendData is fired. The underlying DTLS/
107 // ICE channels may be unwritable while ReadyToSendData is true, because data
108 // can still be queued in usrsctp.
109 virtual bool ReadyToSendData() = 0;
110
111 sigslot::signal0<> SignalReadyToSendData;
112 // ReceiveDataParams includes SID, seq num, timestamp, etc. CopyOnWriteBuffer
113 // contains message payload.
114 sigslot::signal2<const ReceiveDataParams&, const rtc::CopyOnWriteBuffer&>
115 SignalDataReceived;
116 // Parameter is SID of closed stream.
117 sigslot::signal1<int> SignalStreamClosedRemotely;
118
119 // Helper for debugging.
120 virtual void set_debug_name_for_testing(const char* debug_name) = 0;
121};
122
123// Factory class which can be used to allow fake SctpTransports to be injected
124// for testing. Or, theoretically, SctpTransportInternal implementations that
125// use something other than usrsctp.
126class SctpTransportInternalFactory {
127 public:
128 virtual ~SctpTransportInternalFactory() {}
129
130 // Create an SCTP transport using |channel| for the underlying transport.
131 virtual std::unique_ptr<SctpTransportInternal> CreateSctpTransport(
deadbeef5bd5ca32017-02-10 11:31:50 -0800132 rtc::PacketTransportInternal* channel) = 0;
deadbeef953c2ce2017-01-09 14:53:41 -0800133};
134
135} // namespace cricket
136
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200137#endif // MEDIA_SCTP_SCTPTRANSPORTINTERNAL_H_