summaryrefslogtreecommitdiff
path: root/udp.c
blob: 44fd7f36f860c44c0d5dfc25b926ca5cd613a1a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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;
    }
}