~noskcaj/ubuntu/saucy/sflphone/merge-1.2.3-2

« back to all changes in this revision

Viewing changes to daemon/src/video/socket_pair.cpp

  • Committer: Jackson Doak
  • Date: 2013-07-10 21:04:46 UTC
  • mfrom: (20.1.3 sid)
  • Revision ID: noskcaj@ubuntu.com-20130710210446-y8f587vza807icr9
Properly merged from upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
 
3
 *  Copyright (c) 2002 Fabrice Bellard
 
4
 *
 
5
 *  Author: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
 
6
 *
 
7
 *  This program is free software; you can redistribute it and/or modify
 
8
 *  it under the terms of the GNU General Public License as published by
 
9
 *  the Free Software Foundation; either version 3 of the License, or
 
10
 *  (at your option) any later version.
 
11
 *
 
12
 *  This program 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
 
15
 *  GNU General Public License for more details.
 
16
 *
 
17
 *  You should have received a copy of the GNU General Public License
 
18
 *  along with this program; if not, write to the Free Software
 
19
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 
20
 *
 
21
 *  Additional permission under GNU GPL version 3 section 7:
 
22
 *
 
23
 *  If you modify this program, or any covered work, by linking or
 
24
 *  combining it with the OpenSSL project's OpenSSL library (or a
 
25
 *  modified version of that library), containing parts covered by the
 
26
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 
27
 *  grants you additional permission to convey the resulting work.
 
28
 *  Corresponding Source for a non-source form of such a combination
 
29
 *  shall include the source code for the parts of OpenSSL used as well
 
30
 *  as that of the covered work.
 
31
 */
 
32
 
 
33
#include "socket_pair.h"
 
34
#include "scoped_lock.h"
 
35
#include "logger.h"
 
36
#include <cstring>
 
37
#include <stdexcept>
 
38
#include <unistd.h>
 
39
#include <poll.h>
 
40
#include <sys/types.h>
 
41
#include <sys/socket.h>
 
42
#include <netdb.h>
 
43
 
 
44
 
 
45
extern "C" {
 
46
#include <libavutil/avstring.h>
 
47
#include <libavformat/avformat.h>
 
48
}
 
49
 
 
50
namespace {
 
51
 
 
52
int ff_network_wait_fd(int fd)
 
53
{
 
54
    struct pollfd p = { fd, POLLOUT, 0 };
 
55
    int ret;
 
56
    ret = poll(&p, 1, 100);
 
57
    return ret < 0 ? errno : p.revents & (POLLOUT | POLLERR | POLLHUP) ? 0 : AVERROR(EAGAIN);
 
58
}
 
59
 
 
60
struct addrinfo*
 
61
udp_resolve_host(const char *node, int service)
 
62
{
 
63
    struct addrinfo hints = { 0 }, *res = 0;
 
64
    int error;
 
65
    char sport[16];
 
66
 
 
67
    snprintf(sport, sizeof(sport), "%d", service);
 
68
 
 
69
    hints.ai_socktype = SOCK_DGRAM;
 
70
    hints.ai_family   = AF_UNSPEC;
 
71
    hints.ai_flags = AI_PASSIVE;
 
72
    if ((error = getaddrinfo(node, sport, &hints, &res))) {
 
73
        res = NULL;
 
74
        ERROR("%s\n", gai_strerror(error));
 
75
    }
 
76
 
 
77
    return res;
 
78
}
 
79
 
 
80
unsigned
 
81
udp_set_url(struct sockaddr_storage *addr,
 
82
            const char *hostname, int port)
 
83
{
 
84
    struct addrinfo *res0;
 
85
    int addr_len;
 
86
 
 
87
    res0 = udp_resolve_host(hostname, port);
 
88
    if (res0 == 0) return 0;
 
89
    memcpy(addr, res0->ai_addr, res0->ai_addrlen);
 
90
    addr_len = res0->ai_addrlen;
 
91
    freeaddrinfo(res0);
 
92
 
 
93
    return addr_len;
 
94
}
 
95
 
 
96
int
 
97
udp_socket_create(sockaddr_storage *addr, socklen_t *addr_len,
 
98
                  int local_port)
 
99
{
 
100
    int udp_fd = -1;
 
101
    struct addrinfo *res0 = NULL, *res = NULL;
 
102
 
 
103
    res0 = udp_resolve_host(0, local_port);
 
104
    if (res0 == 0)
 
105
        return -1;
 
106
    for (res = res0; res; res=res->ai_next) {
 
107
        udp_fd = socket(res->ai_family, SOCK_DGRAM | SOCK_NONBLOCK, 0);
 
108
        if (udp_fd != -1) break;
 
109
        ERROR("socket error");
 
110
    }
 
111
 
 
112
    if (udp_fd < 0) {
 
113
        freeaddrinfo(res0);
 
114
        return -1;
 
115
    }
 
116
 
 
117
    memcpy(addr, res->ai_addr, res->ai_addrlen);
 
118
    *addr_len = res->ai_addrlen;
 
119
 
 
120
#if HAVE_SDP_CUSTOM_IO
 
121
    // bind socket so that we send from and receive
 
122
    // on local port
 
123
    if (bind(udp_fd, reinterpret_cast<sockaddr*>(addr), *addr_len) < 0) {
 
124
        ERROR("Bind failed: %s", strerror(errno));
 
125
        close(udp_fd);
 
126
        udp_fd = -1;
 
127
    }
 
128
#endif
 
129
 
 
130
    freeaddrinfo(res0);
 
131
 
 
132
    return udp_fd;
 
133
}
 
134
 
 
135
const int RTP_BUFFER_SIZE = 1472;
 
136
 
 
137
}
 
