]> git.sesse.net Git - vlc/blob - modules/access/rtsp/access.c
Real RTSP: discard unsupported legacy username and password syntax
[vlc] / modules / access / rtsp / access.c
1 /*****************************************************************************
2  * access.c: Real rtsp input
3  *****************************************************************************
4  * Copyright (C) 2005 VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
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>
36
37 #include <vlc_network.h>
38 #include "rtsp.h"
39 #include "real.h"
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 static int  Open ( vlc_object_t * );
45 static void Close( vlc_object_t * );
46
47 vlc_module_begin ()
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" )
55 vlc_module_end ()
56
57
58 /*****************************************************************************
59  * Exported prototypes
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 );
64
65 struct access_sys_t
66 {
67     rtsp_client_t *p_rtsp;
68
69     int fd;
70
71     block_t *p_header;
72 };
73
74 /*****************************************************************************
75  * Network wrappers
76  *****************************************************************************/
77 static int RtspConnect( void *p_userdata, char *psz_server, int i_port )
78 {
79     access_t *p_access = (access_t *)p_userdata;
80     access_sys_t *p_sys = p_access->p_sys;
81
82     /* Open connection */
83     p_sys->fd = net_ConnectTCP( p_access, psz_server, i_port );
84     if( p_sys->fd < 0 )
85     {
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 );
89         return VLC_EGENERIC;
90     }
91
92     return VLC_SUCCESS;
93 }
94
95 static int RtspDisconnect( void *p_userdata )
96 {
97     access_t *p_access = (access_t *)p_userdata;
98     access_sys_t *p_sys = p_access->p_sys;
99
100     net_Close( p_sys->fd );
101     return VLC_SUCCESS;
102 }
103
104 static int RtspRead( void *p_userdata, uint8_t *p_buffer, int i_buffer )
105 {
106     access_t *p_access = (access_t *)p_userdata;
107     access_sys_t *p_sys = p_access->p_sys;
108
109     return net_Read( p_access, p_sys->fd, 0, p_buffer, i_buffer, true );
110 }
111
112 static int RtspReadLine( void *p_userdata, uint8_t *p_buffer, int i_buffer )
113 {
114     access_t *p_access = (access_t *)p_userdata;
115     access_sys_t *p_sys = p_access->p_sys;
116
117     char *psz = net_Gets( p_access, p_sys->fd, 0 );
118
119     //fprintf(stderr, "ReadLine: %s\n", psz);
120
121     if( psz ) strncpy( (char *)p_buffer, psz, i_buffer );
122     else *p_buffer = 0;
123
124     free( psz );
125     return 0;
126 }
127
128 static int RtspWrite( void *p_userdata, uint8_t *p_buffer, int i_buffer )
129 {
130     VLC_UNUSED(i_buffer);
131     access_t *p_access = (access_t *)p_userdata;
132     access_sys_t *p_sys = p_access->p_sys;
133
134     //fprintf(stderr, "Write: %s", p_buffer);
135
136     net_Printf( p_access, p_sys->fd, 0, "%s", p_buffer );
137
138     return 0;
139 }
140
141 /*****************************************************************************
142  * Open: open the rtsp connection
143  *****************************************************************************/
144 static int Open( vlc_object_t *p_this )
145 {
146     access_t *p_access = (access_t *)p_this;
147     access_sys_t *p_sys;
148     char* psz_server = NULL;
149     int i_result;
150
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 ) ))
155     {
156             return VLC_EGENERIC;
157     }
158
159     /* Discard legacy username/password syntax - not supported */
160     const char *psz_location = strchr( p_access->psz_location, '@' );
161     if( psz_location != NULL )
162         psz_location++;
163     else
164         psz_location = p_access->psz_location;
165
166     p_access->pf_read = NULL;
167     p_access->pf_block = BlockRead;
168     p_access->pf_seek = Seek;
169     p_access->pf_control = Control;
170     p_access->info.i_pos = 0;
171     p_access->info.b_eof = false;
172     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
173     if( !p_sys )
174         return VLC_ENOMEM;
175     p_sys->p_rtsp = malloc( sizeof( rtsp_client_t) );
176     if( !p_sys->p_rtsp )
177     {
178         free( p_sys );
179         return VLC_ENOMEM;
180     }
181
182     p_sys->p_header = NULL;
183     p_sys->p_rtsp->p_userdata = p_access;
184     p_sys->p_rtsp->pf_connect = RtspConnect;
185     p_sys->p_rtsp->pf_disconnect = RtspDisconnect;
186     p_sys->p_rtsp->pf_read = RtspRead;
187     p_sys->p_rtsp->pf_read_line = RtspReadLine;
188     p_sys->p_rtsp->pf_write = RtspWrite;
189
190     i_result = rtsp_connect( p_sys->p_rtsp, psz_location, 0 );
191     if( i_result )
192     {
193         msg_Dbg( p_access, "could not connect to: %s", psz_location );
194         free( p_sys->p_rtsp );
195         p_sys->p_rtsp = NULL;
196         goto error;
197     }
198
199     msg_Dbg( p_access, "rtsp connected" );
200
201     /* looking for server type */
202     if( rtsp_search_answers( p_sys->p_rtsp, "Server" ) )
203         psz_server = strdup( rtsp_search_answers( p_sys->p_rtsp, "Server" ) );
204     else
205     {
206         if( rtsp_search_answers( p_sys->p_rtsp, "RealChallenge1" ) )
207             psz_server = strdup("Real");
208         else
209             psz_server = strdup("unknown");
210     }
211
212     if( strstr( psz_server, "Real" ) || strstr( psz_server, "Helix" ) )
213     {
214         uint32_t bandwidth = 10485800;
215         rmff_header_t *h;
216
217         msg_Dbg( p_access, "found a real/helix rtsp server" );
218
219         if( !(h = real_setup_and_get_header( p_sys->p_rtsp, bandwidth )) )
220         {
221             /* Check if we got a redirect */
222             if( rtsp_search_answers( p_sys->p_rtsp, "Location" ) )
223             {
224                 msg_Dbg( p_access, "redirect: %s",
225                          rtsp_search_answers(p_sys->p_rtsp, "Location") );
226                 msg_Warn( p_access, "redirect not supported" );
227                 goto error;
228             }
229
230
231             msg_Err( p_access, "rtsp session can not be established" );
232             dialog_Fatal( p_access, _("Session failed"), "%s",
233                     _("The requested RTSP session could not be established.") );
234             goto error;
235         }
236
237         p_sys->p_header = block_Alloc( 4096 );
238         p_sys->p_header->i_buffer =
239             rmff_dump_header( h, (char *)p_sys->p_header->p_buffer, 1024 );
240         rmff_free_header( h );
241     }
242     else
243     {
244         msg_Warn( p_access, "only real/helix rtsp servers supported for now" );
245         goto error;
246     }
247
248     free( psz_server );
249     return VLC_SUCCESS;
250
251  error:
252     free( psz_server );
253     Close( p_this );
254     return VLC_EGENERIC;
255 }
256
257 /*****************************************************************************
258  * Close: close the target
259  *****************************************************************************/
260 static void Close( vlc_object_t * p_this )
261 {
262     access_t     *p_access = (access_t*)p_this;
263     access_sys_t *p_sys = p_access->p_sys;
264
265     if( p_sys->p_rtsp ) rtsp_close( p_sys->p_rtsp );
266     free( p_sys->p_rtsp );
267     free( p_sys );
268 }
269
270 /*****************************************************************************
271  * Read: standard read on a file descriptor.
272  *****************************************************************************/
273 static block_t *BlockRead( access_t *p_access )
274 {
275     access_sys_t *p_sys = p_access->p_sys;
276     block_t *p_block;
277     rmff_pheader_t pheader;
278     int i_size;
279
280     if( p_sys->p_header )
281     {
282         p_block = p_sys->p_header;
283         p_sys->p_header = NULL;
284         return p_block;
285     }
286
287     i_size = real_get_rdt_chunk_header( p_access->p_sys->p_rtsp, &pheader );
288     if( i_size <= 0 ) return NULL;
289
290     p_block = block_Alloc( i_size );
291     p_block->i_buffer = real_get_rdt_chunk( p_access->p_sys->p_rtsp, &pheader,
292                                             &p_block->p_buffer );
293
294     return p_block;
295 }
296
297 /*****************************************************************************
298  * Seek: seek to a specific location in a file
299  *****************************************************************************/
300 static int Seek( access_t *p_access, uint64_t i_pos )
301 {
302     VLC_UNUSED(p_access);
303     VLC_UNUSED(i_pos);
304     return VLC_SUCCESS;
305 }
306
307 /*****************************************************************************
308  * Control:
309  *****************************************************************************/
310 static int Control( access_t *p_access, int i_query, va_list args )
311 {
312     switch( i_query )
313     {
314         case ACCESS_CAN_SEEK:
315         case ACCESS_CAN_FASTSEEK:
316         case ACCESS_CAN_PAUSE:
317             *va_arg( args, bool* ) = false;
318             break;
319
320         case ACCESS_CAN_CONTROL_PACE:
321             *va_arg( args, bool* ) = true;
322             break;
323
324         case ACCESS_GET_PTS_DELAY:
325             *va_arg( args, int64_t * ) = INT64_C(1000)
326                 * var_InheritInteger(p_access, "network-caching");
327             break;
328
329         case ACCESS_SET_PAUSE_STATE:
330             /* Nothing to do */
331             break;
332
333         default:
334             return VLC_EGENERIC;
335     }
336
337     return VLC_SUCCESS;
338 }