item.c 23.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*****************************************************************************
 * item.c: input_item management
 *****************************************************************************
 * Copyright (C) 1998-2004 the VideoLAN team
 * $Id$
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
 *
 * 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.
 *
 * 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.
 *
19
20
21
 * 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.
22
23
 *****************************************************************************/

24
25
26
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
27
#include <assert.h>
28

29
#include <vlc_common.h>
30
#include <vlc_url.h>
31
32
33
#include "vlc_playlist.h"
#include "vlc_interface.h"

34
#include "item.h"
zorglub's avatar
zorglub committed
35

36
37
static void GuessType( input_item_t *p_item );

38
39
/** Stuff moved out of vlc_input.h -- FIXME: should probably not be inline
 * anyway. */
40
static inline void input_item_Init( vlc_object_t *p_o, input_item_t *p_i )
41
42
{
    memset( p_i, 0, sizeof(input_item_t) );
43

44
45
46
47
48
49
50
51
    p_i->psz_name = NULL;
    p_i->psz_uri = NULL;
    TAB_INIT( p_i->i_es, p_i->es );
    TAB_INIT( p_i->i_options, p_i->ppsz_options );
    p_i->optflagv = NULL, p_i->optflagc = 0;
    TAB_INIT( p_i->i_categories, p_i->pp_categories );

    p_i->i_type = ITEM_TYPE_UNKNOWN;
52
    p_i->b_fixed_name = true;
53
54
55
56

    p_i->p_stats = NULL;
    p_i->p_meta = NULL;

57
    vlc_mutex_init( &p_i->lock );
58
59
60
61
62
63
64
65
66
    vlc_event_manager_t * p_em = &p_i->event_manager;
    vlc_event_manager_init( p_em, p_i, p_o );
    vlc_event_manager_register_event_type( p_em, vlc_InputItemMetaChanged );
    vlc_event_manager_register_event_type( p_em, vlc_InputItemSubItemAdded );
    vlc_event_manager_register_event_type( p_em, vlc_InputItemDurationChanged );
    vlc_event_manager_register_event_type( p_em, vlc_InputItemPreparsedChanged );
    vlc_event_manager_register_event_type( p_em, vlc_InputItemNameChanged );
    vlc_event_manager_register_event_type( p_em, vlc_InputItemInfoChanged );
    vlc_event_manager_register_event_type( p_em, vlc_InputItemErrorWhenReadingChanged );
67
68
}

