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