]> git.sesse.net Git - vlc/blob - modules/services_discovery/shout.c
98a8f6b29c98f1d6babbdd97bb9bfc997eee04da
[vlc] / modules / services_discovery / shout.c
1 /*****************************************************************************
2  * sap.c :  SAP interface module
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id: sap.c 9217 2004-11-07 11:02:59Z courmisch $
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@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., 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 vlc_module_begin();
61     set_description( _("HAL device detection") );
62     set_category( CAT_PLAYLIST );
63     set_subcategory( SUBCAT_PLAYLIST_SD );
64
65     set_capability( "services_discovery", 0 );
66     set_callbacks( Open, Close );
67
68 vlc_module_end();
69
70
71 /*****************************************************************************
72  * Local structures
73  *****************************************************************************/
74
75 struct services_discovery_sys_t
76 {
77     /* playlist node */
78     playlist_item_t *p_node;
79     input_thread_t *p_input;
80
81 };
82
83 /*****************************************************************************
84  * Local prototypes
85  *****************************************************************************/
86
87 /* Main functions */
88     static void Run    ( services_discovery_t *p_intf );
89
90 /*****************************************************************************
91  * Open: initialize and create stuff
92  *****************************************************************************/
93 static int Open( vlc_object_t *p_this )
94 {
95     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
96     services_discovery_sys_t *p_sys  = malloc(
97                                     sizeof( services_discovery_sys_t ) );
98
99     vlc_value_t         val;
100     playlist_t          *p_playlist;
101     playlist_view_t     *p_view;
102     playlist_item_t     *p_item;
103
104     p_sd->pf_run = Run;
105     p_sd->p_sys  = p_sys;
106
107     /* Create our playlist node */
108     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
109                                                 FIND_ANYWHERE );
110     if( !p_playlist )
111     {
112         msg_Warn( p_sd, "unable to find playlist, cancelling");
113         return VLC_EGENERIC;
114     }
115
116     p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
117     p_sys->p_node = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
118                                          _("Shoutcast"), p_view->p_root );
119     p_item = playlist_ItemNew( p_playlist, "http/shout-b4s://shoutcast.com/sbin/xmllister.phtml?service=vlc&no_compress=1&limit=1000", "Top 1000" );
120     playlist_NodeAddItem( p_playlist, p_item,
121                           p_sys->p_node->pp_parents[0]->i_view,
122                           p_sys->p_node, PLAYLIST_APPEND,
123                           PLAYLIST_END );
124
125     p_sys->p_input = input_CreateThread( p_playlist, &p_item->input );
126     /* We need to declare the parents of the node as the same of the
127      * parent's ones */
128     playlist_CopyParents( p_sys->p_node, p_item );
129     
130     p_sys->p_node->i_flags |= PLAYLIST_RO_FLAG;
131     val.b_bool = VLC_TRUE;
132     var_Set( p_playlist, "intf-change", val );
133
134     vlc_object_release( p_playlist );
135
136     return VLC_SUCCESS;
137 }
138
139 /*****************************************************************************
140  * Close:
141  *****************************************************************************/
142 static void Close( vlc_object_t *p_this )
143 {
144     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
145     services_discovery_sys_t *p_sys  = p_sd->p_sys;
146     playlist_t *p_playlist =  (playlist_t *) vlc_object_find( p_sd,
147                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
148     if( p_playlist )
149     {
150         playlist_NodeDelete( p_playlist, p_sys->p_node, VLC_TRUE, VLC_TRUE );
151         vlc_object_release( p_playlist );
152     }
153     if( p_sd->p_sys->p_input )
154     {
155         input_StopThread( p_sd->p_sys->p_input );
156         input_DestroyThread( p_sd->p_sys->p_input );
157         vlc_object_detach( p_sd->p_sys->p_input );
158         vlc_object_destroy( p_sd->p_sys->p_input );
159         p_sd->p_sys->p_input = NULL;        
160     }
161     free( p_sys );
162 }
163
164 /*****************************************************************************
165  * Run: main thread
166  *****************************************************************************/
167 static void Run( services_discovery_t *p_sd )
168 {
169     while( !p_sd->b_die )
170     {
171         if( p_sd->p_sys->p_input &&
172             ( p_sd->p_sys->p_input->b_eof || p_sd->p_sys->p_input->b_error ) )
173         {
174             input_StopThread( p_sd->p_sys->p_input );
175             input_DestroyThread( p_sd->p_sys->p_input );
176             vlc_object_detach( p_sd->p_sys->p_input );
177             vlc_object_destroy( p_sd->p_sys->p_input );
178             p_sd->p_sys->p_input = NULL;
179         }
180         msleep( 100000 );
181     }
182 }