138
 
 
139
namespace sfl_video {
 
140
 
 
141
SocketPair::SocketPair(const char *uri, int localPort) :
 
142
           rtcpWriteMutex_(),
 
143
           rtpHandle_(0),
 
144
           rtcpHandle_(0),
 
145
           rtpDestAddr_(),
 
146
           rtpDestAddrLen_(),
 
147
           rtcpDestAddr_(),
 
148
           rtcpDestAddrLen_(),
 
149
           interrupted_(false)
 
150
{
 
151
    pthread_mutex_init(&rtcpWriteMutex_, NULL);
 
152
    openSockets(uri, localPort);
 
153
}
 
154
 
 
155
SocketPair::~SocketPair()
 
156
{
 
157
    interrupted_ = true;
 
158
    closeSockets();
 
159
 
 
160
    // destroy in reverse order
 
161
    pthread_mutex_destroy(&rtcpWriteMutex_);
 
162
}
 
163
 
 
164
void
 
165
SocketPair::interrupt()
 
166
{
 
167
    interrupted_ = true;
 
168
}
 
169
 
 
170
void
 
171
SocketPair::closeSockets()
 
172
{
 
173
    if (rtcpHandle_ > 0 and close(rtcpHandle_))
 
174
        ERROR("%s", strerror(errno));
 
175
    if (rtpHandle_ > 0 and close(rtpHandle_))
 
176
        ERROR("%s", strerror(errno));
 
177
}
 
178
 
 
179
 
 
180
void
 
181
SocketPair::openSockets(const char *uri, int local_rtp_port)
 
182
{
 
183
    char hostname[256];
 
184
    char buf[1024];
 
185
    char path[1024];
 
186
 
 
187
    int rtp_port;
 
188
    av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
 
189
                 path, sizeof(path), uri);
 
190
 
 
191
    const int rtcp_port = rtp_port + 1;
 
192
 
 
193
#if HAVE_SDP_CUSTOM_IO
 
194
    const int local_rtcp_port = local_rtp_port + 1;
 
195
#else
 
196
    WARN("libavformat too old for socket reuse, using random source ports");
 
197
    local_rtp_port = 0;
 
198
    const int local_rtcp_port = 0;
 
199
#endif
 
200
 
 
201
    sockaddr_storage rtp_addr, rtcp_addr;
 
202
    socklen_t rtp_len, rtcp_len;
 
203
 
 
204
    // Open sockets and store addresses for sending
 
205
    if ((rtpHandle_ = udp_socket_create(&rtp_addr, &rtp_len, local_rtp_port)) == -1 or
 
206
        (rtcpHandle_ = udp_socket_create(&rtcp_addr, &rtcp_len, local_rtcp_port)) == -1 or
 
207
        (rtpDestAddrLen_ = udp_set_url(&rtpDestAddr_, hostname, rtp_port)) == 0 or
 
208
        (rtcpDestAddrLen_ = udp_set_url(&rtcpDestAddr_, hostname, rtcp_port)) == 0) {
 
209
 
 
210
        // Handle failed socket creation
 
211
        closeSockets();
 
212
        throw std::runtime_error("Socket creation failed");
 
213
    }
 
214
}
 
215
 
 
216
AVIOContext *
 
217
SocketPair::createAVIOContext()
 
