FFmpeg
Loading...
Searching...
No Matches
srtpproto.c
Go to the documentation of this file.
1/*
2 * SRTP network protocol
3 * Copyright (c) 2012 Martin Storsjo
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#include "libavutil/opt.h"
23#include "avformat.h"
24#include "url.h"
25
26#include "rtpdec.h"
27#include "srtp.h"
28
29typedef struct SRTPProtoContext {
30 const AVClass *class;
32 const char *out_suite, *out_params;
33 const char *in_suite, *in_params;
34 struct SRTPContext srtp_out, srtp_in;
37
38#define D AV_OPT_FLAG_DECODING_PARAM
39#define E AV_OPT_FLAG_ENCODING_PARAM
40static const AVOption srtp_options[] = {
41 { "srtp_out_suite", "", offsetof(SRTPProtoContext, out_suite), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
42 { "srtp_out_params", "", offsetof(SRTPProtoContext, out_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
43 { "srtp_in_suite", "", offsetof(SRTPProtoContext, in_suite), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
44 { "srtp_in_params", "", offsetof(SRTPProtoContext, in_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
45 { NULL }
46};
47
49 .class_name = "srtp",
50 .item_name = av_default_item_name,
51 .option = srtp_options,
52 .version = LIBAVUTIL_VERSION_INT,
53};
54
56{
57 SRTPProtoContext *s = h->priv_data;
58 ff_srtp_free(&s->srtp_out);
59 ff_srtp_free(&s->srtp_in);
60 ffurl_closep(&s->rtp_hd);
61 return 0;
62}
63
64static int srtp_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
65{
66 SRTPProtoContext *s = h->priv_data;
67 char hostname[256], buf[1024], path[1024];
68 int rtp_port, ret;
69
70 if (s->out_suite && s->out_params)
71 if ((ret = ff_srtp_set_crypto(&s->srtp_out, s->out_suite, s->out_params)) < 0)
72 goto fail;
73 if (s->in_suite && s->in_params)
74 if ((ret = ff_srtp_set_crypto(&s->srtp_in, s->in_suite, s->in_params)) < 0)
75 goto fail;
76
77 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
78 path, sizeof(path), uri);
79 ff_url_join(buf, sizeof(buf), "rtp", NULL, hostname, rtp_port, "%s", path);
80 if ((ret = ffurl_open_whitelist(&s->rtp_hd, buf, flags, &h->interrupt_callback,
81 options, h->protocol_whitelist, h->protocol_blacklist, h)) < 0)
82 goto fail;
83
84 h->max_packet_size = FFMIN(s->rtp_hd->max_packet_size,
85 sizeof(s->encryptbuf)) - 14;
86 h->is_streamed = 1;
87 return 0;
88
89fail:
91 return ret;
92}
93
94static int srtp_read(URLContext *h, uint8_t *buf, int size)
95{
96 SRTPProtoContext *s = h->priv_data;
97 int ret;
98start:
99 ret = ffurl_read(s->rtp_hd, buf, size);
100 if (ret > 0 && s->srtp_in.aes) {
101 if (ff_srtp_decrypt(&s->srtp_in, buf, &ret) < 0)
102 goto start;
103 }
104 return ret;
105}
106
107static int srtp_write(URLContext *h, const uint8_t *buf, int size)
108{
109 SRTPProtoContext *s = h->priv_data;
110 if (!s->srtp_out.aes)
111 return ffurl_write(s->rtp_hd, buf, size);
112 size = ff_srtp_encrypt(&s->srtp_out, buf, size, s->encryptbuf,
113 sizeof(s->encryptbuf));
114 if (size < 0)
115 return size;
116 return ffurl_write(s->rtp_hd, s->encryptbuf, size);
117}
118
120{
121 SRTPProtoContext *s = h->priv_data;
122 return ffurl_get_file_handle(s->rtp_hd);
123}
124
125static int srtp_get_multi_file_handle(URLContext *h, int **handles,
126 int *numhandles)
127{
128 SRTPProtoContext *s = h->priv_data;
129 return ffurl_get_multi_file_handle(s->rtp_hd, handles, numhandles);
130}
131
133 .name = "srtp",
134 .url_open2 = srtp_open,
135 .url_read = srtp_read,
136 .url_write = srtp_write,
137 .url_close = srtp_close,
138 .url_get_file_handle = srtp_get_file_handle,
139 .url_get_multi_file_handle = srtp_get_multi_file_handle,
140 .priv_data_size = sizeof(SRTPProtoContext),
141 .priv_data_class = &srtp_context_class,
143};
#define E
Definition avdct.c:34
#define D
Definition avdct.c:35
Main libavformat public API header.
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it.
Definition avio.c:461
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition avio.c:656
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
Definition avio.c:882
int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
Return the file descriptors associated with this URL.
Definition avio.c:889
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
#define fail
Definition test.h:479
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
Definition utils.c:361
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define FFMIN(a, b)
Definition macros.h:49
AVOptions.
const URLProtocol ff_srtp_protocol
Definition srtpproto.c:132
#define RTP_MAX_PACKET_LENGTH
Definition rtpdec.h:37
int ff_srtp_encrypt(struct SRTPContext *s, const uint8_t *in, int len, uint8_t *out, int outlen)
Definition srtp.c:239
int ff_srtp_set_crypto(struct SRTPContext *s, const char *suite, const char *params)
Definition srtp.c:66
int ff_srtp_decrypt(struct SRTPContext *s, uint8_t *buf, int *lenptr)
Definition srtp.c:127
void ff_srtp_free(struct SRTPContext *s)
Definition srtp.c:32
static const AVOption srtp_options[]
Definition srtpproto.c:40
static int srtp_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
Definition srtpproto.c:125
static int srtp_read(URLContext *h, uint8_t *buf, int size)
Definition srtpproto.c:94
static int srtp_close(URLContext *h)
Definition srtpproto.c:55
static int srtp_write(URLContext *h, const uint8_t *buf, int size)
Definition srtpproto.c:107
static int srtp_get_file_handle(URLContext *h)
Definition srtpproto.c:119
static const AVClass srtp_context_class
Definition srtpproto.c:48
static int srtp_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
Definition srtpproto.c:64
Describe the class of an AVClass context structure.
Definition log.h:76
AVOption.
Definition opt.h:428
const char * out_suite
Definition srtpproto.c:32
const char * in_params
Definition srtpproto.c:33
const char * out_params
Definition srtpproto.c:32
uint8_t encryptbuf[RTP_MAX_PACKET_LENGTH]
Definition srtpproto.c:35
const char * in_suite
Definition srtpproto.c:33
struct SRTPContext srtp_out srtp_in
Definition srtpproto.c:34
URLContext * rtp_hd
Definition srtpproto.c:31
int size
int ff_url_join(char *str, int size, const char *proto, const char *authorization, const char *hostname, int port, const char *fmt,...)
Definition url.c:40
unbuffered private I/O API
static int ffurl_write(URLContext *h, const uint8_t *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition url.h:204
static int ffurl_read(URLContext *h, uint8_t *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf.
Definition url.h:183
#define URL_PROTOCOL_FLAG_NETWORK
Definition url.h:33