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