]> git.sesse.net Git - vlc/blobdiff - modules/misc/rtsp.c
Strings
[vlc] / modules / misc / rtsp.c
index 6d13b844e88046e72fb6a2770e453319a2ef16d1..a6cd6c2a0f818be65a963e9560b5ac4d33e769af 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * rtsp.c: rtsp VoD server module
  *****************************************************************************
- * Copyright (C) 2003-2004 VideoLAN
+ * Copyright (C) 2003-2006 the VideoLAN team
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
@@ -19,7 +19,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -35,7 +35,9 @@
 
 #include "vlc_httpd.h"
 #include "vlc_vod.h"
+#include "vlc_url.h"
 #include "network.h"
+#include "charset.h"
 
 /*****************************************************************************
  * Module descriptor
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
-#define HOST_TEXT N_( "Host address" )
+#define HOST_TEXT N_( "RTSP host address" )
+/// \bug [String] extra space
 #define HOST_LONGTEXT N_( \
-    "You can set the address, port and path the rtsp interface will bind to." \
-    "\n Syntax is address:port/path. Default is to bind to localhost address "\
-    "on port 554, with no path. Use 0.0.0.0 to bind to all addresses." )
+    "This defines the address, port and path the RTSP VOD server will listen " \
+    "on.\nSyntax is address:port/path. The default is to listen on all "\
+    "interfaces (address 0.0.0.0), on port 554, with no path.\n To listen " \
+    "only on the local interface, use \"localhost\" as address." )
+
+#define THROTLE_TEXT N_( "Maximum number of connections" )
+#define THROTLE_LONGTEXT N_( "This limits the maximum number of clients " \
+    "that can connect to the RTSP VOD. 0 means no limit."  )
+
 vlc_module_begin();
     set_shortname( _("RTSP VoD" ) );
     set_description( _("RTSP VoD server") );
@@ -57,6 +66,8 @@ vlc_module_begin();
     set_callbacks( Open, Close );
     add_shortcut( "rtsp" );
     add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
+    add_integer( "rtsp-throttle-users", 0, NULL, THROTLE_TEXT,
+                                           THROTLE_LONGTEXT, VLC_TRUE );
 vlc_module_end();
 
 /*****************************************************************************
@@ -111,10 +122,10 @@ struct vod_media_t
 
     /* RTSP server */
     httpd_url_t  *p_rtsp_url;
-    char         *psz_rtsp_control;
+    char         *psz_rtsp_control_v4;
+    char         *psz_rtsp_control_v6;
     char         *psz_rtsp_path;
 
-    char *psz_destination;
     int  i_port;
     int  i_port_audio;
     int  i_port_video;
@@ -132,6 +143,7 @@ struct vod_media_t
     int        i_es;
     media_es_t **es;
     char       *psz_mux;
+    vlc_bool_t  b_raw;
 
     /* RTSP client */
     int           i_rtsp;
@@ -149,16 +161,17 @@ struct vod_sys_t
 {
     /* RTSP server */
     httpd_host_t *p_rtsp_host;
-    char *psz_host;
     char *psz_path;
     int i_port;
+    int i_throttle_users;
+    int i_connections;
 
     /* List of media */
     int i_media;
     vod_media_t **media;
 };
 
-static vod_media_t *MediaNew( vod_t *, char *, input_item_t * );
+static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
 static void         MediaDel( vod_t *, vod_media_t * );
 static int          MediaAddES( vod_t *, vod_media_t *, es_format_t * );
 static void         MediaDelES( vod_t *, vod_media_t *, es_format_t * );
@@ -172,7 +185,7 @@ static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
                            httpd_message_t *, httpd_message_t * );
 
