media_discoverer.c 9.83 KB
Newer Older
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
/*****************************************************************************
 * media_discoverer.c: libvlc new API media discoverer functions
 *****************************************************************************
 * Copyright (C) 2007 the VideoLAN team
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # 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.
 *
 * 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.
 *****************************************************************************/

#include "libvlc_internal.h"
#include <vlc/libvlc.h>
#include <assert.h>
27
#include "vlc_services_discovery.h"
28
29
30
31

/*
 * Private functions
 */
32

33
34
35
36
37
38
39
40
/**************************************************************************
 *       services_discovery_item_added (Private) (VLC event callback)
 **************************************************************************/

static void services_discovery_item_added( const vlc_event_t * p_event,
                                           void * user_data )
{
    input_item_t * p_item = p_event->u.services_discovery_item_added.p_new_item;
41
    const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
42
43
    libvlc_media_descriptor_t * p_md;
    libvlc_media_discoverer_t * p_mdis = user_data;
44
    libvlc_media_list_t * p_mlist = p_mdis->p_mlist;
45
46
47
48

    p_md = libvlc_media_descriptor_new_from_input_item(
            p_mdis->p_libvlc_instance,
            p_item, NULL );
49

50
51
52
53
54
55
56
57
58
59
    /* If we have a category, that mean we have to group the items having
     * that category in a media_list. */
    if( psz_cat )
    {
        p_mlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, psz_cat );
        if( p_mlist == kVLCDictionaryNotFound )
        {
            libvlc_media_descriptor_t * p_catmd;
            p_catmd = libvlc_media_descriptor_new_as_node( p_mdis->p_libvlc_instance, psz_cat, NULL );
            p_mlist = libvlc_media_descriptor_subitems( p_catmd, NULL );
60
61
            p_mlist->b_read_only = VLC_TRUE;

62
            /* Insert the newly created mlist in our dictionary */
63
            vlc_dictionary_insert( &p_mdis->catname_to_submedialist, psz_cat, p_mlist );
64
65
            
            /* Insert the md into the root list */
66
            _libvlc_media_list_add_media_descriptor( p_mdis->p_mlist, p_catmd, NULL );
67
68
69
70
71
72

            /* We don't release the mlist cause the dictionary
             * doesn't retain the object. But we release the md. */
            libvlc_media_descriptor_release( p_catmd );
        }
    }
73
74

    libvlc_media_list_lock( p_mdis->p_mlist );
75
    _libvlc_media_list_add_media_descriptor( p_mlist, p_md, NULL );
76
77
78
79
80
81
82
83
84
85
    libvlc_media_list_unlock( p_mdis->p_mlist );
}

/**************************************************************************
 *       services_discovery_item_removed (Private) (VLC event callback)
 **************************************************************************/

static void services_discovery_item_removed( const vlc_event_t * p_event,
                                             void * user_data )
{
86
87
88
89
90
91
92
93
94
95
96
    input_item_t * p_item = p_event->u.services_discovery_item_added.p_new_item;
    libvlc_media_descriptor_t * p_md;
    libvlc_media_discoverer_t * p_mdis = user_data;

    int i, count = libvlc_media_list_count( p_mdis->p_mlist, NULL );
    libvlc_media_list_lock( p_mdis->p_mlist );
    for( i = 0; i < count; i++ )
    {
        p_md = libvlc_media_list_item_at_index( p_mdis->p_mlist, i, NULL );
        if( p_md->p_input_item == p_item )
        {
97
            _libvlc_media_list_remove_index( p_mdis->p_mlist, i, NULL );
98
99
100
101
102
103
104
105
106
107
108
109
110
            break;
        }
    }
    libvlc_media_list_unlock( p_mdis->p_mlist );
}

/**************************************************************************
 *       services_discovery_started (Private) (VLC event callback)
 **************************************************************************/

static void services_discovery_started( const vlc_event_t * p_event,
                                        void * user_data )
{
111
    (void)p_event;
112
113
    libvlc_media_discoverer_t * p_mdis = user_data;
    libvlc_event_t event;
114
    p_mdis->running = VLC_TRUE;
115
116
117
118
119
120
121
122
123
124
125
    event.type = libvlc_MediaDiscovererStarted;
    libvlc_event_send( p_mdis->p_event_manager, &event );
}

/**************************************************************************
 *       services_discovery_ended (Private) (VLC event callback)
 **************************************************************************/

static void services_discovery_ended( const vlc_event_t * p_event,
                                      void * user_data )
{
126
    (void)p_event;
127
128
    libvlc_media_discoverer_t * p_mdis = user_data;
    libvlc_event_t event;
129
    p_mdis->running = VLC_FALSE;
130
131
    event.type = libvlc_MediaDiscovererEnded;
    libvlc_event_send( p_mdis->p_event_manager, &event );
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
}

