]> git.sesse.net Git - vlc/blob - src/stream_output/announce.c
SAP announce: rewrite, use one thread per SAP group (fixes #1839)
[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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_sout.h>
33 #include "stream_output.h"
34
35 #include <assert.h>
36
37 /* Private functions for the announce handler */
38 static announce_handler_t*  announce_HandlerCreate( vlc_object_t *);
39 static int announce_Register( announce_handler_t *p_announce,
40                               session_descriptor_t *p_session,
41                               announce_method_t *p_method );
42 static int announce_UnRegister( announce_handler_t *p_announce,
43                                 session_descriptor_t *p_session );
44
45 struct announce_method_t
46 {
47 } sap_method;
48
49 /****************************************************************************
50  * Sout-side functions
51  ****************************************************************************/
52
53 /**
54  *  Register a new session with the announce handler, using a pregenerated SDP
55  *
56  * \param p_sout a sout instance structure
57  * \param psz_sdp the SDP to register
58  * \param psz_dst session address (needed for SAP address auto detection)
59  * \param p_method an announce method descriptor
60  * \return the new session descriptor structure
61  */
62 session_descriptor_t *
63 sout_AnnounceRegisterSDP( sout_instance_t *p_sout, const char *psz_sdp,
64                             const char *psz_dst, announce_method_t *p_method )
65 {
66     session_descriptor_t *p_session;
67     announce_handler_t *p_announce = (announce_handler_t*)
68                                      vlc_object_find( p_sout,
69                                               VLC_OBJECT_ANNOUNCE,
70                                               FIND_ANYWHERE );
71     if( !p_announce )
72     {
73         msg_Dbg( p_sout, "no announce handler found, creating one" );
74         p_announce = announce_HandlerCreate( VLC_OBJECT( p_sout ) );
75         if( !p_announce )
76         {
77             msg_Err( p_sout, "Creation failed" );
78             return NULL;
79         }
80         vlc_object_yield( p_announce );
81     }
82
83     p_session = malloc( sizeof( *p_session ) );
84     memset( p_session, 0, sizeof( *p_session ) );
85     p_session->psz_sdp = strdup( psz_sdp );
86
87     /* GRUIK. We should not convert back-and-forth from string to numbers */
88     struct addrinfo *res;
89     if (vlc_getaddrinfo (VLC_OBJECT (p_sout), psz_dst, 0, NULL, &res) == 0)
90     {
91         if (res->ai_addrlen <= sizeof (p_session->addr))
92             memcpy (&p_session->addr, res->ai_addr,
93                     p_session->addrlen = res->ai_addrlen);
94         vlc_freeaddrinfo (res);
95     }
96
97     announce_Register( p_announce, p_session, p_method );
98
99     vlc_object_release( p_announce );
100     return p_session;
101 }
102
103 /**
104  *  UnRegister an existing session
105  *
106  * \param p_sout a sout instance structure
107  * \param p_session the session descriptor
108  * \return VLC_SUCCESS or an error
109  */
110 int sout_AnnounceUnRegister( sout_instance_t *p_sout,
111                              session_descriptor_t *p_session )
112 {
113     int i_ret;
114     announce_handler_t *p_announce = (announce_handler_t*)
115                               vlc_object_find( p_sout,
116                                               VLC_OBJECT_ANNOUNCE,
117                                               FIND_ANYWHERE );
118     if( !p_announce )
119     {
120         msg_Dbg( p_sout, "unable to remove announce: no announce handler" );
121         return VLC_ENOOBJ;
122     }
123     i_ret  = announce_UnRegister( p_announce, p_session );
124     if( i_ret == 0 )
125         free( p_session );
126
127     vlc_object_release( p_announce );
128
129     return i_ret;
130 }
131
132 /**
133  * \return the SAP announce method
134  */
135 announce_method_t * sout_SAPMethod (void)
136 {
137     return &sap_method;
138 }
139
140 void sout_MethodRelease (announce_method_t *m)
141 {
142     assert (m == &sap_method);
143 }
144
145 /************************************************************************
146  * Announce handler functions (private)
147  ************************************************************************/
148
149 /**
150  * Create the announce handler object
151  *
152  * \param p_this a vlc_object structure
153  * \return the new announce handler or NULL on error
154  */
155 static announce_handler_t *announce_HandlerCreate( vlc_object_t *p_this )
156 {
157     announce_handler_t *p_announce;
158
159     p_announce = vlc_object_create( p_this, VLC_OBJECT_ANNOUNCE );
160
161     if( !p_announce )
162         return NULL;
163
164     p_announce->p_sap = NULL;
165     vlc_object_attach( p_announce, p_this->p_libvlc);
166
167     return p_announce;
168 }
169
170 /**
171  * Destroy a  announce handler object
172  *
173  * \param p_announce the announce handler to destroy
174  * \return VLC_SUCCESS or an error
175  */
176 int announce_HandlerDestroy( announce_handler_t *p_announce )
177 {
178     if( p_announce->p_sap )
179         SAP_Destroy( p_announce->p_sap );
180
181     /* Free the structure */
182     vlc_object_release( p_announce );
183
184     return VLC_SUCCESS;
185 }
186
187 /* Register an announce */
188 static int announce_Register( announce_handler_t *p_announce,
189                               session_descriptor_t *p_session,
190                               announce_method_t *p_method )
191 {
192     if (p_method == NULL)
193         return VLC_EGENERIC;
194
195     msg_Dbg( p_announce, "registering announce");
196     if( p_method == &sap_method )
197     {
198         /* Do we already have a SAP announce handler ? */
199         if( !p_announce->p_sap )
200         {
201             sap_handler_t *p_sap = SAP_Create (VLC_OBJECT(p_announce));
202             msg_Dbg( p_announce, "creating SAP announce handler");
203             if( !p_sap )
204             {
205                 msg_Err( p_announce, "SAP handler creation failed" );
206                 return VLC_ENOOBJ;
207             }
208             p_announce->p_sap = p_sap;
209         }
210         /* this will set p_session->p_sap for later deletion */
211         msg_Dbg( p_announce, "adding SAP session");
212         SAP_Add( p_announce->p_sap, p_session );
213     }
214     else
215     {
216         msg_Err( p_announce, "announce type unsupported" );
217         return VLC_EGENERIC;
218     }
219     return VLC_SUCCESS;
220 }
221
222
223 /* Unregister an announce */
224 static int announce_UnRegister( announce_handler_t *p_announce,
225                                 session_descriptor_t *p_session )
226 {
227     msg_Dbg( p_announce, "unregistering announce" );
228     SAP_Del( p_announce->p_sap, p_session );
229     return VLC_SUCCESS;
230 }