-static char *SDPGenerate( vod_media_t *, char * );
+static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
 
 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
 {
@@ -201,32 +214,31 @@ static int Open( vlc_object_t *p_this )
     vlc_UrlParse( &url, psz_url, 0 );
     if( psz_url ) free( psz_url );
 
-    if( !url.psz_host || !*url.psz_host )
-    {
-        if( url.psz_host ) free( url.psz_host );
-        url.psz_host = strdup( "localhost" );
-    }
     if( url.i_port <= 0 ) url.i_port = 554;
 
     p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
     if( !p_sys ) goto error;
     p_sys->p_rtsp_host = 0;
 
+    var_Create( p_this, "rtsp-throttle-users", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
+    p_sys->i_throttle_users = var_GetInteger( p_this, "rtsp-throtle-users" );
+    msg_Dbg( p_this, "allowing up to %d connections", p_sys->i_throttle_users );
+    p_sys->i_connections = 0;
+
     p_sys->p_rtsp_host =
         httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
     if( !p_sys->p_rtsp_host )
     {
-        msg_Err( p_vod, "cannot create http server (%s:%i)",
+        msg_Err( p_vod, "cannot create RTSP server (%s:%i)",
                  url.psz_host, url.i_port );
         goto error;
     }
 
-    p_sys->psz_host = strdup( url.psz_host );
     p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
     p_sys->i_port = url.i_port;
 
     vlc_UrlClean( &url );
-    p_sys->media = 0;
+    p_sys->media = NULL;
     p_sys->i_media = 0;
 
     p_vod->pf_media_new = MediaNew;
@@ -236,11 +248,11 @@ static int Open( vlc_object_t *p_this )
 
     return VLC_SUCCESS;
 
- error:
-
+error:
     if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
     if( p_sys ) free( p_sys );
     vlc_UrlClean( &url );
+
     return VLC_EGENERIC;
 }
 
@@ -253,45 +265,59 @@ static void Close( vlc_object_t * p_this )
     vod_sys_t *p_sys = p_vod->p_sys;
 
     httpd_HostDelete( p_sys->p_rtsp_host );
+    var_Destroy( p_this, "rtsp-throttle-users" );
 
     /* TODO delete medias */
-
+    free( p_sys->psz_path );
     free( p_sys );
 }
 
 /*****************************************************************************
  * Media handling
  *****************************************************************************/
-static vod_media_t *MediaNew( vod_t *p_vod, char *psz_name,
+static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
                               input_item_t *p_item )
 {
     vod_sys_t *p_sys = p_vod->p_sys;
     vod_media_t *p_media = malloc( sizeof(vod_media_t) );
     int i;
 
+    if( !p_media )
+    {
+        msg_Err( p_vod, "not enough memory" );
+        return NULL;
+    }
+
     memset( p_media, 0, sizeof(vod_media_t) );
     p_media->es = 0;
     p_media->psz_mux = 0;
     p_media->rtsp = 0;
+    p_media->b_raw = VLC_FALSE;
 
     asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
     p_media->p_rtsp_url =
-        httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, 0, 0,
-                            NULL, 0 );
+        httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
+                            NULL, NULL );
 
     if( !p_media->p_rtsp_url )
     {
-        msg_Err( p_vod, "cannot create http url (%s)", p_media->psz_rtsp_path);
+        msg_Err( p_vod, "cannot create RTSP url (%s)", p_media->psz_rtsp_path);
         free( p_media->psz_rtsp_path );
         free( p_media );
-        return 0;
+        return NULL;
     }
 
-    msg_Dbg( p_vod, "created rtsp url: %s", p_media->psz_rtsp_path );
+    msg_Dbg( p_vod, "created RTSP url: %s", p_media->psz_rtsp_path );
 
-    asprintf( &p_media->psz_rtsp_control, "rtsp://%s:%d%s",
-              p_sys->psz_host, p_sys->i_port, p_media->psz_rtsp_path );
+    asprintf( &p_media->psz_rtsp_control_v4,
+               "a=control:rtsp://%%s:%d%s/trackID=%%d\r\n",
+               p_sys->i_port, p_media->psz_rtsp_path );
+    asprintf( &p_media->psz_rtsp_control_v6,
+               "a=control:rtsp://[%%s]:%d%s/trackID=%%d\r\n",
+              p_sys->i_port, p_media->psz_rtsp_path );
 
+    httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
+                    RtspCallback, (void*)p_media );
     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
                     RtspCallback, (void*)p_media );
     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
@@ -340,13 +366,18 @@ static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
     while( p_media->i_rtsp > 0 ) RtspClientDel( p_media, p_media->rtsp[0] );
     httpd_UrlDelete( p_media->p_rtsp_url );
     if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
-    if( p_media->psz_rtsp_control ) free( p_media->psz_rtsp_control );
+    if( p_media->psz_rtsp_control_v6 ) free( p_media->psz_rtsp_control_v6 );
+    if( p_media->psz_rtsp_control_v4 ) free( p_media->psz_rtsp_control_v4 );
 
     TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
 
     while( p_media->i_es ) MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
 
     vlc_mutex_destroy( &p_media->lock );
+    free( p_media->psz_session_name );
+    free( p_media->psz_session_description );
+    free( p_media->psz_session_url );
+    free( p_media->psz_session_email );
     free( p_media );
 }
 
@@ -359,118 +390,117 @@ static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
     p_media->psz_mux = NULL;
 
     /* TODO: update SDP, etc... */
