]> git.sesse.net Git - vlc/blob - src/stream_output/announce.c
Unexport some unused APIs
[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 /* 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 );
40
41 struct announce_method_t
42 {
43 } sap_method;
44
45 /****************************************************************************
46  * Sout-side functions
47  ****************************************************************************/
48
49 /**
50  * Create and initialize a session descriptor
51  *
52  * \return a new session descriptor
53  */
54 static session_descriptor_t * sout_AnnounceSessionCreate (vlc_object_t *obj,
55                                                    const char *cfgpref)
56 {
57     size_t cfglen = strlen (cfgpref);
58     if (cfglen > 100)
59         return NULL;
60
61     char varname[cfglen + sizeof ("description")], *subvar = varname + cfglen;
62     strcpy (varname, cfgpref);
63
64     session_descriptor_t *p_session = calloc (1, sizeof (*p_session));
65     if (p_session == NULL)
66         return NULL;
67
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);
72
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);
81
82     return p_session;
83 }
84
85 /**
86  *  Register a new session with the announce handler, using a pregenerated SDP
87  *
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
93  */
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 )
98 {
99     session_descriptor_t *p_session;
100     announce_handler_t *p_announce = (announce_handler_t*)
101                                      vlc_object_find( p_sout,
102                                               VLC_OBJECT_ANNOUNCE,
103                                               FIND_ANYWHERE );
104     if( !p_announce )
105     {
106         msg_Dbg( p_sout, "no announce handler found, creating one" );
107         p_announce = announce_HandlerCreate( VLC_OBJECT( p_sout ) );
108         if( !p_announce )
109         {
110             msg_Err( p_sout, "Creation failed" );
111             return NULL;
112         }
113         vlc_object_yield( p_announce );
114     }
115
116     p_session = sout_AnnounceSessionCreate(VLC_OBJECT (p_sout), cfgpref);
117     p_session->psz_sdp = strdup( psz_sdp );
118
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)
122     {
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);
127     }
128
129     announce_Register( p_announce, p_session, p_method );
130
131     vlc_object_release( p_announce );
132     return p_session;
133 }
134
135 /**
136  *  UnRegister an existing session
137  *
138  * \param p_sout a sout instance structure
139  * \param p_session the session descriptor
140  * \return VLC_SUCCESS or an error
141  */
142 int sout_AnnounceUnRegister( sout_instance_t *p_sout,
143                              session_descriptor_t *p_session )
144 {
145     int i_ret;
146     announce_handler_t *p_announce = (announce_handler_t*)
147                               vlc_object_find( p_sout,
148                                               VLC_OBJECT_ANNOUNCE,
149                                               FIND_ANYWHERE );
150     if( !p_announce )
151     {
152         msg_Dbg( p_sout, "unable to remove announce: no announce handler" );
153         return VLC_ENOOBJ;
154     }
155     i_ret  = announce_UnRegister( p_announce, p_session );
156
157     vlc_object_release( p_announce );
158
159     return i_ret;
160 }
161
162 /**
163  * Destroy a session descriptor and free all
164  *
165  * \param p_session the session to destroy
166  * \return Nothing
167  */
168 void sout_AnnounceSessionDestroy( session_descriptor_t *p_session )
169 {
170     if( p_session )
171     {
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);
180         free( p_session );
181     }
182 }
183
184 /**
185  * \return the SAP announce method
186  */
187 announce_method_t * sout_SAPMethod (void)
188 {
189     return &sap_method;
190 }
191
192 void sout_MethodRelease (announce_method_t *m)
193 {
194     assert (m == &sap_method);
195 }
196
197 /************************************************************************
198  * Announce handler functions (private)
199  ************************************************************************/
200
201 /**
202  * Create the announce handler object
203  *
204  * \param p_this a vlc_object structure
205  * \return the new announce handler or NULL on error
206  */
207 static announce_handler_t *announce_HandlerCreate( vlc_object_t *p_this )
208 {
209     announce_handler_t *p_announce;
210
211     p_announce = vlc_object_create( p_this, VLC_OBJECT_ANNOUNCE );
212
213     if( !p_announce )
214     {
215         msg_Err( p_this, "out of memory" );
216         return NULL;
217     }
218
219     p_announce->p_sap = NULL;
220     vlc_object_attach( p_announce, p_this->p_libvlc);
221
222     return p_announce;
223 }
224
225 /**
226  * Destroy a  announce handler object
227  *
228  * \param p_announce the announce handler to destroy
229  * \return VLC_SUCCESS or an error
230  */
231 int announce_HandlerDestroy( announce_handler_t *p_announce )
232 {
233     if( p_announce->p_sap )
234     {
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 );
239     }
240
241     /* Free the structure */
242     vlc_object_destroy( p_announce );
243
244     return VLC_SUCCESS;
245 }
246
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 )
251 {
252     if (p_method == NULL)
253         return VLC_EGENERIC;
254
255     msg_Dbg( p_announce, "registering announce");
256     if( p_method == &sap_method )
257     {
258         /* Do we already have a SAP announce handler ? */
259         if( !p_announce->p_sap )
260         {
261             sap_handler_t *p_sap = announce_SAPHandlerCreate( p_announce );
262             msg_Dbg( p_announce, "creating SAP announce handler");
263             if( !p_sap )
264             {
265                 msg_Err( p_announce, "SAP handler creation failed" );
266                 return VLC_ENOOBJ;
267             }
268             p_announce->p_sap = p_sap;
269         }
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 );
273     }
274     else
275     {
276         msg_Err( p_announce, "announce type unsupported" );
277         return VLC_EGENERIC;
278     }
279     return VLC_SUCCESS;
280 }
281
282
283 /* Unregister an announce */
284 static int announce_UnRegister( announce_handler_t *p_announce,
285                                 session_descriptor_t *p_session )
286 {
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 );
290     return VLC_SUCCESS;
291 }