packet.c 18.3 KB
Newer Older
1 2 3
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
4
#include <netinet/in.h>
5
#include <stdbool.h>
6 7 8
#include "../../util/common.h"
#include "../../util/dbg.h"
#include "../clock.h"
Andreas Schmidt's avatar
Andreas Schmidt committed
9
#include "packet.h"
10

11
static void *encode_general_header(void *buf_ptr, const PrrtPacket *packet);
12

13
static void *encode_data_header(void *buf_ptr, const void *payload);
14

15
static void *encode_redundancy_header(void *buf_ptr, const void *payload);
16

17
static void *encode_feedback_header(void *buf_ptr, const void *payload);
18

19
static void *decode_data_header(void *dstBuffer, const void *srcBuffer);
Andreas Schmidt's avatar
Andreas Schmidt committed
20

21
static void *decode_redundancy_header(void *dstBuffer, const void *srcBuffer);
22

23
static void *decode_feedback_header(void *dstBuffer, const void *srcBuffer);
Andreas Schmidt's avatar
Andreas Schmidt committed
24

25
prrtPacketLength_t PrrtPacket_size(PrrtPacket *packet_ptr) {
26
    return (prrtPacketLength_t) (packet_ptr->payloadLength + PRRT_PACKET_ENCODED_GENERAL_HEADER_LENGTH);
27 28
}

