]> git.sesse.net Git - vlc/blob - src/stream_output/announce.c
* ./include/announce.h
[vlc] / src / stream_output / announce.c
1 /*****************************************************************************
2  * announce.c : Session announcement
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  *
6  * Authors: ClĂ©ment Stenac <zorglub@via.ecp.fr>
7  *          Damien Lucas <nitrox@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                                /* free() */
28 #include <stdio.h>                                              /* sprintf() */
29 #include <string.h>                                            /* strerror() */
30
31 #include <unistd.h>
32
33 #include <vlc/vlc.h>
34
35 #include <vlc/sout.h>
36 #undef DEBUG_BUFFER
37
38 #include <announce.h>
39 #include <network.h>
40
41 #define SAP_IPV4_ADDR "224.2.127.254" /* Standard port and address for SAP */
42 #define SAP_PORT 9875
43
44 #define SAP_IPV6_ADDR_1 "FF0"
45 #define SAP_IPV6_ADDR_2 "::2:7FFE"
46
47 /*****************************************************************************
48  * sout_SAPNew: Creates a SAP Session
49  *****************************************************************************/
50 sap_session_t * sout_SAPNew ( sout_instance_t *p_sout , 
51                 char * psz_url_arg , char *psz_port_arg , 
52                 char * psz_name_arg, int ip_version, 
53                 char * psz_v6_scope )
54 {
55       
56     sap_session_t       *p_new; /* The SAP structure */
57     module_t            *p_network; /* Network module */
58     network_socket_t    socket_desc; /* Socket descriptor */
59     char                psz_network[6]; /* IPv4 or IPv6 */
60     char                *sap_ipv6_addr=NULL; /* IPv6 built address */     
61     
62     /* Allocate the SAP structure */ 
63     p_new = (sap_session_t *)malloc( sizeof ( sap_session_t ) ) ;
64     if ( !p_new )
65     {
66         msg_Err( p_sout, "No memory left" );        
67         return NULL;
68     }
69                 
70     /* Fill the information in the structure */
71     sprintf ( p_new->psz_url , "%s" , psz_url_arg );
72     sprintf ( p_new->psz_name , "%s" , psz_name_arg );
73     /* Port is not implemented in sout */
74     sprintf ( p_new->psz_port, "%s" , psz_port_arg );
75
76     p_new->i_ip_version = ip_version;
77                         
78     /* Only "6" triggers IPv6. IPv4 is default */
79     if(ip_version != 6)
80     {
81         msg_Dbg( p_sout , "Creating IPv4 SAP socket" );
82         
83         /* Fill the socket descriptor */
84         socket_desc.i_type            = NETWORK_UDP;
85         socket_desc.psz_bind_addr     = "";
86         socket_desc.i_bind_port       = 0;
87         socket_desc.psz_server_addr   = SAP_IPV4_ADDR;
88         socket_desc.i_server_port     = SAP_PORT;
89         socket_desc.i_handle          = 0;
90        
91         /* Call the network module */ 
92         sprintf ( psz_network, "ipv4" ); 
93         p_sout->p_private=(void*) &socket_desc;
94         if( !( p_network = module_Need( p_sout, "network", psz_network ) ) )
95         {
96              msg_Warn( p_sout, "failed to open a connection (udp)" );
97              return NULL;
98         }
99         module_Unneed( p_sout, p_network );
100               
101         p_new->socket   =       socket_desc.i_handle;
102         if(p_new->socket <= 0 )
103         {
104             msg_Warn( p_sout, "Unable to initialize SAP" );
105             return NULL;
106         }
107         
108     }
109     else
110     {
111         msg_Dbg(p_sout , "Creating IPv6 SAP socket with scope %s" 
112                         , psz_v6_scope );
113
114         /* Initialize and build the IPv6 address to broadcast to */
115         sap_ipv6_addr = (char *)malloc(28*sizeof(char));
116         if ( !sap_ipv6_addr )
117         {
118             msg_Err( p_sout, "No memory left" );        
119             return NULL;
120         }
121         sprintf(sap_ipv6_addr,"%s%c%s",
122                          SAP_IPV6_ADDR_1,
123                          psz_v6_scope[0],
124                          SAP_IPV6_ADDR_2); 
125
126         /* Fill the socket descriptor */
127         socket_desc.i_type        = NETWORK_UDP;
128         socket_desc.psz_bind_addr = ""; 
129         socket_desc.i_bind_port   = 0;
130         socket_desc.psz_server_addr = sap_ipv6_addr;
131         socket_desc.i_server_port     = SAP_PORT;
132         socket_desc.i_handle          = 0;
133             
134         sprintf ( psz_network, "ipv6" ); 
135         
136         /* Call the network module */
137         p_sout->p_private=(void*) &socket_desc;
138         if( !( p_network = module_Need( p_sout, "network", psz_network ) ) )
139         {
140             msg_Warn( p_sout, "failed to open a connection (udp)" );
141             return NULL;
142         }
143         module_Unneed( p_sout, p_network );
144                
145         p_new->socket   =       socket_desc.i_handle;
146
147         if(p_new->socket <= 0 )
148         {
149              msg_Warn( p_sout, "Unable to initialize SAP" );
150              return NULL;
151          }
152                 
153
154         /* Free what we allocated */
155         if( sap_ipv6_addr ) free(sap_ipv6_addr);
156     }
157             
158     msg_Dbg (p_sout,"SAP initialization complete");
159   
160     return(p_new);
161 }
162
163 /*****************************************************************************
164  * sout_SAPDelete: Deletes a SAP Session 
165  *****************************************************************************/
166 void sout_SAPDelete( sout_instance_t *p_sout , sap_session_t * p_this )
167 {
168         if( close(p_this->socket) )
169                 msg_Err ( p_sout, "Unable to close SAP socket");
170         
171         if( p_this ) free( p_this );        
172 }       
173
174 /*****************************************************************************
175  * sout_SAPSend: Sends a SAP packet 
176  *****************************************************************************/
177 void sout_SAPSend( sout_instance_t *p_sout, sap_session_t * p_this)
178 {
179         
180     char *sap_head;                         /* SAP header */
181     char sap_msg[1000];                     /* SDP content */
182     char *sap_send;                         /* What we send */
183     char *payload_type="application/sdp";   
184     int i_send_result=0;                    /* Result of send */
185     int i;
186     int i_header_size;                      /* SAP header size */
187     int i_msg_size;                         /* SDP content size */
188     int i_size;                             /* Total size */
189     
190     /* We send a packet every 24 calls to the function */
191     if( p_this->sendnow == 24 )
192     {
193         i_header_size = 9 + strlen( payload_type );
194         sap_head = ( char * )malloc( i_header_size * sizeof( char ) );
195         
196         if( ! sap_head )         
197         {
198             msg_Warn( p_sout , "No memory left");
199             return;
200         }       
201          
202         /* Create the SAP headers */
203         sap_head[0]=0x20; /* Means IPv4, not encrypted, not compressed */
204         sap_head[1]=0x00; /* No authentification */
205         sap_head[2]=0x42; /* Version */
206         sap_head[3]=0x12; /* Version */
207           
208         sap_head[4]=0x01; /* Source IP  FIXME: we should get the real address */
209         sap_head[5]=0x02; /* idem */
210         sap_head[6]=0x03; /* idem */
211         sap_head[7]=0x04; /* idem */
212           
213         strncpy( sap_head+8 , payload_type , 15 );
214         sap_head[ i_header_size-1 ] = '\0'; 
215        
216         /* Create the SDP content */
217         /* Do not add spaces at beginning of the lines ! */  
218           sprintf(sap_msg,"v=0\n\
219 o=VideoLAN 3247692199 3247895918 IN IP4 VideoLAN\n\
220 s=%s\n\
221 u=VideoLAN\n\
222 t=0 0\n\
223 m=audio %s udp 14\n\
224 c=IN IP4 %s/15\n\
225 a=type:test\n", p_this->psz_name , p_this->psz_port , p_this->psz_url );
226      
227        i_msg_size = strlen( sap_msg );     
228        i_size = i_msg_size + i_header_size;
229      
230        /* Create the message */
231        sap_send = ( char* )malloc( i_size*sizeof(char) ); 
232        if(! sap_send)
233        {
234            msg_Err( p_sout ,  "No memory left") ;
235           return;
236        } 
237       
238        for(i=0 ; i<i_header_size ; i++)
239        {
240            sap_send[i] = sap_head[i];
241        }
242        
243        for( ;  i<i_size; i++)
244        {
245            sap_send[i] = sap_msg[i-i_header_size];
246        }
247     
248       if(i_size<1024) /* We mustn't send packets larger than 1024B */     
249       {
250           if( p_this->i_ip_version == 6)
251           {
252                i_send_result =  send( p_this->socket , sap_send ,
253                            i_size , 0 );
254           } 
255           else
256           {
257                 i_send_result =  send( p_this->socket , sap_send ,
258                              i_size , 0 );
259            }
260       }
261      
262       if(i_send_result == -1)
263       {
264           msg_Warn(p_sout , "SAP Send failed on socket %i. " ,
265                           p_this->socket );
266           perror("sendto"); 
267        }
268       
269        p_this->sendnow = 0;
270        
271        /* Free what we allocated */
272        if(sap_send) free(sap_send);
273        if(sap_head) free(sap_head);
274    }
275     
276    p_this->sendnow++;  
277 }