06ad34d5822aa84c4985089dfa691c01dbc1f2bb
2 * UDP prototype streaming system
3 * Copyright (c) 2000 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
29 int max_payload_size
; /* in bytes */
32 #define UDP_TX_BUF_SIZE 32768
34 /* put it in UDP context */
35 static struct sockaddr_in dest_addr
;
37 /* return non zero if error */
38 static int udp_open(URLContext
*h
, const char *uri
, int flags
)
41 struct sockaddr_in my_addr
;
44 int port
, udp_socket
, tmp
;
50 if (!(flags
& URL_WRONLY
))
53 /* fill the dest addr */
55 if (!strstart(p
, "udp:", &p
))
60 memcpy(hostname
, p
, q
- p
);
61 hostname
[q
- p
] = '\0';
62 port
= strtol(q
+1, NULL
, 10);
66 dest_addr
.sin_family
= AF_INET
;
67 dest_addr
.sin_port
= htons(port
);
68 if ((inet_aton(hostname
, &dest_addr
.sin_addr
)) == 0) {
69 hp
= gethostbyname(hostname
);
72 memcpy ((char *) &dest_addr
.sin_addr
, hp
->h_addr
, hp
->h_length
);
75 udp_socket
= socket(PF_INET
, SOCK_DGRAM
, 0);
79 my_addr
.sin_family
= AF_INET
;
80 my_addr
.sin_port
= htons(local_port
);
81 my_addr
.sin_addr
.s_addr
= htonl (INADDR_ANY
);
83 /* the bind is needed to give a port to the socket now */
84 if (bind(udp_socket
,(struct sockaddr
*)&my_addr
, sizeof(my_addr
)) < 0)
87 /* limit the tx buf size to limit latency */
89 tmp
= UDP_TX_BUF_SIZE
;
90 if (setsockopt(udp_socket
, SOL_SOCKET
, SO_SNDBUF
, &tmp
, sizeof(tmp
)) < 0) {
91 perror("setsockopt sndbuf");
95 s
= av_malloc(sizeof(UDPContext
));
99 s
->udp_socket
= udp_socket
;
100 h
->packet_size
= 1500;
106 int udp_close(URLContext
*h
)
108 UDPContext
*s
= h
->priv_data
;
109 close(s
->udp_socket
);
113 int udp_write(URLContext
*h
, UINT8
*buf
, int size
)
115 UDPContext
*s
= h
->priv_data
;
118 /* primitive way to avoid big packets */
122 if (len
> h
->packet_size
)
123 len
= h
->packet_size
;
125 ret
= sendto (s
->udp_socket
, buf
, len
, 0,
126 (struct sockaddr
*) &dest_addr
,
136 URLProtocol udp_protocol
= {