merge
[libav.git] / libav / udp.c
CommitLineData
de6d9b64
FB
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 <errno.h>
24#include <sys/types.h>
25#include <sys/socket.h>
26#include <netinet/in.h>
27#include <arpa/inet.h>
28#include <netdb.h>
29
30#include "avformat.h"
31
32typedef struct {
33 int udp_socket;
34 int max_payload_size; /* in bytes */
35} UDPContext;
36
37#define UDP_TX_BUF_SIZE 32768
38
39/* put it in UDP context */
40static struct sockaddr_in dest_addr;
41
42/* return non zero if error */
43static int udp_open(URLContext *h, const char *uri, int flags)
44{
45 int local_port = 0;
46 struct sockaddr_in my_addr;
47 const char *p, *q;
48 char hostname[1024];
49 int port, udp_socket, tmp;
50 struct hostent *hp;
51 UDPContext *s;
52
53 h->is_streamed = 1;
54
55 if (!(flags & URL_WRONLY))
56 return -EIO;
57
58 /* fill the dest addr */
59 p = uri;
60 if (!strstart(p, "udp:", &p))
61 return -1;
62 q = strchr(p, ':');
63 if (!q)
64 return -1;
65 memcpy(hostname, p, q - p);
66 hostname[q - p] = '\0';
67 port = strtol(q+1, NULL, 10);
68 if (port <= 0)
69 return -1;
70
71 dest_addr.sin_family = AF_INET;
72 dest_addr.sin_port = htons(port);
73 if ((inet_aton(hostname, &dest_addr.sin_addr)) == 0) {
74 hp = gethostbyname(hostname);
75 if (!hp)
76 return -1;
77 memcpy ((char *) &dest_addr.sin_addr, hp->h_addr, hp->h_length);
78 }
79
80 udp_socket = socket(PF_INET, SOCK_DGRAM, 0);
81 if (udp_socket < 0)
82 return -1;
83
84 my_addr.sin_family = AF_INET;
85 my_addr.sin_port = htons(local_port);
86 my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
87
88 /* the bind is needed to give a port to the socket now */
89 if (bind(udp_socket,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
90 goto fail;
91
92 /* limit the tx buf size to limit latency */
93
94 tmp = UDP_TX_BUF_SIZE;
95 if (setsockopt(udp_socket, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
96 perror("setsockopt sndbuf");
97 goto fail;
98 }
99
100 s = malloc(sizeof(UDPContext));
101 if (!s)
102 return -ENOMEM;
103 h->priv_data = s;
104 s->udp_socket = udp_socket;
105 h->packet_size = 1500;
106 return 0;
107 fail:
108 return -EIO;
109}
110
111int udp_close(URLContext *h)
112{
113 UDPContext *s = h->priv_data;
114 close(s->udp_socket);
115 return 0;
116}
117
118int udp_write(URLContext *h, UINT8 *buf, int size)
119{
120 UDPContext *s = h->priv_data;
121 int ret, len, size1;
122
123 /* primitive way to avoid big packets */
124 size1 = size;
125 while (size > 0) {
126 len = size;
127 if (len > h->packet_size)
128 len = h->packet_size;
129
130 ret = sendto (s->udp_socket, buf, len, 0,
131 (struct sockaddr *) &dest_addr,
132 sizeof (dest_addr));
133 if (ret < 0)
134 perror("sendto");
135 buf += len;
136 size -= len;
137 }
138 return size1 - size;
139}
140
141URLProtocol udp_protocol = {
142 "udp",
143 udp_open,
144 NULL, /* read */
145 udp_write,
146 NULL, /* seek */
147 udp_close,
148};