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