Coverage Report

Created: 2025-07-23 06:11

/src/vlc/modules/demux/webvtt.c
Line
Count
Source (jump to first uncovered line)
1
/*****************************************************************************
2
 * webvtt.c: WEBVTT text demuxer (as ISO1446-30 payload)
3
 *****************************************************************************
4
 * Copyright (C) 2017 VideoLabs, VLC authors and VideoLAN
5
 *
6
 * This program is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation; either version 2.1 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program 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
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program; if not, write to the Free Software Foundation,
18
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19
 *****************************************************************************/
20
21
/*****************************************************************************
22
 * Preamble
23
 *****************************************************************************/
24
25
#ifdef HAVE_CONFIG_H
26
# include "config.h"
27
#endif
28
29
#include <vlc_common.h>
30
#include <vlc_demux.h>
31
#include <vlc_memstream.h>
32
33
#include "../codec/webvtt/webvtt.h"
34
35
/*****************************************************************************
36
 * Prototypes:
37
 *****************************************************************************/
38
39
struct index_entry_s
40
{
41
    vlc_tick_t time;
42
    unsigned active;
43
};
44
45
typedef struct
46
{
47
    es_out_id_t *es;
48
    bool         b_slave;
49
    bool         b_first_time;
50
    int          i_next_block_flags;
51
    vlc_tick_t   i_next_demux_time;
52
    vlc_tick_t   i_length;
53
    struct
54
    {
55
        void    *p_data;
56
        size_t   i_data;
57
    } regions_headers, styles_headers;
58
59
    struct
60
    {
61
        webvtt_cue_t *p_array;
62
        size_t  i_alloc;
63
        size_t  i_count;
64
    } cues;
65
66
    struct
67
    {
68
        struct index_entry_s *p_array;
69
        size_t   i_alloc;
70
        size_t   i_count;
71
        size_t   i_current;
72
    } index;
73
74
    webvtt_text_parser_t *p_streamparser;
75
} demux_sys_t;
76
77
0
#define WEBVTT_PREALLOC 64
78
79
/*****************************************************************************
80
 *
81
 *****************************************************************************/
