diff options
author | Fabrice Bellard <fabrice@bellard.org> | 2000-12-20 00:02:47 +0000 |
---|---|---|
committer | Fabrice Bellard <fabrice@bellard.org> | 2000-12-20 00:02:47 +0000 |
commit | 9aeeeb63f7e1ab7b0b7bb839a5f258667a2d2d78 (patch) | |
tree | 133769894d45da35e05ded6ea39d33bb81e7ae18 /udp.c | |
parent | 77bb6835ba752bb9335d208963a53227bbb1bc63 (diff) | |
download | ffmpeg-9aeeeb63f7e1ab7b0b7bb839a5f258667a2d2d78.tar.gz |
Initial revision
Originally committed as revision 2 to svn://svn.ffmpeg.org/ffmpeg/trunk
Diffstat (limited to 'udp.c')
-rw-r--r-- | udp.c | 123 |
1 files changed, 123 insertions, 0 deletions
@@ -0,0 +1,123 @@ +/* + * UDP prototype streaming system + * Copyright (c) 2000 Gerard Lantau. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <netdb.h> + +#include "mpegenc.h" + +#define UDP_TX_BUF_SIZE 32768 +/* put it in UDP context */ +static struct sockaddr_in dest_addr; + +/* return non zero if error */ +int udp_tx_open(UDPContext *s, + const char *uri, + int local_port) +{ + struct sockaddr_in my_addr; + const char *p, *q; + char hostname[1024]; + int port, udp_socket, tmp; + struct hostent *hp; + + /* fill the dest addr */ + p = uri; + if (!strstart(p, "udp:", &p)) + return -1; + q = strchr(p, ':'); + if (!q) + return -1; + memcpy(hostname, p, q - p); + hostname[q - p] = '\0'; + port = strtol(q+1, NULL, 10); + if (port <= 0) + return -1; + + dest_addr.sin_family = AF_INET; + dest_addr.sin_port = htons(port); + if ((inet_aton(hostname, &dest_addr.sin_addr)) == 0) { + hp = gethostbyname(hostname); + if (!hp) + return -1; + memcpy ((char *) &dest_addr.sin_addr, hp->h_addr, hp->h_length); + } + + udp_socket = socket(PF_INET, SOCK_DGRAM, 0); + if (udp_socket < 0) + return -1; + + my_addr.sin_family = AF_INET; + my_addr.sin_port = htons(local_port); + my_addr.sin_addr.s_addr = htonl (INADDR_ANY); + + /* the bind is needed to give a port to the socket now */ + if (bind(udp_socket,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) + goto fail; + + /* limit the tx buf size to limit latency */ + + tmp = UDP_TX_BUF_SIZE; + if (setsockopt(udp_socket, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) { + perror("setsockopt sndbuf"); + goto fail; + } + + s->udp_socket = udp_socket; + s->max_payload_size = 1024; + return 0; + fail: + return -1; +} + +void udp_tx_close(UDPContext *s) +{ + close(s->udp_socket); +} + +extern int data_out_size; + +void udp_write_data(void *opaque, UINT8 *buf, int size) +{ + UDPContext *s = opaque; + int ret, len; + + /* primitive way to avoid big packets */ + data_out_size += size; + while (size > 0) { + len = size; + if (len > s->max_payload_size) + len = s->max_payload_size; + + ret = sendto (s->udp_socket, buf, len, 0, + (struct sockaddr *) &dest_addr, + sizeof (dest_addr)); + if (ret < 0) + perror("sendto"); + buf += len; + size -= len; + } +} + |