forked from OpenResearchInstitute/ka9q-radio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulticast.h
176 lines (149 loc) · 4.64 KB
/
multicast.h
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// $Id: multicast.h,v 1.25 2018/12/30 13:17:15 karn Exp karn $
// Multicast and RTP functions, constants and structures
// Not every RTP module uses these yet, they need to be revised
// Copyright 2018, Phil Karn, KA9Q
#ifndef _MULTICAST_H
#define _MULTICAST_H 1
#include <stdint.h>
#include <sys/socket.h>
#include <netdb.h>
#include <assert.h>
#define NTP_EPOCH 2208988800UL // Seconds between Jan 1 1900 and Jan 1 1970
#define RTP_MIN_SIZE 12 // min size of RTP header
#define RTP_VERS 2
#define RTP_MARKER 0x80 // Marker flag in mpt field
#define IQ_PT12 (95) // NON-standard payload for 12-bit packed integers, BIG ENDIAN
#define IQ_PT (97) // NON-standard payload type for my raw I/Q stream - 16 bit little endian
#define IQ_PT8 (98) // NON-standard payload type for my raw I/Q stream - 8 bit version
#define AX25_PT (96) // NON-standard paylaod type for my raw AX.25 frames
#define PCM_MONO_PT (11)
#define PCM_STEREO_PT (10)
#define OPUS_PT (111) // Hard-coded NON-standard payload type for OPUS (should be dynamic with sdp)
// Internal representation of RTP header -- NOT what's on wire!
struct rtp_header {
int version;
uint8_t type;
uint16_t seq;
uint32_t timestamp;
uint32_t ssrc;
unsigned int marker:1;
unsigned int pad:1;
unsigned int extension:1;
int cc;
uint32_t csrc[15];
};
// RTP sender/receiver state
struct rtp_state {
uint32_t ssrc;
int init;
uint16_t seq;
uint32_t timestamp;
long long packets;
long long bytes;
long long drops;
long long dupes;
};
// Internal format of sender report segment
struct rtcp_sr {
unsigned int ssrc;
long long ntp_timestamp;
unsigned int rtp_timestamp;
unsigned int packet_count;
unsigned int byte_count;
};
// Internal format of receiver report segment
struct rtcp_rr {
unsigned int ssrc;
int lost_fract;
int lost_packets;
int highest_seq;
int jitter;
int lsr; // Last SR
int dlsr; // Delay since last SR
};
// Internal format of RTCP source description
enum sdes_type {
CNAME=1,
NAME=2,
EMAIL=3,
PHONE=4,
LOC=5,
TOOL=6,
NOTE=7,
PRIV=8,
};
// Individual source description item
struct rtcp_sdes {
enum sdes_type type;
uint32_t ssrc;
int mlen;
char message[256];
};
// For caching back conversions of binary socket structures to printable addresses
struct sockcache {
struct sockaddr_storage old_sockaddr;
char host[NI_MAXHOST];
char port[NI_MAXSERV];
};
// Convert between internal and wire representations of RTP header
void *ntoh_rtp(struct rtp_header *,void *);
void *hton_rtp(void *, struct rtp_header *);
extern char *Default_mcast_iface;
int setup_mcast(char const *target,struct sockaddr *,int output,int ttl,int offset);
extern char Default_mcast_port[];
void update_sockcache(struct sockcache *sc,struct sockaddr *sa);
// Function to process incoming RTP packet headers
// Returns number of samples dropped or skipped by silence suppression, if any
int rtp_process(struct rtp_state *state,struct rtp_header *rtp,int samples);
// Generate RTCP source description segment
unsigned char *gen_sdes(unsigned char *output,int bufsize,uint32_t ssrc,struct rtcp_sdes const *sdes,int sc);
// Generate RTCP bye segment
unsigned char *gen_bye(unsigned char *output,int bufsize,uint32_t const *ssrcs,int sc);
// Generate RTCP sender report segment
unsigned char *gen_sr(unsigned char *output,int bufsize,struct rtcp_sr const *sr,struct rtcp_rr const *rr,int rc);
// Generate RTCP receiver report segment
unsigned char *gen_rr(unsigned char *output,int bufsize,uint32_t ssrc,struct rtcp_rr const *rr,int rc);
// Utility routines for reading from, and writing integers to, network format in char buffers
static inline unsigned short get8(unsigned char const *dp){
assert(dp != NULL);
return *dp;
}
static inline unsigned short get16(unsigned char const *dp){
assert(dp != NULL);
return dp[0] << 8 | dp[1];
}
static inline unsigned long get24(unsigned char const *dp){
assert(dp != NULL);
return dp[0] << 16 | dp[1] << 8 | dp[2];
}
static inline unsigned long get32(unsigned char const *dp){
assert(dp != NULL);
return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
}
static inline unsigned char *put8(unsigned char *dp,uint8_t x){
assert(dp != NULL);
*dp++ = x;
return dp;
}
static inline unsigned char *put16(unsigned char *dp,uint16_t x){
assert(dp != NULL);
*dp++ = x >> 8;
*dp++ = x;
return dp;
}
static inline unsigned char *put24(unsigned char *dp,uint32_t x){
assert(dp != NULL);
*dp++ = x >> 16;
*dp++ = x >> 8;
*dp++ = x;
return dp;
}
static inline unsigned char *put32(unsigned char *dp,uint32_t x){
assert(dp != NULL);
*dp++ = x >> 24;
*dp++ = x >> 16;
*dp++ = x >> 8;
*dp++ = x;
return dp;
}
#endif