1 /*****************************************************************************
2 * udp.c: raw UDP input module
3 *****************************************************************************
4 * Copyright (C) 2001-2005 the VideoLAN team
5 * Copyright (C) 2007 Remi Denis-Courmont
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>
14 * Reviewed: 23 October 2003, Jean-Paul Saman <jpsaman _at_ videolan _dot_ org>
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.
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.
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
29 *****************************************************************************/
31 /*****************************************************************************
33 *****************************************************************************/
39 #include <vlc_common.h>
40 #include <vlc_plugin.h>
41 #include <vlc_access.h>
42 #include <vlc_network.h>
46 /*****************************************************************************
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." )
54 static int Open ( vlc_object_t
* );
55 static void Close( vlc_object_t
* );
58 set_shortname( N_("UDP" ) )
59 set_description( N_("UDP input") )
60 set_category( CAT_INPUT
)
61 set_subcategory( SUBCAT_INPUT_ACCESS
)
63 add_integer( "udp-caching", DEFAULT_PTS_DELAY
/ 1000, CACHING_TEXT
,
64 CACHING_LONGTEXT
, true )
67 set_capability( "access", 0 )
68 add_shortcut( "udp", "udpstream", "udp4", "udp6" )
70 set_callbacks( Open
, Close
)
73 /*****************************************************************************
75 *****************************************************************************/
76 #define RTP_HEADER_LEN 12
78 static block_t
*BlockUDP( access_t
* );
79 static int Control( access_t
*, int, va_list );
81 /*****************************************************************************
82 * Open: open the socket
83 *****************************************************************************/
84 static int Open( vlc_object_t
*p_this
)
86 access_t
*p_access
= (access_t
*)p_this
;
88 char *psz_name
= strdup( p_access
->psz_location
);
90 const char *psz_server_addr
, *psz_bind_addr
= "";
91 int i_bind_port
, i_server_port
= 0;
96 access_InitFields( p_access
);
97 ACCESS_SET_CALLBACKS( NULL
, BlockUDP
, Control
, NULL
);
99 if (strlen (p_access
->psz_access
) > 0)
101 switch (p_access
->psz_access
[strlen (p_access
->psz_access
) - 1])
113 i_bind_port
= var_InheritInteger( p_access
, "server-port" );
115 /* Parse psz_name syntax :
116 * [serveraddr[:serverport]][@[bindaddr]:[bindport]] */
117 psz_parser
= strchr( psz_name
, '@' );
118 if( psz_parser
!= NULL
)
120 /* Found bind address and/or bind port */
121 *psz_parser
++ = '\0';
122 psz_bind_addr
= psz_parser
;
124 if( psz_bind_addr
[0] == '[' )
125 /* skips bracket'd IPv6 address */
126 psz_parser
= strchr( psz_parser
, ']' );
128 if( psz_parser
!= NULL
)
130 psz_parser
= strchr( psz_parser
, ':' );
131 if( psz_parser
!= NULL
)
133 *psz_parser
++ = '\0';
134 i_bind_port
= atoi( psz_parser
);
139 psz_server_addr
= psz_name
;
140 psz_parser
= ( psz_server_addr
[0] == '[' )
141 ? strchr( psz_name
, ']' ) /* skips bracket'd IPv6 address */
144 if( psz_parser
!= NULL
)
146 psz_parser
= strchr( psz_parser
, ':' );
147 if( psz_parser
!= NULL
)
149 *psz_parser
++ = '\0';
150 i_server_port
= atoi( psz_parser
);
154 msg_Dbg( p_access
, "opening server=%s:%d local=%s:%d",
155 psz_server_addr
, i_server_port
, psz_bind_addr
, i_bind_port
);
157 fd
= net_OpenDgram( p_access
, psz_bind_addr
, i_bind_port
,
158 psz_server_addr
, i_server_port
, fam
, IPPROTO_UDP
);
162 msg_Err( p_access
, "cannot open socket" );
165 p_access
->p_sys
= (void *)(intptr_t)fd
;
167 /* Update default_pts to a suitable value for udp access */
168 var_Create( p_access
, "udp-caching", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
172 /*****************************************************************************
173 * Close: free unused data structures
174 *****************************************************************************/
175 static void Close( vlc_object_t
*p_this
)
177 access_t
*p_access
= (access_t
*)p_this
;
179 net_Close( (intptr_t)p_access
->p_sys
);
182 /*****************************************************************************
184 *****************************************************************************/
185 static int Control( access_t
*p_access
, int i_query
, va_list args
)
193 case ACCESS_CAN_SEEK
:
194 case ACCESS_CAN_FASTSEEK
:
195 case ACCESS_CAN_PAUSE
:
196 case ACCESS_CAN_CONTROL_PACE
:
197 pb_bool
= (bool*)va_arg( args
, bool* );
201 case ACCESS_GET_PTS_DELAY
:
202 pi_64
= (int64_t*)va_arg( args
, int64_t * );
203 *pi_64
= var_GetInteger(p_access
,"udp-caching") * 1000;
207 case ACCESS_SET_PAUSE_STATE
:
208 case ACCESS_GET_TITLE_INFO
:
209 case ACCESS_SET_TITLE
:
210 case ACCESS_SET_SEEKPOINT
:
211 case ACCESS_SET_PRIVATE_ID_STATE
:
212 case ACCESS_GET_CONTENT_TYPE
:
216 msg_Warn( p_access
, "unimplemented query in control" );
223 /*****************************************************************************
225 *****************************************************************************/
226 static block_t
*BlockUDP( access_t
*p_access
)
228 access_sys_t
*p_sys
= p_access
->p_sys
;
232 if( p_access
->info
.b_eof
)
236 p_block
= block_New( p_access
, MTU
);
237 len
= net_Read( p_access
, (intptr_t)p_sys
, NULL
,
238 p_block
->p_buffer
, MTU
, false );
241 block_Release( p_block
);
245 return block_Realloc( p_block
, 0, len
);