Browse code

lavf: Add a protocol for SRTP encryption/decryption

This is mostly useful for encryption together with the RTP muxer,
but could also be set up as IO towards the peer with the SDP
demuxer with custom IO.

Signed-off-by: Martin Storsjö <martin@martin.st>

Martin Storsjö authored on 2012/12/12 07:22:48
Showing 4 changed files
... ...
@@ -367,6 +367,7 @@ OBJS-$(CONFIG_RTMPTE_PROTOCOL)           += rtmpproto.o rtmppkt.o
367 367
 OBJS-$(CONFIG_RTMPTS_PROTOCOL)           += rtmpproto.o rtmppkt.o
368 368
 OBJS-$(CONFIG_RTP_PROTOCOL)              += rtpproto.o
369 369
 OBJS-$(CONFIG_SCTP_PROTOCOL)             += sctp.o
370
+OBJS-$(CONFIG_SRTP_PROTOCOL)             += srtpproto.o srtp.o
370 371
 OBJS-$(CONFIG_TCP_PROTOCOL)              += tcp.o
371 372
 OBJS-$(CONFIG_TLS_PROTOCOL)              += tls.o
372 373
 OBJS-$(CONFIG_UDP_PROTOCOL)              += udp.o
... ...
@@ -280,6 +280,7 @@ void av_register_all(void)
280 280
     REGISTER_PROTOCOL(RTMPTS,           rtmpts);
281 281
     REGISTER_PROTOCOL(RTP,              rtp);
282 282
     REGISTER_PROTOCOL(SCTP,             sctp);
283
+    REGISTER_PROTOCOL(SRTP,             srtp);
283 284
     REGISTER_PROTOCOL(TCP,              tcp);
284 285
     REGISTER_PROTOCOL(TLS,              tls);
285 286
     REGISTER_PROTOCOL(UDP,              udp);
286 287
new file mode 100644
... ...
@@ -0,0 +1,144 @@
0
+/*
1
+ * SRTP network protocol
2
+ * Copyright (c) 2012 Martin Storsjo
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * Libav is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "libavutil/opt.h"
22
+#include "avformat.h"
23
+#include "avio_internal.h"
24
+#include "url.h"
25
+
26
+#include "internal.h"
27
+#include "srtp.h"
28
+
29
+typedef struct SRTPProtoContext {
30
+    const AVClass *class;
31
+    URLContext *rtp_hd;
32
+    const char *out_suite, *out_params;
33
+    const char *in_suite, *in_params;
34
+    struct SRTPContext srtp_out, srtp_in;
35
+    uint8_t encryptbuf[1500];
36
+} SRTPProtoContext;
37
+
38
+#define D AV_OPT_FLAG_DECODING_PARAM
39
+#define E AV_OPT_FLAG_ENCODING_PARAM
40
+static const AVOption 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, E },
44
+    { "srtp_in_params", "", offsetof(SRTPProtoContext, in_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
45
+    { NULL }
46
+};
47
+
48
+static const AVClass srtp_context_class = {
49
+    .class_name     = "srtp",
50
+    .item_name      = av_default_item_name,
51
+    .option         = options,
52
+    .version        = LIBAVUTIL_VERSION_INT,
53
+};
54
+
55
+static int srtp_close(URLContext *h)
56
+{
57
+    SRTPProtoContext *s = h->priv_data;
58
+    ff_srtp_free(&s->srtp_out);
59
+    ff_srtp_free(&s->srtp_in);
60
+    ffurl_close(s->rtp_hd);
61
+    s->rtp_hd = NULL;
62
+    return 0;
63
+}
64
+
65
+static int srtp_open(URLContext *h, const char *uri, int flags)
66
+{
67
+    SRTPProtoContext *s = h->priv_data;
68
+    char hostname[256], buf[1024], path[1024];
69
+    int rtp_port, ret;
70
+
71
+    if (s->out_suite && s->out_params)
72
+        if ((ret = ff_srtp_set_crypto(&s->srtp_out, s->out_suite, s->out_params)) < 0)
73
+            goto fail;
74
+    if (s->in_suite && s->in_params)
75
+        if ((ret = ff_srtp_set_crypto(&s->srtp_in, s->in_suite, s->in_params)) < 0)
76
+            goto fail;
77
+
78
+    av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
79
+                 path, sizeof(path), uri);
80
+    ff_url_join(buf, sizeof(buf), "rtp", NULL, hostname, rtp_port, "%s", path);
81
+    if ((ret = ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL)) < 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
+
89
+fail:
90
+    srtp_close(h);
91
+    return ret;
92
+}
93
+
94
+static int srtp_read(URLContext *h, uint8_t *buf, int size)
95
+{
96
+    SRTPProtoContext *s = h->priv_data;
97
+    int ret;
98
+start:
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
+
107
+static 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
+
119
+static int srtp_get_file_handle(URLContext *h)
120
+{
121
+    SRTPProtoContext *s = h->priv_data;
122
+    return ffurl_get_file_handle(s->rtp_hd);
123
+}
124
+
125
+static 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
+
132
+URLProtocol ff_srtp_protocol = {
133
+    .name                      = "srtp",
134
+    .url_open                  = 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,
142
+    .flags                     = URL_PROTOCOL_FLAG_NETWORK,
143
+};
... ...
@@ -30,8 +30,8 @@
30 30
 #include "libavutil/avutil.h"
31 31
 
32 32
 #define LIBAVFORMAT_VERSION_MAJOR 54
33
-#define LIBAVFORMAT_VERSION_MINOR 20
34
-#define LIBAVFORMAT_VERSION_MICRO  5
33
+#define LIBAVFORMAT_VERSION_MINOR 21
34
+#define LIBAVFORMAT_VERSION_MICRO  0
35 35
 
36 36
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
37 37
                                                LIBAVFORMAT_VERSION_MINOR, \