aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/src/protocols/rtc/rtc_packet.h
blob: 7dc2f82c3d35df90c3be796f5c2b00128ce0fc8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright (c) 2021 Cisco and/or its affiliates.
 */

/*        data packet
 *     +-----------------------------------------+
 *     | uint64_t: timestamp                     |
 *     |                                         |
 *     +-----------------------------------------+
 *     | uint32_t: prod rate (bytes per sec)     |
 *     +-----------------------------------------+
 *     | payload                                 |
 *     | ...                                     |
 */

/*        nack packet
 *     +-----------------------------------------+
 *     | uint64_t: timestamp                     |
 *     |                                         |
 *     +-----------------------------------------+
 *     | uint32_t: prod rate (bytes per sec)     |
 *     +-----------------------------------------+
 *     | uint32_t: current seg in production     |
 *     +-----------------------------------------+
 */

#pragma once
#ifndef _WIN32
#include <arpa/inet.h>
#else
#include <hicn/transport/portability/win_portability.h>
#endif

namespace transport {

namespace protocol {

namespace rtc {

inline uint64_t _ntohll(const uint64_t *input) {
  uint64_t return_val;
  uint8_t *tmp = (uint8_t *)&return_val;

  tmp[0] = (uint8_t)(*input >> 56);
  tmp[1] = (uint8_t)(*input >> 48);
  tmp[2] = (uint8_t)(*input >> 40);
  tmp[3] = (uint8_t)(*input >> 32);
  tmp[4] = (uint8_t)(*input >> 24);
  tmp[5] = (uint8_t)(*input >> 16);
  tmp[6] = (uint8_t)(*input >> 8);
  tmp[7] = (uint8_t)(*input >> 0);

  return return_val;
}

inline uint64_t _htonll(const uint64_t *input) { return (_ntohll(input)); }

const uint32_t DATA_HEADER_SIZE = 12;  // bytes
                                       // XXX: sizeof(data_packet_t) is 16
                                       // beacuse of padding
const uint32_t NACK_HEADER_SIZE = 16;

struct data_packet_t {
  uint64_t timestamp;
  uint32_t prod_rate;

  inline uint64_t getTimestamp() const { return _ntohll(&timestamp); }
  inline void setTimestamp(uint64_t time) { timestamp = _htonll(&time); }

  inline uint32_t getProductionRate() const { return ntohl(prod_rate); }
  inline void setProductionRate(uint32_t rate) { prod_rate = htonl(rate); }
};

struct nack_packet_t {
  uint64_t timestamp;
  uint32_t prod_rate;
  uint32_t prod_seg;

  inline uint64_t getTimestamp() const { return _ntohll(&timestamp); }
  inline void setTimestamp(uint64_t time) { timestamp = _htonll(&time); }

  inline uint32_t getProductionRate() const { return ntohl(prod_rate); }
  inline void setProductionRate(uint32_t rate) { prod_rate = htonl(rate); }

  inline uint32_t getProductionSegement() const { return ntohl(prod_seg); }
  inline void setProductionSegement(uint32_t seg) { prod_seg = htonl(seg); }
};

}  // end namespace rtc

}  // end namespace protocol

}  // end namespace transport