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 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_access.h>
34 #include <vlc_dialog.h>
36 #include <vlc_network.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
47 set_description( N_("Real RTSP") )
48 set_shortname( N_("Real RTSP") )
49 set_category( CAT_INPUT )
50 set_subcategory( SUBCAT_INPUT_ACCESS )
51 set_capability( "access", 10 )
52 set_callbacks( Open, Close )
53 add_shortcut( "realrtsp", "rtsp", "pnm" )
57 /*****************************************************************************
59 *****************************************************************************/
60 static block_t *BlockRead( access_t * );
61 static int Seek( access_t *, uint64_t );
62 static int Control( access_t *, int, va_list );
66 rtsp_client_t *p_rtsp;
73 /*****************************************************************************
75 *****************************************************************************/
76 static int RtspConnect( void *p_userdata, char *psz_server, int i_port )
78 access_t *p_access = (access_t *)p_userdata;
79 access_sys_t *p_sys = p_access->p_sys;
82 p_sys->fd = net_ConnectTCP( p_access, psz_server, i_port );
85 msg_Err( p_access, "cannot connect to %s:%d", psz_server, i_port );
86 dialog_Fatal( p_access, _("Connection failed"),
87 _("VLC could not connect to \"%s:%d\"."), psz_server, i_port );
94 static int RtspDisconnect( void *p_userdata )
96 access_t *p_access = (access_t *)p_userdata;
97 access_sys_t *p_sys = p_access->p_sys;
99 net_Close( p_sys->fd );
103 static int RtspRead( void *p_userdata, uint8_t *p_buffer, int i_buffer )
105 access_t *p_access = (access_t *)p_userdata;
106 access_sys_t *p_sys = p_access->p_sys;
108 return net_Read( p_access, p_sys->fd, 0, p_buffer, i_buffer, true );
111 static int RtspReadLine( void *p_userdata, uint8_t *p_buffer, int i_buffer )
113 access_t *p_access = (access_t *)p_userdata;
114 access_sys_t *p_sys = p_access->p_sys;
116 char *psz = net_Gets( p_access, p_sys->fd, 0 );
118 //fprintf(stderr, "ReadLine: %s\n", psz);
120 if( psz ) strncpy( (char *)p_buffer, psz, i_buffer );
127 static int RtspWrite( void *p_userdata, uint8_t *p_buffer, int i_buffer )
129 VLC_UNUSED(i_buffer);
130 access_t *p_access = (access_t *)p_userdata;
131 access_sys_t *p_sys = p_access->p_sys;
133 //fprintf(stderr, "Write: %s", p_buffer);
135 net_Printf( p_access, p_sys->fd, 0, "%s", p_buffer );
140 /*****************************************************************************
141 * Open: open the rtsp connection
142 *****************************************************************************/
143 static int Open( vlc_object_t *p_this )
145 access_t *p_access = (access_t *)p_this;
147 char* psz_server = NULL;
150 if( !p_access->psz_access || (
151 strncmp( p_access->psz_access, "rtsp", 4 ) &&
152 strncmp( p_access->psz_access, "pnm", 3 ) &&
153 strncmp( p_access->psz_access, "realrtsp", 8 ) ))
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 = 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 ) );
171 p_sys->p_rtsp = malloc( sizeof( rtsp_client_t) );
178 p_sys->p_header = NULL;
179 p_sys->p_rtsp->p_userdata = p_access;
180 p_sys->p_rtsp->pf_connect = RtspConnect;
181 p_sys->p_rtsp->pf_disconnect = RtspDisconnect;
182 p_sys->p_rtsp->pf_read = RtspRead;
183 p_sys->p_rtsp->pf_read_line = RtspReadLine;
184 p_sys->p_rtsp->pf_write = RtspWrite;
186 i_result = rtsp_connect( p_sys->p_rtsp, p_access->psz_location, 0 );
189 msg_Dbg( p_access, "could not connect to: %s", p_access->psz_location );
190 free( p_sys->p_rtsp );
191 p_sys->p_rtsp = NULL;
195 msg_Dbg( p_access, "rtsp connected" );
197 /* looking for server type */
198 if( rtsp_search_answers( p_sys->p_rtsp, "Server" ) )
199 psz_server = strdup( rtsp_search_answers( p_sys->p_rtsp, "Server" ) );
202 if( rtsp_search_answers( p_sys->p_rtsp, "RealChallenge1" ) )
203 psz_server = strdup("Real");
205 psz_server = strdup("unknown");
208 if( strstr( psz_server, "Real" ) || strstr( psz_server, "Helix" ) )
210 uint32_t bandwidth = 10485800;
213 msg_Dbg( p_access, "found a real/helix rtsp server" );
215 if( !(h = real_setup_and_get_header( p_sys->p_rtsp, bandwidth )) )
217 /* Check if we got a redirect */
218 if( rtsp_search_answers( p_sys->p_rtsp, "Location" ) )
220 msg_Dbg( p_access, "redirect: %s",
221 rtsp_search_answers(p_sys->p_rtsp, "Location") );
222 msg_Warn( p_access, "redirect not supported" );
227 msg_Err( p_access, "rtsp session can not be established" );
228 dialog_Fatal( p_access, _("Session failed"), "%s",
229 _("The requested RTSP session could not be established.") );
233 p_sys->p_header = block_Alloc( 4096 );
234 p_sys->p_header->i_buffer =
235 rmff_dump_header( h, (char *)p_sys->p_header->p_buffer, 1024 );
236 rmff_free_header( h );
240 msg_Warn( p_access, "only real/helix rtsp servers supported for now" );
253 /*****************************************************************************
254 * Close: close the target
255 *****************************************************************************/
256 static void Close( vlc_object_t * p_this )
258 access_t *p_access = (access_t*)p_this;
259 access_sys_t *p_sys = p_access->p_sys;
261 if( p_sys->p_rtsp ) rtsp_close( p_sys->p_rtsp );
262 free( p_sys->p_rtsp );
266 /*****************************************************************************
267 * Read: standard read on a file descriptor.
268 *****************************************************************************/
269 static block_t *BlockRead( access_t *p_access )
271 access_sys_t *p_sys = p_access->p_sys;
273 rmff_pheader_t pheader;
276 if( p_sys->p_header )
278 p_block = p_sys->p_header;
279 p_sys->p_header = NULL;
283 i_size = real_get_rdt_chunk_header( p_access->p_sys->p_rtsp, &pheader );
284 if( i_size <= 0 ) return NULL;
286 p_block = block_Alloc( i_size );
287 p_block->i_buffer = real_get_rdt_chunk( p_access->p_sys->p_rtsp, &pheader,
288 &p_block->p_buffer );
293 /*****************************************************************************
294 * Seek: seek to a specific location in a file
295 *****************************************************************************/
296 static int Seek( access_t *p_access, uint64_t i_pos )
298 VLC_UNUSED(p_access);
303 /*****************************************************************************
305 *****************************************************************************/
306 static int Control( access_t *p_access, int i_query, va_list args )
311 case ACCESS_CAN_SEEK:
312 case ACCESS_CAN_FASTSEEK:
313 case ACCESS_CAN_PAUSE:
314 *va_arg( args, bool* ) = false;
317 case ACCESS_CAN_CONTROL_PACE:
318 *va_arg( args, bool* ) = true;
321 case ACCESS_GET_PTS_DELAY:
322 *va_arg( args, int64_t * ) = INT64_C(1000)
323 * var_InheritInteger(p_access, "network-caching");
327 case ACCESS_SET_PAUSE_STATE:
331 case ACCESS_GET_TITLE_INFO:
332 case ACCESS_SET_TITLE:
333 case ACCESS_SET_SEEKPOINT:
334 case ACCESS_SET_PRIVATE_ID_STATE:
335 case ACCESS_GET_META:
336 case ACCESS_GET_CONTENT_TYPE:
340 msg_Warn( p_access, "unimplemented query in control" );