Mercurial > libavformat.hg
comparison udp.c @ 1428:7316227e64eb libavformat
Make it possible to reuse UDP socket (optional, disabled by default)
Patch by Thijs % thijsvermeir A telenet P be %
Original thread:
Subject: Re: [Ffmpeg-devel] [PATCH proposal] Reuse UDP socket v2
Date: October 28, 2006 11:09:00 AM CEDT
author | gpoirier |
---|---|
date | Sat, 28 Oct 2006 17:16:18 +0000 |
parents | 0899bfe4105c |
children | 92afee454599 |
comparison
equal
deleted
inserted
replaced
1427:f1614c754d5b | 1428:7316227e64eb |
---|---|
38 typedef struct { | 38 typedef struct { |
39 int udp_fd; | 39 int udp_fd; |
40 int ttl; | 40 int ttl; |
41 int is_multicast; | 41 int is_multicast; |
42 int local_port; | 42 int local_port; |
43 int reuse_socket; | |
43 #ifndef CONFIG_IPV6 | 44 #ifndef CONFIG_IPV6 |
44 struct ip_mreq mreq; | 45 struct ip_mreq mreq; |
45 struct sockaddr_in dest_addr; | 46 struct sockaddr_in dest_addr; |
46 #else | 47 #else |
47 struct sockaddr_storage dest_addr; | 48 struct sockaddr_storage dest_addr; |
234 * url syntax: udp://host:port[?option=val...] | 235 * url syntax: udp://host:port[?option=val...] |
235 * option: 'multicast=1' : enable multicast | 236 * option: 'multicast=1' : enable multicast |
236 * 'ttl=n' : set the ttl value (for multicast only) | 237 * 'ttl=n' : set the ttl value (for multicast only) |
237 * 'localport=n' : set the local port | 238 * 'localport=n' : set the local port |
238 * 'pkt_size=n' : set max packet size | 239 * 'pkt_size=n' : set max packet size |
240 * 'reuse=1' : enable reusing the socket | |
239 * | 241 * |
240 * @param s1 media file context | 242 * @param s1 media file context |
241 * @param uri of the remote server | 243 * @param uri of the remote server |
242 * @return zero if no error. | 244 * @return zero if no error. |
243 */ | 245 */ |
309 | 311 |
310 h->priv_data = s; | 312 h->priv_data = s; |
311 s->ttl = 16; | 313 s->ttl = 16; |
312 s->is_multicast = 0; | 314 s->is_multicast = 0; |
313 s->local_port = 0; | 315 s->local_port = 0; |
316 s->reuse_socket = 0; | |
314 p = strchr(uri, '?'); | 317 p = strchr(uri, '?'); |
315 if (p) { | 318 if (p) { |
316 s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p); | 319 s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p); |
320 s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p); | |
317 if (find_info_tag(buf, sizeof(buf), "ttl", p)) { | 321 if (find_info_tag(buf, sizeof(buf), "ttl", p)) { |
318 s->ttl = strtol(buf, NULL, 10); | 322 s->ttl = strtol(buf, NULL, 10); |
319 } | 323 } |
320 if (find_info_tag(buf, sizeof(buf), "localport", p)) { | 324 if (find_info_tag(buf, sizeof(buf), "localport", p)) { |
321 s->local_port = strtol(buf, NULL, 10); | 325 s->local_port = strtol(buf, NULL, 10); |
348 /* special case: the bind must be done on the multicast address port */ | 352 /* special case: the bind must be done on the multicast address port */ |
349 my_addr.sin_port = s->dest_addr.sin_port; | 353 my_addr.sin_port = s->dest_addr.sin_port; |
350 } else { | 354 } else { |
351 my_addr.sin_port = htons(s->local_port); | 355 my_addr.sin_port = htons(s->local_port); |
352 } | 356 } |
357 | |
358 if (s->reuse_socket) | |
359 if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0) | |
360 goto fail; | |
353 | 361 |
354 /* the bind is needed to give a port to the socket now */ | 362 /* the bind is needed to give a port to the socket now */ |
355 if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) | 363 if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) |
356 goto fail; | 364 goto fail; |
357 | 365 |