services_discovery: implement SD categories and use in Qt interface
[vlc.git] / modules / services_discovery / shout.c
blob378c2351c98591f3e116ac9ed6c31982b9d91bc3
1 /*****************************************************************************
2 * shout.c: Shoutcast services discovery module
3 *****************************************************************************
4 * Copyright (C) 2005-2007 the VideoLAN team
5 * $Id$
7 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8 * Antoine Cellerier <dionoea -@T- videolan -d.t- org>
9 * Pierre d'Herbemont <pdherbemont # videolan.org>
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
27 * Includes
28 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_services_discovery.h>
38 /*****************************************************************************
39 * Module descriptor
40 *****************************************************************************/
42 enum type_e { ShoutRadio = 0, ShoutTV = 1, Freebox = 2, FrenchTV = 3 };
44 static int Open( vlc_object_t *, enum type_e );
45 static void Close( vlc_object_t * );
47 struct shout_item_t
49 const char *psz_url;
50 const char *psz_name;
51 const char *ppsz_options[2];
52 const struct shout_item_t * p_children;
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 }
60 /* WARN: We support only two levels */
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()
67 static const struct shout_item_t p_frenchtv[] = {
68 itemWithChildren( N_("Canal +"), p_frenchtv_canalplus ),
69 endItem()
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 itemWithOption( N_("Freebox TV"),"http://mafreebox.freebox.fr/freeboxtv/playlist.m3u", "deinterlace=1"),
76 itemWithChildren(N_("French TV"), p_frenchtv ),
77 endItem()
80 #undef endItem
81 #undef item
82 #undef itemWithOptions
83 #undef itemWithChildren
85 struct shout_category_t {
86 services_discovery_t * p_sd;
87 const char * psz_category;
88 const struct shout_item_t * p_parent;
91 /* Main functions */
92 #define OPEN( type ) \
93 static int Open ## type ( vlc_object_t *p_this ) \
94 { \
95 msg_Dbg( p_this, "Starting " #type ); \
96 return Open( p_this, type ); \
99 OPEN( ShoutRadio )
100 OPEN( ShoutTV )
101 OPEN( Freebox )
102 OPEN( FrenchTV )
104 #undef OPEN
106 static int vlc_sd_probe_Open( vlc_object_t * );
108 vlc_module_begin ()
109 set_category( CAT_PLAYLIST )
110 set_subcategory( SUBCAT_PLAYLIST_SD )
112 add_obsolete_integer( "shoutcast-limit" )
114 set_shortname( "Shoutcast")
115 set_description( N_("Shoutcast Radio") )
116 set_capability( "services_discovery", 0 )
117 set_callbacks( OpenShoutRadio, Close )
118 add_shortcut( "shoutcast" )
120 add_submodule ()
121 set_shortname( "ShoutcastTV" )
122 set_description( N_("Shoutcast TV") )
123 set_capability( "services_discovery", 0 )
124 set_callbacks( OpenShoutTV, Close )
125 add_shortcut( "shoutcasttv" )
127 add_submodule ()
128 set_shortname( "frenchtv")
129 set_description( N_("French TV") )
130 set_capability( "services_discovery", 0 )
131 set_callbacks( OpenFrenchTV, Close )
132 add_shortcut( "frenchtv" )
134 add_submodule ()
135 set_shortname( "Freebox")
136 set_description( N_("Freebox TV") )
137 set_capability( "services_discovery", 0 )
138 set_callbacks( OpenFreebox, Close )
139 add_shortcut( "freebox" )
141 VLC_SD_PROBE_SUBMODULE
142 vlc_module_end ()
145 /*****************************************************************************
146 * Local prototypes
147 *****************************************************************************/
149 static void *Run( void * );
150 struct services_discovery_sys_t
152 vlc_thread_t thread;
153 enum type_e i_type;
156 /*****************************************************************************
157 * Open: initialize and create stuff
158 *****************************************************************************/
159 static int Open( vlc_object_t *p_this, enum type_e i_type )
161 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
163 p_sd->p_sys = malloc (sizeof (*(p_sd->p_sys)));
164 if (p_sd->p_sys == NULL)
165 return VLC_ENOMEM;
167 p_sd->p_sys->i_type = i_type;
168 if (vlc_clone (&p_sd->p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW))
170 free (p_sd->p_sys);
171 return VLC_EGENERIC;
173 return VLC_SUCCESS;
176 /*****************************************************************************
177 * ItemAdded:
178 *****************************************************************************/
179 static void ItemAdded( const vlc_event_t * p_event, void * user_data )
181 struct shout_category_t * params = user_data;
182 const struct shout_item_t * p_parent = params->p_parent;
183 input_item_t * p_input = p_event->u.input_item_subitem_added.p_new_child;
185 for(int i = 0; p_parent->ppsz_options[i] != NULL; i++)
186 input_item_AddOption( p_input, p_parent->ppsz_options[i], VLC_INPUT_OPTION_TRUSTED);
188 services_discovery_AddItem( params->p_sd, p_input, params->psz_category );
191 /*****************************************************************************
192 * CreateInputItemFromShoutItem:
193 *****************************************************************************/
194 static input_item_t * CreateInputItemFromShoutItem( services_discovery_t *p_sd,
195 const struct shout_item_t * p_item )
197 /* Create the item */
198 input_item_t *p_input = input_item_New( p_sd, p_item->psz_url,
199 vlc_gettext(p_item->psz_name) );
201 /* Copy options */
202 for(int i = 0; p_item->ppsz_options[i] != NULL; i++)
203 input_item_AddOption( p_input, p_item->ppsz_options[i], VLC_INPUT_OPTION_TRUSTED );
204 input_item_AddOption( p_input, "no-playlist-autostart", VLC_INPUT_OPTION_TRUSTED );
206 return p_input;
209 /*****************************************************************************
210 * AddSubitemsOfShoutItemURL:
211 *****************************************************************************/
212 static void AddSubitemsOfShoutItemURL( services_discovery_t *p_sd,
213 const struct shout_item_t * p_item,
214 const char * psz_category )
216 struct shout_category_t category = { p_sd, psz_category, p_item };
218 /* Create the item */
219 input_item_t *p_input = CreateInputItemFromShoutItem( p_sd, p_item );
221 /* Read every subitems, and add them in ItemAdded */
222 vlc_event_attach( &p_input->event_manager, vlc_InputItemSubItemAdded,
223 ItemAdded, &category );
224 input_Read( p_sd, p_input );
225 vlc_event_detach( &p_input->event_manager, vlc_InputItemSubItemAdded,
226 ItemAdded, &category );
228 vlc_gc_decref( p_input );
231 /*****************************************************************************
232 * Run:
233 *****************************************************************************/
234 static void *Run( void *data )
236 services_discovery_t *p_sd = data;
237 services_discovery_sys_t *p_sys = p_sd->p_sys;
238 enum type_e i_type = p_sys->i_type;
239 int i, j;
240 int canc = vlc_savecancel();
242 if( !p_items[i_type].p_children )
244 AddSubitemsOfShoutItemURL( p_sd, &p_items[i_type], NULL );
245 vlc_restorecancel(canc);
246 return NULL;
248 for( i = 0; p_items[i_type].p_children[i].psz_name; i++ )
250 const struct shout_item_t * p_subitem = &p_items[i_type].p_children[i];
251 if( !p_subitem->p_children )
253 AddSubitemsOfShoutItemURL( p_sd, p_subitem, p_subitem->psz_name );
254 continue;
256 for( j = 0; p_subitem->p_children[j].psz_name; j++ )
258 input_item_t *p_input = CreateInputItemFromShoutItem( p_sd, &p_subitem->p_children[j] );
259 services_discovery_AddItem( p_sd,
260 p_input,
261 p_subitem->psz_name );
262 vlc_gc_decref( p_input );
265 vlc_restorecancel(canc);
266 return NULL;
269 /*****************************************************************************
270 * Close:
271 *****************************************************************************/
272 static void Close( vlc_object_t *p_this )
274 services_discovery_t *p_sd = (services_discovery_t *)p_this;
275 services_discovery_sys_t *p_sys = p_sd->p_sys;
277 vlc_join (p_sys->thread, NULL);
278 free (p_sys);
281 static int vlc_sd_probe_Open( vlc_object_t *obj )
283 vlc_probe_t *probe = (vlc_probe_t *)obj;
285 vlc_sd_probe_Add( probe, "shoutcast{longname=\"Shoutcast Radio\"}",
286 N_("Shoutcast Radio"), SD_CAT_INTERNET );
287 vlc_sd_probe_Add( probe, "shoutcasttv{longname=\"Shoutcast TV\"}",
288 N_("Shoutcast TV"), SD_CAT_INTERNET );
289 vlc_sd_probe_Add( probe, "frenchtv{longname=\"French TV\"}",
290 N_("French TV"), SD_CAT_INTERNET );
291 return VLC_PROBE_CONTINUE;