Statistics
| Branch: | Revision:

ffmpeg / libavformat / udp.c @ 5cd62665

History | View | Annotate | Download (7.33 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * UDP prototype streaming system
3 2d225591 Fabrice Bellard
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 19720f15 Fabrice Bellard
 * 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.
9 de6d9b64 Fabrice Bellard
 *
10 19720f15 Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 de6d9b64 Fabrice Bellard
 *
15 19720f15 Fabrice Bellard
 * 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
18 de6d9b64 Fabrice Bellard
 */
19 8be1c656 Fabrice Bellard
#include "avformat.h"
20 de6d9b64 Fabrice Bellard
#include <unistd.h>
21
#include <sys/types.h>
22 b8a78f41 Michael Niedermayer
#include <sys/socket.h>
23 de6d9b64 Fabrice Bellard
#include <netinet/in.h>
24 9ddd71fc François Revol
#ifndef __BEOS__
25
# include <arpa/inet.h>
26
#else
27
# include "barpainet.h"
28
#endif
29 de6d9b64 Fabrice Bellard
#include <netdb.h>
30
31
typedef struct {
32 2d225591 Fabrice Bellard
    int udp_fd;
33
    int ttl;
34
    int is_multicast;
35
    int local_port;
36
    struct ip_mreq mreq;
37
    struct sockaddr_in dest_addr;
38 de6d9b64 Fabrice Bellard
} UDPContext;
39
40
#define UDP_TX_BUF_SIZE 32768
41
42 2d225591 Fabrice Bellard
/**
43
 * If no filename is given to av_open_input_file because you want to
44
 * get the local port first, then you must call this function to set
45
 * the remote server address.
46
 *
47
 * url syntax: udp://host:port[?option=val...]
48
 * option: 'multicast=1' : enable multicast 
49
 *         'ttl=n'       : set the ttl value (for multicast only)
50
 *         'localport=n' : set the local port
51 9899efb4 Max Krasnyansky
 *         'pkt_size=n'  : set max packet size
52 2d225591 Fabrice Bellard
 *
53
 * @param s1 media file context
54
 * @param uri of the remote server
55
 * @return zero if no error.
56
 */
57
int udp_set_remote_url(URLContext *h, const char *uri)
58
{
59
    UDPContext *s = h->priv_data;
60
    char hostname[256];
61
    int port;
62
    
63
    url_split(NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
64 de6d9b64 Fabrice Bellard
65 2d225591 Fabrice Bellard
    /* set the destination address */
66
    if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
67
        return -EIO;
68
    s->dest_addr.sin_family = AF_INET;
69
    s->dest_addr.sin_port = htons(port);
70
    return 0;
71
}
72
73
/**
74
 * Return the local port used by the UDP connexion
75
 * @param s1 media file context
76
 * @return the local port number
77
 */
78
int udp_get_local_port(URLContext *h)
79
{
80
    UDPContext *s = h->priv_data;
81
    return s->local_port;
82
}
83
84
/**
85
 * Return the udp file handle for select() usage to wait for several RTP
86
 * streams at the same time.
87
 * @param h media file context
88
 */
89
int udp_get_file_handle(URLContext *h)
90
{
91
    UDPContext *s = h->priv_data;
92
    return s->udp_fd;
93
}
94
95
/* put it in UDP context */
96 de6d9b64 Fabrice Bellard
/* return non zero if error */
97
static int udp_open(URLContext *h, const char *uri, int flags)
98
{
99 2d225591 Fabrice Bellard
    struct sockaddr_in my_addr, my_addr1;
100 de6d9b64 Fabrice Bellard
    char hostname[1024];
101 2d225591 Fabrice Bellard
    int port, udp_fd = -1, tmp;
102
    UDPContext *s = NULL;
103
    int is_output, len;
104
    const char *p;
105
    char buf[256];
106 de6d9b64 Fabrice Bellard
107
    h->is_streamed = 1;
108 9899efb4 Max Krasnyansky
    h->max_packet_size = 1472;
109 de6d9b64 Fabrice Bellard
110 2d225591 Fabrice Bellard
    is_output = (flags & URL_WRONLY);
111 de6d9b64 Fabrice Bellard
    
112 2d225591 Fabrice Bellard
    s = av_malloc(sizeof(UDPContext));
113
    if (!s)
114
        return -ENOMEM;
115
116
    h->priv_data = s;
117
    s->ttl = 16;
118
    s->is_multicast = 0;
119
    p = strchr(uri, '?');
120
    if (p) {
121
        s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
122
        if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
123
            s->ttl = strtol(buf, NULL, 10);
124
        }
125
        if (find_info_tag(buf, sizeof(buf), "localport", p)) {
126
            s->local_port = strtol(buf, NULL, 10);
127
        }
128 9899efb4 Max Krasnyansky
        if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
129
            h->max_packet_size = strtol(buf, NULL, 10);
130
        }
131 de6d9b64 Fabrice Bellard
    }
132 2d225591 Fabrice Bellard
133
    /* fill the dest addr */
134
    url_split(NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
135 de6d9b64 Fabrice Bellard
    
136 2d225591 Fabrice Bellard
    /* XXX: fix url_split */
137
    if (hostname[0] == '\0' || hostname[0] == '?') {
138
        /* only accepts null hostname if input */
139
        if (s->is_multicast || (flags & URL_WRONLY))
140
            goto fail;
141
    } else {
142
        udp_set_remote_url(h, uri);
143
    }
144 de6d9b64 Fabrice Bellard
145 2d225591 Fabrice Bellard
    udp_fd = socket(PF_INET, SOCK_DGRAM, 0);
146
    if (udp_fd < 0)
147
        goto fail;
148
149 772956c5 Fabrice Bellard
    my_addr.sin_family = AF_INET;
150
    my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
151 2d225591 Fabrice Bellard
    if (s->is_multicast && !(h->flags & URL_WRONLY)) {
152 772956c5 Fabrice Bellard
        /* special case: the bind must be done on the multicast address port */
153
        my_addr.sin_port = s->dest_addr.sin_port;
154 2d225591 Fabrice Bellard
    } else {
155
        my_addr.sin_port = htons(s->local_port);
156
    }
157 de6d9b64 Fabrice Bellard
158
    /* the bind is needed to give a port to the socket now */
159 2d225591 Fabrice Bellard
    if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) 
160 de6d9b64 Fabrice Bellard
        goto fail;
161
162 2d225591 Fabrice Bellard
    len = sizeof(my_addr1);
163
    getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
164
    s->local_port = ntohs(my_addr1.sin_port);
165 de6d9b64 Fabrice Bellard
166 9ddd71fc François Revol
#ifndef CONFIG_BEOS_NETSERVER
167 2d225591 Fabrice Bellard
    if (s->is_multicast) {
168
        if (h->flags & URL_WRONLY) {
169
            /* output */
170
            if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL, 
171
                           &s->ttl, sizeof(s->ttl)) < 0) {
172
                perror("IP_MULTICAST_TTL");
173
                goto fail;
174
            }
175
        } else {
176
            /* input */
177
            memset(&s->mreq, 0, sizeof(s->mreq));
178
            s->mreq.imr_multiaddr = s->dest_addr.sin_addr;
179
            s->mreq.imr_interface.s_addr = htonl (INADDR_ANY);
180
            if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, 
181
                           &s->mreq, sizeof(s->mreq)) < 0) {
182
                perror("rtp: IP_ADD_MEMBERSHIP");
183
                goto fail;
184
            }
