yading@11
|
1 /*
|
yading@11
|
2 * SRTP network protocol
|
yading@11
|
3 * Copyright (c) 2012 Martin Storsjo
|
yading@11
|
4 *
|
yading@11
|
5 * This file is part of FFmpeg.
|
yading@11
|
6 *
|
yading@11
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@11
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@11
|
9 * License as published by the Free Software Foundation; either
|
yading@11
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@11
|
11 *
|
yading@11
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@11
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@11
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@11
|
15 * Lesser General Public License for more details.
|
yading@11
|
16 *
|
yading@11
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@11
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@11
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@11
|
20 */
|
yading@11
|
21
|
yading@11
|
22 #include "libavutil/opt.h"
|
yading@11
|
23 #include "avformat.h"
|
yading@11
|
24 #include "avio_internal.h"
|
yading@11
|
25 #include "url.h"
|
yading@11
|
26
|
yading@11
|
27 #include "internal.h"
|
yading@11
|
28 #include "rtpdec.h"
|
yading@11
|
29 #include "srtp.h"
|
yading@11
|
30
|
yading@11
|
31 typedef struct SRTPProtoContext {
|
yading@11
|
32 const AVClass *class;
|
yading@11
|
33 URLContext *rtp_hd;
|
yading@11
|
34 const char *out_suite, *out_params;
|
yading@11
|
35 const char *in_suite, *in_params;
|
yading@11
|
36 struct SRTPContext srtp_out, srtp_in;
|
yading@11
|
37 uint8_t encryptbuf[RTP_MAX_PACKET_LENGTH];
|
yading@11
|
38 } SRTPProtoContext;
|
yading@11
|
39
|
yading@11
|
40 #define D AV_OPT_FLAG_DECODING_PARAM
|
yading@11
|
41 #define E AV_OPT_FLAG_ENCODING_PARAM
|
yading@11
|
42 static const AVOption options[] = {
|
yading@11
|
43 { "srtp_out_suite", "", offsetof(SRTPProtoContext, out_suite), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
|
yading@11
|
44 { "srtp_out_params", "", offsetof(SRTPProtoContext, out_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
|
yading@11
|
45 { "srtp_in_suite", "", offsetof(SRTPProtoContext, in_suite), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
|
yading@11
|
46 { "srtp_in_params", "", offsetof(SRTPProtoContext, in_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
|
yading@11
|
47 { NULL }
|
yading@11
|
48 };
|
yading@11
|
49
|
yading@11
|
50 static const AVClass srtp_context_class = {
|
yading@11
|
51 .class_name = "srtp",
|
yading@11
|
52 .item_name = av_default_item_name,
|
yading@11
|
53 .option = options,
|
yading@11
|
54 .version = LIBAVUTIL_VERSION_INT,
|
yading@11
|
55 };
|
yading@11
|
56
|
yading@11
|
57 static int srtp_close(URLContext *h)
|
yading@11
|
58 {
|
yading@11
|
59 SRTPProtoContext *s = h->priv_data;
|
yading@11
|
60 ff_srtp_free(&s->srtp_out);
|
yading@11
|
61 ff_srtp_free(&s->srtp_in);
|
yading@11
|
62 ffurl_close(s->rtp_hd);
|
yading@11
|
63 s->rtp_hd = NULL;
|
yading@11
|
64 return 0;
|
yading@11
|
65 }
|
yading@11
|
66
|
yading@11
|
67 static int srtp_open(URLContext *h, const char *uri, int flags)
|
yading@11
|
68 {
|
yading@11
|
69 SRTPProtoContext *s = h->priv_data;
|
yading@11
|
70 char hostname[256], buf[1024], path[1024];
|
yading@11
|
71 int rtp_port, ret;
|
yading@11
|
72
|
yading@11
|
73 if (s->out_suite && s->out_params)
|
yading@11
|
74 if ((ret = ff_srtp_set_crypto(&s->srtp_out, s->out_suite, s->out_params)) < 0)
|
yading@11
|
75 goto fail;
|
yading@11
|
76 if (s->in_suite && s->in_params)
|
yading@11
|
77 if ((ret = ff_srtp_set_crypto(&s->srtp_in, s->in_suite, s->in_params)) < 0)
|
yading@11
|
78 goto fail;
|
yading@11
|
79
|
yading@11
|
80 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
|
yading@11
|
81 path, sizeof(path), uri);
|
yading@11
|
82 ff_url_join(buf, sizeof(buf), "rtp", NULL, hostname, rtp_port, "%s", path);
|
yading@11
|
83 if ((ret = ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL)) < 0)
|
yading@11
|
84 goto fail;
|
yading@11
|
85
|
yading@11
|
86 h->max_packet_size = FFMIN(s->rtp_hd->max_packet_size,
|
yading@11
|
87 sizeof(s->encryptbuf)) - 14;
|
yading@11
|
88 h->is_streamed = 1;
|
yading@11
|
89 return 0;
|
yading@11
|
90
|
yading@11
|
91 fail:
|
yading@11
|
92 srtp_close(h);
|
yading@11
|
93 return ret;
|
yading@11
|
94 }
|
yading@11
|
95
|
yading@11
|
96 static int srtp_read(URLContext *h, uint8_t *buf, int size)
|
yading@11
|
97 {
|
yading@11
|
98 SRTPProtoContext *s = h->priv_data;
|
yading@11
|
99 int ret;
|
yading@11
|
100 start:
|
yading@11
|
101 ret = ffurl_read(s->rtp_hd, buf, size);
|
yading@11
|
102 if (ret > 0 && s->srtp_in.aes) {
|
yading@11
|
103 if (ff_srtp_decrypt(&s->srtp_in, buf, &ret) < 0)
|
yading@11
|
104 goto start;
|
yading@11
|
105 }
|
yading@11
|
106 return ret;
|
yading@11
|
107 }
|
yading@11
|
108
|
yading@11
|
109 static int srtp_write(URLContext *h, const uint8_t *buf, int size)
|
yading@11
|
110 {
|
yading@11
|
111 SRTPProtoContext *s = h->priv_data;
|
yading@11
|
112 if (!s->srtp_out.aes)
|
yading@11
|
113 return ffurl_write(s->rtp_hd, buf, size);
|
yading@11
|
114 size = ff_srtp_encrypt(&s->srtp_out, buf, size, s->encryptbuf,
|
yading@11
|
115 sizeof(s->encryptbuf));
|
yading@11
|
116 if (size < 0)
|
yading@11
|
117 return size;
|
yading@11
|
118 return ffurl_write(s->rtp_hd, s->encryptbuf, size);
|
yading@11
|
119 }
|
yading@11
|
120
|
yading@11
|
121 static int srtp_get_file_handle(URLContext *h)
|
yading@11
|
122 {
|
yading@11
|
123 SRTPProtoContext *s = h->priv_data;
|
yading@11
|
124 return ffurl_get_file_handle(s->rtp_hd);
|
yading@11
|
125 }
|
yading@11
|
126
|
yading@11
|
127 static int srtp_get_multi_file_handle(URLContext *h, int **handles,
|
yading@11
|
128 int *numhandles)
|
yading@11
|
129 {
|
yading@11
|
130 SRTPProtoContext *s = h->priv_data;
|
yading@11
|
131 return ffurl_get_multi_file_handle(s->rtp_hd, handles, numhandles);
|
yading@11
|
132 }
|
yading@11
|
133
|
yading@11
|
134 URLProtocol ff_srtp_protocol = {
|
yading@11
|
135 .name = "srtp",
|
yading@11
|
136 .url_open = srtp_open,
|
yading@11
|
137 .url_read = srtp_read,
|
yading@11
|
138 .url_write = srtp_write,
|
yading@11
|
139 .url_close = srtp_close,
|
yading@11
|
140 .url_get_file_handle = srtp_get_file_handle,
|
yading@11
|
141 .url_get_multi_file_handle = srtp_get_multi_file_handle,
|
yading@11
|
142 .priv_data_size = sizeof(SRTPProtoContext),
|
yading@11
|
143 .priv_data_class = &srtp_context_class,
|
yading@11
|
144 .flags = URL_PROTOCOL_FLAG_NETWORK,
|
yading@11
|
145 };
|