]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpproto.c
Merge commit '5d01bd181bb77e6740462095d7be4e0733a59420'
[ffmpeg] / libavformat / rtpproto.c
1 /*
2  * RTP network 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
22 /**
23  * @file
24  * RTP protocol
25  */
26
27 #include "libavutil/parseutils.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/opt.h"
30 #include "avformat.h"
31 #include "avio_internal.h"
32 #include "rtp.h"
33 #include "rtpproto.h"
34 #include "url.h"
35
36 #include <stdarg.h>
37 #include "internal.h"
38 #include "network.h"
39 #include "os_support.h"
40 #include <fcntl.h>
41 #if HAVE_POLL_H
42 #include <sys/poll.h>
43 #endif
44
45 typedef struct RTPContext {
46     const AVClass *class;
47     URLContext *rtp_hd, *rtcp_hd, *fec_hd;
48     int rtp_fd, rtcp_fd, nb_ssm_include_addrs, nb_ssm_exclude_addrs;
49     struct sockaddr_storage **ssm_include_addrs, **ssm_exclude_addrs;
50     int write_to_source;
51     struct sockaddr_storage last_rtp_source, last_rtcp_source;
52     socklen_t last_rtp_source_len, last_rtcp_source_len;
53     int ttl;
54     int buffer_size;
55     int rtcp_port, local_rtpport, local_rtcpport;
56     int connect;
57     int pkt_size;
58     int dscp;
59     char *sources;
60     char *block;
61     char *fec_options_str;
62 } RTPContext;
63
64 #define OFFSET(x) offsetof(RTPContext, x)
65 #define D AV_OPT_FLAG_DECODING_PARAM
66 #define E AV_OPT_FLAG_ENCODING_PARAM
67 static const AVOption options[] = {
68     { "ttl",                "Time to live (in milliseconds, multicast only)",                   OFFSET(ttl),             AV_OPT_TYPE_INT,    { .i64 = -1 },    -1, INT_MAX, .flags = D|E },
69     { "buffer_size",        "Send/Receive buffer size (in bytes)",                              OFFSET(buffer_size),     AV_OPT_TYPE_INT,    { .i64 = -1 },    -1, INT_MAX, .flags = D|E },
70     { "rtcp_port",          "Custom rtcp port",                                                 OFFSET(rtcp_port),       AV_OPT_TYPE_INT,    { .i64 = -1 },    -1, INT_MAX, .flags = D|E },
71     { "local_rtpport",      "Local rtp port",                                                   OFFSET(local_rtpport),   AV_OPT_TYPE_INT,    { .i64 = -1 },    -1, INT_MAX, .flags = D|E },
72     { "local_rtcpport",     "Local rtcp port",                                                  OFFSET(local_rtcpport),  AV_OPT_TYPE_INT,    { .i64 = -1 },    -1, INT_MAX, .flags = D|E },
73     { "connect",            "Connect socket",                                                   OFFSET(connect),         AV_OPT_TYPE_BOOL,   { .i64 =  0 },     0, 1,       .flags = D|E },
74     { "write_to_source",    "Send packets to the source address of the latest received packet", OFFSET(write_to_source), AV_OPT_TYPE_BOOL,   { .i64 =  0 },     0, 1,       .flags = D|E },
75     { "pkt_size",           "Maximum packet size",                                              OFFSET(pkt_size),        AV_OPT_TYPE_INT,    { .i64 = -1 },    -1, INT_MAX, .flags = D|E },
76     { "dscp",               "DSCP class",                                                       OFFSET(dscp),            AV_OPT_TYPE_INT,    { .i64 = -1 },    -1, INT_MAX, .flags = D|E },
77     { "sources",            "Source list",                                                      OFFSET(sources),         AV_OPT_TYPE_STRING, { .str = NULL },               .flags = D|E },
78     { "block",              "Block list",                                                       OFFSET(block),           AV_OPT_TYPE_STRING, { .str = NULL },               .flags = D|E },
79     { "fec",                "FEC",                                                              OFFSET(fec_options_str), AV_OPT_TYPE_STRING, { .str = NULL },               .flags = E },
80     { NULL }
81 };
82
83 static const AVClass rtp_class = {
84     .class_name = "rtp",
85     .item_name  = av_default_item_name,
86     .option     = options,
87     .version    = LIBAVUTIL_VERSION_INT,
88 };
89
90 /**
91  * If no filename is given to av_open_input_file because you want to
92  * get the local port first, then you must call this function to set
93  * the remote server address.
94  *
95  * @param h media file context
96  * @param uri of the remote server
97  * @return zero if no error.
98  */
99
100 int ff_rtp_set_remote_url(URLContext *h, const char *uri)
101 {
102     RTPContext *s = h->priv_data;
103     char hostname[256];
104     int port, rtcp_port;
105     const char *p;
106
107     char buf[1024];
108     char path[1024];
109
110     av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
111                  path, sizeof(path), uri);
112     rtcp_port = port + 1;
113
114     p = strchr(uri, '?');
115     if (p) {
116         if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
117             rtcp_port = strtol(buf, NULL, 10);
118         }
119     }
120
121     ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
122     ff_udp_set_remote_url(s->rtp_hd, buf);
123
124     ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, rtcp_port, "%s", path);
125     ff_udp_set_remote_url(s->rtcp_hd, buf);
126     return 0;
127 }
128
129 static struct addrinfo* rtp_resolve_host(const char *hostname, int port,
130                                          int type, int family, int flags)
131 {
132     struct addrinfo hints = { 0 }, *res = 0;
133     int error;
134     char service[16];
135
136     snprintf(service, sizeof(service), "%d", port);
137     hints.ai_socktype = type;
138     hints.ai_family   = family;
139     hints.ai_flags    = flags;
140     if ((error = getaddrinfo(hostname, service, &hints, &res))) {
141         res = NULL;
142         av_log(NULL, AV_LOG_ERROR, "rtp_resolve_host: %s\n", gai_strerror(error));
143     }
144
145     return res;
146 }
147
148 static int compare_addr(const struct sockaddr_storage *a,
149                         const struct sockaddr_storage *b)
150 {
151     if (a->ss_family != b->ss_family)
152         return 1;
153     if (a->ss_family == AF_INET) {
154         return (((const struct sockaddr_in *)a)->sin_addr.s_addr !=
155                 ((const struct sockaddr_in *)b)->sin_addr.s_addr);
156     }
157
158 #if HAVE_STRUCT_SOCKADDR_IN6
159     if (a->ss_family == AF_INET6) {
160         const uint8_t *s6_addr_a = ((const struct sockaddr_in6 *)a)->sin6_addr.s6_addr;
161         const uint8_t *s6_addr_b = ((const struct sockaddr_in6 *)b)->sin6_addr.s6_addr;
162         return memcmp(s6_addr_a, s6_addr_b, 16);
163     }
164 #endif
165     return 1;
166 }
167
168 static int get_port(const struct sockaddr_storage *ss)
169 {
170     if (ss->ss_family == AF_INET)
171         return ntohs(((const struct sockaddr_in *)ss)->sin_port);
172 #if HAVE_STRUCT_SOCKADDR_IN6
173     if (ss->ss_family == AF_INET6)
174         return ntohs(((const struct sockaddr_in6 *)ss)->sin6_port);
175 #endif
176     return 0;
177 }
178
179 static void set_port(struct sockaddr_storage *ss, int port)
180 {
181     if (ss->ss_family == AF_INET)
182         ((struct sockaddr_in *)ss)->sin_port = htons(port);
183 #if HAVE_STRUCT_SOCKADDR_IN6
184     else if (ss->ss_family == AF_INET6)
185         ((struct sockaddr_in6 *)ss)->sin6_port = htons(port);
186 #endif
187 }
188
189 static int rtp_check_source_lists(RTPContext *s, struct sockaddr_storage *source_addr_ptr)
190 {
191     int i;
192     if (s->nb_ssm_exclude_addrs) {
193         for (i = 0; i < s->nb_ssm_exclude_addrs; i++) {
194             if (!compare_addr(source_addr_ptr, s->ssm_exclude_addrs[i]))
195                 return 1;
196         }
197     }
198     if (s->nb_ssm_include_addrs) {
199         for (i = 0; i < s->nb_ssm_include_addrs; i++) {
200             if (!compare_addr(source_addr_ptr, s->ssm_include_addrs[i]))
201                 return 0;
202         }
203         return 1;
204     }
205     return 0;
206 }
207
208 /**
209  * add option to url of the form:
210  * "http://host:port/path?option1=val1&option2=val2...
211  */
212
213 static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
214 {
215     char buf1[1024];
216     va_list ap;
217
218     va_start(ap, fmt);
219     if (strchr(buf, '?'))
220         av_strlcat(buf, "&", buf_size);
221     else
222         av_strlcat(buf, "?", buf_size);
223     vsnprintf(buf1, sizeof(buf1), fmt, ap);
224     av_strlcat(buf, buf1, buf_size);
225     va_end(ap);
226 }
227
228 static void build_udp_url(RTPContext *s,
229                           char *buf, int buf_size,
230                           const char *hostname,
231                           int port, int local_port,
232                           const char *include_sources,
233                           const char *exclude_sources)
234 {
235     ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
236     if (local_port >= 0)
237         url_add_option(buf, buf_size, "localport=%d", local_port);
238     if (s->ttl >= 0)
239         url_add_option(buf, buf_size, "ttl=%d", s->ttl);
240     if (s->buffer_size >= 0)
241         url_add_option(buf, buf_size, "buffer_size=%d", s->buffer_size);
242     if (s->pkt_size >= 0)
243         url_add_option(buf, buf_size, "pkt_size=%d", s->pkt_size);
244     if (s->connect)
245         url_add_option(buf, buf_size, "connect=1");
246     if (s->dscp >= 0)
247         url_add_option(buf, buf_size, "dscp=%d", s->dscp);
248     url_add_option(buf, buf_size, "fifo_size=0");
249     if (include_sources && include_sources[0])
250         url_add_option(buf, buf_size, "sources=%s", include_sources);
251     if (exclude_sources && exclude_sources[0])
252         url_add_option(buf, buf_size, "block=%s", exclude_sources);
253 }
254
255 static void rtp_parse_addr_list(URLContext *h, char *buf,
256                                 struct sockaddr_storage ***address_list_ptr,
257                                 int *address_list_size_ptr)
258 {
259     struct addrinfo *ai = NULL;
260     struct sockaddr_storage *source_addr;
261     char tmp = '\0', *p = buf, *next;
262
263     /* Resolve all of the IPs */
264
265     while (p && p[0]) {
266         next = strchr(p, ',');
267
268         if (next) {
269             tmp = *next;
270             *next = '\0';
271         }
272
273         ai = rtp_resolve_host(p, 0, SOCK_DGRAM, AF_UNSPEC, 0);
274         if (ai) {
275             source_addr = av_mallocz(sizeof(struct sockaddr_storage));
276             if (!source_addr) {
277                 freeaddrinfo(ai);
278                 break;
279             }
280
281             memcpy(source_addr, ai->ai_addr, ai->ai_addrlen);
282             freeaddrinfo(ai);
283             dynarray_add(address_list_ptr, address_list_size_ptr, source_addr);
284         } else {
285             av_log(h, AV_LOG_WARNING, "Unable to resolve %s\n", p);
286         }
287
288         if (next) {
289             *next = tmp;
290             p = next + 1;
291         } else {
292             p = NULL;
293         }
294     }
295 }
296
297 /**
298  * url syntax: rtp://host:port[?option=val...]
299  * option: 'ttl=n'            : set the ttl value (for multicast only)
300  *         'rtcpport=n'       : set the remote rtcp port to n
301  *         'localrtpport=n'   : set the local rtp port to n
302  *         'localrtcpport=n'  : set the local rtcp port to n
303  *         'pkt_size=n'       : set max packet size
304  *         'connect=0/1'      : do a connect() on the UDP socket
305  *         'sources=ip[,ip]'  : list allowed source IP addresses
306  *         'block=ip[,ip]'    : list disallowed source IP addresses
307  *         'write_to_source=0/1' : send packets to the source address of the latest received packet
308  *         'dscp=n'           : set DSCP value to n (QoS)
309  * deprecated option:
310  *         'localport=n'      : set the local port to n
311  *
312  * if rtcpport isn't set the rtcp port will be the rtp port + 1
313  * if local rtp port isn't set any available port will be used for the local
314  * rtp and rtcp ports
315  * if the local rtcp port is not set it will be the local rtp port + 1
316  */
317
318 static int rtp_open(URLContext *h, const char *uri, int flags)
319 {
320     RTPContext *s = h->priv_data;
321     AVDictionary *fec_opts = NULL;
322     int rtp_port;
323     char hostname[256], include_sources[1024] = "", exclude_sources[1024] = "";
324     char *sources = include_sources, *block = exclude_sources;
325     char *fec_protocol = NULL;
326     char buf[1024];
327     char path[1024];
328     const char *p;
329     int i, max_retry_count = 3;
330     int rtcpflags;
331
332     av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
333                  path, sizeof(path), uri);
334     /* extract parameters */
335     if (s->rtcp_port < 0)
336         s->rtcp_port = rtp_port + 1;
337
338     p = strchr(uri, '?');
339     if (p) {
340         if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
341             s->ttl = strtol(buf, NULL, 10);
342         }
343         if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
344             s->rtcp_port = strtol(buf, NULL, 10);
345         }
346         if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
347             s->local_rtpport = strtol(buf, NULL, 10);
348         }
349         if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
350             s->local_rtpport = strtol(buf, NULL, 10);
351         }
352         if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
353             s->local_rtcpport = strtol(buf, NULL, 10);
354         }
355         if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
356             s->pkt_size = strtol(buf, NULL, 10);
357         }
358         if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
359             s->connect = strtol(buf, NULL, 10);
360         }
361         if (av_find_info_tag(buf, sizeof(buf), "write_to_source", p)) {
362             s->write_to_source = strtol(buf, NULL, 10);
363         }
364         if (av_find_info_tag(buf, sizeof(buf), "dscp", p)) {
365             s->dscp = strtol(buf, NULL, 10);
366         }
367         if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
368             av_strlcpy(include_sources, buf, sizeof(include_sources));
369
370             rtp_parse_addr_list(h, buf, &s->ssm_include_addrs, &s->nb_ssm_include_addrs);
371         } else {
372             rtp_parse_addr_list(h, s->sources, &s->ssm_include_addrs, &s->nb_ssm_include_addrs);
373             sources = s->sources;
374         }
375         if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
376             av_strlcpy(exclude_sources, buf, sizeof(exclude_sources));
377             rtp_parse_addr_list(h, buf, &s->ssm_exclude_addrs, &s->nb_ssm_exclude_addrs);
378         } else {
379             rtp_parse_addr_list(h, s->block, &s->ssm_exclude_addrs, &s->nb_ssm_exclude_addrs);
380             block = s->block;
381         }
382     }
383
384     if (s->fec_options_str) {
385         p = s->fec_options_str;
386
387         if (!(fec_protocol = av_get_token(&p, "="))) {
388             av_log(h, AV_LOG_ERROR, "Failed to parse the FEC protocol value\n");
389             goto fail;
390         }
391         if (strcmp(fec_protocol, "prompeg")) {
392             av_log(h, AV_LOG_ERROR, "Unsupported FEC protocol %s\n", fec_protocol);
393             goto fail;
394         }
395
396         p = s->fec_options_str + strlen(fec_protocol);
397         while (*p && *p == '=') p++;
398
399         if (av_dict_parse_string(&fec_opts, p, "=", ":", 0) < 0) {
400             av_log(h, AV_LOG_ERROR, "Failed to parse the FEC options\n");
401             goto fail;
402         }
403         if (s->ttl > 0) {
404             snprintf(buf, sizeof (buf), "%d", s->ttl);
405             av_dict_set(&fec_opts, "ttl", buf, 0);
406         }
407     }
408
409     for (i = 0; i < max_retry_count; i++) {
410         build_udp_url(s, buf, sizeof(buf),
411                       hostname, rtp_port, s->local_rtpport,
412                       sources, block);
413         if (ffurl_open_whitelist(&s->rtp_hd, buf, flags, &h->interrupt_callback,
414                                  NULL, h->protocol_whitelist, h->protocol_blacklist, h) < 0)
415             goto fail;
416         s->local_rtpport = ff_udp_get_local_port(s->rtp_hd);
417         if(s->local_rtpport == 65535) {
418             s->local_rtpport = -1;
419             continue;
420         }
421         rtcpflags = flags | AVIO_FLAG_WRITE;
422         if (s->local_rtcpport < 0) {
423             s->local_rtcpport = s->local_rtpport + 1;
424             build_udp_url(s, buf, sizeof(buf),
425                           hostname, s->rtcp_port, s->local_rtcpport,
426                           sources, block);
427             if (ffurl_open_whitelist(&s->rtcp_hd, buf, rtcpflags,
428                                      &h->interrupt_callback, NULL,
429                                      h->protocol_whitelist, h->protocol_blacklist, h) < 0) {
430                 s->local_rtpport = s->local_rtcpport = -1;
431                 continue;
432             }
433             break;
434         }
435         build_udp_url(s, buf, sizeof(buf),
436                       hostname, s->rtcp_port, s->local_rtcpport,
437                       sources, block);
438         if (ffurl_open_whitelist(&s->rtcp_hd, buf, rtcpflags, &h->interrupt_callback,
439                                  NULL, h->protocol_whitelist, h->protocol_blacklist, h) < 0)
440             goto fail;
441         break;
442     }
443
444     s->fec_hd = NULL;
445     if (fec_protocol) {
446         ff_url_join(buf, sizeof(buf), fec_protocol, NULL, hostname, rtp_port, NULL);
447         if (ffurl_open_whitelist(&s->fec_hd, buf, flags, &h->interrupt_callback,
448                              &fec_opts, h->protocol_whitelist, h->protocol_blacklist, h) < 0)
449             goto fail;
450     }
451
452     /* just to ease handle access. XXX: need to suppress direct handle
453        access */
454     s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
455     s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
456
457     h->max_packet_size = s->rtp_hd->max_packet_size;
458     h->is_streamed = 1;
459
460     av_free(fec_protocol);
461     av_dict_free(&fec_opts);
462
463     return 0;
464
465  fail:
466     if (s->rtp_hd)
467         ffurl_close(s->rtp_hd);
468     if (s->rtcp_hd)
469         ffurl_close(s->rtcp_hd);
470     ffurl_closep(&s->fec_hd);
471     av_free(fec_protocol);
472     av_dict_free(&fec_opts);
473     return AVERROR(EIO);
474 }
475
476 static int rtp_read(URLContext *h, uint8_t *buf, int size)
477 {
478     RTPContext *s = h->priv_data;
479     int len, n, i;
480     struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
481     int poll_delay = h->flags & AVIO_FLAG_NONBLOCK ? 0 : 100;
482     struct sockaddr_storage *addrs[2] = { &s->last_rtp_source, &s->last_rtcp_source };
483     socklen_t *addr_lens[2] = { &s->last_rtp_source_len, &s->last_rtcp_source_len };
484
485     for(;;) {
486         if (ff_check_interrupt(&h->interrupt_callback))
487             return AVERROR_EXIT;
488         n = poll(p, 2, poll_delay);
489         if (n > 0) {
490             /* first try RTCP, then RTP */
491             for (i = 1; i >= 0; i--) {
492                 if (!(p[i].revents & POLLIN))
493                     continue;
494                 *addr_lens[i] = sizeof(*addrs[i]);
495                 len = recvfrom(p[i].fd, buf, size, 0,
496                                 (struct sockaddr *)addrs[i], addr_lens[i]);
497                 if (len < 0) {
498                     if (ff_neterrno() == AVERROR(EAGAIN) ||
499                         ff_neterrno() == AVERROR(EINTR))
500                         continue;
501                     return AVERROR(EIO);
502                 }
503                 if (rtp_check_source_lists(s, addrs[i]))
504                     continue;
505                 return len;
506             }
507         } else if (n < 0) {
508             if (ff_neterrno() == AVERROR(EINTR))
509                 continue;
510             return AVERROR(EIO);
511         }
512         if (h->flags & AVIO_FLAG_NONBLOCK)
513             return AVERROR(EAGAIN);
514     }
515 }
516
517 static int rtp_write(URLContext *h, const uint8_t *buf, int size)
518 {
519     RTPContext *s = h->priv_data;
520     int ret, ret_fec;
521     URLContext *hd;
522
523     if (size < 2)
524         return AVERROR(EINVAL);
525
526     if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
527         av_log(h, AV_LOG_WARNING, "Data doesn't look like RTP packets, "
528                                   "make sure the RTP muxer is used\n");
529
530     if (s->write_to_source) {
531         int fd;
532         struct sockaddr_storage *source, temp_source;
533         socklen_t *source_len, temp_len;
534         if (!s->last_rtp_source.ss_family && !s->last_rtcp_source.ss_family) {
535             av_log(h, AV_LOG_ERROR,
536                    "Unable to send packet to source, no packets received yet\n");
537             // Intentionally not returning an error here
538             return size;
539         }
540
541         if (RTP_PT_IS_RTCP(buf[1])) {
542             fd = s->rtcp_fd;
543             source     = &s->last_rtcp_source;
544             source_len = &s->last_rtcp_source_len;
545         } else {
546             fd = s->rtp_fd;
547             source     = &s->last_rtp_source;
548             source_len = &s->last_rtp_source_len;
549         }
550         if (!source->ss_family) {
551             source      = &temp_source;
552             source_len  = &temp_len;
553             if (RTP_PT_IS_RTCP(buf[1])) {
554                 temp_source = s->last_rtp_source;
555                 temp_len    = s->last_rtp_source_len;
556                 set_port(source, get_port(source) + 1);
557                 av_log(h, AV_LOG_INFO,
558                        "Not received any RTCP packets yet, inferring peer port "
559                        "from the RTP port\n");
560             } else {
561                 temp_source = s->last_rtcp_source;
562                 temp_len    = s->last_rtcp_source_len;
563                 set_port(source, get_port(source) - 1);
564                 av_log(h, AV_LOG_INFO,
565                        "Not received any RTP packets yet, inferring peer port "
566                        "from the RTCP port\n");
567             }
568         }
569
570         if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
571             ret = ff_network_wait_fd(fd, 1);
572             if (ret < 0)
573                 return ret;
574         }
575         ret = sendto(fd, buf, size, 0, (struct sockaddr *) source,
576                      *source_len);
577
578         return ret < 0 ? ff_neterrno() : ret;
579     }
580
581     if (RTP_PT_IS_RTCP(buf[1])) {
582         /* RTCP payload type */
583         hd = s->rtcp_hd;
584     } else {
585         /* RTP payload type */
586         hd = s->rtp_hd;
587     }
588
589     if ((ret = ffurl_write(hd, buf, size)) < 0) {
590         return ret;
591     }
592
593     if (s->fec_hd && !RTP_PT_IS_RTCP(buf[1])) {
594         if ((ret_fec = ffurl_write(s->fec_hd, buf, size)) < 0) {
595             av_log(h, AV_LOG_ERROR, "Failed to send FEC\n");
596             return ret_fec;
597         }
598     }
599
600     return ret;
601 }
602
603 static int rtp_close(URLContext *h)
604 {
605     RTPContext *s = h->priv_data;
606     int i;
607
608     for (i = 0; i < s->nb_ssm_include_addrs; i++)
609         av_freep(&s->ssm_include_addrs[i]);
610     av_freep(&s->ssm_include_addrs);
611     for (i = 0; i < s->nb_ssm_exclude_addrs; i++)
612         av_freep(&s->ssm_exclude_addrs[i]);
613     av_freep(&s->ssm_exclude_addrs);
614
615     ffurl_close(s->rtp_hd);
616     ffurl_close(s->rtcp_hd);
617     ffurl_closep(&s->fec_hd);
618     return 0;
619 }
620
621 /**
622  * Return the local rtp port used by the RTP connection
623  * @param h media file context
624  * @return the local port number
625  */
626
627 int ff_rtp_get_local_rtp_port(URLContext *h)
628 {
629     RTPContext *s = h->priv_data;
630     return ff_udp_get_local_port(s->rtp_hd);
631 }
632
633 /**
634  * Return the local rtcp port used by the RTP connection
635  * @param h media file context
636  * @return the local port number
637  */
638
639 static int rtp_get_file_handle(URLContext *h)
640 {
641     RTPContext *s = h->priv_data;
642     return s->rtp_fd;
643 }
644
645 static int rtp_get_multi_file_handle(URLContext *h, int **handles,
646                                      int *numhandles)
647 {
648     RTPContext *s = h->priv_data;
649     int *hs       = *handles = av_malloc(sizeof(**handles) * 2);
650     if (!hs)
651         return AVERROR(ENOMEM);
652     hs[0] = s->rtp_fd;
653     hs[1] = s->rtcp_fd;
654     *numhandles = 2;
655     return 0;
656 }
657
658 const URLProtocol ff_rtp_protocol = {
659     .name                      = "rtp",
660     .url_open                  = rtp_open,
661     .url_read                  = rtp_read,
662     .url_write                 = rtp_write,
663     .url_close                 = rtp_close,
664     .url_get_file_handle       = rtp_get_file_handle,
665     .url_get_multi_file_handle = rtp_get_multi_file_handle,
666     .priv_data_size            = sizeof(RTPContext),
667     .flags                     = URL_PROTOCOL_FLAG_NETWORK,
668     .priv_data_class           = &rtp_class,
669 };