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