Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Added buffer configuration guide #1951

Merged
merged 7 commits into from
May 18, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions docs/API/configure-buffers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Configuration Guidelines
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved

## Receiver Buffer Size

The receiver buffer can be configured with `SRTO_RCVBUF` socket option.
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved
Buffer size in bytes is expected to be passed in the `optval` argument of the `srt_setsockopt(..)` function.
However, internally the value will be converted into the number of packets stored in the receiver buffer.

The allowed value of `SRTO_RCVBUF` is also limited by the value of the flow control window size `SRTO_FC` socket option.
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved
See issue [#700](https://github.com/Haivision/srt/issues/700).

The default flow control window size is 25600 packets. It is approximately:

- **270 Mbits** of payload in the default live streaming configuration with an SRT payload size of **1316 bytes**;
- **300 Mbits** of payload with an SRT payload size of **1456 bytes**.
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved

The default receiver buffer size is 8192 packets. It is approximately:
- **86 Mbits** of payload with the effective SRT payload size of **1316 bytes**.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's about 1456 bytes?


### Setting Receiver Buffer Size

As already mentioned, the maximum allowed size of the receiver buffer is limited by the value of `SRTO_FC`.
Setting `SRTO_RCVBUF` option value using `srt_setsockopt(..)` function converts internally the provided size in bytes to size in packets
using the configured value of `SRTO_MSS` option to estimate the maximum possible payload of a packet.
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved

The following function returns the buffer size in packets:

```c++
int getRbufSizePkts(int SRTO_RCVBUF, int SRTO_MSS, int SRTO_FC)
{
// UDP header size is assumed to be 28 bytes
// 20 bytes IPv4 + 8 bytes of UDP
const int UDPHDR_SIZE = 28;
const in pkts = (rbuf_size / (SRTO_MSS - UDPHDR_SIZE));

return min(pkts, SRTO_FC);
}
```

If `SRTO_RCVBUF` in packets exceeds `SRTO_FC`, then it is silently set to the value in bytes corresponding to `SRTO_FC`.
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved
Therefore, to set higher values of `SRTO_RCVBUF` the value of `SRTO_FC` must be increased first.

### Calculating Target Size In Packets
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved

The minimum size of the receiver buffer in packets can be calculated
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved

pktsRBufSize = bps / 8 × (RTTsec + latency_sec) / bytePayloadSize,
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved

where

- `bps` - is the payload bitrate of the stream in bits per second;
- `RTTsec` - RTT of the network connection in seconds;
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

latency is missing?

- `bytePayloadSize` - expected size of the payload of SRT data packet.
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved

If the whole remainder of the MTU is expected to be used,

bytePayloadSize = MSS - 44

- 44 - size of **IPv4** headers:
- 20 bytes **IPv4**
- 8 bytes of UDP
- 16 bytes of SRT packet header).

- `MSS` - Maximum segment size (aka MTU), see `SRTO_MSS`.
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved

### Calculating Target Size to Set

To determine the value to pass in `srt_setsockopt(..)` with `SRTO_RCVBUF`
the size in packets `pktsRBufSize` must be converted to the size in bytes
assuming the internal conversion of the `srt_setsockopt(..)` function.

The target size of the payload stored by the receiver buffer would be

SRTO_RCVBUF = pktsRBufSize × (SRTO_MSS - UDPHDR_SIZE)

where
- `UDPHDR_SIZE = 28` (20 bytes IPv4, 8 bytes of UDP);
- `SRTO_MSS` is the corresponding socket option value at the moment of setting `SRTO_RCVBUF`.
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved


### To Summ up
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved
maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved


```c++

auto CalculateTargetRBufSize(int msRTT, int bpsRate, int bytesPayloadSize, int msLatency, int SRTO_MSS, int SRTO_FC)
{
const int UDPHDR_SIZE = 28;
const int targetPayloadBytes = (msLatency + msRTT / 2) * bpsRate / 1000 / 8;
const int targetNumPackets = targetPayloadBytes / bytesPayloadSize;
const int targetSizeValue = targetNumPackets * (SRTO_MSS - UDPHDR_SIZE);
return {targetNumPackets, targetSizeValue};
}

// Configuring

const auto [fc, rcvbuf] = CalculateTargetRBufSize(msRTT, bpsRate, bytesPayloadSize, SRTO_RCVLATENCY, SRTO_MSS, SRTO_FC);

int optval = fc;
int optlen = sizeof optval;
srt_setsockopt(sock, 0, SRTO_FC, (void*) &optval, optlen);

optval = rcvbuf;
srt_setsockopt(sock, 0, SRTO_RCVBUF, (void*) &optval, optlen);


```


maxsharabayko marked this conversation as resolved.
Show resolved Hide resolved