-    asprintf( &psz_urlc, "%s/trackid=%d",
+    asprintf( &psz_urlc, "%s/trackID=%d",
               p_media->psz_rtsp_path, p_media->i_es );
     msg_Dbg( p_vod, "  - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
 
     switch( p_fmt->i_codec )
     {
-    case VLC_FOURCC( 's', '1', '6', 'b' ):
-        if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
-        {
-            p_es->i_payload_type = 11;
-        }
-        else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
-        {
-            p_es->i_payload_type = 10;
-        }
-        else
-        {
+        case VLC_FOURCC( 's', '1', '6', 'b' ):
+            if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
+            {
+                p_es->i_payload_type = 11;
+            }
+            else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
+            {
+                p_es->i_payload_type = 10;
+            }
+            else
+            {
+                p_es->i_payload_type = p_media->i_payload_type++;
+            }
+            p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
+            sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
+                    p_fmt->audio.i_channels );
+            break;
+        case VLC_FOURCC( 'u', '8', ' ', ' ' ):
             p_es->i_payload_type = p_media->i_payload_type++;
-        }
+            p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
+            sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
+                    p_fmt->audio.i_channels );
+            break;
+        case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
+            p_es->i_payload_type = 14;
+            p_es->psz_rtpmap = strdup( "MPA/90000" );
+            break;
+        case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
+            p_es->i_payload_type = 32;
+            p_es->psz_rtpmap = strdup( "MPV/90000" );
+            break;
+        case VLC_FOURCC( 'a', '5', '2', ' ' ):
+            p_es->i_payload_type = p_media->i_payload_type++;
+            p_es->psz_rtpmap = strdup( "ac3/90000" );
+            break;
+        case VLC_FOURCC( 'H', '2', '6', '3' ):
+            p_es->i_payload_type = p_media->i_payload_type++;
+            p_es->psz_rtpmap = strdup( "H263-1998/90000" );
+            break;
+        case VLC_FOURCC( 'm', 'p', '4', 'v' ):
+            p_es->i_payload_type = p_media->i_payload_type++;
+            p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
+            if( p_fmt->i_extra > 0 )
+            {
+                char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
+                p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
+                sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
+                sprintf( p_es->psz_fmtp,
+                        "profile-level-id=3; config=%s;", p_hexa );
+                free( p_hexa );
+            }
+            break;
+        case VLC_FOURCC( 'm', 'p', '4', 'a' ):
+            p_es->i_payload_type = p_media->i_payload_type++;
+            p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
+            sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
+            if( p_fmt->i_extra > 0 )
+            {
+                char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
+                p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
+                sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
+                sprintf( p_es->psz_fmtp,
+                        "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
+                        "config=%s; SizeLength=13;IndexLength=3; "
+                        "IndexDeltaLength=3; Profile=1;", p_hexa );
+                free( p_hexa );
+            }
+            break;
+        case VLC_FOURCC( 'm', 'p', '2', 't' ):
+            p_media->psz_mux = "ts";
+            p_es->i_payload_type = 33;
+            p_es->psz_rtpmap = strdup( "MP2T/90000" );
+            break;
+        case VLC_FOURCC( 'm', 'p', '2', 'p' ):
+            p_media->psz_mux = "ps";
+            p_es->i_payload_type = p_media->i_payload_type++;
+            p_es->psz_rtpmap = strdup( "MP2P/90000" );
+            break;
+        case VLC_FOURCC( 's', 'a', 'm', 'r' ):
+            p_es->i_payload_type = p_media->i_payload_type++;
+            p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
+                                    "AMR/8000/2" : "AMR/8000" );
+            p_es->psz_fmtp = strdup( "octet-align=1" );
+            break;
+        case VLC_FOURCC( 's', 'a', 'w', 'b' ):
+            p_es->i_payload_type = p_media->i_payload_type++;
+            p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
+                                    "AMR-WB/16000/2" : "AMR-WB/16000" );
+            p_es->psz_fmtp = strdup( "octet-align=1" );
+            break;
 
