| 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 |
#ifdef HAVE_CONFIG_H |
|---|
| 27 |
# include "config.h" |
|---|
| 28 |
#endif |
|---|
| 29 |
|
|---|
| 30 |
#include <vlc_common.h> |
|---|
| 31 |
#include <vlc_block.h> |
|---|
| 32 |
|
|---|
| 33 |
#include <vlc_network.h> |
|---|
| 34 |
#include <vlc_sout.h> |
|---|
| 35 |
#include "rtp.h" |
|---|
| 36 |
|
|---|
| 37 |
#include <assert.h> |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
struct rtcp_sender_t |
|---|
| 54 |
{ |
|---|
| 55 |
size_t length; |
|---|
| 56 |
uint8_t payload[28 + 8 + (2 * 257) + 8]; |
|---|
| 57 |
int handle; |
|---|
| 58 |
|
|---|
| 59 |
uint32_t packets; |
|---|
| 60 |
uint32_t bytes; |
|---|
| 61 |
unsigned counter; |
|---|
| 62 |
}; |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
rtcp_sender_t *OpenRTCP (vlc_object_t *obj, int rtp_fd, int proto, |
|---|
| 66 |
bool mux) |
|---|
| 67 |
{ |
|---|
| 68 |
rtcp_sender_t *rtcp; |
|---|
| 69 |
uint8_t *ptr; |
|---|
| 70 |
int fd; |
|---|
| 71 |
char src[NI_MAXNUMERICHOST]; |
|---|
| 72 |
int sport; |
|---|
| 73 |
|
|---|
| 74 |
if (net_GetSockAddress (rtp_fd, src, &sport)) |
|---|
| 75 |
return NULL; |
|---|
| 76 |
|
|---|
| 77 |
if (mux) |
|---|
| 78 |
{ |
|---|
| 79 |
|
|---|
| 80 |
#ifndef WIN32 |
|---|
| 81 |
fd = dup (rtp_fd); |
|---|
| 82 |
#else |
|---|
| 83 |
WSAPROTOCOL_INFO info; |
|---|
| 84 |
WSADuplicateSocket (rtp_fd, GetCurrentProcessId (), &info); |
|---|
| 85 |
fd = WSASocket (info.iAddressFamily, info.iSocketType, info.iProtocol, |
|---|
| 86 |
&info, 0, 0); |
|---|
| 87 |
#endif |
|---|
| 88 |
} |
|---|
| 89 |
else |
|---|
| 90 |
{ |
|---|
| 91 |
|
|---|
| 92 |
char dst[NI_MAXNUMERICHOST]; |
|---|
| 93 |
int dport; |
|---|
| 94 |
|
|---|
| 95 |
if (net_GetPeerAddress (rtp_fd, dst, &dport)) |
|---|
| 96 |
return NULL; |
|---|
| 97 |
|
|---|
| 98 |
sport++; |
|---|
| 99 |
dport++; |
|---|
| 100 |
|
|---|
| 101 |
fd = net_OpenDgram (obj, src, sport, dst, dport, AF_UNSPEC, proto); |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
if (fd == -1) |
|---|
| 105 |
return NULL; |
|---|
| 106 |
|
|---|
| 107 |
rtcp = malloc (sizeof (*rtcp)); |
|---|
| 108 |
if (rtcp == NULL) |
|---|
| 109 |
{ |
|---|
| 110 |
net_Close (fd); |
|---|
| 111 |
return NULL; |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
rtcp->handle = fd; |
|---|
| 115 |
rtcp->bytes = rtcp->packets = rtcp->counter = 0; |
|---|
| 116 |
|
|---|
| 117 |
ptr = (uint8_t *)strchr (src, '%'); |
|---|
| 118 |
if (ptr != NULL) |
|---|
| 119 |
*ptr = '\0'; |
|---|
| 120 |
|
|---|
| 121 |
ptr = rtcp->payload; |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
ptr[0] = 2 << 6; |
|---|
| 125 |
ptr[1] = 200; |
|---|
| 126 |
SetWBE (ptr + 2, 6); |
|---|
| 127 |
memset (ptr + 4, 0, 4); |
|---|
| 128 |
SetQWBE (ptr + 8, NTPtime64 ()); |
|---|
| 129 |
memset (ptr + 16, 0, 12); |
|---|
| 130 |
ptr += 28; |
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
uint8_t *sdes = ptr; |
|---|
| 134 |
ptr[0] = (2 << 6) | 1; |
|---|
| 135 |
ptr[1] = 202; |
|---|
| 136 |
uint8_t *lenptr = ptr + 2; |
|---|
| 137 |
memset (ptr + 4, 0, 4); |
|---|
| 138 |
ptr += 8; |
|---|
| 139 |
|
|---|
| 140 |
ptr[0] = 1; |
|---|
| 141 |
assert (NI_MAXNUMERICHOST <= 256); |
|---|
| 142 |
ptr[1] = strlen (src); |
|---|
| 143 |
memcpy (ptr + 2, src, ptr[1]); |
|---|
| 144 |
ptr += ptr[1] + 2; |
|---|
| 145 |
|
|---|
| 146 |
static const char tool[] = PACKAGE_STRING; |
|---|
| 147 |
ptr[0] = 6; |
|---|
| 148 |
ptr[1] = (sizeof (tool) > 256) ? 255 : (sizeof (tool) - 1); |
|---|
| 149 |
memcpy (ptr + 2, tool, ptr[1]); |
|---|
| 150 |
ptr += ptr[1] + 2; |
|---|
| 151 |
|
|---|
| 152 |
while ((ptr - sdes) & 3) |
|---|
| 153 |
*ptr++ = 0; |
|---|
| 154 |
SetWBE (lenptr, (ptr - sdes - 1) >> 2); |
|---|
| 155 |
|
|---|
| 156 |
rtcp->length = ptr - rtcp->payload; |
|---|
| 157 |
return rtcp; |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
void CloseRTCP (rtcp_sender_t *rtcp) |
|---|
| 162 |
{ |
|---|
| 163 |
if (rtcp == NULL) |
|---|
| 164 |
return; |
|---|
| 165 |
|
|---|
| 166 |
uint8_t *ptr = rtcp->payload; |
|---|
| 167 |
uint64_t now64 = NTPtime64 (); |
|---|
| 168 |
SetQWBE (ptr + 8, now64); |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
ptr += rtcp->length; |
|---|
| 172 |
ptr[0] = (2 << 6) | 1; |
|---|
| 173 |
ptr[1] = 203; |
|---|
| 174 |
SetWBE (ptr + 2, 1); |
|---|
| 175 |
memcpy (ptr + 4, rtcp->payload + 4, 4); |
|---|
| 176 |
rtcp->length += 8; |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
send (rtcp->handle, rtcp->payload, rtcp->length, 0); |
|---|
| 181 |
net_Close (rtcp->handle); |
|---|
| 182 |
free (rtcp); |
|---|
| 183 |
} |
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
void SendRTCP (rtcp_sender_t *restrict rtcp, const block_t *rtp) |
|---|
| 187 |
{ |
|---|
| 188 |
if ((rtcp == NULL) |
|---|
| 189 |
|| (rtp->i_buffer < 12)) |
|---|
| 190 |
return; |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
rtcp->packets++; |
|---|
| 194 |
rtcp->bytes += rtp->i_buffer; |
|---|
| 195 |
rtcp->counter += rtp->i_buffer; |
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
if ((rtcp->counter / 80) < rtcp->length) |
|---|
| 199 |
return; |
|---|
| 200 |
|
|---|
| 201 |
uint8_t *ptr = rtcp->payload; |
|---|
| 202 |
uint32_t last = GetDWBE (ptr + 8); |
|---|
| 203 |
uint64_t now64 = NTPtime64 (); |
|---|
| 204 |
if ((now64 >> 32) < (last + 5)) |
|---|
| 205 |
return; |
|---|
| 206 |
|
|---|
| 207 |
memcpy (ptr + 4, rtp->p_buffer + 8, 4); |
|---|
| 208 |
SetQWBE (ptr + 8, now64); |
|---|
| 209 |
memcpy (ptr + 16, rtp->p_buffer + 4, 4); |
|---|
| 210 |
SetDWBE (ptr + 20, rtcp->packets); |
|---|
| 211 |
SetDWBE (ptr + 24, rtcp->bytes); |
|---|
| 212 |
memcpy (ptr + 28 + 4, rtp->p_buffer + 8, 4); |
|---|
| 213 |
|
|---|
| 214 |
if (send (rtcp->handle, ptr, rtcp->length, 0) == (ssize_t)rtcp->length) |
|---|
| 215 |
rtcp->counter = 0; |
|---|
| 216 |
} |
|---|