]> git.sesse.net Git - vlc/blob - modules/access/rtsp/access.c
update module LIST file.
[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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc/vlc.h>
32 #include <vlc_access.h>
33 #include <vlc_interface.h>
34
35 #include <vlc_network.h>
36 #include "rtsp.h"
37 #include "real.h"
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 static int  Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
44
45 #define CACHING_TEXT N_("Caching value (ms)")
46 #define CACHING_LONGTEXT N_( \
47     "Caching value for RTSP streams. This " \
48     "value should be set in milliseconds." )
49
50 vlc_module_begin();
51     set_description( _("Real RTSP") );
52     set_shortname( _("Real RTSP") );
53     set_category( CAT_INPUT );
54     set_subcategory( SUBCAT_INPUT_ACCESS );
55     add_integer( "realrtsp-caching", 3000, NULL,
56                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
57     set_capability( "access2", 10 );
58     set_callbacks( Open, Close );
59     add_shortcut( "realrtsp" );
60     add_shortcut( "rtsp" );
61     add_shortcut( "pnm" );
62 vlc_module_end();
63
64
65 /*****************************************************************************
66  * Exported prototypes
67  *****************************************************************************/
68 static block_t *BlockRead( access_t * );
69 static int     Seek( access_t *, int64_t );
70 static int     Control( access_t *, int, va_list );
71
72 struct access_sys_t
73 {
74     vlc_bool_t b_seekable;
75     vlc_bool_t b_pace_control;
76
77     rtsp_client_t *p_rtsp;
78
79     int fd;
80
81     block_t *p_header;
82 };
83
84 /*****************************************************************************
85  * Network wrappers
86  *****************************************************************************/
87 static int RtspConnect( void *p_userdata, char *psz_server, int i_port )
88 {
89     access_t *p_access = (access_t *)p_userdata;
90     access_sys_t *p_sys = p_access->p_sys;
91
92     /* Open connection */
93     p_sys->fd = net_ConnectTCP( p_access, psz_server, i_port );
94     if( p_sys->fd < 0 )
95     {
96         msg_Err( p_access, "cannot connect to %s:%d", psz_server, i_port );
97         intf_UserFatal( p_access, VLC_FALSE, _("Connection failed"),
98                         _("VLC could not connect to \"%s:%d\"."), psz_server, i_port );
99         return VLC_EGENERIC;
100     }
101
102     return VLC_SUCCESS;
103 }
104
105 static int RtspDisconnect( void *p_userdata )
106 {
107     access_t *p_access = (access_t *)p_userdata;
108     access_sys_t *p_sys = p_access->p_sys;
109
110     net_Close( p_sys->fd );
111     return VLC_SUCCESS;
112 }
113
114 static int RtspRead( void *p_userdata, uint8_t *p_buffer, int i_buffer )
115 {
116     access_t *p_access = (access_t *)p_userdata;
117     access_sys_t *p_sys = p_access->p_sys;
118
119     return net_Read( p_access, p_sys->fd, 0, p_buffer, i_buffer, VLC_TRUE );
120 }
121
122 static int RtspReadLine( void *p_userdata, uint8_t *p_buffer, int i_buffer )
123 {
124     access_t *p_access = (access_t *)p_userdata;
125     access_sys_t *p_sys = p_access->p_sys;
126
127     char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, 0 );
128
129     //fprintf(stderr, "ReadLine: %s\n", psz);
130
131     if( psz ) strncpy( (char *)p_buffer, psz, i_buffer );
132     else *p_buffer = 0;
133
134     free( psz );
135     return 0;
136 }
137
138 static int RtspWrite( void *p_userdata, uint8_t *p_buffer, int i_buffer )
139 {
140     access_t *p_access = (access_t *)p_userdata;
141     access_sys_t *p_sys = p_access->p_sys;
142
143     //fprintf(stderr, "Write: %s", p_buffer);
144
145     net_Printf( VLC_OBJECT(p_access), p_sys->fd, 0, "%s", p_buffer );
146
147     return 0;
148 }
149
150 /*****************************************************************************
151  * Open: open the rtsp connection
152  *****************************************************************************/
153 static int Open( vlc_object_t *p_this )
154 {
155     access_t *p_access = (access_t *)p_this;
156     access_sys_t *p_sys;
157     char *psz_server = 0;
158     int i_result;
159
160     if( !p_access->psz_access || (
161         strncmp( p_access->psz_access, "rtsp", 4 ) &&
162         strncmp( p_access->psz_access, "pnm", 3 )  &&
163         strncmp( p_access->psz_access, "realrtsp", 8 ) ))
164     {
165             return VLC_EGENERIC;
166     }
167
168     p_access->pf_read = NULL;
169     p_access->pf_block = BlockRead;
170     p_access->pf_seek = Seek;
171     p_access->pf_control = Control;
172     p_access->info.i_update = 0;
173     p_access->info.i_size = 0;
174     p_access->info.i_pos = 0;
175     p_access->info.b_eof = VLC_FALSE;
176     p_access->info.i_title = 0;
177     p_access->info.i_seekpoint = 0;
178     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
179     p_sys->p_rtsp = malloc( sizeof( rtsp_client_t) );
180
181     p_sys->p_header = 0;
182     p_sys->p_rtsp->p_userdata = p_access;
183     p_sys->p_rtsp->pf_connect = RtspConnect;
184     p_sys->p_rtsp->pf_disconnect = RtspDisconnect;
185     p_sys->p_rtsp->pf_read = RtspRead;
186     p_sys->p_rtsp->pf_read_line = RtspReadLine;
187     p_sys->p_rtsp->pf_write = RtspWrite;
188
189     i_result = rtsp_connect( p_sys->p_rtsp, p_access->psz_path, 0 );
190     if( i_result )
191     {
192         msg_Dbg( p_access, "could not connect to: %s", p_access->psz_path );
193         free( p_sys->p_rtsp );
194         p_sys->p_rtsp = 0;
195         goto error;
196     }
197
198     msg_Dbg( p_access, "rtsp connected" );
199
200     /* looking for server type */
201     if( rtsp_search_answers( p_sys->p_rtsp, "Server" ) )
202         psz_server = strdup( rtsp_search_answers( p_sys->p_rtsp, "Server" ) );
203     else
204     {
205         if( rtsp_search_answers( p_sys->p_rtsp, "RealChallenge1" ) )
206             psz_server = strdup("Real");
207         else
208             psz_server = strdup("unknown");
209     }
210
211     if( strstr( psz_server, "Real" ) || strstr( psz_server, "Helix" ) )
212     {
213         uint32_t bandwidth = 10485800;
214         rmff_header_t *h;
215
216         msg_Dbg( p_access, "found a real/helix rtsp server" );
217
218         if( !(h = real_setup_and_get_header( p_sys->p_rtsp, bandwidth )) )
219         {
220             /* Check if we got a redirect */
221             if( rtsp_search_answers( p_sys->p_rtsp, "Location" ) )
222             {
223                 msg_Dbg( p_access, "redirect: %s",
224                          rtsp_search_answers(p_sys->p_rtsp, "Location") );
225                 msg_Warn( p_access, "redirect not supported" );
226                 goto error;
227             }
228
229
230             msg_Err( p_access, "rtsp session can not be established" );
231             intf_UserFatal( p_access, VLC_FALSE, _("Session failed"),
232                     _("The requested RTSP session could not be established.") );
233             goto error;
234         }
235
236         p_sys->p_header = block_New( p_access, 4096 );
237         p_sys->p_header->i_buffer =
238             rmff_dump_header( h, (char *)p_sys->p_header->p_buffer, 1024 );
239         rmff_free_header( h );
240     }
241     else
242     {
243         msg_Warn( p_access, "only real/helix rtsp servers supported for now" );
244         goto error;
245     }
246
247     /* Update default_pts to a suitable value for file access */
248     var_Create( p_access, "realrtsp-caching",
249                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
250
251     free( psz_server );
252     return VLC_SUCCESS;
253
254  error:
255     free( psz_server );
256     Close( p_this );
257     return VLC_EGENERIC;
258 }
259
260 /*****************************************************************************
261  * Close: close the target
262  *****************************************************************************/
263 static void Close( vlc_object_t * p_this )
264 {
265     access_t     *p_access = (access_t*)p_this;
266     access_sys_t *p_sys = p_access->p_sys;
267
268     if( p_sys->p_rtsp ) rtsp_close( p_sys->p_rtsp );
269     free( p_sys->p_rtsp );
270     free( p_sys );
271 }
272
273 /*****************************************************************************
274  * Read: standard read on a file descriptor.
275  *****************************************************************************/
276 static block_t *BlockRead( access_t *p_access )
277 {
278     access_sys_t *p_sys = p_access->p_sys;
279     block_t *p_block;
280     rmff_pheader_t pheader;
281     int i_size;
282
283     if( p_sys->p_header )
284     {
285         p_block = p_sys->p_header;
286         p_sys->p_header = 0;
287         return p_block;
288     }
289
290     i_size = real_get_rdt_chunk_header( p_access->p_sys->p_rtsp, &pheader );
291     if( i_size <= 0 ) return 0;
292
293     p_block = block_New( p_access, i_size );
294     p_block->i_buffer = real_get_rdt_chunk( p_access->p_sys->p_rtsp, &pheader,
295                                             &p_block->p_buffer );
296
297     return p_block;
298 }
299
300 /*****************************************************************************
301  * Seek: seek to a specific location in a file
302  *****************************************************************************/
303 static int Seek( access_t *p_access, int64_t i_pos )
304 {
305     return VLC_SUCCESS;
306 }
307
308 /*****************************************************************************
309  * Control:
310  *****************************************************************************/
311 static int Control( access_t *p_access, int i_query, va_list args )
312 {
313     vlc_bool_t   *pb_bool;
314     int          *pi_int;
315     int64_t      *pi_64;
316
317     switch( i_query )
318     {
319         /* */
320         case ACCESS_CAN_SEEK:
321         case ACCESS_CAN_FASTSEEK:
322             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
323             *pb_bool = VLC_FALSE;//p_sys->b_seekable;
324             break;
325
326         case ACCESS_CAN_PAUSE:
327             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
328             *pb_bool = VLC_FALSE;
329             break;
330
331         case ACCESS_CAN_CONTROL_PACE:
332             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
333             *pb_bool = VLC_TRUE;//p_sys->b_pace_control;
334             break;
335
336         /* */
337         case ACCESS_GET_MTU:
338             pi_int = (int*)va_arg( args, int * );
339             *pi_int = 0;
340             break;
341
342         case ACCESS_GET_PTS_DELAY:
343             pi_64 = (int64_t*)va_arg( args, int64_t * );
344             *pi_64 = var_GetInteger( p_access, "realrtsp-caching" ) * 1000;
345             break;
346
347         /* */
348         case ACCESS_SET_PAUSE_STATE:
349             /* Nothing to do */
350             break;
351
352         case ACCESS_GET_TITLE_INFO:
353         case ACCESS_SET_TITLE:
354         case ACCESS_SET_SEEKPOINT:
355         case ACCESS_SET_PRIVATE_ID_STATE:
356         case ACCESS_GET_META:
357         case ACCESS_GET_CONTENT_TYPE:
358             return VLC_EGENERIC;
359
360         default:
361             msg_Warn( p_access, "unimplemented query in control" );
362             return VLC_EGENERIC;
363     }
364
365     return VLC_SUCCESS;
366 }