yading@11: /* yading@11: * SRTP network protocol yading@11: * Copyright (c) 2012 Martin Storsjo yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include "libavutil/opt.h" yading@11: #include "avformat.h" yading@11: #include "avio_internal.h" yading@11: #include "url.h" yading@11: yading@11: #include "internal.h" yading@11: #include "rtpdec.h" yading@11: #include "srtp.h" yading@11: yading@11: typedef struct SRTPProtoContext { yading@11: const AVClass *class; yading@11: URLContext *rtp_hd; yading@11: const char *out_suite, *out_params; yading@11: const char *in_suite, *in_params; yading@11: struct SRTPContext srtp_out, srtp_in; yading@11: uint8_t encryptbuf[RTP_MAX_PACKET_LENGTH]; yading@11: } SRTPProtoContext; yading@11: yading@11: #define D AV_OPT_FLAG_DECODING_PARAM yading@11: #define E AV_OPT_FLAG_ENCODING_PARAM yading@11: static const AVOption options[] = { yading@11: { "srtp_out_suite", "", offsetof(SRTPProtoContext, out_suite), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E }, yading@11: { "srtp_out_params", "", offsetof(SRTPProtoContext, out_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E }, yading@11: { "srtp_in_suite", "", offsetof(SRTPProtoContext, in_suite), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E }, yading@11: { "srtp_in_params", "", offsetof(SRTPProtoContext, in_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E }, yading@11: { NULL } yading@11: }; yading@11: yading@11: static const AVClass srtp_context_class = { yading@11: .class_name = "srtp", yading@11: .item_name = av_default_item_name, yading@11: .option = options, yading@11: .version = LIBAVUTIL_VERSION_INT, yading@11: }; yading@11: yading@11: static int srtp_close(URLContext *h) yading@11: { yading@11: SRTPProtoContext *s = h->priv_data; yading@11: ff_srtp_free(&s->srtp_out); yading@11: ff_srtp_free(&s->srtp_in); yading@11: ffurl_close(s->rtp_hd); yading@11: s->rtp_hd = NULL; yading@11: return 0; yading@11: } yading@11: yading@11: static int srtp_open(URLContext *h, const char *uri, int flags) yading@11: { yading@11: SRTPProtoContext *s = h->priv_data; yading@11: char hostname[256], buf[1024], path[1024]; yading@11: int rtp_port, ret; yading@11: yading@11: if (s->out_suite && s->out_params) yading@11: if ((ret = ff_srtp_set_crypto(&s->srtp_out, s->out_suite, s->out_params)) < 0) yading@11: goto fail; yading@11: if (s->in_suite && s->in_params) yading@11: if ((ret = ff_srtp_set_crypto(&s->srtp_in, s->in_suite, s->in_params)) < 0) yading@11: goto fail; yading@11: yading@11: av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port, yading@11: path, sizeof(path), uri); yading@11: ff_url_join(buf, sizeof(buf), "rtp", NULL, hostname, rtp_port, "%s", path); yading@11: if ((ret = ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL)) < 0) yading@11: goto fail; yading@11: yading@11: h->max_packet_size = FFMIN(s->rtp_hd->max_packet_size, yading@11: sizeof(s->encryptbuf)) - 14; yading@11: h->is_streamed = 1; yading@11: return 0; yading@11: yading@11: fail: yading@11: srtp_close(h); yading@11: return ret; yading@11: } yading@11: yading@11: static int srtp_read(URLContext *h, uint8_t *buf, int size) yading@11: { yading@11: SRTPProtoContext *s = h->priv_data; yading@11: int ret; yading@11: start: yading@11: ret = ffurl_read(s->rtp_hd, buf, size); yading@11: if (ret > 0 && s->srtp_in.aes) { yading@11: if (ff_srtp_decrypt(&s->srtp_in, buf, &ret) < 0) yading@11: goto start; yading@11: } yading@11: return ret; yading@11: } yading@11: yading@11: static int srtp_write(URLContext *h, const uint8_t *buf, int size) yading@11: { yading@11: SRTPProtoContext *s = h->priv_data; yading@11: if (!s->srtp_out.aes) yading@11: return ffurl_write(s->rtp_hd, buf, size); yading@11: size = ff_srtp_encrypt(&s->srtp_out, buf, size, s->encryptbuf, yading@11: sizeof(s->encryptbuf)); yading@11: if (size < 0) yading@11: return size; yading@11: return ffurl_write(s->rtp_hd, s->encryptbuf, size); yading@11: } yading@11: yading@11: static int srtp_get_file_handle(URLContext *h) yading@11: { yading@11: SRTPProtoContext *s = h->priv_data; yading@11: return ffurl_get_file_handle(s->rtp_hd); yading@11: } yading@11: yading@11: static int srtp_get_multi_file_handle(URLContext *h, int **handles, yading@11: int *numhandles) yading@11: { yading@11: SRTPProtoContext *s = h->priv_data; yading@11: return ffurl_get_multi_file_handle(s->rtp_hd, handles, numhandles); yading@11: } yading@11: yading@11: URLProtocol ff_srtp_protocol = { yading@11: .name = "srtp", yading@11: .url_open = srtp_open, yading@11: .url_read = srtp_read, yading@11: .url_write = srtp_write, yading@11: .url_close = srtp_close, yading@11: .url_get_file_handle = srtp_get_file_handle, yading@11: .url_get_multi_file_handle = srtp_get_multi_file_handle, yading@11: .priv_data_size = sizeof(SRTPProtoContext), yading@11: .priv_data_class = &srtp_context_class, yading@11: .flags = URL_PROTOCOL_FLAG_NETWORK, yading@11: };