-        p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
-        sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
-                 p_fmt->audio.i_channels );
-        break;
-    case VLC_FOURCC( 'u', '8', ' ', ' ' ):
-        p_es->i_payload_type = p_media->i_payload_type++;
-        p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
-        sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
-                 p_fmt->audio.i_channels );
-        break;
-    case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
-        p_es->i_payload_type = 14;
-        p_es->psz_rtpmap = strdup( "MPA/90000" );
-        break;
-    case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
-        p_es->i_payload_type = 32;
-        p_es->psz_rtpmap = strdup( "MPV/90000" );
-        break;
-    case VLC_FOURCC( 'a', '5', '2', ' ' ):
-        p_es->i_payload_type = p_media->i_payload_type++;
-        p_es->psz_rtpmap = strdup( "ac3/90000" );
-        break;
-    case VLC_FOURCC( 'H', '2', '6', '3' ):
-        p_es->i_payload_type = p_media->i_payload_type++;
-        p_es->psz_rtpmap = strdup( "H263-1998/90000" );
-        break;
-    case VLC_FOURCC( 'm', 'p', '4', 'v' ):
-        p_es->i_payload_type = p_media->i_payload_type++;
-        p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
-        if( p_fmt->i_extra > 0 )
-        {
-            char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
-            p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
-            sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
-            sprintf( p_es->psz_fmtp,
-                     "profile-level-id=3; config=%s;", p_hexa );
-            free( p_hexa );
-        }
-        break;
-    case VLC_FOURCC( 'm', 'p', '4', 'a' ):
-        p_es->i_payload_type = p_media->i_payload_type++;
-        p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
-        sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
-        if( p_fmt->i_extra > 0 )
-        {
-            char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
-            p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
-            sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
-            sprintf( p_es->psz_fmtp,
-                     "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
-                     "config=%s; SizeLength=13;IndexLength=3; "
-                     "IndexDeltaLength=3; Profile=1;", p_hexa );
-            free( p_hexa );
-        }
-        break;
-    case VLC_FOURCC( 'm', 'p', '2', 't' ):
-        p_media->psz_mux = "ts";
-        p_es->i_payload_type = 33;
-        p_es->psz_rtpmap = strdup( "MP2T/90000" );
-        break;
-    case VLC_FOURCC( 'm', 'p', '2', 'p' ):
-        p_media->psz_mux = "ps";
-        p_es->i_payload_type = p_media->i_payload_type++;
-        p_es->psz_rtpmap = strdup( "MP2P/90000" );
-        break;
-    case VLC_FOURCC( 's', 'a', 'm', 'r' ):
-        p_es->i_payload_type = p_media->i_payload_type++;
-        p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
-                                   "AMR/8000/2" : "AMR/8000" );
-        p_es->psz_fmtp = strdup( "octet-align=1" );
-        break; 
-    case VLC_FOURCC( 's', 'a', 'w', 'b' ):
-        p_es->i_payload_type = p_media->i_payload_type++;
-        p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
-                                   "AMR-WB/16000/2" : "AMR-WB/16000" );
-        p_es->psz_fmtp = strdup( "octet-align=1" );
-        break; 
-
-    default:
-        msg_Err( p_vod, "cannot add this stream (unsupported "
-                 "codec: %4.4s)", (char*)&p_fmt->i_codec );
-        free( p_es );
-        return VLC_EGENERIC;
+        default:
+            msg_Err( p_vod, "cannot add this stream (unsupported "
+                    "codec: %4.4s)", (char*)&p_fmt->i_codec );
+            free( p_es );
+            return VLC_EGENERIC;
     }
 
     p_es->p_rtsp_url =
-        httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, 0, 0, NULL,
-                            0 );
+        httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
+                            NULL );
 
     if( !p_es->p_rtsp_url )
     {
-        msg_Err( p_vod, "cannot create http url (%s)", psz_urlc );
+        msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
         free( psz_urlc );
         free( p_es );
         return VLC_EGENERIC;
@@ -564,13 +594,17 @@ static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
 {
     rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
+
+    if( !p_rtsp ) return NULL;
     memset( p_rtsp, 0, sizeof(rtsp_client_t) );
     p_rtsp->es = 0;
 
     p_rtsp->psz_session = psz_session;
     TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
 
-    msg_Dbg( p_media->p_vod, "new session: %s", psz_session );
+    p_media->p_vod->p_sys->i_connections++;
+    msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
+             psz_session, p_media->p_vod->p_sys->i_throttle_users );
 
     return p_rtsp;
 }
