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