|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 MolotovTv |
| 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 | +#include <hiredis/hiredis.h> |
| 22 | +#include <sys/time.h> |
| 23 | +#include "libavutil/mem.h" |
| 24 | +#include "libavutil/opt.h" |
| 25 | +#include "libavutil/parseutils.h" |
| 26 | +#include "avformat.h" |
| 27 | +#include "url.h" |
| 28 | +#include "urldecode.h" |
| 29 | + |
| 30 | +typedef struct { |
| 31 | + const AVClass *class; |
| 32 | + redisContext *ctx; |
| 33 | + char *key; |
| 34 | + int append; |
| 35 | + int64_t timeout; |
| 36 | + int64_t ttl; |
| 37 | +} LIBHIREDISContext; |
| 38 | + |
| 39 | +#define STR_LEN 1024 |
| 40 | + |
| 41 | +#define DEFAULT_IP "127.0.0.1" |
| 42 | +#define DEFAULT_PORT 6379 |
| 43 | +#define DEFAULT_TIMEOUT 500000 |
| 44 | + |
| 45 | +#define COMMAND_APPEND "APPEND" |
| 46 | +#define COMMAND_SET "SET" |
| 47 | +#define COMMAND_EXPIRE "EXPIRE" |
| 48 | + |
| 49 | +#define OFFSET(x) offsetof(LIBHIREDISContext, x) |
| 50 | +#define D AV_OPT_FLAG_DECODING_PARAM |
| 51 | +#define E AV_OPT_FLAG_ENCODING_PARAM |
| 52 | +static const AVOption options[] = { |
| 53 | + { "timeout", "Set timeout (in microseconds) of socket I/O operations", OFFSET(timeout), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, .flags = D | E }, |
| 54 | + { "ttl", "Time to live (in seconds) of redis key", OFFSET(ttl), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, .flags = D | E }, |
| 55 | + { NULL } |
| 56 | +}; |
| 57 | + |
| 58 | +static int libhiredis_open(URLContext *h, const char *uri, int flags) |
| 59 | +{ |
| 60 | + char hostname[STR_LEN], path[STR_LEN], buf[STR_LEN]; |
| 61 | + int port; |
| 62 | + const char *ip; |
| 63 | + char *p; |
| 64 | + struct timeval tval = { 0 }; |
| 65 | + LIBHIREDISContext *r = h->priv_data; |
| 66 | + |
| 67 | + if (flags & AVIO_FLAG_READ) { |
| 68 | + av_log(h, AV_LOG_ERROR, "Redis read not supported yet.\n"); |
| 69 | + return AVERROR(EINVAL); |
| 70 | + } |
| 71 | + /* Init */ |
| 72 | + r->append = 0; |
| 73 | + /* Parse URI */ |
| 74 | + av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), |
| 75 | + &port, path, sizeof(path), uri); |
| 76 | + /* IP */ |
| 77 | + if (*hostname == '\0') |
| 78 | + ip = DEFAULT_IP; |
| 79 | + else |
| 80 | + ip = hostname; |
| 81 | + /* Port */ |
| 82 | + if (port < 0) |
| 83 | + port = DEFAULT_PORT; |
| 84 | + if (port <= 0 || port > 65535 ) { |
| 85 | + av_log(h, AV_LOG_ERROR, "Invalid port\n"); |
| 86 | + return AVERROR(EINVAL); |
| 87 | + } |
| 88 | + /* Key */ |
| 89 | + if (*path == '\0' || *(path + 1) == '\0') { |
| 90 | + av_log(h, AV_LOG_ERROR, "No key\n"); |
| 91 | + return AVERROR(EINVAL); |
| 92 | + } |
| 93 | + p = strchr(path, '?'); |
| 94 | + if (p) |
| 95 | + *p = '\0'; |
| 96 | + r->key = ff_urldecode(path + 1, 0); /* skip leading '/' */ |
| 97 | + if (!r->key) |
| 98 | + return AVERROR(ENOMEM); |
| 99 | + /* Options */ |
| 100 | + p = strchr(uri, '?'); |
| 101 | + if (p) { |
| 102 | + if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) |
| 103 | + r->timeout = strtol(buf, NULL, 10); |
| 104 | + if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) |
| 105 | + r->ttl = strtol(buf, NULL, 10); |
| 106 | + } |
| 107 | + if (r->timeout < 0) |
| 108 | + r->timeout = DEFAULT_TIMEOUT; |
| 109 | + /* Redis connect */ |
| 110 | + tval.tv_sec = r->timeout / 1000000; |
| 111 | + tval.tv_usec = r->timeout % 1000000; |
| 112 | + r->ctx = redisConnectWithTimeout(ip, port, tval); |
| 113 | + if (r->ctx == NULL || r->ctx->err) { |
| 114 | + av_free(r->key); |
| 115 | + if (r->ctx) { |
| 116 | + av_log(h, AV_LOG_ERROR, "Error connect: %s\n", r->ctx->errstr); |
| 117 | + redisFree(r->ctx); |
| 118 | + return AVERROR_EXTERNAL; |
| 119 | + } |
| 120 | + return AVERROR(ENOMEM); |
| 121 | + } |
| 122 | + /* Command timeout */ |
| 123 | + if (redisSetTimeout(r->ctx, tval) != REDIS_OK) { |
| 124 | + if (r->ctx->err) |
| 125 | + av_log(h, AV_LOG_ERROR, "Error set timeout: %s\n", r->ctx->errstr); |
| 126 | + av_free(r->key); |
| 127 | + redisFree(r->ctx); |
| 128 | + return AVERROR_EXTERNAL; |
| 129 | + } |
| 130 | + |
| 131 | + return 0; |
| 132 | +} |
| 133 | + |
| 134 | +static int libhiredis_write(URLContext *h, const uint8_t *buf, int size) |
| 135 | +{ |
| 136 | + LIBHIREDISContext *r = h->priv_data; |
| 137 | + const char *command; |
| 138 | + redisReply *reply; |
| 139 | + |
| 140 | + if (r->append) |
| 141 | + command = COMMAND_APPEND; |
| 142 | + else |
| 143 | + command = COMMAND_SET; |
| 144 | + /* Redis command */ |
| 145 | + reply = redisCommand(r->ctx, "%s %s %b", command, r->key, buf, (size_t)size); |
| 146 | + if (!reply) { |
| 147 | + if (r->ctx->err == REDIS_ERR_IO) |
| 148 | + return AVERROR(EIO); |
| 149 | + if (r->ctx->err == REDIS_ERR_TIMEOUT) |
| 150 | + return AVERROR(EAGAIN); |
| 151 | + return AVERROR(ENOMEM); |
| 152 | + } else if (reply->type == REDIS_REPLY_ERROR) { |
| 153 | + av_log(h, AV_LOG_ERROR, "Error command (%s): %s\n", command, reply->str); |
| 154 | + freeReplyObject(reply); |
| 155 | + return AVERROR_EXTERNAL; |
| 156 | + } |
| 157 | + /* Ok */ |
| 158 | + freeReplyObject(reply); |
| 159 | + r->append = 1; |
| 160 | + |
| 161 | + return size; |
| 162 | +} |
| 163 | + |
| 164 | +static int libhiredis_close(URLContext *h) |
| 165 | +{ |
| 166 | + LIBHIREDISContext *r = h->priv_data; |
| 167 | + redisReply *reply; |
| 168 | + |
| 169 | + /* TTL / Redis EXPIRE command */ |
| 170 | + if (r->ttl > 0) { |
| 171 | + reply = redisCommand(r->ctx, "%s %s %d", COMMAND_EXPIRE, r->key, r->ttl); |
| 172 | + if (!reply) { |
| 173 | + if (r->ctx->err == REDIS_ERR_IO) |
| 174 | + return AVERROR(EIO); |
| 175 | + if (r->ctx->err == REDIS_ERR_TIMEOUT) |
| 176 | + return AVERROR(EAGAIN); |
| 177 | + return AVERROR(ENOMEM); |
| 178 | + } else if (reply->type == REDIS_REPLY_ERROR) { |
| 179 | + av_log(h, AV_LOG_ERROR, "Error command (%s): %s\n", COMMAND_EXPIRE, reply->str); |
| 180 | + freeReplyObject(reply); |
| 181 | + return AVERROR_EXTERNAL; |
| 182 | + } |
| 183 | + /* Ok */ |
| 184 | + freeReplyObject(reply); |
| 185 | + } |
| 186 | + |
| 187 | + av_free(r->key); |
| 188 | + redisFree(r->ctx); |
| 189 | + |
| 190 | + return 0; |
| 191 | +} |
| 192 | + |
| 193 | +static const AVClass libhiredis_context_class = { |
| 194 | + .class_name = "libhiredis", |
| 195 | + .item_name = av_default_item_name, |
| 196 | + .option = options, |
| 197 | + .version = LIBAVUTIL_VERSION_INT, |
| 198 | +}; |
| 199 | + |
| 200 | +const URLProtocol ff_libhiredis_protocol = { |
| 201 | + .name = "redis", |
| 202 | + .url_open = libhiredis_open, |
| 203 | + .url_write = libhiredis_write, |
| 204 | + .url_close = libhiredis_close, |
| 205 | + .priv_data_size = sizeof(LIBHIREDISContext), |
| 206 | + .priv_data_class = &libhiredis_context_class, |
| 207 | + .flags = URL_PROTOCOL_FLAG_NETWORK, |
| 208 | +}; |
0 commit comments