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.

147 lines
4.8KB

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