1 /*****************************************************************************
2 * rtcp.c: RTCP stream output support
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *****************************************************************************/
22 /*****************************************************************************
24 *****************************************************************************/
27 #include <vlc_block.h>
29 #include <vlc_network.h>
37 * NOTE on RTCP implementation:
38 * - there is a single sender (us), no conferencing here! => n = sender = 1,
39 * - as such we need not bother to include Receiver Reports,
40 * - in unicast case, there is a single receiver => members = 1 + 1 = 2,
41 * and obviously n > 25% of members,
42 * - in multicast case, we do not want to maintain the number of receivers
43 * and we assume it is big (i.e. than 3) because that's what broadcasting is
45 * - it is assumed we_sent = true (could be wrong), since we are THE sender,
46 * - we always send SR + SDES, while running,
47 * - FIXME: we do not implement separate rate limiting for SDES,
48 * - we do not implement any profile-specific extensions for the time being.
52 size_t length; /* RTCP packet length */
53 uint8_t payload[28 + 8 + (2 * 257)];
54 int handle; /* RTCP socket handler */
56 uint32_t packets; /* RTP packets sent */
57 uint32_t bytes; /* RTP bytes sent */
58 unsigned counter; /* RTP packets sent since last RTCP packet */
62 rtcp_sender_t *OpenRTCP (vlc_object_t *obj, int rtp_fd, int proto)
66 char src[NI_MAXNUMERICHOST], dst[NI_MAXNUMERICHOST];
70 if (net_GetSockAddress (rtp_fd, src, &sport)
71 || net_GetPeerAddress (rtp_fd, dst, &dport))
77 fd = net_OpenDgram (obj, src, sport, dst, dport, AF_UNSPEC, proto);
81 rtcp = malloc (sizeof (*rtcp));
89 rtcp->bytes = rtcp->packets = rtcp->counter = 0;
91 ptr = (uint8_t *)strchr (src, '%');
93 *ptr = '\0'; /* remove scope ID frop IPv6 addresses */
98 ptr[0] = 2 << 6; /* V = 2, P = RC = 0 */
99 ptr[1] = 200; /* payload type: Sender Report */
100 SetWBE (ptr + 2, 6); /* length = 6 (7 double words) */
101 memset (ptr + 4, 0, 4); /* SSRC unknown yet */
102 SetQWBE (ptr + 8, NTPtime64 ());
103 memset (ptr + 16, 0, 12); /* timestamp and counters */
106 /* Source description */
108 ptr[0] = (2 << 6) | 1; /* V = 2, P = 0, SC = 1 */
109 ptr[1] = 202; /* payload type: Source Description */
110 uint8_t *lenptr = ptr + 2;
111 memset (ptr + 4, 0, 4); /* SSRC unknown yet */
114 ptr[0] = 1; /* CNAME - mandatory */
115 assert (NI_MAXNUMERICHOST <= 256);
116 ptr[1] = strlen (src);
117 memcpy (ptr + 2, src, ptr[1]);
120 static const char tool[] = PACKAGE_STRING;
121 ptr[0] = 6; /* TOOL */
122 ptr[1] = (sizeof (tool) > 256) ? 255 : (sizeof (tool) - 1);
123 memcpy (ptr + 2, tool, ptr[1]);
126 while ((ptr - sdes) & 3) /* 32-bits padding */
128 SetWBE (lenptr, ptr - sdes);
130 rtcp->length = ptr - rtcp->payload;
135 void CloseRTCP (rtcp_sender_t *rtcp)
140 uint8_t *ptr = rtcp->payload;
142 ptr[0] = (2 << 6) | 1; /* V = 2, P = 0, SC = 1 */
143 ptr[1] = 203; /* payload type: Bye */
145 /* SSRC is already there :) */
147 /* We are THE sender, so we are more important than anybody else, so
148 * we can afford not to check bandwidth constraints here. */
149 send (rtcp->handle, rtcp->payload, 8, 0);
150 net_Close (rtcp->handle);
154 void SendRTCP (rtcp_sender_t *restrict rtcp, const block_t *rtp)
156 if ((rtcp == NULL) /* RTCP sender off */
157 || (rtp->i_buffer < 12)) /* too short RTP packet */
160 /* Updates statistics */
162 rtcp->bytes += rtp->i_buffer;
163 rtcp->counter += rtp->i_buffer;
165 /* 1.25% rate limit */
166 if ((rtcp->counter / 80) < rtcp->length)
169 uint8_t *ptr = rtcp->payload;
170 uint32_t last = GetDWBE (ptr + 8); // last RTCP SR send time
171 uint64_t now64 = NTPtime64 ();
172 if ((now64 >> 32) < (last + 5))
173 return; // no more than one SR every 5 seconds
175 memcpy (ptr + 4, rtp->p_buffer + 8, 4); /* SR SSRC */
176 SetQWBE (ptr + 8, now64);
177 memcpy (ptr + 16, rtp->p_buffer + 4, 4); /* RTP timestamp */
178 SetDWBE (ptr + 20, rtcp->packets);
179 SetDWBE (ptr + 24, rtcp->bytes);
180 memcpy (ptr + 28 + 4, rtp->p_buffer + 8, 4); /* SDES SSRC */
182 if (send (rtcp->handle, ptr, rtcp->length, 0) == (ssize_t)rtcp->length)