2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Reliable Internet Streaming Transport protocol
24 #include "libavutil/avassert.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/parseutils.h"
27 #include "libavutil/time.h"
32 #include "os_support.h"
35 #include <librist/librist.h>
37 typedef struct RISTContext {
47 struct rist_logging_settings logging_settings;
48 struct rist_peer_config peer_config;
50 struct rist_peer *peer;
54 #define D AV_OPT_FLAG_DECODING_PARAM
55 #define E AV_OPT_FLAG_ENCODING_PARAM
56 #define OFFSET(x) offsetof(RISTContext, x)
57 static const AVOption librist_options[] = {
58 { "rist_profile","set profile", OFFSET(profile), AV_OPT_TYPE_INT, {.i64=RIST_PROFILE_MAIN}, 0, 2, .flags = D|E, "profile" },
59 { "simple", NULL, 0, AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_SIMPLE}, 0, 0, .flags = D|E, "profile" },
60 { "main", NULL, 0, AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_MAIN}, 0, 0, .flags = D|E, "profile" },
61 { "advanced", NULL, 0, AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_ADVANCED}, 0, 0, .flags = D|E, "profile" },
62 { "buffer_size", "set buffer_size", OFFSET(buffer_size), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, .flags = D|E },
63 { "log_level", "set loglevel", OFFSET(log_level), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = D|E },
64 { "secret", "set encryption secret",OFFSET(secret), AV_OPT_TYPE_STRING,{.str=NULL}, 0, 0, .flags = D|E },
65 { "encryption","set encryption type",OFFSET(encryption), AV_OPT_TYPE_INT ,{.i64=0}, 0, INT_MAX, .flags = D|E },
69 static int risterr2ret(int err)
73 return AVERROR(ENOMEM);
75 return AVERROR_EXTERNAL;
79 static int log_cb(void *arg, enum rist_log_level log_level, const char *msg)
84 case RIST_LOG_ERROR: level = AV_LOG_ERROR; break;
85 case RIST_LOG_WARN: level = AV_LOG_WARNING; break;
86 case RIST_LOG_NOTICE: level = AV_LOG_VERBOSE; break;
87 case RIST_LOG_INFO: level = AV_LOG_INFO; break;
88 case RIST_LOG_DEBUG: level = AV_LOG_DEBUG; break;
89 case RIST_LOG_DISABLE: level = AV_LOG_QUIET; break;
90 case RIST_LOG_SIMULATE: level = AV_LOG_TRACE; break;
91 default: level = AV_LOG_PANIC;
94 av_log(arg, level, "%s", msg);
99 static int librist_close(URLContext *h)
101 RISTContext *s = h->priv_data;
107 ret = rist_destroy(s->ctx);
110 return risterr2ret(ret);
113 static int librist_open(URLContext *h, const char *uri, int flags)
115 RISTContext *s = h->priv_data;
116 struct rist_logging_settings *logging_settings = &s->logging_settings;
117 struct rist_peer_config *peer_config = &s->peer_config;
120 if ((flags & AVIO_FLAG_READ_WRITE) == AVIO_FLAG_READ_WRITE)
121 return AVERROR(EINVAL);
123 ret = rist_logging_set(&logging_settings, s->log_level, log_cb, h, NULL, NULL);
125 return risterr2ret(ret);
127 if (flags & AVIO_FLAG_WRITE)
128 ret = rist_sender_create(&s->ctx, s->profile, 0, logging_settings);
132 if (flags & AVIO_FLAG_READ)
133 ret = rist_receiver_create(&s->ctx, s->profile, logging_settings);
137 ret = rist_peer_config_defaults_set(peer_config);
141 ret = rist_parse_address(uri, (const struct rist_peer_config **)&peer_config);
145 if (((s->encryption == 128 || s->encryption == 256) && !s->secret) ||
146 ((peer_config->key_size == 128 || peer_config->key_size == 256) && !peer_config->secret[0])) {
147 av_log(h, AV_LOG_ERROR, "secret is mandatory if encryption is enabled\n");
149 return AVERROR(EINVAL);
152 if (s->secret && peer_config->secret[0] == 0)
153 av_strlcpy(peer_config->secret, s->secret, FFMIN(RIST_MAX_STRING_SHORT - 1, strlen(s->secret)));
155 if (s->secret && (s->encryption == 128 || s->encryption == 256))
156 peer_config->key_size = s->encryption;
158 if (s->buffer_size) {
159 peer_config->recovery_length_min = s->buffer_size;
160 peer_config->recovery_length_max = s->buffer_size;
163 ret = rist_peer_create(s->ctx, &s->peer, &s->peer_config);
167 ret = rist_start(s->ctx);
171 h->max_packet_size = 9968;
178 return risterr2ret(ret);
181 static int librist_read(URLContext *h, uint8_t *buf, int size)
183 RISTContext *s = h->priv_data;
184 const struct rist_data_block *data_block;
187 ret = rist_receiver_data_read(s->ctx, &data_block, s->queue_count <= 0 ? POLLING_TIME : 0);
189 return risterr2ret(ret);
191 if (ret == 0 || data_block->payload_len <= 0)
194 s->queue_count = ret - 1;
195 size = data_block->payload_len;
197 memcpy(buf, data_block->payload, size);
198 rist_receiver_data_block_free((struct rist_data_block**)&data_block);
203 static int librist_write(URLContext *h, const uint8_t *buf, int size)
205 RISTContext *s = h->priv_data;
206 struct rist_data_block data_block = { 0 };
209 data_block.ts_ntp = 0;
210 data_block.payload = buf;
211 data_block.payload_len = size;
213 ret = rist_sender_data_write(s->ctx, &data_block);
215 return risterr2ret(ret);
220 static const AVClass librist_class = {
221 .class_name = "librist",
222 .item_name = av_default_item_name,
223 .option = librist_options,
224 .version = LIBAVUTIL_VERSION_INT,
227 const URLProtocol ff_librist_protocol = {
229 .url_open = librist_open,
230 .url_read = librist_read,
231 .url_write = librist_write,
232 .url_close = librist_close,
233 .priv_data_size = sizeof(RISTContext),
234 .flags = URL_PROTOCOL_FLAG_NETWORK,
235 .priv_data_class = &librist_class,