69
static inline void input_item_Clean( input_item_t *p_i )
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
{
    int i;

    vlc_event_manager_fini( &p_i->event_manager );

    free( p_i->psz_name );
    free( p_i->psz_uri );
    if( p_i->p_stats )
    {
        vlc_mutex_destroy( &p_i->p_stats->lock );
        free( p_i->p_stats );
    }

    if( p_i->p_meta )
        vlc_meta_Delete( p_i->p_meta );

    for( i = 0; i < p_i->i_options; i++ )
        free( p_i->ppsz_options[i] );
    TAB_CLEAN( p_i->i_options, p_i->ppsz_options );
    free( p_i->optflagv);

    for( i = 0; i < p_i->i_es; i++ )
    {
        es_format_Clean( p_i->es[i] );
        free( p_i->es[i] );
    }
    TAB_CLEAN( p_i->i_es, p_i->es );

    for( i = 0; i < p_i->i_categories; i++ )
    {
        info_category_t *p_category = p_i->pp_categories[i];
        int j;

        for( j = 0; j < p_category->i_infos; j++ )
        {
            struct info_t *p_info = p_category->pp_infos[j];

            free( p_info->psz_name);
            free( p_info->psz_value );
            free( p_info );
        }
        TAB_CLEAN( p_category->i_infos, p_category->pp_infos );

        free( p_category->psz_name );
        free( p_category );
    }
    TAB_CLEAN( p_i->i_categories, p_i->pp_categories );

    vlc_mutex_destroy( &p_i->lock );
}
120
121
122
123
124
125
126
127
128
129
void input_item_SetErrorWhenReading( input_item_t *p_i, bool b_error )
{
    bool b_changed;

    vlc_mutex_lock( &p_i->lock );

    b_changed = p_i->b_error_when_reading != b_error;
    p_i->b_error_when_reading = b_error;

    vlc_mutex_unlock( &p_i->lock );
130

131
132
133
134
135
136
137
138
139
140
    if( b_changed )
    {
        vlc_event_t event;

        event.type = vlc_InputItemErrorWhenReadingChanged;
        event.u.input_item_error_when_reading_changed.new_value = b_error;
        vlc_event_send( &p_i->event_manager, &event );
    }
}
void input_item_SetPreparsed( input_item_t *p_i, bool b_preparsed )
141
{
142
    bool b_send_event = false;
143

144
    vlc_mutex_lock( &p_i->lock );
145

146
147
    if( !p_i->p_meta )
        p_i->p_meta = vlc_meta_New();
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
    int i_new_status;
    if( b_preparsed )
        i_new_status = p_i->p_meta->i_status | ITEM_PREPARSED;
    else
        i_new_status = p_i->p_meta->i_status & ~ITEM_PREPARSED;
    if( p_i->p_meta->i_status != i_new_status )
    {
        p_i->p_meta->i_status = i_new_status;
        b_send_event = true;
    }

    vlc_mutex_unlock( &p_i->lock );

    if( b_send_event )
    {
        vlc_event_t event;
        event.type = vlc_InputItemPreparsedChanged;
        event.u.input_item_preparsed_changed.new_status = i_new_status;
        vlc_event_send( &p_i->event_manager, &event );
    }
}
void input_item_SetArtNotFound( input_item_t *p_i, bool b_not_found )
{
    vlc_mutex_lock( &p_i->lock );

    if( !p_i->p_meta )
        p_i->p_meta = vlc_meta_New();

    if( b_not_found )
        p_i->p_meta->i_status |= ITEM_ART_NOTFOUND;
    else
        p_i->p_meta->i_status &= ~ITEM_ART_NOTFOUND;

    vlc_mutex_unlock( &p_i->lock );
}
void input_item_SetArtFetched( input_item_t *p_i, bool b_art_fetched )
{
    vlc_mutex_lock( &p_i->lock );

    if( !p_i->p_meta )
        p_i->p_meta = vlc_meta_New();

    if( b_art_fetched )
        p_i->p_meta->i_status |= ITEM_ART_FETCHED;
    else
        p_i->p_meta->i_status &= ~ITEM_ART_FETCHED;

    vlc_mutex_unlock( &p_i->lock );
197
198
}

199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
void input_item_SetMeta( input_item_t *p_i, vlc_meta_type_t meta_type, const char *psz_val )
{
    vlc_event_t event;

    vlc_mutex_lock( &p_i->lock );
    if( !p_i->p_meta )
        p_i->p_meta = vlc_meta_New();
    vlc_meta_Set( p_i->p_meta, meta_type, psz_val );
    vlc_mutex_unlock( &p_i->lock );

    /* Notify interested third parties */
    event.type = vlc_InputItemMetaChanged;
    event.u.input_item_meta_changed.meta_type = meta_type;
    vlc_event_send( &p_i->event_manager, &event );
}

215
216
/* FIXME GRRRRRRRRRR args should be in the reverse order to be 
 * consistant with (nearly?) all or copy funcs */
217
void input_item_CopyOptions( input_item_t *p_parent,
218
                             input_item_t *p_child )
219
{
220
221
222
    vlc_mutex_lock( &p_parent->lock );

    for( int i = 0 ; i< p_parent->i_options; i++ )
223
    {
224
        if( !strcmp( p_parent->ppsz_options[i], "meta-file" ) )
225
            continue;
226

227
228
229
        input_item_AddOption( p_child,
                              p_parent->ppsz_options[i],
                              p_parent->optflagv[i] );
230
231
    }

232
    vlc_mutex_unlock( &p_parent->lock );
233
234
235
236
237
238
}

/* This won't hold the item, but can tell to interested third parties
 * Like the playlist, that there is a new sub item. With this design
 * It is not the input item's responsability to keep all the ref of
 * the input item children. */
239
void input_item_AddSubItem( input_item_t *p_parent, input_item_t *p_child )
240
{
241
    vlc_mutex_lock( &p_parent->lock );
242
243
244

    p_parent->i_type = ITEM_TYPE_PLAYLIST;

245
246
    vlc_mutex_unlock( &p_parent->lock );

247
    /* Notify interested third parties */
248
249
    vlc_event_t event;

250
251
252
253
254
    event.type = vlc_InputItemSubItemAdded;
    event.u.input_item_subitem_added.p_new_child = p_child;
    vlc_event_send( &p_parent->event_manager, &event );
}

255
bool input_item_HasErrorWhenReading( input_item_t *p_item )
256
{
257
258
259
260
261
262
263
    vlc_mutex_lock( &p_item->lock );

    bool b_error = p_item->b_error_when_reading;

    vlc_mutex_unlock( &p_item->lock );

    return b_error;
264
265
}

266
267
bool input_item_MetaMatch( input_item_t *p_i,
                           vlc_meta_type_t meta_type, const char *psz )
268
269
{
    vlc_mutex_lock( &p_i->lock );
270

271
272
273
274
275
    if( !p_i->p_meta )
    {
        vlc_mutex_unlock( &p_i->lock );
        return false;
    }
276
277
278
    const char *psz_meta = vlc_meta_Get( p_i->p_meta, meta_type );
    bool b_ret = psz_meta && strcasestr( psz_meta, psz );

279
280
    vlc_mutex_unlock( &p_i->lock );

281
    return b_ret;
282
283
}

284
char *input_item_GetMeta( input_item_t *p_i, vlc_meta_type_t meta_type )
285
286
287
288
289
290
291
292
293
{
    vlc_mutex_lock( &p_i->lock );

    if( !p_i->p_meta )
    {
        vlc_mutex_unlock( &p_i->lock );
        return NULL;
    }

294
    char *psz = NULL;
295
296
297
298
299
300
301
    if( vlc_meta_Get( p_i->p_meta, meta_type ) )
        psz = strdup( vlc_meta_Get( p_i->p_meta, meta_type ) );

    vlc_mutex_unlock( &p_i->lock );
    return psz;
}

302
303
304
305
306
307
308
309
/* Get the title of a given item or fallback to the name if the title is empty */
char *input_item_GetTitleFbName( input_item_t *p_item )
{
    char *psz_ret;
    vlc_mutex_lock( &p_item->lock );

    if( !p_item->p_meta )
    {
310
        psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
311
        vlc_mutex_unlock( &p_item->lock );
312
        return psz_ret;
313
314
    }

315
316
317
    const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
    if( !EMPTY_STR( psz_title ) )
        psz_ret = strdup( psz_title );
318
319
320
321
322
323
324
    else
        psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;

    vlc_mutex_unlock( &p_item->lock );
    return psz_ret;
}

325
char *input_item_GetName( input_item_t *p_item )
326
{
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
    vlc_mutex_lock( &p_item->lock );

    char *psz_name = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;

    vlc_mutex_unlock( &p_item->lock );
    return psz_name;
}
void input_item_SetName( input_item_t *p_item, const char *psz_name )
{
    vlc_mutex_lock( &p_item->lock );

    free( p_item->psz_name );
    p_item->psz_name = strdup( psz_name );

    vlc_mutex_unlock( &p_item->lock );
342
343
}

344
char *input_item_GetURI( input_item_t *p_i )
345
346
{
    vlc_mutex_lock( &p_i->lock );
347

348
    char *psz_s = p_i->psz_uri ? strdup( p_i->psz_uri ) : NULL;
349

350
351
352
    vlc_mutex_unlock( &p_i->lock );
    return psz_s;
}
353
void input_item_SetURI( input_item_t *p_i, const char *psz_uri )
354
355
{
    vlc_mutex_lock( &p_i->lock );
356

357
358
    free( p_i->psz_uri );
    p_i->psz_uri = strdup( psz_uri );
359

360
    GuessType( p_i );
361

362
363
    if( !p_i->psz_name && p_i->i_type == ITEM_TYPE_FILE )
    {
364
365
366
        const char *psz_filename = strrchr( p_i->psz_uri, '/' );

        if( psz_filename && *psz_filename == '/' )
367
368
369
            psz_filename++;
        if( psz_filename && *psz_filename )
            p_i->psz_name = strdup( psz_filename );
370
371
372
373

        /* Make the name more readable */
        if( p_i->psz_name )
            decode_URI( p_i->psz_name );
374
375
    }

376
    /* The name is NULL: fill it with everything except login and password */
377
    if( !p_i->psz_name )
378
    {
379
        int r;
380
381
382
383
384
        vlc_url_t url;
        vlc_UrlParse( &url, psz_uri, 0 );
        if( url.psz_protocol )
        {
            if( url.i_port > 0 )
385
                r=asprintf( &p_i->psz_name, "%s://%s:%d%s", url.psz_protocol,
386
387
388
                          url.psz_host, url.i_port,
                          url.psz_path ? url.psz_path : "" );
            else
389
                r=asprintf( &p_i->psz_name, "%s://%s%s", url.psz_protocol,
390
391
392
393
394
395
                          url.psz_host ? url.psz_host : "",
                          url.psz_path ? url.psz_path : "" );
        }
        else
        {
            if( url.i_port > 0 )
396
                r=asprintf( &p_i->psz_name, "%s:%d%s", url.psz_host, url.i_port,
397
398
                          url.psz_path ? url.psz_path : "" );
            else
399
                r=asprintf( &p_i->psz_name, "%s%s", url.psz_host,
400
401
402
                          url.psz_path ? url.psz_path : "" );
        }
        vlc_UrlClean( &url );
403
404
        if( -1==r )
            p_i->psz_name=NULL; /* recover from undefined value */
405
406
407
408

        /* Make the name more readable */
        if( p_i->psz_name )
            decode_URI( p_i->psz_name );
409
    }
410

411
412
413
    vlc_mutex_unlock( &p_i->lock );
}

414
mtime_t input_item_GetDuration( input_item_t *p_i )
415
416
{
    vlc_mutex_lock( &p_i->lock );
417

418
    mtime_t i_duration = p_i->i_duration;
419

420
421
422
423
    vlc_mutex_unlock( &p_i->lock );
    return i_duration;
}

424
void input_item_SetDuration( input_item_t *p_i, mtime_t i_duration )
425
{
426
    bool b_send_event = false;
427
428
429
430
431

    vlc_mutex_lock( &p_i->lock );
    if( p_i->i_duration != i_duration )
    {
        p_i->i_duration = i_duration;
432
        b_send_event = true;
433
434
435
    }
    vlc_mutex_unlock( &p_i->lock );

436
    if( b_send_event )
437
438
    {
        vlc_event_t event;
439

440
441
442
443
444
445
446
        event.type = vlc_InputItemDurationChanged;
        event.u.input_item_duration_changed.new_duration = i_duration;
        vlc_event_send( &p_i->event_manager, &event );
    }
}


447
bool input_item_IsPreparsed( input_item_t *p_item )
448
{
449
450
451
    vlc_mutex_lock( &p_item->lock );
    bool b_preparsed = p_item->p_meta ? ( p_item->p_meta->i_status & ITEM_PREPARSED ) != 0 : false;
    vlc_mutex_unlock( &p_item->lock );
452

453
    return b_preparsed;
454
455
}

456
bool input_item_IsArtFetched( input_item_t *p_item )
457
{
458
459
460
    vlc_mutex_lock( &p_item->lock );
    bool b_fetched = p_item->p_meta ? ( p_item->p_meta->i_status & ITEM_ART_FETCHED ) != 0 : false;
    vlc_mutex_unlock( &p_item->lock );
461

462
    return b_fetched;
463
464
}

465
static void input_item_Destroy ( gc_object_t *p_gc )
466
{
467
    input_item_t *p_item = vlc_priv( p_gc, input_item_t );
468

469
470
    input_item_Clean( p_item );
    free( p_item );
471
472
}

473
474
int input_item_AddOption( input_item_t *p_input, const char *psz_option,
                          unsigned flags )
475
{
476
477
    int err = VLC_SUCCESS;

478
    if( psz_option == NULL )
479
        return VLC_EGENERIC;
480
481

    vlc_mutex_lock( &p_input->lock );
482
    if (flags & VLC_INPUT_OPTION_UNIQUE)
483
    {
484
485
486
        for (int i = 0 ; i < p_input->i_options; i++)
            if( !strcmp( p_input->ppsz_options[i], psz_option ) )
                goto out;
487
488
    }

489
490
491
492
493
494
495
496
497
    uint8_t *flagv = realloc (p_input->optflagv, p_input->optflagc + 1);
    if (flagv == NULL)
    {
        err = VLC_ENOMEM;
        goto out;
    }
    p_input->optflagv = flagv;
    flagv[p_input->optflagc++] = flags;

498
499
500
501
    INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
                 p_input->i_options, strdup( psz_option ) );
out:
    vlc_mutex_unlock( &p_input->lock );
502
    return err;
503
}
504

505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/**
 * Get a info item from a given category in a given input item.
 *
 * \param p_i The input item to get info from
 * \param psz_cat String representing the category for the info
 * \param psz_name String representing the name of the desired info
 * \return A pointer to the string with the given info if found, or an
 *         empty string otherwise. The caller should free the returned
 *         pointer.
 */
char *input_item_GetInfo( input_item_t *p_i,
                          const char *psz_cat,
                          const char *psz_name )
{
    vlc_mutex_lock( &p_i->lock );

    for( int i = 0; i< p_i->i_categories; i++ )
    {
        const info_category_t *p_cat = p_i->pp_categories[i];

        if( !psz_cat || strcmp( p_cat->psz_name, psz_cat ) )
            continue;

        for( int j = 0; j < p_cat->i_infos; j++ )
        {
            if( !strcmp( p_cat->pp_infos[j]->psz_name, psz_name ) )
            {
                char *psz_ret = strdup( p_cat->pp_infos[j]->psz_value );
                vlc_mutex_unlock( &p_i->lock );
                return psz_ret;
            }
        }
    }
    vlc_mutex_unlock( &p_i->lock );
    return strdup( "" );
}

static int InputItemVaAddInfo( input_item_t *p_i,
                               const char *psz_cat,
                               const char *psz_name,
                               const char *psz_format, va_list args )
546
547
548
549
550
{
    int i;
    info_t *p_info = NULL;
    info_category_t *p_cat = NULL ;

551
    vlc_assert_locked( &p_i->lock );
552
553
554
555
556
557
558
559
560
561
562

    for( i = 0 ; i < p_i->i_categories ; i ++ )
    {
        if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
        {
            p_cat = p_i->pp_categories[i];
            break;
        }
    }
    if( !p_cat )
    {
563
        if( !(p_cat = malloc( sizeof(*p_cat) )) )
564
            return VLC_ENOMEM;
565

566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
        p_cat->psz_name = strdup( psz_cat );
        p_cat->i_infos = 0;
        p_cat->pp_infos = 0;
        INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
                     p_cat );
    }

    for( i = 0; i< p_cat->i_infos; i++ )
    {
        if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
        {
            p_info = p_cat->pp_infos[i];
            break;
        }
    }

    if( !p_info )
    {
584
        if( ( p_info = malloc( sizeof( *p_info ) ) ) == NULL )
585
            return VLC_ENOMEM;
586

587
588
589
590
591
        INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
        p_info->psz_name = strdup( psz_name );
    }
    else
    {
592
        free( p_info->psz_value );
593
594
    }

595
    if( vasprintf( &p_info->psz_value, psz_format, args ) == -1 )
596
        p_info->psz_value = NULL;
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625

    return p_info->psz_value ? VLC_SUCCESS : VLC_ENOMEM;
}

static int InputItemAddInfo( input_item_t *p_i,
                             const char *psz_cat,
                             const char *psz_name,
                             const char *psz_format, ... )
{
    va_list args;

    va_start( args, psz_format );
    const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
    va_end( args );

    return i_ret;
}

int input_item_AddInfo( input_item_t *p_i,
                        const char *psz_cat,
                        const char *psz_name,
                        const char *psz_format, ... )
{
    va_list args;

    vlc_mutex_lock( &p_i->lock );

    va_start( args, psz_format );
    const int i_ret = InputItemVaAddInfo( p_i, psz_cat, psz_name, psz_format, args );
626
627
628
629
    va_end( args );

    vlc_mutex_unlock( &p_i->lock );

630
631

    if( !i_ret )
632
633
634
635
636
637
    {
        vlc_event_t event;

        event.type = vlc_InputItemInfoChanged;
        vlc_event_send( &p_i->event_manager, &event );
    }
638
    return i_ret;
639
}
640

641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
int input_item_DelInfo( input_item_t *p_i,
                        const char *psz_cat,
                        const char *psz_name )
{
    info_category_t *p_cat = NULL;
    int i_cat;
    int i;

    vlc_mutex_lock( &p_i->lock );
    for( i_cat = 0; i_cat < p_i->i_categories; i_cat++ )
    {
        if( !strcmp( p_i->pp_categories[i_cat]->psz_name,
                     psz_cat ) )
        {
            p_cat = p_i->pp_categories[i_cat];
            break;
        }
    }
    if( p_cat == NULL )
    {
        vlc_mutex_unlock( &p_i->lock );
        return VLC_EGENERIC;
    }

    if( psz_name )
    {
        /* Remove a specific info */
        for( i = 0; i < p_cat->i_infos; i++ )
        {
            if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
            {
                free( p_cat->pp_infos[i]->psz_name );
ivoire's avatar
ivoire committed
673
                free( p_cat->pp_infos[i]->psz_value );
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
                free( p_cat->pp_infos[i] );
                REMOVE_ELEM( p_cat->pp_infos, p_cat->i_infos, i );
                break;
            }
        }
        if( i >= p_cat->i_infos )
        {
            vlc_mutex_unlock( &p_i->lock );
            return VLC_EGENERIC;
        }
    }
    else
    {
        /* Remove the complete categorie */
        for( i = 0; i < p_cat->i_infos; i++ )
        {
            free( p_cat->pp_infos[i]->psz_name );
ivoire's avatar
ivoire committed
691
            free( p_cat->pp_infos[i]->psz_value );
692
693
            free( p_cat->pp_infos[i] );
        }
ivoire's avatar
ivoire committed
694
        free( p_cat->pp_infos );
695
696
        REMOVE_ELEM( p_i->pp_categories, p_i->i_categories, i_cat );
    }
ivoire's avatar
ivoire committed
697
698
699
700

    // Free the category
    free( p_cat->psz_name );
    free( p_cat );
701
702
703
704
705
706
707
708
709
710
    vlc_mutex_unlock( &p_i->lock );


    vlc_event_t event;
    event.type = vlc_InputItemInfoChanged;
    vlc_event_send( &p_i->event_manager, &event );

    return VLC_SUCCESS;
}

711
712
713
714
715
716
void input_item_SetEpg( input_item_t *p_item,
                        const char *psz_epg, const vlc_epg_t *p_epg )
{
    input_item_DelInfo( p_item, psz_epg, NULL );

#ifdef HAVE_LOCALTIME_R
717
    vlc_mutex_lock( &p_item->lock );
718
719
720
721
722
723
724
725
726
727
728
729
730
    for( int i = 0; i < p_epg->i_event; i++ )
    {
        const vlc_epg_event_t *p_evt = p_epg->pp_event[i];
        time_t t_start = (time_t)p_evt->i_start;
        struct tm tm_start;
        char psz_start[128];

        localtime_r( &t_start, &tm_start );

        snprintf( psz_start, sizeof(psz_start), "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",
                  1900 + tm_start.tm_year, 1 + tm_start.tm_mon, tm_start.tm_mday,
                  tm_start.tm_hour, tm_start.tm_min, tm_start.tm_sec );
        if( p_evt->psz_short_description || p_evt->psz_description )
731
732
733
734
            InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s",
                              p_evt->psz_name,
                              p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
                              p_evt->psz_short_description ? p_evt->psz_short_description : p_evt->psz_description );
735
        else
736
737
738
739
740
741
742
743
744
745
746
747
            InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d)",
                              p_evt->psz_name,
                              p_evt->i_duration/60/60, (p_evt->i_duration/60)%60 );
    }
    vlc_mutex_unlock( &p_item->lock );

    if( p_epg->i_event > 0 )
    {
        vlc_event_t event;

        event.type = vlc_InputItemInfoChanged;
        vlc_event_send( &p_item->event_manager, &event );
748
749
750
751
    }
#endif
}

752

753
input_item_t *__input_item_NewExt( vlc_object_t *p_obj, const char *psz_uri,
754
755
756
                                  const char *psz_name,
                                  int i_options,
                                  const char *const *ppsz_options,
757
                                  unsigned i_option_flags,
758
                                  mtime_t i_duration )
759
{
760
    return input_item_NewWithType( p_obj, psz_uri, psz_name,
761
                                  i_options, ppsz_options, i_option_flags,
762
763
764
765
                                  i_duration, ITEM_TYPE_UNKNOWN );
}


766
input_item_t *input_item_NewWithType( vlc_object_t *p_obj, const char *psz_uri,
767
768
769
                                const char *psz_name,
                                int i_options,
                                const char *const *ppsz_options,
770
                                unsigned i_option_flags,
771
                                mtime_t i_duration,
772
773
                                int i_type )
{
774
    libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
775
    static vlc_mutex_t input_id_lock = VLC_STATIC_MUTEX;
776

777
778
779
    input_item_t* p_input = malloc( sizeof(input_item_t ) );
    if( !p_input )
        return NULL;
780

781
    input_item_Init( p_obj, p_input );
782
    vlc_gc_init( p_input, input_item_Destroy );
783

784
    vlc_mutex_lock( &input_id_lock );
785
    p_input->i_id = ++priv->i_last_input_id;
786
    vlc_mutex_unlock( &input_id_lock );
787

788
    p_input->b_fixed_name = false;
789
790

    p_input->i_type = i_type;
geal's avatar
geal committed
791
    p_input->b_prefers_tree = false;
792

793
794
    if( psz_uri )
        input_item_SetURI( p_input, psz_uri );
795

796
797
798
799
800
    if( i_type != ITEM_TYPE_UNKNOWN )
        p_input->i_type = i_type;

    if( psz_name )
        input_item_SetName( p_input, psz_name );
801

802
803
    p_input->i_duration = i_duration;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
804
    for( int i = 0; i < i_options; i++ )
805
        input_item_AddOption( p_input, ppsz_options[i], i_option_flags );
806
807
808
809
810
811
812
    return p_input;
}

/* Guess the type of the item using the beginning of the mrl */
static void GuessType( input_item_t *p_item)
{
    int i;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
813
    static struct { const char *psz_search; int i_type; }  types_array[] =
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
    {
        { "http", ITEM_TYPE_NET },
        { "dvd", ITEM_TYPE_DISC },
        { "cdda", ITEM_TYPE_CDDA },
        { "mms", ITEM_TYPE_NET },
        { "rtsp", ITEM_TYPE_NET },
        { "udp", ITEM_TYPE_NET },
        { "rtp", ITEM_TYPE_NET },
        { "vcd", ITEM_TYPE_DISC },
        { "v4l", ITEM_TYPE_CARD },
        { "dshow", ITEM_TYPE_CARD },
        { "pvr", ITEM_TYPE_CARD },
        { "dvb", ITEM_TYPE_CARD },
        { "qpsk", ITEM_TYPE_CARD },
        { "sdp", ITEM_TYPE_NET },
829
830
        { "ftp", ITEM_TYPE_NET },
        { "smb", ITEM_TYPE_NET },
831
832
833
        { NULL, 0 }
    };

834
    if( !p_item->psz_uri || !strstr( p_item->psz_uri, "://" ) )
835
836
837
838
839
    {
        p_item->i_type = ITEM_TYPE_FILE;
        return;
    }

840
841
842
843
844
845
846
847
848
    for( i = 0; types_array[i].psz_search != NULL; i++ )
    {
        if( !strncmp( p_item->psz_uri, types_array[i].psz_search,
                      strlen( types_array[i].psz_search ) ) )
        {
            p_item->i_type = types_array[i].i_type;
            return;
        }
    }
849
    p_item->i_type = ITEM_TYPE_FILE;
850
}