opengl: factorize program creation
[vlc.git] / modules / stream_out / rtcp.c
blobd989efac4426e0b1feaa926ad1aa4645a7203b4c
1 /*****************************************************************************
2 * rtcp.c: RTCP stream output support
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *****************************************************************************/
21 /*****************************************************************************
22 * Preamble
23 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <vlc_common.h>
30 #include <vlc_block.h>
32 #include <vlc_network.h>
33 #include <vlc_sout.h>
34 #include <vlc_fs.h>
35 #include "rtp.h"
37 #include <assert.h>
39 #ifndef SOL_IP
40 # define SOL_IP IPPROTO_IP
41 #endif
44 * NOTE on RTCP implementation:
45 * - there is a single sender (us), no conferencing here! => n = sender = 1,
46 * - as such we need not bother to include Receiver Reports,
47 * - in unicast case, there is a single receiver => members = 1 + 1 = 2,
48 * and obviously n > 25% of members,
49 * - in multicast case, we do not want to maintain the number of receivers
50 * and we assume it is big (i.e. than 3) because that's what broadcasting is
51 * all about,
52 * - it is assumed we_sent = true (could be wrong), since we are THE sender,
53 * - we always send SR + SDES, while running,
54 * - FIXME: we do not implement separate rate limiting for SDES,
55 * - we do not implement any profile-specific extensions for the time being.
57 struct rtcp_sender_t
59 size_t length; /* RTCP packet length */
60 uint8_t payload[28 + 8 + (2 * 257) + 8];
61 int handle; /* RTCP socket handler */
63 uint32_t packets; /* RTP packets sent */
64 uint32_t bytes; /* RTP bytes sent */
65 unsigned counter; /* RTP packets sent since last RTCP packet */
69 rtcp_sender_t *OpenRTCP (vlc_object_t *obj, int rtp_fd, int proto,
70 bool mux)
72 rtcp_sender_t *rtcp;
73 uint8_t *ptr;
74 int fd;
75 char src[NI_MAXNUMERICHOST];
76 int sport;
78 if (net_GetSockAddress (rtp_fd, src, &sport))
79 return NULL;
81 if (mux)
83 /* RTP/RTCP mux: duplicate the socket */
84 #ifndef _WIN32
85 fd = vlc_dup (rtp_fd);
86 #else
87 WSAPROTOCOL_INFO info;
88 WSADuplicateSocket (rtp_fd, GetCurrentProcessId (), &info);
89 fd = WSASocket (info.iAddressFamily, info.iSocketType, info.iProtocol,
90 &info, 0, 0);
91 #endif
93 else
95 /* RTCP on a separate port */
96 char dst[NI_MAXNUMERICHOST];
97 int dport;
99 if (net_GetPeerAddress (rtp_fd, dst, &dport))
100 return NULL;
102 sport++;
103 dport++;
105 fd = net_OpenDgram (obj, src, sport, dst, dport, proto);
106 if (fd != -1)
108 /* Copy the multicast IPv4 TTL value (useless for IPv6) */
109 int ttl;
110 socklen_t len = sizeof (ttl);
112 if (!getsockopt (rtp_fd, SOL_IP, IP_MULTICAST_TTL, &ttl, &len))
113 setsockopt (fd, SOL_IP, IP_MULTICAST_TTL, &ttl, len);
115 /* Ignore all incoming RTCP-RR packets */
116 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0 }, sizeof (int));
120 if (fd == -1)
121 return NULL;
123 rtcp = malloc (sizeof (*rtcp));
124 if (rtcp == NULL)
126 net_Close (fd);
127 return NULL;
130 rtcp->handle = fd;
131 rtcp->bytes = rtcp->packets = rtcp->counter = 0;
133 ptr = (uint8_t *)strchr (src, '%');
134 if (ptr != NULL)
135 *ptr = '\0'; /* remove scope ID frop IPv6 addresses */
137 ptr = rtcp->payload;
139 /* Sender report */
140 ptr[0] = 2 << 6; /* V = 2, P = RC = 0 */
141 ptr[1] = 200; /* payload type: Sender Report */
142 SetWBE (ptr + 2, 6); /* length = 6 (7 double words) */
143 memset (ptr + 4, 0, 4); /* SSRC unknown yet */
144 SetQWBE (ptr + 8, NTPtime64 ());
145 memset (ptr + 16, 0, 12); /* timestamp and counters */
146 ptr += 28;
148 /* Source description */
149 uint8_t *sdes = ptr;
150 ptr[0] = (2 << 6) | 1; /* V = 2, P = 0, SC = 1 */
151 ptr[1] = 202; /* payload type: Source Description */
152 uint8_t *lenptr = ptr + 2;
153 memset (ptr + 4, 0, 4); /* SSRC unknown yet */
154 ptr += 8;
156 ptr[0] = 1; /* CNAME - mandatory */
157 assert (NI_MAXNUMERICHOST <= 256);
158 ptr[1] = strlen (src);
159 memcpy (ptr + 2, src, ptr[1]);
160 ptr += ptr[1] + 2;
162 static const char tool[] = PACKAGE_STRING;
163 ptr[0] = 6; /* TOOL */
164 ptr[1] = (sizeof (tool) > 256) ? 255 : (sizeof (tool) - 1);
165 memcpy (ptr + 2, tool, ptr[1]);
166 ptr += ptr[1] + 2;
168 while ((ptr - sdes) & 3) /* 32-bits padding */
169 *ptr++ = 0;
170 SetWBE (lenptr, (ptr - sdes - 1) >> 2);
172 rtcp->length = ptr - rtcp->payload;
173 return rtcp;
177 void CloseRTCP (rtcp_sender_t *rtcp)
179 if (rtcp == NULL)
180 return;
182 uint8_t *ptr = rtcp->payload;
183 uint64_t now64 = NTPtime64 ();
184 SetQWBE (ptr + 8, now64); /* Update the Sender Report timestamp */
186 /* Bye */
187 ptr += rtcp->length;
188 ptr[0] = (2 << 6) | 1; /* V = 2, P = 0, SC = 1 */
189 ptr[1] = 203; /* payload type: Bye */
190 SetWBE (ptr + 2, 1);
191 memcpy (ptr + 4, rtcp->payload + 4, 4); /* Copy SSRC from Sender Report */
192 rtcp->length += 8;
194 /* We are THE sender, so we are more important than anybody else, so
195 * we can afford not to check bandwidth constraints here. */
196 send (rtcp->handle, rtcp->payload, rtcp->length, 0);
197 net_Close (rtcp->handle);
198 free (rtcp);
202 void SendRTCP (rtcp_sender_t *restrict rtcp, const block_t *rtp)
204 if ((rtcp == NULL) /* RTCP sender off */
205 || (rtp->i_buffer < 12)) /* too short RTP packet */
206 return;
208 /* Updates statistics */
209 rtcp->packets++;
210 rtcp->bytes += rtp->i_buffer;
211 rtcp->counter += rtp->i_buffer;
213 /* 1.25% rate limit */
214 if ((rtcp->counter / 80) < rtcp->length)
215 return;
217 uint8_t *ptr = rtcp->payload;
218 uint32_t last = GetDWBE (ptr + 8); // last RTCP SR send time
219 uint64_t now64 = NTPtime64 ();
220 if ((now64 >> 32) < (last + 5))
221 return; // no more than one SR every 5 seconds
223 memcpy (ptr + 4, rtp->p_buffer + 8, 4); /* SR SSRC */
224 SetQWBE (ptr + 8, now64);
225 memcpy (ptr + 16, rtp->p_buffer + 4, 4); /* RTP timestamp */
226 SetDWBE (ptr + 20, rtcp->packets);
227 SetDWBE (ptr + 24, rtcp->bytes);
228 memcpy (ptr + 28 + 4, rtp->p_buffer + 8, 4); /* SDES SSRC */
230 if (send (rtcp->handle, ptr, rtcp->length, 0) == (ssize_t)rtcp->length)
231 rtcp->counter = 0;