]> git.sesse.net Git - vlc/blob - modules/access/udp.c
Trailing ;
[vlc] / modules / access / udp.c
1 /*****************************************************************************
2  * udp.c: raw UDP input module
3  *****************************************************************************
4  * Copyright (C) 2001-2005 the VideoLAN team
5  * Copyright (C) 2007 Remi Denis-Courmont
6  * $Id$
7  *
8  * Authors: Christophe Massiot <massiot@via.ecp.fr>
9  *          Tristan Leteurtre <tooney@via.ecp.fr>
10  *          Laurent Aimar <fenrir@via.ecp.fr>
11  *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
12  *          Remi Denis-Courmont
13  *
14  * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
29  *****************************************************************************/
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <vlc_common.h>
40 #include <vlc_plugin.h>
41 #include <vlc_access.h>
42 #include <vlc_network.h>
43
44 #define MTU 65535
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 #define CACHING_TEXT N_("Caching value in ms")
50 #define CACHING_LONGTEXT N_( \
51     "Caching value for UDP streams. This " \
52     "value should be set in milliseconds." )
53
54 static int  Open ( vlc_object_t * );
55 static void Close( vlc_object_t * );
56
57 vlc_module_begin ()
58     set_shortname( N_("UDP" ) )
59     set_description( N_("UDP input") )
60     set_category( CAT_INPUT )
61     set_subcategory( SUBCAT_INPUT_ACCESS )
62
63     add_integer( "udp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
64                  CACHING_LONGTEXT, true )
65     add_obsolete_integer( "rtp-late" )
66     add_obsolete_bool( "udp-auto-mtu" )
67
68     set_capability( "access", 0 )
69     add_shortcut( "udp" )
70     add_shortcut( "udpstream" )
71     add_shortcut( "udp4" )
72     add_shortcut( "udp6" )
73
74     set_callbacks( Open, Close )
75 vlc_module_end ()
76
77 /*****************************************************************************
78  * Local prototypes
79  *****************************************************************************/
80 #define RTP_HEADER_LEN 12
81
82 static block_t *BlockUDP( access_t * );
83 static int Control( access_t *, int, va_list );
84
85 /*****************************************************************************
86  * Open: open the socket
87  *****************************************************************************/
88 static int Open( vlc_object_t *p_this )
89 {
90     access_t     *p_access = (access_t*)p_this;
91
92     char *psz_name = strdup( p_access->psz_path );
93     char *psz_parser;
94     const char *psz_server_addr, *psz_bind_addr = "";
95     int  i_bind_port, i_server_port = 0;
96     int fam = AF_UNSPEC;
97     int fd;
98
99     /* Set up p_access */
100     access_InitFields( p_access );
101     ACCESS_SET_CALLBACKS( NULL, BlockUDP, Control, NULL );
102
103     if (strlen (p_access->psz_access) > 0)
104     {
105         switch (p_access->psz_access[strlen (p_access->psz_access) - 1])
106         {
107             case '4':
108                 fam = AF_INET;
109                 break;
110
111             case '6':
112                 fam = AF_INET6;
113                 break;
114         }
115     }
116
117     i_bind_port = var_CreateGetInteger( p_access, "server-port" );
118
119     /* Parse psz_name syntax :
120      * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
121     psz_parser = strchr( psz_name, '@' );
122     if( psz_parser != NULL )
123     {
124         /* Found bind address and/or bind port */
125         *psz_parser++ = '\0';
126         psz_bind_addr = psz_parser;
127
128         if( psz_bind_addr[0] == '[' )
129             /* skips bracket'd IPv6 address */
130             psz_parser = strchr( psz_parser, ']' );
131
132         if( psz_parser != NULL )
133         {
134             psz_parser = strchr( psz_parser, ':' );
135             if( psz_parser != NULL )
136             {
137                 *psz_parser++ = '\0';
138                 i_bind_port = atoi( psz_parser );
139             }
140         }
141     }
142
143     psz_server_addr = psz_name;
144     psz_parser = ( psz_server_addr[0] == '[' )
145         ? strchr( psz_name, ']' ) /* skips bracket'd IPv6 address */
146         : psz_name;
147
148     if( psz_parser != NULL )
149     {
150         psz_parser = strchr( psz_parser, ':' );
151         if( psz_parser != NULL )
152         {
153             *psz_parser++ = '\0';
154             i_server_port = atoi( psz_parser );
155         }
156     }
157
158     msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
159              psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
160
161     fd = net_OpenDgram( p_access, psz_bind_addr, i_bind_port,
162                         psz_server_addr, i_server_port, fam, IPPROTO_UDP );
163     free (psz_name);
164     if( fd == -1 )
165     {
166         msg_Err( p_access, "cannot open socket" );
167         return VLC_EGENERIC;
168     }
169     p_access->p_sys = (void *)(intptr_t)fd;
170
171     /* Update default_pts to a suitable value for udp access */
172     var_Create( p_access, "udp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
173     return VLC_SUCCESS;
174 }
175
176 /*****************************************************************************
177  * Close: free unused data structures
178  *****************************************************************************/
179 static void Close( vlc_object_t *p_this )
180 {
181     access_t     *p_access = (access_t*)p_this;
182
183     net_Close( (intptr_t)p_access->p_sys );
184 }
185
186 /*****************************************************************************
187  * Control:
188  *****************************************************************************/
189 static int Control( access_t *p_access, int i_query, va_list args )
190 {
191     bool    *pb_bool;
192     int64_t *pi_64;
193
194     switch( i_query )
195     {
196         /* */
197         case ACCESS_CAN_SEEK:
198         case ACCESS_CAN_FASTSEEK:
199         case ACCESS_CAN_PAUSE:
200         case ACCESS_CAN_CONTROL_PACE:
201             pb_bool = (bool*)va_arg( args, bool* );
202             *pb_bool = false;
203             break;
204         /* */
205         case ACCESS_GET_PTS_DELAY:
206             pi_64 = (int64_t*)va_arg( args, int64_t * );
207             *pi_64 = var_GetInteger( p_access, "udp-caching" ) * 1000;
208             break;
209
210         /* */
211         case ACCESS_SET_PAUSE_STATE:
212         case ACCESS_GET_TITLE_INFO:
213         case ACCESS_SET_TITLE:
214         case ACCESS_SET_SEEKPOINT:
215         case ACCESS_SET_PRIVATE_ID_STATE:
216         case ACCESS_GET_CONTENT_TYPE:
217             return VLC_EGENERIC;
218
219         default:
220             msg_Warn( p_access, "unimplemented query in control" );
221             return VLC_EGENERIC;
222
223     }
224     return VLC_SUCCESS;
225 }
226
227 /*****************************************************************************
228  * BlockUDP:
229  *****************************************************************************/
230 static block_t *BlockUDP( access_t *p_access )
231 {
232     access_sys_t *p_sys = p_access->p_sys;
233     block_t      *p_block;
234     ssize_t len;
235
236     if( p_access->info.b_eof )
237         return NULL;
238
239     /* Read data */
240     p_block = block_New( p_access, MTU );
241     len = net_Read( p_access, (intptr_t)p_sys, NULL,
242                     p_block->p_buffer, MTU, false );
243     if( len < 0 )
244     {
245         block_Release( p_block );
246         return NULL;
247     }
248
249     return block_Realloc( p_block, 0, len );
250 }