]> git.sesse.net Git - vlc/blob - src/stream_output/announce.c
vlc_mutex_init: remove unused paramter
[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/vlc.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     {
163         msg_Err( p_this, "out of memory" );
164         return NULL;
165     }
166
167     p_announce->p_sap = NULL;
168     vlc_object_attach( p_announce, p_this->p_libvlc);
169
170     return p_announce;
171 }
172
173 /**
174  * Destroy a  announce handler object
175  *
176  * \param p_announce the announce handler to destroy
177  * \return VLC_SUCCESS or an error
178  */
179 int announce_HandlerDestroy( announce_handler_t *p_announce )
180 {
181     if( p_announce->p_sap )
182     {
183         /* Exit the SAP */
184         vlc_object_release( p_announce->p_sap );
185     }
186
187     /* Free the structure */
188     vlc_object_release( p_announce );
189
190     return VLC_SUCCESS;
191 }
192
193 /* Register an announce */
194 static int announce_Register( announce_handler_t *p_announce,
195                               session_descriptor_t *p_session,
196                               announce_method_t *p_method )
197 {
198     if (p_method == NULL)
199         return VLC_EGENERIC;
200
201     msg_Dbg( p_announce, "registering announce");
202     if( p_method == &sap_method )
203     {
204         /* Do we already have a SAP announce handler ? */
205         if( !p_announce->p_sap )
206         {
207             sap_handler_t *p_sap = announce_SAPHandlerCreate( p_announce );
208             msg_Dbg( p_announce, "creating SAP announce handler");
209             if( !p_sap )
210             {
211                 msg_Err( p_announce, "SAP handler creation failed" );
212                 return VLC_ENOOBJ;
213             }
214             p_announce->p_sap = p_sap;
215         }
216         /* this will set p_session->p_sap for later deletion */
217         msg_Dbg( p_announce, "adding SAP session");
218         p_announce->p_sap->pf_add( p_announce->p_sap, p_session );
219     }
220     else
221     {
222         msg_Err( p_announce, "announce type unsupported" );
223         return VLC_EGENERIC;
224     }
225     return VLC_SUCCESS;
226 }
227
228
229 /* Unregister an announce */
230 static int announce_UnRegister( announce_handler_t *p_announce,
231                                 session_descriptor_t *p_session )
232 {
233     msg_Dbg( p_announce, "unregistering announce" );
234     if( p_announce->p_sap )
235         p_announce->p_sap->pf_del( p_announce->p_sap, p_session );
236     return VLC_SUCCESS;
237 }