82
static int cue_Compare( const void *a_, const void *b_ )
83
0
{
84
0
    webvtt_cue_t *a = (webvtt_cue_t *)a_;
85
0
    webvtt_cue_t *b = (webvtt_cue_t *)b_;
86
0
    if( a->i_start == b->i_start )
87
0
    {
88
0
        if( a->i_stop > b->i_stop )
89
0
            return -1;
90
0
        else
91
0
            return ( a->i_stop < b->i_stop ) ? 1 : 0;
92
0
    }
93
0
    else return a->i_start < b->i_start ? -1 : 1;
94
0
}
95
96
static block_t *ConvertWEBVTT( const webvtt_cue_t *p_cue, bool b_continued )
97
0
{
98
0
    struct vlc_memstream stream;
99
100
0
    if( vlc_memstream_open( &stream ) )
101
0
        return NULL;
102
103
0
    const size_t paylsize = 8 + strlen( p_cue->psz_text );
104
0
    const size_t idensize = (p_cue->psz_id) ? 8 + strlen( p_cue->psz_id ) : 0;
105
0
    const size_t attrsize = (p_cue->psz_attrs) ? 8 + strlen( p_cue->psz_attrs ) : 0;
106
0
    const size_t vttcsize = 8 + paylsize + attrsize + idensize;
107
108
0
    uint8_t vttcbox[8] = { 0, 0, 0, 0, 'v', 't', 't', 'c' };
109
0
    if( b_continued )
110
0
        vttcbox[7] = 'x';
111
0
    SetDWBE( vttcbox, vttcsize );
112
0
    vlc_memstream_write( &stream, vttcbox, 8 );
113
114
0
    if( p_cue->psz_id )
115
0
    {
116
0
        uint8_t idenbox[8] = { 0, 0, 0, 0, 'i', 'd', 'e', 'n' };
117
0
        SetDWBE( idenbox, idensize );
118
0
        vlc_memstream_write( &stream, idenbox, 8 );
119
0
        vlc_memstream_write( &stream, p_cue->psz_id, idensize - 8 );
120
0
    }
121
122
0
    if( p_cue->psz_attrs )
123
0
    {
124
0
        uint8_t attrbox[8] = { 0, 0, 0, 0, 's', 't', 't', 'g' };
125
0
        SetDWBE( attrbox, attrsize );
126
0
        vlc_memstream_write( &stream, attrbox, 8 );
127
0
        vlc_memstream_write( &stream, p_cue->psz_attrs, attrsize - 8 );
128
0
    }
129
130
0
    uint8_t paylbox[8] = { 0, 0, 0, 0, 'p', 'a', 'y', 'l' };
131
0
    SetDWBE( paylbox, paylsize );
132
0
    vlc_memstream_write( &stream, paylbox, 8 );
133
0
    vlc_memstream_write( &stream, p_cue->psz_text, paylsize - 8 );
134
135
0
    if( vlc_memstream_close( &stream ) == 0 )
136
0
        return block_heap_Alloc( stream.ptr, stream.length );
137
0
    return NULL;
138
0
}
139
140
struct memstream_wrap
141
{
142
    struct vlc_memstream memstream;
143
    bool b_opened;
144
};
145
146
static void memstream_Append( struct memstream_wrap *mw, const char *psz )
147
0
{
148
0
    if( mw->b_opened )
149
0
    {
150
0
        vlc_memstream_puts( &mw->memstream, psz );
151
0
        vlc_memstream_putc( &mw->memstream, '\n' );
152
0
    }
153
0
}
154
155
static void memstream_Grab( struct memstream_wrap *mw, void **pp, size_t *pi )
156
0
{
157
0
    if( mw->b_opened && vlc_memstream_close( &mw->memstream ) == 0 )
158
0
    {
159
0
        *pp = mw->memstream.ptr;
160
0
        *pi = mw->memstream.length;
161
0
    }
162
0
}
163
164
/*****************************************************************************
165
 * Seekable demux Open() parser callbacks
166
 *****************************************************************************/
