Initial revision
[libav.git] / udp.c
1 /*
2 * UDP prototype streaming system
3 * Copyright (c) 2000 Gerard Lantau.
4 *
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.
9 *
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.
14 *
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.
18 */
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <netdb.h>
28
29 #include "mpegenc.h"
30
31 #define UDP_TX_BUF_SIZE 32768
32 /* put it in UDP context */
33 static struct sockaddr_in dest_addr;
34
35 /* return non zero if error */
36 int udp_tx_open(UDPContext *s,
37 const char *uri,
38 int local_port)
39 {
40 struct sockaddr_in my_addr;
41 const char *p, *q;
42 char hostname[1024];
43 int port, udp_socket, tmp;
44 struct hostent *hp;
45
46 /* fill the dest addr */
47 p = uri;
48 if (!strstart(p, "udp:", &p))
49 return -1;
50 q = strchr(p, ':');
51 if (!q)
52 return -1;
53 memcpy(hostname, p, q - p);
54 hostname[q - p] = '\0';
55 port = strtol(q+1, NULL, 10);
56 if (port <= 0)
57 return -1;
58
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);
63 if (!hp)
64 return -1;
65 memcpy ((char *) &dest_addr.sin_addr, hp->h_addr, hp->h_length);
66 }
67
68 udp_socket = socket(PF_INET, SOCK_DGRAM, 0);
69 if (udp_socket < 0)
70 return -1;
71
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);
75
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)
78 goto fail;
79
80 /* limit the tx buf size to limit latency */
81
82 tmp = UDP_TX_BUF_SIZE;
83 if (setsockopt(udp_socket, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
84 perror("setsockopt sndbuf");
85 goto fail;
86 }
87
88 s->udp_socket = udp_socket;
89 s->max_payload_size = 1024;
90 return 0;
91 fail:
92 return -1;
93 }
94
95 void udp_tx_close(UDPContext *s)
96 {
97 close(s->udp_socket);
98 }
99
100 extern int data_out_size;
101
102 void udp_write_data(void *opaque, UINT8 *buf, int size)
103 {
104 UDPContext *s = opaque;
105 int ret, len;
106
107 /* primitive way to avoid big packets */
108 data_out_size += size;
109 while (size > 0) {
110 len = size;
111 if (len > s->max_payload_size)
112 len = s->max_payload_size;
113
114 ret = sendto (s->udp_socket, buf, len, 0,
115 (struct sockaddr *) &dest_addr,
116 sizeof (dest_addr));
117 if (ret < 0)
118 perror("sendto");
119 buf += len;
120 size -= len;
121 }
122 }
123