/*
 * Public libvlc functions
 */

/**************************************************************************
 *       new (Public)
 *
 * Init an object.
 **************************************************************************/
libvlc_media_discoverer_t *
libvlc_media_discoverer_new_from_name( libvlc_instance_t * p_inst,
                                       const char * psz_name,
                                       libvlc_exception_t * p_e )
{
    libvlc_media_discoverer_t * p_mdis;
149
 
150
151
152
153
154
155
156
157
158
    p_mdis = malloc(sizeof(libvlc_media_discoverer_t));
    if( !p_mdis )
    {
        libvlc_exception_raise( p_e, "Not enough memory" );
        return NULL;
    }

    p_mdis->p_libvlc_instance = p_inst;
    p_mdis->p_mlist = libvlc_media_list_new( p_inst, NULL );
159
    p_mdis->p_mlist->b_read_only = VLC_TRUE;
160
    p_mdis->running = VLC_FALSE;
161

162
163
    vlc_dictionary_init( &p_mdis->catname_to_submedialist, 0 );

164
165
166
167
168
169
170
171
    p_mdis->p_event_manager = libvlc_event_manager_new( p_mdis,
            p_inst, NULL );

    libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
            libvlc_MediaDiscovererStarted, NULL );
    libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
            libvlc_MediaDiscovererEnded, NULL );

172
173
174
175
176
    p_mdis->p_sd = services_discovery_Create( (vlc_object_t*)p_inst->p_libvlc_int, psz_name );

    if( !p_mdis->p_sd )
    {
        free( p_mdis );
177
        libvlc_exception_raise( p_e, "Can't find the services_discovery module named '%s'", psz_name );
178
179
180
181
182
183
184
185
186
187
188
        return NULL;
    }

    vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
                      vlc_ServicesDiscoveryItemAdded,
                      services_discovery_item_added,
                      p_mdis );
    vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
                      vlc_ServicesDiscoveryItemRemoved,
                      services_discovery_item_removed,
                      p_mdis );
189
190
191
192
193
194
195
196
    vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
                      vlc_ServicesDiscoveryStarted,
                      services_discovery_started,
                      p_mdis );
    vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
                      vlc_ServicesDiscoveryEnded,
                      services_discovery_ended,
                      p_mdis );
197
 
198
199
200
201
202
203
204
205
206
207
208
209
210
    services_discovery_Start( p_mdis->p_sd );

    /* Here we go */

    return p_mdis;
}

/**************************************************************************
 * release (Public)
 **************************************************************************/
void
libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis )
{
211
212
    int i;

213
214
    libvlc_media_list_release( p_mdis->p_mlist );
    services_discovery_Destroy( p_mdis->p_sd );
215
216
217
218
219
220
221
222
223
224
225
226
227

    /* Free catname_to_submedialist and all the mlist */
    char ** all_keys = vlc_dictionary_all_keys( &p_mdis->catname_to_submedialist );
    for( i = 0; all_keys[i]; i++ )
    {
        libvlc_media_list_t * p_catmlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, all_keys[i] );
        libvlc_media_list_release( p_catmlist );
        free( all_keys[i] );
    }
    free( all_keys );

    vlc_dictionary_clear( &p_mdis->catname_to_submedialist );

228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
    free( p_mdis );
}

/**************************************************************************
 * localized_name (Public)
 **************************************************************************/
char *
libvlc_media_discoverer_localized_name( libvlc_media_discoverer_t * p_mdis )
{
    return services_discovery_GetLocalizedName( p_mdis->p_sd );
}

/**************************************************************************
 * media_list (Public)
 **************************************************************************/
libvlc_media_list_t *
libvlc_media_discoverer_media_list( libvlc_media_discoverer_t * p_mdis )
{
246
247
    libvlc_media_list_retain( p_mdis->p_mlist );
    return p_mdis->p_mlist;
248
249
}

250
251
252
253
254
255
256
257
258
/**************************************************************************
 * event_manager (Public)
 **************************************************************************/
libvlc_event_manager_t *
libvlc_media_discoverer_event_manager( libvlc_media_discoverer_t * p_mdis )
{
    return p_mdis->p_event_manager;
}

259
260
261
262

/**************************************************************************
 * running (Public)
 **************************************************************************/
263
vlc_bool_t
264
265
266
267
268
libvlc_media_discoverer_is_running( libvlc_media_discoverer_t * p_mdis )
{
    return p_mdis->running;
}