29
int PrrtPacket_print(PrrtPacket *packet_ptr) {
30 31
    printf(" 0                   1                   2                   3\n"
                   " 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1\n"
32
                   "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
33
    prrtPacketType_t type = PrrtPacket_type(packet_ptr);
34

35
    printf("| %5u | %5u | %13u | %29u |\n", type, PrrtPacket_priority(packet_ptr), packet_ptr->index,
36
           packet_ptr->sequenceNumber);
37
    printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
38

39
    if (type == PACKET_TYPE_DATA) {
40
        PrrtPacketDataPayload *payload = packet_ptr->payload;
41 42
        printf("| %61u |\n", payload->timestamp);
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
43
        printf("| %61u |\n", payload->groupRTprop_us);
44
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
45 46
        printf("| %61u |\n", payload->packetTimeout_us);
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
47 48
        printf("| %61s |\n", (char *) (packet_ptr->payload + PRRT_PACKET_DATA_HEADER_SIZE));
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
49
    } else if (type == PACKET_TYPE_REDUNDANCY) {
50
        PrrtPacketRedundancyPayload *payload = packet_ptr->payload;
51
        printf("| %29u | %13u | %13u |\n", payload->baseSequenceNumber, payload->n, payload->k);
52
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
53
    } else if (type == PACKET_TYPE_FEEDBACK) {
54 55
        prrtPacketLength_t i = 0;

56
        PrrtPacketFeedbackPayload *payload = packet_ptr->payload;
57

58
        printf("| %61u |\n", payload->groupRTT_us);
59
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
60
        printf("| %61u |\n", payload->forwardTripTimestamp_us);
61
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
62
        printf("| %29u | %29u |\n", payload->erasureCount, payload->packetCount);
63
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
64
        printf("| %29u | %29u |\n", payload->gapLength, payload->gapCount);
65
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
66
        printf("| %29u | %29u |\n", payload->burstLength, payload->burstCount);
67
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
68 69 70 71 72

        prrtPacketLength_t remainingSpace = ((prrtPacketLength_t) (packet_ptr->payloadLength -
                                                                   PRRT_PACKET_FEEDBACK_HEADER_SIZE));
        prrtPacketLength_t blockCount = (prrtPacketLength_t) (remainingSpace / sizeof(PrrtIncompleteBlock));

73
        for (i = 0; i < blockCount; ++i) {
74 75 76 77
            PrrtIncompleteBlock block = payload->incompleteBlocks[i];
            printf("| %29u | %29u |\n", block.sequenceNumberBase, block.repairCycleIndex);
            printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
        }
78
    } else {
79 80
        printf("| Unhandeled Type                                               |\n");
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
81 82 83 84 85
    }

    return 0;
}

86
PrrtPacket *PrrtPacket_copy(PrrtPacket *original) {
87 88 89 90 91
    __sync_fetch_and_add(&original->refcount, 1);
    return original;
}

PrrtPacket *PrrtPacket_deepcopy(PrrtPacket *original) {
92 93
    PrrtPacket *newPacket = calloc(1, sizeof(PrrtPacket));
    check_mem(newPacket);
94
    void *payload = calloc(1, original->payloadLength);
95 96 97
    check_mem(payload);

    newPacket->payload = payload;
98 99
    newPacket->payloadLength = original->payloadLength;
    memcpy(newPacket->payload, original->payload, original->payloadLength);
100 101

    newPacket->index = original->index;
102
    newPacket->sequenceNumber = original->sequenceNumber;
103 104
    newPacket->type_priority = original->type_priority;

105 106
    newPacket->refcount = 1;

107 108 109

    return newPacket;
    error:
110
    PERROR("Not enough memory for packet copies.%s", "");
Andreas Schmidt's avatar
Andreas Schmidt committed
111
    return NULL;
112 113
}

114 115
static PrrtPacket *
create_header(uint8_t priority, prrtSequenceNumber_t seqno, prrtPacketLength_t size, uint8_t type, uint8_t index) {
Andreas Schmidt's avatar
Andreas Schmidt committed
116 117 118 119 120 121
    PrrtPacket *packet = calloc(1, sizeof(PrrtPacket));
    check_mem(packet);

    packet->type_priority = type << 4;
    packet->type_priority |= priority & 0x0F;
    packet->index = index;
122 123
    packet->sequenceNumber = seqno;
    packet->payloadLength = size;
124
    packet->refcount = 1;
Andreas Schmidt's avatar
Andreas Schmidt committed
125 126 127 128 129

    return packet;

    error:
    PERROR("Could not create packet.%s", "");
Andreas Schmidt's avatar
Andreas Schmidt committed
130
    return NULL;
Andreas Schmidt's avatar
Andreas Schmidt committed
131 132
}

133
bool PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr) {
134
    void *payload = packet_ptr->payload;
135

136
    check(packet_ptr->payloadLength + PRRT_PACKET_ENCODED_GENERAL_HEADER_LENGTH <= buf_size, "Buffer too small.");
137

138 139
    buf_ptr = encode_general_header(buf_ptr, packet_ptr);

140
    prrtPacketType_t type = PrrtPacket_type(packet_ptr);
141
    if (type == PACKET_TYPE_DATA) {
142
        buf_ptr = encode_data_header(buf_ptr, payload);
143
        PrrtPacket_copy_payload_to_buffer(buf_ptr, packet_ptr, PRRT_PACKET_DATA_HEADER_SIZE);
144
    } else if (type == PACKET_TYPE_REDUNDANCY) {
145
        buf_ptr = encode_redundancy_header(buf_ptr, payload);
146
        PrrtPacket_copy_payload_to_buffer(buf_ptr, packet_ptr, PRRT_PACKET_REDUNDANCY_HEADER_SIZE);
147
    } else if (type == PACKET_TYPE_FEEDBACK) {
148
        encode_feedback_header(buf_ptr, payload);
149 150
    } else {
        perror("NOT IMPLEMENTED");
151
        return false;
152
    }
153
    return true;
154 155

    error:
156
    return false;
157 158
}

159
#define identity(x) (x)
160

161 162
#define PrrtPacketField_encode(payload, buf_ptr, field_t, field, conversion) \
    do { \
163 164 165 166
        field_t PrrtPacketField_encode_value = conversion((payload)->field); \
        /* memcpy to avoid misaligned access.
         * The compiler will optimize this into appropriate mov instructions */ \
        memcpy(buf_ptr, &PrrtPacketField_encode_value, sizeof(field_t)); \
167 168
        (buf_ptr) += sizeof(field_t); \
    } while (false)
