]> git.sesse.net Git - vlc/blob - modules/services_discovery/shout.c
Strings
[vlc] / modules / services_discovery / shout.c
1 /*****************************************************************************
2  * shout.c:  Shoutcast services discovery module
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 #include <vlc_interaction.h>
32
33 #include <vlc/input.h>
34
35 #include "network.h"
36
37 #include <errno.h>                                                 /* ENOMEM */
38
39 #ifdef HAVE_UNISTD_H
40 #    include <unistd.h>
41 #endif
42 #ifdef HAVE_SYS_TIME_H
43 #    include <sys/time.h>
44 #endif
45
46 /************************************************************************
47  * Macros and definitions
48  ************************************************************************/
49
50 #define MAX_LINE_LENGTH 256
51
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56
57 /* Callbacks */
58     static int  Open ( vlc_object_t * );
59     static void Close( vlc_object_t * );
60
61 #define LIMIT_TEXT N_("Number of streams")
62 /// \bug [String] -which would be listed + to list
63 #define LIMIT_LONGTEXT N_("Maximum number of Shoutcast radio streams which " \
64                           "would be listed.")
65
66 vlc_module_begin();
67     set_shortname( "Shoutcast");
68     set_description( _("Shoutcast radio listings") );
69     set_category( CAT_PLAYLIST );
70     set_subcategory( SUBCAT_PLAYLIST_SD );
71
72     add_integer( "shoutcast-limit", 1000, NULL, LIMIT_TEXT,
73                     LIMIT_LONGTEXT, VLC_TRUE );
74
75     set_capability( "services_discovery", 0 );
76     set_callbacks( Open, Close );
77
78 vlc_module_end();
79
80
81 /*****************************************************************************
82  * Local structures
83  *****************************************************************************/
84
85 struct services_discovery_sys_t
86 {
87     /* playlist node */
88     playlist_item_t *p_node;
89     playlist_item_t *p_item;
90     int i_limit;
91     vlc_bool_t b_dialog;
92 };
93
94 /*****************************************************************************
95  * Local prototypes
96  *****************************************************************************/
97
98 /* Main functions */
99     static void Run    ( services_discovery_t *p_intf );
100
101 /*****************************************************************************
102  * Open: initialize and create stuff
103  *****************************************************************************/
104 static int Open( vlc_object_t *p_this )
105 {
106     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
107     services_discovery_sys_t *p_sys  = malloc(
108                                     sizeof( services_discovery_sys_t ) );
109
110     vlc_value_t         val;
111     playlist_t          *p_playlist;
112     playlist_view_t     *p_view;
113     playlist_item_t     *p_item;
114
115     char *psz_shoutcast_url;
116     char *psz_shoutcast_title;
117
118     p_sd->pf_run = Run;
119     p_sd->p_sys  = p_sys;
120
121     /* Create our playlist node */
122     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
123                                                 FIND_ANYWHERE );
124     if( !p_playlist )
125     {
126         msg_Warn( p_sd, "unable to find playlist, cancelling");
127         return VLC_EGENERIC;
128     }
129
130     p_sys->i_limit = config_GetInt( p_this->p_libvlc, "shoutcast-limit" );
131     #define SHOUTCAST_BASE_URL "http/shout-b4s://www.shoutcast.com/sbin/xmllister.phtml?service=vlc&no_compress=1&limit="
132     psz_shoutcast_url = (char *)malloc( strlen( SHOUTCAST_BASE_URL ) + 20 );
133     psz_shoutcast_title = (char *)malloc( 6 + 20 );
134
135     sprintf( psz_shoutcast_url, SHOUTCAST_BASE_URL "%d", p_sys->i_limit );
136     sprintf( psz_shoutcast_title, "Top %d", p_sys->i_limit );
137
138     p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
139     p_sys->p_node = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
140                                          _("Shoutcast"), p_view->p_root );
141     p_item = playlist_ItemNew( p_playlist, psz_shoutcast_url,
142                                      psz_shoutcast_title );
143     free( psz_shoutcast_url );
144     free( psz_shoutcast_title );
145     playlist_NodeAddItem( p_playlist, p_item,
146                           p_sys->p_node->pp_parents[0]->i_view,
147                           p_sys->p_node, PLAYLIST_APPEND,
148                           PLAYLIST_END );
149
150     /* We need to declare the parents of the node as the same of the
151      * parent's ones */
152     playlist_CopyParents( p_sys->p_node, p_item );
153
154     p_sys->p_item = p_item;
155     p_sys->p_node->i_flags |= PLAYLIST_RO_FLAG;
156     val.b_bool = VLC_TRUE;
157     var_Set( p_playlist, "intf-change", val );
158
159     vlc_object_release( p_playlist );
160
161     return VLC_SUCCESS;
162 }
163
164 /*****************************************************************************
165  * Close:
166  *****************************************************************************/
167 static void Close( vlc_object_t *p_this )
168 {
169     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
170     services_discovery_sys_t *p_sys  = p_sd->p_sys;
171     playlist_t *p_playlist =  (playlist_t *) vlc_object_find( p_sd,
172                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
173     if( p_playlist )
174     {
175         playlist_NodeDelete( p_playlist, p_sys->p_node, VLC_TRUE, VLC_TRUE );
176         vlc_object_release( p_playlist );
177     }
178     free( p_sys );
179 }
180
181 /*****************************************************************************
182  * Run: main thread
183  *****************************************************************************/
184 static void Run( services_discovery_t *p_sd )
185 {
186     services_discovery_sys_t *p_sys  = p_sd->p_sys;
187     int i_id = input_Read( p_sd, &p_sys->p_item->input, VLC_FALSE );
188     int i_dialog_id;
189
190     i_dialog_id = intf_UserProgress( p_sd, "Shoutcast" , "Connecting...", 0.0 );
191
192     p_sys->b_dialog = VLC_TRUE;
193     while( !p_sd->b_die )
194     {
195         input_thread_t *p_input = (input_thread_t *)vlc_object_get( p_sd,
196                                                                     i_id );
197
198         /* The Shoutcast server does not return a content-length so we
199          * can't know where we are. Use the number of inserted items
200          * as a hint */
201         if( p_input != NULL )
202         {
203             int i_state = var_GetInteger( p_input, "state" );
204             if( i_state == PLAYING_S )
205             {
206                 float f_pos = (float)(p_sys->p_item->i_children)* 2 *100.0 /
207                               (float)(p_sys->i_limit);
208                 intf_UserProgressUpdate( p_sd, i_dialog_id, "Downloading",
209                                          f_pos );
210             }
211             vlc_object_release( p_input );
212         }
213         else if( p_sys->b_dialog )
214         {
215             p_sys->b_dialog  = VLC_FALSE;
216             intf_UserHide( p_sd, i_dialog_id );
217         }
218         msleep( 10000 );
219     }
220 }