@@ -592,7 +626,9 @@ static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
 
 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
 {
-    msg_Dbg( p_media->p_vod, "closing session: %s", p_rtsp->psz_session );
+    p_media->p_vod->p_sys->i_connections--;
+    msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
+             p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
 
     while( p_rtsp->i_es-- )
     {
@@ -613,47 +649,137 @@ static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
 {
     vod_media_t *p_media = (vod_media_t*)p_args;
     vod_t *p_vod = p_media->p_vod;
-    char *psz_destination = p_media->psz_destination;
+    char *psz_transport = NULL;
+    char *psz_playnow = NULL; /* support option: x-playNow */
     char *psz_session = NULL;
+    char *psz_cseq = NULL;
     rtsp_client_t *p_rtsp;
+    int i_port = 0;
+    int i_cseq = 0;
 
     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
 
-    fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
+    msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
 
     answer->i_proto   = HTTPD_PROTO_RTSP;
     answer->i_version = query->i_version;
     answer->i_type    = HTTPD_MSG_ANSWER;
+    answer->i_body    = 0;
+    answer->p_body      = NULL;
 
     switch( query->i_type )
     {
-        case HTTPD_MSG_DESCRIBE:
+        case HTTPD_MSG_SETUP:
         {
-            char *psz_sdp =
-                SDPGenerate( p_media, psz_destination ?
-                             psz_destination : "0.0.0.0" );
+            psz_playnow = httpd_MsgGet( query, "x-playNow" );
+            psz_transport = httpd_MsgGet( query, "Transport" );
+            msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
 
-            answer->i_status = 200;
-            answer->psz_status = strdup( "OK" );
-            httpd_MsgAdd( answer, "Content-type",  "%s", "application/sdp" );
+            if( strstr( psz_transport, "unicast" ) &&
+                strstr( psz_transport, "client_port=" ) )
+            {
+                rtsp_client_t *p_rtsp;
+                char ip[NI_MAXNUMERICHOST];
+                i_port = atoi( strstr( psz_transport, "client_port=" ) +
+                                strlen("client_port=") );
 
-            answer->p_body = psz_sdp;
-            answer->i_body = strlen( psz_sdp );
-            break;
+                if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
+                    strstr( psz_transport, "RAW/RAW/UDP" ) )
+                {
+                    p_media->b_raw = VLC_TRUE;
+                }
+
+                if( httpd_ClientIP( cl, ip ) == NULL )
+                {
+                    answer->i_status = 500;
+                    answer->psz_status = strdup( "Internal server error" );
+                    answer->i_body = 0;
+                    answer->p_body = NULL;
+                    break;
+                }
+
+                msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
+                         ip, i_port );
+
+                psz_session = httpd_MsgGet( query, "Session" );
+                if( !psz_session || !*psz_session )
+                {
+                    if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
+                        ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
+                    {
+                        answer->i_status = 503;
+                        answer->psz_status = strdup( "Too many connections" );
+                        answer->i_body = 0;
+                        answer->p_body = NULL;
+                        break;
+                    }
+                    asprintf( &psz_session, "%d", rand() );
+                    p_rtsp = RtspClientNew( p_media, psz_session );
+                }
+                else
+                {
+                    p_rtsp = RtspClientGet( p_media, psz_session );
+                    if( !p_rtsp )
+                    {
+                        answer->i_status = 454;
+                        answer->psz_status = strdup( "Unknown session id" );
+                        answer->i_body = 0;
+                        answer->p_body = NULL;
+                        break;
+                    }
+                }
+
+                answer->i_status = 200;
+                answer->psz_status = strdup( "OK" );
+                answer->i_body = 0;
+                answer->p_body = NULL;
+
+                if( p_media->b_raw )
+                {
+                    if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
+                    {
+                        httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
+                                      i_port, i_port + 1 );
+                    }
+                    else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
+                    {
+                        httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
+                                      i_port, i_port + 1 );
+                    }
+                }
+                else
+                    httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
+                                  i_port, i_port + 1 );
+            }
+            else /* TODO  strstr( psz_transport, "interleaved" ) ) */
+            {
+                answer->i_status = 461;
+                answer->psz_status = strdup( "Unsupported Transport" );
+                answer->i_body = 0;
+                answer->p_body = NULL;
+            }
+
+            /* Intentional fall-through on x-playNow option in RTSP request */
+            if( !psz_playnow )
+                break;
         }
 
         case HTTPD_MSG_PLAY:
         {
-            char *psz_output, *ip;
+            char *psz_output, ip[NI_MAXNUMERICHOST];
             int i, i_port_audio = 0, i_port_video = 0;
 
             /* for now only multicast so easy */
-            answer->i_status = 200;
-            answer->psz_status = strdup( "OK" );
-            answer->i_body = 0;
-            answer->p_body = NULL;
+            if( !psz_playnow )
+            {
+                answer->i_status = 200;
+                answer->psz_status = strdup( "OK" );
+                answer->i_body = 0;
+                answer->p_body = NULL;
+            }
 
-            psz_session = httpd_MsgGet( query, "Session" );
+            if( !psz_session )
+                psz_session = httpd_MsgGet( query, "Session" );
             msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
 
             p_rtsp = RtspClientGet( p_media, psz_session );
@@ -668,7 +794,7 @@ static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
             }
             else if( p_rtsp->b_playing ) break;
 
-            if( !(ip = httpd_ClientIP( cl )) ) break;
+            if( httpd_ClientIP( cl, ip ) == NULL ) break;
 
             p_rtsp->b_playing = VLC_TRUE;
 
@@ -683,8 +809,16 @@ static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
 
             if( p_media->psz_mux )
             {
-                asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
-                          ip, i_port_video, p_media->psz_mux );
+                if( p_media->b_raw )
+                {
+                    asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
+                              ip, i_port, p_media->psz_mux );
+                }
+                else
+                {
+                    asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
+                              ip, i_port_video, p_media->psz_mux );
+                }
             }
             else
             {
@@ -695,7 +829,30 @@ static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
                               psz_output );
             free( psz_output );