185
        }
186 de6d9b64 Fabrice Bellard
    }
187 9ddd71fc François Revol
#endif
188 de6d9b64 Fabrice Bellard
189 2d225591 Fabrice Bellard
    if (is_output) {
190
        /* limit the tx buf size to limit latency */
191
        tmp = UDP_TX_BUF_SIZE;
192
        if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
193
            perror("setsockopt sndbuf");
194
            goto fail;
195
        }
196
    }
197
198
    s->udp_fd = udp_fd;
199 de6d9b64 Fabrice Bellard
    return 0;
200
 fail:
201 2d225591 Fabrice Bellard
    if (udp_fd >= 0)
202 9ddd71fc François Revol
#ifdef CONFIG_BEOS_NETSERVER
203
        closesocket(udp_fd);
204
#else
205 2d225591 Fabrice Bellard
        close(udp_fd);
206 9ddd71fc François Revol
#endif
207 2d225591 Fabrice Bellard
    av_free(s);
208 de6d9b64 Fabrice Bellard
    return -EIO;
209
}
210
211 0c1a9eda Zdenek Kabelac
static int udp_read(URLContext *h, uint8_t *buf, int size)
212 de6d9b64 Fabrice Bellard
{
213
    UDPContext *s = h->priv_data;
214 2d225591 Fabrice Bellard
    struct sockaddr_in from;
215
    int from_len, len;
216
217
    for(;;) {
218
        from_len = sizeof(from);
219
        len = recvfrom (s->udp_fd, buf, size, 0,
220
                        (struct sockaddr *)&from, &from_len);
221
        if (len < 0) {
222
            if (errno != EAGAIN && errno != EINTR)
223
                return -EIO;
224
        } else {
225
            break;
226
        }
227
    }
228
    return len;
229 de6d9b64 Fabrice Bellard
}
230
231 0c1a9eda Zdenek Kabelac
static int udp_write(URLContext *h, uint8_t *buf, int size)
232 de6d9b64 Fabrice Bellard
{
233
    UDPContext *s = h->priv_data;
234 2d225591 Fabrice Bellard
    int ret;
235
236
    for(;;) {
237
        ret = sendto (s->udp_fd, buf, size, 0, 
238
                      (struct sockaddr *) &s->dest_addr,
239
                      sizeof (s->dest_addr));
240
        if (ret < 0) {
241
            if (errno != EINTR && errno != EAGAIN)
242
                return -EIO;
243
        } else {
244
            break;
245
        }
246 de6d9b64 Fabrice Bellard
    }
247 2d225591 Fabrice Bellard
    return size;
248
}
249
250
static int udp_close(URLContext *h)
251
{
252
    UDPContext *s = h->priv_data;
253
254 9ddd71fc François Revol
#ifndef CONFIG_BEOS_NETSERVER
255 2d225591 Fabrice Bellard
    if (s->is_multicast && !(h->flags & URL_WRONLY)) {
256
        if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, 
257
                       &s->mreq, sizeof(s->mreq)) < 0) {
258
            perror("IP_DROP_MEMBERSHIP");
259
        }
260
    }
261
    close(s->udp_fd);
262 9ddd71fc François Revol
#else
263
    closesocket(s->udp_fd);
264
#endif
265 2d225591 Fabrice Bellard
    av_free(s);
266
    return 0;
267 de6d9b64 Fabrice Bellard
}
268
269
URLProtocol udp_protocol = {
270
    "udp",
271
    udp_open,
272 2d225591 Fabrice Bellard
    udp_read,
273 de6d9b64 Fabrice Bellard
    udp_write,
274
    NULL, /* seek */
275
    udp_close,
276
};