Coverage Report

Created: 2026-01-16 07:48

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