169

170 171
void *encode_redundancy_header(void *buf_ptr, const void *payload) {
    const PrrtPacketRedundancyPayload *redundancyPayload = payload;
172

173 174 175 176
    PrrtPacketField_encode(redundancyPayload, buf_ptr, prrtSequenceNumber_t, baseSequenceNumber, htons);
    PrrtPacketField_encode(redundancyPayload, buf_ptr, prrtTimestamp_t, timestamp, htonl);
    PrrtPacketField_encode(redundancyPayload, buf_ptr, uint8_t, n, identity);
    PrrtPacketField_encode(redundancyPayload, buf_ptr, uint8_t, k, identity);
177 178 179 180

    return buf_ptr;
}

181
void *encode_feedback_header(void *buf_ptr, const void *payload) {
182
    const PrrtPacketFeedbackPayload *feedbackPayload = payload;
183

184 185 186 187 188 189 190 191 192 193
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtTimedelta_t, groupRTT_us, htonl);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtTimestamp_t, forwardTripTimestamp_us, htonl);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, erasureCount, htons);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, packetCount, htons);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, gapLength, htons);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, gapCount, htons);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, burstLength, htons);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, burstCount, htons);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtPacketType_t, ackPacketType, identity);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, ackSequenceNumber, htons);
194

195 196 197
    return buf_ptr;
}

198
void *encode_data_header(void *buf_ptr, const void *payload) {
199
    const PrrtPacketDataPayload *data_payload = payload;
200

201 202 203
    PrrtPacketField_encode(data_payload, buf_ptr, prrtPacketLength_t, dataLength, htonl);
    PrrtPacketField_encode(data_payload, buf_ptr, prrtTimestamp_t, timestamp, htonl);
    PrrtPacketField_encode(data_payload, buf_ptr, prrtTimedelta_t, groupRTprop_us, htonl);
rna's avatar
rna committed
204
    PrrtPacketField_encode(data_payload, buf_ptr, prrtTimestamp_t, packetTimeout_us, htonl);
205
    PrrtPacketField_encode(data_payload, buf_ptr, prrtDeliveryRate_t, btlbw, htonl);
206 207 208 209

    return buf_ptr;
}

210
void *encode_general_header(void *buf_ptr, const PrrtPacket *packet) {
211

212 213 214
    PrrtPacketField_encode(packet, buf_ptr, uint8_t, type_priority, identity);
    PrrtPacketField_encode(packet, buf_ptr, uint8_t, index, identity);
    PrrtPacketField_encode(packet, buf_ptr, prrtSequenceNumber_t, sequenceNumber, htons);
215

216
    return buf_ptr;
217 218
}

