1 /*****************************************************************************
2 * access.c: Real rtsp input
3 *****************************************************************************
4 * Copyright (C) 2005 VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <vlc/input.h>
29 #include <vlc_interaction.h>
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Open ( vlc_object_t * );
39 static void Close( vlc_object_t * );
41 #define CACHING_TEXT N_("Caching value (ms)")
42 #define CACHING_LONGTEXT N_( \
43 "Caching value for RTSP streams. This " \
44 "value should be set in milliseconds." )
47 set_description( _("Real RTSP") );
48 set_shortname( _("Real RTSP") );
49 set_category( CAT_INPUT );
50 set_subcategory( SUBCAT_INPUT_ACCESS );
51 add_integer( "realrtsp-caching", 3000, NULL,
52 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
53 set_capability( "access2", 10 );
54 set_callbacks( Open, Close );
55 add_shortcut( "realrtsp" );
56 add_shortcut( "rtsp" );
57 add_shortcut( "pnm" );
61 /*****************************************************************************
63 *****************************************************************************/
64 static block_t *BlockRead( access_t * );
65 static int Seek( access_t *, int64_t );
66 static int Control( access_t *, int, va_list );
70 vlc_bool_t b_seekable;
71 vlc_bool_t b_pace_control;
73 rtsp_client_t *p_rtsp;
80 /*****************************************************************************
82 *****************************************************************************/
83 static int RtspConnect( void *p_userdata, char *psz_server, int i_port )
85 access_t *p_access = (access_t *)p_userdata;
86 access_sys_t *p_sys = p_access->p_sys;
89 p_sys->fd = net_ConnectTCP( p_access, psz_server, i_port );
92 msg_Err( p_access, "cannot connect to %s:%d", psz_server, i_port );
93 intf_UserFatal( p_access, VLC_FALSE, _("Connection failed"),
94 _("VLC could not connect to \"%s:%d\"."), psz_server, i_port );
101 static int RtspDisconnect( void *p_userdata )
103 access_t *p_access = (access_t *)p_userdata;
104 access_sys_t *p_sys = p_access->p_sys;
106 net_Close( p_sys->fd );
110 static int RtspRead( void *p_userdata, uint8_t *p_buffer, int i_buffer )
112 access_t *p_access = (access_t *)p_userdata;
113 access_sys_t *p_sys = p_access->p_sys;
115 return net_Read( p_access, p_sys->fd, 0, p_buffer, i_buffer, VLC_TRUE );
118 static int RtspReadLine( void *p_userdata, uint8_t *p_buffer, int i_buffer )
120 access_t *p_access = (access_t *)p_userdata;
121 access_sys_t *p_sys = p_access->p_sys;
123 char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, 0 );
125 //fprintf(stderr, "ReadLine: %s\n", psz);
127 if( psz ) strncpy( (char *)p_buffer, psz, i_buffer );
130 if( psz ) free( psz );
134 static int RtspWrite( void *p_userdata, uint8_t *p_buffer, int i_buffer )
136 access_t *p_access = (access_t *)p_userdata;
137 access_sys_t *p_sys = p_access->p_sys;
139 //fprintf(stderr, "Write: %s", p_buffer);
141 net_Printf( VLC_OBJECT(p_access), p_sys->fd, 0, "%s", p_buffer );
146 /*****************************************************************************
147 * Open: open the rtsp connection
148 *****************************************************************************/
149 static int Open( vlc_object_t *p_this )
151 access_t *p_access = (access_t *)p_this;
153 char *psz_server = 0;
156 if( !p_access->b_force ) return VLC_EGENERIC;
158 p_access->pf_read = NULL;
159 p_access->pf_block = BlockRead;
160 p_access->pf_seek = Seek;
161 p_access->pf_control = Control;
162 p_access->info.i_update = 0;
163 p_access->info.i_size = 0;
164 p_access->info.i_pos = 0;
165 p_access->info.b_eof = VLC_FALSE;
166 p_access->info.i_title = 0;
167 p_access->info.i_seekpoint = 0;
168 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
169 p_sys->p_rtsp = malloc( sizeof( rtsp_client_t) );
172 p_sys->p_rtsp->p_userdata = p_access;
173 p_sys->p_rtsp->pf_connect = RtspConnect;
174 p_sys->p_rtsp->pf_disconnect = RtspDisconnect;
175 p_sys->p_rtsp->pf_read = RtspRead;
176 p_sys->p_rtsp->pf_read_line = RtspReadLine;
177 p_sys->p_rtsp->pf_write = RtspWrite;
179 i_result = rtsp_connect( p_sys->p_rtsp, p_access->psz_path, 0 );
182 msg_Dbg( p_access, "could not connect to: %s", p_access->psz_path );
183 free( p_sys->p_rtsp );
188 msg_Dbg( p_access, "rtsp connected" );
190 /* looking for server type */
191 if( rtsp_search_answers( p_sys->p_rtsp, "Server" ) )
192 psz_server = strdup( rtsp_search_answers( p_sys->p_rtsp, "Server" ) );
195 if( rtsp_search_answers( p_sys->p_rtsp, "RealChallenge1" ) )
196 psz_server = strdup("Real");
198 psz_server = strdup("unknown");
201 if( strstr( psz_server, "Real" ) || strstr( psz_server, "Helix" ) )
203 uint32_t bandwidth = 10485800;
206 msg_Dbg( p_access, "found a real/helix rtsp server" );
208 if( !(h = real_setup_and_get_header( p_sys->p_rtsp, bandwidth )) )
210 /* Check if we got a redirect */
211 if( rtsp_search_answers( p_sys->p_rtsp, "Location" ) )
213 msg_Dbg( p_access, "redirect: %s",
214 rtsp_search_answers(p_sys->p_rtsp, "Location") );
215 msg_Warn( p_access, "redirect not supported" );
220 msg_Err( p_access, "rtsp session can not be established" );
221 intf_UserFatal( p_access, VLC_FALSE, _("Session failed"),
222 _("The requested RTSP session could not be established.") );
226 p_sys->p_header = block_New( p_access, 4096 );
227 p_sys->p_header->i_buffer =
228 rmff_dump_header( h, p_sys->p_header->p_buffer, 1024 );
229 rmff_free_header( h );
233 msg_Dbg( p_access, "only real/helix rtsp servers supported for now" );
237 /* Update default_pts to a suitable value for file access */
238 var_Create( p_access, "realrtsp-caching",
239 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
241 if( psz_server ) free( psz_server );
245 if( psz_server ) free( psz_server );
250 /*****************************************************************************
251 * Close: close the target
252 *****************************************************************************/
253 static void Close( vlc_object_t * p_this )
255 access_t *p_access = (access_t*)p_this;
256 access_sys_t *p_sys = p_access->p_sys;
258 if( p_sys->p_rtsp ) rtsp_close( p_sys->p_rtsp );
259 if( p_sys->p_rtsp ) free( p_sys->p_rtsp );
263 /*****************************************************************************
264 * Read: standard read on a file descriptor.
265 *****************************************************************************/
266 static block_t *BlockRead( access_t *p_access )
268 access_sys_t *p_sys = p_access->p_sys;
270 rmff_pheader_t pheader;
273 if( p_sys->p_header )
275 p_block = p_sys->p_header;
280 i_size = real_get_rdt_chunk_header( p_access->p_sys->p_rtsp, &pheader );
281 if( i_size <= 0 ) return 0;
283 p_block = block_New( p_access, i_size );
284 p_block->i_buffer = real_get_rdt_chunk( p_access->p_sys->p_rtsp, &pheader,
285 &p_block->p_buffer );
290 /*****************************************************************************
291 * Seek: seek to a specific location in a file
292 *****************************************************************************/
293 static int Seek( access_t *p_access, int64_t i_pos )
298 /*****************************************************************************
300 *****************************************************************************/
301 static int Control( access_t *p_access, int i_query, va_list args )
310 case ACCESS_CAN_SEEK:
311 case ACCESS_CAN_FASTSEEK:
312 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
313 *pb_bool = VLC_FALSE;//p_sys->b_seekable;
316 case ACCESS_CAN_PAUSE:
317 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
318 *pb_bool = VLC_FALSE;
321 case ACCESS_CAN_CONTROL_PACE:
322 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
323 *pb_bool = VLC_TRUE;//p_sys->b_pace_control;
328 pi_int = (int*)va_arg( args, int * );
332 case ACCESS_GET_PTS_DELAY:
333 pi_64 = (int64_t*)va_arg( args, int64_t * );
334 *pi_64 = var_GetInteger( p_access, "realrtsp-caching" ) * 1000;
338 case ACCESS_SET_PAUSE_STATE:
342 case ACCESS_GET_TITLE_INFO:
343 case ACCESS_SET_TITLE:
344 case ACCESS_SET_SEEKPOINT:
345 case ACCESS_SET_PRIVATE_ID_STATE:
346 case ACCESS_GET_META:
350 msg_Warn( p_access, "unimplemented query in control" );