]> git.sesse.net Git - vlc/blob - src/stream_output/announce.c
fix two memleaks and remove a bogus net_Close()
[vlc] / src / stream_output / announce.c
1 /*****************************************************************************
2  * announce.c : announce handler
3  *****************************************************************************
4  * Copyright (C) 2002-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                                /* free() */
28 #include <stdio.h>                                              /* sprintf() */
29 #include <string.h>                                            /* strerror() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/sout.h>
33
34 /*****************************************************************************
35  * Local prototypes
36  *****************************************************************************/
37 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
38
39
40 /****************************************************************************
41  * Sout-side functions
42  ****************************************************************************/
43
44 /**
45  *  Register a new session with the announce handler
46  *
47  * \param p_sout a sout instance structure
48  * \param p_session a session descriptor
49  * \param p_method an announce method descriptor
50  * \return VLC_SUCCESS or an error
51  */
52 int sout_AnnounceRegister( sout_instance_t *p_sout,
53                        session_descriptor_t *p_session,
54                        announce_method_t *p_method )
55 {
56     int i_ret;
57     announce_handler_t *p_announce = (announce_handler_t*)
58                               vlc_object_find( p_sout,
59                                               VLC_OBJECT_ANNOUNCE,
60                                               FIND_ANYWHERE );
61
62     if( !p_announce )
63     {
64         msg_Dbg( p_sout, "No announce handler found, creating one" );
65         p_announce = announce_HandlerCreate( p_sout );
66         if( !p_announce )
67         {
68             msg_Err( p_sout, "Creation failed" );
69             return VLC_ENOMEM;
70         }
71         vlc_object_yield( p_announce );
72         msg_Dbg( p_sout,"Creation done" );
73     }
74
75     i_ret = announce_Register( p_announce, p_session, p_method );
76     vlc_object_release( p_announce );
77
78     return i_ret;
79 }
80
81 /**
82  *  Register a new session with the announce handler, using a pregenerated SDP
83  *
84  * \param p_sout a sout instance structure
85  * \param psz_sdp the SDP to register
86  * \param p_method an announce method descriptor
87  * \return the new session descriptor structure
88  */
89 session_descriptor_t *sout_AnnounceRegisterSDP( sout_instance_t *p_sout,
90                           char *psz_sdp, announce_method_t *p_method )
91 {
92     session_descriptor_t *p_session;
93     announce_handler_t *p_announce = (announce_handler_t*)
94                                      vlc_object_find( p_sout,
95                                               VLC_OBJECT_ANNOUNCE,
96                                               FIND_ANYWHERE );
97     if( !p_announce )
98     {
99         msg_Dbg( p_sout, "no announce handler found, creating one" );
100         p_announce = announce_HandlerCreate( p_sout );
101         if( !p_announce )
102         {
103             msg_Err( p_sout, "Creation failed" );
104             return NULL;
105         }
106         vlc_object_yield( p_announce );
107     }
108
109     if( p_method->i_type != METHOD_TYPE_SAP )
110     {
111         msg_Warn( p_sout,"forcing SAP announcement");
112     }
113
114     p_session = sout_AnnounceSessionCreate();
115     p_session->psz_sdp = strdup( psz_sdp );
116     announce_Register( p_announce, p_session, p_method );
117
118     vlc_object_release( p_announce );
119     return p_session;
120 }
121
122 /**
123  *  UnRegister an existing session
124  *
125  * \param p_sout a sout instance structure
126  * \param p_session the session descriptor
127  * \return VLC_SUCCESS or an error
128  */
129 int sout_AnnounceUnRegister( sout_instance_t *p_sout,
130                              session_descriptor_t *p_session )
131 {
132     int i_ret;
133     announce_handler_t *p_announce = (announce_handler_t*)
134                               vlc_object_find( p_sout,
135                                               VLC_OBJECT_ANNOUNCE,
136                                               FIND_ANYWHERE );
137     if( !p_announce )
138     {
139         msg_Dbg( p_sout, "Unable to remove announce: no announce handler" );
140         return VLC_ENOOBJ;
141     }
142     i_ret  = announce_UnRegister( p_announce, p_session );
143
144     vlc_object_release( p_announce );
145
146     return i_ret;
147 }
148
149 /**
150  * Create and initialize a session descriptor
151  *
152  * \return a new session descriptor
153  */
154 session_descriptor_t * sout_AnnounceSessionCreate()
155 {
156     session_descriptor_t *p_session;
157
158     p_session = (session_descriptor_t *)malloc( sizeof(session_descriptor_t));
159
160     if( p_session)
161     {
162         p_session->p_sap = NULL;
163         p_session->psz_sdp = NULL;
164         p_session->psz_name = NULL;
165         p_session->psz_uri = NULL;
166         p_session->i_port = 0;
167         p_session->psz_group = NULL;
168     }
169
170     return p_session;
171 }
172
173 /**
174  * Destroy a session descriptor and free all
175  *
176  * \param p_session the session to destroy
177  * \return Nothing
178  */
179 void sout_AnnounceSessionDestroy( session_descriptor_t *p_session )
180 {
181     if( p_session )
182     {
183         FREE( p_session->psz_name );
184         FREE( p_session->psz_group );
185         FREE( p_session->psz_uri );
186         FREE( p_session->psz_sdp );
187         free( p_session );
188     }
189 }
190
191 /**
192  * Create and initialize an announcement method structure
193  *
194  * \param i_type METHOD_TYPE_SAP or METHOD_TYPE_SLP
195  * \return a new announce_method structure
196  */
197 announce_method_t * sout_AnnounceMethodCreate( int i_type )
198 {
199     announce_method_t *p_method;
200
201     p_method = (announce_method_t *)malloc( sizeof(announce_method_t) );
202
203     if( p_method )
204     {
205         p_method->i_type = i_type;
206         if( i_type == METHOD_TYPE_SAP )
207         {
208             /* Default values */
209             p_method->psz_address = NULL;
210             p_method->i_ip_version = 4 ;
211             p_method->sz_ipv6_scope = '\0';
212         }
213     }
214     return p_method;
215 }
216
217 /************************************************************************
218  * Announce handler functions (private)
219  ************************************************************************/
220
221 /**
222  * Create the announce handler object
223  *
224  * \param p_this a vlc_object structure
225  * \return the new announce handler or NULL on error
226  */
227 announce_handler_t *__announce_HandlerCreate( vlc_object_t *p_this )
228 {
229     announce_handler_t *p_announce;
230
231     p_announce = vlc_object_create( p_this, VLC_OBJECT_ANNOUNCE );
232
233     if( !p_announce )
234     {
235         msg_Err( p_this, "out of memory" );
236         return NULL;
237     }
238
239     p_announce->p_sap = NULL;
240
241     vlc_object_attach( p_announce, p_this->p_vlc);
242
243
244     return p_announce;
245 }
246
247 /**
248  * Destroy a  announce handler object
249  *
250  * \param p_announce the announce handler to destroy
251  * \return VLC_SUCCESS or an error
252  */
253 int announce_HandlerDestroy( announce_handler_t *p_announce )
254 {
255
256     if( p_announce->p_sap )
257     {
258         p_announce->p_sap->b_die = VLC_TRUE;
259         /* Wait for the SAP thread to exit */
260         vlc_thread_join( p_announce->p_sap );
261         announce_SAPHandlerDestroy( p_announce->p_sap );
262     }
263
264     /* Free the structure */
265     vlc_object_destroy( p_announce );
266
267     return VLC_SUCCESS;
268 }
269
270 /* Register an announce */
271 int announce_Register( announce_handler_t *p_announce,
272                        session_descriptor_t *p_session,
273                        announce_method_t *p_method )
274 {
275
276     msg_Dbg( p_announce, "registering announce");
277     if( p_method->i_type == METHOD_TYPE_SAP )
278     {
279         /* Do we already have a SAP announce handler ? */
280         if( !p_announce->p_sap )
281         {
282             sap_handler_t *p_sap = announce_SAPHandlerCreate( p_announce );
283             msg_Dbg( p_announce, "creating SAP announce handler");
284             if( !p_sap )
285             {
286                 msg_Err( p_announce, "SAP handler creation failed" );
287                 return VLC_ENOOBJ;
288             }
289             p_announce->p_sap = p_sap;
290         }
291         /* this will set p_session->p_sap for later deletion */
292         msg_Dbg( p_announce, "adding SAP session");
293         p_announce->p_sap->pf_add( p_announce->p_sap, p_session, p_method );
294     }
295     else if( p_method->i_type == METHOD_TYPE_SLP )
296     {
297         msg_Dbg( p_announce, "SLP unsupported at the moment" );
298         return VLC_EGENERIC;
299     }
300     else
301     {
302         msg_Dbg( p_announce, "Announce type unsupported" );
303         return VLC_EGENERIC;
304     }
305     return VLC_SUCCESS;;
306 }
307
308
309 /* Unregister an announce */
310 int announce_UnRegister( announce_handler_t *p_announce,
311                   session_descriptor_t *p_session )
312 {
313     msg_Dbg( p_announce, "unregistering announce" );
314     if( p_session->p_sap != NULL ) /* SAP Announce */
315     {
316         if( !p_announce->p_sap )
317         {
318             msg_Err( p_announce, "can't remove announce, no SAP handler");
319             return VLC_ENOOBJ;
320         }
321         p_announce->p_sap->pf_del( p_announce->p_sap, p_session );
322     }
323     return VLC_SUCCESS;
324 }