1 /*****************************************************************************
2 * announce.c : announce handler
3 *****************************************************************************
4 * Copyright (C) 2002-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* free() */
28 #include <stdio.h> /* sprintf() */
29 #include <string.h> /* strerror() */
34 /****************************************************************************
36 ****************************************************************************/
39 * Register a new session with the announce handler
41 * \param p_sout a sout instance structure
42 * \param p_session a session descriptor
43 * \param p_method an announce method descriptor
44 * \return VLC_SUCCESS or an error
46 int sout_AnnounceRegister( sout_instance_t *p_sout,
47 session_descriptor_t *p_session,
48 announce_method_t *p_method )
51 announce_handler_t *p_announce = (announce_handler_t*)
52 vlc_object_find( p_sout,
58 msg_Dbg( p_sout, "No announce handler found, creating one" );
59 p_announce = announce_HandlerCreate( p_sout );
62 msg_Err( p_sout, "Creation failed" );
65 vlc_object_yield( p_announce );
66 msg_Dbg( p_sout, "creation done" );
69 i_ret = announce_Register( p_announce, p_session, p_method );
70 vlc_object_release( p_announce );
76 * Register a new session with the announce handler, using a pregenerated SDP
78 * \param p_sout a sout instance structure
79 * \param psz_sdp the SDP to register
80 * \param psz_uri session URI (needed for SAP address auto detection
81 * \param p_method an announce method descriptor
82 * \return the new session descriptor structure
84 session_descriptor_t *sout_AnnounceRegisterSDP( sout_instance_t *p_sout,
85 const char *psz_sdp, const char *psz_uri,
86 announce_method_t *p_method )
88 session_descriptor_t *p_session;
89 announce_handler_t *p_announce = (announce_handler_t*)
90 vlc_object_find( p_sout,
95 msg_Dbg( p_sout, "no announce handler found, creating one" );
96 p_announce = announce_HandlerCreate( p_sout );
99 msg_Err( p_sout, "Creation failed" );
102 vlc_object_yield( p_announce );
105 if( p_method->i_type != METHOD_TYPE_SAP )
107 msg_Warn( p_sout, "forcing SAP announcement");
110 p_session = sout_AnnounceSessionCreate();
111 p_session->psz_sdp = strdup( psz_sdp );
112 p_session->psz_uri = strdup( psz_uri );
113 announce_Register( p_announce, p_session, p_method );
115 vlc_object_release( p_announce );
120 * UnRegister an existing session
122 * \param p_sout a sout instance structure
123 * \param p_session the session descriptor
124 * \return VLC_SUCCESS or an error
126 int sout_AnnounceUnRegister( sout_instance_t *p_sout,
127 session_descriptor_t *p_session )
130 announce_handler_t *p_announce = (announce_handler_t*)
131 vlc_object_find( p_sout,
136 msg_Dbg( p_sout, "unable to remove announce: no announce handler" );
139 i_ret = announce_UnRegister( p_announce, p_session );
141 vlc_object_release( p_announce );
147 * Create and initialize a session descriptor
149 * \return a new session descriptor
151 session_descriptor_t * sout_AnnounceSessionCreate(void)
153 session_descriptor_t *p_session;
155 p_session = (session_descriptor_t *)malloc( sizeof(session_descriptor_t));
156 if (p_session == NULL)
159 memset (p_session, 0, sizeof (*p_session));
164 * Destroy a session descriptor and free all
166 * \param p_session the session to destroy
169 void sout_AnnounceSessionDestroy( session_descriptor_t *p_session )
173 FREENULL( p_session->psz_name );
174 FREENULL( p_session->psz_group );
175 FREENULL( p_session->psz_uri );
176 FREENULL( p_session->psz_sdp );
182 * Create and initialize an announcement method structure
184 * \param i_type METHOD_TYPE_SAP
185 * \return a new announce_method structure
187 announce_method_t * sout_AnnounceMethodCreate( int i_type )
189 announce_method_t *p_method;
191 p_method = (announce_method_t *)malloc( sizeof(announce_method_t) );
192 if( p_method == NULL )
195 p_method->i_type = i_type;
199 /************************************************************************
200 * Announce handler functions (private)
201 ************************************************************************/
204 * Create the announce handler object
206 * \param p_this a vlc_object structure
207 * \return the new announce handler or NULL on error
209 announce_handler_t *__announce_HandlerCreate( vlc_object_t *p_this )
211 announce_handler_t *p_announce;
213 p_announce = vlc_object_create( p_this, VLC_OBJECT_ANNOUNCE );
217 msg_Err( p_this, "out of memory" );
221 p_announce->p_sap = NULL;
222 vlc_object_attach( p_announce, p_this->p_libvlc);
228 * Destroy a announce handler object
230 * \param p_announce the announce handler to destroy
231 * \return VLC_SUCCESS or an error
233 int announce_HandlerDestroy( announce_handler_t *p_announce )
236 if( p_announce->p_sap )
238 p_announce->p_sap->b_die = VLC_TRUE;
239 /* Wait for the SAP thread to exit */
240 vlc_thread_join( p_announce->p_sap );
241 announce_SAPHandlerDestroy( p_announce->p_sap );
244 /* Free the structure */
245 vlc_object_destroy( p_announce );
250 /* Register an announce */
251 int announce_Register( announce_handler_t *p_announce,
252 session_descriptor_t *p_session,
253 announce_method_t *p_method )
256 msg_Dbg( p_announce, "registering announce");
257 if( p_method->i_type == METHOD_TYPE_SAP )
259 /* Do we already have a SAP announce handler ? */
260 if( !p_announce->p_sap )
262 sap_handler_t *p_sap = announce_SAPHandlerCreate( p_announce );
263 msg_Dbg( p_announce, "creating SAP announce handler");
266 msg_Err( p_announce, "SAP handler creation failed" );
269 p_announce->p_sap = p_sap;
271 /* this will set p_session->p_sap for later deletion */
272 msg_Dbg( p_announce, "adding SAP session");
273 p_announce->p_sap->pf_add( p_announce->p_sap, p_session );
277 msg_Dbg( p_announce, "announce type unsupported" );
284 /* Unregister an announce */
285 int announce_UnRegister( announce_handler_t *p_announce,
286 session_descriptor_t *p_session )
288 msg_Dbg( p_announce, "unregistering announce" );
289 if( p_session->p_sap != NULL ) /* SAP Announce */
291 if( !p_announce->p_sap )
293 msg_Err( p_announce, "can't remove announce, no SAP handler");
296 p_announce->p_sap->pf_del( p_announce->p_sap, p_session );