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