item.c 40.3 KB
Newer Older
1 2 3
/*****************************************************************************
 * item.c: input_item management
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 1998-2004 VLC authors and VideoLAN
5 6 7 8
 * $Id$
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12 13 14 15
 * (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
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser 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
#include <time.h>
29
#include <limits.h>
30

31
#include <vlc_common.h>
32
#include <vlc_url.h>
33
#include <vlc_interface.h>
34
#include <vlc_charset.h>
35

36
#include "item.h"
37
#include "info.h"
38

39 40 41 42 43 44 45
struct input_item_opaque
{
    struct input_item_opaque *next;
    void *value;
    char name[1];
};

46
static int GuessType( const input_item_t *p_item, bool *p_net );
47

48 49 50 51 52 53 54 55 56 57
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 );
58

59 60 61 62 63 64 65 66 67
    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 );
    }
}
68
void input_item_SignalPreparseEnded( input_item_t *p_i, int status )
69 70 71
{
    vlc_event_t event;
    event.type = vlc_InputItemPreparseEnded;
72
    event.u.input_item_preparse_ended.new_status = status;
73 74 75
    vlc_event_send( &p_i->event_manager, &event );
}

76
void input_item_SetPreparsed( input_item_t *p_i, bool b_preparsed )
77
{
78
    bool b_send_event = false;
79

80
    vlc_mutex_lock( &p_i->lock );
81

82 83
    if( !p_i->p_meta )
        p_i->p_meta = vlc_meta_New();
84

85 86
    int status = vlc_meta_GetStatus(p_i->p_meta);
    int new_status;
87
    if( b_preparsed )
88
        new_status = status | ITEM_PREPARSED;
89
    else
90 91
        new_status = status & ~ITEM_PREPARSED;
    if( status != new_status )
92
    {
93
        vlc_meta_SetStatus(p_i->p_meta, new_status);
94 95 96 97 98 99 100 101 102
        b_send_event = true;
    }

    vlc_mutex_unlock( &p_i->lock );

    if( b_send_event )
    {
        vlc_event_t event;
        event.type = vlc_InputItemPreparsedChanged;
103
        event.u.input_item_preparsed_changed.new_status = new_status;
104 105 106
        vlc_event_send( &p_i->event_manager, &event );
    }
}
107

108 109 110 111 112 113 114
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();

115
    int status = vlc_meta_GetStatus(p_i->p_meta);
116

117
    if( b_not_found )
118
        status |= ITEM_ART_NOTFOUND;
119
    else
120
        status &= ~ITEM_ART_NOTFOUND;
121

122
    vlc_meta_SetStatus(p_i->p_meta, status);
123

124 125
    vlc_mutex_unlock( &p_i->lock );
}
126

127 128 129 130 131 132 133
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();

134
    int status = vlc_meta_GetStatus(p_i->p_meta);
135

136
    if( b_art_fetched )
137
        status |= ITEM_ART_FETCHED;
138
    else
139 140 141
        status &= ~ITEM_ART_FETCHED;

    vlc_meta_SetStatus(p_i->p_meta, status);
142 143

    vlc_mutex_unlock( &p_i->lock );
144 145
}

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
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 );
}

162 163
void input_item_CopyOptions( input_item_t *p_child,
                             input_item_t *p_parent )
164
{
165 166
    char **optv = NULL;
    uint8_t *flagv = NULL;
167 168 169
    int optc = 0;
    char **optv_realloc = NULL;
    uint8_t *flagv_realloc = NULL;
170

171 172
    vlc_mutex_lock( &p_parent->lock );

173
    if( p_parent->i_options > 0 )
174
    {
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        optv = malloc( p_parent->i_options * sizeof (*optv) );
        if( likely(optv) )
            flagv = malloc( p_parent->i_options * sizeof (*flagv) );

        if( likely(flagv) )
        {
            for( int i = 0; i < p_parent->i_options; i++ )
            {
                char *psz_dup = strdup( p_parent->ppsz_options[i] );
                if( likely(psz_dup) )
                {
                    flagv[optc] = p_parent->optflagv[i];
                    optv[optc++] = psz_dup;
                }
            }
        }
191
    }
192

193
    vlc_mutex_unlock( &p_parent->lock );
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
    if( likely(optv && flagv && optc ) )
    {
        vlc_mutex_lock( &p_child->lock );

        if( INT_MAX - p_child->i_options >= optc &&
            SIZE_MAX / sizeof (*flagv) >= (size_t) (p_child->i_options + optc) )
            flagv_realloc = realloc( p_child->optflagv,
                                    (p_child->i_options + optc) * sizeof (*flagv) );
        if( likely(flagv_realloc) )
        {
            p_child->optflagv = flagv_realloc;
            if( SIZE_MAX / sizeof (*optv) >= (size_t) (p_child->i_options + optc) )
                optv_realloc = realloc( p_child->ppsz_options,
                                       (p_child->i_options + optc) * sizeof (*optv) );
            if( likely(optv_realloc) )
            {
                p_child->ppsz_options = optv_realloc;
                memcpy( p_child->ppsz_options + p_child->i_options, optv,
                        optc * sizeof (*optv) );
                memcpy( p_child->optflagv + p_child->i_options, flagv,
                        optc * sizeof (*flagv) );
                p_child->i_options += optc;
                p_child->optflagc += optc;
            }
        }

        vlc_mutex_unlock( &p_child->lock );
    }
223

224 225 226 227 228 229
    if( unlikely(!flagv_realloc || !optv_realloc) )
    {
        /* Didn't copy pointers, so need to free the strdup() */
        for( int i=0; i<optc; i++ )
            free( optv[i] );
    }
230 231 232

    free( flagv );
    free( optv );
233 234
}

235
static void post_subitems( input_item_node_t *p_node )
236
{
237 238 239 240 241 242 243 244 245
    for( int i = 0; i < p_node->i_children; i++ )
    {
        vlc_event_t event;
        event.type = vlc_InputItemSubItemAdded;
        event.u.input_item_subitem_added.p_new_child = p_node->pp_children[i]->p_item;
        vlc_event_send( &p_node->p_item->event_manager, &event );

        post_subitems( p_node->pp_children[i] );
    }
246 247
}

248 249
/* 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
250
 * It is not the input item's responsibility to keep all the ref of
251
 * the input item children. */
252
void input_item_PostSubItem( input_item_t *p_parent, input_item_t *p_child )
253
{
254 255
    input_item_node_t *p_node = input_item_node_Create( p_parent );
    input_item_node_AppendItem( p_node, p_child );
256
    input_item_node_PostAndDelete( p_node );
257 258
}

259
bool input_item_HasErrorWhenReading( input_item_t *p_item )
260
{
261 262 263 264 265 266 267
    vlc_mutex_lock( &p_item->lock );

    bool b_error = p_item->b_error_when_reading;

    vlc_mutex_unlock( &p_item->lock );

    return b_error;
268 269
}

270 271
bool input_item_MetaMatch( input_item_t *p_i,
                           vlc_meta_type_t meta_type, const char *psz )
272 273
{
    vlc_mutex_lock( &p_i->lock );
274

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

283 284
    vlc_mutex_unlock( &p_i->lock );

285
    return b_ret;
286 287
}

288
char *input_item_GetMeta( input_item_t *p_i, vlc_meta_type_t meta_type )
289 290 291 292 293 294 295 296 297
{
    vlc_mutex_lock( &p_i->lock );

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

298
    char *psz = NULL;
299 300 301 302 303 304 305
    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;
}

306 307 308 309 310 311 312 313
/* 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 )
    {
314
        psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;
315
        vlc_mutex_unlock( &p_item->lock );
316
        return psz_ret;
317 318
    }

319 320 321
    const char *psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title );
    if( !EMPTY_STR( psz_title ) )
        psz_ret = strdup( psz_title );
322 323 324 325 326 327 328
    else
        psz_ret = p_item->psz_name ? strdup( p_item->psz_name ) : NULL;

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

329
char *input_item_GetName( input_item_t *p_item )
330
{
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    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 );
346 347
}

348
char *input_item_GetURI( input_item_t *p_i )
349 350
{
    vlc_mutex_lock( &p_i->lock );
351

352
    char *psz_s = p_i->psz_uri ? strdup( p_i->psz_uri ) : NULL;
353

354 355 356
    vlc_mutex_unlock( &p_i->lock );
    return psz_s;
}
357

358
void input_item_SetURI( input_item_t *p_i, const char *psz_uri )
359
{
360
    assert( psz_uri );
361
#ifndef NDEBUG
362 363 364 365
    if( !strstr( psz_uri, "://" )
     || strchr( psz_uri, ' ' ) || strchr( psz_uri, '"' ) )
        fprintf( stderr, "Warning: %s(\"%s\"): file path instead of URL.\n",
                 __func__, psz_uri );
366
#endif
367
    vlc_mutex_lock( &p_i->lock );
368 369
    free( p_i->psz_uri );
    p_i->psz_uri = strdup( psz_uri );
370

371
    p_i->i_type = GuessType( p_i, &p_i->b_net );
372

373 374 375 376
    if( p_i->psz_name )
        ;
    else
    if( p_i->i_type == ITEM_TYPE_FILE || p_i->i_type == ITEM_TYPE_DIRECTORY )
377
    {
378 379 380
        const char *psz_filename = strrchr( p_i->psz_uri, '/' );

        if( psz_filename && *psz_filename == '/' )
381 382 383
            psz_filename++;
        if( psz_filename && *psz_filename )
            p_i->psz_name = strdup( psz_filename );
384 385 386

        /* Make the name more readable */
        if( p_i->psz_name )
387
        {
388
            vlc_uri_decode( p_i->psz_name );
389 390
            EnsureUTF8( p_i->psz_name );
        }
391
    }
392 393
    else
    {   /* Strip login and password from title */
394
        int r;
395
        vlc_url_t url;
396

397
        vlc_UrlParse( &url, psz_uri );
398 399 400
        if( url.psz_protocol )
        {
            if( url.i_port > 0 )
401
                r=asprintf( &p_i->psz_name, "%s://%s:%d%s", url.psz_protocol,
402 403 404
                          url.psz_host, url.i_port,
                          url.psz_path ? url.psz_path : "" );
            else
405
                r=asprintf( &p_i->psz_name, "%s://%s%s", url.psz_protocol,
406 407 408 409 410 411
                          url.psz_host ? url.psz_host : "",
                          url.psz_path ? url.psz_path : "" );
        }
        else
        {
            if( url.i_port > 0 )
412
                r=asprintf( &p_i->psz_name, "%s:%d%s", url.psz_host, url.i_port,
413 414
                          url.psz_path ? url.psz_path : "" );
            else
415
                r=asprintf( &p_i->psz_name, "%s%s", url.psz_host,
416 417 418
                          url.psz_path ? url.psz_path : "" );
        }
        vlc_UrlClean( &url );
419 420
        if( -1==r )
            p_i->psz_name=NULL; /* recover from undefined value */
421
    }
422

423 424 425
    vlc_mutex_unlock( &p_i->lock );
}

426
mtime_t input_item_GetDuration( input_item_t *p_i )
427 428
{
    vlc_mutex_lock( &p_i->lock );
429

430
    mtime_t i_duration = p_i->i_duration;
431

432 433 434 435
    vlc_mutex_unlock( &p_i->lock );
    return i_duration;
}

436
void input_item_SetDuration( input_item_t *p_i, mtime_t i_duration )
437
{
438
    bool b_send_event = false;
439 440 441 442 443

    vlc_mutex_lock( &p_i->lock );
    if( p_i->i_duration != i_duration )
    {
        p_i->i_duration = i_duration;
444
        b_send_event = true;
445 446 447
    }
    vlc_mutex_unlock( &p_i->lock );

448
    if( b_send_event )
449 450
    {
        vlc_event_t event;
451

452 453 454 455 456 457
        event.type = vlc_InputItemDurationChanged;
        event.u.input_item_duration_changed.new_duration = i_duration;
        vlc_event_send( &p_i->event_manager, &event );
    }
}

458 459 460 461 462 463 464 465 466 467 468
char *input_item_GetNowPlayingFb( input_item_t *p_item )
{
    char *psz_meta = input_item_GetMeta( p_item, vlc_meta_NowPlaying );
    if( !psz_meta || strlen( psz_meta ) == 0 )
    {
        free( psz_meta );
        return input_item_GetMeta( p_item, vlc_meta_ESNowPlaying );
    }

    return psz_meta;
}
469

470
bool input_item_IsPreparsed( input_item_t *p_item )
471
{
472
    vlc_mutex_lock( &p_item->lock );
473
    bool b_preparsed = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_PREPARSED ) != 0 : false;
474
    vlc_mutex_unlock( &p_item->lock );
475

476
    return b_preparsed;
477 478
}

479
bool input_item_IsArtFetched( input_item_t *p_item )
480
{
481
    vlc_mutex_lock( &p_item->lock );
482
    bool b_fetched = p_item->p_meta ? ( vlc_meta_GetStatus(p_item->p_meta) & ITEM_ART_FETCHED ) != 0 : false;
483
    vlc_mutex_unlock( &p_item->lock );
484

485
    return b_fetched;
486 487
}

488 489 490 491 492 493 494 495 496 497 498
bool input_item_ShouldPreparseSubItems( input_item_t *p_item )
{
    bool b_ret;

    vlc_mutex_lock( &p_item->lock );
    b_ret = p_item->i_preparse_depth == -1 ? true : p_item->i_preparse_depth > 0;
    vlc_mutex_unlock( &p_item->lock );

    return b_ret;
}

499
input_item_t *input_item_Hold( input_item_t *p_item )
500
{
501
    input_item_owner_t *owner = item_owner(p_item);
502

503 504 505 506 507 508 509 510 511 512 513
    atomic_fetch_add( &owner->refs, 1 );
    return p_item;
}

void input_item_Release( input_item_t *p_item )
{
    input_item_owner_t *owner = item_owner(p_item);

    if( atomic_fetch_sub(&owner->refs, 1) != 1 )
        return;

514 515 516 517 518 519 520 521 522 523 524 525 526
    vlc_event_manager_fini( &p_item->event_manager );

    free( p_item->psz_name );
    free( p_item->psz_uri );
    if( p_item->p_stats != NULL )
    {
        vlc_mutex_destroy( &p_item->p_stats->lock );
        free( p_item->p_stats );
    }

    if( p_item->p_meta != NULL )
        vlc_meta_Delete( p_item->p_meta );

527 528 529 530 531 532
    for( input_item_opaque_t *o = p_item->opaques, *next; o != NULL; o = next )
    {
        next = o->next;
        free( o );
    }

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
    for( int i = 0; i < p_item->i_options; i++ )
        free( p_item->ppsz_options[i] );
    TAB_CLEAN( p_item->i_options, p_item->ppsz_options );
    free( p_item->optflagv );

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

    for( int i = 0; i < p_item->i_epg; i++ )
        vlc_epg_Delete( p_item->pp_epg[i] );
    TAB_CLEAN( p_item->i_epg, p_item->pp_epg );

    for( int i = 0; i < p_item->i_categories; i++ )
        info_category_Delete( p_item->pp_categories[i] );
    TAB_CLEAN( p_item->i_categories, p_item->pp_categories );

553 554 555 556
    for( int i = 0; i < p_item->i_slaves; i++ )
        input_item_slave_Delete( p_item->pp_slaves[i] );
    TAB_CLEAN( p_item->i_slaves, p_item->pp_slaves );

557 558
    vlc_mutex_destroy( &p_item->lock );
    free( owner );
559 560
}

561 562
int input_item_AddOption( input_item_t *p_input, const char *psz_option,
                          unsigned flags )
563
{
564 565
    int err = VLC_SUCCESS;

566
    if( psz_option == NULL )
567
        return VLC_EGENERIC;
568 569

    vlc_mutex_lock( &p_input->lock );
570
    if (flags & VLC_INPUT_OPTION_UNIQUE)
571
    {
572 573 574
        for (int i = 0 ; i < p_input->i_options; i++)
            if( !strcmp( p_input->ppsz_options[i], psz_option ) )
                goto out;
575 576
    }

577 578 579 580 581 582 583 584 585
    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;

586 587 588 589
    INSERT_ELEM( p_input->ppsz_options, p_input->i_options,
                 p_input->i_options, strdup( psz_option ) );
out:
    vlc_mutex_unlock( &p_input->lock );
590
    return err;
591
}
592

593 594 595 596 597 598 599 600 601 602
int input_item_AddOptions( input_item_t *p_item, int i_options,
                           const char *const *ppsz_options,
                           unsigned i_flags )
{
    int i_ret = VLC_SUCCESS;
    for( int i = 0; i < i_options && i_ret == VLC_SUCCESS; i++ )
        i_ret = input_item_AddOption( p_item, ppsz_options[i], i_flags );
    return i_ret;
}

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
int input_item_AddOpaque(input_item_t *item, const char *name, void *value)
{
    assert(name != NULL);

    size_t namelen = strlen(name);
    input_item_opaque_t *entry = malloc(sizeof (*entry) + namelen);
    if (unlikely(entry == NULL))
        return VLC_ENOMEM;

    memcpy(entry->name, name, namelen + 1);
    entry->value = value;

    vlc_mutex_lock(&item->lock);
    entry->next = item->opaques;
    item->opaques = entry;
    vlc_mutex_unlock(&item->lock);
    return VLC_SUCCESS;
}

622 623 624 625 626 627 628 629
void input_item_ApplyOptions(vlc_object_t *obj, input_item_t *item)
{
    vlc_mutex_lock(&item->lock);
    assert(item->optflagc == (unsigned)item->i_options);

    for (unsigned i = 0; i < (unsigned)item->i_options; i++)
        var_OptionParse(obj, item->ppsz_options[i],
                        !!(item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED));
630 631 632 633 634 635 636

    for (const input_item_opaque_t *o = item->opaques; o != NULL; o = o->next)
    {
        var_Create(obj, o->name, VLC_VAR_ADDRESS);
        var_SetAddress(obj, o->name, o->value);
    }

637 638 639
    vlc_mutex_unlock(&item->lock);
}

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 673 674 675 676 677 678 679 680 681 682 683 684
bool input_item_slave_GetType(const char *psz_filename,
                              enum slave_type *p_slave_type)
{
    static const char *const ppsz_sub_exts[] = { SLAVE_SPU_EXTENSIONS, NULL };
    static const char *const ppsz_audio_exts[] = { SLAVE_AUDIO_EXTENSIONS, NULL };

    static struct {
        enum slave_type i_type;
        const char *const *ppsz_exts;
    } p_slave_list[] = {
        { SLAVE_TYPE_SPU, ppsz_sub_exts },
        { SLAVE_TYPE_AUDIO, ppsz_audio_exts },
    };

    const char *psz_ext = strrchr(psz_filename, '.');
    if (psz_ext == NULL || *(++psz_ext) == '\0')
        return false;

    for (unsigned int i = 0; i < sizeof(p_slave_list) / sizeof(*p_slave_list); ++i)
    {
        for (const char *const *ppsz_slave_ext = p_slave_list[i].ppsz_exts;
             *ppsz_slave_ext != NULL; ppsz_slave_ext++)
        {
            if (!strcasecmp(psz_ext, *ppsz_slave_ext))
            {
                *p_slave_type = p_slave_list[i].i_type;
                return true;
            }
        }
    }
    return false;
}

input_item_slave_t *input_item_slave_New(const char *psz_uri, enum slave_type i_type,
                                       enum slave_priority i_priority)
{
    if( !psz_uri )
        return NULL;

    input_item_slave_t *p_slave = malloc( sizeof( *p_slave ) + strlen( psz_uri ) + 1 );
    if( !p_slave )
        return NULL;

    p_slave->i_type = i_type;
    p_slave->i_priority = i_priority;
685
    p_slave->b_forced = false;
686 687 688 689 690 691 692
    strcpy( p_slave->psz_uri, psz_uri );

    return p_slave;
}

int input_item_AddSlave(input_item_t *p_item, input_item_slave_t *p_slave)
{
693 694
    if( p_item == NULL || p_slave == NULL
     || p_slave->i_priority < SLAVE_PRIORITY_MATCH_NONE )
695 696 697 698 699 700 701 702 703 704
        return VLC_EGENERIC;

    vlc_mutex_lock( &p_item->lock );

    INSERT_ELEM( p_item->pp_slaves, p_item->i_slaves, p_item->i_slaves, p_slave );

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

705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
static info_category_t *InputItemFindCat( input_item_t *p_item,
                                          int *pi_index, const char *psz_cat )
{
    vlc_assert_locked( &p_item->lock );
    for( int i = 0; i < p_item->i_categories && psz_cat; i++ )
    {
        info_category_t *p_cat = p_item->pp_categories[i];

        if( !strcmp( p_cat->psz_name, psz_cat ) )
        {
            if( pi_index )
                *pi_index = i;
            return p_cat;
        }
    }
    return NULL;
}

723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
/**
 * 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 );

739 740
    const info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
    if( p_cat )
741
    {
742 743
        info_t *p_info = info_category_FindInfo( p_cat, NULL, psz_name );
        if( p_info && p_info->psz_value )
744
        {
745 746 747
            char *psz_ret = strdup( p_info->psz_value );
            vlc_mutex_unlock( &p_i->lock );
            return psz_ret;
748 749 750 751 752 753 754 755 756 757
        }
    }
    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 )
758
{
759
    vlc_assert_locked( &p_i->lock );
760

761
    info_category_t *p_cat = InputItemFindCat( p_i, NULL, psz_cat );
762 763
    if( !p_cat )
    {
764 765
        p_cat = info_category_New( psz_cat );
        if( !p_cat )
766
            return VLC_ENOMEM;
767 768 769
        INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
                     p_cat );
    }
770 771 772 773
    info_t *p_info = info_category_VaAddInfo( p_cat, psz_name, psz_format, args );
    if( !p_info || !p_info->psz_value )
        return VLC_EGENERIC;
    return VLC_SUCCESS;
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
}

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 );
801 802 803 804
    va_end( args );

    vlc_mutex_unlock( &p_i->lock );

805 806

    if( !i_ret )
807 808 809 810 811 812
    {
        vlc_event_t event;

        event.type = vlc_InputItemInfoChanged;
        vlc_event_send( &p_i->event_manager, &event );
    }
813
    return i_ret;
814
}
815

816 817 818 819 820
int input_item_DelInfo( input_item_t *p_i,
                        const char *psz_cat,
                        const char *psz_name )
{
    vlc_mutex_lock( &p_i->lock );
821 822 823
    int i_cat;
    info_category_t *p_cat = InputItemFindCat( p_i, &i_cat, psz_cat );
    if( !p_cat )
824 825 826 827 828 829 830 831
    {
        vlc_mutex_unlock( &p_i->lock );
        return VLC_EGENERIC;
    }

    if( psz_name )
    {
        /* Remove a specific info */
832 833
        int i_ret = info_category_DeleteInfo( p_cat, psz_name );
        if( i_ret )
834 835 836 837 838 839 840 841
        {
            vlc_mutex_unlock( &p_i->lock );
            return VLC_EGENERIC;
        }
    }
    else
    {
        /* Remove the complete categorie */
842
        info_category_Delete( p_cat );
843 844 845 846 847 848 849 850 851 852 853
        REMOVE_ELEM( p_i->pp_categories, p_i->i_categories, i_cat );
    }
    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;
}
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
void input_item_ReplaceInfos( input_item_t *p_item, info_category_t *p_cat )
{
    vlc_mutex_lock( &p_item->lock );
    int i_cat;
    info_category_t *p_old = InputItemFindCat( p_item, &i_cat, p_cat->psz_name );
    if( p_old )
    {
        info_category_Delete( p_old );
        p_item->pp_categories[i_cat] = p_cat;
    }
    else
    {
        INSERT_ELEM( p_item->pp_categories, p_item->i_categories, p_item->i_categories,
                     p_cat );
    }
    vlc_mutex_unlock( &p_item->lock );


    vlc_event_t event;
    event.type = vlc_InputItemInfoChanged;
    vlc_event_send( &p_item->event_manager, &event );
}
void input_item_MergeInfos( input_item_t *p_item, info_category_t *p_cat )
{
    vlc_mutex_lock( &p_item->lock );
    info_category_t *p_old = InputItemFindCat( p_item, NULL, p_cat->psz_name );
    if( p_old )
    {
        for( int i = 0; i < p_cat->i_infos; i++ )
            info_category_ReplaceInfo( p_old, p_cat->pp_infos[i] );
        TAB_CLEAN( p_cat->i_infos, p_cat->pp_infos );
        info_category_Delete( p_cat );
    }
    else
    {
        INSERT_ELEM( p_item->pp_categories, p_item->i_categories, p_item->i_categories,
                     p_cat );
    }
    vlc_mutex_unlock( &p_item->lock );


    vlc_event_t event;
    event.type = vlc_InputItemInfoChanged;
    vlc_event_send( &p_item->event_manager, &event );
}
899

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
void input_item_SetEpgEvent( input_item_t *p_item, const vlc_epg_event_t *p_epg_evt )
{
    bool b_changed = false;
    vlc_mutex_lock( &p_item->lock );

    for( int i = 0; i < p_item->i_epg; i++ )
    {
        vlc_epg_t *p_epg = p_item->pp_epg[i];
        for( size_t j = 0; j < p_epg->i_event; j++ )
        {
            /* Same event can exist in more than one table */
            if( p_epg->pp_event[j]->i_id == p_epg_evt->i_id )
            {
                vlc_epg_event_t *p_dup = vlc_epg_event_Duplicate( p_epg_evt );
                if( p_dup )
                {
                    if( p_epg->p_current == p_epg->pp_event[j] )
                        p_epg->p_current = p_dup;
                    vlc_epg_event_Delete( p_epg->pp_event[j] );
                    p_epg->pp_event[j] = p_dup;
                    b_changed = true;
                }
                break;
            }
        }
    }
    vlc_mutex_unlock( &p_item->lock );

    if ( b_changed )
    {
        vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
        vlc_event_send( &p_item->event_manager, &event );
    }
}

935
//#define EPG_DEBUG
936
void input_item_SetEpg( input_item_t *p_item, const vlc_epg_t *p_update, bool b_current_source )
937
{
938 939 940 941
    vlc_epg_t *p_epg = vlc_epg_Duplicate( p_update );
    if( !p_epg )
        return;

942 943 944
    vlc_mutex_lock( &p_item->lock );

    /* */
945
    vlc_epg_t **pp_epg = NULL;
946 947
    for( int i = 0; i < p_item->i_epg; i++ )
    {
948 949
        if( p_item->pp_epg[i]->i_source_id == p_update->i_source_id &&
            p_item->pp_epg[i]->i_id == p_update->i_id )
François Cartegnie's avatar
François Cartegnie committed
950
        {
951
            pp_epg = &p_item->pp_epg[i];
François Cartegnie's avatar
François Cartegnie committed
952 953
            break;
        }
954 955
    }

956 957
    /* replace with new version */
    if( pp_epg )
958
    {
959
        vlc_epg_Delete( *pp_epg );
960 961
        if( *pp_epg == p_item->p_epg_table ) /* current table can have changed */
            p_item->p_epg_table = NULL;
962
        *pp_epg = p_epg;
963
    }
François Cartegnie's avatar
François Cartegnie committed
964 965
    else
    {
966
        TAB_APPEND( p_item->i_epg, p_item->pp_epg, p_epg );
François Cartegnie's avatar
François Cartegnie committed
967
    }
968

969 970 971
    if( b_current_source && p_epg->b_present )
        p_item->p_epg_table = p_epg;

972 973
    vlc_mutex_unlock( &p_item->lock );

974 975 976
    if( !p_epg )
        return;

977 978 979 980 981
#ifdef EPG_DEBUG
    char *psz_epg;
    if( asprintf( &psz_epg, "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
        goto signal;

982 983
    input_item_DelInfo( p_item, psz_epg, NULL );

984
    vlc_mutex_lock( &p_item->lock );
François Cartegnie's avatar
François Cartegnie committed
985
    for( size_t i = 0; i < p_epg->i_event; i++ )
986 987 988 989 990 991 992 993 994 995 996 997
    {
        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 )
998
            InputItemAddInfo( p_item, psz_epg, psz_start, "%s (%2.2d:%2.2d) - %s %s",
999 1000
                              p_evt->psz_name,
                              p_evt->i_duration/60/60, (p_evt->i_duration/60)%60,
1001 1002
                              p_evt->psz_short_description ? p_evt->psz_short_description : "" ,
                              p_evt->psz_description ? p_evt->psz_description : "" );
1003
        else
1004 1005 1006 1007 1008
            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 );
1009 1010 1011
    free( psz_epg );
signal:
#endif
1012 1013 1014 1015
    do {
        vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
        vlc_event_send( &p_item->event_manager, &event );
    } while(0);
1016 1017
}

1018
void input_item_ChangeEPGSource( input_item_t *p_item, int i_source_id )
Laurent Aimar's avatar
Laurent Aimar committed
1019 1020
{
    vlc_mutex_lock( &p_item->lock );
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
    p_item->p_epg_table = NULL;
    if( i_source_id > 0 )
    {
        /* Update pointer to current/next table in the full schedule */
        for( int i = 0; i < p_item->i_epg; i++ )
        {
            if( p_item->pp_epg[i]->i_source_id == i_source_id &&
                p_item->pp_epg[i]->b_present )
            {
                p_item->p_epg_table = p_item->pp_epg[i];
                break;
            }
        }
    }
Laurent Aimar's avatar
Laurent Aimar committed
1035
    vlc_mutex_unlock( &p_item->lock );
1036 1037 1038 1039 1040
}

void input_item_SetEpgOffline( input_item_t *p_item )
{
    input_item_ChangeEPGSource( p_item, -1 );
Laurent Aimar's avatar
Laurent Aimar committed
1041 1042 1043 1044

#ifdef EPG_DEBUG
    vlc_mutex_lock( &p_item->lock );
    const int i_epg_info = p_item->i_epg;
1045
    if( i_epg_info > 0 )
Laurent Aimar's avatar
Laurent Aimar committed
1046
    {
1047 1048 1049 1050 1051 1052 1053 1054
        char *ppsz_epg_info[i_epg_info];
        for( int i = 0; i < p_item->i_epg; i++ )
        {
            const vlc_epg_t *p_epg = p_item->pp_epg[i];
            if( asprintf( &ppsz_epg_info[i], "EPG %s", p_epg->psz_name ? p_epg->psz_name : "unknown" ) < 0 )
                ppsz_epg_info[i] = NULL;
        }
        vlc_mutex_unlock( &p_item->lock );
Laurent Aimar's avatar
Laurent Aimar committed
1055

1056 1057 1058 1059 1060 1061 1062
        for( int i = 0; i < i_epg_info; i++ )
        {
            if( !ppsz_epg_info[i] )
                continue;
            input_item_DelInfo( p_item, ppsz_epg_info[i], NULL );
            free( ppsz_epg_info[i] );
        }
Laurent Aimar's avatar
Laurent Aimar committed
1063
    }
1064 1065
    else
        vlc_mutex_unlock( &p_item->lock );
Laurent Aimar's avatar
Laurent Aimar committed
1066 1067 1068 1069 1070 1071
#endif

    vlc_event_t event = { .type = vlc_InputItemInfoChanged, };
    vlc_event_send( &p_item->event_manager, &event );
}

1072
input_item_t *
1073 1074
input_item_NewExt( const char *psz_uri, const char *psz_name,
                   mtime_t duration, int type, enum input_item_net_type i_net )
1075
{
1076 1077
    input_item_owner_t *owner = calloc( 1, sizeof( *owner ) );
    if( unlikely(owner == NULL) )
1078
        return NULL;
1079

1080 1081
    atomic_init( &owner->refs, 1 );

1082
    input_item_t *p_input = &owner->item;
1083
    vlc_event_manager_t * p_em = &p_input->event_manager;
1084

1085
    vlc_mutex_init( &p_input->lock );
1086

1087 1088 1089
    p_input->psz_name = NULL;
    if( psz_name )
        input_item_SetName( p_input, psz_name );
1090

1091
    p_input->psz_uri = NULL;
1092 1093
    if( psz_uri )
        input_item_SetURI( p_input, psz_uri );
1094
    else
1095
    {
1096
        p_input->i_type = ITEM_TYPE_UNKNOWN;
1097 1098
        p_input->b_net = false;
    }
1099

1100 1101 1102
    TAB_INIT( p_input->i_options, p_input->ppsz_options );
    p_input->optflagc = 0;
    p_input->optflagv = NULL;
1103
    p_input->opaques = NULL;
1104

1105 1106 1107 1108 1109 1110
    p_input->i_duration = duration;
    TAB_INIT( p_input->i_categories, p_input->pp_categories );
    TAB_INIT( p_input->i_es, p_input->es );
    p_input->p_stats = NULL;
    p_input->p_meta = NULL;
    TAB_INIT( p_input->i_epg, p_input->pp_epg );
1111
    TAB_INIT( p_input->i_slaves, p_input->pp_slaves );
1112

1113 1114 1115 1116 1117 1118 1119 1120 1121
    vlc_event_manager_init( p_em, p_input );
    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_InputItemSubItemTreeAdded );
    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 );
1122
    vlc_event_manager_register_event_type( p_em, vlc_InputItemPreparseEnded );
1123

1124 1125 1126
    if( type != ITEM_TYPE_UNKNOWN )
        p_input->i_type = type;
    p_input->b_error_when_reading = false;
1127

1128 1129
    if( i_net != ITEM_NET_UNKNOWN )
        p_input->b_net = i_net == ITEM_NET;
1130 1131 1132
    return p_input;
}

1133
input_item_t *input_item_Copy( input_item_t *p_input )
1134
{
1135 1136
    vlc_meta_t *meta = NULL;
    input_item_t *item;
1137
    bool b_net;
1138

1139
    vlc_mutex_lock( &p_input->lock );
1140

1141 1142 1143
    item = input_item_NewExt( p_input->psz_uri, p_input->psz_name,
                              p_input->i_duration, p_input->i_type,
                              ITEM_NET_UNKNOWN );
1144
    if( likely(item != NULL) && p_input->p_meta != NULL )
1145
    {
1146 1147
        meta = vlc_meta_New();
        vlc_meta_Merge( meta, p_input->p_meta );
1148
    }
1149
    b_net = p_input->b_net;
1150 1151
    vlc_mutex_unlock( &p_input->lock );

1152 1153
    if( likely(item != NULL) )
    {   /* No need to lock; no other thread has seen this new item yet. */
1154
        input_item_CopyOptions( item, p_input );
1155
        item->p_meta = meta;
1156
        item->b_net = b_net;
1157 1158 1159
    }

    return item;
1160 1161
}

1162
struct item_type_entry
1163
{
1164
    const char *psz_scheme;
1165
    uint8_t    i_type;
1166
    bool       b_net;
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
};

static int typecmp( const void *key, const void *entry )
{
    const struct item_type_entry *type = entry;
    const char *uri = key, *scheme = type->psz_scheme;

    return strncmp( uri, scheme, strlen( scheme ) );
}

/* Guess the type of the item using the beginning of the mrl */
1178
static int GuessType( const input_item_t *p_item, bool *p_net )
1179 1180 1181
{
    static const struct item_type_entry tab[] =
    {   /* /!\ Alphabetical order /!\ */
1182
        /* Short match work, not just exact match */
1183 1184 1185
        { "alsa",   ITEM_TYPE_CARD, false },
        { "atsc",   ITEM_TYPE_CARD, false },
        { "bd",     ITEM_TYPE_DISC, false },
1186
        { "bluray", ITEM_TYPE_DISC, false },
1187
        { "cable",  ITEM_TYPE_CARD, false },
1188
        { "cdda",   ITEM_TYPE_DISC, false },
1189 1190 1191 1192 1193 1194
        { "cqam",   ITEM_TYPE_CARD, false },
        { "dc1394", ITEM_TYPE_CARD, false },
        { "dccp",   ITEM_TYPE_STREAM, true },
        { "deckli", ITEM_TYPE_CARD, false }, /* decklink */
        { "dir",    ITEM_TYPE_DIRECTORY, false },
        { "dshow",  ITEM_TYPE_CARD, false },
1195
        { "dtv",    ITEM_TYPE_CARD, false },
1196 1197 1198 1199
        { "dvb",    ITEM_TYPE_CARD, false },
        { "dvd",    ITEM_TYPE_DISC, false },
        { "eyetv",  ITEM_TYPE_CARD, false },
        { "fd",     ITEM_TYPE_UNKNOWN, false },
1200
        { "file",   ITEM_TYPE_FILE, false },
1201 1202 1203 1204
        { "ftp",    ITEM_TYPE_FILE, true },
        { "http",   ITEM_TYPE_FILE, true },
        { "icyx",   ITEM_TYPE_STREAM, true },
        { "imem",   ITEM_TYPE_UNKNOWN, false },
1205
        { "isdb-",  ITEM_TYPE_CARD, false },
1206 1207 1208 1209 1210 1211
        { "itpc",   ITEM_TYPE_PLAYLIST, true },
        { "jack",   ITEM_TYPE_CARD, false },
        { "linsys", ITEM_TYPE_CARD, false },
        { "live",   ITEM_TYPE_STREAM, true }, /* livedotcom */
        { "mms",    ITEM_TYPE_STREAM, true },
        { "mtp",    ITEM_TYPE_DISC, false },
Thomas Guillem's avatar
Thomas Guillem committed
1212
        { "nfs",    ITEM_TYPE_FILE, true },
1213 1214 1215
        { "ofdm",   ITEM_TYPE_CARD, false },
        { "oss",    ITEM_TYPE_CARD, false },
        { "pnm",    ITEM_TYPE_STREAM, true },
1216
        { "pulse",  ITEM_TYPE_CARD, false },
1217 1218 1219
        { "qam",    ITEM_TYPE_CARD, false },
        { "qpsk",   ITEM_TYPE_CARD, false },
        { "qtcapt", ITEM_TYPE_CARD, false }, /* qtcapture */
1220
        { "qtsound",ITEM_TYPE_CARD, false },
1221 1222
        { "raw139", ITEM_TYPE_CARD, false }, /* raw1394 */
        { "rt",     ITEM_TYPE_STREAM, true }, /* rtp, rtsp, rtmp */
1223
        { "satell", ITEM_TYPE_CARD, false }, /* satellite */
Julian Scheel's avatar
Julian Scheel committed
1224
        { "satip",  ITEM_TYPE_STREAM, true }, /* satellite over ip */
1225 1226 1227 1228 1229
        { "screen", ITEM_TYPE_CARD, false },
        { "sdp",    ITEM_TYPE_STREAM, true },
        { "sftp",   ITEM_TYPE_FILE, true },
        { "shm",    ITEM_TYPE_CARD, false },
        { "smb",    ITEM_TYPE_FILE, true },
1230
        { "stream", ITEM_TYPE_STREAM, false },
1231 1232 1233 1234 1235
        { "svcd",   ITEM_TYPE_DISC, false },
        { "tcp",    ITEM_TYPE_STREAM, true },
        { "terres", ITEM_TYPE_CARD, false }, /* terrestrial */
        { "udp",    ITEM_TYPE_STREAM, true },  /* udplite too */
        { "unsv",   ITEM_TYPE_STREAM, true },
1236
        { "upnp",   ITEM_TYPE_FILE, true },
1237 1238 1239
        { "usdigi", ITEM_TYPE_CARD, false }, /* usdigital */
        { "v4l",    ITEM_TYPE_CARD, false },
        { "vcd",    ITEM_TYPE_DISC, false },
1240 1241
        { "vdr",    ITEM_TYPE_STREAM, true },
        { "wasapi", ITEM_TYPE_CARD, false },
1242
        { "window", ITEM_TYPE_CARD, false },
1243
    };
1244 1245 1246

#ifndef NDEBUG
    for( size_t i = 1; i < ARRAY_SIZE( tab ); i++ )
1247
        assert( typecmp( (tab + i)->psz_scheme, tab + i - 1 ) > 0 );
1248 1249
#endif

1250
    *p_net = false;
1251

1252 1253 1254 1255 1256 1257 1258 1259
    if( strstr( p_item->psz_uri, "://" ) == NULL )
        return ITEM_TYPE_UNKNOWN; /* invalid URI */

    const struct item_type_entry *e =
        bsearch( p_item->psz_uri, tab, ARRAY_SIZE( tab ),
                 sizeof( tab[0] ), typecmp );
    if( e == NULL )
        return ITEM_TYPE_UNKNOWN;
1260

1261 1262
    *p_net = e->b_net;
    return e->i_type;
1263
}
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282

input_item_node_t *input_item_node_Create( input_item_t *p_input )
{
    input_item_node_t* p_node = malloc( sizeof( input_item_node_t ) );
    if( !p_node )
        return NULL;

    assert( p_input );

    p_node->p_item = p_input;
    vlc_gc_incref( p_input );

    p_node->p_parent = NULL;
    p_node->i_children = 0;
    p_node->pp_children = NULL;

    return p_node;
}

1283
static void RecursiveNodeDelete( input_item_node_t *p_node )
1284
{
1285 1286
    for( int i = 0; i < p_node->i_children; i++ )
        RecursiveNodeDelete( p_node->pp_children[i] );
1287

1288 1289 1290
    vlc_gc_decref( p_node->p_item );
    free( p_node->pp_children );
    free( p_node );
1291
}
1292

1293 1294
void input_item_node_Delete( input_item_node_t *p_node )
{
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
    if( p_node->p_parent )
        for( int i = 0; i < p_node->p_parent->i_children; i++ )
            if( p_node->p_parent->pp_children[i] == p_node )
            {
                REMOVE_ELEM( p_node->p_parent->pp_children,
                        p_node->p_parent->i_children,
                        i );
                break;
            }

    RecursiveNodeDelete( p_node );
1306 1307 1308 1309
}

input_item_node_t *input_item_node_AppendItem( input_item_node_t *p_node, input_item_t *p_item )
{
1310
    int i_preparse_depth;
1311 1312
    input_item_node_t *p_new_child = input_item_node_Create( p_item );
    if( !p_new_child ) return NULL;
1313 1314 1315

    vlc_mutex_lock( &p_node->p_item->lock );
    i_preparse_depth = p_node->p_item->i_preparse_depth;
1316 1317 1318
    vlc_mutex_unlock( &p_node->p_item->lock );

    vlc_mutex_lock( &p_item->lock );
1319 1320 1321 1322 1323
    p_item->i_preparse_depth = i_preparse_depth > 0 ?
                               i_preparse_depth -1 :
                               i_preparse_depth;
    vlc_mutex_unlock( &p_item->lock );

1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
    input_item_node_AppendNode( p_node, p_new_child );
    return p_new_child;
}

void input_item_node_AppendNode( input_item_node_t *p_parent, input_item_node_t *p_child )
{
    assert( p_parent && p_child && p_child->p_parent == NULL );
    INSERT_ELEM( p_parent->pp_children,
                 p_parent->i_children,
                 p_parent->i_children,
                 p_child );
    p_child->p_parent = p_parent;
}
1337 1338 1339

void input_item_node_PostAndDelete( input_item_node_t *p_root )
{
1340
    post_subitems( p_root );
1341

1342 1343 1344 1345
    vlc_event_t event;
    event.type = vlc_InputItemSubItemTreeAdded;
    event.u.input_item_subitem_tree_added.p_root = p_root;
    vlc_event_send( &p_root->p_item->event_manager, &event );
1346

1347
    input_item_node_Delete( p_root );
1348
}
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380

/* Called by es_out when a new Elementary Stream is added or updated. */
void input_item_UpdateTracksInfo(input_item_t *item, const es_format_t *fmt)
{
    int i;
    es_format_t *fmt_copy = malloc(sizeof *fmt_copy);
    if (!fmt_copy)
        return;

    es_format_Copy(fmt_copy, fmt);
    /* XXX: we could free p_extra to save memory, we will likely not need
     * the decoder specific data */

    vlc_mutex_lock( &item->lock );

    for( i = 0; i < item->i_es; i++ )
    {
        if (item->es[i]->i_id != fmt->i_id)
            continue;

        /* We've found the right ES, replace it */
        es_format_Clean(item->es[i]);
        free(item->es[i]);
        item->es[i] = fmt_copy;
        vlc_mutex_unlock( &item->lock );
        return;
    }

    /* ES not found, insert it */
    TAB_APPEND(item->i_es, item->es, fmt_copy);
    vlc_mutex_unlock( &item->lock );
}