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