219
bool PrrtPacket_decode(void *srcBuffer, uint16_t srcBufferSize, PrrtPacket *targetPacket) {
220 221 222 223

    // targetPacket is uninitialized, so we need to set the reference count
    targetPacket->refcount = 1;

224
    prrtPacketLength_t payload_len = (prrtPacketLength_t) (srcBufferSize - PRRT_PACKET_ENCODED_GENERAL_HEADER_LENGTH);
Andreas Schmidt's avatar
Andreas Schmidt committed
225 226 227 228 229
    targetPacket->type_priority = *(uint8_t *) srcBuffer;
    srcBuffer += 1;
    uint8_t *index_ptr = (uint8_t *) srcBuffer;
    targetPacket->index = *index_ptr;
    srcBuffer += 1;
230 231 232
    prrtSequenceNumber_t *seqno_prt = (prrtSequenceNumber_t *) srcBuffer;
    targetPacket->sequenceNumber = ntohs(*seqno_prt);
    srcBuffer += sizeof(prrtSequenceNumber_t);
233

Andreas Schmidt's avatar
Andreas Schmidt committed
234
    void *payload_buffer = calloc(1, payload_len);
235 236
    check_mem(payload_buffer);

Andreas Schmidt's avatar
Andreas Schmidt committed
237
    targetPacket->payload = payload_buffer;
238
    targetPacket->payloadLength = payload_len;
Andreas Schmidt's avatar
Andreas Schmidt committed
239

240
    prrtPacketType_t packetType = PrrtPacket_type(targetPacket);
241
    if (packetType == PACKET_TYPE_DATA) {
Andreas Schmidt's avatar
Andreas Schmidt committed
242 243
        srcBuffer = decode_data_header(srcBuffer, payload_buffer);
        PrrtPacket_copy_buffer_to_payload(targetPacket, srcBuffer, PRRT_PACKET_DATA_HEADER_SIZE);
244
    } else if (packetType == PACKET_TYPE_REDUNDANCY) {
Andreas Schmidt's avatar
Andreas Schmidt committed
245 246
        srcBuffer = decode_redundancy_header(srcBuffer, payload_buffer);
        PrrtPacket_copy_buffer_to_payload(targetPacket, srcBuffer, PRRT_PACKET_REDUNDANCY_HEADER_SIZE);
247
    } else if (packetType == PACKET_TYPE_FEEDBACK) {
248
        decode_feedback_header(srcBuffer, payload_buffer);
Andreas Schmidt's avatar
Andreas Schmidt committed
249
    } else {
250
        printf("NOT IMPLEMENTED\n");
Andreas Schmidt's avatar
Andreas Schmidt committed
251
    }
252
    return true;
253 254

    error:
255
    return false;
Andreas Schmidt's avatar
Andreas Schmidt committed
256
}
257

258 259
#define PrrtPacketField_decode(payload, buf_ptr, field_t, field, conversion) \
    do { \
260 261 262 263 264
        field_t PrrtPacketField_decode_value; \
        /* memcpy to avoid misaligned access.
         * The compiler will optimize this into appropriate mov instructions */ \
        memcpy(&PrrtPacketField_decode_value, buf_ptr, sizeof(field_t)); \
        (payload)->field = conversion(PrrtPacketField_decode_value); \
265 266 267
        (buf_ptr) += sizeof(field_t); \
    } while (false)

268
void *decode_redundancy_header(void *dstBuffer, const void *srcBuffer) {
Andreas Schmidt's avatar
Andreas Schmidt committed
269
    PrrtPacketRedundancyPayload *redundancyPayload = (PrrtPacketRedundancyPayload *) srcBuffer;
270

271 272 273 274
    PrrtPacketField_decode(redundancyPayload, dstBuffer, prrtSequenceNumber_t, baseSequenceNumber, ntohs);
    PrrtPacketField_decode(redundancyPayload, dstBuffer, prrtTimestamp_t, timestamp, ntohl);
    PrrtPacketField_decode(redundancyPayload, dstBuffer, uint8_t, n, identity);
    PrrtPacketField_decode(redundancyPayload, dstBuffer, uint8_t, k, identity);
275

Andreas Schmidt's avatar
Andreas Schmidt committed
276
    return dstBuffer;
277 278
}

279
void *decode_feedback_header(void *dstBuffer, const void *srcBuffer) {
Andreas Schmidt's avatar
Andreas Schmidt committed
280
    PrrtPacketFeedbackPayload *feedback_payload = (PrrtPacketFeedbackPayload *) srcBuffer;
281

282 283 284 285 286 287 288 289 290 291
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtTimedelta_t, groupRTT_us, ntohl);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtTimestamp_t, forwardTripTimestamp_us, ntohl);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, erasureCount, ntohs);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, packetCount, ntohs);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, gapLength, ntohs);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, gapCount, ntohs);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, burstLength, ntohs);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, burstCount, ntohs);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtPacketType_t, ackPacketType, identity);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, ackSequenceNumber, ntohs);
292

Andreas Schmidt's avatar
Andreas Schmidt committed
293
    return dstBuffer;
Andreas Schmidt's avatar
Andreas Schmidt committed
294 295
}

