]> git.sesse.net Git - vlc/blob - src/playlist/services_discovery.c
Use vlc_object_kill(). Needs triple checking.
[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     const char *psz_parser = psz_modules ?: "";
32     int retval = VLC_SUCCESS;
33
34     for (;;)
35     {
36         while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
37             psz_parser++;
38
39         if( *psz_parser == '\0' )
40             break;
41
42         const char *psz_next = strchr( psz_parser, ':' );
43         if( psz_next == NULL )
44             psz_next = psz_parser + strlen( psz_parser );
45
46         char psz_plugin[psz_next - psz_parser + 1];
47         memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
48         psz_plugin[sizeof (psz_plugin) - 1] = '\0';
49         psz_parser = psz_next;
50
51         /* Perform the addition */
52         msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
53         services_discovery_t *p_sd = vlc_object_create( p_playlist,
54                                                         VLC_OBJECT_SD );
55         if( p_sd == NULL )
56             return VLC_ENOMEM;
57
58         p_sd->pf_run = NULL;
59         p_sd->p_module = module_Need( p_sd, "services_discovery", psz_plugin, 0 );
60
61         if( p_sd->p_module == NULL )
62         {
63             msg_Err( p_playlist, "no suitable services discovery module" );
64             vlc_object_destroy( p_sd );
65             retval = VLC_EGENERIC;
66             continue;
67         }
68         p_sd->psz_module = strdup( psz_plugin );
69         p_sd->b_die = VLC_FALSE; /* FIXME */
70
71         PL_LOCK;
72         TAB_APPEND( p_playlist->i_sds, p_playlist->pp_sds, p_sd );
73         PL_UNLOCK;
74
75         if ((p_sd->pf_run != NULL)
76          && vlc_thread_create( p_sd, "services_discovery", RunSD,
77                                VLC_THREAD_PRIORITY_LOW, VLC_FALSE))
78         {
79             msg_Err( p_sd, "cannot create services discovery thread" );
80             vlc_object_destroy( p_sd );
81             retval = VLC_EGENERIC;
82             continue;
83         }
84     }
85
86     return retval;
87 }
88
89 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
90                                        const char *psz_module )
91 {
92     int i;
93     services_discovery_t *p_sd = NULL;
94
95     PL_LOCK;
96     for( i = 0 ; i< p_playlist->i_sds ; i ++ )
97     {
98         if( !strcmp( psz_module, p_playlist->pp_sds[i]->psz_module ) )
99         {
100             p_sd = p_playlist->pp_sds[i];
101             REMOVE_ELEM( p_playlist->pp_sds, p_playlist->i_sds, i );
102             break;
103         }
104     }
105     PL_UNLOCK;
106
107     if( p_sd )
108     {
109         vlc_object_kill( p_sd );
110         if( p_sd->pf_run ) vlc_thread_join( p_sd );
111
112         free( p_sd->psz_module );
113         module_Unneed( p_sd, p_sd->p_module );
114         vlc_object_destroy( p_sd );
115     }
116     else
117     {
118         msg_Warn( p_playlist, "module %s is not loaded", psz_module );
119         return VLC_EGENERIC;
120     }
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 }