forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcycle.c
332 lines (277 loc) · 8.29 KB
/
cycle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*****************************************************************************
* cycle.c: cycle stream output module
*****************************************************************************
* Copyright (C) 2015 Rémi Denis-Courmont
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Rémi Denis-Courmont reserves the right to redistribute this file under
* the terms of the GNU Lesser General Public License as published by the
* the Free Software Foundation; either version 2.1 or the License, or
* (at his option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <assert.h>
#include <stdlib.h>
#define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_block.h>
#include <vlc_sout.h>
typedef struct sout_cycle sout_cycle_t;
struct sout_cycle
{
sout_cycle_t *next;
mtime_t offset;
char chain[1];
};
struct sout_stream_id_sys_t
{
sout_stream_id_sys_t *prev;
sout_stream_id_sys_t *next;
es_format_t fmt;
void *id;
};
struct sout_stream_sys_t
{
sout_stream_t *stream; /*< Current output stream */
sout_stream_id_sys_t *first; /*< First elementary stream */
sout_stream_id_sys_t *last; /*< Last elementary stream */
sout_cycle_t *start;
sout_cycle_t *next;
mtime_t (*clock)(const block_t *);
mtime_t period; /*< Total cycle duration */
};
static mtime_t get_dts(const block_t *block)
{
return block->i_dts;
}
static sout_stream_id_sys_t *Add(sout_stream_t *stream, const es_format_t *fmt)
{
sout_stream_sys_t *sys = stream->p_sys;
sout_stream_id_sys_t *id = malloc(sizeof (*id));
if (unlikely(id == NULL))
return NULL;
id->next = NULL;
if (es_format_Copy(&id->fmt, fmt))
{
es_format_Clean(&id->fmt);
free(id);
return NULL;
}
if (sys->stream != NULL)
id->id = sout_StreamIdAdd(sys->stream, &id->fmt);
id->prev = sys->last;
sys->last = id;
if (id->prev != NULL)
id->prev->next = id;
else
sys->first = id;
return id;
}
static void Del(sout_stream_t *stream, sout_stream_id_sys_t *id)
{
sout_stream_sys_t *sys = stream->p_sys;
if (id->prev != NULL)
id->prev->next = id->next;
else
sys->first = id->next;
if (id->next != NULL)
id->next->prev = id->prev;
else
sys->last = id->prev;
if (sys->stream != NULL)
sout_StreamIdDel(sys->stream, id->id);
es_format_Clean(&id->fmt);
free(id);
}
static int AddStream(sout_stream_t *stream, char *chain)
{
sout_stream_sys_t *sys = stream->p_sys;
msg_Dbg(stream, "starting new phase \"%s\"", chain);
/* TODO format */
sys->stream = sout_StreamChainNew(stream->p_sout, chain,
stream->p_next, NULL);
if (sys->stream == NULL)
return -1;
for (sout_stream_id_sys_t *id = sys->first; id != NULL; id = id->next)
id->id = sout_StreamIdAdd(sys->stream, &id->fmt);
return 0;
}
static void DelStream(sout_stream_t *stream)
{
sout_stream_sys_t *sys = stream->p_sys;
if (sys->stream == NULL)
return;
for (sout_stream_id_sys_t *id = sys->first; id != NULL; id = id->next)
if (id->id != NULL)
sout_StreamIdDel(sys->stream, id->id);
sout_StreamChainDelete(sys->stream, NULL);
sys->stream = NULL;
}
static int Send(sout_stream_t *stream, sout_stream_id_sys_t *id,
block_t *block)
{
sout_stream_sys_t *sys = stream->p_sys;
for (block_t *next = block->p_next; block != NULL; block = next)
{
block->p_next = NULL;
/* FIXME: deal with key frames properly */
while (sys->clock(block) >= sys->next->offset)
{
DelStream(stream);
AddStream(stream, sys->next->chain);
sys->next->offset += sys->period;
sys->next = sys->next->next;
if (sys->next == NULL)
sys->next = sys->start;
}
if (sys->stream != NULL)
sout_StreamIdSend(sys->stream, id->id, block);
else
block_Release(block);
}
return VLC_SUCCESS;
}
static int AppendPhase(sout_cycle_t ***restrict pp,
mtime_t offset, const char *chain)
{
size_t len = strlen(chain);
sout_cycle_t *cycle = malloc(sizeof (*cycle) + len);
if (unlikely(cycle == NULL))
return -1;
cycle->next = NULL;
cycle->offset = offset;
memcpy(cycle->chain, chain, len + 1);
**pp = cycle;
*pp = &cycle->next;
return 0;
}
static mtime_t ParseTime(const char *str)
{
char *end;
unsigned long long u = strtoull(str, &end, 0);
switch (*end)
{
case 'w':
if (u > 15250284U)
return -1;
return CLOCK_FREQ * 604800LLU * u;
case 'd':
if (u > 106751991U)
return -1;
return CLOCK_FREQ * 86400LLU * u;
case 'h':
if (u > 2562047788U)
return -1;
return CLOCK_FREQ * 3600LLU * u;
case 'm':
if (u > 153722867280U)
return -1;
return CLOCK_FREQ * 60LLU * u;
case 's':
case 0:
if (u > 9223372036854U)
return -1;
return CLOCK_FREQ * u;
}
return -1;
}
static int Open(vlc_object_t *obj)
{
sout_stream_t *stream = (sout_stream_t *)obj;
sout_stream_sys_t *sys = malloc(sizeof (*sys));
if (unlikely(sys == NULL))
return VLC_ENOMEM;
sys->stream = NULL;
sys->first = NULL;
sys->last = NULL;
sys->start = NULL;
sys->clock = get_dts;
mtime_t offset = 0;
sout_cycle_t **pp = &sys->start;
const char *chain = "";
for (const config_chain_t *cfg = stream->p_cfg;
cfg != NULL;
cfg = cfg->p_next)
{
if (!strcmp(cfg->psz_name, "dst"))
{
chain = cfg->psz_value;
}
else if (!strcmp(cfg->psz_name, "duration"))
{
mtime_t t = ParseTime(cfg->psz_value);
if (t > 0)
{
AppendPhase(&pp, offset, chain);
offset += t;
}
chain = "";
}
else if (!strcmp(cfg->psz_name, "offset"))
{
mtime_t t = ParseTime(cfg->psz_value);
if (t > offset)
{
AppendPhase(&pp, offset, chain);
offset = t;
}
chain = "";
}
else
{
msg_Err(stream, "unknown option \"%s\"", cfg->psz_name);
}
}
if (sys->start == NULL || offset <= 0)
{
free(sys);
msg_Err(stream, "unknown or invalid cycle specification");
return VLC_EGENERIC;
}
sys->next = sys->start;
sys->period = offset;
stream->pf_add = Add;
stream->pf_del = Del;
stream->pf_send = Send;
stream->p_sys = sys;
return VLC_SUCCESS;
}
static void Close(vlc_object_t *obj)
{
sout_stream_t *stream = (sout_stream_t *)obj;
sout_stream_sys_t *sys = stream->p_sys;
assert(sys->first == NULL && sys->last == NULL);
if (sys->stream != NULL)
sout_StreamChainDelete(sys->stream, NULL);
for (sout_cycle_t *cycle = sys->start, *next; cycle != NULL; cycle = next)
{
next = cycle->next;
free(cycle);
}
free(sys);
}
vlc_module_begin()
set_shortname(N_("cycle"))
set_description(N_("Cyclic stream output"))
set_capability("sout stream", 0)
set_category(CAT_SOUT)
set_subcategory(SUBCAT_SOUT_STREAM)
set_callbacks(Open, Close)
add_shortcut("cycle")
vlc_module_end()