lavf: Add a protocol for SRTP encryption/decryption
authorMartin Storsjö <martin@martin.st>
Tue, 11 Dec 2012 22:22:48 +0000 (00:22 +0200)
committerMartin Storsjö <martin@martin.st>
Tue, 15 Jan 2013 09:55:10 +0000 (11:55 +0200)
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>
libavformat/Makefile
libavformat/allformats.c
libavformat/srtpproto.c [new file with mode: 0644]
libavformat/version.h

index 7c0b926..1f008a9 100644 (file)
@@ -367,6 +367,7 @@ OBJS-$(CONFIG_RTMPTE_PROTOCOL)           += rtmpproto.o rtmppkt.o
 OBJS-$(CONFIG_RTMPTS_PROTOCOL)           += rtmpproto.o rtmppkt.o
 OBJS-$(CONFIG_RTP_PROTOCOL)              += rtpproto.o
 OBJS-$(CONFIG_SCTP_PROTOCOL)             += sctp.o
+OBJS-$(CONFIG_SRTP_PROTOCOL)             += srtpproto.o srtp.o
 OBJS-$(CONFIG_TCP_PROTOCOL)              += tcp.o
 OBJS-$(CONFIG_TLS_PROTOCOL)              += tls.o
 OBJS-$(CONFIG_UDP_PROTOCOL)              += udp.o
index 34cf566..4aae223 100644 (file)
@@ -280,6 +280,7 @@ void av_register_all(void)
     REGISTER_PROTOCOL(RTMPTS,           rtmpts);
     REGISTER_PROTOCOL(RTP,              rtp);
     REGISTER_PROTOCOL(SCTP,             sctp);
+    REGISTER_PROTOCOL(SRTP,             srtp);
     REGISTER_PROTOCOL(TCP,              tcp);
     REGISTER_PROTOCOL(TLS,              tls);
     REGISTER_PROTOCOL(UDP,              udp);
diff --git a/libavformat/srtpproto.c b/libavformat/srtpproto.c
new file mode 100644 (file)
index 0000000..1cacfdb
--- /dev/null
@@ -0,0 +1,144 @@
+/*
+ * SRTP network protocol
+ * Copyright (c) 2012 Martin Storsjo
+ *
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * Libav is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/opt.h"
+#include "avformat.h"
+#include "avio_internal.h"
+#include "url.h"
+
+#include "internal.h"
+#include "srtp.h"
+
+typedef struct SRTPProtoContext {
+    const AVClass *class;
+    URLContext *rtp_hd;
+    const char *out_suite, *out_params;
+    const char *in_suite, *in_params;
+    struct SRTPContext srtp_out, srtp_in;
+    uint8_t encryptbuf[1500];
+} SRTPProtoContext;
+
+#define D AV_OPT_FLAG_DECODING_PARAM
+#define E AV_OPT_FLAG_ENCODING_PARAM
+static const AVOption options[] = {
+    { "srtp_out_suite", "", offsetof(SRTPProtoContext, out_suite), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
+    { "srtp_out_params", "", offsetof(SRTPProtoContext, out_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
+    { "srtp_in_suite", "", offsetof(SRTPProtoContext, in_suite), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
+    { "srtp_in_params", "", offsetof(SRTPProtoContext, in_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
+    { NULL }
+};
+
+static const AVClass srtp_context_class = {
+    .class_name     = "srtp",
+    .item_name      = av_default_item_name,
+    .option         = options,
+    .version        = LIBAVUTIL_VERSION_INT,
+};
+
+static int srtp_close(URLContext *h)
+{
+    SRTPProtoContext *s = h->priv_data;
+    ff_srtp_free(&s->srtp_out);
+    ff_srtp_free(&s->srtp_in);
+    ffurl_close(s->rtp_hd);
+    s->rtp_hd = NULL;
+    return 0;
+}
+
+static int srtp_open(URLContext *h, const char *uri, int flags)
+{
+    SRTPProtoContext *s = h->priv_data;
+    char hostname[256], buf[1024], path[1024];
+    int rtp_port, ret;
+
+    if (s->out_suite && s->out_params)
+        if ((ret = ff_srtp_set_crypto(&s->srtp_out, s->out_suite, s->out_params)) < 0)
+            goto fail;
+    if (s->in_suite && s->in_params)
+        if ((ret = ff_srtp_set_crypto(&s->srtp_in, s->in_suite, s->in_params)) < 0)
+            goto fail;
+
+    av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
+                 path, sizeof(path), uri);
+    ff_url_join(buf, sizeof(buf), "rtp", NULL, hostname, rtp_port, "%s", path);
+    if ((ret = ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL)) < 0)
+        goto fail;
+
+    h->max_packet_size = FFMIN(s->rtp_hd->max_packet_size,
+                               sizeof(s->encryptbuf)) - 14;
+    h->is_streamed = 1;
+    return 0;
+
+fail:
+    srtp_close(h);
+    return ret;
+}
+
+static int srtp_read(URLContext *h, uint8_t *buf, int size)
+{
+    SRTPProtoContext *s = h->priv_data;
+    int ret;
+start:
+    ret = ffurl_read(s->rtp_hd, buf, size);
+    if (ret > 0 && s->srtp_in.aes) {
+        if (ff_srtp_decrypt(&s->srtp_in, buf, &ret) < 0)
+            goto start;
+    }
+    return ret;
+}
+
+static int srtp_write(URLContext *h, const uint8_t *buf, int size)
+{
+    SRTPProtoContext *s = h->priv_data;
+    if (!s->srtp_out.aes)
+        return ffurl_write(s->rtp_hd, buf, size);
+    size = ff_srtp_encrypt(&s->srtp_out, buf, size, s->encryptbuf,
+                           sizeof(s->encryptbuf));
+    if (size < 0)
+        return size;
+    return ffurl_write(s->rtp_hd, s->encryptbuf, size);
+}
+
+static int srtp_get_file_handle(URLContext *h)
+{
+    SRTPProtoContext *s = h->priv_data;
+    return ffurl_get_file_handle(s->rtp_hd);
+}
+
+static int srtp_get_multi_file_handle(URLContext *h, int **handles,
+                                      int *numhandles)
+{
+    SRTPProtoContext *s = h->priv_data;
+    return ffurl_get_multi_file_handle(s->rtp_hd, handles, numhandles);
+}
+
+URLProtocol ff_srtp_protocol = {
+    .name                      = "srtp",
+    .url_open                  = srtp_open,
+    .url_read                  = srtp_read,
+    .url_write                 = srtp_write,
+    .url_close                 = srtp_close,
+    .url_get_file_handle       = srtp_get_file_handle,
+    .url_get_multi_file_handle = srtp_get_multi_file_handle,
+    .priv_data_size            = sizeof(SRTPProtoContext),
+    .priv_data_class           = &srtp_context_class,
+    .flags                     = URL_PROTOCOL_FLAG_NETWORK,
+};
index cb6bc77..89f1f6c 100644 (file)
@@ -30,8 +30,8 @@
 #include "libavutil/avutil.h"
 
 #define LIBAVFORMAT_VERSION_MAJOR 54
-#define LIBAVFORMAT_VERSION_MINOR 20
-#define LIBAVFORMAT_VERSION_MICRO  5
+#define LIBAVFORMAT_VERSION_MINOR 21
+#define LIBAVFORMAT_VERSION_MICRO  0
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \