~thopiekar/+junk/vlc-2.0.8-github_community

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*****************************************************************************
 * services_discovery.c : Manage playlist services_discovery modules
 *****************************************************************************
 * Copyright (C) 1999-2004 VLC authors and VideoLAN
 * $Id: d8b93d6f12209a043d1d1fd673c94885698d0198 $
 *
 * 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 Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 * 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.
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>

#include <vlc_common.h>
#include "vlc_playlist.h"
#include "vlc_events.h"
#include <vlc_services_discovery.h>
#include <vlc_probe.h>
#include <vlc_modules.h>
#include "playlist_internal.h"
#include "../libvlc.h"

typedef struct
{
    char *name;
    char *longname;
    int category;
} vlc_sd_probe_t;

int vlc_sd_probe_Add (vlc_probe_t *probe, const char *name,
                      const char *longname, int category)
{
    vlc_sd_probe_t names = { strdup(name), strdup(longname), category };

    if (unlikely (names.name == NULL || names.longname == NULL
               || vlc_probe_add (probe, &names, sizeof (names))))
    {
        free (names.name);
        free (names.longname);
        return VLC_ENOMEM;
    }
    return VLC_PROBE_CONTINUE;
}

#undef vlc_sd_GetNames

/**
 * Gets the list of available services discovery plugins.
 */
char **vlc_sd_GetNames (vlc_object_t *obj, char ***pppsz_longnames, int **pp_categories)
{
    size_t count;
    vlc_sd_probe_t *tab = vlc_probe (obj, "services probe", &count);

    if (count == 0)
    {
        free (tab);
        return NULL;
    }

    char **names = malloc (sizeof(char *) * (count + 1));
    char **longnames = malloc (sizeof(char *) * (count + 1));
    int *categories = malloc(sizeof(int) * (count + 1));

    if (unlikely (names == NULL || longnames == NULL || categories == NULL))
        abort();
    for( size_t i = 0; i < count; i++ )
    {
        names[i] = tab[i].name;
        longnames[i] = tab[i].longname;
        categories[i] = tab[i].category;
    }
    free (tab);
    names[count] = longnames[count] = NULL;
    categories[count] = 0;
    *pppsz_longnames = longnames;
    if( pp_categories ) *pp_categories = categories;
    else free( categories );
    return names;
}


static void services_discovery_Destructor ( vlc_object_t *p_obj );

/*
 * Services discovery
 * Basically you just listen to Service discovery event through the
 * sd's event manager.
 * That's how the playlist get's Service Discovery information
 */

/*******************************************************************//**
 * Create a Service discovery
 ***********************************************************************/
services_discovery_t *vlc_sd_Create( vlc_object_t *p_super,
                                     const char *cfg )
{
    services_discovery_t *p_sd;

    p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), "services discovery" );
    if( !p_sd )
        return NULL;
    free(config_ChainCreate( &p_sd->psz_name, &p_sd->p_cfg, cfg ));

    vlc_event_manager_t *em = &p_sd->event_manager;
    vlc_event_manager_init( em, p_sd );
    vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryItemAdded);
    vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryItemRemoved);
    vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryStarted);
    vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryEnded);

    vlc_object_set_destructor( p_sd, services_discovery_Destructor );
    return p_sd;
}

/*******************************************************************//**
 * Start a Service Discovery
 ***********************************************************************/
bool vlc_sd_Start ( services_discovery_t * p_sd )
{
    assert(!p_sd->p_module);

    p_sd->p_module = module_need( p_sd, "services_discovery",
                                  p_sd->psz_name, true );
    if( p_sd->p_module == NULL )
    {
        msg_Err( p_sd, "no suitable services discovery module" );
        return false;
    }

    vlc_event_t event = {
        .type = vlc_ServicesDiscoveryStarted
    };
    vlc_event_send( &p_sd->event_manager, &event );
    return true;
}

/*******************************************************************//**
 * Stop a Service Discovery
 ***********************************************************************/
void vlc_sd_Stop ( services_discovery_t * p_sd )
{
    vlc_event_t event = {
        .type = vlc_ServicesDiscoveryEnded
    };

    vlc_event_send( &p_sd->event_manager, &event );

    module_unneed( p_sd, p_sd->p_module );
    p_sd->p_module = NULL;
}

/*******************************************************************//**
 * Destroy a Service Discovery
 ***********************************************************************/
void vlc_sd_Destroy( services_discovery_t *p_sd )
{
    config_ChainDestroy( p_sd->p_cfg );
    free( p_sd->psz_name );
    vlc_object_release( p_sd );
}

/*******************************************************************//**
 * Destructor of the Service Discovery
 ***********************************************************************/
static void services_discovery_Destructor ( vlc_object_t *p_obj )
{
    services_discovery_t * p_sd = (services_discovery_t *)p_obj;
    assert(!p_sd->p_module); /* Forgot to call Stop */
    vlc_event_manager_fini( &p_sd->event_manager );
}

/*******************************************************************//**
 * Get the Localized Name
 *
 * This is useful for interfaces and libVLC
 ***********************************************************************/
char *
services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
{
    return strdup( module_get_name( p_sd->p_module, true ) );
}

/*******************************************************************//**
 * Getter for the EventManager
 *
 * You can receive event notification
 * This is the preferred way to get new items
 ***********************************************************************/
vlc_event_manager_t *
services_discovery_EventManager ( services_discovery_t * p_sd )
{
    return &p_sd->event_manager;
}

/*******************************************************************//**
 * Add an item to the Service Discovery listing
 ***********************************************************************/
void
services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
                             const char * psz_category )
{
    vlc_event_t event;
    event.type = vlc_ServicesDiscoveryItemAdded;
    event.u.services_discovery_item_added.p_new_item = p_item;
    event.u.services_discovery_item_added.psz_category = psz_category;

    vlc_event_send( &p_sd->event_manager, &event );
}

/*******************************************************************//**
 * Remove an item from the Service Discovery listing
 ***********************************************************************/
void
services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
{
    vlc_event_t event;
    event.type = vlc_ServicesDiscoveryItemRemoved;
    event.u.services_discovery_item_removed.p_item = p_item;

    vlc_event_send( &p_sd->event_manager, &event );
}

/*
 * Playlist - Services discovery bridge
 */

struct vlc_sd_internal_t
{
    /* the playlist items for category and onelevel */
    playlist_item_t      *p_node;
    services_discovery_t *p_sd; /**< Loaded service discovery modules */
    char                 *psz_name;
};

 /* A new item has been added to a certain sd */
static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
{
    input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
    const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
    playlist_item_t * p_parent = user_data;
    playlist_t * p_playlist = p_parent->p_playlist;

    msg_Dbg( p_playlist, "Adding %s in %s",
                p_input->psz_name ? p_input->psz_name : "(null)",
                psz_cat ? psz_cat : "(null)" );

    PL_LOCK;
    /* If p_parent is in root category (this is clearly a hack) and we have a cat */
    if( !EMPTY_STR(psz_cat) )
    {
        /* */
        playlist_item_t * p_cat;
        p_cat = playlist_ChildSearchName( p_parent, psz_cat );
        if( !p_cat )
        {
            p_cat = playlist_NodeCreate( p_playlist, psz_cat,
                                         p_parent, PLAYLIST_END, 0, NULL );
            p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
        }
        p_parent = p_cat;
    }

    playlist_NodeAddInput( p_playlist, p_input, p_parent,
                           PLAYLIST_APPEND, PLAYLIST_END,
                           pl_Locked );
    PL_UNLOCK;
}

 /* A new item has been removed from a certain sd */
static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
{
    input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
    playlist_item_t * p_sd_node = user_data;
    playlist_t *p_playlist = p_sd_node->p_playlist;

    PL_LOCK;
    playlist_item_t *p_item =
        playlist_ItemFindFromInputAndRoot( p_playlist, p_input,
                                           p_sd_node, false );
    if( !p_item )
    {
        PL_UNLOCK; return;
    }
    playlist_item_t *p_parent = p_item->p_parent;
    /* if the item was added under a category and the category node
       becomes empty, delete that node as well */
    if( p_parent->i_children > 1 || p_parent == p_sd_node )
        playlist_DeleteItem( p_playlist, p_item, true );
    else
        playlist_NodeDelete( p_playlist, p_parent, true, true );
    PL_UNLOCK;
}

int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,
                                   const char *psz_name )
{
    /* Perform the addition */
    services_discovery_t *p_sd;

    msg_Dbg( p_playlist, "adding services_discovery %s...", psz_name );
    p_sd = vlc_sd_Create( VLC_OBJECT(p_playlist), psz_name );
    if( !p_sd )
        return VLC_ENOMEM;

    /* Free in playlist_ServicesDiscoveryRemove */
    vlc_sd_internal_t * p_sds = malloc( sizeof(*p_sds) );
    if( !p_sds )
    {
        vlc_sd_Destroy( p_sd );
        return VLC_ENOMEM;
    }

    playlist_item_t *p_node;

    /* Look for configuration chain "longname" */
    const char *psz_longname = "?";
    if( p_sd->p_cfg )
    {
        config_chain_t *cfg = p_sd->p_cfg;
        while( cfg )
        {
            if( cfg->psz_name && !strcmp( cfg->psz_name, "longname" ) )
            {
                psz_longname = cfg->psz_value;
                break;
            }
            cfg = cfg->p_next;
        }
    }

    PL_LOCK;
    p_node = playlist_NodeCreate( p_playlist, psz_longname,
                                  p_playlist->p_root, PLAYLIST_END, 0, NULL );
    PL_UNLOCK;

    vlc_event_manager_t *em = services_discovery_EventManager( p_sd );
    vlc_event_attach( em, vlc_ServicesDiscoveryItemAdded,
                      playlist_sd_item_added, p_node );

    vlc_event_attach( em, vlc_ServicesDiscoveryItemRemoved,
                      playlist_sd_item_removed, p_node );

    if( !vlc_sd_Start( p_sd ) )
    {
        vlc_sd_Destroy( p_sd );
        free( p_sds );
        return VLC_EGENERIC;
    }

    p_sds->p_sd = p_sd;
    p_sds->p_node = p_node;
    p_sds->psz_name = strdup( psz_name );

    PL_LOCK;
    TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
    PL_UNLOCK;

    return VLC_SUCCESS;
}

int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
                                      const char *psz_name )
{
    playlist_private_t *priv = pl_priv( p_playlist );
    vlc_sd_internal_t * p_sds = NULL;

    PL_LOCK;
    for( int i = 0; i < priv->i_sds; i++ )
    {
        if( !strcmp( psz_name, priv->pp_sds[i]->psz_name ) )
        {
            p_sds = priv->pp_sds[i];
            REMOVE_ELEM( priv->pp_sds, priv->i_sds, i );
            break;
        }
    }
    PL_UNLOCK;

    if( !p_sds )
    {
        msg_Warn( p_playlist, "discovery %s is not loaded", psz_name );
        return VLC_EGENERIC;
    }

    services_discovery_t *p_sd = p_sds->p_sd;
    assert( p_sd );

    vlc_sd_Stop( p_sd );

    vlc_event_detach( services_discovery_EventManager( p_sd ),
                        vlc_ServicesDiscoveryItemAdded,
                        playlist_sd_item_added,
                        p_sds->p_node );

    vlc_event_detach( services_discovery_EventManager( p_sd ),
                        vlc_ServicesDiscoveryItemRemoved,
                        playlist_sd_item_removed,
                        p_sds->p_node );

    /* Remove the sd playlist node if it exists */
    PL_LOCK;
    playlist_NodeDelete( p_playlist, p_sds->p_node, true, false );
    PL_UNLOCK;

    vlc_sd_Destroy( p_sd );
    free( p_sds->psz_name );
    free( p_sds );

    return VLC_SUCCESS;
}

bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
                                         const char *psz_name )
{
    playlist_private_t *priv = pl_priv( p_playlist );
    bool found = false;
    PL_LOCK;

    for( int i = 0; i < priv->i_sds; i++ )
    {
        vlc_sd_internal_t *sd = priv->pp_sds[i];

        if( sd->psz_name && !strcmp( psz_name, sd->psz_name ) )
        {
            found = true;
            break;
        }
    }
    PL_UNLOCK;
    return found;
}

int playlist_ServicesDiscoveryControl( playlist_t *p_playlist, const char *psz_name, int i_control, ... )
{
    playlist_private_t *priv = pl_priv( p_playlist );
    int i_ret = VLC_EGENERIC;
    int i;

    PL_LOCK;
    for( i = 0; i < priv->i_sds; i++ )
    {
        vlc_sd_internal_t *sd = priv->pp_sds[i];
        if( sd->psz_name && !strcmp( psz_name, sd->psz_name ) )
        {
            va_list args;
            va_start( args, i_control );
            i_ret = vlc_sd_control( sd->p_sd, i_control, args );
            va_end( args );
            break;
        }
    }

    assert( i != priv->i_sds );
    PL_UNLOCK;

    return i_ret;
}

void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
{
    playlist_private_t *priv = pl_priv( p_playlist );

    while( priv->i_sds > 0 )
        playlist_ServicesDiscoveryRemove( p_playlist,
                                          priv->pp_sds[0]->psz_name );
}