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