packet.h 6.58 KB
Newer Older
1 2 3 4
#ifndef PRRT_FRAME_H
#define PRRT_FRAME_H

#include <stdint.h>
5
#include "../../util/pipe.h"
Andreas Schmidt's avatar
Andreas Schmidt committed
6
#include "codingParams.h"
7
#include <stdbool.h>
8
#include <netinet/in.h>
9

Stefan Reif's avatar
Stefan Reif committed
10

11 12 13 14 15 16 17
typedef enum {
    PACKET_TYPE_DATA = 0,
    PACKET_TYPE_REPETITION = 1,
    PACKET_TYPE_REDUNDANCY = 2,
    PACKET_TYPE_FEEDBACK = 3,
    PACKET_TYPE_PRESENT_REDUNDANCY = 4,
    PACKET_TYPE_CHANNEL_FEEDBACK = 5
18
} prrtPacketTypeEnum_t;
19 20 21

typedef uint16_t prrtSequenceNumber_t;
typedef uint8_t prrtIndex_t;
22

23
typedef uint32_t prrtTimestamp_t; // microsecond
24 25
typedef atomic_uint_fast32_t prrtAtomicTimestamp_t;

26
typedef uint32_t prrtTimedelta_t; // microsecond
27 28
typedef atomic_uint_fast32_t prrtAtomicTimedelta_t;

29
typedef int32_t prrtTimeDifference_t; // microsecond
Andreas Schmidt's avatar
Andreas Schmidt committed
30
typedef int64_t prrtTimeDiff_t; // microsecond
31
typedef uint32_t prrtPacketLength_t; // bytes
Andreas Schmidt's avatar
Andreas Schmidt committed
32
typedef float prrtPacketLossRate_t;
33

34
typedef uint32_t prrtDeliveryRate_t; // bits per second
35 36
typedef atomic_uint_fast32_t prrtAtomicDeliveryRate_t;

37
typedef uint32_t prrtByteCount_t;
38
typedef uint8_t prrtPacketType_t;
39

40 41
typedef uint16_t prrtRefcount_t;

42 43 44 45
typedef struct prrtIncompleteBlock {
    prrtSequenceNumber_t sequenceNumberBase;
    uint16_t repairCycleIndex;
} PrrtIncompleteBlock;
46

47
typedef struct prrtPacket {
Andreas Schmidt's avatar
Andreas Schmidt committed
48
    ListNode asListNode;
49
    uint8_t type_priority;
50 51 52
    prrtIndex_t index;
    prrtSequenceNumber_t sequenceNumber;
    void *payload;
53
    struct timespec channelReceive;
54
    prrtPacketLength_t payloadLength;
55 56 57 58 59 60 61 62

    // Packet State field that are not put into the packet on the wire
    prrtByteCount_t delivered;
    prrtTimestamp_t delivered_time;
    prrtTimestamp_t first_sent_time;
    bool is_app_limited;
    prrtTimestamp_t sent_time;
    prrtTimedelta_t rtt;
63 64

    struct sockaddr_in sender_addr;
65 66

    prrtRefcount_t refcount;
67
} PrrtPacket;
68
#define PRRT_PACKET_GENERAL_HEADER_SIZE 8
69
#define PRRT_PACKET_ENCODED_GENERAL_HEADER_LENGTH 4
70

71
typedef struct prrtPacketDataPayload {
72
    prrtPacketLength_t dataLength;
73
    prrtTimestamp_t timestamp;
Andreas Schmidt's avatar
Andreas Schmidt committed
74
    prrtTimedelta_t RTprop_us;
75
    prrtTimestamp_t packetTimeout_us;
Andreas Schmidt's avatar
Andreas Schmidt committed
76 77
    prrtDeliveryRate_t btl_datarate;
    prrtTimedelta_t btl_pace;
rna's avatar
rna committed
78
} __attribute__((packed)) PrrtPacketDataPayload;
79 80 81 82 83

#define PRRT_PACKET_DATA_HEADER_SIZE (\
        sizeof(prrtPacketLength_t) + \
        sizeof(prrtTimestamp_t) + \
        sizeof(prrtTimedelta_t) + \
84
        sizeof(prrtTimestamp_t) + \
Andreas Schmidt's avatar
Andreas Schmidt committed
85 86
        sizeof(prrtDeliveryRate_t) + \
        sizeof(prrtTimedelta_t))
87

88
typedef struct prrtPacketRedundancyPayload {
89
    prrtSequenceNumber_t baseSequenceNumber;
90
    prrtTimestamp_t timestamp;
Andreas Schmidt's avatar
Andreas Schmidt committed
91
    prrtTimedelta_t btl_pace;
92 93
    uint8_t n;
    uint8_t k;
rna's avatar
rna committed
94
} __attribute__((packed)) PrrtPacketRedundancyPayload;
95 96 97 98

#define PRRT_PACKET_REDUNDANCY_HEADER_SIZE (\
        sizeof(prrtSequenceNumber_t) + \
        sizeof(prrtTimestamp_t) + \
Andreas Schmidt's avatar
Andreas Schmidt committed
99
        sizeof(prrtTimedelta_t) + \
100 101
        sizeof(uint8_t) + \
        sizeof(uint8_t) )
102