218
{
 
219
    // FIXME: Caller must free buffer!
 
220
    unsigned char *buffer(static_cast<unsigned char *>(av_malloc(RTP_BUFFER_SIZE)));
 
221
 
 
222
    AVIOContext *context = avio_alloc_context(buffer,
 
223
            RTP_BUFFER_SIZE, 1, reinterpret_cast<void*>(this),
 
224
            &readCallback, &writeCallback, 0);
 
225
 
 
226
    context->max_packet_size = RTP_BUFFER_SIZE;
 
227
    return context;
 
228
}
 
229
 
 
230
int
 
231
SocketPair::readCallback(void *opaque, uint8_t *buf, int buf_size)
 
232
{
 
233
    SocketPair *context = static_cast<SocketPair*>(opaque);
 
234
 
 
235
    struct sockaddr_storage from;
 
236
    socklen_t from_len;
 
237
    int len, n;
 
238
    struct pollfd p[2] = { {context->rtpHandle_, POLLIN, 0}, {context->rtcpHandle_, POLLIN, 0}};
 
239
 
 
240
    for(;;) {
 
241
        if (context->interrupted_)
 
242
            return AVERROR_EXIT;
 
243
 
 
244
        /* build fdset to listen to RTP and RTCP packets */
 
245
        n = poll(p, 2, 100);
 
246
        if (n > 0) {
 
247
            /* first try RTCP */
 
248
            if (p[1].revents & POLLIN) {
 
249
                from_len = sizeof(from);
 
250
 
 
251
                {
 
252
                    len = recvfrom(context->rtcpHandle_, buf, buf_size, 0,
 
253
                            (struct sockaddr *)&from, &from_len);
 
254
                }
 
255
 
 
256
                if (len < 0) {
 
257
                    if (errno == EAGAIN or errno == EINTR)
 
258
                        continue;
 
259
                    return AVERROR(EIO);
 
260
                }
 
261
                break;
 
262
            }
 
263
            /* then RTP */
 
264
            if (p[0].revents & POLLIN) {
 
265
                from_len = sizeof(from);
 
266
 
 
267
                {
 
268
                    len = recvfrom(context->rtpHandle_, buf, buf_size, 0,
 
269
                            (struct sockaddr *)&from, &from_len);
 
270
                }
 
271
 
 
272
                if (len < 0) {
 
273
                    if (errno == EAGAIN or errno == EINTR)
 
274
                        continue;
 
275
                    return AVERROR(EIO);
 
276
                }
 
277
                break;
 
278
            }
 
279
        } else if (n < 0) {
 
280
            if (errno == EINTR)
 
281
                continue;
 
282
            return AVERROR(EIO);
 
283
        }
 
284
    }
 
285
    return len;
 
286
}
 
287
 
 
288
/* RTCP packet types */
 
289
enum RTCPType {
 
290
    RTCP_FIR    = 192,
 
291
    RTCP_IJ     = 195,
 
292
    RTCP_SR     = 200,
 
293
    RTCP_TOKEN  = 210
 
294
};
 
295
 
 
296
#define RTP_PT_IS_RTCP(x) (((x) >= RTCP_FIR && (x) <= RTCP_IJ) || \
 
297
                           ((x) >= RTCP_SR  && (x) <= RTCP_TOKEN))
 
298
 
 
299
int
 
300
SocketPair::writeCallback(void *opaque, uint8_t *buf, int buf_size)
 
301
{
 
302
    SocketPair *context = static_cast<SocketPair*>(opaque);
 
303
 
 
304
    int ret;
 
305
 
 
306
    if (RTP_PT_IS_RTCP(buf[1])) {
 
307
        /* RTCP payload type */
 
308
        sfl::ScopedLock lock(context->rtcpWriteMutex_);
 
309
        ret = ff_network_wait_fd(context->rtcpHandle_);
 
310
        if (ret < 0)
 
311
            return ret;
 
312
 
 
313
        ret = sendto(context->rtcpHandle_, buf, buf_size, 0,
 
314
                     (sockaddr*) &context->rtcpDestAddr_, context->rtcpDestAddrLen_);
 
315
    } else {
 
316
        /* RTP payload type */
 
317
        ret = ff_network_wait_fd(context->rtpHandle_);
 
318
        if (ret < 0)
 
319
            return ret;
 
320
 
 
321
        ret = sendto(context->rtpHandle_, buf, buf_size, 0,
 
322
                     (sockaddr*) &context->rtpDestAddr_, context->rtpDestAddrLen_);
 
323
    }
 
324
 
 
325
    return ret < 0 ? errno : ret;
 
326
}
 
327
 
 
328
}