]> git.sesse.net Git - vlc/blob - src/stream_output/announce.c
Fix some memleaks
[vlc] / src / stream_output / announce.c
1 /*****************************************************************************
2  * announce.c : announce handler
3  *****************************************************************************
4  * Copyright (C) 2002-2007 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                                /* free() */
28 #include <stdio.h>                                              /* sprintf() */
29 #include <string.h>                                            /* strerror() */
30 #include <assert.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc_sout.h>
34 #include <vlc_network.h> /* FIXME: fix RegisterSDP() and remove this */
35 #include "stream_output.h"
36
37 struct announce_method_t
38 {
39 } sap_method;
40
41 /****************************************************************************
42  * Sout-side functions
43  ****************************************************************************/
44
45 /**
46  *  Register a new session with the announce handler
47  *
48  * \param p_sout a sout instance structure
49  * \param p_session a session descriptor
50  * \param p_method an announce method descriptor
51  * \return VLC_SUCCESS or an error
52  */
53 int sout_AnnounceRegister( sout_instance_t *p_sout,
54                        session_descriptor_t *p_session,
55                        announce_method_t *p_method )
56 {
57     int i_ret;
58     announce_handler_t *p_announce = (announce_handler_t*)
59                               vlc_object_find( p_sout,
60                                               VLC_OBJECT_ANNOUNCE,
61                                               FIND_ANYWHERE );
62
63     if( !p_announce )
64     {
65         msg_Dbg( p_sout, "No announce handler found, creating one" );
66         p_announce = announce_HandlerCreate( p_sout );
67         if( !p_announce )
68         {
69             msg_Err( p_sout, "Creation failed" );
70             return VLC_ENOMEM;
71         }
72         vlc_object_yield( p_announce );
73         msg_Dbg( p_sout, "creation done" );
74     }
75
76     i_ret = announce_Register( p_announce, p_session, p_method );
77     vlc_object_release( p_announce );
78
79     return i_ret;
80 }
81
82 /**
83  *  Register a new session with the announce handler, using a pregenerated SDP
84  *
85  * \param p_sout a sout instance structure
86  * \param psz_sdp the SDP to register
87  * \param psz_uri session address (needed for SAP address auto detection)
88  * \param p_method an announce method descriptor
89  * \return the new session descriptor structure
90  */
91 session_descriptor_t *
92 sout_AnnounceRegisterSDP( sout_instance_t *p_sout, const char *cfgpref,
93                           const char *psz_sdp, const char *psz_uri,
94                           announce_method_t *p_method )
95 {
96     session_descriptor_t *p_session;
97     announce_handler_t *p_announce = (announce_handler_t*)
98                                      vlc_object_find( p_sout,
99                                               VLC_OBJECT_ANNOUNCE,
100                                               FIND_ANYWHERE );
101     if( !p_announce )
102     {
103         msg_Dbg( p_sout, "no announce handler found, creating one" );
104         p_announce = announce_HandlerCreate( p_sout );
105         if( !p_announce )
106         {
107             msg_Err( p_sout, "Creation failed" );
108             return NULL;
109         }
110         vlc_object_yield( p_announce );
111     }
112
113     p_session = sout_AnnounceSessionCreate(VLC_OBJECT (p_sout), cfgpref);
114     p_session->psz_sdp = strdup( psz_sdp );
115
116     /* GRUIK. We should not convert back-and-forth from string to numbers */
117     struct addrinfo *res;
118     if (vlc_getaddrinfo (VLC_OBJECT (p_sout), psz_uri, 0, NULL, &res) == 0)
119     {
120         if (res->ai_addrlen <= sizeof (p_session->addr))
121             memcpy (&p_session->addr, res->ai_addr,
122                     p_session->addrlen = res->ai_addrlen);
123         vlc_freeaddrinfo (res);
124     }
125
126     announce_Register( p_announce, p_session, p_method );
127
128     vlc_object_release( p_announce );
129     return p_session;
130 }
131
132 /**
133  *  UnRegister an existing session
134  *
135  * \param p_sout a sout instance structure
136  * \param p_session the session descriptor
137  * \return VLC_SUCCESS or an error
138  */
139 int sout_AnnounceUnRegister( sout_instance_t *p_sout,
140                              session_descriptor_t *p_session )
141 {
142     int i_ret;
143     announce_handler_t *p_announce = (announce_handler_t*)
144                               vlc_object_find( p_sout,
145                                               VLC_OBJECT_ANNOUNCE,
146                                               FIND_ANYWHERE );
147     if( !p_announce )
148     {
149         msg_Dbg( p_sout, "unable to remove announce: no announce handler" );
150         return VLC_ENOOBJ;
151     }
152     i_ret  = announce_UnRegister( p_announce, p_session );
153
154     vlc_object_release( p_announce );
155
156     return i_ret;
157 }
158
159 /**
160  * Create and initialize a session descriptor
161  *
162  * \return a new session descriptor
163  */
164 session_descriptor_t * sout_AnnounceSessionCreate (vlc_object_t *obj,
165                                                    const char *cfgpref)
166 {
167     size_t cfglen = strlen (cfgpref);
168     if (cfglen > 100)
169         return NULL;
170
171     char varname[cfglen + sizeof ("description")], *subvar = varname + cfglen;
172     strcpy (varname, cfgpref);
173
174     session_descriptor_t *p_session = calloc (1, sizeof (*p_session));
175     if (p_session == NULL)
176         return NULL;
177
178     strcpy (subvar, "name");
179     p_session->psz_name = var_GetNonEmptyString (obj, varname);
180     strcpy (subvar, "group");
181     p_session->psz_group = var_GetNonEmptyString (obj, varname);
182
183     strcpy (subvar, "description");
184     p_session->description = var_GetNonEmptyString (obj, varname);
185     strcpy (subvar, "url");
186     p_session->url = var_GetNonEmptyString (obj, varname);
187     strcpy (subvar, "email");
188     p_session->email = var_GetNonEmptyString (obj, varname);
189     strcpy (subvar, "phone");
190     p_session->phone = var_GetNonEmptyString (obj, varname);
191
192     return p_session;
193 }
194
195 int sout_SessionSetMedia (vlc_object_t *obj, session_descriptor_t *p_session,
196                           const char *fmt, const char *src, int sport,
197                           const char *dst, int dport)
198 {
199     if ((p_session->sdpformat = strdup (fmt)) == NULL)
200         return VLC_ENOMEM;
201
202     /* GRUIK. We should not convert back-and-forth from string to numbers */
203     struct addrinfo *res;
204     if (vlc_getaddrinfo (obj, dst, dport, NULL, &res) == 0)
205     {
206         if (res->ai_addrlen > sizeof (p_session->addr))
207             goto oflow;
208
209         memcpy (&p_session->addr, res->ai_addr,
210                 p_session->addrlen = res->ai_addrlen);
211         vlc_freeaddrinfo (res);
212     }
213     if (vlc_getaddrinfo (obj, src, sport, NULL, &res) == 0)
214     {
215         if (res->ai_addrlen > sizeof (p_session->orig))
216             goto oflow;
217         memcpy (&p_session->orig, res->ai_addr,
218                p_session->origlen = res->ai_addrlen);
219         vlc_freeaddrinfo (res);
220     }
221     return 0;
222
223 oflow:
224     vlc_freeaddrinfo (res);
225     return VLC_ENOMEM;
226 }
227
228 /**
229  * Destroy a session descriptor and free all
230  *
231  * \param p_session the session to destroy
232  * \return Nothing
233  */
234 void sout_AnnounceSessionDestroy( session_descriptor_t *p_session )
235 {
236     if( p_session )
237     {
238         free (p_session->psz_name);
239         free (p_session->psz_group);
240         free (p_session->psz_sdp);
241         free (p_session->description);
242         free (p_session->sdpformat);
243         free (p_session->url);
244         free (p_session->email);
245         free (p_session->phone);
246         free( p_session );
247     }
248 }
249
250 /**
251  * \return the SAP announce method
252  */
253 announce_method_t * sout_SAPMethod (void)
254 {
255     return &sap_method;
256 }
257
258 void sout_MethodRelease (announce_method_t *m)
259 {
260     assert (m == &sap_method);
261 }
262
263 /************************************************************************
264  * Announce handler functions (private)
265  ************************************************************************/
266
267 /**
268  * Create the announce handler object
269  *
270  * \param p_this a vlc_object structure
271  * \return the new announce handler or NULL on error
272  */
273 announce_handler_t *__announce_HandlerCreate( vlc_object_t *p_this )
274 {
275     announce_handler_t *p_announce;
276
277     p_announce = vlc_object_create( p_this, VLC_OBJECT_ANNOUNCE );
278
279     if( !p_announce )
280     {
281         msg_Err( p_this, "out of memory" );
282         return NULL;
283     }
284
285     p_announce->p_sap = NULL;
286     vlc_object_attach( p_announce, p_this->p_libvlc);
287
288     return p_announce;
289 }
290
291 /**
292  * Destroy a  announce handler object
293  *
294  * \param p_announce the announce handler to destroy
295  * \return VLC_SUCCESS or an error
296  */
297 int announce_HandlerDestroy( announce_handler_t *p_announce )
298 {
299     if( p_announce->p_sap )
300     {
301         ((vlc_object_t *)p_announce->p_sap)->b_die = VLC_TRUE;
302         /* Wait for the SAP thread to exit */
303         vlc_thread_join( (vlc_object_t *)p_announce->p_sap );
304         announce_SAPHandlerDestroy( p_announce->p_sap );
305     }
306
307     /* Free the structure */
308     vlc_object_destroy( p_announce );
309
310     return VLC_SUCCESS;
311 }
312
313 /* Register an announce */
314 int announce_Register( announce_handler_t *p_announce,
315                        session_descriptor_t *p_session,
316                        announce_method_t *p_method )
317 {
318     if (p_method == NULL)
319         return VLC_EGENERIC;
320
321     msg_Dbg( p_announce, "registering announce");
322     if( p_method == &sap_method )
323     {
324         /* Do we already have a SAP announce handler ? */
325         if( !p_announce->p_sap )
326         {
327             sap_handler_t *p_sap = announce_SAPHandlerCreate( p_announce );
328             msg_Dbg( p_announce, "creating SAP announce handler");
329             if( !p_sap )
330             {
331                 msg_Err( p_announce, "SAP handler creation failed" );
332                 return VLC_ENOOBJ;
333             }
334             p_announce->p_sap = p_sap;
335         }
336         /* this will set p_session->p_sap for later deletion */
337         msg_Dbg( p_announce, "adding SAP session");
338         p_announce->p_sap->pf_add( p_announce->p_sap, p_session );
339     }
340     else
341     {
342         msg_Err( p_announce, "announce type unsupported" );
343         return VLC_EGENERIC;
344     }
345     return VLC_SUCCESS;
346 }
347
348
349 /* Unregister an announce */
350 int announce_UnRegister( announce_handler_t *p_announce,
351                   session_descriptor_t *p_session )
352 {
353     msg_Dbg( p_announce, "unregistering announce" );
354     if( p_announce->p_sap )
355         p_announce->p_sap->pf_del( p_announce->p_sap, p_session );
356     return VLC_SUCCESS;
357 }