2 * This file is part of DisOrder.
3 * Copyright (C) 2009 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file clients/rtpmon.c
21 * This progam monitors the rate at which data arrives by RTP and
22 * constantly display it. It is intended for debugging only.
24 * TODO de-dupe with playrtp.
29 #include <sys/socket.h>
30 #include <sys/types.h>
32 #include <netinet/in.h>
45 #include "configuration.h"
48 /** @brief Record of one packet */
50 /** @brief When packet arrived */
53 /** @brief Serial number of first sample */
57 /** @brief Bytes per frame */
58 static unsigned bpf
= 4;
60 /** @brief Frame serial number */
61 static uint32_t serial
;
63 /** @brief Size of ring buffer */
64 #define RINGSIZE 16384
66 /** @brief Ring buffer */
67 static struct entry ring
[RINGSIZE
];
69 /** @brief Where new packets join the ring */
70 static unsigned ringtail
;
72 static const struct option options
[] = {
73 { "help", no_argument
, 0, 'h' },
74 { "version", no_argument
, 0, 'V' },
75 { "bpf", required_argument
, 0, 'b' },
79 static void help(void) {
81 " rtpmon [OPTIONS] [ADDRESS] PORT\n"
83 " --bpf, -b Bytes/frame (default 4)\n"
84 " --help, -h Display usage message\n"
85 " --version, -V Display version number\n"
91 /** @brief Compute the rate by sampling at two points in the ring buffer */
92 static double rate(unsigned earlier
, unsigned later
) {
93 const uint32_t frames
= ring
[later
].serial
- ring
[earlier
].serial
;
94 const int64_t us
= tvsub_us(ring
[later
].when
, ring
[earlier
].when
);
97 return 1000000.0 * frames
/ us
;
102 /** @brief Called to say we received some bytes
103 * @param when When we received them
104 * @param n How many frames of audio data we received
106 static void frames(const struct timeval
*when
, size_t n
) {
107 const time_t prev
= ring
[(ringtail
- 1) % RINGSIZE
].when
.tv_sec
;
109 ring
[ringtail
].when
= *when
;
110 ring
[ringtail
].serial
= serial
;
112 ringtail
= (ringtail
+ 1) % RINGSIZE
;
113 // Report once a second
114 if(prev
!= when
->tv_sec
) {
115 if(printf("%8.2f %8.2f %8.2f\n",
116 rate((ringtail
- RINGSIZE
/ 8) % RINGSIZE
,
117 (ringtail
- 1) % RINGSIZE
),
118 rate((ringtail
- RINGSIZE
/ 4) % RINGSIZE
,
119 (ringtail
- 1) % RINGSIZE
),
120 rate((ringtail
- RINGSIZE
/ 2) % RINGSIZE
,
121 (ringtail
- 1) % RINGSIZE
)) < 0
122 || fflush(stdout
) < 0)
123 fatal(errno
, "stdout");
127 int main(int argc
, char **argv
) {
129 struct addrinfo
*res
;
130 struct stringlist sl
;
132 struct ipv6_mreq mreq6
;
138 struct sockaddr_in in
;
139 struct sockaddr_in6 in6
;
141 union any_sockaddr mgroup
;
143 static const struct addrinfo prefs
= {
144 .ai_flags
= AI_PASSIVE
,
145 .ai_family
= PF_INET
,
146 .ai_socktype
= SOCK_DGRAM
,
147 .ai_protocol
= IPPROTO_UDP
149 static const int one
= 1;
152 if(!setlocale(LC_CTYPE
, ""))
153 fatal(errno
, "error calling setlocale");
154 while((n
= getopt_long(argc
, argv
, "hVb:", options
, 0)) >= 0) {
157 case 'V': version("rtpmon");
158 case 'b': bpf
= atoi(optarg
); break;
159 default: fatal(0, "invalid option");
167 /* Use command-line ADDRESS+PORT or just PORT */
172 fatal(0, "usage: rtpmon [OPTIONS] [ADDRESS] PORT");
174 if(!(res
= get_address(&sl
, &prefs
, &sockname
)))
176 /* Create the socket */
177 if((rtpfd
= socket(res
->ai_family
,
179 res
->ai_protocol
)) < 0)
180 fatal(errno
, "error creating socket");
181 /* Allow multiple listeners */
182 xsetsockopt(rtpfd
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof one
);
183 is_multicast
= multicast(res
->ai_addr
);
184 /* The multicast and unicast/broadcast cases are different enough that they
185 * are totally split. Trying to find commonality between them causes more
186 * trouble that it's worth. */
188 /* Stash the multicast group address */
189 memcpy(&mgroup
, res
->ai_addr
, res
->ai_addrlen
);
190 switch(res
->ai_addr
->sa_family
) {
192 mgroup
.in
.sin_port
= 0;
195 mgroup
.in6
.sin6_port
= 0;
198 fatal(0, "unsupported family %d", (int)res
->ai_addr
->sa_family
);
200 /* Bind to to the multicast group address */
201 if(bind(rtpfd
, res
->ai_addr
, res
->ai_addrlen
) < 0)
202 fatal(errno
, "error binding socket to %s", format_sockaddr(res
->ai_addr
));
203 /* Add multicast group membership */
204 switch(mgroup
.sa
.sa_family
) {
206 mreq
.imr_multiaddr
= mgroup
.in
.sin_addr
;
207 mreq
.imr_interface
.s_addr
= 0; /* use primary interface */
208 if(setsockopt(rtpfd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
209 &mreq
, sizeof mreq
) < 0)
210 fatal(errno
, "error calling setsockopt IP_ADD_MEMBERSHIP");
213 mreq6
.ipv6mr_multiaddr
= mgroup
.in6
.sin6_addr
;
214 memset(&mreq6
.ipv6mr_interface
, 0, sizeof mreq6
.ipv6mr_interface
);
215 if(setsockopt(rtpfd
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
,
216 &mreq6
, sizeof mreq6
) < 0)
217 fatal(errno
, "error calling setsockopt IPV6_JOIN_GROUP");
220 fatal(0, "unsupported address family %d", res
->ai_family
);
222 /* Report what we did */
223 info("listening on %s multicast group %s",
224 format_sockaddr(res
->ai_addr
), format_sockaddr(&mgroup
.sa
));
227 switch(res
->ai_addr
->sa_family
) {
229 struct sockaddr_in
*in
= (struct sockaddr_in
*)res
->ai_addr
;
231 memset(&in
->sin_addr
, 0, sizeof (struct in_addr
));
232 if(bind(rtpfd
, res
->ai_addr
, res
->ai_addrlen
) < 0)
233 fatal(errno
, "error binding socket to 0.0.0.0 port %d",
234 ntohs(in
->sin_port
));
238 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)res
->ai_addr
;
240 memset(&in6
->sin6_addr
, 0, sizeof (struct in6_addr
));
244 fatal(0, "unsupported family %d", (int)res
->ai_addr
->sa_family
);
246 if(bind(rtpfd
, res
->ai_addr
, res
->ai_addrlen
) < 0)
247 fatal(errno
, "error binding socket to %s", format_sockaddr(res
->ai_addr
));
248 /* Report what we did */
249 info("listening on %s", format_sockaddr(res
->ai_addr
));
252 struct rtp_header header
;
257 iov
[0].iov_base
= &header
;
258 iov
[0].iov_len
= sizeof header
;
259 iov
[1].iov_base
= buffer
;
260 iov
[1].iov_len
= sizeof buffer
;
261 n
= readv(rtpfd
, iov
, 2);
262 gettimeofday(&when
, 0);
268 fatal(errno
, "error reading from socket");
271 if((size_t)n
<= sizeof (struct rtp_header
)) {
272 info("ignored a short packet");
275 frames(&when
, (n
- sizeof header
) / bpf
);