2 * UDP prototype streaming system
3 * Copyright (c) 2000 Gerard Lantau.
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 2 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, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
31 #define UDP_TX_BUF_SIZE 32768
32 /* put it in UDP context */
33 static struct sockaddr_in dest_addr;
35 /* return non zero if error */
36 int udp_tx_open(UDPContext *s,
40 struct sockaddr_in my_addr;
43 int port, udp_socket, tmp;
46 /* fill the dest addr */
48 if (!strstart(p, "udp:", &p))
53 memcpy(hostname, p, q - p);
54 hostname[q - p] = '\0';
55 port = strtol(q+1, NULL, 10);
59 dest_addr.sin_family = AF_INET;
60 dest_addr.sin_port = htons(port);
61 if ((inet_aton(hostname, &dest_addr.sin_addr)) == 0) {
62 hp = gethostbyname(hostname);
65 memcpy ((char *) &dest_addr.sin_addr, hp->h_addr, hp->h_length);
68 udp_socket = socket(PF_INET, SOCK_DGRAM, 0);
72 my_addr.sin_family = AF_INET;
73 my_addr.sin_port = htons(local_port);
74 my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
76 /* the bind is needed to give a port to the socket now */
77 if (bind(udp_socket,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
80 /* limit the tx buf size to limit latency */
82 tmp = UDP_TX_BUF_SIZE;
83 if (setsockopt(udp_socket, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
84 perror("setsockopt sndbuf");
88 s->udp_socket = udp_socket;
89 s->max_payload_size = 1024;
95 void udp_tx_close(UDPContext *s)
100 extern int data_out_size;
102 void udp_write_data(void *opaque, UINT8 *buf, int size)
104 UDPContext *s = opaque;
107 /* primitive way to avoid big packets */
108 data_out_size += size;
111 if (len > s->max_payload_size)
112 len = s->max_payload_size;
114 ret = sendto (s->udp_socket, buf, len, 0,
115 (struct sockaddr *) &dest_addr,