]> git.sesse.net Git - vlc/blob - src/playlist/services_discovery.c
Fix a few bugs
[vlc] / src / playlist / services_discovery.c
1 /*****************************************************************************
2  * services_discovery.c : Manage playlist services_discovery modules
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23 #include <vlc/vlc.h>
24 #include <vlc/input.h>
25 #include "vlc_playlist.h"
26 #include "playlist_internal.h"
27
28 /*****************************************************************************
29  * Local prototypes
30  *****************************************************************************/
31
32 static void RunSD( services_discovery_t *p_sd );
33
34
35 /***************************************************************************
36 ***************************************************************************/
37
38 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,
39                                    const char *psz_module )
40 {
41     services_discovery_t *p_sd;
42
43     p_sd = vlc_object_create( p_playlist, VLC_OBJECT_SD );
44     p_sd->pf_run = NULL;
45
46     p_sd->p_module = module_Need( p_sd, "services_discovery", psz_module, 0 );
47
48     if( p_sd->p_module == NULL )
49     {
50         msg_Err( p_playlist, "no suitable services discovery module" );
51         vlc_object_destroy( p_sd );
52         return VLC_EGENERIC;
53     }
54
55     p_sd->psz_module = strdup( psz_module );
56     p_sd->b_die = VLC_FALSE;
57
58     vlc_mutex_lock( &p_playlist->object_lock );
59
60     INSERT_ELEM( p_playlist->pp_sds, p_playlist->i_sds, p_playlist->i_sds,
61                  p_sd );
62
63     vlc_mutex_unlock( &p_playlist->object_lock );
64
65     if( !p_sd->pf_run ) return VLC_SUCCESS;
66
67     if( vlc_thread_create( p_sd, "services_discovery", RunSD,
68                            VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
69     {
70         msg_Err( p_sd, "cannot create services discovery thread" );
71         vlc_object_destroy( p_sd );
72         return VLC_EGENERIC;
73     }
74
75
76     return VLC_SUCCESS;
77 }
78
79 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
80                                        const char *psz_module )
81 {
82     int i;
83     services_discovery_t *p_sd = NULL;
84
85     PL_LOCK;
86     for( i = 0 ; i< p_playlist->i_sds ; i ++ )
87     {
88         if( !strcmp( psz_module, p_playlist->pp_sds[i]->psz_module ) )
89         {
90             p_sd = p_playlist->pp_sds[i];
91             REMOVE_ELEM( p_playlist->pp_sds, p_playlist->i_sds, i );
92             break;
93         }
94     }
95
96     if( p_sd )
97     {
98         PL_UNLOCK;
99         p_sd->b_die = VLC_TRUE;
100         if( p_sd->pf_run ) vlc_thread_join( p_sd );
101
102         free( p_sd->psz_module );
103         module_Unneed( p_sd, p_sd->p_module );
104         PL_LOCK;
105         vlc_object_destroy( p_sd );
106     }
107     else
108     {
109         msg_Warn( p_playlist, "module %s is not loaded", psz_module );
110         PL_UNLOCK;
111         return VLC_EGENERIC;
112     }
113     PL_UNLOCK;
114     return VLC_SUCCESS;
115 }
116
117 vlc_bool_t playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
118                                               const char *psz_module )
119 {
120     int i;
121     PL_LOCK;
122
123     for( i = 0 ; i< p_playlist->i_sds ; i ++ )
124     {
125         if( !strcmp( psz_module, p_playlist->pp_sds[i]->psz_module ) )
126         {
127             PL_UNLOCK;
128             return VLC_TRUE;
129         }
130     }
131     PL_UNLOCK;
132     return VLC_FALSE;
133 }
134
135
136 /**
137  * Load all service discovery modules in a string
138  *
139  * \param p_playlist the playlist
140  * \param psz_modules a list of modules separated by commads
141  * return VLC_SUCCESS or an error
142  */
143 int playlist_AddSDModules( playlist_t *p_playlist, char *psz_modules )
144 {
145     if( psz_modules && *psz_modules )
146     {
147         char *psz_parser = psz_modules;
148         char *psz_next;
149
150         while( psz_parser && *psz_parser )
151         {
152             while( *psz_parser == ' ' || *psz_parser == ':' )
153                 psz_parser++;
154
155             if( (psz_next = strchr( psz_parser, ':' ) ) )
156                 *psz_next++ = '\0';
157
158             if( *psz_parser == '\0' )
159             {
160                 break;
161             }
162
163             playlist_ServicesDiscoveryAdd( p_playlist, psz_parser );
164
165             psz_parser = psz_next;
166         }
167     }
168     return VLC_SUCCESS;
169 }
170
171 static void RunSD( services_discovery_t *p_sd )
172 {
173     p_sd->pf_run( p_sd );
174     return;
175 }