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