103
typedef struct prrtPacketFeedbackPayload {
104
    prrtTimestamp_t forwardTripTimestamp_us;
Andreas Schmidt's avatar
Andreas Schmidt committed
105
    prrtTimedelta_t btl_pace;
106 107 108 109 110 111
    prrtSequenceNumber_t erasureCount;
    prrtSequenceNumber_t packetCount;
    prrtSequenceNumber_t gapLength;
    prrtSequenceNumber_t gapCount;
    prrtSequenceNumber_t burstLength;
    prrtSequenceNumber_t burstCount;
112 113
    prrtSequenceNumber_t ackSequenceNumber;
    prrtPacketType_t ackPacketType;
114
    PrrtIncompleteBlock *incompleteBlocks;
rna's avatar
rna committed
115
} __attribute__((packed)) PrrtPacketFeedbackPayload;
116 117 118 119

#define PRRT_PACKET_FEEDBACK_HEADER_SIZE (\
        sizeof(prrtTimedelta_t) + \
        sizeof(prrtTimestamp_t) + \
Andreas Schmidt's avatar
Andreas Schmidt committed
120
        sizeof(prrtTimedelta_t) + \
121 122 123 124 125 126 127 128
        sizeof(prrtSequenceNumber_t) + \
        sizeof(prrtSequenceNumber_t) + \
        sizeof(prrtSequenceNumber_t) + \
        sizeof(prrtSequenceNumber_t) + \
        sizeof(prrtSequenceNumber_t) + \
        sizeof(prrtSequenceNumber_t) + \
        sizeof(prrtSequenceNumber_t) + \
        sizeof(prrtPacketType_t) )
129

130

131 132 133
#define PrrtPacket_type(packet_ptr) ((prrtPacketType_t) (((packet_ptr)->type_priority >> 4) & 0x0F))

#define PrrtPacket_priority(packet_ptr) ((uint8_t) (packet_ptr->type_priority & 0x0F))
134 135 136

prrtPacketLength_t PrrtPacket_size(PrrtPacket *packet_ptr);

137
int PrrtPacket_print(PrrtPacket *packet_ptr);
138

139
PrrtPacket *PrrtPacket_copy(PrrtPacket *original);
140

141 142
PrrtPacket *PrrtPacket_deepcopy(PrrtPacket *original);

143
PrrtPacket *PrrtPacket_create_data_packet(uint8_t priority, const void *payloadPointer,
rna's avatar
rna committed
144
                                          prrtPacketLength_t dataLength, prrtSequenceNumber_t sequenceNumber,
145
                                          prrtTimedelta_t targetDelay);
146

147 148 149 150
PrrtPacket *PrrtPacket_reconstruct_data_packet(PrrtPacketDataPayload *payload, prrtIndex_t index,
                                               prrtSequenceNumber_t sequenceNumber);

PrrtPacket *
Andreas Schmidt's avatar
Andreas Schmidt committed
151
PrrtPacket_create_feedback_packet(uint8_t priority, prrtSequenceNumber_t sequenceNumber,
152 153 154 155
                                  prrtSequenceNumber_t gapLength, prrtSequenceNumber_t gapCount,
                                  prrtSequenceNumber_t burstLength, prrtSequenceNumber_t burstCount,
                                  prrtTimestamp_t forwardTripTime, prrtSequenceNumber_t erasureCount,
                                  prrtSequenceNumber_t packetCount, prrtSequenceNumber_t ackSequenceNumber,
Andreas Schmidt's avatar
Andreas Schmidt committed
156
                                  prrtPacketType_t ackPacketType, prrtTimedelta_t btl_pace);
157

158 159 160
PrrtPacket *PrrtPacket_create_redundancy_packet(uint8_t priority, void *payloadPointer,
                                                prrtPacketLength_t payloadLength,
                                                prrtSequenceNumber_t sequenceNumber, uint8_t index,
Andreas Schmidt's avatar
Andreas Schmidt committed
161
                                                prrtSequenceNumber_t baseSequenceNumber,
162
                                                PrrtCodingConfiguration *codingParams);
163

164
bool PrrtPacket_decode(void *srcBuffer, uint16_t srcBufferSize, PrrtPacket *targetPacket);
165

166
bool PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr);
167

Andreas Schmidt's avatar
Andreas Schmidt committed
168
int PrrtPacket_destroy(PrrtPacket *packet);
169

rna's avatar
rna committed
170 171
#define PrrtPacket_copy_payload_to_buffer(dst, packet, header_size) memcpy(dst, (packet)->payload + (header_size), (packet)->payloadLength - (header_size));
#define PrrtPacket_copy_buffer_to_payload(packet, src, header_size) memcpy((packet)->payload + (header_size), src, (packet)->payloadLength - (header_size));
172

Andreas Schmidt's avatar
Andreas Schmidt committed
173
#define PrrtPacket_get_data_timestamp(packet) ((PrrtPacketDataPayload*) packet->payload)->timestamp;
174
#define PrrtPacket_get_redundancy_timestamp(packet) ((PrrtPacketRedundancyPayload*) packet->payload)->timestamp;
175

Stefan Reif's avatar
Stefan Reif committed
176 177
#define PrrtPacket_byListNode(lnptr) ((PrrtPacket *) (lnptr))

rna's avatar
rna committed
178
#define PrrtDataPacket_packet_timeout(packet) ((PrrtPacketDataPayload*) (packet)->payload)->packetTimeout_us
179

180
#endif //PRRT_FRAME_H