]> git.sesse.net Git - vlc/blob - modules/services_discovery/shout.c
10faac4190178191140ad673f7259c7070b79990
[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  *          Antoine Cellerier <dionoea -@T- videolan -d.t- org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Includes
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/intf.h>
32 #include <vlc_interaction.h>
33
34 #include <vlc/input.h>
35
36 #include "network.h"
37
38 #include <errno.h>                                                 /* ENOMEM */
39
40 #ifdef HAVE_UNISTD_H
41 #    include <unistd.h>
42 #endif
43 #ifdef HAVE_SYS_TIME_H
44 #    include <sys/time.h>
45 #endif
46
47 /************************************************************************
48  * Macros and definitions
49  ************************************************************************/
50
51 #define MAX_LINE_LENGTH 256
52 #define SHOUTCAST_BASE_URL "http/shout-winamp://www.shoutcast.com/sbin/newxml.phtml"
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57
58 /* Callbacks */
59     static int  Open ( vlc_object_t * );
60     static void Close( vlc_object_t * );
61
62 vlc_module_begin();
63     set_shortname( "Shoutcast");
64     set_description( _("Shoutcast radio listings") );
65     set_category( CAT_PLAYLIST );
66     set_subcategory( SUBCAT_PLAYLIST_SD );
67
68     add_suppressed_integer( "shoutcast-limit" );
69
70     set_capability( "services_discovery", 0 );
71     set_callbacks( Open, Close );
72
73 vlc_module_end();
74
75
76 /*****************************************************************************
77  * Local structures
78  *****************************************************************************/
79
80 struct services_discovery_sys_t
81 {
82     playlist_item_t *p_item;
83     vlc_bool_t b_dialog;
84 };
85
86 /*****************************************************************************
87  * Local prototypes
88  *****************************************************************************/
89
90 /* Main functions */
91     static void Run    ( services_discovery_t *p_intf );
92
93 /*****************************************************************************
94  * Open: initialize and create stuff
95  *****************************************************************************/
96 static int Open( vlc_object_t *p_this )
97 {
98     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
99     services_discovery_sys_t *p_sys  = malloc(
100                                     sizeof( services_discovery_sys_t ) );
101
102     vlc_value_t         val;
103     playlist_t          *p_playlist;
104     playlist_view_t     *p_view;
105     playlist_item_t     *p_item;
106
107     p_sd->pf_run = Run;
108     p_sd->p_sys  = p_sys;
109
110     /* Create our playlist node */
111     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
112                                                 FIND_ANYWHERE );
113     if( !p_playlist )
114     {
115         msg_Warn( p_sd, "unable to find playlist, cancelling");
116         return VLC_EGENERIC;
117     }
118
119     p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
120
121     p_sys->p_item =
122     p_item = playlist_ItemNew( p_playlist, SHOUTCAST_BASE_URL, _("Shoutcast") );
123     playlist_NodeAddItem( p_playlist, p_item, p_view->i_id,
124                           p_view->p_root, PLAYLIST_APPEND,
125                           PLAYLIST_END );
126
127     p_sys->p_item->i_flags |= PLAYLIST_RO_FLAG;
128
129     val.b_bool = VLC_TRUE;
130     var_Set( p_playlist, "intf-change", val );
131
132     vlc_object_release( p_playlist );
133
134     return VLC_SUCCESS;
135 }
136
137 /*****************************************************************************
138  * Close:
139  *****************************************************************************/
140 static void Close( vlc_object_t *p_this )
141 {
142     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
143     services_discovery_sys_t *p_sys  = p_sd->p_sys;
144     playlist_t *p_playlist =  (playlist_t *) vlc_object_find( p_sd,
145                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
146     if( p_playlist )
147     {
148         playlist_NodeDelete( p_playlist, p_sys->p_item, VLC_TRUE, VLC_TRUE );
149         vlc_object_release( p_playlist );
150     }
151     free( p_sys );
152 }
153
154 /*****************************************************************************
155  * Run: main thread
156  *****************************************************************************/
157 static void Run( services_discovery_t *p_sd )
158 {
159     services_discovery_sys_t *p_sys  = p_sd->p_sys;
160     int i_id = input_Read( p_sd, &p_sys->p_item->input, VLC_FALSE );
161     int i_dialog_id;
162
163     i_dialog_id = intf_UserProgress( p_sd, "Shoutcast" , "Connecting...", 0.0 );
164
165     p_sys->b_dialog = VLC_TRUE;
166     while( !p_sd->b_die )
167     {
168         input_thread_t *p_input = (input_thread_t *)vlc_object_get( p_sd,
169                                                                     i_id );
170
171         /* The Shoutcast server does not return a content-length so we
172          * can't know where we are. Use the number of inserted items
173          * as a hint */
174         if( p_input != NULL )
175         {
176             int i_state = var_GetInteger( p_input, "state" );
177             if( i_state == PLAYING_S )
178             {
179                 float f_pos = (float)(p_sys->p_item->i_children)* 2 *100.0 /
180                               260 /* gruiiik FIXME */;
181                 intf_UserProgressUpdate( p_sd, i_dialog_id, "Downloading",
182                                          f_pos );
183             }
184             vlc_object_release( p_input );
185         }
186         else if( p_sys->b_dialog )
187         {
188             p_sys->b_dialog  = VLC_FALSE;
189             intf_UserHide( p_sd, i_dialog_id );
190         }
191         msleep( 10000 );
192     }
193 }