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.

324 lines
10KB

  1. /*
  2. * Advanced Message Queuing Protocol (AMQP) 0-9-1
  3. * Copyright (c) 2020 Andriy Gelman
  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 <amqp.h>
  22. #include <amqp_tcp_socket.h>
  23. #include <sys/time.h>
  24. #include "avformat.h"
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/time.h"
  28. #include "network.h"
  29. #include "url.h"
  30. #include "urldecode.h"
  31. typedef struct AMQPContext {
  32. const AVClass *class;
  33. amqp_connection_state_t conn;
  34. amqp_socket_t *socket;
  35. const char *exchange;
  36. const char *routing_key;
  37. int pkt_size;
  38. int64_t connection_timeout;
  39. int pkt_size_overflow;
  40. int delivery_mode;
  41. } AMQPContext;
  42. #define STR_LEN 1024
  43. #define DEFAULT_CHANNEL 1
  44. #define OFFSET(x) offsetof(AMQPContext, x)
  45. #define D AV_OPT_FLAG_DECODING_PARAM
  46. #define E AV_OPT_FLAG_ENCODING_PARAM
  47. static const AVOption options[] = {
  48. { "pkt_size", "Maximum send/read packet size", OFFSET(pkt_size), AV_OPT_TYPE_INT, { .i64 = 131072 }, 4096, INT_MAX, .flags = D | E },
  49. { "exchange", "Exchange to send/read packets", OFFSET(exchange), AV_OPT_TYPE_STRING, { .str = "amq.direct" }, 0, 0, .flags = D | E },
  50. { "routing_key", "Key to filter streams", OFFSET(routing_key), AV_OPT_TYPE_STRING, { .str = "amqp" }, 0, 0, .flags = D | E },
  51. { "connection_timeout", "Initial connection timeout", OFFSET(connection_timeout), AV_OPT_TYPE_DURATION, { .i64 = -1 }, -1, INT64_MAX, .flags = D | E},
  52. { "delivery_mode", "Delivery mode", OFFSET(delivery_mode), AV_OPT_TYPE_INT, { .i64 = AMQP_DELIVERY_PERSISTENT }, 1, 2, .flags = E, "delivery_mode"},
  53. { "persistent", "Persistent delivery mode", 0, AV_OPT_TYPE_CONST, { .i64 = AMQP_DELIVERY_PERSISTENT }, 0, 0, E, "delivery_mode" },
  54. { "non-persistent", "Non-persistent delivery mode", 0, AV_OPT_TYPE_CONST, { .i64 = AMQP_DELIVERY_NONPERSISTENT }, 0, 0, E, "delivery_mode" },
  55. { NULL }
  56. };
  57. static int amqp_proto_open(URLContext *h, const char *uri, int flags)
  58. {
  59. int ret, server_msg;
  60. char hostname[STR_LEN], credentials[STR_LEN], path[STR_LEN];
  61. int port;
  62. const char *user, *password = NULL, *vhost;
  63. const char *user_decoded, *password_decoded, *vhost_decoded;
  64. char *p;
  65. amqp_rpc_reply_t broker_reply;
  66. struct timeval tval = { 0 };
  67. AMQPContext *s = h->priv_data;
  68. h->is_streamed = 1;
  69. h->max_packet_size = s->pkt_size;
  70. av_url_split(NULL, 0, credentials, sizeof(credentials),
  71. hostname, sizeof(hostname), &port, path, sizeof(path), uri);
  72. if (port < 0)
  73. port = 5672;
  74. if (hostname[0] == '\0' || port <= 0 || port > 65535 ) {
  75. av_log(h, AV_LOG_ERROR, "Invalid hostname/port\n");
  76. return AVERROR(EINVAL);
  77. }
  78. p = strchr(credentials, ':');
  79. if (p) {
  80. *p = '\0';
  81. password = p + 1;
  82. }
  83. if (!password || *password == '\0')
  84. password = "guest";
  85. password_decoded = ff_urldecode(password, 0);
  86. if (!password_decoded)
  87. return AVERROR(ENOMEM);
  88. user = credentials;
  89. if (*user == '\0')
  90. user = "guest";
  91. user_decoded = ff_urldecode(user, 0);
  92. if (!user_decoded) {
  93. av_freep(&password_decoded);
  94. return AVERROR(ENOMEM);
  95. }
  96. /* skip query for now */
  97. p = strchr(path, '?');
  98. if (p)
  99. *p = '\0';
  100. vhost = path;
  101. if (*vhost == '\0')
  102. vhost = "/";
  103. else
  104. vhost++; /* skip leading '/' */
  105. vhost_decoded = ff_urldecode(vhost, 0);
  106. if (!vhost_decoded) {
  107. av_freep(&user_decoded);
  108. av_freep(&password_decoded);
  109. return AVERROR(ENOMEM);
  110. }
  111. s->conn = amqp_new_connection();
  112. if (!s->conn) {
  113. av_freep(&vhost_decoded);
  114. av_freep(&user_decoded);
  115. av_freep(&password_decoded);
  116. av_log(h, AV_LOG_ERROR, "Error creating connection\n");
  117. return AVERROR_EXTERNAL;
  118. }
  119. s->socket = amqp_tcp_socket_new(s->conn);
  120. if (!s->socket) {
  121. av_log(h, AV_LOG_ERROR, "Error creating socket\n");
  122. goto destroy_connection;
  123. }
  124. if (s->connection_timeout < 0)
  125. s->connection_timeout = (h->rw_timeout > 0 ? h->rw_timeout : 5000000);
  126. tval.tv_sec = s->connection_timeout / 1000000;
  127. tval.tv_usec = s->connection_timeout % 1000000;
  128. ret = amqp_socket_open_noblock(s->socket, hostname, port, &tval);
  129. if (ret) {
  130. av_log(h, AV_LOG_ERROR, "Error connecting to server: %s\n",
  131. amqp_error_string2(ret));
  132. goto destroy_connection;
  133. }
  134. broker_reply = amqp_login(s->conn, vhost_decoded, 0, s->pkt_size, 0,
  135. AMQP_SASL_METHOD_PLAIN, user_decoded, password_decoded);
  136. if (broker_reply.reply_type != AMQP_RESPONSE_NORMAL) {
  137. av_log(h, AV_LOG_ERROR, "Error login\n");
  138. server_msg = AMQP_ACCESS_REFUSED;
  139. goto close_connection;
  140. }
  141. amqp_channel_open(s->conn, DEFAULT_CHANNEL);
  142. broker_reply = amqp_get_rpc_reply(s->conn);
  143. if (broker_reply.reply_type != AMQP_RESPONSE_NORMAL) {
  144. av_log(h, AV_LOG_ERROR, "Error set channel\n");
  145. server_msg = AMQP_CHANNEL_ERROR;
  146. goto close_connection;
  147. }
  148. if (h->flags & AVIO_FLAG_READ) {
  149. amqp_bytes_t queuename;
  150. char queuename_buff[STR_LEN];
  151. amqp_queue_declare_ok_t *r;
  152. r = amqp_queue_declare(s->conn, DEFAULT_CHANNEL, amqp_empty_bytes,
  153. 0, 0, 0, 1, amqp_empty_table);
  154. broker_reply = amqp_get_rpc_reply(s->conn);
  155. if (!r || broker_reply.reply_type != AMQP_RESPONSE_NORMAL) {
  156. av_log(h, AV_LOG_ERROR, "Error declare queue\n");
  157. server_msg = AMQP_RESOURCE_ERROR;
  158. goto close_channel;
  159. }
  160. /* store queuename */
  161. queuename.bytes = queuename_buff;
  162. queuename.len = FFMIN(r->queue.len, STR_LEN);
  163. memcpy(queuename.bytes, r->queue.bytes, queuename.len);
  164. amqp_queue_bind(s->conn, DEFAULT_CHANNEL, queuename,
  165. amqp_cstring_bytes(s->exchange),
  166. amqp_cstring_bytes(s->routing_key), amqp_empty_table);
  167. broker_reply = amqp_get_rpc_reply(s->conn);
  168. if (broker_reply.reply_type != AMQP_RESPONSE_NORMAL) {
  169. av_log(h, AV_LOG_ERROR, "Queue bind error\n");
  170. server_msg = AMQP_INTERNAL_ERROR;
  171. goto close_channel;
  172. }
  173. amqp_basic_consume(s->conn, DEFAULT_CHANNEL, queuename, amqp_empty_bytes,
  174. 0, 1, 0, amqp_empty_table);
  175. broker_reply = amqp_get_rpc_reply(s->conn);
  176. if (broker_reply.reply_type != AMQP_RESPONSE_NORMAL) {
  177. av_log(h, AV_LOG_ERROR, "Set consume error\n");
  178. server_msg = AMQP_INTERNAL_ERROR;
  179. goto close_channel;
  180. }
  181. }
  182. av_freep(&vhost_decoded);
  183. av_freep(&user_decoded);
  184. av_freep(&password_decoded);
  185. return 0;
  186. close_channel:
  187. amqp_channel_close(s->conn, DEFAULT_CHANNEL, server_msg);
  188. close_connection:
  189. amqp_connection_close(s->conn, server_msg);
  190. destroy_connection:
  191. amqp_destroy_connection(s->conn);
  192. av_freep(&vhost_decoded);
  193. av_freep(&user_decoded);
  194. av_freep(&password_decoded);
  195. return AVERROR_EXTERNAL;
  196. }
  197. static int amqp_proto_write(URLContext *h, const unsigned char *buf, int size)
  198. {
  199. int ret;
  200. AMQPContext *s = h->priv_data;
  201. int fd = amqp_socket_get_sockfd(s->socket);
  202. amqp_bytes_t message = { size, (void *)buf };
  203. amqp_basic_properties_t props;
  204. ret = ff_network_wait_fd_timeout(fd, 1, h->rw_timeout, &h->interrupt_callback);
  205. if (ret)
  206. return ret;
  207. props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG;
  208. props.content_type = amqp_cstring_bytes("octet/stream");
  209. props.delivery_mode = s->delivery_mode;
  210. ret = amqp_basic_publish(s->conn, DEFAULT_CHANNEL, amqp_cstring_bytes(s->exchange),
  211. amqp_cstring_bytes(s->routing_key), 0, 0,
  212. &props, message);
  213. if (ret) {
  214. av_log(h, AV_LOG_ERROR, "Error publish: %s\n", amqp_error_string2(ret));
  215. return AVERROR_EXTERNAL;
  216. }
  217. return size;
  218. }
  219. static int amqp_proto_read(URLContext *h, unsigned char *buf, int size)
  220. {
  221. AMQPContext *s = h->priv_data;
  222. int fd = amqp_socket_get_sockfd(s->socket);
  223. int ret;
  224. amqp_rpc_reply_t broker_reply;
  225. amqp_envelope_t envelope;
  226. ret = ff_network_wait_fd_timeout(fd, 0, h->rw_timeout, &h->interrupt_callback);
  227. if (ret)
  228. return ret;
  229. amqp_maybe_release_buffers(s->conn);
  230. broker_reply = amqp_consume_message(s->conn, &envelope, NULL, 0);
  231. if (broker_reply.reply_type != AMQP_RESPONSE_NORMAL)
  232. return AVERROR_EXTERNAL;
  233. if (envelope.message.body.len > size) {
  234. s->pkt_size_overflow = FFMAX(s->pkt_size_overflow, envelope.message.body.len);
  235. av_log(h, AV_LOG_WARNING, "Message exceeds space in the buffer. "
  236. "Message will be truncated. Setting -pkt_size %d "
  237. "may resolve this issue.\n", s->pkt_size_overflow);
  238. }
  239. size = FFMIN(size, envelope.message.body.len);
  240. memcpy(buf, envelope.message.body.bytes, size);
  241. amqp_destroy_envelope(&envelope);
  242. return size;
  243. }
  244. static int amqp_proto_close(URLContext *h)
  245. {
  246. AMQPContext *s = h->priv_data;
  247. amqp_channel_close(s->conn, DEFAULT_CHANNEL, AMQP_REPLY_SUCCESS);
  248. amqp_connection_close(s->conn, AMQP_REPLY_SUCCESS);
  249. amqp_destroy_connection(s->conn);
  250. return 0;
  251. }
  252. static const AVClass amqp_context_class = {
  253. .class_name = "amqp",
  254. .item_name = av_default_item_name,
  255. .option = options,
  256. .version = LIBAVUTIL_VERSION_INT,
  257. };
  258. const URLProtocol ff_libamqp_protocol = {
  259. .name = "amqp",
  260. .url_close = amqp_proto_close,
  261. .url_open = amqp_proto_open,
  262. .url_read = amqp_proto_read,
  263. .url_write = amqp_proto_write,
  264. .priv_data_size = sizeof(AMQPContext),
  265. .priv_data_class = &amqp_context_class,
  266. .flags = URL_PROTOCOL_FLAG_NETWORK,
  267. };