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.

246 lines
7.5KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * Reliable Internet Streaming Transport protocol
  21. */
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/parseutils.h"
  25. #include "libavutil/time.h"
  26. #include "avformat.h"
  27. #include "internal.h"
  28. #include "network.h"
  29. #include "os_support.h"
  30. #include "url.h"
  31. #include <librist/librist.h>
  32. // RIST_MAX_PACKET_SIZE - 28 minimum protocol overhead
  33. #define MAX_PAYLOAD_SIZE (10000-28)
  34. typedef struct RISTContext {
  35. const AVClass *class;
  36. int profile;
  37. int buffer_size;
  38. int packet_size;
  39. int log_level;
  40. int encryption;
  41. char *secret;
  42. struct rist_logging_settings logging_settings;
  43. struct rist_peer_config peer_config;
  44. struct rist_peer *peer;
  45. struct rist_ctx *ctx;
  46. } RISTContext;
  47. #define D AV_OPT_FLAG_DECODING_PARAM
  48. #define E AV_OPT_FLAG_ENCODING_PARAM
  49. #define OFFSET(x) offsetof(RISTContext, x)
  50. static const AVOption librist_options[] = {
  51. { "rist_profile","set profile", OFFSET(profile), AV_OPT_TYPE_INT, {.i64=RIST_PROFILE_MAIN}, 0, 2, .flags = D|E, "profile" },
  52. { "simple", NULL, 0, AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_SIMPLE}, 0, 0, .flags = D|E, "profile" },
  53. { "main", NULL, 0, AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_MAIN}, 0, 0, .flags = D|E, "profile" },
  54. { "advanced", NULL, 0, AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_ADVANCED}, 0, 0, .flags = D|E, "profile" },
  55. { "buffer_size", "set buffer_size", OFFSET(buffer_size), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, .flags = D|E },
  56. { "pkt_size", "set packet size", OFFSET(packet_size), AV_OPT_TYPE_INT, {.i64=1316}, 1, MAX_PAYLOAD_SIZE, .flags = D|E },
  57. { "log_level", "set loglevel", OFFSET(log_level), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = D|E },
  58. { "secret", "set encryption secret",OFFSET(secret), AV_OPT_TYPE_STRING,{.str=NULL}, 0, 0, .flags = D|E },
  59. { "encryption","set encryption type",OFFSET(encryption), AV_OPT_TYPE_INT ,{.i64=0}, 0, INT_MAX, .flags = D|E },
  60. { NULL }
  61. };
  62. static int risterr2ret(int err)
  63. {
  64. switch (err) {
  65. case RIST_ERR_MALLOC:
  66. return AVERROR(ENOMEM);
  67. default:
  68. return AVERROR_EXTERNAL;
  69. }
  70. }
  71. static int log_cb(void *arg, enum rist_log_level log_level, const char *msg)
  72. {
  73. int level;
  74. switch (log_level) {
  75. case RIST_LOG_ERROR: level = AV_LOG_ERROR; break;
  76. case RIST_LOG_WARN: level = AV_LOG_WARNING; break;
  77. case RIST_LOG_NOTICE: level = AV_LOG_VERBOSE; break;
  78. case RIST_LOG_INFO: level = AV_LOG_INFO; break;
  79. case RIST_LOG_DEBUG: level = AV_LOG_DEBUG; break;
  80. case RIST_LOG_DISABLE: level = AV_LOG_QUIET; break;
  81. case RIST_LOG_SIMULATE: level = AV_LOG_TRACE; break;
  82. default: level = AV_LOG_PANIC;
  83. }
  84. av_log(arg, level, "%s", msg);
  85. return 0;
  86. }
  87. static int librist_close(URLContext *h)
  88. {
  89. RISTContext *s = h->priv_data;
  90. int ret = 0;
  91. s->peer = NULL;
  92. if (s->ctx)
  93. ret = rist_destroy(s->ctx);
  94. s->ctx = NULL;
  95. return risterr2ret(ret);
  96. }
  97. static int librist_open(URLContext *h, const char *uri, int flags)
  98. {
  99. RISTContext *s = h->priv_data;
  100. struct rist_logging_settings *logging_settings = &s->logging_settings;
  101. struct rist_peer_config *peer_config = &s->peer_config;
  102. int ret;
  103. if ((flags & AVIO_FLAG_READ_WRITE) == AVIO_FLAG_READ_WRITE)
  104. return AVERROR(EINVAL);
  105. ret = rist_logging_set(&logging_settings, s->log_level, log_cb, h, NULL, NULL);
  106. if (ret < 0)
  107. return risterr2ret(ret);
  108. if (flags & AVIO_FLAG_WRITE) {
  109. h->max_packet_size = s->packet_size;
  110. ret = rist_sender_create(&s->ctx, s->profile, 0, logging_settings);
  111. }
  112. if (ret < 0)
  113. goto err;
  114. if (flags & AVIO_FLAG_READ) {
  115. h->max_packet_size = MAX_PAYLOAD_SIZE;
  116. ret = rist_receiver_create(&s->ctx, s->profile, logging_settings);
  117. }
  118. if (ret < 0)
  119. goto err;
  120. ret = rist_peer_config_defaults_set(peer_config);
  121. if (ret < 0)
  122. goto err;
  123. ret = rist_parse_address(uri, (const struct rist_peer_config **)&peer_config);
  124. if (ret < 0)
  125. goto err;
  126. if (((s->encryption == 128 || s->encryption == 256) && !s->secret) ||
  127. ((peer_config->key_size == 128 || peer_config->key_size == 256) && !peer_config->secret[0])) {
  128. av_log(h, AV_LOG_ERROR, "secret is mandatory if encryption is enabled\n");
  129. librist_close(h);
  130. return AVERROR(EINVAL);
  131. }
  132. if (s->secret && peer_config->secret[0] == 0)
  133. av_strlcpy(peer_config->secret, s->secret, FFMIN(RIST_MAX_STRING_SHORT - 1, strlen(s->secret)));
  134. if (s->secret && (s->encryption == 128 || s->encryption == 256))
  135. peer_config->key_size = s->encryption;
  136. if (s->buffer_size) {
  137. peer_config->recovery_length_min = s->buffer_size;
  138. peer_config->recovery_length_max = s->buffer_size;
  139. }
  140. ret = rist_peer_create(s->ctx, &s->peer, &s->peer_config);
  141. if (ret < 0)
  142. goto err;
  143. ret = rist_start(s->ctx);
  144. if (ret < 0)
  145. goto err;
  146. return 0;
  147. err:
  148. librist_close(h);
  149. return risterr2ret(ret);
  150. }
  151. static int librist_read(URLContext *h, uint8_t *buf, int size)
  152. {
  153. RISTContext *s = h->priv_data;
  154. const struct rist_data_block *data_block;
  155. int ret;
  156. ret = rist_receiver_data_read(s->ctx, &data_block, POLLING_TIME);
  157. if (ret < 0)
  158. return risterr2ret(ret);
  159. if (ret == 0)
  160. return AVERROR(EAGAIN);
  161. if (data_block->payload_len > MAX_PAYLOAD_SIZE) {
  162. rist_receiver_data_block_free((struct rist_data_block**)&data_block);
  163. return AVERROR_EXTERNAL;
  164. }
  165. size = data_block->payload_len;
  166. memcpy(buf, data_block->payload, size);
  167. rist_receiver_data_block_free((struct rist_data_block**)&data_block);
  168. return size;
  169. }
  170. static int librist_write(URLContext *h, const uint8_t *buf, int size)
  171. {
  172. RISTContext *s = h->priv_data;
  173. struct rist_data_block data_block = { 0 };
  174. int ret;
  175. data_block.ts_ntp = 0;
  176. data_block.payload = buf;
  177. data_block.payload_len = size;
  178. ret = rist_sender_data_write(s->ctx, &data_block);
  179. if (ret < 0)
  180. return risterr2ret(ret);
  181. return ret;
  182. }
  183. static const AVClass librist_class = {
  184. .class_name = "librist",
  185. .item_name = av_default_item_name,
  186. .option = librist_options,
  187. .version = LIBAVUTIL_VERSION_INT,
  188. };
  189. const URLProtocol ff_librist_protocol = {
  190. .name = "rist",
  191. .url_open = librist_open,
  192. .url_read = librist_read,
  193. .url_write = librist_write,
  194. .url_close = librist_close,
  195. .priv_data_size = sizeof(RISTContext),
  196. .flags = URL_PROTOCOL_FLAG_NETWORK,
  197. .priv_data_class = &librist_class,
  198. };