167
struct callback_ctx
168
{
169
    demux_t *p_demux;
170
    struct memstream_wrap regions, styles;
171
    bool b_ordered;
172
};
173
174
static webvtt_cue_t * ParserGetCueHandler( void *priv )
175
0
{
176
0
    struct callback_ctx *ctx = (struct callback_ctx *) priv;
177
0
    demux_sys_t *p_sys = ctx->p_demux->p_sys;
178
    /* invalid recycled cue */
179
0
    if( p_sys->cues.i_count &&
180
0
        p_sys->cues.p_array[p_sys->cues.i_count - 1].psz_text == NULL )
181
0
    {
182
0
        return &p_sys->cues.p_array[p_sys->cues.i_count - 1];
183
0
    }
184
185
0
    if( p_sys->cues.i_alloc <= p_sys->cues.i_count &&
186
0
       (SIZE_MAX / sizeof(webvtt_cue_t)) - WEBVTT_PREALLOC > p_sys->cues.i_alloc )
187
0
    {
188
0
        webvtt_cue_t *p_realloc = realloc( p_sys->cues.p_array,
189
0
                sizeof(webvtt_cue_t) * ( p_sys->cues.i_alloc + WEBVTT_PREALLOC ) );
190
0
        if( p_realloc )
191
0
        {
192
0
            p_sys->cues.p_array = p_realloc;
193
0
            p_sys->cues.i_alloc += WEBVTT_PREALLOC;
194
0
        }
195
0
    }
196
197
0
    if( p_sys->cues.i_alloc > p_sys->cues.i_count )
198
0
        return &p_sys->cues.p_array[p_sys->cues.i_count++];
199
200
0
    return NULL;
201
0
}
202
203
static void ParserCueDoneHandler( void *priv, webvtt_cue_t *p_cue )
204
0
{
205
0
    struct callback_ctx *ctx = (struct callback_ctx *) priv;
206
0
    demux_sys_t *p_sys = ctx->p_demux->p_sys;
207
0
    if( p_cue->psz_text == NULL )
208
0
    {
209
0
        webvtt_cue_Clean( p_cue );
210
0
        webvtt_cue_Init( p_cue );
211
0
        return;
212
0
    }
213
0
    if( p_cue->i_stop > p_sys->i_length )
214
0
        p_sys->i_length = p_cue->i_stop;
215
0
    if( p_sys->cues.i_count > 0 &&
216
0
        p_sys->cues.p_array[p_sys->cues.i_count - 1].i_start != p_cue->i_start )
217
0
        ctx->b_ordered = false;
218
219
    /* Store timings */
220
0
    if( p_sys->index.i_alloc <= p_sys->index.i_count &&
221
0
       (SIZE_MAX / sizeof(struct index_entry_s)) - WEBVTT_PREALLOC * 2 > p_sys->index.i_alloc )
222
0
    {
223
0
        void *p_realloc = realloc( p_sys->index.p_array,
224
0
                                   sizeof(struct index_entry_s) *
225
0
                                   ( p_sys->index.i_alloc + WEBVTT_PREALLOC * 2 ) );
226
0
        if( p_realloc )
227
0
        {
228
0
            p_sys->index.p_array = p_realloc;
229
0
            p_sys->index.i_alloc += WEBVTT_PREALLOC * 2;
230
0
        }
231
0
    }
232
0
    if( p_sys->index.i_alloc > p_sys->index.i_count )
233
0
    {
234
0
        p_sys->index.p_array[p_sys->index.i_count].active = 1; /* tmp start tag */
235
0
        p_sys->index.p_array[p_sys->index.i_count++].time = p_cue->i_start;
236
0
        p_sys->index.p_array[p_sys->index.i_count].active = 0;
237
0
        p_sys->index.p_array[p_sys->index.i_count++].time = p_cue->i_stop;
238
0
    }
239
0
}
240
241
static void ParserHeaderHandler( void *priv, enum webvtt_header_line_e s,
242
                                 bool b_new, const char *psz_line )
243
0
{
244
0
    VLC_UNUSED(b_new);
245
0
    struct callback_ctx *ctx = (struct callback_ctx *) priv;
246
0
    if( s == WEBVTT_HEADER_STYLE )
247
0
        memstream_Append( &ctx->styles, psz_line );
248
0
    else if( s == WEBVTT_HEADER_REGION )
249
0
        memstream_Append( &ctx->regions, psz_line );
250
0
}
251
252
/*****************************************************************************
253
 * Streamed cues DemuxStream() parser callbacks
254
 *****************************************************************************/
255
256
static webvtt_cue_t * StreamParserGetCueHandler( void *priv )
257
0
{
258
0
    VLC_UNUSED(priv);
259
0
    return malloc( sizeof(webvtt_cue_t) );
260
0
}
261
262
static void StreamParserCueDoneHandler( void *priv, webvtt_cue_t *p_cue )
263
0
{
264
0
    demux_t *p_demux = (demux_t *) priv;
265
0
    demux_sys_t *p_sys = p_demux->p_sys;
266
267
0
    if( p_cue->psz_text )
268
0
    {
269
0
        block_t *p_block = ConvertWEBVTT( p_cue, true );
270
0
        if( p_block )
271
0
        {
272
0
            if ( p_sys->b_first_time )
273
0
            {
274
0
                es_out_SetPCR( p_demux->out, p_cue->i_start + VLC_TICK_0 );
275
0
                p_sys->b_first_time = false;
276
0
            }
277
0
            p_sys->i_next_demux_time = p_cue->i_start;
278
0
            p_block->i_dts =
279
0
                    p_block->i_pts = VLC_TICK_0 + p_cue->i_start;
280
0
            if( p_cue->i_stop >= 0 && p_cue->i_stop >= p_cue->i_start )
281
0
                p_block->i_length = p_cue->i_stop - p_cue->i_start;
282
0
            es_out_Send( p_demux->out, p_sys->es, p_block );
283
0
            es_out_SetPCR( p_demux->out, p_cue->i_start + VLC_TICK_0 );
284
0
        }
285
0
    }
286
0
    webvtt_cue_Clean( p_cue );
287
0
    free( p_cue );
288
0
}
289
290
/*****************************************************************************
291
 * Demux Index
292
 *****************************************************************************/
