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 along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_access.h>
35 #include <vlc_dialog.h>
37 #include <vlc_network.h>
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close( vlc_object_t * );
48 set_description( N_("Real RTSP") )
49 set_shortname( N_("Real RTSP") )
50 set_category( CAT_INPUT )
51 set_subcategory( SUBCAT_INPUT_ACCESS )
52 set_capability( "access", 10 )
53 set_callbacks( Open, Close )
54 add_shortcut( "realrtsp", "rtsp", "pnm" )
58 /*****************************************************************************
60 *****************************************************************************/
61 static block_t *BlockRead( access_t * );
62 static int Seek( access_t *, uint64_t );
63 static int Control( access_t *, int, va_list );
67 rtsp_client_t *p_rtsp;
74 /*****************************************************************************
76 *****************************************************************************/
77 static int RtspConnect( void *p_userdata, char *psz_server, int i_port )
79 access_t *p_access = (access_t *)p_userdata;
80 access_sys_t *p_sys = p_access->p_sys;
83 p_sys->fd = net_ConnectTCP( p_access, psz_server, i_port );
86 msg_Err( p_access, "cannot connect to %s:%d", psz_server, i_port );
87 dialog_Fatal( p_access, _("Connection failed"),
88 _("VLC could not connect to \"%s:%d\"."), psz_server, i_port );
95 static int RtspDisconnect( void *p_userdata )
97 access_t *p_access = (access_t *)p_userdata;
98 access_sys_t *p_sys = p_access->p_sys;
100 net_Close( p_sys->fd );
104 static int RtspRead( void *p_userdata, uint8_t *p_buffer, int i_buffer )
106 access_t *p_access = (access_t *)p_userdata;
107 access_sys_t *p_sys = p_access->p_sys;
109 return net_Read( p_access, p_sys->fd, 0, p_buffer, i_buffer, true );
112 static int RtspReadLine( void *p_userdata, uint8_t *p_buffer, int i_buffer )
114 access_t *p_access = (access_t *)p_userdata;
115 access_sys_t *p_sys = p_access->p_sys;
117 char *psz = net_Gets( p_access, p_sys->fd, 0 );
119 //fprintf(stderr, "ReadLine: %s\n", psz);
121 if( psz ) strncpy( (char *)p_buffer, psz, i_buffer );
128 static int RtspWrite( void *p_userdata, uint8_t *p_buffer, int i_buffer )
130 VLC_UNUSED(i_buffer);
131 access_t *p_access = (access_t *)p_userdata;
132 access_sys_t *p_sys = p_access->p_sys;
134 //fprintf(stderr, "Write: %s", p_buffer);
136 net_Printf( p_access, p_sys->fd, 0, "%s", p_buffer );
141 /*****************************************************************************
142 * Open: open the rtsp connection
143 *****************************************************************************/
144 static int Open( vlc_object_t *p_this )
146 access_t *p_access = (access_t *)p_this;
148 char* psz_server = NULL;
151 if( !p_access->psz_access || (
152 strncmp( p_access->psz_access, "rtsp", 4 ) &&
153 strncmp( p_access->psz_access, "pnm", 3 ) &&
154 strncmp( p_access->psz_access, "realrtsp", 8 ) ))
159 p_access->pf_read = NULL;
160 p_access->pf_block = BlockRead;
161 p_access->pf_seek = Seek;
162 p_access->pf_control = Control;
163 p_access->info.i_pos = 0;
164 p_access->info.b_eof = false;
165 p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
168 p_sys->p_rtsp = malloc( sizeof( rtsp_client_t) );
175 p_sys->p_header = NULL;
176 p_sys->p_rtsp->p_userdata = p_access;
177 p_sys->p_rtsp->pf_connect = RtspConnect;
178 p_sys->p_rtsp->pf_disconnect = RtspDisconnect;
179 p_sys->p_rtsp->pf_read = RtspRead;
180 p_sys->p_rtsp->pf_read_line = RtspReadLine;
181 p_sys->p_rtsp->pf_write = RtspWrite;
183 i_result = rtsp_connect( p_sys->p_rtsp, p_access->psz_location, 0 );
186 msg_Dbg( p_access, "could not connect to: %s", p_access->psz_location );
187 free( p_sys->p_rtsp );
188 p_sys->p_rtsp = NULL;
192 msg_Dbg( p_access, "rtsp connected" );
194 /* looking for server type */
195 if( rtsp_search_answers( p_sys->p_rtsp, "Server" ) )
196 psz_server = strdup( rtsp_search_answers( p_sys->p_rtsp, "Server" ) );
199 if( rtsp_search_answers( p_sys->p_rtsp, "RealChallenge1" ) )
200 psz_server = strdup("Real");
202 psz_server = strdup("unknown");
205 if( strstr( psz_server, "Real" ) || strstr( psz_server, "Helix" ) )
207 uint32_t bandwidth = 10485800;
210 msg_Dbg( p_access, "found a real/helix rtsp server" );
212 if( !(h = real_setup_and_get_header( p_sys->p_rtsp, bandwidth )) )
214 /* Check if we got a redirect */
215 if( rtsp_search_answers( p_sys->p_rtsp, "Location" ) )
217 msg_Dbg( p_access, "redirect: %s",
218 rtsp_search_answers(p_sys->p_rtsp, "Location") );
219 msg_Warn( p_access, "redirect not supported" );
224 msg_Err( p_access, "rtsp session can not be established" );
225 dialog_Fatal( p_access, _("Session failed"), "%s",
226 _("The requested RTSP session could not be established.") );
230 p_sys->p_header = block_Alloc( 4096 );
231 p_sys->p_header->i_buffer =
232 rmff_dump_header( h, (char *)p_sys->p_header->p_buffer, 1024 );
233 rmff_free_header( h );
237 msg_Warn( p_access, "only real/helix rtsp servers supported for now" );
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 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;
276 p_sys->p_header = NULL;
280 i_size = real_get_rdt_chunk_header( p_access->p_sys->p_rtsp, &pheader );
281 if( i_size <= 0 ) return NULL;
283 p_block = block_Alloc( 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, uint64_t i_pos )
295 VLC_UNUSED(p_access);
300 /*****************************************************************************
302 *****************************************************************************/
303 static int Control( access_t *p_access, int i_query, va_list args )
307 case ACCESS_CAN_SEEK:
308 case ACCESS_CAN_FASTSEEK:
309 case ACCESS_CAN_PAUSE:
310 *va_arg( args, bool* ) = false;
313 case ACCESS_CAN_CONTROL_PACE:
314 *va_arg( args, bool* ) = true;
317 case ACCESS_GET_PTS_DELAY:
318 *va_arg( args, int64_t * ) = INT64_C(1000)
319 * var_InheritInteger(p_access, "network-caching");
322 case ACCESS_SET_PAUSE_STATE: