]> git.sesse.net Git - vlc/blob - modules/access/rtsp/access.c
input options whitelisting, step 2 (refs #1371)
[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
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc_access.h>
29 #include <vlc_interface.h>
30
31 #include <vlc_network.h>
32 #include "rtsp.h"
33 #include "real.h"
34
35 /*****************************************************************************
36  * Module descriptor
37  *****************************************************************************/
38 static int  Open ( vlc_object_t * );
39 static void Close( vlc_object_t * );
40
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." )
45
46 vlc_module_begin();
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         change_safe();
54     set_capability( "access2", 10 );
55     set_callbacks( Open, Close );
56     add_shortcut( "realrtsp" );
57     add_shortcut( "rtsp" );
58     add_shortcut( "pnm" );
59 vlc_module_end();
60
61
62 /*****************************************************************************
63  * Exported prototypes
64  *****************************************************************************/
65 static block_t *BlockRead( access_t * );
66 static int     Seek( access_t *, int64_t );
67 static int     Control( access_t *, int, va_list );
68
69 struct access_sys_t
70 {
71     vlc_bool_t b_seekable;
72     vlc_bool_t b_pace_control;
73
74     rtsp_client_t *p_rtsp;
75
76     int fd;
77
78     block_t *p_header;
79 };
80
81 /*****************************************************************************
82  * Network wrappers
83  *****************************************************************************/
84 static int RtspConnect( void *p_userdata, char *psz_server, int i_port )
85 {
86     access_t *p_access = (access_t *)p_userdata;
87     access_sys_t *p_sys = p_access->p_sys;
88
89     /* Open connection */
90     p_sys->fd = net_ConnectTCP( p_access, psz_server, i_port );
91     if( p_sys->fd < 0 )
92     {
93         msg_Err( p_access, "cannot connect to %s:%d", psz_server, i_port );
94         intf_UserFatal( p_access, VLC_FALSE, _("Connection failed"),
95                         _("VLC could not connect to \"%s:%d\"."), psz_server, i_port );
96         return VLC_EGENERIC;
97     }
98
99     return VLC_SUCCESS;
100 }
101
102 static int RtspDisconnect( void *p_userdata )
103 {
104     access_t *p_access = (access_t *)p_userdata;
105     access_sys_t *p_sys = p_access->p_sys;
106
107     net_Close( p_sys->fd );
108     return VLC_SUCCESS;
109 }
110
111 static int RtspRead( void *p_userdata, uint8_t *p_buffer, int i_buffer )
112 {
113     access_t *p_access = (access_t *)p_userdata;
114     access_sys_t *p_sys = p_access->p_sys;
115
116     return net_Read( p_access, p_sys->fd, 0, p_buffer, i_buffer, VLC_TRUE );
117 }
118
119 static int RtspReadLine( void *p_userdata, uint8_t *p_buffer, int i_buffer )
120 {
121     access_t *p_access = (access_t *)p_userdata;
122     access_sys_t *p_sys = p_access->p_sys;
123
124     char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, 0 );
125
126     //fprintf(stderr, "ReadLine: %s\n", psz);
127
128     if( psz ) strncpy( (char *)p_buffer, psz, i_buffer );
129     else *p_buffer = 0;
130
131     if( psz ) free( psz );
132     return 0;
133 }
134
135 static int RtspWrite( void *p_userdata, uint8_t *p_buffer, int i_buffer )
136 {
137     access_t *p_access = (access_t *)p_userdata;
138     access_sys_t *p_sys = p_access->p_sys;
139
140     //fprintf(stderr, "Write: %s", p_buffer);
141
142     net_Printf( VLC_OBJECT(p_access), p_sys->fd, 0, "%s", p_buffer );
143
144     return 0;
145 }
146
147 /*****************************************************************************
148  * Open: open the rtsp connection
149  *****************************************************************************/
150 static int Open( vlc_object_t *p_this )
151 {
152     access_t *p_access = (access_t *)p_this;
153     access_sys_t *p_sys;
154     char *psz_server = 0;
155     int i_result;
156
157     if( !p_access->psz_access || (
158         strncmp( p_access->psz_access, "rtsp", 4 ) &&
159         strncmp( p_access->psz_access, "pnm", 3 )  &&
160         strncmp( p_access->psz_access, "realrtsp", 8 ) ))
161     {
162             return VLC_EGENERIC;
163     }
164
165     p_access->pf_read = NULL;
166     p_access->pf_block = BlockRead;
167     p_access->pf_seek = Seek;
168     p_access->pf_control = Control;
169     p_access->info.i_update = 0;
170     p_access->info.i_size = 0;
171     p_access->info.i_pos = 0;
172     p_access->info.b_eof = VLC_FALSE;
173     p_access->info.i_title = 0;
174     p_access->info.i_seekpoint = 0;
175     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
176     p_sys->p_rtsp = malloc( sizeof( rtsp_client_t) );
177
178     p_sys->p_header = 0;
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;
185
186     i_result = rtsp_connect( p_sys->p_rtsp, p_access->psz_path, 0 );
187     if( i_result )
188     {
189         msg_Dbg( p_access, "could not connect to: %s", p_access->psz_path );
190         free( p_sys->p_rtsp );
191         p_sys->p_rtsp = 0;
192         goto error;
193     }
194
195     msg_Dbg( p_access, "rtsp connected" );
196
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" ) );
200     else
201     {
202         if( rtsp_search_answers( p_sys->p_rtsp, "RealChallenge1" ) )
203             psz_server = strdup("Real");
204         else
205             psz_server = strdup("unknown");
206     }
207
208     if( strstr( psz_server, "Real" ) || strstr( psz_server, "Helix" ) )
209     {
210         uint32_t bandwidth = 10485800;
211         rmff_header_t *h;
212
213         msg_Dbg( p_access, "found a real/helix rtsp server" );
214
215         if( !(h = real_setup_and_get_header( p_sys->p_rtsp, bandwidth )) )
216         {
217             /* Check if we got a redirect */
218             if( rtsp_search_answers( p_sys->p_rtsp, "Location" ) )
219             {
220                 msg_Dbg( p_access, "redirect: %s",
221                          rtsp_search_answers(p_sys->p_rtsp, "Location") );
222                 msg_Warn( p_access, "redirect not supported" );
223                 goto error;
224             }
225
226
227             msg_Err( p_access, "rtsp session can not be established" );
228             intf_UserFatal( p_access, VLC_FALSE, _("Session failed"),
229                     _("The requested RTSP session could not be established.") );
230             goto error;
231         }
232
233         p_sys->p_header = block_New( p_access, 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 );
237     }
238     else
239     {
240         msg_Warn( p_access, "only real/helix rtsp servers supported for now" );
241         goto error;
242     }
243
244     /* Update default_pts to a suitable value for file access */
245     var_Create( p_access, "realrtsp-caching",
246                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
247
248     if( psz_server ) free( psz_server );
249     return VLC_SUCCESS;
250
251  error:
252     if( psz_server ) 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     if( p_sys->p_rtsp ) 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 = 0;
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 0;
289
290     p_block = block_New( p_access, 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, int64_t i_pos )
301 {
302     return VLC_SUCCESS;
303 }
304
305 /*****************************************************************************
306  * Control:
307  *****************************************************************************/
308 static int Control( access_t *p_access, int i_query, va_list args )
309 {
310     vlc_bool_t   *pb_bool;
311     int          *pi_int;
312     int64_t      *pi_64;
313
314     switch( i_query )
315     {
316         /* */
317         case ACCESS_CAN_SEEK:
318         case ACCESS_CAN_FASTSEEK:
319             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
320             *pb_bool = VLC_FALSE;//p_sys->b_seekable;
321             break;
322
323         case ACCESS_CAN_PAUSE:
324             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
325             *pb_bool = VLC_FALSE;
326             break;
327
328         case ACCESS_CAN_CONTROL_PACE:
329             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
330             *pb_bool = VLC_TRUE;//p_sys->b_pace_control;
331             break;
332
333         /* */
334         case ACCESS_GET_MTU:
335             pi_int = (int*)va_arg( args, int * );
336             *pi_int = 0;
337             break;
338
339         case ACCESS_GET_PTS_DELAY:
340             pi_64 = (int64_t*)va_arg( args, int64_t * );
341             *pi_64 = var_GetInteger( p_access, "realrtsp-caching" ) * 1000;
342             break;
343
344         /* */
345         case ACCESS_SET_PAUSE_STATE:
346             /* Nothing to do */
347             break;
348
349         case ACCESS_GET_TITLE_INFO:
350         case ACCESS_SET_TITLE:
351         case ACCESS_SET_SEEKPOINT:
352         case ACCESS_SET_PRIVATE_ID_STATE:
353         case ACCESS_GET_META:
354         case ACCESS_GET_CONTENT_TYPE:
355             return VLC_EGENERIC;
356
357         default:
358             msg_Warn( p_access, "unimplemented query in control" );
359             return VLC_EGENERIC;
360     }
361
362     return VLC_SUCCESS;
363 }