c1bb2daf65b40d215ee491634f575b56f80028f5
[vlc.git] / 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         vlc_object_kill ((vlc_object_t *)p_announce->p_sap);
184         /* Wait for the SAP thread to exit */
185         vlc_thread_join( (vlc_object_t *)p_announce->p_sap );
186         announce_SAPHandlerDestroy( p_announce->p_sap );
187     }
188
189     /* Free the structure */
190     vlc_object_release( p_announce );
191
192     return VLC_SUCCESS;
193 }
194
195 /* Register an announce */
196 static int announce_Register( announce_handler_t *p_announce,
197                               session_descriptor_t *p_session,
198                               announce_method_t *p_method )
199 {
200     if (p_method == NULL)
201         return VLC_EGENERIC;
202
203     msg_Dbg( p_announce, "registering announce");
204     if( p_method == &sap_method )
205     {
206         /* Do we already have a SAP announce handler ? */
207         if( !p_announce->p_sap )
208         {
209             sap_handler_t *p_sap = announce_SAPHandlerCreate( p_announce );
210             msg_Dbg( p_announce, "creating SAP announce handler");
211             if( !p_sap )
212             {
213                 msg_Err( p_announce, "SAP handler creation failed" );
214                 return VLC_ENOOBJ;
215             }
216             p_announce->p_sap = p_sap;
217         }
218         /* this will set p_session->p_sap for later deletion */
219         msg_Dbg( p_announce, "adding SAP session");
220         p_announce->p_sap->pf_add( p_announce->p_sap, p_session );
221     }
222     else
223     {
224         msg_Err( p_announce, "announce type unsupported" );
225         return VLC_EGENERIC;
226     }
227     return VLC_SUCCESS;
228 }
229
230
231 /* Unregister an announce */
232 static int announce_UnRegister( announce_handler_t *p_announce,
233                                 session_descriptor_t *p_session )
234 {
235     msg_Dbg( p_announce, "unregistering announce" );
236     if( p_announce->p_sap )
237         p_announce->p_sap->pf_del( p_announce->p_sap, p_session );
238     return VLC_SUCCESS;
239 }