3 * Copyright (c) 2019 Andriy Gelman
5 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavutil/avstring.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/time.h"
29 #define ZMQ_STRERROR zmq_strerror(zmq_errno())
31 typedef struct ZMQContext {
36 int pkt_size_overflow; /*keep track of the largest packet during overflow*/
39 #define OFFSET(x) offsetof(ZMQContext, x)
40 #define D AV_OPT_FLAG_DECODING_PARAM
41 #define E AV_OPT_FLAG_ENCODING_PARAM
42 static const AVOption options[] = {
43 { "pkt_size", "Maximum send/read packet size", OFFSET(pkt_size), AV_OPT_TYPE_INT, { .i64 = 32768 }, -1, INT_MAX, .flags = D | E },
47 static int zmq_proto_wait(URLContext *h, void *socket, int write)
50 int ev = write ? ZMQ_POLLOUT : ZMQ_POLLIN;
51 zmq_pollitem_t items = { .socket = socket, .fd = 0, .events = ev, .revents = 0 };
52 ret = zmq_poll(&items, 1, POLLING_TIME);
54 av_log(h, AV_LOG_ERROR, "Error occured during zmq_poll(): %s\n", ZMQ_STRERROR);
55 return AVERROR_EXTERNAL;
57 return items.revents & ev ? 0 : AVERROR(EAGAIN);
60 static int zmq_proto_wait_timeout(URLContext *h, void *socket, int write, int64_t timeout, AVIOInterruptCB *int_cb)
63 int64_t wait_start = 0;
66 if (ff_check_interrupt(int_cb))
68 ret = zmq_proto_wait(h, socket, write);
69 if (ret != AVERROR(EAGAIN))
73 wait_start = av_gettime_relative();
74 else if (av_gettime_relative() - wait_start > timeout)
75 return AVERROR(ETIMEDOUT);
80 static int zmq_proto_open(URLContext *h, const char *uri, int flags)
83 ZMQContext *s = h->priv_data;
84 s->pkt_size_overflow = 0;
88 h->max_packet_size = s->pkt_size;
90 s->context = zmq_ctx_new();
92 /*errno not set on failure during zmq_ctx_new()*/
93 av_log(h, AV_LOG_ERROR, "Error occured during zmq_ctx_new()\n");
94 return AVERROR_EXTERNAL;
97 av_strstart(uri, "zmq:", &uri);
99 /*publish during write*/
100 if (h->flags & AVIO_FLAG_WRITE) {
101 s->socket = zmq_socket(s->context, ZMQ_PUB);
103 av_log(h, AV_LOG_ERROR, "Error occured during zmq_socket(): %s\n", ZMQ_STRERROR);
107 ret = zmq_bind(s->socket, uri);
109 av_log(h, AV_LOG_ERROR, "Error occured during zmq_bind(): %s\n", ZMQ_STRERROR);
114 /*subscribe for read*/
115 if (h->flags & AVIO_FLAG_READ) {
116 s->socket = zmq_socket(s->context, ZMQ_SUB);
118 av_log(h, AV_LOG_ERROR, "Error occured during zmq_socket(): %s\n", ZMQ_STRERROR);
122 ret = zmq_setsockopt(s->socket, ZMQ_SUBSCRIBE, "", 0);
124 av_log(h, AV_LOG_ERROR, "Error occured during zmq_setsockopt(): %s\n", ZMQ_STRERROR);
128 ret = zmq_connect(s->socket, uri);
130 av_log(h, AV_LOG_ERROR, "Error occured during zmq_connect(): %s\n", ZMQ_STRERROR);
137 zmq_close(s->socket);
139 zmq_ctx_term(s->context);
140 return AVERROR_EXTERNAL;
143 static int zmq_proto_write(URLContext *h, const unsigned char *buf, int size)
146 ZMQContext *s = h->priv_data;
148 ret = zmq_proto_wait_timeout(h, s->socket, 1, h->rw_timeout, &h->interrupt_callback);
151 ret = zmq_send(s->socket, buf, size, 0);
153 av_log(h, AV_LOG_ERROR, "Error occured during zmq_send(): %s\n", ZMQ_STRERROR);
154 return AVERROR_EXTERNAL;
156 return ret; /*number of bytes sent*/
159 static int zmq_proto_read(URLContext *h, unsigned char *buf, int size)
162 ZMQContext *s = h->priv_data;
164 ret = zmq_proto_wait_timeout(h, s->socket, 0, h->rw_timeout, &h->interrupt_callback);
167 ret = zmq_recv(s->socket, buf, size, 0);
169 av_log(h, AV_LOG_ERROR, "Error occured during zmq_recv(): %s\n", ZMQ_STRERROR);
170 return AVERROR_EXTERNAL;
173 s->pkt_size_overflow = FFMAX(s->pkt_size_overflow, ret);
174 av_log(h, AV_LOG_WARNING, "Message exceeds available space in the buffer. Message will be truncated. Setting -pkt_size %d may resolve the issue.\n", s->pkt_size_overflow);
177 return ret; /*number of bytes read*/
180 static int zmq_proto_close(URLContext *h)
182 ZMQContext *s = h->priv_data;
183 zmq_close(s->socket);
184 zmq_ctx_term(s->context);
188 static const AVClass zmq_context_class = {
190 .item_name = av_default_item_name,
192 .version = LIBAVUTIL_VERSION_INT,
195 const URLProtocol ff_libzmq_protocol = {
197 .url_close = zmq_proto_close,
198 .url_open = zmq_proto_open,
199 .url_read = zmq_proto_read,
200 .url_write = zmq_proto_write,
201 .priv_data_size = sizeof(ZMQContext),
202 .priv_data_class = &zmq_context_class,
203 .flags = URL_PROTOCOL_FLAG_NETWORK,