]> git.sesse.net Git - vlc/blob - modules/services_discovery/shout.c
242de0ee631b0dc8dc8fd0063efa0b8b27ff0293
[vlc] / modules / services_discovery / shout.c
1 /*****************************************************************************
2  * shout.c:  Shoutcast services discovery module
3  *****************************************************************************
4  * Copyright (C) 2005 VideoLAN
5  * $Id$
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Includes
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/intf.h>
31
32 #include <vlc/input.h>
33
34 #include "network.h"
35
36 #include <errno.h>                                                 /* ENOMEM */
37
38 #ifdef HAVE_UNISTD_H
39 #    include <unistd.h>
40 #endif
41 #ifdef HAVE_SYS_TIME_H
42 #    include <sys/time.h>
43 #endif
44
45 /************************************************************************
46  * Macros and definitions
47  ************************************************************************/
48
49 #define MAX_LINE_LENGTH 256
50
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55
56 /* Callbacks */
57     static int  Open ( vlc_object_t * );
58     static void Close( vlc_object_t * );
59
60 #define LIMIT_TEXT N_("Maximum number of shoutcast servers to be listed")
61 #define LIMIT_LONGTEXT LIMIT_TEXT
62
63 vlc_module_begin();
64     set_description( _("Shoutcast radio listings") );
65     set_category( CAT_PLAYLIST );
66     set_subcategory( SUBCAT_PLAYLIST_SD );
67
68     add_integer( "shoutcast-limit", 1000, NULL, LIMIT_TEXT,
69                     LIMIT_LONGTEXT, VLC_TRUE );
70
71     set_capability( "services_discovery", 0 );
72     set_callbacks( Open, Close );
73
74 vlc_module_end();
75
76
77 /*****************************************************************************
78  * Local structures
79  *****************************************************************************/
80
81 struct services_discovery_sys_t
82 {
83     /* playlist node */
84     playlist_item_t *p_node;
85     input_thread_t *p_input;
86
87 };
88
89 /*****************************************************************************
90  * Local prototypes
91  *****************************************************************************/
92
93 /* Main functions */
94     static void Run    ( services_discovery_t *p_intf );
95
96 /*****************************************************************************
97  * Open: initialize and create stuff
98  *****************************************************************************/
99 static int Open( vlc_object_t *p_this )
100 {
101     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
102     services_discovery_sys_t *p_sys  = malloc(
103                                     sizeof( services_discovery_sys_t ) );
104
105     vlc_value_t         val;
106     playlist_t          *p_playlist;
107     playlist_view_t     *p_view;
108     playlist_item_t     *p_item;
109
110     int i_limit;
111     char *psz_shoutcast_url;
112     char *psz_shoutcast_title;
113
114     p_sd->pf_run = Run;
115     p_sd->p_sys  = p_sys;
116
117     /* Create our playlist node */
118     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
119                                                 FIND_ANYWHERE );
120     if( !p_playlist )
121     {
122         msg_Warn( p_sd, "unable to find playlist, cancelling");
123         return VLC_EGENERIC;
124     }
125
126     i_limit = config_GetInt( p_this->p_libvlc, "shoutcast-limit" );
127     #define SHOUTCAST_BASE_URL "http/shout-b4s://shoutcast.com/sbin/xmllister.phtml?service=vlc&no_compress=1&limit="
128     psz_shoutcast_url = (char *)malloc( strlen( SHOUTCAST_BASE_URL ) + 20 );
129     psz_shoutcast_title = (char *)malloc( 6 + 20 );
130
131     sprintf( psz_shoutcast_url, SHOUTCAST_BASE_URL "%d", i_limit );
132     sprintf( psz_shoutcast_title, "Top %d", i_limit );
133
134     p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
135     p_sys->p_node = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
136                                          _("Shoutcast"), p_view->p_root );
137     p_item = playlist_ItemNew( p_playlist, psz_shoutcast_url,
138                                      psz_shoutcast_title );
139     free( psz_shoutcast_url );
140     free( psz_shoutcast_title );
141     playlist_NodeAddItem( p_playlist, p_item,
142                           p_sys->p_node->pp_parents[0]->i_view,
143                           p_sys->p_node, PLAYLIST_APPEND,
144                           PLAYLIST_END );
145
146     p_sys->p_input = input_CreateThread( p_playlist, &p_item->input );
147     /* We need to declare the parents of the node as the same of the
148      * parent's ones */
149     playlist_CopyParents( p_sys->p_node, p_item );
150     
151     p_sys->p_node->i_flags |= PLAYLIST_RO_FLAG;
152     val.b_bool = VLC_TRUE;
153     var_Set( p_playlist, "intf-change", val );
154
155     vlc_object_release( p_playlist );
156
157     return VLC_SUCCESS;
158 }
159
160 /*****************************************************************************
161  * Close:
162  *****************************************************************************/
163 static void Close( vlc_object_t *p_this )
164 {
165     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
166     services_discovery_sys_t *p_sys  = p_sd->p_sys;
167     playlist_t *p_playlist =  (playlist_t *) vlc_object_find( p_sd,
168                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
169     if( p_playlist )
170     {
171         playlist_NodeDelete( p_playlist, p_sys->p_node, VLC_TRUE, VLC_TRUE );
172         vlc_object_release( p_playlist );
173     }
174     if( p_sd->p_sys->p_input )
175     {
176         input_StopThread( p_sd->p_sys->p_input );
177         input_DestroyThread( p_sd->p_sys->p_input );
178         vlc_object_detach( p_sd->p_sys->p_input );
179         vlc_object_destroy( p_sd->p_sys->p_input );
180         p_sd->p_sys->p_input = NULL;        
181     }
182     free( p_sys );
183 }
184
185 /*****************************************************************************
186  * Run: main thread
187  *****************************************************************************/
188 static void Run( services_discovery_t *p_sd )
189 {
190     while( !p_sd->b_die )
191     {
192         if( p_sd->p_sys->p_input &&
193             ( p_sd->p_sys->p_input->b_eof || p_sd->p_sys->p_input->b_error ) )
194         {
195             input_StopThread( p_sd->p_sys->p_input );
196             input_DestroyThread( p_sd->p_sys->p_input );
197             vlc_object_detach( p_sd->p_sys->p_input );
198             vlc_object_destroy( p_sd->p_sys->p_input );
199             p_sd->p_sys->p_input = NULL;
200         }
201         msleep( 100000 );
202     }
203 }