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