293
294
static int index_Compare( const void *a_, const void *b_ )
295
0
{
296
0
    struct index_entry_s *a = (struct index_entry_s *) a_;
297
0
    struct index_entry_s *b = (struct index_entry_s *) b_;
298
0
    if( a->time == b->time )
299
0
    {
300
0
        if( a->active > b->active )
301
0
            return -1;
302
0
        else
303
0
            return b->active - a->active;
304
0
    }
305
0
    else return a->time < b->time ? -1 : 1;
306
0
}
307
308
static size_t getIndexByTime( demux_sys_t *p_sys, vlc_tick_t i_time )
309
0
{
310
0
    for( size_t i=0; i<p_sys->index.i_count; i++ )
311
0
    {
312
0
        if( p_sys->index.p_array[i].time >= i_time )
313
0
            return i;
314
0
    }
315
0
    return 0;
316
0
}
317
318
static void BuildIndex( demux_t *p_demux )
319
0
{
320
0
    demux_sys_t *p_sys = p_demux->p_sys;
321
322
    /* Order time entries ascending, start time before end time */
323
0
    qsort( p_sys->index.p_array, p_sys->index.i_count,
324
0
           sizeof(struct index_entry_s), index_Compare );
325
326
    /* Build actives count
327
    TIME 3000 count 1
328
    TIME 14500 count 2 (1 overlap)
329
    TIME 16100 count 3 (2 overlaps)
330
    TIME 16100 count 2 (1 overlap.. because there next start == end)
331
    TIME 18000 count 3
332
    TIME 18000 count 2 */
333
0
    unsigned i_overlaps = 0;
334
0
    for( size_t i=0; i<p_sys->index.i_count; i++ )
335
0
    {
336
0
        if( p_sys->index.p_array[i].active )
337
0
            p_sys->index.p_array[i].active = ++i_overlaps;
338
0
        else
339
0
            p_sys->index.p_array[i].active = --i_overlaps;
340
0
    }
341
0
}
342
343
static block_t *demux_From( demux_t *p_demux, vlc_tick_t i_start )
344
0
{
345
0
    demux_sys_t *p_sys = p_demux->p_sys;
346
347
0
    block_t *p_list = NULL;
348
0
    block_t **pp_append = &p_list;
349
0
    for( size_t i=0; i<p_sys->cues.i_count; i++ )
350
0
    {
351
0
        const webvtt_cue_t *p_cue = &p_sys->cues.p_array[i];
352
0
        if( p_cue->i_start > i_start )
353
0
        {
354
0
            break;
355
0
        }
356
0
        else if( p_cue->i_start <= i_start && p_cue->i_stop > i_start )
357
0
        {
358
0
            *pp_append = ConvertWEBVTT( p_cue, p_sys->index.i_current > 0 );
359
0
            if( *pp_append )
360
0
                pp_append = &((*pp_append)->p_next);
361
0
        }
362
0
    }
363
364
0
    return ( p_list ) ? block_ChainGather( p_list ) : NULL;
365
0
}
366
367
static int ReadWEBVTT( demux_t *p_demux )
368
0
{
369
0
    demux_sys_t *p_sys = p_demux->p_sys;
370
371
0
    struct callback_ctx ctx;
372
0
    ctx.p_demux = p_demux;
373
0
    ctx.b_ordered = true;
374
375
0
    webvtt_text_parser_t *p_parser =
376
0
            webvtt_text_parser_New( &ctx, ParserGetCueHandler,
377
0
                                          ParserCueDoneHandler,
378
0
                                          ParserHeaderHandler );
379
0
    if( p_parser == NULL )
380
0
        return VLC_EGENERIC;
381
382
0
    ctx.regions.b_opened = !vlc_memstream_open( &ctx.regions.memstream );
383
0
    ctx.styles.b_opened = !vlc_memstream_open( &ctx.styles.memstream );
384
385
0
    char *psz_line;
386
0
    while( (psz_line = vlc_stream_ReadLine( p_demux->s )) )
387
0
        webvtt_text_parser_Feed( p_parser, psz_line );
388
0
    webvtt_text_parser_Feed( p_parser, NULL );
389
390
0
    if( !ctx.b_ordered )
391
0
        qsort( p_sys->cues.p_array, p_sys->cues.i_count, sizeof(webvtt_cue_t), cue_Compare );
392
393
0
    BuildIndex( p_demux );
394
395
0
    memstream_Grab( &ctx.regions, &p_sys->regions_headers.p_data,
396
0
                                  &p_sys->regions_headers.i_data );
397
0
    memstream_Grab( &ctx.styles, &p_sys->styles_headers.p_data,
398
0
                                 &p_sys->styles_headers.i_data );
399
400
0
    webvtt_text_parser_Delete( p_parser );
401
402
0
    return VLC_SUCCESS;
403
0
}
404
405
static void MakeExtradata( demux_sys_t *p_sys, void **p_extra, size_t *pi_extra )
406
0
{
407
0
    struct vlc_memstream extradata;
408
0
    if( vlc_memstream_open( &extradata ) )
409
0
        return;
410
0
    vlc_memstream_puts( &extradata, "WEBVTT\n\n");
411
0
    vlc_memstream_write( &extradata, p_sys->regions_headers.p_data,
412
0
                                     p_sys->regions_headers.i_data );
413
0
    vlc_memstream_write( &extradata, p_sys->styles_headers.p_data,
414
0
                                     p_sys->styles_headers.i_data );
415
0
    if( vlc_memstream_close( &extradata ) == 0 )
416
0
    {
417
0
        if( extradata.length )
418
0
        {
419
0
            *p_extra = extradata.ptr;
420
0
            *pi_extra = extradata.length;
421
0
        }
422
0
        else free( extradata.ptr );
423
0
    }
424
0
}
425
426
/*****************************************************************************
427
 * Control:
428
 *****************************************************************************/