-            free( ip );
+            break;
+        }
+
+        case HTTPD_MSG_DESCRIBE:
+        {
+            char *psz_sdp =
+                SDPGenerate( p_media, cl );
+
+            if( psz_sdp != NULL )
+            {
+                answer->i_status = 200;
+                answer->psz_status = strdup( "OK" );
+                httpd_MsgAdd( answer, "Content-type",  "%s", "application/sdp" );
+
+                answer->p_body = (uint8_t *)psz_sdp;
+                answer->i_body = strlen( psz_sdp );
+            }
+            else
+            {
+                answer->i_status = 500;
+                answer->psz_status = strdup( "Internal server error" );
+                answer->p_body = NULL;
+                answer->i_body = 0;
+            }
             break;
         }
 
@@ -738,8 +895,9 @@ static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
 
     httpd_MsgAdd( answer, "Server", "VLC Server" );
     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
-    httpd_MsgAdd( answer, "Cseq", "%d",
-                  atoi( httpd_MsgGet( query, "Cseq" ) ) );
+    psz_cseq = httpd_MsgGet( query, "Cseq" );
+    psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
+    httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
 
     if( psz_session )
@@ -757,108 +915,159 @@ static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
     vod_media_t *p_media = p_es->p_media;
     vod_t *p_vod = p_media->p_vod;
     rtsp_client_t *p_rtsp = NULL;
-    char *psz_session = NULL;
     char *psz_transport = NULL;
+    char *psz_playnow = NULL; /* support option: x-playNow */
+    char *psz_session = NULL;
     char *psz_position = NULL;
+    char *psz_cseq = NULL;
+    int i_cseq = 0;
     int i;
 
     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
 
-    fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
+    msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
 
     answer->i_proto   = HTTPD_PROTO_RTSP;
     answer->i_version = query->i_version;
     answer->i_type    = HTTPD_MSG_ANSWER;
+    answer->i_body    = 0;
+    answer->p_body      = NULL;
 
     switch( query->i_type )
     {
-    case HTTPD_MSG_SETUP:
-        psz_transport = httpd_MsgGet( query, "Transport" );
-        fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
+        case HTTPD_MSG_SETUP:
+            psz_playnow = httpd_MsgGet( query, "x-playNow" );
+            psz_transport = httpd_MsgGet( query, "Transport" );
 
-        if( strstr( psz_transport, "multicast" ) && p_media->psz_destination )
-        {
-            fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
-            answer->i_status = 200;
-            answer->psz_status = strdup( "OK" );
-            answer->i_body = 0;
-            answer->p_body = NULL;
+            msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
 
-            psz_session = httpd_MsgGet( query, "Session" );
-            if( !psz_session || !*psz_session )
+            if( strstr( psz_transport, "unicast" ) &&
+                strstr( psz_transport, "client_port=" ) )
             {
-                asprintf( &psz_session, "%d", rand() );
-            }
+                rtsp_client_t *p_rtsp;
+                rtsp_client_es_t *p_rtsp_es;
+                char ip[NI_MAXNUMERICHOST];
+                int i_port = atoi( strstr( psz_transport, "client_port=" ) +
+                                strlen("client_port=") );
 
-            httpd_MsgAdd( answer, "Transport",
-                          "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
-                          p_media->psz_destination, p_media->i_port,
-                          p_media->i_port+1, p_media->i_ttl );
-        }
-        else if( strstr( psz_transport, "unicast" ) &&
-                 strstr( psz_transport, "client_port=" ) )
-        {
-            rtsp_client_t *p_rtsp;
-            rtsp_client_es_t *p_rtsp_es;
-            char *ip = httpd_ClientIP( cl );
-            int i_port = atoi( strstr( psz_transport, "client_port=" ) +
-                               strlen("client_port=") );
+                if( httpd_ClientIP( cl, ip ) == NULL )
+                {
+                    answer->i_status = 500;
+                    answer->psz_status = strdup( "Internal server error" );
+                    answer->i_body = 0;
+                    answer->p_body = NULL;
+                    break;
+                }
+
+                msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
+                        ip, i_port );
+
+                psz_session = httpd_MsgGet( query, "Session" );
+                if( !psz_session || !*psz_session )
+                {
+                    if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
+                        ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
+                    {
+                        answer->i_status = 503;
+                        answer->psz_status = strdup( "Too many connections" );
+                        answer->i_body = 0;
+                        answer->p_body = NULL;
+                        break;
+                    }
+                    asprintf( &psz_session, "%d", rand() );
+                    p_rtsp = RtspClientNew( p_media, psz_session );
+                }
+                else
+                {
+                    p_rtsp = RtspClientGet( p_media, psz_session );
+                    if( !p_rtsp )
+                    {
+                        answer->i_status = 454;
+                        answer->psz_status = strdup( "Unknown session id" );
+                        answer->i_body = 0;
+                        answer->p_body = NULL;
+                        break;
+                    }
+                }
 
-            if( !ip )
+                p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
+                p_rtsp_es->i_port = i_port;
+                p_rtsp_es->psz_ip = strdup( ip );
+                p_rtsp_es->p_media_es = p_es;
+                TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
+
+                answer->i_status = 200;
+                answer->psz_status = strdup( "OK" );
+                answer->i_body = 0;
+                answer->p_body = NULL;
+
+                if( p_media->b_raw )
+                {
+                    if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
+                    {
+                        httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
+                                      i_port, i_port + 1 );
+                    }
+                    else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
+                    {
+                        httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
+                                      i_port, i_port + 1 );
+                    }
+                }
+                else
+                {
+                    httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
+                                  i_port, i_port + 1 );
+                }
+            }
+            else /* TODO  strstr( psz_transport, "interleaved" ) ) */
             {
-                answer->i_status = 400;
-                answer->psz_status = strdup( "Internal server error" );
+                answer->i_status = 461;
+                answer->psz_status = strdup( "Unsupported Transport" );
                 answer->i_body = 0;
                 answer->p_body = NULL;
-                break;
             }
 
