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 size_t cfglen = strlen (cfgpref);
61 char varname[cfglen + sizeof ("description")], *subvar = varname + cfglen;
62 strcpy (varname, cfgpref);
64 session_descriptor_t *p_session = calloc (1, sizeof (*p_session));
65 if (p_session == NULL)
68 strcpy (subvar, "name");
69 p_session->psz_name = var_GetNonEmptyString (obj, varname);
70 strcpy (subvar, "group");
71 p_session->psz_group = var_GetNonEmptyString (obj, varname);
73 strcpy (subvar, "description");
74 p_session->description = var_GetNonEmptyString (obj, varname);
75 strcpy (subvar, "url");
76 p_session->url = var_GetNonEmptyString (obj, varname);
77 strcpy (subvar, "email");
78 p_session->email = var_GetNonEmptyString (obj, varname);
79 strcpy (subvar, "phone");
80 p_session->phone = var_GetNonEmptyString (obj, varname);
86 * Register a new session with the announce handler, using a pregenerated SDP
88 * \param p_sout a sout instance structure
89 * \param psz_sdp the SDP to register
90 * \param psz_uri session address (needed for SAP address auto detection)
91 * \param p_method an announce method descriptor
92 * \return the new session descriptor structure
94 session_descriptor_t *
95 sout_AnnounceRegisterSDP( sout_instance_t *p_sout, const char *cfgpref,
96 const char *psz_sdp, const char *psz_uri,
97 announce_method_t *p_method )
99 session_descriptor_t *p_session;
100 announce_handler_t *p_announce = (announce_handler_t*)
101 vlc_object_find( p_sout,
106 msg_Dbg( p_sout, "no announce handler found, creating one" );
107 p_announce = announce_HandlerCreate( VLC_OBJECT( p_sout ) );
110 msg_Err( p_sout, "Creation failed" );
113 vlc_object_yield( p_announce );
116 p_session = sout_AnnounceSessionCreate(VLC_OBJECT (p_sout), cfgpref);
117 p_session->psz_sdp = strdup( psz_sdp );
119 /* GRUIK. We should not convert back-and-forth from string to numbers */
120 struct addrinfo *res;
121 if (vlc_getaddrinfo (VLC_OBJECT (p_sout), psz_uri, 0, NULL, &res) == 0)
123 if (res->ai_addrlen <= sizeof (p_session->addr))
124 memcpy (&p_session->addr, res->ai_addr,
125 p_session->addrlen = res->ai_addrlen);
126 vlc_freeaddrinfo (res);
129 announce_Register( p_announce, p_session, p_method );
131 vlc_object_release( p_announce );
136 * UnRegister an existing session
138 * \param p_sout a sout instance structure
139 * \param p_session the session descriptor
140 * \return VLC_SUCCESS or an error
142 int sout_AnnounceUnRegister( sout_instance_t *p_sout,
143 session_descriptor_t *p_session )
146 announce_handler_t *p_announce = (announce_handler_t*)
147 vlc_object_find( p_sout,
152 msg_Dbg( p_sout, "unable to remove announce: no announce handler" );
155 i_ret = announce_UnRegister( p_announce, p_session );
157 vlc_object_release( p_announce );
163 * Destroy a session descriptor and free all
165 * \param p_session the session to destroy
168 void sout_AnnounceSessionDestroy( session_descriptor_t *p_session )
172 free (p_session->psz_name);
173 free (p_session->psz_group);
174 free (p_session->psz_sdp);
175 free (p_session->description);
176 free (p_session->sdpformat);
177 free (p_session->url);
178 free (p_session->email);
179 free (p_session->phone);
185 * \return the SAP announce method
187 announce_method_t * sout_SAPMethod (void)
192 void sout_MethodRelease (announce_method_t *m)
194 assert (m == &sap_method);
197 /************************************************************************
198 * Announce handler functions (private)
199 ************************************************************************/
202 * Create the announce handler object
204 * \param p_this a vlc_object structure
205 * \return the new announce handler or NULL on error
207 static announce_handler_t *announce_HandlerCreate( vlc_object_t *p_this )
209 announce_handler_t *p_announce;
211 p_announce = vlc_object_create( p_this, VLC_OBJECT_ANNOUNCE );
215 msg_Err( p_this, "out of memory" );
219 p_announce->p_sap = NULL;
220 vlc_object_attach( p_announce, p_this->p_libvlc);
226 * Destroy a announce handler object
228 * \param p_announce the announce handler to destroy
229 * \return VLC_SUCCESS or an error
231 int announce_HandlerDestroy( announce_handler_t *p_announce )
233 if( p_announce->p_sap )
235 vlc_object_kill ((vlc_object_t *)p_announce->p_sap);
236 /* Wait for the SAP thread to exit */
237 vlc_thread_join( (vlc_object_t *)p_announce->p_sap );
238 announce_SAPHandlerDestroy( p_announce->p_sap );
241 /* Free the structure */
242 vlc_object_destroy( p_announce );
247 /* Register an announce */
248 static int announce_Register( announce_handler_t *p_announce,
249 session_descriptor_t *p_session,
250 announce_method_t *p_method )
252 if (p_method == NULL)
255 msg_Dbg( p_announce, "registering announce");
256 if( p_method == &sap_method )
258 /* Do we already have a SAP announce handler ? */
259 if( !p_announce->p_sap )
261 sap_handler_t *p_sap = announce_SAPHandlerCreate( p_announce );
262 msg_Dbg( p_announce, "creating SAP announce handler");
265 msg_Err( p_announce, "SAP handler creation failed" );
268 p_announce->p_sap = p_sap;
270 /* this will set p_session->p_sap for later deletion */
271 msg_Dbg( p_announce, "adding SAP session");
272 p_announce->p_sap->pf_add( p_announce->p_sap, p_session );
276 msg_Err( p_announce, "announce type unsupported" );
283 /* Unregister an announce */
284 static int announce_UnRegister( announce_handler_t *p_announce,
285 session_descriptor_t *p_session )
287 msg_Dbg( p_announce, "unregistering announce" );
288 if( p_announce->p_sap )
289 p_announce->p_sap->pf_del( p_announce->p_sap, p_session );