]> git.sesse.net Git - vlc/blob - modules/stream_out/rtcp.c
rtp sout: fix DCCP socket leak
[vlc] / modules / stream_out / rtcp.c
1 /*****************************************************************************
2  * rtcp.c: RTCP stream output support
3  *****************************************************************************
4  * Copyright © 2007 Rémi Denis-Courmont
5  * $Id$
6  *
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.
11  *
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.
16  *
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  *****************************************************************************/
21
22 /*****************************************************************************
23  * Preamble
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 #ifndef SOL_IP
40 # define SOL_IP IPPROTO_IP
41 #endif
42
43 /*
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.
56  */
57 struct rtcp_sender_t
58 {
59     size_t   length;  /* RTCP packet length */
60     uint8_t  payload[28 + 8 + (2 * 257) + 8];
61     int      handle;  /* RTCP socket handler */
62
63     uint32_t packets; /* RTP packets sent */
64     uint32_t bytes;   /* RTP bytes sent */
65     unsigned counter; /* RTP packets sent since last RTCP packet */
66 };
67
68
69 rtcp_sender_t *OpenRTCP (vlc_object_t *obj, int rtp_fd, int proto,
70                          bool mux)
71 {
72     rtcp_sender_t *rtcp;
73     uint8_t *ptr;
74     int fd;
75     char src[NI_MAXNUMERICHOST];
76     int sport;
77
78     if (net_GetSockAddress (rtp_fd, src, &sport))
79         return NULL;
80
81     if (mux)
82     {
83         /* RTP/RTCP mux: duplicate the socket */
84 #ifndef WIN32
85         fd = dup (rtp_fd);
86 #elif defined(UNDER_CE)
87  #warning Muxed RTP/RTCP unimplemented!
88         fd = -1;
89 #else
90         WSAPROTOCOL_INFO info;
91         WSADuplicateSocket (rtp_fd, GetCurrentProcessId (), &info);
92         fd = WSASocket (info.iAddressFamily, info.iSocketType, info.iProtocol,
93                         &info, 0, 0);
94 #endif
95     }
96     else
97     {
98         /* RTCP on a separate port */
99         char dst[NI_MAXNUMERICHOST];
100         int dport;
101
102         if (net_GetPeerAddress (rtp_fd, dst, &dport))
103             return NULL;
104
105         sport++;
106         dport++;
107
108         fd = net_OpenDgram (obj, src, sport, dst, dport, AF_UNSPEC, proto);
109         if (fd != -1)
110         {
111             /* Copy the multicast IPv4 TTL value (useless for IPv6) */
112             int ttl;
113             socklen_t len = sizeof (ttl);
114
115             if (!getsockopt (rtp_fd, SOL_IP, IP_MULTICAST_TTL, &ttl, &len))
116                 setsockopt (fd, SOL_IP, IP_MULTICAST_TTL, &ttl, len);
117
118             /* Ignore all incoming RTCP-RR packets */
119             setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0 }, sizeof (int));
120         }
121     }
122
123     if (fd == -1)
124         return NULL;
125
126     rtcp = malloc (sizeof (*rtcp));
127     if (rtcp == NULL)
128     {
129         net_Close (fd);
130         return NULL;
131     }
132
133     rtcp->handle = fd;
134     rtcp->bytes = rtcp->packets = rtcp->counter = 0;
135
136     ptr = (uint8_t *)strchr (src, '%');
137     if (ptr != NULL)
138         *ptr = '\0'; /* remove scope ID frop IPv6 addresses */
139
140     ptr = rtcp->payload;
141
142     /* Sender report */
143     ptr[0] = 2 << 6; /* V = 2, P = RC = 0 */
144     ptr[1] = 200; /* payload type: Sender Report */
145     SetWBE (ptr + 2, 6); /* length = 6 (7 double words) */
146     memset (ptr + 4, 0, 4); /* SSRC unknown yet */
147     SetQWBE (ptr + 8, NTPtime64 ());
148     memset (ptr + 16, 0, 12); /* timestamp and counters */
149     ptr += 28;
150
151     /* Source description */
152     uint8_t *sdes = ptr;
153     ptr[0] = (2 << 6) | 1; /* V = 2, P = 0, SC = 1 */
154     ptr[1] = 202; /* payload type: Source Description */
155     uint8_t *lenptr = ptr + 2;
156     memset (ptr + 4, 0, 4); /* SSRC unknown yet */
157     ptr += 8;
158
159     ptr[0] = 1; /* CNAME - mandatory */
160     assert (NI_MAXNUMERICHOST <= 256);
161     ptr[1] = strlen (src);
162     memcpy (ptr + 2, src, ptr[1]);
163     ptr += ptr[1] + 2;
164
165     static const char tool[] = PACKAGE_STRING;
166     ptr[0] = 6; /* TOOL */
167     ptr[1] = (sizeof (tool) > 256) ? 255 : (sizeof (tool) - 1);
168     memcpy (ptr + 2, tool, ptr[1]);
169     ptr += ptr[1] + 2;
170
171     while ((ptr - sdes) & 3) /* 32-bits padding */
172         *ptr++ = 0;
173     SetWBE (lenptr, (ptr - sdes - 1) >> 2);
174
175     rtcp->length = ptr - rtcp->payload;
176     return rtcp;
177 }
178
179
180 void CloseRTCP (rtcp_sender_t *rtcp)
181 {
182     if (rtcp == NULL)
183         return;
184
185     uint8_t *ptr = rtcp->payload;
186     uint64_t now64 = NTPtime64 ();
187     SetQWBE (ptr + 8, now64); /* Update the Sender Report timestamp */
188
189     /* Bye */
190     ptr += rtcp->length;
191     ptr[0] = (2 << 6) | 1; /* V = 2, P = 0, SC = 1 */
192     ptr[1] = 203; /* payload type: Bye */
193     SetWBE (ptr + 2, 1);
194     memcpy (ptr + 4, rtcp->payload + 4, 4); /* Copy SSRC from Sender Report */
195     rtcp->length += 8;
196
197     /* We are THE sender, so we are more important than anybody else, so
198      * we can afford not to check bandwidth constraints here. */
199     send (rtcp->handle, rtcp->payload, rtcp->length, 0);
200     net_Close (rtcp->handle);
201     free (rtcp);
202 }
203
204
205 void SendRTCP (rtcp_sender_t *restrict rtcp, const block_t *rtp)
206 {
207     if ((rtcp == NULL) /* RTCP sender off */
208      || (rtp->i_buffer < 12)) /* too short RTP packet */
209         return;
210
211     /* Updates statistics */
212     rtcp->packets++;
213     rtcp->bytes += rtp->i_buffer;
214     rtcp->counter += rtp->i_buffer;
215
216     /* 1.25% rate limit */
217     if ((rtcp->counter / 80) < rtcp->length)
218         return;
219
220     uint8_t *ptr = rtcp->payload;
221     uint32_t last = GetDWBE (ptr + 8); // last RTCP SR send time
222     uint64_t now64 = NTPtime64 ();
223     if ((now64 >> 32) < (last + 5))
224         return; // no more than one SR every 5 seconds
225
226     memcpy (ptr + 4, rtp->p_buffer + 8, 4); /* SR SSRC */
227     SetQWBE (ptr + 8, now64);
228     memcpy (ptr + 16, rtp->p_buffer + 4, 4); /* RTP timestamp */
229     SetDWBE (ptr + 20, rtcp->packets);
230     SetDWBE (ptr + 24, rtcp->bytes);
231     memcpy (ptr + 28 + 4, rtp->p_buffer + 8, 4); /* SDES SSRC */
232
233     if (send (rtcp->handle, ptr, rtcp->length, 0) == (ssize_t)rtcp->length)
234         rtcp->counter = 0;
235 }