]> git.sesse.net Git - vlc/blob - src/playlist/services_discovery.c
A bit of vlc/libvlc cleanup:
[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_playlist.h"
25 #include "playlist_internal.h"
26
27 static void RunSD( services_discovery_t *p_sd );
28
29 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,  const char *psz_modules )
30 {
31     if( psz_modules && *psz_modules )
32     {
33         const char *psz_parser = psz_modules;
34         char *psz_next;
35
36         while( psz_parser && *psz_parser )
37         {
38             while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
39                 psz_parser++;
40
41             if( (psz_next = strchr( psz_parser, ':' ) ) )
42                 *psz_next++ = '\0';
43
44             if( *psz_parser == '\0' )
45                 break;
46             msg_Dbg( p_playlist, "Add services_discovery %s", psz_parser );
47             /* Perform the addition */
48             {
49                 services_discovery_t *p_sd = vlc_object_create( p_playlist,
50                                                                 VLC_OBJECT_SD );
51                 p_sd->pf_run = NULL;
52                 p_sd->p_module = module_Need( p_sd, "services_discovery", psz_parser, 0 );
53
54                 if( p_sd->p_module == NULL )
55                 {
56                     msg_Err( p_playlist, "no suitable services discovery module" );
57                     vlc_object_destroy( p_sd );
58                     return VLC_EGENERIC;
59                 }
60                 p_sd->psz_module = strdup( psz_parser );
61                 p_sd->b_die = VLC_FALSE;
62
63                 PL_LOCK;
64                 TAB_APPEND( p_playlist->i_sds, p_playlist->pp_sds, p_sd );
65                 PL_UNLOCK;
66
67                 if( !p_sd->pf_run ) {
68                     psz_parser = psz_next;
69                     continue;
70                 }
71
72                 if( vlc_thread_create( p_sd, "services_discovery", RunSD,
73                                        VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
74                 {
75                     msg_Err( p_sd, "cannot create services discovery thread" );
76                     vlc_object_destroy( p_sd );
77                     return VLC_EGENERIC;
78                 }
79             }
80             psz_parser = psz_next;
81         }
82     }
83     return VLC_SUCCESS;
84 }
85
86 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
87                                        const char *psz_module )
88 {
89     int i;
90     services_discovery_t *p_sd = NULL;
91
92     PL_LOCK;
93     for( i = 0 ; i< p_playlist->i_sds ; i ++ )
94     {
95         if( !strcmp( psz_module, p_playlist->pp_sds[i]->psz_module ) )
96         {
97             p_sd = p_playlist->pp_sds[i];
98             REMOVE_ELEM( p_playlist->pp_sds, p_playlist->i_sds, i );
99             break;
100         }
101     }
102
103     if( p_sd )
104     {
105         PL_UNLOCK;
106         p_sd->b_die = VLC_TRUE;
107         if( p_sd->pf_run ) vlc_thread_join( p_sd );
108
109         free( p_sd->psz_module );
110         module_Unneed( p_sd, p_sd->p_module );
111         PL_LOCK;
112         vlc_object_destroy( p_sd );
113     }
114     else
115     {
116         msg_Warn( p_playlist, "module %s is not loaded", psz_module );
117         PL_UNLOCK;
118         return VLC_EGENERIC;
119     }
120     PL_UNLOCK;
121     return VLC_SUCCESS;
122 }
123
124 vlc_bool_t playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
125                                               const char *psz_module )
126 {
127     int i;
128     PL_LOCK;
129
130     for( i = 0 ; i< p_playlist->i_sds ; i ++ )
131     {
132         if( !strcmp( psz_module, p_playlist->pp_sds[i]->psz_module ) )
133         {
134             PL_UNLOCK;
135             return VLC_TRUE;
136         }
137     }
138     PL_UNLOCK;
139     return VLC_FALSE;
140 }
141
142 static void RunSD( services_discovery_t *p_sd )
143 {
144     p_sd->pf_run( p_sd );
145     return;
146 }