]> git.sesse.net Git - ffmpeg/blob - libavformat/librist.c
avformat/librist: rework librist_read
[ffmpeg] / libavformat / librist.c
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 /**
20  * @file
21  * Reliable Internet Streaming Transport protocol
22  */
23
24 #include "libavutil/avassert.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/parseutils.h"
27 #include "libavutil/time.h"
28
29 #include "avformat.h"
30 #include "internal.h"
31 #include "network.h"
32 #include "os_support.h"
33 #include "url.h"
34
35 #include <librist/librist.h>
36
37 typedef struct RISTContext {
38     const AVClass *class;
39
40     int profile;
41     int buffer_size;
42     int log_level;
43     int encryption;
44     char *secret;
45
46     struct rist_logging_settings logging_settings;
47     struct rist_peer_config peer_config;
48
49     struct rist_peer *peer;
50     struct rist_ctx *ctx;
51 } RISTContext;
52
53 #define D AV_OPT_FLAG_DECODING_PARAM
54 #define E AV_OPT_FLAG_ENCODING_PARAM
55 #define OFFSET(x) offsetof(RISTContext, x)
56 static const AVOption librist_options[] = {
57     { "rist_profile","set profile",     OFFSET(profile),     AV_OPT_TYPE_INT,   {.i64=RIST_PROFILE_MAIN},     0, 2, .flags = D|E, "profile" },
58     { "simple",      NULL,              0,                   AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_SIMPLE},   0, 0, .flags = D|E, "profile" },
59     { "main",        NULL,              0,                   AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_MAIN},     0, 0, .flags = D|E, "profile" },
60     { "advanced",    NULL,              0,                   AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_ADVANCED}, 0, 0, .flags = D|E, "profile" },
61     { "buffer_size", "set buffer_size", OFFSET(buffer_size), AV_OPT_TYPE_INT,   {.i64=0},                     0, INT_MAX, .flags = D|E },
62     { "log_level",   "set loglevel",    OFFSET(log_level),   AV_OPT_TYPE_INT,   {.i64=-1},                   -1, INT_MAX, .flags = D|E },
63     { "secret", "set encryption secret",OFFSET(secret),      AV_OPT_TYPE_STRING,{.str=NULL},                  0, 0,       .flags = D|E },
64     { "encryption","set encryption type",OFFSET(encryption), AV_OPT_TYPE_INT   ,{.i64=0},                     0, INT_MAX, .flags = D|E },
65     { NULL }
66 };
67
68 static int risterr2ret(int err)
69 {
70     switch (err) {
71     case RIST_ERR_MALLOC:
72         return AVERROR(ENOMEM);
73     default:
74         return AVERROR_EXTERNAL;
75     }
76 }
77
78 static int log_cb(void *arg, enum rist_log_level log_level, const char *msg)
79 {
80     int level;
81
82     switch (log_level) {
83     case RIST_LOG_ERROR:    level = AV_LOG_ERROR;   break;
84     case RIST_LOG_WARN:     level = AV_LOG_WARNING; break;
85     case RIST_LOG_NOTICE:   level = AV_LOG_VERBOSE; break;
86     case RIST_LOG_INFO:     level = AV_LOG_INFO;    break;
87     case RIST_LOG_DEBUG:    level = AV_LOG_DEBUG;   break;
88     case RIST_LOG_DISABLE:  level = AV_LOG_QUIET;   break;
89     case RIST_LOG_SIMULATE: level = AV_LOG_TRACE;   break;
90     default: level = AV_LOG_PANIC;
91     }
92
93     av_log(arg, level, "%s", msg);
94
95     return 0;
96 }
97
98 static int librist_close(URLContext *h)
99 {
100     RISTContext *s = h->priv_data;
101     int ret = 0;
102
103     s->peer = NULL;
104
105     if (s->ctx)
106         ret = rist_destroy(s->ctx);
107     s->ctx = NULL;
108
109     return risterr2ret(ret);
110 }
111
112 static int librist_open(URLContext *h, const char *uri, int flags)
113 {
114     RISTContext *s = h->priv_data;
115     struct rist_logging_settings *logging_settings = &s->logging_settings;
116     struct rist_peer_config *peer_config = &s->peer_config;
117     int ret;
118
119     if ((flags & AVIO_FLAG_READ_WRITE) == AVIO_FLAG_READ_WRITE)
120         return AVERROR(EINVAL);
121
122     ret = rist_logging_set(&logging_settings, s->log_level, log_cb, h, NULL, NULL);
123     if (ret < 0)
124         return risterr2ret(ret);
125
126     if (flags & AVIO_FLAG_WRITE)
127         ret = rist_sender_create(&s->ctx, s->profile, 0, logging_settings);
128     if (ret < 0)
129         goto err;
130
131     if (flags & AVIO_FLAG_READ)
132         ret = rist_receiver_create(&s->ctx, s->profile, logging_settings);
133     if (ret < 0)
134         goto err;
135
136     ret = rist_peer_config_defaults_set(peer_config);
137     if (ret < 0)
138         goto err;
139
140     ret = rist_parse_address(uri, (const struct rist_peer_config **)&peer_config);
141     if (ret < 0)
142         goto err;
143
144     if (((s->encryption == 128 || s->encryption == 256) && !s->secret) ||
145         ((peer_config->key_size == 128 || peer_config->key_size == 256) && !peer_config->secret[0])) {
146         av_log(h, AV_LOG_ERROR, "secret is mandatory if encryption is enabled\n");
147         librist_close(h);
148         return AVERROR(EINVAL);
149     }
150
151     if (s->secret && peer_config->secret[0] == 0)
152         av_strlcpy(peer_config->secret, s->secret, FFMIN(RIST_MAX_STRING_SHORT - 1, strlen(s->secret)));
153
154     if (s->secret && (s->encryption == 128 || s->encryption == 256))
155         peer_config->key_size = s->encryption;
156
157     if (s->buffer_size) {
158         peer_config->recovery_length_min = s->buffer_size;
159         peer_config->recovery_length_max = s->buffer_size;
160     }
161
162     ret = rist_peer_create(s->ctx, &s->peer, &s->peer_config);
163     if (ret < 0)
164         goto err;
165
166     ret = rist_start(s->ctx);
167     if (ret < 0)
168         goto err;
169
170     h->max_packet_size = 9968;
171
172     return 0;
173
174 err:
175     librist_close(h);
176
177     return risterr2ret(ret);
178 }
179
180 static int librist_read(URLContext *h, uint8_t *buf, int size)
181 {
182     RISTContext *s = h->priv_data;
183     const struct rist_data_block *data_block;
184     int ret;
185
186     ret = rist_receiver_data_read(s->ctx, &data_block, POLLING_TIME);
187     if (ret < 0)
188         return risterr2ret(ret);
189
190     if (ret == 0)
191         return AVERROR(EAGAIN);
192
193     if (data_block->payload_len > 9968) {
194         rist_receiver_data_block_free((struct rist_data_block**)&data_block);
195         return AVERROR_EXTERNAL;
196     }
197
198     size = data_block->payload_len;
199     memcpy(buf, data_block->payload, size);
200     rist_receiver_data_block_free((struct rist_data_block**)&data_block);
201
202     return size;
203 }
204
205 static int librist_write(URLContext *h, const uint8_t *buf, int size)
206 {
207     RISTContext *s = h->priv_data;
208     struct rist_data_block data_block = { 0 };
209     int ret;
210
211     data_block.ts_ntp = 0;
212     data_block.payload = buf;
213     data_block.payload_len = size;
214
215     ret = rist_sender_data_write(s->ctx, &data_block);
216     if (ret < 0)
217         return risterr2ret(ret);
218
219     return ret;
220 }
221
222 static const AVClass librist_class = {
223     .class_name = "librist",
224     .item_name  = av_default_item_name,
225     .option     = librist_options,
226     .version    = LIBAVUTIL_VERSION_INT,
227 };
228
229 const URLProtocol ff_librist_protocol = {
230     .name                = "rist",
231     .url_open            = librist_open,
232     .url_read            = librist_read,
233     .url_write           = librist_write,
234     .url_close           = librist_close,
235     .priv_data_size      = sizeof(RISTContext),
236     .flags               = URL_PROTOCOL_FLAG_NETWORK,
237     .priv_data_class     = &librist_class,
238 };