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

rtsp.c

00001 /*****************************************************************************
00002  * rtsp.c: rtsp VoD server module
00003  *****************************************************************************
00004  * Copyright (C) 2003-2004 the VideoLAN team
00005  * $Id: rtsp.c 13526 2005-12-04 12:57:04Z jpsaman $
00006  *
00007  * Authors: Laurent Aimar <[email protected]>
00008  *          Gildas Bazin <[email protected]>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 /*****************************************************************************
00026  * Preamble
00027  *****************************************************************************/
00028 #include <stdlib.h>
00029 
00030 #include <errno.h>
00031 
00032 #include <vlc/vlc.h>
00033 #include <vlc/input.h>
00034 #include <vlc/sout.h>
00035 
00036 #include "vlc_httpd.h"
00037 #include "vlc_vod.h"
00038 #include "network.h"
00039 
00040 /*****************************************************************************
00041  * Module descriptor
00042  *****************************************************************************/
00043 static int  Open ( vlc_object_t * );
00044 static void Close( vlc_object_t * );
00045 
00046 #define HOST_TEXT N_( "Host address" )
00047 #define HOST_LONGTEXT N_( \
00048     "You can set the address, port and path the rtsp interface will bind to." \
00049     "\n Syntax is address:port/path. Default is to bind to any address "\
00050     "on port 554, with no path." )
00051 vlc_module_begin();
00052     set_shortname( _("RTSP VoD" ) );
00053     set_description( _("RTSP VoD server") );
00054     set_category( CAT_SOUT );
00055     set_subcategory( SUBCAT_SOUT_VOD );
00056     set_capability( "vod server", 1 );
00057     set_callbacks( Open, Close );
00058     add_shortcut( "rtsp" );
00059     add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
00060 vlc_module_end();
00061 
00062 /*****************************************************************************
00063  * Exported prototypes
00064  *****************************************************************************/
00065 
00066 typedef struct media_es_t media_es_t;
00067 
00068 typedef struct
00069 {
00070     media_es_t *p_media_es;
00071     char *psz_ip;
00072     int i_port;
00073 
00074 } rtsp_client_es_t;
00075 
00076 typedef struct
00077 {
00078     char *psz_session;
00079     int64_t i_last; /* for timeout */
00080 
00081     vlc_bool_t b_playing; /* is it in "play" state */
00082     vlc_bool_t b_paused; /* is it in "pause" state */
00083 
00084     int i_es;
00085     rtsp_client_es_t **es;
00086 
00087 } rtsp_client_t;
00088 
00089 struct media_es_t
00090 {
00091     /* VoD server */
00092     vod_t *p_vod;
00093 
00094     /* RTSP server */
00095     httpd_url_t *p_rtsp_url;
00096 
00097     vod_media_t *p_media;
00098 
00099     es_format_t fmt;
00100     int         i_port;
00101     uint8_t     i_payload_type;
00102     char        *psz_rtpmap;
00103     char        *psz_fmtp;
00104 
00105 };
00106 
00107 struct vod_media_t
00108 {
00109     /* VoD server */
00110     vod_t *p_vod;
00111 
00112     /* RTSP server */
00113     httpd_url_t  *p_rtsp_url;
00114     char         *psz_rtsp_control;
00115     char         *psz_rtsp_path;
00116 
00117     int  i_port;
00118     int  i_port_audio;
00119     int  i_port_video;
00120     int  i_ttl;
00121     int  i_payload_type;
00122 
00123     int64_t i_sdp_id;
00124     int     i_sdp_version;
00125 
00126     vlc_bool_t b_multicast;
00127 
00128     vlc_mutex_t lock;
00129 
00130     /* ES list */
00131     int        i_es;
00132     media_es_t **es;
00133     char       *psz_mux;
00134 
00135     /* RTSP client */
00136     int           i_rtsp;
00137     rtsp_client_t **rtsp;
00138 
00139     /* Infos */
00140     char *psz_session_name;
00141     char *psz_session_description;
00142     char *psz_session_url;
00143     char *psz_session_email;
00144     mtime_t i_length;
00145 };
00146 
00147 struct vod_sys_t
00148 {
00149     /* RTSP server */
00150     httpd_host_t *p_rtsp_host;
00151     char *psz_host;
00152     char *psz_path;
00153     int i_port;
00154 
00155     /* List of media */
00156     int i_media;
00157     vod_media_t **media;
00158 };
00159 
00160 static vod_media_t *MediaNew( vod_t *, char *, input_item_t * );
00161 static void         MediaDel( vod_t *, vod_media_t * );
00162 static int          MediaAddES( vod_t *, vod_media_t *, es_format_t * );
00163 static void         MediaDelES( vod_t *, vod_media_t *, es_format_t * );
00164 
00165 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
00166 static rtsp_client_t *RtspClientGet( vod_media_t *, char * );
00167 static void           RtspClientDel( vod_media_t *, rtsp_client_t * );
00168 
00169 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
00170                          httpd_message_t *, httpd_message_t * );
00171 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
00172                            httpd_message_t *, httpd_message_t * );
00173 
00174 static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
00175 
00176 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
00177 {
00178     static const char hex[16] = "0123456789abcdef";
00179     int i;
00180 
00181     for( i = 0; i < i_data; i++ )
00182     {
00183         s[2*i+0] = hex[(p_data[i]>>4)&0xf];
00184         s[2*i+1] = hex[(p_data[i]   )&0xf];
00185     }
00186     s[2*i_data] = '\0';
00187 }
00188 
00189 /*****************************************************************************
00190  * Open: Starts the RTSP server module
00191  *****************************************************************************/
00192 static int Open( vlc_object_t *p_this )
00193 {
00194     vod_t *p_vod = (vod_t *)p_this;
00195     vod_sys_t *p_sys = 0;
00196     char *psz_url = 0;
00197     vlc_url_t url;
00198 
00199     psz_url = config_GetPsz( p_vod, "rtsp-host" );
00200     vlc_UrlParse( &url, psz_url, 0 );
00201     if( psz_url ) free( psz_url );
00202 
00203     if( url.i_port <= 0 ) url.i_port = 554;
00204 
00205     p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
00206     if( !p_sys ) goto error;
00207     p_sys->p_rtsp_host = 0;
00208 
00209     p_sys->p_rtsp_host =
00210         httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
00211     if( !p_sys->p_rtsp_host )
00212     {
00213         msg_Err( p_vod, "cannot create http server (%s:%i)",
00214                  url.psz_host, url.i_port );
00215         goto error;
00216     }
00217 
00218     p_sys->psz_host = strdup( url.psz_host ? url.psz_host : "0.0.0.0" );
00219     p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
00220     p_sys->i_port = url.i_port;
00221 
00222     vlc_UrlClean( &url );
00223     p_sys->media = 0;
00224     p_sys->i_media = 0;
00225 
00226     p_vod->pf_media_new = MediaNew;
00227     p_vod->pf_media_del = MediaDel;
00228     p_vod->pf_media_add_es = MediaAddES;
00229     p_vod->pf_media_del_es = MediaDelES;
00230 
00231     return VLC_SUCCESS;
00232 
00233  error:
00234 
00235     if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
00236     if( p_sys ) free( p_sys );
00237     vlc_UrlClean( &url );
00238     return VLC_EGENERIC;
00239 }
00240 
00241 /*****************************************************************************
00242  * Close:
00243  *****************************************************************************/
00244 static void Close( vlc_object_t * p_this )
00245 {
00246     vod_t *p_vod = (vod_t *)p_this;
00247     vod_sys_t *p_sys = p_vod->p_sys;
00248 
00249     httpd_HostDelete( p_sys->p_rtsp_host );
00250 
00251     /* TODO delete medias */
00252 
00253     free( p_sys->psz_host );
00254     free( p_sys->psz_path );
00255     free( p_sys );
00256 }
00257 
00258 /*****************************************************************************
00259  * Media handling
00260  *****************************************************************************/
00261 static vod_media_t *MediaNew( vod_t *p_vod, char *psz_name,
00262                               input_item_t *p_item )
00263 {
00264     vod_sys_t *p_sys = p_vod->p_sys;
00265     vod_media_t *p_media = malloc( sizeof(vod_media_t) );
00266     int i;
00267 
00268     memset( p_media, 0, sizeof(vod_media_t) );
00269     p_media->es = 0;
00270     p_media->psz_mux = 0;
00271     p_media->rtsp = 0;
00272 
00273     asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
00274     p_media->p_rtsp_url =
00275         httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
00276                             NULL, NULL );
00277 
00278     if( !p_media->p_rtsp_url )
00279     {
00280         msg_Err( p_vod, "cannot create http url (%s)", p_media->psz_rtsp_path);
00281         free( p_media->psz_rtsp_path );
00282         free( p_media );
00283         return 0;
00284     }
00285 
00286     msg_Dbg( p_vod, "created rtsp url: %s", p_media->psz_rtsp_path );
00287 
00288     asprintf( &p_media->psz_rtsp_control, "rtsp://%s:%d%s",
00289               p_sys->psz_host, p_sys->i_port, p_media->psz_rtsp_path );
00290 
00291     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
00292                     RtspCallback, (void*)p_media );
00293     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
00294                     RtspCallback, (void*)p_media );
00295     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
00296                     RtspCallback, (void*)p_media );
00297     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
00298                     RtspCallback, (void*)p_media );
00299 
00300     p_media->p_vod = p_vod;
00301 
00302     TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
00303 
00304     vlc_mutex_init( p_vod, &p_media->lock );
00305     p_media->psz_session_name = strdup("");
00306     p_media->psz_session_description = strdup("");
00307     p_media->psz_session_url = strdup("");
00308     p_media->psz_session_email = strdup("");
00309 
00310     p_media->i_port_audio = 1234;
00311     p_media->i_port_video = 1236;
00312     p_media->i_port       = 1238;
00313     p_media->i_payload_type = 96;
00314 
00315     p_media->i_sdp_id = mdate();
00316     p_media->i_sdp_version = 1;
00317     p_media->i_length = p_item->i_duration;
00318 
00319     vlc_mutex_lock( &p_item->lock );
00320     msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
00321     for( i = 0; i < p_item->i_es; i++ )
00322     {
00323         MediaAddES( p_vod, p_media, p_item->es[i] );
00324     }
00325     vlc_mutex_unlock( &p_item->lock );
00326 
00327     return p_media;
00328 }
00329 
00330 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
00331 {
00332     vod_sys_t *p_sys = p_vod->p_sys;
00333 
00334     msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
00335 
00336     while( p_media->i_rtsp > 0 ) RtspClientDel( p_media, p_media->rtsp[0] );
00337     httpd_UrlDelete( p_media->p_rtsp_url );
00338     if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
00339     if( p_media->psz_rtsp_control ) free( p_media->psz_rtsp_control );
00340 
00341     TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
00342 
00343     while( p_media->i_es ) MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
00344 
00345     vlc_mutex_destroy( &p_media->lock );
00346     free( p_media->psz_session_name );
00347     free( p_media->psz_session_description );
00348     free( p_media->psz_session_url );
00349     free( p_media->psz_session_email );
00350     free( p_media );
00351 }
00352 
00353 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
00354 {
00355     media_es_t *p_es = malloc( sizeof(media_es_t) );
00356     char *psz_urlc;
00357 
00358     memset( p_es, 0, sizeof(media_es_t) );
00359     p_media->psz_mux = NULL;
00360 
00361     /* TODO: update SDP, etc... */
00362     asprintf( &psz_urlc, "%s/trackid=%d",
00363               p_media->psz_rtsp_path, p_media->i_es );
00364     msg_Dbg( p_vod, "  - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
00365 
00366     switch( p_fmt->i_codec )
00367     {
00368     case VLC_FOURCC( 's', '1', '6', 'b' ):
00369         if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
00370         {
00371             p_es->i_payload_type = 11;
00372         }
00373         else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
00374         {
00375             p_es->i_payload_type = 10;
00376         }
00377         else
00378         {
00379             p_es->i_payload_type = p_media->i_payload_type++;
00380         }
00381 
00382         p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
00383         sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
00384                  p_fmt->audio.i_channels );
00385         break;
00386     case VLC_FOURCC( 'u', '8', ' ', ' ' ):
00387         p_es->i_payload_type = p_media->i_payload_type++;
00388         p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
00389         sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
00390                  p_fmt->audio.i_channels );
00391         break;
00392     case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
00393         p_es->i_payload_type = 14;
00394         p_es->psz_rtpmap = strdup( "MPA/90000" );
00395         break;
00396     case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
00397         p_es->i_payload_type = 32;
00398         p_es->psz_rtpmap = strdup( "MPV/90000" );
00399         break;
00400     case VLC_FOURCC( 'a', '5', '2', ' ' ):
00401         p_es->i_payload_type = p_media->i_payload_type++;
00402         p_es->psz_rtpmap = strdup( "ac3/90000" );
00403         break;
00404     case VLC_FOURCC( 'H', '2', '6', '3' ):
00405         p_es->i_payload_type = p_media->i_payload_type++;
00406         p_es->psz_rtpmap = strdup( "H263-1998/90000" );
00407         break;
00408     case VLC_FOURCC( 'm', 'p', '4', 'v' ):
00409         p_es->i_payload_type = p_media->i_payload_type++;
00410         p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
00411         if( p_fmt->i_extra > 0 )
00412         {
00413             char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
00414             p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
00415             sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
00416             sprintf( p_es->psz_fmtp,
00417                      "profile-level-id=3; config=%s;", p_hexa );
00418             free( p_hexa );
00419         }
00420         break;
00421     case VLC_FOURCC( 'm', 'p', '4', 'a' ):
00422         p_es->i_payload_type = p_media->i_payload_type++;
00423         p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
00424         sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
00425         if( p_fmt->i_extra > 0 )
00426         {
00427             char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
00428             p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
00429             sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
00430             sprintf( p_es->psz_fmtp,
00431                      "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
00432                      "config=%s; SizeLength=13;IndexLength=3; "
00433                      "IndexDeltaLength=3; Profile=1;", p_hexa );
00434             free( p_hexa );
00435         }
00436         break;
00437     case VLC_FOURCC( 'm', 'p', '2', 't' ):
00438         p_media->psz_mux = "ts";
00439         p_es->i_payload_type = 33;
00440         p_es->psz_rtpmap = strdup( "MP2T/90000" );
00441         break;
00442     case VLC_FOURCC( 'm', 'p', '2', 'p' ):
00443         p_media->psz_mux = "ps";
00444         p_es->i_payload_type = p_media->i_payload_type++;
00445         p_es->psz_rtpmap = strdup( "MP2P/90000" );
00446         break;
00447     case VLC_FOURCC( 's', 'a', 'm', 'r' ):
00448         p_es->i_payload_type = p_media->i_payload_type++;
00449         p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
00450                                    "AMR/8000/2" : "AMR/8000" );
00451         p_es->psz_fmtp = strdup( "octet-align=1" );
00452         break; 
00453     case VLC_FOURCC( 's', 'a', 'w', 'b' ):
00454         p_es->i_payload_type = p_media->i_payload_type++;
00455         p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
00456                                    "AMR-WB/16000/2" : "AMR-WB/16000" );
00457         p_es->psz_fmtp = strdup( "octet-align=1" );
00458         break; 
00459 
00460     default:
00461         msg_Err( p_vod, "cannot add this stream (unsupported "
00462                  "codec: %4.4s)", (char*)&p_fmt->i_codec );
00463         free( p_es );
00464         return VLC_EGENERIC;
00465     }
00466 
00467     p_es->p_rtsp_url =
00468         httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
00469                             NULL );
00470 
00471     if( !p_es->p_rtsp_url )
00472     {
00473         msg_Err( p_vod, "cannot create http url (%s)", psz_urlc );
00474         free( psz_urlc );
00475         free( p_es );
00476         return VLC_EGENERIC;
00477     }
00478     free( psz_urlc );
00479 
00480     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
00481                     RtspCallbackES, (void*)p_es );
00482     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
00483                     RtspCallbackES, (void*)p_es );
00484     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
00485                     RtspCallbackES, (void*)p_es );
00486     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
00487                     RtspCallbackES, (void*)p_es );
00488 
00489     es_format_Copy( &p_es->fmt, p_fmt );
00490     p_es->p_vod = p_vod;
00491     p_es->p_media = p_media;
00492 
00493 #if 0
00494     /* Choose the port */
00495     if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
00496     {
00497         p_es->i_port = p_media->i_port_audio;
00498         p_media->i_port_audio = 0;
00499     }
00500     else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
00501     {
00502         p_es->i_port = p_media->i_port_video;
00503         p_media->i_port_video = 0;
00504     }
00505     while( !p_es->i_port )
00506     {
00507         if( p_media->i_port != p_media->i_port_audio &&
00508             p_media->i_port != p_media->i_port_video )
00509         {
00510             p_es->i_port = p_media->i_port;
00511             p_media->i_port += 2;
00512             break;
00513         }
00514         p_media->i_port += 2;
00515     }
00516 #else
00517 
00518     p_es->i_port = 0;
00519 #endif
00520 
00521     vlc_mutex_lock( &p_media->lock );
00522     TAB_APPEND( p_media->i_es, p_media->es, p_es );
00523     vlc_mutex_unlock( &p_media->lock );
00524 
00525     p_media->i_sdp_version++;
00526 
00527     return VLC_SUCCESS;
00528 }
00529 
00530 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
00531 {
00532     media_es_t *p_es = 0;
00533     int i;
00534 
00535     /* Find the ES */
00536     for( i = 0; i < p_media->i_es; i++ )
00537     {
00538         if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
00539             p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
00540             p_media->es[i]->fmt.i_id == p_fmt->i_id )
00541         {
00542             p_es = p_media->es[i];
00543         }
00544     }
00545     if( !p_es ) return;
00546 
00547     msg_Dbg( p_vod, "  - Removing ES %4.4s", (char *)&p_fmt->i_codec );
00548 
00549     vlc_mutex_lock( &p_media->lock );
00550     TAB_REMOVE( p_media->i_es, p_media->es, p_es );
00551     vlc_mutex_unlock( &p_media->lock );
00552 
00553     if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
00554     if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
00555     p_media->i_sdp_version++;
00556 
00557     if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
00558     es_format_Clean( &p_es->fmt );
00559 }
00560 
00561 /****************************************************************************
00562  * RTSP server implementation
00563  ****************************************************************************/
00564 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
00565 {
00566     rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
00567     memset( p_rtsp, 0, sizeof(rtsp_client_t) );
00568     p_rtsp->es = 0;
00569 
00570     p_rtsp->psz_session = psz_session;
00571     TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
00572 
00573     msg_Dbg( p_media->p_vod, "new session: %s", psz_session );
00574 
00575     return p_rtsp;
00576 }
00577 
00578 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
00579 {
00580     int i;
00581 
00582     for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
00583     {
00584         if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
00585         {
00586             return p_media->rtsp[i];
00587         }
00588     }
00589 
00590     return NULL;
00591 }
00592 
00593 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
00594 {
00595     msg_Dbg( p_media->p_vod, "closing session: %s", p_rtsp->psz_session );
00596 
00597     while( p_rtsp->i_es-- )
00598     {
00599         if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
00600             free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
00601         free( p_rtsp->es[p_rtsp->i_es] );
00602         if( !p_rtsp->i_es ) free( p_rtsp->es );
00603     }
00604 
00605     TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
00606 
00607     free( p_rtsp->psz_session );
00608     free( p_rtsp );
00609 }
00610 
00611 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
00612                          httpd_message_t *answer, httpd_message_t *query )
00613 {
00614     vod_media_t *p_media = (vod_media_t*)p_args;
00615     vod_t *p_vod = p_media->p_vod;
00616     char *psz_session = NULL;
00617     rtsp_client_t *p_rtsp;
00618 
00619     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
00620 
00621     fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
00622 
00623     answer->i_proto   = HTTPD_PROTO_RTSP;
00624     answer->i_version = query->i_version;
00625     answer->i_type    = HTTPD_MSG_ANSWER;
00626 
00627     switch( query->i_type )
00628     {
00629         case HTTPD_MSG_DESCRIBE:
00630         {
00631             char *psz_sdp =
00632                 SDPGenerate( p_media, cl );
00633 
00634             if( psz_sdp != NULL )
00635             {
00636                 answer->i_status = 200;
00637                 answer->psz_status = strdup( "OK" );
00638                 httpd_MsgAdd( answer, "Content-type",  "%s", "application/sdp" );
00639     
00640                 answer->p_body = (uint8_t *)psz_sdp;
00641                 answer->i_body = strlen( psz_sdp );
00642             }
00643             else
00644             {
00645                 answer->i_status = 500;
00646                 answer->psz_status = strdup( "Internal server error" );
00647                 answer->p_body = NULL;
00648                 answer->i_body = 0;
00649             }
00650             break;
00651         }
00652 
00653         case HTTPD_MSG_PLAY:
00654         {
00655             char *psz_output, ip[NI_MAXNUMERICHOST];
00656             int i, i_port_audio = 0, i_port_video = 0;
00657 
00658             /* for now only multicast so easy */
00659             answer->i_status = 200;
00660             answer->psz_status = strdup( "OK" );
00661             answer->i_body = 0;
00662             answer->p_body = NULL;
00663 
00664             psz_session = httpd_MsgGet( query, "Session" );
00665             msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
00666 
00667             p_rtsp = RtspClientGet( p_media, psz_session );
00668             if( !p_rtsp ) break;
00669 
00670             if( p_rtsp->b_playing && p_rtsp->b_paused )
00671             {
00672                 vod_MediaControl( p_vod, p_media, psz_session,
00673                                   VOD_MEDIA_PAUSE );
00674                 p_rtsp->b_paused = VLC_FALSE;
00675                 break;
00676             }
00677             else if( p_rtsp->b_playing ) break;
00678 
00679             if( httpd_ClientIP( cl, ip ) == NULL ) break;
00680 
00681             p_rtsp->b_playing = VLC_TRUE;
00682 
00683             /* FIXME for != 1 video and 1 audio */
00684             for( i = 0; i < p_rtsp->i_es; i++ )
00685             {
00686                 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
00687                     i_port_audio = p_rtsp->es[i]->i_port;
00688                 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
00689                     i_port_video = p_rtsp->es[i]->i_port;
00690             }
00691 
00692             if( p_media->psz_mux )
00693             {
00694                 asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
00695                           ip, i_port_video, p_media->psz_mux );
00696             }
00697             else
00698             {
00699                 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
00700                           "port-audio=%i}", ip, i_port_video, i_port_audio );
00701             }
00702 
00703             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
00704                               psz_output );
00705             free( psz_output );
00706             break;
00707         }
00708 
00709         case HTTPD_MSG_PAUSE:
00710             psz_session = httpd_MsgGet( query, "Session" );
00711             msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
00712 
00713             p_rtsp = RtspClientGet( p_media, psz_session );
00714             if( !p_rtsp ) break;
00715 
00716             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
00717             p_rtsp->b_paused = VLC_TRUE;
00718 
00719             answer->i_status = 200;
00720             answer->psz_status = strdup( "OK" );
00721             answer->i_body = 0;
00722             answer->p_body = NULL;
00723             break;
00724 
00725         case HTTPD_MSG_TEARDOWN:
00726             /* for now only multicast so easy again */
00727             answer->i_status = 200;
00728             answer->psz_status = strdup( "OK" );
00729             answer->i_body = 0;
00730             answer->p_body = NULL;
00731 
00732             psz_session = httpd_MsgGet( query, "Session" );
00733             msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
00734 
00735             p_rtsp = RtspClientGet( p_media, psz_session );
00736             if( !p_rtsp ) break;
00737 
00738             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
00739             RtspClientDel( p_media, p_rtsp );
00740             break;
00741 
00742         default:
00743             return VLC_EGENERIC;
00744     }
00745 
00746     httpd_MsgAdd( answer, "Server", "VLC Server" );
00747     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
00748     httpd_MsgAdd( answer, "Cseq", "%d",
00749                   atoi( httpd_MsgGet( query, "Cseq" ) ) );
00750     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
00751 
00752     if( psz_session )
00753     {
00754         httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
00755     }
00756 
00757     return VLC_SUCCESS;
00758 }
00759 
00760 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
00761                            httpd_message_t *answer, httpd_message_t *query )
00762 {
00763     media_es_t *p_es = (media_es_t*)p_args;
00764     vod_media_t *p_media = p_es->p_media;
00765     vod_t *p_vod = p_media->p_vod;
00766     rtsp_client_t *p_rtsp = NULL;
00767     char *psz_session = NULL;
00768     char *psz_transport = NULL;
00769     char *psz_position = NULL;
00770     int i;
00771 
00772     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
00773 
00774     fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
00775 
00776     answer->i_proto   = HTTPD_PROTO_RTSP;
00777     answer->i_version = query->i_version;
00778     answer->i_type    = HTTPD_MSG_ANSWER;
00779 
00780     switch( query->i_type )
00781     {
00782     case HTTPD_MSG_SETUP:
00783         psz_transport = httpd_MsgGet( query, "Transport" );
00784         fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
00785 
00786         if( strstr( psz_transport, "unicast" ) &&
00787             strstr( psz_transport, "client_port=" ) )
00788         {
00789             rtsp_client_t *p_rtsp;
00790             rtsp_client_es_t *p_rtsp_es;
00791             char ip[NI_MAXNUMERICHOST];
00792             int i_port = atoi( strstr( psz_transport, "client_port=" ) +
00793                                strlen("client_port=") );
00794 
00795             if( httpd_ClientIP( cl, ip ) == NULL )
00796             {
00797                 answer->i_status = 500;
00798                 answer->psz_status = strdup( "Internal server error" );
00799                 answer->i_body = 0;
00800                 answer->p_body = NULL;
00801                 break;
00802             }
00803 
00804             fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n",
00805                      ip, i_port );
00806 
00807             psz_session = httpd_MsgGet( query, "Session" );
00808             if( !psz_session || !*psz_session )
00809             {
00810                 asprintf( &psz_session, "%d", rand() );
00811                 p_rtsp = RtspClientNew( p_media, psz_session );
00812             }
00813             else
00814             {
00815                 p_rtsp = RtspClientGet( p_media, psz_session );
00816                 if( !p_rtsp )
00817                 {
00818                     /* FIXME right error code */
00819                     answer->i_status = 454;
00820                     answer->psz_status = strdup( "Unknown session id" );
00821                     answer->i_body = 0;
00822                     answer->p_body = NULL;
00823                     free( ip );
00824                     break;
00825                 }
00826             }
00827 
00828             p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
00829             p_rtsp_es->i_port = i_port;
00830             p_rtsp_es->psz_ip = strdup( ip );
00831             p_rtsp_es->p_media_es = p_es;
00832             TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
00833 
00834             answer->i_status = 200;
00835             answer->psz_status = strdup( "OK" );
00836             answer->i_body = 0;
00837             answer->p_body = NULL;
00838 
00839             httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
00840                           i_port, i_port + 1 );
00841         }
00842         else /* TODO  strstr( psz_transport, "interleaved" ) ) */
00843         {
00844             answer->i_status = 461;
00845             answer->psz_status = strdup( "Unsupported Transport" );
00846             answer->i_body = 0;
00847             answer->p_body = NULL;
00848         }
00849         break;
00850 
00851         case HTTPD_MSG_TEARDOWN:
00852             answer->i_status = 200;
00853             answer->psz_status = strdup( "OK" );
00854             answer->i_body = 0;
00855             answer->p_body = NULL;
00856 
00857             psz_session = httpd_MsgGet( query, "Session" );
00858             msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
00859 
00860             p_rtsp = RtspClientGet( p_media, psz_session );
00861             if( !p_rtsp ) break;
00862 
00863             for( i = 0; i < p_rtsp->i_es; i++ )
00864             {
00865                 if( p_rtsp->es[i]->p_media_es == p_es )
00866                 {
00867                     if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
00868                     TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
00869                     break;
00870                 }
00871             }
00872 
00873             if( !p_rtsp->i_es )
00874             {
00875                 vod_MediaControl( p_vod, p_media, psz_session,
00876                                   VOD_MEDIA_STOP );
00877                 RtspClientDel( p_media, p_rtsp );
00878             }
00879             break;
00880 
00881         case HTTPD_MSG_PLAY:
00882             /* This is kind of a kludge. Should we only support Aggregate
00883              * Operations ? */
00884             psz_session = httpd_MsgGet( query, "Session" );
00885             msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
00886 
00887             p_rtsp = RtspClientGet( p_media, psz_session );
00888 
00889             psz_position = httpd_MsgGet( query, "Range" );
00890             if( psz_position ) psz_position = strstr( psz_position, "npt=" );
00891             if( psz_position )
00892             {
00893                 float f_pos;
00894 
00895                 msg_Dbg( p_vod, "seeking request: %s", psz_position );
00896 
00897                 psz_position += 4;
00898                 if( sscanf( psz_position, "%f", &f_pos ) == 1 )
00899                 {
00900                     f_pos /= ((float)(p_media->i_length/1000))/1000 / 100;
00901                     vod_MediaControl( p_vod, p_media, psz_session,
00902                                       VOD_MEDIA_SEEK, (double)f_pos );
00903                 }
00904             }
00905 
00906             answer->i_status = 200;
00907             answer->psz_status = strdup( "OK" );
00908             answer->i_body = 0;
00909             answer->p_body = NULL;
00910             break;
00911 
00912         case HTTPD_MSG_PAUSE:
00913             /* This is kind of a kludge. Should we only support Aggregate
00914              * Operations ? */
00915             psz_session = httpd_MsgGet( query, "Session" );
00916             msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
00917 
00918             p_rtsp = RtspClientGet( p_media, psz_session );
00919             if( !p_rtsp ) break;
00920 
00921             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
00922             p_rtsp->b_paused = VLC_TRUE;
00923 
00924             answer->i_status = 200;
00925             answer->psz_status = strdup( "OK" );
00926             answer->i_body = 0;
00927             answer->p_body = NULL;
00928             break;
00929 
00930         default:
00931             return VLC_EGENERIC;
00932             break;
00933     }
00934 
00935     httpd_MsgAdd( answer, "Server", "VLC Server" );
00936     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
00937     httpd_MsgAdd( answer, "Cseq", "%d",
00938                   atoi( httpd_MsgGet( query, "Cseq" ) ) );
00939     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
00940 
00941     if( psz_session )
00942     {
00943         httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
00944     }
00945 
00946     return VLC_SUCCESS;
00947 }
00948 
00949 /*****************************************************************************
00950  * SDPGenerate: TODO
00951  * FIXME: need to be moved to a common place ?
00952  *****************************************************************************/
00953 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
00954 {
00955     int i, i_size;
00956     char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
00957 
00958     if( httpd_ServerIP( cl, ip ) == NULL )
00959         return NULL;
00960 
00961     p = strchr( ip, '%' );
00962     if( p != NULL )
00963         *p = '\0'; /* remove scope if present */
00964 
00965     ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
00966 
00967     /* Calculate size */
00968     i_size = sizeof( "v=0\r\n" ) +
00969         sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
00970         sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
00971         sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
00972         sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
00973         sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
00974         sizeof( "t=0 0\r\n" ) + /* FIXME */
00975         sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
00976         sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
00977         sizeof( "a=range:npt=0-1000000000.000\r\n" );
00978 
00979     for( i = 0; i < p_media->i_es; i++ )
00980     {
00981         media_es_t *p_es = p_media->es[i];
00982 
00983         i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
00984         if( p_es->psz_rtpmap )
00985         {
00986             i_size += sizeof( "a=rtpmap:* *\r\n" ) +
00987                 strlen( p_es->psz_rtpmap ) + 9;
00988         }
00989         if( p_es->psz_fmtp )
00990         {
00991             i_size += sizeof( "a=fmtp:* *\r\n" ) +
00992                 strlen( p_es->psz_fmtp ) + 9;
00993         }
00994 
00995         i_size += sizeof( "a=control:*/trackid=*\r\n" ) +
00996             strlen( p_media->psz_rtsp_control ) + 9;
00997     }
00998 
00999     p = psz_sdp = malloc( i_size );
01000     p += sprintf( p, "v=0\r\n" );
01001     p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
01002                   p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
01003     if( *p_media->psz_session_name )
01004         p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
01005     if( *p_media->psz_session_description )
01006         p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
01007     if( *p_media->psz_session_url )
01008         p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
01009     if( *p_media->psz_session_email )
01010         p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
01011 
01012     p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
01013     p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
01014 
01015     p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
01016 
01017     if( p_media->i_length > 0 )
01018     p += sprintf( p, "a=range:npt=0-%.3f\r\n",
01019                   ((float)(p_media->i_length/1000))/1000 );
01020 
01021     for( i = 0; i < p_media->i_es; i++ )
01022     {
01023         media_es_t *p_es = p_media->es[i];
01024 
01025         if( p_es->fmt.i_cat == AUDIO_ES )
01026         {
01027             p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
01028                           p_es->i_port, p_es->i_payload_type );
01029         }
01030         else if( p_es->fmt.i_cat == VIDEO_ES )
01031         {
01032             p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
01033                           p_es->i_port, p_es->i_payload_type );
01034         }
01035         else
01036         {
01037             continue;
01038         }
01039 
01040         if( p_es->psz_rtpmap )
01041         {
01042             p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
01043                           p_es->psz_rtpmap );
01044         }
01045         if( p_es->psz_fmtp )
01046         {
01047             p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
01048                           p_es->psz_fmtp );
01049         }
01050 
01051         p += sprintf( p, "a=control:%s/trackid=%d\r\n",
01052                       p_media->psz_rtsp_control, i );
01053     }
01054 
01055     return psz_sdp;
01056 }

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