]> git.sesse.net Git - vlc/blob - modules/services_discovery/shout.c
udev: remove item when the device is removed
[vlc] / modules / services_discovery / shout.c
1 /*****************************************************************************
2  * shout.c:  Shoutcast services discovery module
3  *****************************************************************************
4  * Copyright (C) 2005-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8  *          Antoine Cellerier <dionoea -@T- videolan -d.t- org>
9  *          Pierre d'Herbemont <pdherbemont # videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Includes
28  *****************************************************************************/
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_services_discovery.h>
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41
42 enum type_e { ShoutRadio = 0, ShoutTV = 1, Freebox = 2, FrenchTV = 3 };
43
44 static int  Open( vlc_object_t *, enum type_e );
45 static void Close( vlc_object_t * );
46
47 struct shout_item_t
48 {
49     const char *psz_url;
50     const char *psz_name;
51     const char *ppsz_options[2];
52     const struct shout_item_t * p_children;
53 };
54
55 #define endItem( ) { NULL, NULL, { NULL }, NULL }
56 #define item( title, url ) { url, title, { NULL }, NULL }
57 #define itemWithOption( title, url, option ) { url, title, { option, NULL }, NULL }
58 #define itemWithChildren( title, children ) { "vlc://nop", title, { NULL }, children }
59
60 /* WARN: We support only two levels */
61
62 static const struct shout_item_t p_frenchtv_canalplus[] = {
63     itemWithOption( N_("Les Guignols"), "http://www.canalplus.fr/index.php?pid=1784", "http-forward-cookies" ),
64     endItem()
65 };
66     
67 static const struct shout_item_t p_frenchtv[] = {
68     itemWithChildren( N_("Canal +"),  p_frenchtv_canalplus ),
69     endItem()
70 };
71
72 static const struct shout_item_t p_items[] = {
73     item(            N_("Shoutcast Radio"), "http/shout-winamp://www.shoutcast.com/sbin/newxml.phtml" ),
74     item(            N_("Shoutcast TV"),    "http/shout-winamp://www.shoutcast.com/sbin/newtvlister.phtml?alltv=1" ),
75     item(            N_("Freebox TV"),      "http://mafreebox.freebox.fr/freeboxtv/playlist.m3u" ),
76     itemWithChildren(N_("French TV"),        p_frenchtv ),
77     endItem()
78 };
79
80 #undef endItem
81 #undef item
82 #undef itemWithOptions
83 #undef itemWithChildren
84
85 struct shout_category_t {
86     services_discovery_t * p_sd;
87     const char * psz_category;
88 };
89
90 /* Main functions */
91 #define OPEN( type )                                \
92 static int Open ## type ( vlc_object_t *p_this )    \
93 {                                                   \
94     msg_Dbg( p_this, "Starting " #type );           \
95     return Open( p_this, type );                    \
96 }
97
98 OPEN( ShoutRadio )
99 OPEN( ShoutTV )
100 OPEN( Freebox )
101 OPEN( FrenchTV )
102
103 #undef OPEN
104
105 vlc_module_begin ()
106     set_category( CAT_PLAYLIST )
107     set_subcategory( SUBCAT_PLAYLIST_SD )
108
109     add_obsolete_integer( "shoutcast-limit" )
110
111         set_shortname( "Shoutcast")
112         set_description( N_("Shoutcast radio listings") )
113         set_capability( "services_discovery", 0 )
114         set_callbacks( OpenShoutRadio, Close )
115         add_shortcut( "shoutcast" )
116
117     add_submodule ()
118         set_shortname( "ShoutcastTV" )
119         set_description( N_("Shoutcast TV listings") )
120         set_capability( "services_discovery", 0 )
121         set_callbacks( OpenShoutTV, Close )
122         add_shortcut( "shoutcasttv" )
123
124     add_submodule ()
125         set_shortname( "frenchtv")
126         set_description( N_("French TV") )
127         set_capability( "services_discovery", 0 )
128         set_callbacks( OpenFrenchTV, Close )
129         add_shortcut( "frenchtv" )
130
131     add_submodule ()
132         set_shortname( "Freebox")
133         set_description( N_("Freebox TV listing (French ISP free.fr services)") )
134         set_capability( "services_discovery", 0 )
135         set_callbacks( OpenFreebox, Close )
136         add_shortcut( "freebox" )
137
138 vlc_module_end ()
139
140
141 /*****************************************************************************
142  * Local prototypes
143  *****************************************************************************/
144
145 static void *Run( void * );
146 struct services_discovery_sys_t
147 {
148     vlc_thread_t thread;
149     enum type_e i_type;
150 };
151
152 /*****************************************************************************
153  * Open: initialize and create stuff
154  *****************************************************************************/
155 static int Open( vlc_object_t *p_this, enum type_e i_type )
156 {
157     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
158
159     p_sd->p_sys = malloc (sizeof (*(p_sd->p_sys)));
160     if (p_sd->p_sys == NULL)
161         return VLC_ENOMEM;
162
163     p_sd->p_sys->i_type = i_type;
164     if (vlc_clone (&p_sd->p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
165     {
166         free (p_sd->p_sys);
167         return VLC_EGENERIC;
168     }
169     return VLC_SUCCESS;
170 }
171
172 /*****************************************************************************
173  * ItemAdded:
174  *****************************************************************************/
175 static void ItemAdded( const vlc_event_t * p_event, void * user_data )
176 {
177     struct shout_category_t * params = user_data;
178     services_discovery_AddItem( params->p_sd,
179             p_event->u.input_item_subitem_added.p_new_child,
180             params->psz_category );
181 }
182
183 /*****************************************************************************
184  * CreateInputItemFromShoutItem:
185  *****************************************************************************/
186 static input_item_t * CreateInputItemFromShoutItem( services_discovery_t *p_sd,
187                                          const struct shout_item_t * p_item )
188 {
189     int i;
190     /* Create the item */
191     input_item_t *p_input = input_item_New( p_sd, p_item->psz_url,
192                                             vlc_gettext(p_item->psz_name) );
193
194     /* Copy options */
195     for( i = 0; p_item->ppsz_options[i] != NULL; i++ )
196         input_item_AddOption( p_input, p_item->ppsz_options[i], VLC_INPUT_OPTION_TRUSTED );
197     input_item_AddOption( p_input, "no-playlist-autostart", VLC_INPUT_OPTION_TRUSTED );
198
199     return p_input;
200 }
201
202 /*****************************************************************************
203  * AddSubitemsOfShoutItemURL:
204  *****************************************************************************/
205 static void AddSubitemsOfShoutItemURL( services_discovery_t *p_sd,
206                                        const struct shout_item_t * p_item,
207                                        const char * psz_category )
208 {
209     struct shout_category_t category = { p_sd, psz_category };
210
211     /* Create the item */
212     input_item_t *p_input = CreateInputItemFromShoutItem( p_sd, p_item );
213
214     /* Read every subitems, and add them in ItemAdded */
215     vlc_event_attach( &p_input->event_manager, vlc_InputItemSubItemAdded,
216                       ItemAdded, &category );
217     input_Read( p_sd, p_input );
218     vlc_event_detach( &p_input->event_manager, vlc_InputItemSubItemAdded,
219                       ItemAdded, &category );
220
221     vlc_gc_decref( p_input );
222 }
223
224 /*****************************************************************************
225  * Run:
226  *****************************************************************************/
227 static void *Run( void *data )
228 {
229     services_discovery_t *p_sd = data;
230     services_discovery_sys_t *p_sys = p_sd->p_sys;
231     enum type_e i_type = p_sys->i_type;
232     int i, j;
233     int canc = vlc_savecancel();
234     
235     if( !p_items[i_type].p_children )
236     {
237         AddSubitemsOfShoutItemURL( p_sd, &p_items[i_type], NULL );
238         vlc_restorecancel(canc);
239         return NULL;
240     }
241     for( i = 0; p_items[i_type].p_children[i].psz_name; i++ )
242     {
243         const struct shout_item_t * p_subitem = &p_items[i_type].p_children[i];
244         if( !p_subitem->p_children )
245         {
246             AddSubitemsOfShoutItemURL( p_sd, p_subitem, p_subitem->psz_name );
247             continue;
248         }
249         for( j = 0; p_subitem->p_children[j].psz_name; j++ )
250         {
251             input_item_t *p_input = CreateInputItemFromShoutItem( p_sd, &p_subitem->p_children[j] );
252             services_discovery_AddItem( p_sd,
253                 p_input,
254                 p_subitem->psz_name );
255             vlc_gc_decref( p_input );
256         }
257     }
258     vlc_restorecancel(canc);
259     return NULL;
260 }
261
262 /*****************************************************************************
263  * Close:
264  *****************************************************************************/
265 static void Close( vlc_object_t *p_this )
266 {
267     services_discovery_t *p_sd = (services_discovery_t *)p_this;
268     services_discovery_sys_t *p_sys = p_sd->p_sys;
269
270     vlc_join (p_sys->thread, NULL);
271     free (p_sys);
272 }