1 /*****************************************************************************
2 * announce.c : announce handler
3 *****************************************************************************
4 * Copyright (C) 2002-2004 VideoLAN
7 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* free() */
28 #include <stdio.h> /* sprintf() */
29 #include <string.h> /* strerror() */
34 /*****************************************************************************
36 *****************************************************************************/
37 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
40 /****************************************************************************
42 ****************************************************************************/
45 * Register a new session with the announce handler
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
52 int sout_AnnounceRegister( sout_instance_t *p_sout,
53 session_descriptor_t *p_session,
54 announce_method_t *p_method )
57 announce_handler_t *p_announce = (announce_handler_t*)
58 vlc_object_find( p_sout,
64 msg_Dbg( p_sout, "No announce handler found, creating one" );
65 p_announce = announce_HandlerCreate( p_sout );
68 msg_Err( p_sout, "Creation failed" );
71 vlc_object_yield( p_announce );
72 msg_Dbg( p_sout,"Creation done" );
75 i_ret = announce_Register( p_announce, p_session, p_method );
76 vlc_object_release( p_announce );
82 * Register a new session with the announce handler, using a pregenerated SDP
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
89 session_descriptor_t *sout_AnnounceRegisterSDP( sout_instance_t *p_sout,
90 char *psz_sdp, announce_method_t *p_method )
92 session_descriptor_t *p_session;
93 announce_handler_t *p_announce = (announce_handler_t*)
94 vlc_object_find( p_sout,
99 msg_Dbg( p_sout, "no announce handler found, creating one" );
100 p_announce = announce_HandlerCreate( p_sout );
103 msg_Err( p_sout, "Creation failed" );
106 vlc_object_yield( p_announce );
109 if( p_method->i_type != METHOD_TYPE_SAP )
111 msg_Warn( p_sout,"forcing SAP announcement");
114 p_session = sout_AnnounceSessionCreate();
115 p_session->psz_sdp = strdup( psz_sdp );
116 announce_Register( p_announce, p_session, p_method );
118 vlc_object_release( p_announce );
123 * UnRegister an existing session
125 * \param p_sout a sout instance structure
126 * \param p_session the session descriptor
127 * \return VLC_SUCCESS or an error
129 int sout_AnnounceUnRegister( sout_instance_t *p_sout,
130 session_descriptor_t *p_session )
133 announce_handler_t *p_announce = (announce_handler_t*)
134 vlc_object_find( p_sout,
139 msg_Dbg( p_sout, "Unable to remove announce: no announce handler" );
142 i_ret = announce_UnRegister( p_announce, p_session );
144 vlc_object_release( p_announce );
150 * Create and initialize a session descriptor
152 * \return a new session descriptor
154 session_descriptor_t * sout_AnnounceSessionCreate()
156 session_descriptor_t *p_session;
158 p_session = (session_descriptor_t *)malloc( sizeof(session_descriptor_t));
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;
173 * Destroy a session descriptor and free all
175 * \param p_session the session to destroy
178 void sout_AnnounceSessionDestroy( session_descriptor_t *p_session )
182 FREE( p_session->psz_name );
183 FREE( p_session->psz_uri );
184 FREE( p_session->psz_sdp );
190 * Create and initialize an announcement method structure
192 * \param i_type METHOD_TYPE_SAP or METHOD_TYPE_SLP
193 * \return a new announce_method structure
195 announce_method_t * sout_AnnounceMethodCreate( int i_type )
197 announce_method_t *p_method;
199 p_method = (announce_method_t *)malloc( sizeof(announce_method_t) );
203 p_method->i_type = i_type;
204 if( i_type == METHOD_TYPE_SAP )
207 p_method->psz_address = NULL;
208 p_method->i_ip_version = 4 ;
209 p_method->psz_ipv6_scope = strdup("8");
215 /************************************************************************
216 * Announce handler functions (private)
217 ************************************************************************/
220 * Create the announce handler object
222 * \param p_this a vlc_object structure
223 * \return the new announce handler or NULL on error
225 announce_handler_t *__announce_HandlerCreate( vlc_object_t *p_this )
227 announce_handler_t *p_announce;
229 p_announce = vlc_object_create( p_this, VLC_OBJECT_ANNOUNCE );
233 msg_Err( p_this, "out of memory" );
237 p_announce->p_sap = NULL;
239 vlc_object_attach( p_announce, p_this->p_vlc);
246 * Destroy a announce handler object
248 * \param p_announce the announce handler to destroy
249 * \return VLC_SUCCESS or an error
251 int announce_HandlerDestroy( announce_handler_t *p_announce )
254 if( p_announce->p_sap )
256 p_announce->p_sap->b_die = VLC_TRUE;
257 /* Wait for the SAP thread to exit */
258 vlc_thread_join( p_announce->p_sap );
259 announce_SAPHandlerDestroy( p_announce->p_sap );
262 /* Free the structure */
263 vlc_object_destroy( p_announce );
268 /* Register an announce */
269 int announce_Register( announce_handler_t *p_announce,
270 session_descriptor_t *p_session,
271 announce_method_t *p_method )
274 msg_Dbg( p_announce, "registering announce");
275 if( p_method->i_type == METHOD_TYPE_SAP )
277 /* Do we already have a SAP announce handler ? */
278 if( !p_announce->p_sap )
280 sap_handler_t *p_sap = announce_SAPHandlerCreate( p_announce );
281 msg_Dbg( p_announce, "creating SAP announce handler");
284 msg_Err( p_announce, "SAP handler creation failed" );
287 p_announce->p_sap = p_sap;
289 /* this will set p_session->p_sap for later deletion */
290 msg_Dbg( p_announce, "adding SAP session");
291 p_announce->p_sap->pf_add( p_announce->p_sap, p_session, p_method );
293 else if( p_method->i_type == METHOD_TYPE_SLP )
295 msg_Dbg( p_announce, "SLP unsupported at the moment" );
300 msg_Dbg( p_announce, "Announce type unsupported" );
307 /* Unregister an announce */
308 int announce_UnRegister( announce_handler_t *p_announce,
309 session_descriptor_t *p_session )
311 msg_Dbg( p_announce, "unregistering announce" );
312 if( p_session->p_sap != NULL ) /* SAP Announce */
314 if( !p_announce->p_sap )
316 msg_Err( p_announce, "can't remove announce, no SAP handler");
319 p_announce->p_sap->pf_del( p_announce->p_sap, p_session );