FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "avio_internal.h"
25 #include "url.h"
26 
27 #include "internal.h"
28 #include "rtpdec.h"
29 #include "srtp.h"
30 
31 typedef struct SRTPProtoContext {
32  const AVClass *class;
34  const char *out_suite, *out_params;
35  const char *in_suite, *in_params;
36  struct SRTPContext srtp_out, srtp_in;
39 
40 #define D AV_OPT_FLAG_DECODING_PARAM
41 #define E AV_OPT_FLAG_ENCODING_PARAM
42 static const AVOption options[] = {
43  { "srtp_out_suite", "", offsetof(SRTPProtoContext, out_suite), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
44  { "srtp_out_params", "", offsetof(SRTPProtoContext, out_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
45  { "srtp_in_suite", "", offsetof(SRTPProtoContext, in_suite), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
46  { "srtp_in_params", "", offsetof(SRTPProtoContext, in_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
47  { NULL }
48 };
49 
50 static const AVClass srtp_context_class = {
51  .class_name = "srtp",
52  .item_name = av_default_item_name,
53  .option = options,
54  .version = LIBAVUTIL_VERSION_INT,
55 };
56 
57 static int srtp_close(URLContext *h)
58 {
60  ff_srtp_free(&s->srtp_out);
61  ff_srtp_free(&s->srtp_in);
62  ffurl_close(s->rtp_hd);
63  s->rtp_hd = NULL;
64  return 0;
65 }
66 
67 static int srtp_open(URLContext *h, const char *uri, int flags)
68 {
70  char hostname[256], buf[1024], path[1024];
71  int rtp_port, ret;
72 
73  if (s->out_suite && s->out_params)
74  if ((ret = ff_srtp_set_crypto(&s->srtp_out, s->out_suite, s->out_params)) < 0)
75  goto fail;
76  if (s->in_suite && s->in_params)
77  if ((ret = ff_srtp_set_crypto(&s->srtp_in, s->in_suite, s->in_params)) < 0)
78  goto fail;
79 
80  av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
81  path, sizeof(path), uri);
82  ff_url_join(buf, sizeof(buf), "rtp", NULL, hostname, rtp_port, "%s", path);
83  if ((ret = ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL)) < 0)
84  goto fail;
85 
87  sizeof(s->encryptbuf)) - 14;
88  h->is_streamed = 1;
89  return 0;
90 
91 fail:
92  srtp_close(h);
93  return ret;
94 }
95 
96 static int srtp_read(URLContext *h, uint8_t *buf, int size)
97 {
99  int ret;
100 start:
101  ret = ffurl_read(s->rtp_hd, buf, size);
102  if (ret > 0 && s->srtp_in.aes) {
103  if (ff_srtp_decrypt(&s->srtp_in, buf, &ret) < 0)
104  goto start;
105  }
106  return ret;
107 }
108 
109 static int srtp_write(URLContext *h, const uint8_t *buf, int size)
110 {
112  if (!s->srtp_out.aes)
113  return ffurl_write(s->rtp_hd, buf, size);
114  size = ff_srtp_encrypt(&s->srtp_out, buf, size, s->encryptbuf,
115  sizeof(s->encryptbuf));
116  if (size < 0)
117  return size;
118  return ffurl_write(s->rtp_hd, s->encryptbuf, size);
119 }
120 
122 {
124  return ffurl_get_file_handle(s->rtp_hd);
125 }
126 
127 static int srtp_get_multi_file_handle(URLContext *h, int **handles,
128  int *numhandles)
129 {
131  return ffurl_get_multi_file_handle(s->rtp_hd, handles, numhandles);
132 }
133 
135  .name = "srtp",
136  .url_open = srtp_open,
137  .url_read = srtp_read,
138  .url_write = srtp_write,
139  .url_close = srtp_close,
140  .url_get_file_handle = srtp_get_file_handle,
141  .url_get_multi_file_handle = srtp_get_multi_file_handle,
142  .priv_data_size = sizeof(SRTPProtoContext),
143  .priv_data_class = &srtp_context_class,
145 };
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:3951
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:35
URLProtocol ff_srtp_protocol
Definition: srtpproto.c:134
int ff_srtp_encrypt(struct SRTPContext *s, const uint8_t *in, int len, uint8_t *out, int outlen)
Definition: srtp.c:238
#define RTP_MAX_PACKET_LENGTH
Definition: rtpdec.h:36
AVOption.
Definition: opt.h:255
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition: avio.c:347
int is_streamed
true if streamed (no seek possible), default = false
Definition: url.h:46
uint8_t encryptbuf[RTP_MAX_PACKET_LENGTH]
Definition: srtpproto.c:37
AVIOInterruptCB interrupt_callback
Definition: url.h:48
static int srtp_get_file_handle(URLContext *h)
Definition: srtpproto.c:121
static int srtp_read(URLContext *h, uint8_t *buf, int size)
Definition: srtpproto.c:96
static const AVClass srtp_context_class
Definition: srtpproto.c:50
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
void ff_srtp_free(struct SRTPContext *s)
Definition: srtp.c:31
uint8_t
AVOptions.
const char * out_params
Definition: srtpproto.c:34
static int srtp_write(URLContext *h, const uint8_t *buf, int size)
Definition: srtpproto.c:109
ptrdiff_t size
Definition: opengl_enc.c:101
int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
Return the file descriptors associated with this URL.
Definition: avio.c:558
static int srtp_open(URLContext *h, const char *uri, int flags)
Definition: srtpproto.c:67
struct AVAES * aes
Definition: srtp.h:31
av_default_item_name
#define fail()
Definition: checkasm.h:55
struct SRTPContext srtp_out srtp_in
Definition: srtpproto.c:36
const char * out_suite
Definition: srtpproto.c:34
int ff_srtp_decrypt(struct SRTPContext *s, uint8_t *buf, int *lenptr)
Definition: srtp.c:126
#define FFMIN(a, b)
Definition: common.h:66
ret
Definition: avfilter.c:974
static int srtp_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
Definition: srtpproto.c:127
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
Definition: avio.c:551
static const AVOption options[]
Definition: srtpproto.c:42
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:36
const char * in_suite
Definition: srtpproto.c:35
const char * in_params
Definition: srtpproto.c:35
void * buf
Definition: avisynth_c.h:553
Definition: url.h:39
#define E
Definition: srtpproto.c:41
Describe the class of an AVClass context structure.
Definition: log.h:67
void * priv_data
Definition: url.h:42
const char * name
Definition: url.h:53
URLContext * rtp_hd
Definition: srtpproto.c:33
static int flags
Definition: cpu.c:47
int ffurl_close(URLContext *h)
Definition: avio.c:392
Main libavformat public API header.
int ffurl_open(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
Definition: avio.c:272
static int srtp_close(URLContext *h)
Definition: srtpproto.c:57
int ff_srtp_set_crypto(struct SRTPContext *s, const char *suite, const char *params)
Definition: srtp.c:65
int max_packet_size
if non zero, the stream is packetized with this max packet size
Definition: url.h:45
void INT64 start
Definition: avisynth_c.h:553
unbuffered private I/O API
#define D
Definition: srtpproto.c:40
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf...
Definition: avio.c:333