2 * Copyright (c) 2013 Stefano Sabatini
4 * This file is part of FFmpeg.
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.
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.
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
23 * receive commands through libzeromq and broker them to filters
27 #include "libavutil/avstring.h"
28 #include "libavutil/bprint.h"
29 #include "libavutil/opt.h"
35 typedef struct ZMQContext {
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 },
51 static av_cold int init(AVFilterContext *ctx)
53 ZMQContext *zmq = ctx->priv;
55 zmq->zmq = zmq_ctx_new();
57 av_log(ctx, AV_LOG_ERROR,
58 "Could not create ZMQ context: %s\n", zmq_strerror(errno));
59 return AVERROR_EXTERNAL;
62 zmq->responder = zmq_socket(zmq->zmq, ZMQ_REP);
63 if (!zmq->responder) {
64 av_log(ctx, AV_LOG_ERROR,
65 "Could not create ZMQ socket: %s\n", zmq_strerror(errno));
66 return AVERROR_EXTERNAL;
69 if (zmq_bind(zmq->responder, zmq->bind_address) == -1) {
70 av_log(ctx, AV_LOG_ERROR,
71 "Could not bind ZMQ socket to address '%s': %s\n",
72 zmq->bind_address, zmq_strerror(errno));
73 return AVERROR_EXTERNAL;
76 zmq->command_count = -1;
80 static void av_cold uninit(AVFilterContext *ctx)
82 ZMQContext *zmq = ctx->priv;
84 zmq_close(zmq->responder);
85 zmq_ctx_destroy(zmq->zmq);
88 typedef struct Command {
89 char *target, *command, *arg;
92 #define SPACES " \f\t\n\r"
94 static int parse_command(Command *cmd, const char *command_str, void *log_ctx)
96 const char **buf = &command_str;
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);
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);
112 cmd->arg = av_get_token(buf, SPACES);
116 static int recv_msg(AVFilterContext *ctx, char **buf, int *buf_size)
118 ZMQContext *zmq = ctx->priv;
122 if (zmq_msg_init(&msg) == -1) {
123 av_log(ctx, AV_LOG_WARNING,
124 "Could not initialize receive message: %s\n", zmq_strerror(errno));
125 return AVERROR_EXTERNAL;
128 if (zmq_msg_recv(&msg, zmq->responder, ZMQ_DONTWAIT) == -1) {
130 av_log(ctx, AV_LOG_WARNING,
131 "Could not receive message: %s\n", zmq_strerror(errno));
132 ret = AVERROR_EXTERNAL;
136 *buf_size = zmq_msg_size(&msg) + 1;
137 *buf = av_malloc(*buf_size);
139 ret = AVERROR(ENOMEM);
142 memcpy(*buf, zmq_msg_data(&msg), *buf_size - 1);
143 (*buf)[*buf_size-1] = 0;
150 static int filter_frame(AVFilterLink *inlink, AVFrame *ref)
152 AVFilterContext *ctx = inlink->dst;
153 ZMQContext *zmq = ctx->priv;
157 char *recv_buf, *send_buf;
162 /* receive command */
163 if (recv_msg(ctx, &recv_buf, &recv_buf_size) < 0)
165 zmq->command_count++;
168 if (parse_command(&cmd, recv_buf, ctx) < 0) {
169 av_log(ctx, AV_LOG_ERROR, "Could not parse command #%d\n", zmq->command_count);
173 /* process command */
174 av_log(ctx, AV_LOG_VERBOSE,
175 "Processing command #%d target:%s command:%s arg:%s\n",
176 zmq->command_count, cmd.target, cmd.command, cmd.arg);
177 ret = avfilter_graph_send_command(inlink->graph,
178 cmd.target, cmd.command, cmd.arg,
179 cmd_buf, sizeof(cmd_buf),
180 AVFILTER_CMD_FLAG_ONE);
181 send_buf = av_asprintf("%d %s%s%s",
182 -ret, av_err2str(ret), cmd_buf[0] ? "\n" : "", cmd_buf);
184 ret = AVERROR(ENOMEM);
187 av_log(ctx, AV_LOG_VERBOSE,
188 "Sending command reply for command #%d:\n%s\n",
189 zmq->command_count, send_buf);
190 if (zmq_send(zmq->responder, send_buf, strlen(send_buf), 0) == -1)
191 av_log(ctx, AV_LOG_ERROR, "Failed to send reply for command #%d: %s\n",
192 zmq->command_count, zmq_strerror(ret));
198 av_freep(&cmd.target);
199 av_freep(&cmd.command);
203 return ff_filter_frame(ctx->outputs[0], ref);
206 #if CONFIG_ZMQ_FILTER
208 #define zmq_options options
209 AVFILTER_DEFINE_CLASS(zmq);
211 static const AVFilterPad zmq_inputs[] = {
214 .type = AVMEDIA_TYPE_VIDEO,
215 .filter_frame = filter_frame,
220 static const AVFilterPad zmq_outputs[] = {
223 .type = AVMEDIA_TYPE_VIDEO,
228 AVFilter ff_vf_zmq = {
230 .description = NULL_IF_CONFIG_SMALL("Receive commands through ZMQ and broker them to filters."),
233 .priv_size = sizeof(ZMQContext),
234 .inputs = zmq_inputs,
235 .outputs = zmq_outputs,
236 .priv_class = &zmq_class,
241 #if CONFIG_AZMQ_FILTER
243 #define azmq_options options
244 AVFILTER_DEFINE_CLASS(azmq);
246 static const AVFilterPad azmq_inputs[] = {
249 .type = AVMEDIA_TYPE_AUDIO,
250 .filter_frame = filter_frame,
255 static const AVFilterPad azmq_outputs[] = {
258 .type = AVMEDIA_TYPE_AUDIO,
263 AVFilter ff_af_azmq = {
265 .description = NULL_IF_CONFIG_SMALL("Receive commands through ZMQ and broker them to filters."),
268 .priv_size = sizeof(ZMQContext),
269 .inputs = azmq_inputs,
270 .outputs = azmq_outputs,
271 .priv_class = &azmq_class,