296
void *decode_data_header(void *dstBuffer, const void *srcBuffer) {
Andreas Schmidt's avatar
Andreas Schmidt committed
297
    PrrtPacketDataPayload *data_payload = (PrrtPacketDataPayload *) srcBuffer;
Andreas Schmidt's avatar
Andreas Schmidt committed
298

299 300 301 302
    PrrtPacketField_decode(data_payload, dstBuffer, prrtPacketLength_t, dataLength, ntohl);
    PrrtPacketField_decode(data_payload, dstBuffer, prrtTimestamp_t, timestamp, ntohl);
    PrrtPacketField_decode(data_payload, dstBuffer, prrtTimedelta_t, groupRTprop_us, ntohl);
    PrrtPacketField_decode(data_payload, dstBuffer, prrtTimedelta_t, packetTimeout_us, ntohl);
303
    PrrtPacketField_decode(data_payload, dstBuffer, prrtDeliveryRate_t, btlbw, ntohl);
Andreas Schmidt's avatar
Andreas Schmidt committed
304

Andreas Schmidt's avatar
Andreas Schmidt committed
305
    return dstBuffer;
306 307
}

308
int PrrtPacket_destroy(PrrtPacket *packet) {
309 310 311 312 313 314 315 316 317 318

    prrtRefcount_t refcount = __sync_fetch_and_sub(&packet->refcount, 1);
    if (refcount > 1) {
        return 0;
    } else {
        if (packet->payload != NULL) {
            free(packet->payload);
        }
        free(packet);
        return 0;
319
    }
320 321
}

322
PrrtPacket *PrrtPacket_create_data_packet(uint8_t priority, const void *payloadPointer,
rna's avatar
rna committed
323
                                          prrtPacketLength_t dataLength, prrtSequenceNumber_t sequenceNumber,
324
                                          prrtTimedelta_t targetDelay) {
Andreas Schmidt's avatar
Andreas Schmidt committed
325
    PrrtPacket *packet = create_header(priority, sequenceNumber,
326 327
                                       (prrtPacketLength_t) (dataLength + PRRT_PACKET_DATA_HEADER_SIZE),
                                       PACKET_TYPE_DATA, 0);
328

329
    PrrtPacketDataPayload *dataPayload = calloc(1, packet->payloadLength);
Andreas Schmidt's avatar
Andreas Schmidt committed
330 331
    check_mem(dataPayload);
    packet->payload = dataPayload;
332

rna's avatar
rna committed
333
    dataPayload->dataLength = dataLength;
334
    dataPayload->timestamp = PrrtClock_get_current_time_us();
335
    dataPayload->packetTimeout_us = dataPayload->timestamp + targetDelay;
336
    dataPayload->groupRTprop_us = 0;
Andreas Schmidt's avatar
Andreas Schmidt committed
337
    PrrtPacket_copy_buffer_to_payload(packet, payloadPointer, PRRT_PACKET_DATA_HEADER_SIZE)
338

Andreas Schmidt's avatar
Andreas Schmidt committed
339
    return packet;
340

341
    error:
342
    PERROR("Could not create packet.%s", "");
Andreas Schmidt's avatar
Andreas Schmidt committed
343
    return NULL;
344 345
}

346 347
PrrtPacket *PrrtPacket_reconstruct_data_packet(PrrtPacketDataPayload *payload, prrtIndex_t index,
                                               prrtSequenceNumber_t sequenceNumber) {
rna's avatar
rna committed
348 349 350
    prrtPacketLength_t dataLength = payload->dataLength;

    PrrtPacket *packet = create_header(0, sequenceNumber,
351 352
                                       (prrtPacketLength_t) (dataLength + PRRT_PACKET_DATA_HEADER_SIZE),
                                       PACKET_TYPE_DATA, index);
rna's avatar
rna committed
353 354 355 356 357 358 359 360 361 362 363 364 365 366

    PrrtPacketDataPayload *dataPayload = calloc(1, packet->payloadLength);
    check_mem(dataPayload);
    packet->payload = dataPayload;

    PrrtPacket_copy_buffer_to_payload(packet, payload, 0)

    return packet;
    error:
    PERROR("Could not reconstruct packet.%s", "");
    return NULL;

}

