f_zmq.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (c) 2013 Stefano Sabatini
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * receive commands through libzeromq and broker them to filters
  23. */
  24. #include <zmq.h>
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/bprint.h"
  27. #include "libavutil/opt.h"
  28. #include "avfilter.h"
  29. #include "internal.h"
  30. #include "avfiltergraph.h"
  31. #include "audio.h"
  32. #include "video.h"
  33. typedef struct {
  34. const AVClass *class;
  35. void *zmq;
  36. void *responder;
  37. char *bind_address;
  38. int command_count;
  39. } ZMQContext;
  40. #define OFFSET(x) offsetof(ZMQContext, x)
  41. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
  42. static const AVOption options[] = {
  43. { "bind_address", "set bind address", OFFSET(bind_address), AV_OPT_TYPE_STRING, {.str = "tcp://*:5555"}, 0, 0, FLAGS },
  44. { "b", "set bind address", OFFSET(bind_address), AV_OPT_TYPE_STRING, {.str = "tcp://*:5555"}, 0, 0, FLAGS },
  45. { NULL }
  46. };
  47. static av_cold int init(AVFilterContext *ctx)
  48. {
  49. ZMQContext *zmq = ctx->priv;
  50. zmq->zmq = zmq_ctx_new();
  51. if (!zmq->zmq) {
  52. av_log(ctx, AV_LOG_ERROR,
  53. "Could not create ZMQ context: %s\n", zmq_strerror(errno));
  54. return AVERROR_EXTERNAL;
  55. }
  56. zmq->responder = zmq_socket(zmq->zmq, ZMQ_REP);
  57. if (!zmq->responder) {
  58. av_log(ctx, AV_LOG_ERROR,
  59. "Could not create ZMQ socket: %s\n", zmq_strerror(errno));
  60. return AVERROR_EXTERNAL;
  61. }
  62. if (zmq_bind(zmq->responder, zmq->bind_address) == -1) {
  63. av_log(ctx, AV_LOG_ERROR,
  64. "Could not bind ZMQ socket to address '%s': %s\n",
  65. zmq->bind_address, zmq_strerror(errno));
  66. return AVERROR_EXTERNAL;
  67. }
  68. zmq->command_count = -1;
  69. return 0;
  70. }
  71. static void av_cold uninit(AVFilterContext *ctx)
  72. {
  73. ZMQContext *zmq = ctx->priv;
  74. zmq_close(zmq->responder);
  75. zmq_ctx_destroy(zmq->zmq);
  76. }
  77. typedef struct {
  78. char *target, *command, *arg;
  79. } Command;
  80. #define SPACES " \f\t\n\r"
  81. static int parse_command(Command *cmd, const char *command_str, void *log_ctx)
  82. {
  83. const char **buf = &command_str;
  84. cmd->target = av_get_token(buf, SPACES);
  85. if (!cmd->target || !cmd->target[0]) {
  86. av_log(log_ctx, AV_LOG_ERROR,
  87. "No target specified in command '%s'\n", command_str);
  88. return AVERROR(EINVAL);
  89. }
  90. cmd->command = av_get_token(buf, SPACES);
  91. if (!cmd->command || !cmd->command[0]) {
  92. av_log(log_ctx, AV_LOG_ERROR,
  93. "No command specified in command '%s'\n", command_str);
  94. return AVERROR(EINVAL);
  95. }
  96. cmd->arg = av_get_token(buf, SPACES);
  97. return 0;
  98. }
  99. static int recv_msg(AVFilterContext *ctx, char **buf, int *buf_size)
  100. {
  101. ZMQContext *zmq = ctx->priv;
  102. zmq_msg_t msg;
  103. int ret = 0;
  104. if (zmq_msg_init(&msg) == -1) {
  105. av_log(ctx, AV_LOG_WARNING,
  106. "Could not initialize receive message: %s\n", zmq_strerror(errno));
  107. return AVERROR_EXTERNAL;
  108. }
  109. if (zmq_msg_recv(&msg, zmq->responder, ZMQ_DONTWAIT) == -1) {
  110. if (errno != EAGAIN)
  111. av_log(ctx, AV_LOG_WARNING,
  112. "Could not receive message: %s\n", zmq_strerror(errno));
  113. ret = AVERROR_EXTERNAL;
  114. goto end;
  115. }
  116. *buf_size = zmq_msg_size(&msg) + 1;
  117. *buf = av_malloc(*buf_size);
  118. if (!*buf) {
  119. ret = AVERROR(ENOMEM);
  120. goto end;
  121. }
  122. memcpy(*buf, zmq_msg_data(&msg), *buf_size);
  123. (*buf)[*buf_size-1] = 0;
  124. end:
  125. zmq_msg_close(&msg);
  126. return ret;
  127. }
  128. static int filter_frame(AVFilterLink *inlink, AVFrame *ref)
  129. {
  130. AVFilterContext *ctx = inlink->dst;
  131. ZMQContext *zmq = ctx->priv;
  132. while (1) {
  133. char cmd_buf[1024];
  134. char *recv_buf, *send_buf;
  135. int recv_buf_size;
  136. Command cmd = {0};
  137. int ret;
  138. /* receive command */
  139. if (recv_msg(ctx, &recv_buf, &recv_buf_size) < 0)
  140. break;
  141. zmq->command_count++;
  142. /* parse command */
  143. if (parse_command(&cmd, recv_buf, ctx) < 0) {
  144. av_log(ctx, AV_LOG_ERROR, "Could not parse command #%d\n", zmq->command_count);
  145. goto end;
  146. }
  147. /* process command */
  148. av_log(ctx, AV_LOG_VERBOSE,
  149. "Processing command #%d target:%s command:%s arg:%s\n",
  150. zmq->command_count, cmd.target, cmd.command, cmd.arg);
  151. ret = avfilter_graph_send_command(inlink->graph,
  152. cmd.target, cmd.command, cmd.arg,
  153. cmd_buf, sizeof(cmd_buf),
  154. AVFILTER_CMD_FLAG_ONE);
  155. send_buf = av_asprintf("%d %s%s%s",
  156. -ret, av_err2str(ret), cmd_buf[0] ? "\n" : "", cmd_buf);
  157. if (!send_buf) {
  158. ret = AVERROR(ENOMEM);
  159. goto end;
  160. }
  161. av_log(ctx, AV_LOG_VERBOSE,
  162. "Sending command reply for command #%d:\n%s\n",
  163. zmq->command_count, send_buf);
  164. if (zmq_send(zmq->responder, send_buf, strlen(send_buf), 0) == -1)
  165. av_log(ctx, AV_LOG_ERROR, "Failed to send reply for command #%d: %s\n",
  166. zmq->command_count, zmq_strerror(ret));
  167. end:
  168. av_freep(&send_buf);
  169. av_freep(&recv_buf);
  170. recv_buf_size = 0;
  171. av_freep(&cmd.target);
  172. av_freep(&cmd.command);
  173. av_freep(&cmd.arg);
  174. }
  175. return ff_filter_frame(ctx->outputs[0], ref);
  176. }
  177. #if CONFIG_ZMQ_FILTER
  178. #define zmq_options options
  179. AVFILTER_DEFINE_CLASS(zmq);
  180. static const AVFilterPad zmq_inputs[] = {
  181. {
  182. .name = "default",
  183. .type = AVMEDIA_TYPE_VIDEO,
  184. .filter_frame = filter_frame,
  185. },
  186. { NULL }
  187. };
  188. static const AVFilterPad zmq_outputs[] = {
  189. {
  190. .name = "default",
  191. .type = AVMEDIA_TYPE_VIDEO,
  192. },
  193. { NULL }
  194. };
  195. AVFilter ff_vf_zmq = {
  196. .name = "zmq",
  197. .description = NULL_IF_CONFIG_SMALL("Receive commands through ZMQ and broker them to filters."),
  198. .init = init,
  199. .uninit = uninit,
  200. .priv_size = sizeof(ZMQContext),
  201. .inputs = zmq_inputs,
  202. .outputs = zmq_outputs,
  203. .priv_class = &zmq_class,
  204. };
  205. #endif
  206. #if CONFIG_AZMQ_FILTER
  207. #define azmq_options options
  208. AVFILTER_DEFINE_CLASS(azmq);
  209. static const AVFilterPad azmq_inputs[] = {
  210. {
  211. .name = "default",
  212. .type = AVMEDIA_TYPE_AUDIO,
  213. .filter_frame = filter_frame,
  214. },
  215. { NULL }
  216. };
  217. static const AVFilterPad azmq_outputs[] = {
  218. {
  219. .name = "default",
  220. .type = AVMEDIA_TYPE_AUDIO,
  221. },
  222. { NULL }
  223. };
  224. AVFilter ff_af_azmq = {
  225. .name = "azmq",
  226. .description = NULL_IF_CONFIG_SMALL("Receive commands through ZMQ and broker them to filters."),
  227. .init = init,
  228. .uninit = uninit,
  229. .priv_size = sizeof(ZMQContext),
  230. .inputs = azmq_inputs,
  231. .outputs = azmq_outputs,
  232. .priv_class = &azmq_class,
  233. };
  234. #endif