You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.7KB

  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. #include "libavutil/opt.h"
  22. #include "avformat.h"
  23. #include "avio_internal.h"
  24. #include "url.h"
  25. #include "internal.h"
  26. #include "srtp.h"
  27. typedef struct SRTPProtoContext {
  28. const AVClass *class;
  29. URLContext *rtp_hd;
  30. const char *out_suite, *out_params;
  31. const char *in_suite, *in_params;
  32. struct SRTPContext srtp_out, srtp_in;
  33. uint8_t encryptbuf[1500];
  34. } SRTPProtoContext;
  35. #define D AV_OPT_FLAG_DECODING_PARAM
  36. #define E AV_OPT_FLAG_ENCODING_PARAM
  37. static const AVOption options[] = {
  38. { "srtp_out_suite", "", offsetof(SRTPProtoContext, out_suite), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
  39. { "srtp_out_params", "", offsetof(SRTPProtoContext, out_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
  40. { "srtp_in_suite", "", offsetof(SRTPProtoContext, in_suite), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
  41. { "srtp_in_params", "", offsetof(SRTPProtoContext, in_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
  42. { NULL }
  43. };
  44. static const AVClass srtp_context_class = {
  45. .class_name = "srtp",
  46. .item_name = av_default_item_name,
  47. .option = options,
  48. .version = LIBAVUTIL_VERSION_INT,
  49. };
  50. static int srtp_close(URLContext *h)
  51. {
  52. SRTPProtoContext *s = h->priv_data;
  53. ff_srtp_free(&s->srtp_out);
  54. ff_srtp_free(&s->srtp_in);
  55. ffurl_close(s->rtp_hd);
  56. s->rtp_hd = NULL;
  57. return 0;
  58. }
  59. static int srtp_open(URLContext *h, const char *uri, int flags)
  60. {
  61. SRTPProtoContext *s = h->priv_data;
  62. char hostname[256], buf[1024], path[1024];
  63. int rtp_port, ret;
  64. if (s->out_suite && s->out_params)
  65. if ((ret = ff_srtp_set_crypto(&s->srtp_out, s->out_suite, s->out_params)) < 0)
  66. goto fail;
  67. if (s->in_suite && s->in_params)
  68. if ((ret = ff_srtp_set_crypto(&s->srtp_in, s->in_suite, s->in_params)) < 0)
  69. goto fail;
  70. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
  71. path, sizeof(path), uri);
  72. ff_url_join(buf, sizeof(buf), "rtp", NULL, hostname, rtp_port, "%s", path);
  73. if ((ret = ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL)) < 0)
  74. goto fail;
  75. h->max_packet_size = FFMIN(s->rtp_hd->max_packet_size,
  76. sizeof(s->encryptbuf)) - 14;
  77. h->is_streamed = 1;
  78. return 0;
  79. fail:
  80. srtp_close(h);
  81. return ret;
  82. }
  83. static int srtp_read(URLContext *h, uint8_t *buf, int size)
  84. {
  85. SRTPProtoContext *s = h->priv_data;
  86. int ret;
  87. start:
  88. ret = ffurl_read(s->rtp_hd, buf, size);
  89. if (ret > 0 && s->srtp_in.aes) {
  90. if (ff_srtp_decrypt(&s->srtp_in, buf, &ret) < 0)
  91. goto start;
  92. }
  93. return ret;
  94. }
  95. static int srtp_write(URLContext *h, const uint8_t *buf, int size)
  96. {
  97. SRTPProtoContext *s = h->priv_data;
  98. if (!s->srtp_out.aes)
  99. return ffurl_write(s->rtp_hd, buf, size);
  100. size = ff_srtp_encrypt(&s->srtp_out, buf, size, s->encryptbuf,
  101. sizeof(s->encryptbuf));
  102. if (size < 0)
  103. return size;
  104. return ffurl_write(s->rtp_hd, s->encryptbuf, size);
  105. }
  106. static int srtp_get_file_handle(URLContext *h)
  107. {
  108. SRTPProtoContext *s = h->priv_data;
  109. return ffurl_get_file_handle(s->rtp_hd);
  110. }
  111. static int srtp_get_multi_file_handle(URLContext *h, int **handles,
  112. int *numhandles)
  113. {
  114. SRTPProtoContext *s = h->priv_data;
  115. return ffurl_get_multi_file_handle(s->rtp_hd, handles, numhandles);
  116. }
  117. URLProtocol ff_srtp_protocol = {
  118. .name = "srtp",
  119. .url_open = srtp_open,
  120. .url_read = srtp_read,
  121. .url_write = srtp_write,
  122. .url_close = srtp_close,
  123. .url_get_file_handle = srtp_get_file_handle,
  124. .url_get_multi_file_handle = srtp_get_multi_file_handle,
  125. .priv_data_size = sizeof(SRTPProtoContext),
  126. .priv_data_class = &srtp_context_class,
  127. .flags = URL_PROTOCOL_FLAG_NETWORK,
  128. };