429
static int Control( demux_t *p_demux, int i_query, va_list args )
430
0
{
431
0
    demux_sys_t *p_sys = p_demux->p_sys;
432
0
    double *pf;
433
434
0
    switch( i_query )
435
0
    {
436
0
        case DEMUX_CAN_SEEK:
437
0
            *va_arg( args, bool * ) = true;
438
0
            return VLC_SUCCESS;
439
440
0
        case DEMUX_GET_LENGTH:
441
0
            *(va_arg( args, vlc_tick_t * )) = p_sys->i_length;
442
0
            return VLC_SUCCESS;
443
444
0
        case DEMUX_GET_TIME:
445
0
            *va_arg( args, vlc_tick_t * ) = p_sys->i_next_demux_time;
446
0
            return VLC_SUCCESS;
447
448
0
        case DEMUX_SET_TIME:
449
0
            if( p_sys->cues.i_count )
450
0
            {
451
0
                p_sys->index.i_current = getIndexByTime( p_sys, va_arg( args, vlc_tick_t ) );
452
0
                p_sys->b_first_time = true;
453
0
                p_sys->i_next_demux_time =
454
0
                        p_sys->index.p_array[p_sys->index.i_current].time;
455
0
                p_sys->i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
456
0
                return VLC_SUCCESS;
457
0
            }
458
0
            break;
459
460
0
        case DEMUX_GET_POSITION:
461
0
            pf = va_arg( args, double * );
462
0
            if( p_sys->index.i_current >= p_sys->index.i_count )
463
0
            {
464
0
                *pf = 1.0;
465
0
            }
466
0
            else if( p_sys->index.i_count > 0 )
467
0
            {
468
0
                *pf = (double) p_sys->i_next_demux_time /
469
0
                      (p_sys->i_length + VLC_TICK_FROM_MS(500));
470
0
            }
471
0
            else
472
0
            {
473
0
                *pf = 0.0;
474
0
            }
475
0
            return VLC_SUCCESS;
476
477
0
        case DEMUX_SET_POSITION:
478
0
            if( p_sys->cues.i_count )
479
0
            {
480
0
                double f = va_arg( args, double );
481
0
                p_sys->index.i_current = getIndexByTime( p_sys, p_sys->i_length * f );
482
0
                p_sys->b_first_time = true;
483
0
                p_sys->i_next_demux_time =
484
0
                        p_sys->index.p_array[p_sys->index.i_current].time;
485
0
                p_sys->i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
486
0
                return VLC_SUCCESS;
487
0
            }
488
0
            break;
489
490
0
        case DEMUX_SET_NEXT_DEMUX_TIME:
491
0
            p_sys->b_slave = true;
492
0
            p_sys->i_next_demux_time = va_arg( args, vlc_tick_t ) - VLC_TICK_0;
493
0
            return VLC_SUCCESS;
494
495
0
        case DEMUX_CAN_PAUSE:
496
0
        case DEMUX_SET_PAUSE_STATE:
497
0
        case DEMUX_CAN_CONTROL_PACE:
498
0
            return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
499
500
0
        case DEMUX_GET_PTS_DELAY:
501
0
        case DEMUX_GET_FPS:
502
0
        case DEMUX_GET_META:
503
0
        case DEMUX_GET_ATTACHMENTS:
504
0
        case DEMUX_GET_TITLE_INFO:
505
0
        case DEMUX_HAS_UNSUPPORTED_META:
506
0
        case DEMUX_CAN_RECORD:
507
0
        default:
508
0
            break;
509
510
0
    }
511
0
    return VLC_EGENERIC;
512
0
}
513
514
static int ControlStream( demux_t *p_demux, int i_query, va_list args )
515
0
{
516
0
    demux_sys_t *p_sys = p_demux->p_sys;
517
518
0
    switch( i_query )
519
0
    {
520
0
        case DEMUX_GET_TIME:
521
0
            if( p_sys->i_next_demux_time != VLC_TICK_INVALID )
522
0
            {
523
0
                *va_arg( args, vlc_tick_t * ) = p_sys->i_next_demux_time;
524
0
                return VLC_SUCCESS;
525
0
            }
526
0
        default:
527
0
            break;
528
0
    }
529
530
0
    return VLC_EGENERIC;
531
0
}
532
533
/*****************************************************************************
534
 * Demux: Send subtitle to decoder
535
 *****************************************************************************/
536
static int Demux( demux_t *p_demux )
537
0
{
538
0
    demux_sys_t *p_sys = p_demux->p_sys;
539
540
0
    vlc_tick_t i_barrier = p_sys->i_next_demux_time;
541
542
0
    while( p_sys->index.i_current < p_sys->index.i_count &&
543
0
           p_sys->index.p_array[p_sys->index.i_current].time <= i_barrier )
544
0
    {
545
        /* Find start and end of our interval */
546
0
        vlc_tick_t i_start_time = p_sys->index.p_array[p_sys->index.i_current].time;
547
0
        vlc_tick_t i_end_time = i_start_time;
548
        /* use next interval time as end time */
549
0
        while( ++p_sys->index.i_current < p_sys->index.i_count )
550
0
        {
551
0
            if( i_start_time != p_sys->index.p_array[p_sys->index.i_current].time )
552
0
            {
553
0
                i_end_time = p_sys->index.p_array[p_sys->index.i_current].time;
554
0
                break;
555
0
            }
556
0
        }
557
558
0
        block_t *p_block = demux_From( p_demux, i_start_time );
559
0
        if( p_block )
560
0
        {
561
0
            p_block->i_length = i_end_time - i_start_time;
562
0
            p_block->i_dts = p_block->i_pts = VLC_TICK_0 + i_start_time;
563
564
0
            if( p_sys->i_next_block_flags )
565
0
            {
566
0
                p_block->i_flags = p_sys->i_next_block_flags;
567
0
                p_sys->i_next_block_flags = 0;
568
0
            }
569
570
0
            if ( !p_sys->b_slave && p_sys->b_first_time )
571
0
            {
572
0
                es_out_SetPCR( p_demux->out, p_block->i_dts );
573
0
                p_sys->b_first_time = false;
574
0
            }
575
576
0
            es_out_Send( p_demux->out, p_sys->es, p_block );
577
0
        }
578
579
0
        if( p_sys->index.i_current < p_sys->index.i_count &&
580
0
            p_sys->index.p_array[p_sys->index.i_current].active > 1 )
581
0
        {
582
            /* we'll need to clear up overlaps */
583
0
            p_sys->i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
584
0
        }
585
0
    }
586
587
0
    if ( !p_sys->b_slave )
588
0
    {
589
0
        es_out_SetPCR( p_demux->out, VLC_TICK_0 + i_barrier );
590
0
        p_sys->i_next_demux_time += VLC_TICK_FROM_SEC(1);
591
0
    }
592
593
0
    if( p_sys->index.i_current >= p_sys->index.i_count )
594
0
        return VLC_DEMUXER_EOF;
595
596
0
    return VLC_DEMUXER_SUCCESS;
597
0
}
598
599
static int DemuxStream( demux_t *p_demux )
600
0
{
601
0
    demux_sys_t *p_sys = p_demux->p_sys;
602
603
0
    char *psz_line = vlc_stream_ReadLine( p_demux->s );
604
0
    webvtt_text_parser_Feed( p_sys->p_streamparser, psz_line );
605
606
0
    return ( psz_line == NULL ) ? VLC_DEMUXER_EOF : VLC_DEMUXER_SUCCESS;
607
0
}
608
609
/*****************************************************************************
610
 * Module initializers common
611
 *****************************************************************************/