-            fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n",
-                     ip, i_port );
+            /* Intentional fall-through on x-playNow option in RTSP request */
+            if( !psz_playnow )
+                break;
 
+        case HTTPD_MSG_PLAY:
+            /* This is kind of a kludge. Should we only support Aggregate
+             * Operations ? */
             psz_session = httpd_MsgGet( query, "Session" );
-            if( !psz_session || !*psz_session )
-            {
-                asprintf( &psz_session, "%d", rand() );
-                p_rtsp = RtspClientNew( p_media, psz_session );
-            }
-            else
+            msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
+
+            p_rtsp = RtspClientGet( p_media, psz_session );
+
+            psz_position = httpd_MsgGet( query, "Range" );
+            if( psz_position ) psz_position = strstr( psz_position, "npt=" );
+            if( psz_position )
             {
-                p_rtsp = RtspClientGet( p_media, psz_session );
-                if( !p_rtsp )
+                double f_pos;
+                char *end;
+
+                msg_Dbg( p_vod, "seeking request: %s", psz_position );
+
+                psz_position += 4;
+                /* FIXME: npt= is not necessarily formatted as a float */
+                f_pos = us_strtod( psz_position, &end );
+                if( end > psz_position )
                 {
-                    /* FIXME right error code */
-                    answer->i_status = 400;
-                    answer->psz_status = strdup( "Unknown session id" );
-                    answer->i_body = 0;
-                    answer->p_body = NULL;
-                    free( ip );
-                    break;
+                    f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
+                    vod_MediaControl( p_vod, p_media, psz_session,
+                                      VOD_MEDIA_SEEK, f_pos );
                 }
             }
 
-            p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
-            p_rtsp_es->i_port = i_port;
-            p_rtsp_es->psz_ip = strdup( ip );
-            p_rtsp_es->p_media_es = p_es;
-            TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
-
-            answer->i_status = 200;
-            answer->psz_status = strdup( "OK" );
-            answer->i_body = 0;
-            answer->p_body = NULL;
-
-            httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
-                          i_port, i_port + 1 );
-        }
-        else /* TODO  strstr( psz_transport, "interleaved" ) ) */
-        {
-            answer->i_status = 400;
-            answer->psz_status = strdup( "Bad Request" );
-            answer->i_body = 0;
-            answer->p_body = NULL;
-        }
-        break;
+            if( !psz_playnow )
+            {
+                answer->i_status = 200;
+                answer->psz_status = strdup( "OK" );
+                answer->i_body = 0;
+                answer->p_body = NULL;
+            }
+            break;
 
         case HTTPD_MSG_TEARDOWN:
             answer->i_status = 200;
@@ -890,37 +1099,6 @@ static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
             }
             break;
 
-        case HTTPD_MSG_PLAY:
-            /* This is kind of a kludge. Should we only support Aggregate
-             * Operations ? */
-            psz_session = httpd_MsgGet( query, "Session" );
-            msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
-
-            p_rtsp = RtspClientGet( p_media, psz_session );
-
-            psz_position = httpd_MsgGet( query, "Range" );
-            if( psz_position ) psz_position = strstr( psz_position, "npt=" );
-            if( psz_position )
-            {
-                float f_pos;
-
-                msg_Dbg( p_vod, "seeking request: %s", psz_position );
-
-                psz_position += 4;
-                if( sscanf( psz_position, "%f", &f_pos ) == 1 )
-                {
-                    f_pos /= ((float)(p_media->i_length/1000))/1000 / 100;
-                    vod_MediaControl( p_vod, p_media, psz_session,
-                                      VOD_MEDIA_SEEK, (double)f_pos );
-                }
-            }
-
-            answer->i_status = 200;
-            answer->psz_status = strdup( "OK" );
-            answer->i_body = 0;
-            answer->p_body = NULL;
-            break;
-
         case HTTPD_MSG_PAUSE:
             /* This is kind of a kludge. Should we only support Aggregate
              * Operations ? */
