Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

access.c

00001 /*****************************************************************************
00002  * access.c: Real rtsp input
00003  *****************************************************************************
00004  * Copyright (C) 2005 VideoLAN
00005  * $Id$
00006  *
00007  * Authors: Gildas Bazin <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <vlc/vlc.h>
00028 #include <vlc/input.h>
00029 
00030 #include "network.h"
00031 #include "rtsp.h"
00032 #include "real.h"
00033 
00034 /*****************************************************************************
00035  * Module descriptor
00036  *****************************************************************************/
00037 static int  Open ( vlc_object_t * );
00038 static void Close( vlc_object_t * );
00039 
00040 #define CACHING_TEXT N_("Caching value (ms)")
00041 #define CACHING_LONGTEXT N_( \
00042     "Allows you to modify the default caching value for RTSP streams. This " \
00043     "value should be set in millisecond units." )
00044 
00045 vlc_module_begin();
00046     set_description( _("Real RTSP") );
00047     set_shortname( _("Real RTSP") );
00048     set_category( CAT_INPUT );
00049     set_subcategory( SUBCAT_INPUT_ACCESS );
00050     add_integer( "realrtsp-caching", 3000, NULL,
00051                  CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
00052     set_capability( "access2", 10 );
00053     set_callbacks( Open, Close );
00054     add_shortcut( "realrtsp" );
00055     add_shortcut( "rtsp" );
00056 vlc_module_end();
00057 
00058 
00059 /*****************************************************************************
00060  * Exported prototypes
00061  *****************************************************************************/
00062 static block_t *BlockRead( access_t * );
00063 static int     Seek( access_t *, int64_t );
00064 static int     Control( access_t *, int, va_list );
00065 
00066 struct access_sys_t
00067 {
00068     vlc_bool_t b_seekable;
00069     vlc_bool_t b_pace_control;
00070 
00071     rtsp_client_t *p_rtsp;
00072 
00073     int fd;
00074 
00075     block_t *p_header;
00076 };
00077 
00078 /*****************************************************************************
00079  * Network wrappers
00080  *****************************************************************************/
00081 static int RtspConnect( void *p_userdata, char *psz_server, int i_port )
00082 {
00083     access_t *p_access = (access_t *)p_userdata;
00084     access_sys_t *p_sys = p_access->p_sys;
00085 
00086     /* Open connection */
00087     p_sys->fd = net_OpenTCP( p_access, psz_server, i_port );
00088     if( p_sys->fd < 0 )
00089     {
00090         msg_Err( p_access, "cannot connect to %s:%d", psz_server, i_port );
00091         return VLC_EGENERIC;
00092     }
00093 
00094     return VLC_SUCCESS;
00095 }
00096 
00097 static int RtspDisconnect( void *p_userdata )
00098 {
00099     access_t *p_access = (access_t *)p_userdata;
00100     access_sys_t *p_sys = p_access->p_sys;
00101 
00102     net_Close( p_sys->fd );
00103     return VLC_SUCCESS;
00104 }
00105 
00106 static int RtspRead( void *p_userdata, uint8_t *p_buffer, int i_buffer )
00107 {
00108     access_t *p_access = (access_t *)p_userdata;
00109     access_sys_t *p_sys = p_access->p_sys;
00110 
00111     return net_Read( p_access, p_sys->fd, 0, p_buffer, i_buffer, VLC_TRUE );
00112 }
00113 
00114 static int RtspReadLine( void *p_userdata, uint8_t *p_buffer, int i_buffer )
00115 {
00116     access_t *p_access = (access_t *)p_userdata;
00117     access_sys_t *p_sys = p_access->p_sys;
00118 
00119     char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, 0 );
00120 
00121     //fprintf(stderr, "ReadLine: %s\n", psz);
00122 
00123     if( psz ) strncpy( (char *)p_buffer, psz, i_buffer );
00124     else *p_buffer = 0;
00125 
00126     if( psz ) free( psz );
00127     return 0;
00128 }
00129 
00130 static int RtspWrite( void *p_userdata, uint8_t *p_buffer, int i_buffer )
00131 {
00132     access_t *p_access = (access_t *)p_userdata;
00133     access_sys_t *p_sys = p_access->p_sys;
00134 
00135     //fprintf(stderr, "Write: %s", p_buffer);
00136 
00137     net_Printf( VLC_OBJECT(p_access), p_sys->fd, 0, "%s", p_buffer );
00138 
00139     return 0;
00140 }
00141 
00142 /*****************************************************************************
00143  * Open: open the rtsp connection
00144  *****************************************************************************/
00145 static int Open( vlc_object_t *p_this )
00146 {
00147     access_t *p_access = (access_t *)p_this;
00148     access_sys_t *p_sys;
00149     char *psz_server = 0;
00150     int i_result;
00151 
00152     if( !p_access->b_force ) return VLC_EGENERIC;
00153 
00154     p_access->pf_read = NULL;
00155     p_access->pf_block = BlockRead;
00156     p_access->pf_seek = Seek;
00157     p_access->pf_control = Control;
00158     p_access->info.i_update = 0;
00159     p_access->info.i_size = 0;
00160     p_access->info.i_pos = 0;
00161     p_access->info.b_eof = VLC_FALSE;
00162     p_access->info.i_title = 0;
00163     p_access->info.i_seekpoint = 0;
00164     p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
00165     p_sys->p_rtsp = malloc( sizeof( rtsp_client_t) );
00166 
00167     p_sys->p_header = 0;
00168     p_sys->p_rtsp->p_userdata = p_access;
00169     p_sys->p_rtsp->pf_connect = RtspConnect;
00170     p_sys->p_rtsp->pf_disconnect = RtspDisconnect;
00171     p_sys->p_rtsp->pf_read = RtspRead;
00172     p_sys->p_rtsp->pf_read_line = RtspReadLine;
00173     p_sys->p_rtsp->pf_write = RtspWrite;
00174 
00175     i_result = rtsp_connect( p_sys->p_rtsp, p_access->psz_path, 0 );
00176     if( i_result )
00177     {
00178         msg_Dbg( p_access, "could not connect to: %s", p_access->psz_path );
00179         free( p_sys->p_rtsp );
00180         p_sys->p_rtsp = 0;
00181         goto error;
00182     }
00183 
00184     msg_Dbg( p_access, "rtsp connected" );
00185 
00186     /* looking for server type */
00187     if( rtsp_search_answers( p_sys->p_rtsp, "Server" ) )
00188         psz_server = strdup( rtsp_search_answers( p_sys->p_rtsp, "Server" ) );
00189     else
00190     {
00191         if( rtsp_search_answers( p_sys->p_rtsp, "RealChallenge1" ) )
00192             psz_server = strdup("Real");
00193         else
00194             psz_server = strdup("unknown");
00195     }
00196 
00197     if( strstr( psz_server, "Real" ) || strstr( psz_server, "Helix" ) )
00198     {
00199         uint32_t bandwidth = 10485800;
00200         rmff_header_t *h;
00201 
00202         msg_Dbg( p_access, "found a real/helix rtsp server" );
00203 
00204         if( !(h = real_setup_and_get_header( p_sys->p_rtsp, bandwidth )) )
00205         {
00206             /* Check if we got a redirect */
00207             if( rtsp_search_answers( p_sys->p_rtsp, "Location" ) )
00208             {
00209                 msg_Dbg( p_access, "redirect: %s",
00210                          rtsp_search_answers(p_sys->p_rtsp, "Location") );
00211                 msg_Warn( p_access, "redirect not supported" );
00212                 goto error;
00213             }
00214 
00215 
00216             msg_Err( p_access, "rtsp session can not be established" );
00217             goto error;
00218         }
00219 
00220         p_sys->p_header = block_New( p_access, 4096 );
00221         p_sys->p_header->i_buffer =
00222             rmff_dump_header( h, p_sys->p_header->p_buffer, 1024 );
00223         rmff_free_header( h );
00224     }
00225     else
00226     {
00227         msg_Dbg( p_access, "only real/helix rtsp servers supported for now" );
00228         goto error;
00229     }
00230 
00231     /* Update default_pts to a suitable value for file access */
00232     var_Create( p_access, "realrtsp-caching",
00233                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
00234 
00235     if( psz_server ) free( psz_server );
00236     return VLC_SUCCESS;
00237 
00238  error:
00239     if( psz_server ) free( psz_server );
00240     Close( p_this );
00241     return VLC_EGENERIC;
00242 }
00243 
00244 /*****************************************************************************
00245  * Close: close the target
00246  *****************************************************************************/
00247 static void Close( vlc_object_t * p_this )
00248 {
00249     access_t     *p_access = (access_t*)p_this;
00250     access_sys_t *p_sys = p_access->p_sys;
00251 
00252     if( p_sys->p_rtsp ) rtsp_close( p_sys->p_rtsp );
00253     if( p_sys->p_rtsp ) free( p_sys->p_rtsp );
00254     free( p_sys );
00255 }
00256 
00257 /*****************************************************************************
00258  * Read: standard read on a file descriptor.
00259  *****************************************************************************/
00260 static block_t *BlockRead( access_t *p_access )
00261 {
00262     access_sys_t *p_sys = p_access->p_sys;
00263     block_t *p_block;
00264     rmff_pheader_t pheader;
00265     int i_size;
00266 
00267     if( p_sys->p_header )
00268     {
00269         p_block = p_sys->p_header;
00270         p_sys->p_header = 0;
00271         return p_block;
00272     }
00273 
00274     i_size = real_get_rdt_chunk_header( p_access->p_sys->p_rtsp, &pheader );
00275     if( i_size <= 0 ) return 0;
00276 
00277     p_block = block_New( p_access, i_size );
00278     p_block->i_buffer = real_get_rdt_chunk( p_access->p_sys->p_rtsp, &pheader,
00279                                             &p_block->p_buffer );
00280 
00281     return p_block;
00282 }
00283 
00284 /*****************************************************************************
00285  * Seek: seek to a specific location in a file
00286  *****************************************************************************/
00287 static int Seek( access_t *p_access, int64_t i_pos )
00288 {
00289     return VLC_SUCCESS;
00290 }
00291 
00292 /*****************************************************************************
00293  * Control:
00294  *****************************************************************************/
00295 static int Control( access_t *p_access, int i_query, va_list args )
00296 {
00297     vlc_bool_t   *pb_bool;
00298     int          *pi_int;
00299     int64_t      *pi_64;
00300 
00301     switch( i_query )
00302     {
00303         /* */
00304         case ACCESS_CAN_SEEK:
00305         case ACCESS_CAN_FASTSEEK:
00306             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
00307             *pb_bool = VLC_FALSE;//p_sys->b_seekable;
00308             break;
00309 
00310         case ACCESS_CAN_PAUSE:
00311             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
00312             *pb_bool = VLC_FALSE;
00313             break;
00314 
00315         case ACCESS_CAN_CONTROL_PACE:
00316             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
00317             *pb_bool = VLC_TRUE;//p_sys->b_pace_control;
00318             break;
00319 
00320         /* */
00321         case ACCESS_GET_MTU:
00322             pi_int = (int*)va_arg( args, int * );
00323             *pi_int = 0;
00324             break;
00325 
00326         case ACCESS_GET_PTS_DELAY:
00327             pi_64 = (int64_t*)va_arg( args, int64_t * );
00328             *pi_64 = var_GetInteger( p_access, "realrtsp-caching" ) * 1000;
00329             break;
00330 
00331         /* */
00332         case ACCESS_SET_PAUSE_STATE:
00333             /* Nothing to do */
00334             break;
00335 
00336         case ACCESS_GET_TITLE_INFO:
00337         case ACCESS_SET_TITLE:
00338         case ACCESS_SET_SEEKPOINT:
00339         case ACCESS_SET_PRIVATE_ID_STATE:
00340         case ACCESS_GET_META:
00341             return VLC_EGENERIC;
00342 
00343         default:
00344             msg_Warn( p_access, "unimplemented query in control" );
00345             return VLC_EGENERIC;
00346     }
00347 
00348     return VLC_SUCCESS;
00349 }

Generated on Tue Dec 20 10:14:23 2005 for vlc-0.8.4a by  doxygen 1.4.2