367 368 369
PrrtPacket *PrrtPacket_create_redundancy_packet(uint8_t priority, void *payloadPointer,
                                                prrtPacketLength_t payloadLength,
                                                prrtSequenceNumber_t sequenceNumber, uint8_t index,
370
                                                prrtSequenceNumber_t baseSequenceNumber,
371
                                                PrrtCodingConfiguration *codingParams) {
Andreas Schmidt's avatar
Andreas Schmidt committed
372
    PrrtPacket *packet = create_header(priority, sequenceNumber,
373
                                       (prrtPacketLength_t) (payloadLength + PRRT_PACKET_REDUNDANCY_HEADER_SIZE),
Andreas Schmidt's avatar
Andreas Schmidt committed
374
                                       PACKET_TYPE_REDUNDANCY, index);
375

376
    PrrtPacketRedundancyPayload *redundancyPayload = calloc(1, packet->payloadLength);
Andreas Schmidt's avatar
Andreas Schmidt committed
377 378
    check_mem(redundancyPayload);
    packet->payload = redundancyPayload;
379

380
    redundancyPayload->baseSequenceNumber = baseSequenceNumber;
381
    redundancyPayload->timestamp = PrrtClock_get_current_time_us();
382 383
    redundancyPayload->k = codingParams->k;
    redundancyPayload->n = codingParams->n;
Andreas Schmidt's avatar
Andreas Schmidt committed
384
    PrrtPacket_copy_buffer_to_payload(packet, payloadPointer, PRRT_PACKET_REDUNDANCY_HEADER_SIZE);
385

Andreas Schmidt's avatar
Andreas Schmidt committed
386
    return packet;
387 388

    error:
389
    PERROR("Out of memory%s.", "");
390
    return NULL;
391 392
}

393 394 395 396 397 398 399
PrrtPacket *
PrrtPacket_create_feedback_packet(uint8_t priority, prrtSequenceNumber_t sequenceNumber, prrtTimedelta_t groupRTT,
                                  prrtSequenceNumber_t gapLength, prrtSequenceNumber_t gapCount,
                                  prrtSequenceNumber_t burstLength, prrtSequenceNumber_t burstCount,
                                  prrtTimestamp_t forwardTripTime, prrtSequenceNumber_t erasureCount,
                                  prrtSequenceNumber_t packetCount, prrtSequenceNumber_t ackSequenceNumber,
                                  prrtPacketType_t ackPacketType) {
Andreas Schmidt's avatar
Andreas Schmidt committed
400
    PrrtPacket *packet = create_header(priority, sequenceNumber, PRRT_PACKET_FEEDBACK_HEADER_SIZE, PACKET_TYPE_FEEDBACK,
401
                                       0);
Andreas Schmidt's avatar
Andreas Schmidt committed
402

403
    PrrtPacketFeedbackPayload *payload = calloc(1, packet->payloadLength);
404
    check_mem(payload);
Andreas Schmidt's avatar
Andreas Schmidt committed
405
    packet->payload = payload;
406

407 408
    payload->groupRTT_us = groupRTT;
    payload->forwardTripTimestamp_us = forwardTripTime;
Andreas Schmidt's avatar
Andreas Schmidt committed
409 410
    payload->erasureCount = erasureCount;
    payload->packetCount = packetCount;
411 412 413 414
    payload->gapLength = gapLength;
    payload->gapCount = gapCount;
    payload->burstLength = burstLength;
    payload->burstCount = burstCount;
415 416
    payload->ackSequenceNumber = ackSequenceNumber;
    payload->ackPacketType = ackPacketType;
417

Andreas Schmidt's avatar
Andreas Schmidt committed
418
    return packet;
419 420

    error:
421
    return NULL;
422
}