@@ -946,14 +1124,16 @@ static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
 
     httpd_MsgAdd( answer, "Server", "VLC Server" );
     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
-    httpd_MsgAdd( answer, "Cseq", "%d",
-                  atoi( httpd_MsgGet( query, "Cseq" ) ) );
+    psz_cseq = httpd_MsgGet( query, "Cseq" );
+    if (psz_cseq)
+        i_cseq = atoi( psz_cseq );
+    else
+        i_cseq = 0;
+    httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
 
     if( psz_session )
-    {
         httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
-    }
 
     return VLC_SUCCESS;
 }
@@ -962,48 +1142,57 @@ static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
  * SDPGenerate: TODO
  * FIXME: need to be moved to a common place ?
  *****************************************************************************/
-static char *SDPGenerate( vod_media_t *p_media, char *psz_destination )
+static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
 {
     int i, i_size;
-    char *p, *psz_sdp;
+    char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
+    const char *psz_control;
 
-    /* Calculate size */
-    i_size = strlen( "v=0\r\n" ) +
-        strlen( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
-        strlen( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
-        strlen( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
-        strlen( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
-        strlen( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
-        strlen( "t=0 0\r\n" ) + /* FIXME */
-        strlen( "a=tool:"PACKAGE_STRING"\r\n" ) +
-        strlen( "c=IN IP4 */*\r\n" ) + 20 + 10 +
-        strlen( psz_destination ? psz_destination : "0.0.0.0" ) +
-        strlen( "a=range:npt=0-1000000000.000\r\n" );
+    if( httpd_ServerIP( cl, ip ) == NULL )
+        return NULL;
+
+    p = strchr( ip, '%' );
+    if( p != NULL )
+        *p = '\0'; /* remove scope if present */
 
+    ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
+
+    /* Calculate size */
+    i_size = sizeof( "v=0\r\n" ) +
+        sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
+        sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
+        sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
+        sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
+        sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
+        sizeof( "t=0 0\r\n" ) + /* FIXME */
+        sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
+        sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
+        sizeof( "a=range:npt=0-1000000000.000\r\n" );
+
+    psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
+                               : p_media->psz_rtsp_control_v4;
     for( i = 0; i < p_media->i_es; i++ )
     {
         media_es_t *p_es = p_media->es[i];
 
-        i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
+        i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
         if( p_es->psz_rtpmap )
         {
-            i_size += strlen( "a=rtpmap:* *\r\n" ) +
-                strlen( p_es->psz_rtpmap ) + 10;
+            i_size += sizeof( "a=rtpmap:* *\r\n" ) +
+                strlen( p_es->psz_rtpmap ) + 9;
         }
         if( p_es->psz_fmtp )
         {
-            i_size += strlen( "a=fmtp:* *\r\n" ) +
-                strlen( p_es->psz_fmtp ) + 10;
+            i_size += sizeof( "a=fmtp:* *\r\n" ) +
+                strlen( p_es->psz_fmtp ) + 9;
         }
-
-        i_size += strlen( "a=control:*/trackid=*\r\n" ) +
-            strlen( p_media->psz_rtsp_control ) + 10;
     }
+    i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
 
     p = psz_sdp = malloc( i_size );
     p += sprintf( p, "v=0\r\n" );
-    p += sprintf( p, "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n",
-                  p_media->i_sdp_id, p_media->i_sdp_version );
+    p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
+                  p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
     if( *p_media->psz_session_name )
         p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
     if( *p_media->psz_session_description )
@@ -1016,12 +1205,14 @@ static char *SDPGenerate( vod_media_t *p_media, char *psz_destination )
     p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
     p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
 
-    p += sprintf( p, "c=IN IP4 %s/%d\r\n", psz_destination ?
-                  psz_destination : "0.0.0.0", p_media->i_ttl );
+    p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
 
     if( p_media->i_length > 0 )
-    p += sprintf( p, "a=range:npt=0-%.3f\r\n",
-                  ((float)(p_media->i_length/1000))/1000 );
+    {
+        lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
+        p += sprintf( p, "a=range:npt=0-"I64Fd".%03u\r\n", d.quot,
+                      (unsigned)d.rem );
+    }
 
     for( i = 0; i < p_media->i_es; i++ )
     {
@@ -1053,10 +1244,8 @@ static char *SDPGenerate( vod_media_t *p_media, char *psz_destination )
                           p_es->psz_fmtp );
         }
 
-        p += sprintf( p, "a=control:%s/trackid=%d\r\n",
-                      p_media->psz_rtsp_control, i );
+        p += sprintf( p, psz_control, ip, i );
     }
 
-    fprintf( stderr, psz_sdp );
     return psz_sdp;
 }