]> git.sesse.net Git - vlc/blob - src/stream_output/announce.c
Use var_Inherit* instead of var_CreateGet*.
[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 #include "libvlc.h"
35
36 #include <assert.h>
37
38 struct announce_method_t
39 {
40 } sap_method;
41
42 /****************************************************************************
43  * Sout-side functions
44  ****************************************************************************/
45
46 static void sap_destroy (vlc_object_t *p_this)
47 {
48     libvlc_priv (p_this->p_libvlc)->p_sap = NULL;
49 }
50
51 #undef sout_AnnounceRegisterSDP
52
53 static vlc_mutex_t sap_mutex = VLC_STATIC_MUTEX;
54
55 /**
56  *  Registers a new session with the announce handler, using a pregenerated SDP
57  *
58  * \param obj a VLC object
59  * \param psz_sdp the SDP to register
60  * \param psz_dst session address (needed for SAP address auto detection)
61  * \param p_method an announce method descriptor
62  * \return the new session descriptor structure
63  */
64 session_descriptor_t *
65 sout_AnnounceRegisterSDP( vlc_object_t *obj, const char *psz_sdp,
66                           const char *psz_dst, announce_method_t *p_method )
67 {
68     assert (p_method == &sap_method);
69     (void) p_method;
70
71     session_descriptor_t *p_session = calloc( 1, sizeof (*p_session) );
72     if( !p_session )
73         return NULL;
74
75     p_session->psz_sdp = strdup( psz_sdp );
76
77     /* GRUIK. We should not convert back-and-forth from string to numbers */
78     struct addrinfo *res;
79     if (vlc_getaddrinfo (obj, psz_dst, 0, NULL, &res) == 0)
80     {
81         if (res->ai_addrlen <= sizeof (p_session->addr))
82             memcpy (&p_session->addr, res->ai_addr,
83                     p_session->addrlen = res->ai_addrlen);
84         freeaddrinfo (res);
85     }
86
87     vlc_mutex_lock (&sap_mutex);
88     sap_handler_t *p_sap = libvlc_priv (obj->p_libvlc)->p_sap;
89     if (p_sap == NULL)
90     {
91         p_sap = SAP_Create (VLC_OBJECT (obj->p_libvlc));
92         libvlc_priv (obj->p_libvlc)->p_sap = p_sap;
93         vlc_object_set_destructor ((vlc_object_t *)p_sap, sap_destroy);
94     }
95     else
96         vlc_object_hold ((vlc_object_t *)p_sap);
97     vlc_mutex_unlock (&sap_mutex);
98
99     if (p_sap == NULL)
100         goto error;
101
102     msg_Dbg (obj, "adding SAP session");
103     SAP_Add (p_sap, p_session );
104     return p_session;
105
106 error:
107     free (p_session->psz_sdp);
108     free (p_session);
109     return NULL;
110 }
111
112 #undef sout_AnnounceUnRegister
113 /**
114  *  Unregisters an existing session
115  *
116  * \param obj a VLC object
117  * \param p_session the session descriptor
118  * \return VLC_SUCCESS or an error
119  */
120 int sout_AnnounceUnRegister( vlc_object_t *obj,
121                              session_descriptor_t *p_session )
122 {
123     sap_handler_t *p_sap = libvlc_priv (obj->p_libvlc)->p_sap;
124
125     msg_Dbg (obj, "removing SAP session");
126     SAP_Del (p_sap, p_session);
127
128     vlc_mutex_lock (&sap_mutex);
129     vlc_object_release ((vlc_object_t *)p_sap);
130     vlc_mutex_unlock (&sap_mutex);
131
132     free (p_session->psz_sdp);
133     free (p_session);
134
135     return 0;
136 }
137
138 /**
139  * \return the SAP announce method
140  */
141 announce_method_t * sout_SAPMethod (void)
142 {
143     return &sap_method;
144 }
145
146 void sout_MethodRelease (announce_method_t *m)
147 {
148     assert (m == &sap_method);
149 }