Statistics
| Branch: | Revision:

ffmpeg / libavformat / tcp.c @ fdcdd539

History | View | Annotate | Download (5.58 KB)

1
/*
2
 * TCP protocol
3
 * Copyright (c) 2002 Fabrice Bellard
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
#include "avformat.h"
22
#include <unistd.h>
23
#include "network.h"
24
#include "os_support.h"
25
#if HAVE_SYS_SELECT_H
26
#include <sys/select.h>
27
#endif
28
#include <sys/time.h>
29

    
30
typedef struct TCPContext {
31
    int fd;
32
} TCPContext;
33

    
34
/* return non zero if error */
35
static int tcp_open(URLContext *h, const char *uri, int flags)
36
{
37
    struct addrinfo hints, *ai, *cur_ai;
38
    int port, fd = -1;
39
    TCPContext *s = NULL;
40
    fd_set wfds;
41
    int fd_max, ret;
42
    struct timeval tv;
43
    socklen_t optlen;
44
    char hostname[1024],proto[1024],path[1024];
45
    char portstr[10];
46

    
47
    if(!ff_network_init())
48
        return AVERROR(EIO);
49

    
50
    url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
51
        &port, path, sizeof(path), uri);
52
    if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
53
        return AVERROR(EINVAL);
54

    
55
    memset(&hints, 0, sizeof(hints));
56
    hints.ai_family = PF_UNSPEC;
57
    hints.ai_socktype = SOCK_STREAM;
58
    snprintf(portstr, sizeof(portstr), "%d", port);
59
    if (getaddrinfo(hostname, portstr, &hints, &ai))
60
        return AVERROR(EIO);
61

    
62
    cur_ai = ai;
63

    
64
 restart:
65
    fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
66
    if (fd < 0)
67
        goto fail;
68
    ff_socket_nonblock(fd, 1);
69

    
70
 redo:
71
    ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
72
    if (ret < 0) {
73
        if (ff_neterrno() == FF_NETERROR(EINTR))
74
            goto redo;
75
        if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
76
            ff_neterrno() != FF_NETERROR(EAGAIN))
77
            goto fail;
78

    
79
        /* wait until we are connected or until abort */
80
        for(;;) {
81
            if (url_interrupt_cb()) {
82
                ret = AVERROR(EINTR);
83
                goto fail1;
84
            }
85
            fd_max = fd;
86
            FD_ZERO(&wfds);
87
            FD_SET(fd, &wfds);
88
            tv.tv_sec = 0;
89
            tv.tv_usec = 100 * 1000;
90
            ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
91
            if (ret > 0 && FD_ISSET(fd, &wfds))
92
                break;
93
        }
94

    
95
        /* test error */
96
        optlen = sizeof(ret);
97
        getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
98
        if (ret != 0)
99
            goto fail;
100
    }
101
    s = av_malloc(sizeof(TCPContext));
102
    if (!s) {
103
        freeaddrinfo(ai);
104
        return AVERROR(ENOMEM);
105
    }
106
    h->priv_data = s;
107
    h->is_streamed = 1;
108
    s->fd = fd;
109
    freeaddrinfo(ai);
110
    return 0;
111

    
112
 fail:
113
    if (cur_ai->ai_next) {
114
        /* Retry with the next sockaddr */
115
        cur_ai = cur_ai->ai_next;
116
        if (fd >= 0)
117
            closesocket(fd);
118
        goto restart;
119
    }
120
    ret = AVERROR(EIO);
121
 fail1:
122
    if (fd >= 0)
123
        closesocket(fd);
124
    freeaddrinfo(ai);
125
    return ret;
126
}
127

    
128
static int tcp_read(URLContext *h, uint8_t *buf, int size)
129
{
130
    TCPContext *s = h->priv_data;
131
    int len, fd_max, ret;
132
    fd_set rfds;
133
    struct timeval tv;
134

    
135
    for (;;) {
136
        if (url_interrupt_cb())
137
            return AVERROR(EINTR);
138
        fd_max = s->fd;
139
        FD_ZERO(&rfds);
140
        FD_SET(s->fd, &rfds);
141
        tv.tv_sec = 0;
142
        tv.tv_usec = 100 * 1000;
143
        ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
144
        if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
145
            len = recv(s->fd, buf, size, 0);
146
            if (len < 0) {
147
                if (ff_neterrno() != FF_NETERROR(EINTR) &&
148
                    ff_neterrno() != FF_NETERROR(EAGAIN))
149
                    return AVERROR(ff_neterrno());
150
            } else return len;
151
        } else if (ret < 0) {
152
            return -1;
153
        }
154
    }
155
}
156

    
157
static int tcp_write(URLContext *h, uint8_t *buf, int size)
158
{
159
    TCPContext *s = h->priv_data;
160
    int ret, size1, fd_max, len;
161
    fd_set wfds;
162
    struct timeval tv;
163

    
164
    size1 = size;
165
    while (size > 0) {
166
        if (url_interrupt_cb())
167
            return AVERROR(EINTR);
168
        fd_max = s->fd;
169
        FD_ZERO(&wfds);
170
        FD_SET(s->fd, &wfds);
171
        tv.tv_sec = 0;
172
        tv.tv_usec = 100 * 1000;
173
        ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
174
        if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
175
            len = send(s->fd, buf, size, 0);
176
            if (len < 0) {
177
                if (ff_neterrno() != FF_NETERROR(EINTR) &&
178
                    ff_neterrno() != FF_NETERROR(EAGAIN))
179
                    return AVERROR(ff_neterrno());
180
                continue;
181
            }
182
            size -= len;
183
            buf += len;
184
        } else if (ret < 0) {
185
            return -1;
186
        }
187
    }
188
    return size1 - size;
189
}
190

    
191
static int tcp_close(URLContext *h)
192
{
193
    TCPContext *s = h->priv_data;
194
    closesocket(s->fd);
195
    ff_network_close();
196
    av_free(s);
197
    return 0;
198
}
199

    
200
static int tcp_get_file_handle(URLContext *h)
201
{
202
    TCPContext *s = h->priv_data;
203
    return s->fd;
204
}
205

    
206
URLProtocol tcp_protocol = {
207
    "tcp",
208
    tcp_open,
209
    tcp_read,
210
    tcp_write,
211
    NULL, /* seek */
212
    tcp_close,
213
    .url_get_file_handle = tcp_get_file_handle,
214
};