| 1 |
/* |
| 2 |
* SRT - Secure, Reliable, Transport |
| 3 |
* Copyright (c) 2019 Haivision Systems Inc. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
#ifndef INC_SRT_PACKETFILTER_API_H |
| 12 |
#define INC_SRT_PACKETFILTER_API_H |
| 13 |
|
| 14 |
#include "platform_sys.h" |
| 15 |
|
| 16 |
#include <cstring> |
| 17 |
#include <string> |
| 18 |
#include <map> |
| 19 |
#include <vector> |
| 20 |
#include <utility> |
| 21 |
|
| 22 |
namespace srt { |
| 23 |
|
| 24 |
class CPacket; |
| 25 |
|
| 26 |
enum SrtPktHeaderFields |
| 27 |
{ |
| 28 |
SRT_PH_SEQNO = 0, //< sequence number |
| 29 |
SRT_PH_MSGNO = 1, //< message number |
| 30 |
SRT_PH_TIMESTAMP = 2, //< time stamp |
| 31 |
SRT_PH_ID = 3, //< socket ID |
| 32 |
|
| 33 |
// Must be the last value - this is size of all, not a field id |
| 34 |
SRT_PH_E_SIZE |
| 35 |
}; |
| 36 |
|
| 37 |
|
| 38 |
enum SRT_ARQLevel |
| 39 |
{ |
| 40 |
SRT_ARQ_NEVER, //< Never send LOSSREPORT |
| 41 |
SRT_ARQ_ONREQ, //< Only record the loss, but report only those that are returned in receive() |
| 42 |
SRT_ARQ_ALWAYS, //< always send LOSSREPORT immediately after detecting a loss |
| 43 |
}; |
| 44 |
|
| 45 |
struct SrtConfig |
| 46 |
{ |
| 47 |
std::string type; |
| 48 |
typedef std::map<std::string, std::string> par_t; |
| 49 |
par_t parameters; |
| 50 |
}; |
| 51 |
|
| 52 |
struct SrtFilterConfig: SrtConfig |
| 53 |
{ |
| 54 |
size_t extra_size; // needed for filter option check against payload size |
| 55 |
}; |
| 56 |
|
| 57 |
struct SrtFilterInitializer |
| 58 |
{ |
| 59 |
SRTSOCKET socket_id; |
| 60 |
int32_t snd_isn; |
| 61 |
int32_t rcv_isn; |
| 62 |
size_t payload_size; |
| 63 |
size_t rcvbuf_size; |
| 64 |
}; |
| 65 |
|
| 66 |
struct SrtPacket |
| 67 |
{ |
| 68 |
uint32_t hdr[SRT_PH_E_SIZE]; |
| 69 |
char buffer[SRT_LIVE_MAX_PLSIZE]; |
| 70 |
size_t length; |
| 71 |
|
| 72 |
SrtPacket(size_t size): length(size) |
| 73 |
{ |
| 74 |
memset(hdr, 0, sizeof(hdr)); |
| 75 |
} |
| 76 |
|
| 77 |
uint32_t header(SrtPktHeaderFields field) { return hdr[field]; } |
| 78 |
char* data() { return buffer; } |
| 79 |
const char* data() const { return buffer; } |
| 80 |
size_t size() const { return length; } |
| 81 |
}; |
| 82 |
|
| 83 |
|
| 84 |
bool ParseFilterConfig(const std::string& s, SrtFilterConfig& w_config); |
| 85 |
|
| 86 |
|
| 87 |
class SrtPacketFilterBase |
| 88 |
{ |
| 89 |
SrtFilterInitializer initParams; |
| 90 |
|
| 91 |
protected: |
| 92 |
|
| 93 |
SRTSOCKET socketID() const { return initParams.socket_id; } |
| 94 |
int32_t sndISN() const { return initParams.snd_isn; } |
| 95 |
int32_t rcvISN() const { return initParams.rcv_isn; } |
| 96 |
size_t payloadSize() const { return initParams.payload_size; } |
| 97 |
size_t rcvBufferSize() const { return initParams.rcvbuf_size; } |
| 98 |
|
| 99 |
friend class PacketFilter; |
| 100 |
|
| 101 |
// Beside the size of the rows, special values: |
| 102 |
// 0: if you have 0 specified for rows, there are only columns |
| 103 |
// -1: Only during the handshake, use the value specified by peer. |
| 104 |
// -N: The N value still specifies the size, but in particular |
| 105 |
// dimension there is no filter control packet formed nor expected. |
| 106 |
|
| 107 |
public: |
| 108 |
|
| 109 |
typedef std::vector< std::pair<int32_t, int32_t> > loss_seqs_t; |
| 110 |
|
| 111 |
protected: |
| 112 |
|
| 113 |
SrtPacketFilterBase(const SrtFilterInitializer& i): initParams(i) |
| 114 |
{ |
| 115 |
} |
| 116 |
|
| 117 |
// Sender side |
| 118 |
|
| 119 |
/// This function creates and stores the filter control packet with |
| 120 |
/// a prediction to be immediately sent. This is called in the function |
| 121 |
/// that normally is prepared for extracting a data packet from the sender |
| 122 |
/// buffer and send it over the channel. The returned value informs the |
| 123 |
/// caller whether the control packet was available and therefore provided. |
| 124 |
/// @param [OUT] packet Target place where the packet should be stored |
| 125 |
/// @param [IN] seq Sequence number of the packet last requested for sending |
| 126 |
/// @return true if the control packet has been provided |
| 127 |
virtual bool packControlPacket(SrtPacket& packet, int32_t seq) = 0; |
| 128 |
|
| 129 |
/// This is called at the moment when the sender queue decided to pick up |
| 130 |
/// a new packet from the scheduled packets. This should be then used to |
| 131 |
/// continue filling the group, possibly followed by final calculating the |
| 132 |
/// control packet ready to send. The packet received by this function is |
| 133 |
/// potentially allowed to be modified. |
| 134 |
/// @param [INOUT] packet The packet about to send |
| 135 |
virtual void feedSource(CPacket& packet) = 0; |
| 136 |
|
| 137 |
|
| 138 |
// Receiver side |
| 139 |
|
| 140 |
// This function is called at the moment when a new data packet has |
| 141 |
// arrived (no matter if subsequent or recovered). The 'state' value |
| 142 |
// defines the configured level of loss state required to send the |
| 143 |
// loss report. |
| 144 |
virtual bool receive(const CPacket& pkt, loss_seqs_t& loss_seqs) = 0; |
| 145 |
|
| 146 |
// Backward configuration. |
| 147 |
// This should have some stable value after the configuration is parsed, |
| 148 |
// and it should be a stable value set ONCE, after the filter module is ready. |
| 149 |
virtual SRT_ARQLevel arqLevel() = 0; |
| 150 |
|
| 151 |
virtual ~SrtPacketFilterBase() |
| 152 |
{ |
| 153 |
} |
| 154 |
}; |
| 155 |
|
| 156 |
} // namespace srt |
| 157 |
|
| 158 |
#endif |