FFmpeg  4.4.9
f_zmq.c
Go to the documentation of this file.
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 /**
22  * @file
23  * receive commands through libzeromq and broker them to filters
24  */
25 
26 #include <zmq.h>
27 #include "libavutil/avstring.h"
28 #include "libavutil/bprint.h"
29 #include "libavutil/opt.h"
30 #include "avfilter.h"
31 #include "internal.h"
32 #include "audio.h"
33 #include "video.h"
34 
35 typedef struct ZMQContext {
36  const AVClass *class;
37  void *zmq;
38  void *responder;
39  char *bind_address;
41 } ZMQContext;
42 
43 #define OFFSET(x) offsetof(ZMQContext, x)
44 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
45 static const AVOption options[] = {
46  { "bind_address", "set bind address", OFFSET(bind_address), AV_OPT_TYPE_STRING, {.str = "tcp://*:5555"}, 0, 0, FLAGS },
47  { "b", "set bind address", OFFSET(bind_address), AV_OPT_TYPE_STRING, {.str = "tcp://*:5555"}, 0, 0, FLAGS },
48  { NULL }
49 };
50 
52 {
53  ZMQContext *zmq = ctx->priv;
54 
55  zmq->zmq = zmq_ctx_new();
56  if (!zmq->zmq) {
58  "Could not create ZMQ context: %s\n", zmq_strerror(errno));
59  return AVERROR_EXTERNAL;
60  }
61 
62  zmq->responder = zmq_socket(zmq->zmq, ZMQ_REP);
63  if (!zmq->responder) {
65  "Could not create ZMQ socket: %s\n", zmq_strerror(errno));
66  return AVERROR_EXTERNAL;
67  }
68 
69  if (zmq_bind(zmq->responder, zmq->bind_address) == -1) {
71  "Could not bind ZMQ socket to address '%s': %s\n",
72  zmq->bind_address, zmq_strerror(errno));
73  return AVERROR_EXTERNAL;
74  }
75 
76  zmq->command_count = -1;
77  return 0;
78 }
79 
81 {
82  ZMQContext *zmq = ctx->priv;
83 
84  zmq_close(zmq->responder);
85  zmq_ctx_destroy(zmq->zmq);
86 }
87 
88 typedef struct Command {
89  char *target, *command, *arg;
90 } Command;
91 
92 #define SPACES " \f\t\n\r"
93 
94 static int parse_command(Command *cmd, const char *command_str, void *log_ctx)
95 {
96  const char **buf = &command_str;
97 
98  cmd->target = av_get_token(buf, SPACES);
99  if (!cmd->target || !cmd->target[0]) {
100  av_log(log_ctx, AV_LOG_ERROR,
101  "No target specified in command '%s'\n", command_str);
102  return AVERROR(EINVAL);
103  }
104 
105  cmd->command = av_get_token(buf, SPACES);
106  if (!cmd->command || !cmd->command[0]) {
107  av_log(log_ctx, AV_LOG_ERROR,
108  "No command specified in command '%s'\n", command_str);
109  return AVERROR(EINVAL);
110  }
111 
112  cmd->arg = av_get_token(buf, SPACES);
113  return 0;
114 }
115 
116 static int recv_msg(AVFilterContext *ctx, char **buf)
117 {
118  ZMQContext *zmq = ctx->priv;
119  zmq_msg_t msg;
120  int ret = 0;
121 
122  if (zmq_msg_init(&msg) == -1) {
124  "Could not initialize receive message: %s\n", zmq_strerror(errno));
125  return AVERROR_EXTERNAL;
126  }
127 
128  if (zmq_msg_recv(&msg, zmq->responder, ZMQ_DONTWAIT) == -1) {
129  if (errno != EAGAIN)
131  "Could not receive message: %s\n", zmq_strerror(errno));
132  ret = AVERROR_EXTERNAL;
133  goto end;
134  }
135 
136  *buf = av_strndup(zmq_msg_data(&msg), zmq_msg_size(&msg));
137  if (!*buf)
138  ret = AVERROR(ENOMEM);
139 
140 end:
141  zmq_msg_close(&msg);
142  return ret;
143 }
144 
145 static int filter_frame(AVFilterLink *inlink, AVFrame *ref)
146 {
147  AVFilterContext *ctx = inlink->dst;
148  ZMQContext *zmq = ctx->priv;
149 
150  while (1) {
151  char cmd_buf[1024];
152  char *recv_buf = NULL, *send_buf = NULL;
153  Command cmd = {0};
154  int ret;
155 
156  /* receive command */
157  if (recv_msg(ctx, &recv_buf) < 0)
158  break;
159  zmq->command_count++;
160 
161  /* parse command */
162  ret = parse_command(&cmd, recv_buf, ctx);
163  if (ret < 0) {
164  av_log(ctx, AV_LOG_ERROR, "Could not parse command #%d\n", zmq->command_count);
165  cmd_buf[0] = 0;
166  } else {
167  /* process command */
169  "Processing command #%d target:%s command:%s arg:%s\n",
170  zmq->command_count, cmd.target, cmd.command, cmd.arg);
171  ret = avfilter_graph_send_command(inlink->graph,
172  cmd.target, cmd.command, cmd.arg,
173  cmd_buf, sizeof(cmd_buf),
175  }
176  send_buf = av_asprintf("%d %s%s%s",
177  -ret, av_err2str(ret), cmd_buf[0] ? "\n" : "", cmd_buf);
178  if (!send_buf) {
179  ret = AVERROR(ENOMEM);
180  goto end;
181  }
183  "Sending command reply for command #%d:\n%s\n",
184  zmq->command_count, send_buf);
185  if (zmq_send(zmq->responder, send_buf, strlen(send_buf), 0) == -1)
186  av_log(ctx, AV_LOG_ERROR, "Failed to send reply for command #%d: %s\n",
187  zmq->command_count, zmq_strerror(ret));
188 
189  end:
190  av_freep(&send_buf);
191  av_freep(&recv_buf);
192  av_freep(&cmd.target);
193  av_freep(&cmd.command);
194  av_freep(&cmd.arg);
195  }
196 
197  return ff_filter_frame(ctx->outputs[0], ref);
198 }
199 
200 #if CONFIG_ZMQ_FILTER
201 
202 #define zmq_options options
204 
205 static const AVFilterPad zmq_inputs[] = {
206  {
207  .name = "default",
208  .type = AVMEDIA_TYPE_VIDEO,
209  .filter_frame = filter_frame,
210  },
211  { NULL }
212 };
213 
214 static const AVFilterPad zmq_outputs[] = {
215  {
216  .name = "default",
217  .type = AVMEDIA_TYPE_VIDEO,
218  },
219  { NULL }
220 };
221 
223  .name = "zmq",
224  .description = NULL_IF_CONFIG_SMALL("Receive commands through ZMQ and broker them to filters."),
225  .init = init,
226  .uninit = uninit,
227  .priv_size = sizeof(ZMQContext),
228  .inputs = zmq_inputs,
229  .outputs = zmq_outputs,
230  .priv_class = &zmq_class,
231 };
232 
233 #endif
234 
235 #if CONFIG_AZMQ_FILTER
236 
237 #define azmq_options options
239 
240 static const AVFilterPad azmq_inputs[] = {
241  {
242  .name = "default",
243  .type = AVMEDIA_TYPE_AUDIO,
244  .filter_frame = filter_frame,
245  },
246  { NULL }
247 };
248 
249 static const AVFilterPad azmq_outputs[] = {
250  {
251  .name = "default",
252  .type = AVMEDIA_TYPE_AUDIO,
253  },
254  { NULL }
255 };
256 
258  .name = "azmq",
259  .description = NULL_IF_CONFIG_SMALL("Receive commands through ZMQ and broker them to filters."),
260  .init = init,
261  .uninit = uninit,
262  .priv_size = sizeof(ZMQContext),
263  .inputs = azmq_inputs,
264  .outputs = azmq_outputs,
265  .priv_class = &azmq_class,
266 };
267 
268 #endif
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
AVFilter ff_af_azmq
AVFilter ff_vf_zmq
#define av_cold
Definition: attributes.h:88
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1099
Main libavfilter public API header.
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:113
#define NULL
Definition: coverity.c:32
static void av_cold uninit(AVFilterContext *ctx)
Definition: f_zmq.c:80
#define SPACES
Definition: f_zmq.c:92
static const AVOption options[]
Definition: f_zmq.c:45
#define FLAGS
Definition: f_zmq.c:44
static int filter_frame(AVFilterLink *inlink, AVFrame *ref)
Definition: f_zmq.c:145
static av_cold int init(AVFilterContext *ctx)
Definition: f_zmq.c:51
static int recv_msg(AVFilterContext *ctx, char **buf)
Definition: f_zmq.c:116
#define OFFSET(x)
Definition: f_zmq.c:43
static int parse_command(Command *cmd, const char *command_str, void *log_ctx)
Definition: f_zmq.c:94
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
Send a command to one or more filter instances.
#define AVFILTER_CMD_FLAG_ONE
Stop once a filter understood the command (for target=all for example), fast filters are favored auto...
Definition: avfilter.h:706
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
char * av_strndup(const char *s, size_t len)
Duplicate a substring of a string.
Definition: mem.c:265
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
Definition: avstring.c:151
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:288
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AVOptions.
Describe the class of an AVClass context structure.
Definition: log.h:67
An instance of a filter.
Definition: avfilter.h:341
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
AVOption.
Definition: opt.h:248
char * command
Definition: f_sendcmd.c:83
char * target
Definition: f_sendcmd.c:83
char * arg
Definition: f_sendcmd.c:83
void * zmq
Definition: f_zmq.c:37
void * responder
Definition: f_zmq.c:38
int command_count
Definition: f_zmq.c:40
char * bind_address
Definition: f_zmq.c:39
#define av_freep(p)
#define av_log(a,...)
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
AVFormatContext * ctx
Definition: movenc.c:48