1 /*****************************************************************************
2 * announce.c : announce handler
3 *****************************************************************************
4 * Copyright (C) 2002-2007 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 *****************************************************************************/
29 #include "stream_output.h"
33 /* Private functions for the announce handler */
34 static announce_handler_t* announce_HandlerCreate( vlc_object_t *);
35 static int announce_Register( announce_handler_t *p_announce,
36 session_descriptor_t *p_session,
37 announce_method_t *p_method );
38 static int announce_UnRegister( announce_handler_t *p_announce,
39 session_descriptor_t *p_session );
41 struct announce_method_t
45 /****************************************************************************
47 ****************************************************************************/
50 * Create and initialize a session descriptor
52 * \return a new session descriptor
54 static session_descriptor_t * sout_AnnounceSessionCreate (vlc_object_t *obj,
57 return calloc (1, sizeof (session_descriptor_t));
61 * Register a new session with the announce handler, using a pregenerated SDP
63 * \param p_sout a sout instance structure
64 * \param psz_sdp the SDP to register
65 * \param psz_uri session address (needed for SAP address auto detection)
66 * \param p_method an announce method descriptor
67 * \return the new session descriptor structure
69 session_descriptor_t *
70 sout_AnnounceRegisterSDP( sout_instance_t *p_sout, const char *cfgpref,
71 const char *psz_sdp, const char *psz_uri,
72 announce_method_t *p_method )
74 session_descriptor_t *p_session;
75 announce_handler_t *p_announce = (announce_handler_t*)
76 vlc_object_find( p_sout,
81 msg_Dbg( p_sout, "no announce handler found, creating one" );
82 p_announce = announce_HandlerCreate( VLC_OBJECT( p_sout ) );
85 msg_Err( p_sout, "Creation failed" );
88 vlc_object_yield( p_announce );
91 p_session = sout_AnnounceSessionCreate(VLC_OBJECT (p_sout), cfgpref);
92 p_session->psz_sdp = strdup( psz_sdp );
94 /* GRUIK. We should not convert back-and-forth from string to numbers */
96 if (vlc_getaddrinfo (VLC_OBJECT (p_sout), psz_uri, 0, NULL, &res) == 0)
98 if (res->ai_addrlen <= sizeof (p_session->addr))
99 memcpy (&p_session->addr, res->ai_addr,
100 p_session->addrlen = res->ai_addrlen);
101 vlc_freeaddrinfo (res);
104 announce_Register( p_announce, p_session, p_method );
106 vlc_object_release( p_announce );
111 * UnRegister an existing session
113 * \param p_sout a sout instance structure
114 * \param p_session the session descriptor
115 * \return VLC_SUCCESS or an error
117 int sout_AnnounceUnRegister( sout_instance_t *p_sout,
118 session_descriptor_t *p_session )
121 announce_handler_t *p_announce = (announce_handler_t*)
122 vlc_object_find( p_sout,
127 msg_Dbg( p_sout, "unable to remove announce: no announce handler" );
130 i_ret = announce_UnRegister( p_announce, p_session );
132 vlc_object_release( p_announce );
138 * Destroy a session descriptor and free all
140 * \param p_session the session to destroy
143 void sout_AnnounceSessionDestroy( session_descriptor_t *p_session )
149 * \return the SAP announce method
151 announce_method_t * sout_SAPMethod (void)
156 void sout_MethodRelease (announce_method_t *m)
158 assert (m == &sap_method);
161 /************************************************************************
162 * Announce handler functions (private)
163 ************************************************************************/
166 * Create the announce handler object
168 * \param p_this a vlc_object structure
169 * \return the new announce handler or NULL on error
171 static announce_handler_t *announce_HandlerCreate( vlc_object_t *p_this )
173 announce_handler_t *p_announce;
175 p_announce = vlc_object_create( p_this, VLC_OBJECT_ANNOUNCE );
179 msg_Err( p_this, "out of memory" );
183 p_announce->p_sap = NULL;
184 vlc_object_attach( p_announce, p_this->p_libvlc);
190 * Destroy a announce handler object
192 * \param p_announce the announce handler to destroy
193 * \return VLC_SUCCESS or an error
195 int announce_HandlerDestroy( announce_handler_t *p_announce )
197 if( p_announce->p_sap )
199 vlc_object_kill ((vlc_object_t *)p_announce->p_sap);
200 /* Wait for the SAP thread to exit */
201 vlc_thread_join( (vlc_object_t *)p_announce->p_sap );
202 announce_SAPHandlerDestroy( p_announce->p_sap );
205 /* Free the structure */
206 vlc_object_destroy( p_announce );
211 /* Register an announce */
212 static int announce_Register( announce_handler_t *p_announce,
213 session_descriptor_t *p_session,
214 announce_method_t *p_method )
216 if (p_method == NULL)
219 msg_Dbg( p_announce, "registering announce");
220 if( p_method == &sap_method )
222 /* Do we already have a SAP announce handler ? */
223 if( !p_announce->p_sap )
225 sap_handler_t *p_sap = announce_SAPHandlerCreate( p_announce );
226 msg_Dbg( p_announce, "creating SAP announce handler");
229 msg_Err( p_announce, "SAP handler creation failed" );
232 p_announce->p_sap = p_sap;
234 /* this will set p_session->p_sap for later deletion */
235 msg_Dbg( p_announce, "adding SAP session");
236 p_announce->p_sap->pf_add( p_announce->p_sap, p_session );
240 msg_Err( p_announce, "announce type unsupported" );
247 /* Unregister an announce */
248 static int announce_UnRegister( announce_handler_t *p_announce,
249 session_descriptor_t *p_session )
251 msg_Dbg( p_announce, "unregistering announce" );
252 if( p_announce->p_sap )
253 p_announce->p_sap->pf_del( p_announce->p_sap, p_session );