]> git.sesse.net Git - vlc/blob - src/stream_output/announce.c
* The VLC-integrated SAP server now sends compliant packets. They can
[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 <vlc/vlc.h>
32
33 #include <vlc/sout.h>
34 #undef DEBUG_BUFFER
35
36 #include <announce.h>
37 #include <network.h>
38
39 #define SAP_ADDR "224.2.127.254" /* Standard port and address for SAP */
40 #define SAP_PORT 9875
41
42 /*****************************************************************************
43  * sout_SAPNew: Creates a SAP Session
44  *****************************************************************************/
45 sap_session_t * sout_SAPNew ( sout_instance_t *p_sout , char * psz_url_arg , char *psz_port_arg , char * psz_name_arg )
46 {
47         sap_session_t           *p_new;
48         module_t                *p_network;
49         network_socket_t        socket_desc;
50         char                    psz_network[12];       
51         struct                  sockaddr_in addr;
52         int                     ttl=15;
53               
54         p_new = (sap_session_t *)malloc( sizeof ( sap_session_t ) ) ;
55         
56         sprintf ( p_new->psz_url , "%s" , psz_url_arg );
57         sprintf ( p_new->psz_name , "%s" , psz_name_arg );
58         sprintf ( p_new->psz_port, "%s" , psz_port_arg ); /* Not implemented in SO */
59         
60         msg_Dbg (p_sout , "Creating SAP Socket" );
61         
62         socket_desc.i_type        = NETWORK_UDP;
63         socket_desc.psz_bind_addr = SAP_ADDR;
64         socket_desc.i_bind_port   = SAP_PORT;
65         socket_desc.psz_server_addr   = "";
66         socket_desc.i_server_port     = 0;
67         socket_desc.i_handle          = 0;
68         
69         sprintf ( psz_network,"ipv4" ); 
70
71         p_sout->p_private=(void*) &socket_desc;
72         
73         if( !( p_network = module_Need( p_sout, "network", psz_network ) ) )
74         {
75             msg_Warn( p_sout, "failed to open a connection (udp)" );
76         }
77
78         module_Unneed( p_sout, p_network );
79                
80         p_new->socket   =       socket_desc.i_handle;
81
82         memset( &addr , 0 , sizeof(addr) );
83         addr.sin_family      = AF_INET;
84         addr.sin_addr.s_addr = inet_addr(SAP_ADDR);
85         addr.sin_port        = htons( SAP_PORT );
86     
87         setsockopt( p_new->socket, IPPROTO_IP, IP_MULTICAST_TTL,
88                     (void*)&ttl, sizeof(ttl) );
89      
90         p_new->addr = addr;
91     
92         return(p_new);
93 }
94
95 /*****************************************************************************
96  * sout_SAPDelete: Deletes a SAP Session 
97  *****************************************************************************/
98 void sout_SAPDelete( sap_session_t * p_this )
99 {
100         shutdown(p_this->socket,0);
101         free(p_this);        
102 }       
103
104 /*****************************************************************************
105  * sout_SAPSend: Sends a SAP packet 
106  *****************************************************************************/
107 void sout_SAPSend( sout_instance_t *p_sout, sap_session_t * p_this )
108
109       char *sap_head;
110       char sap_msg[1000];
111       char *sap_send;
112       char *payload_type="application/sdp";
113       int i_send_result;
114       int i;
115       int i_header_size;
116       int i_msg_size;
117       int i_size;
118      
119     if( p_this->sendnow == 1 )
120     {
121          i_header_size = 9 + strlen( payload_type );
122          sap_head = ( char * )malloc( i_header_size * sizeof( char ) );
123                 
124           sap_head[0]=0x20; /* Means IPv4, not encrypted, not compressed */
125           sap_head[1]=0x00; /* No authentification */
126           sap_head[2]=0x42; /* Version */
127           sap_head[3]=0x12; /* Version */
128           
129           sap_head[4]=0x01; /* Source IP  FIXME: we should get the real address */
130           sap_head[5]=0x02; /* idem */
131           sap_head[6]=0x03; /* idem */
132           sap_head[7]=0x04; /* idem */
133           
134           strncpy( sap_head+8 , payload_type , 15 );
135           sap_head[ i_header_size-1 ] = '\0'; 
136        
137         /* Do not add spaces at beginning of the lines ! */  
138           sprintf(sap_msg,"v=0\n\
139 o=VideoLAN 3247692199 3247895918 IN IP4 VideoLAN\n\
140 s=%s\n\
141 u=VideoLAN\n\
142 t=0 0\n\
143 m=audio %s udp 14\n\
144 c=IN IP4 %s/15\n\
145 a=type:test\n", p_this->psz_name , p_this->psz_port , p_this->psz_url );
146      
147      i_msg_size = strlen( sap_msg );     
148      i_size = i_msg_size + i_header_size;
149      
150      sap_send = ( char* )malloc( i_size*sizeof(char) ); 
151           
152       for(i=0 ; i<i_header_size ; i++)
153       {
154            sap_send[i] = sap_head[i];
155        }
156
157      for( ;  i<i_size; i++)
158      {
159          sap_send[i] = sap_msg[i-i_header_size];
160      }
161     
162     /* What we send is the SAP header and the SDP packet */
163      
164      if(i_size<1024) /* We mustn't send packets larger than 1024B */     
165          i_send_result =  sendto( p_this->socket , sap_send , i_size , 0 , (struct sockaddr *)&p_this->addr , sizeof(p_this->addr) );
166     
167      if(i_send_result == -1)
168      {
169           msg_Warn(p_sout , "SAP Send failed on socket %i. " , p_this->socket );
170           perror("sendto"); 
171      }
172      p_this->sendnow = 0;
173      if(sap_send) free(sap_send);
174      if(sap_head) free(sap_head);
175    }
176    p_this->sendnow++;  
177 }