612
static int ProbeWEBVTT( demux_t *p_demux )
613
0
{
614
0
    const uint8_t *p_peek;
615
0
    size_t i_peek = vlc_stream_Peek( p_demux->s, &p_peek, 16 );
616
0
    if( i_peek < 16 )
617
0
        return VLC_EGENERIC;
618
619
0
    if( !memcmp( p_peek, "\xEF\xBB\xBF", 3 ) )
620
0
        p_peek += 3;
621
622
0
    if( ( memcmp( p_peek, "WEBVTT", 6 ) ||
623
0
          ( p_peek[6] != '\n' &&
624
0
            p_peek[6] != ' ' &&
625
0
            p_peek[6] != '\t' &&
626
0
           ( p_peek[6] != '\r' || p_peek[7] != '\n' ) )
627
0
        ) && !p_demux->obj.force )
628
0
    {
629
0
        msg_Dbg( p_demux, "subtitle demux discarded" );
630
0
        return VLC_EGENERIC;
631
0
    }
632
633
0
    return VLC_SUCCESS;
634
0
}
635
636
/*****************************************************************************
637
 * Module initializers
638
 *****************************************************************************/
639
int webvtt_OpenDemux ( vlc_object_t *p_this )
640
0
{
641
0
    demux_t        *p_demux = (demux_t*)p_this;
642
0
    demux_sys_t    *p_sys;
643
644
0
    int i_ret = ProbeWEBVTT( p_demux );
645
0
    if( i_ret != VLC_SUCCESS )
646
0
        return i_ret;
647
648
0
    p_demux->pf_demux = Demux;
649
0
    p_demux->pf_control = Control;
650
651
0
    p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
652
0
    if( p_sys == NULL )
653
0
        return VLC_ENOMEM;
654
655
0
    if( ReadWEBVTT( p_demux ) != VLC_SUCCESS )
656
0
    {
657
0
        webvtt_CloseDemux( p_this );
658
0
        return VLC_EGENERIC;
659
0
    }
660
661
0
    es_format_t fmt;
662
0
    es_format_Init( &fmt, SPU_ES, VLC_CODEC_WEBVTT );
663
0
    size_t i_extra = 0;
664
0
    MakeExtradata( p_sys, &fmt.p_extra, &i_extra );
665
0
    fmt.i_extra = i_extra;
666
0
    fmt.i_id = 0;
667
0
    p_sys->es = es_out_Add( p_demux->out, &fmt );
668
0
    es_format_Clean( &fmt );
669
0
    if( p_sys->es == NULL )
670
0
    {
671
0
        webvtt_CloseDemux( p_this );
672
0
        return VLC_EGENERIC;
673
0
    }
674
675
0
    return VLC_SUCCESS;
676
0
}
677
678
int webvtt_OpenDemuxStream ( vlc_object_t *p_this )
679
0
{
680
0
    demux_t        *p_demux = (demux_t*)p_this;
681
0
    demux_sys_t    *p_sys;
682
683
0
    int i_ret = ProbeWEBVTT( p_demux );
684
0
    if( i_ret != VLC_SUCCESS )
685
0
        return i_ret;
686
687
0
    p_demux->pf_demux = DemuxStream;
688
0
    p_demux->pf_control = ControlStream;
689
690
0
    p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
691
0
    if( p_sys == NULL )
692
0
        return VLC_ENOMEM;
693
694
0
    p_sys->p_streamparser = webvtt_text_parser_New( p_demux,
695
0
                                          StreamParserGetCueHandler,
696
0
                                          StreamParserCueDoneHandler,
697
0
                                          NULL );
698
0
    if( !p_sys->p_streamparser )
699
0
    {
700
0
        webvtt_CloseDemux( p_this );
701
0
        return VLC_EGENERIC;
702
0
    }
703
704
0
    es_format_t fmt;
705
0
    es_format_Init( &fmt, SPU_ES, VLC_CODEC_WEBVTT );
706
0
    p_sys->es = es_out_Add( p_demux->out, &fmt );
707
0
    es_format_Clean( &fmt );
708
0
    if( p_sys->es == NULL )
709
0
    {
710
0
        webvtt_CloseDemux( p_this );
711
0
        return VLC_EGENERIC;
712
0
    }
713
714
0
    return VLC_SUCCESS;
715
0
}
716
717
/*****************************************************************************
718
 * Close: Close subtitle demux
719
 *****************************************************************************/
720
void webvtt_CloseDemux( vlc_object_t *p_this )
721
0
{
722
0
    demux_t *p_demux = (demux_t*)p_this;
723
0
    demux_sys_t *p_sys = p_demux->p_sys;
724
725
0
    for( size_t i=0; i< p_sys->cues.i_count; i++ )
726
0
        webvtt_cue_Clean( &p_sys->cues.p_array[i] );
727
0
    free( p_sys->cues.p_array );
728
729
0
    free( p_sys->index.p_array );
730
731
0
    if( p_sys->p_streamparser )
732
0
    {
733
0
        webvtt_text_parser_Feed( p_sys->p_streamparser, NULL );
734
0
        webvtt_text_parser_Delete( p_sys->p_streamparser );
735
0
    }
736
737
0
    free( p_sys->regions_headers.p_data );
738
0
    free( p_sys->styles_headers.p_data );
739
740
0
    free( p_sys );
741
0
}