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"
32 #include "avfiltergraph.h"
44 #define OFFSET(x) offsetof(ZMQContext, x)
45 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
46 static const AVOption options[] = {
47 { "bind_address", "set bind address", OFFSET(bind_address), AV_OPT_TYPE_STRING, {.str = "tcp://*:5555"}, 0, 0, FLAGS },
48 { "b", "set bind address", OFFSET(bind_address), AV_OPT_TYPE_STRING, {.str = "tcp://*:5555"}, 0, 0, FLAGS },
52 static av_cold int init(AVFilterContext *ctx)
54 ZMQContext *zmq = ctx->priv;
56 zmq->zmq = zmq_ctx_new();
58 av_log(ctx, AV_LOG_ERROR,
59 "Could not create ZMQ context: %s\n", zmq_strerror(errno));
60 return AVERROR_EXTERNAL;
63 zmq->responder = zmq_socket(zmq->zmq, ZMQ_REP);
64 if (!zmq->responder) {
65 av_log(ctx, AV_LOG_ERROR,
66 "Could not create ZMQ socket: %s\n", zmq_strerror(errno));
67 return AVERROR_EXTERNAL;
70 if (zmq_bind(zmq->responder, zmq->bind_address) == -1) {
71 av_log(ctx, AV_LOG_ERROR,
72 "Could not bind ZMQ socket to address '%s': %s\n",
73 zmq->bind_address, zmq_strerror(errno));
74 return AVERROR_EXTERNAL;
77 zmq->command_count = -1;
81 static void av_cold uninit(AVFilterContext *ctx)
83 ZMQContext *zmq = ctx->priv;
85 zmq_close(zmq->responder);
86 zmq_ctx_destroy(zmq->zmq);
90 char *target, *command, *arg;
93 #define SPACES " \f\t\n\r"
95 static int parse_command(Command *cmd, const char *command_str, void *log_ctx)
97 const char **buf = &command_str;
99 cmd->target = av_get_token(buf, SPACES);
100 if (!cmd->target || !cmd->target[0]) {
101 av_log(log_ctx, AV_LOG_ERROR,
102 "No target specified in command '%s'\n", command_str);
103 return AVERROR(EINVAL);
106 cmd->command = av_get_token(buf, SPACES);
107 if (!cmd->command || !cmd->command[0]) {
108 av_log(log_ctx, AV_LOG_ERROR,
109 "No command specified in command '%s'\n", command_str);
110 return AVERROR(EINVAL);
113 cmd->arg = av_get_token(buf, SPACES);
117 static int recv_msg(AVFilterContext *ctx, char **buf, int *buf_size)
119 ZMQContext *zmq = ctx->priv;
123 if (zmq_msg_init(&msg) == -1) {
124 av_log(ctx, AV_LOG_WARNING,
125 "Could not initialize receive message: %s\n", zmq_strerror(errno));
126 return AVERROR_EXTERNAL;
129 if (zmq_msg_recv(&msg, zmq->responder, ZMQ_DONTWAIT) == -1) {
131 av_log(ctx, AV_LOG_WARNING,
132 "Could not receive message: %s\n", zmq_strerror(errno));
133 ret = AVERROR_EXTERNAL;
137 *buf_size = zmq_msg_size(&msg) + 1;
138 *buf = av_malloc(*buf_size);
140 ret = AVERROR(ENOMEM);
143 memcpy(*buf, zmq_msg_data(&msg), *buf_size);
144 (*buf)[*buf_size-1] = 0;
151 static int filter_frame(AVFilterLink *inlink, AVFrame *ref)
153 AVFilterContext *ctx = inlink->dst;
154 ZMQContext *zmq = ctx->priv;
158 char *recv_buf, *send_buf;
163 /* receive command */
164 if (recv_msg(ctx, &recv_buf, &recv_buf_size) < 0)
166 zmq->command_count++;
169 if (parse_command(&cmd, recv_buf, ctx) < 0) {
170 av_log(ctx, AV_LOG_ERROR, "Could not parse command #%d\n", zmq->command_count);
174 /* process command */
175 av_log(ctx, AV_LOG_VERBOSE,
176 "Processing command #%d target:%s command:%s arg:%s\n",
177 zmq->command_count, cmd.target, cmd.command, cmd.arg);
178 ret = avfilter_graph_send_command(inlink->graph,
179 cmd.target, cmd.command, cmd.arg,
180 cmd_buf, sizeof(cmd_buf),
181 AVFILTER_CMD_FLAG_ONE);
182 send_buf = av_asprintf("%d %s%s%s",
183 -ret, av_err2str(ret), cmd_buf[0] ? "\n" : "", cmd_buf);
185 ret = AVERROR(ENOMEM);
188 av_log(ctx, AV_LOG_VERBOSE,
189 "Sending command reply for command #%d:\n%s\n",
190 zmq->command_count, send_buf);
191 if (zmq_send(zmq->responder, send_buf, strlen(send_buf), 0) == -1)
192 av_log(ctx, AV_LOG_ERROR, "Failed to send reply for command #%d: %s\n",
193 zmq->command_count, zmq_strerror(ret));
199 av_freep(&cmd.target);
200 av_freep(&cmd.command);
204 return ff_filter_frame(ctx->outputs[0], ref);
207 #if CONFIG_ZMQ_FILTER
209 #define zmq_options options
210 AVFILTER_DEFINE_CLASS(zmq);
212 static const AVFilterPad zmq_inputs[] = {
215 .type = AVMEDIA_TYPE_VIDEO,
216 .filter_frame = filter_frame,
221 static const AVFilterPad zmq_outputs[] = {
224 .type = AVMEDIA_TYPE_VIDEO,
229 AVFilter ff_vf_zmq = {
231 .description = NULL_IF_CONFIG_SMALL("Receive commands through ZMQ and broker them to filters."),
234 .priv_size = sizeof(ZMQContext),
235 .inputs = zmq_inputs,
236 .outputs = zmq_outputs,
237 .priv_class = &zmq_class,
242 #if CONFIG_AZMQ_FILTER
244 #define azmq_options options
245 AVFILTER_DEFINE_CLASS(azmq);
247 static const AVFilterPad azmq_inputs[] = {
250 .type = AVMEDIA_TYPE_AUDIO,
251 .filter_frame = filter_frame,
256 static const AVFilterPad azmq_outputs[] = {
259 .type = AVMEDIA_TYPE_AUDIO,
264 AVFilter ff_af_azmq = {
266 .description = NULL_IF_CONFIG_SMALL("Receive commands through ZMQ and broker them to filters."),
269 .priv_size = sizeof(ZMQContext),
270 .inputs = azmq_inputs,
271 .outputs = azmq_outputs,
272 .priv_class = &azmq_class,