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