]> git.sesse.net Git - vlc/blobdiff - modules/stream_out/rtp.c
* unify the Time-To-Live string and fix its Spanish translation (thanks to the forum...
[vlc] / modules / stream_out / rtp.c
index d60086ef65a47eb02325e8bc5bd5386c01457bb9..1820846d16579b602e373d3b0da241f0c1057700 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * rtp.c: rtp stream output module
  *****************************************************************************
- * Copyright (C) 2003-2004 VideoLAN
+ * Copyright (C) 2003-2004 the VideoLAN team
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
@@ -38,6 +38,9 @@
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
+
+#define MTU_REDUCE 50
+
 #define DST_TEXT N_("Destination")
 #define DST_LONGTEXT N_( \
     "Allows you to specify the output URL used for the streaming output." )
@@ -74,7 +77,7 @@
 #define PORT_VIDEO_LONGTEXT N_( \
     "Allows you to specify the default video port used for the RTP streaming." )
 
-#define TTL_TEXT N_("Time to live")
+#define TTL_TEXT N_("Time-To-Live")
 #define TTL_LONGTEXT N_( \
     "Allows you to specify the time to live for the output stream." )
 
@@ -84,6 +87,7 @@ static void Close( vlc_object_t * );
 #define SOUT_CFG_PREFIX "sout-rtp-"
 
 vlc_module_begin();
+    set_shortname( _("RTP"));
     set_description( _("RTP stream output") );
     set_capability( "sout stream", 0 );
     add_shortcut( "rtp" );
@@ -106,12 +110,12 @@ vlc_module_begin();
     add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT,
                 EMAIL_LONGTEXT, VLC_TRUE );
 
-    add_integer( SOUT_CFG_PREFIX "port-audio", 1234, NULL, PORT_AUDIO_TEXT,
-                 PORT_LONGTEXT, VLC_TRUE );
-    add_integer( SOUT_CFG_PREFIX "port-video", 1236, NULL, PORT_VIDEO_TEXT,
-                 PORT_LONGTEXT, VLC_TRUE );
-    add_integer( SOUT_CFG_PREFIX "port", 1238, NULL, PORT_TEXT,
+    add_integer( SOUT_CFG_PREFIX "port", 1234, NULL, PORT_TEXT,
                  PORT_LONGTEXT, VLC_TRUE );
+    add_integer( SOUT_CFG_PREFIX "port-audio", 1230, NULL, PORT_AUDIO_TEXT,
+                 PORT_AUDIO_LONGTEXT, VLC_TRUE );
+    add_integer( SOUT_CFG_PREFIX "port-video", 1232, NULL, PORT_VIDEO_TEXT,
+                 PORT_VIDEO_LONGTEXT, VLC_TRUE );
 
     add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL, TTL_TEXT,
                  TTL_LONGTEXT, VLC_TRUE );
@@ -351,6 +355,12 @@ static int Open( vlc_object_t *p_this )
     }
 
     var_Get( p_stream, SOUT_CFG_PREFIX "ttl", &val );
+    if( ( val.i_int > 255 ) || ( val.i_int < 0 ) )
+    {
+        msg_Err( p_stream, "illegal TTL %d", val.i_int );
+        free( p_sys );
+        return VLC_EGENERIC;
+    }
     p_sys->i_ttl = val.i_int;
 
     p_sys->i_payload_type = 96;
@@ -388,10 +398,14 @@ static int Open( vlc_object_t *p_this )
     if( *val.psz_string )
     {
         sout_access_out_t *p_grab;
+        char *psz_rtpmap, url[NI_MAXHOST + 8], access[17], psz_ttl[5], ipv;
 
-        char *psz_rtpmap;
-        char access[100];
-        char url[p_sys->psz_destination ? strlen( p_sys->psz_destination ) + 1 + 12+1 : 14];
+        if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
+        {
+            msg_Err( p_stream, "rtp needs a destination when muxing" );
+            free( p_sys );
+            return VLC_EGENERIC;
+        }
 
         /* Check muxer type */
         if( !strncasecmp( val.psz_string, "ps", 2 ) || !strncasecmp( val.psz_string, "mpeg1", 5 ) )
@@ -406,6 +420,7 @@ static int Open( vlc_object_t *p_this )
         else
         {
             msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
+            free( p_sys );
             return VLC_EGENERIC;
         }
 
@@ -418,7 +433,19 @@ static int Open( vlc_object_t *p_this )
         {
             sprintf( access, "udp{raw}" );
         }
-        sprintf( url, "%s:%d", p_sys->psz_destination, p_sys->i_port );
+
+        /* IPv6 needs brackets if not already present */
+        snprintf( url, sizeof( url ),
+                  ( ( p_sys->psz_destination[0] != '[' ) 
+                 && ( strchr( p_sys->psz_destination, ':' ) != NULL ) )
+                  ? "[%s]:%d" : "%s:%d", p_sys->psz_destination,
+                  p_sys->i_port );
+        url[sizeof( url ) - 1] = '\0';
+        /* FIXME: we should check that url is a numerical address, otherwise
+         * the SDP will be quite broken (regardless of the IP protocol version)
+         */
+        ipv = ( strchr( p_sys->psz_destination, ':' ) != NULL ) ? '6' : '4';
+
         if( !( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) )
         {
             msg_Err( p_stream, "cannot create the access out for %s://%s",
@@ -427,11 +454,12 @@ static int Open( vlc_object_t *p_this )
             return VLC_EGENERIC;
         }
         p_sys->i_mtu = config_GetInt( p_stream, "mtu" );  /* XXX beurk */
-        if( p_sys->i_mtu <= 16 )
+        if( p_sys->i_mtu <= 16 + MTU_REDUCE )
         {
             /* better than nothing */
             p_sys->i_mtu = 1500;
         }
+        p_sys->i_mtu -= MTU_REDUCE;
 
         /* the access out grabber TODO export it as sout_AccessOutGrabberNew */
         p_grab = p_sys->p_grab =
@@ -464,38 +492,44 @@ static int Open( vlc_object_t *p_this )
            o= don't use the localhost address. use fully qualified domain name or IP4 address
            p= international phone number (pass via vars?)
            c= IP6 support
-           c= /ttl should only be added in case of multicast
            a= recvonly (missing)
            a= type:broadcast (missing)
            a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
            a= x-plgroup: (missing)
            RTP packets need to get the correct src IP address  */
-        p_sys->psz_sdp =
-            malloc( 10 + 30 + 10 + strlen( p_sys->psz_session_name ) +
-                    10 + strlen( p_sys->psz_session_description ) + 10 + strlen( p_sys->psz_session_url ) +
-                    10 + strlen( p_sys->psz_session_email ) + 10 + strlen( p_sys->psz_destination ) +
-                    10 + 10 + strlen( PACKAGE_STRING ) +
-                    20 + 10 + 20 + 10 + strlen( psz_rtpmap ) );
-        sprintf( p_sys->psz_sdp,
-                 "v=0\r\n"
-                 "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n"
-                 "s=%s\r\n"
-                 "i=%s\r\n"
-                 "u=%s\r\n"
-                 "e=%s\r\n"
-                 "t=0 0\r\n" /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
-                 "a=tool:"PACKAGE_STRING"\r\n"
-                 "c=IN IP4 %s/%d\r\n"
-                 "m=video %d RTP/AVP %d\r\n"
-                 "a=rtpmap:%d %s\r\n",
-                 p_sys->i_sdp_id, p_sys->i_sdp_version,
-                 p_sys->psz_session_name,
-                 p_sys->psz_session_description,
-                 p_sys->psz_session_url,
-                 p_sys->psz_session_email,
-                 p_sys->psz_destination, p_sys->i_ttl,
-                 p_sys->i_port, p_sys->i_payload_type,
-                 p_sys->i_payload_type, psz_rtpmap );
+        if( net_AddressIsMulticast( (vlc_object_t *)p_stream, p_sys->psz_destination ) )
+        {
+            snprintf( psz_ttl, sizeof( psz_ttl ), "/%d", p_sys->i_ttl ? 
+                p_sys->i_ttl : config_GetInt( p_sout, "ttl" ) );
+            psz_ttl[sizeof( psz_ttl ) - 1] = '\0';
+        }
+        else
+        {
+            psz_ttl[0] = '\0'; 
+        }
+
+        asprintf( &p_sys->psz_sdp,
+                  "v=0\r\n"
+                  /* FIXME: source address not known :( */
+                  "o=- "I64Fd" %d IN IP%c %s\r\n"
+                  "s=%s\r\n"
+                  "i=%s\r\n"
+                  "u=%s\r\n"
+                  "e=%s\r\n"
+                  "t=0 0\r\n" /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
+                  "a=tool:"PACKAGE_STRING"\r\n"
+                  "c=IN IP%c %s%s\r\n"
+                  "m=video %d RTP/AVP %d\r\n"
+                  "a=rtpmap:%d %s\r\n",
+                  p_sys->i_sdp_id, p_sys->i_sdp_version,
+                  ipv, ipv == '6' ? "::1" : "127.0.0.1" /* FIXME */,
+                  p_sys->psz_session_name,
+                  p_sys->psz_session_description,
+                  p_sys->psz_session_url,
+                  p_sys->psz_session_email,
+                  ipv, p_sys->psz_destination, psz_ttl,
+                  p_sys->i_port, p_sys->i_payload_type,
+                  p_sys->i_payload_type, psz_rtpmap );
         fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
 
         /* create the rtp context */
@@ -565,6 +599,11 @@ static void Close( vlc_object_t * p_this )
         {
             block_Release( p_sys->packet );
         }
+        if( p_sys->b_export_sap )
+        {   
+            p_sys->p_mux = NULL;
+            SapSetup( p_stream );
+        }
     }
 
     while( p_sys->i_rtsp > 0 )
@@ -694,29 +733,34 @@ static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
            o= don't use the localhost address. use fully qualified domain name or IP4 address
            p= international phone number (pass via vars?)
            c= IP6 support
-           c= /ttl should only be added in case of multicast
            a= recvonly (missing)
            a= type:broadcast (missing)
            a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
            a= x-plgroup: (missing)
            RTP packets need to get the correct src IP address  */
-static char *SDPGenerate( sout_stream_t *p_stream, char *psz_destination, vlc_bool_t b_rtsp )
+static char *SDPGenerate( const sout_stream_t *p_stream,
+                          const char *psz_destination, vlc_bool_t b_rtsp )
 {
     sout_stream_sys_t *p_sys = p_stream->p_sys;
+    sout_instance_t  *p_sout = p_stream->p_sout;
     int i_size;
-    char *psz_sdp, *p;
+    char *psz_sdp, *p, ipv;
     int i;
 
-    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_sys->psz_session_name ) +
-             strlen( "i=*\r\n" ) + strlen( p_sys->psz_session_description ) +
-             strlen( "u=*\r\n" ) + strlen( p_sys->psz_session_url ) +
-             strlen( "e=*\r\n" ) + strlen( p_sys->psz_session_email ) +
-             strlen( "t=0 0\r\n" ) + /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
-             strlen( "a=tool:"PACKAGE_STRING"\r\n" ) +
-             strlen( "c=IN IP4 */*\r\n" ) + 20 + 10 +
-             strlen( psz_destination ? psz_destination : "0.0.0.0") ;
+    /* FIXME: breaks IP version check on unknown destination */
+    if( psz_destination == NULL )
+        psz_destination = "0.0.0.0";
+
+    i_size = sizeof( "v=0\r\n" ) +
+             sizeof( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
+             sizeof( "s=*\r\n" ) + strlen( p_sys->psz_session_name ) +
+             sizeof( "i=*\r\n" ) + strlen( p_sys->psz_session_description ) +
+             sizeof( "u=*\r\n" ) + strlen( p_sys->psz_session_url ) +
+             sizeof( "e=*\r\n" ) + strlen( p_sys->psz_session_email ) +
+             sizeof( "t=0 0\r\n" ) + /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
+             sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
+             sizeof( "c=IN IP4 */*\r\n" ) + 20 + 10 +
+             strlen( psz_destination ) ;
     for( i = 0; i < p_sys->i_es; i++ )
     {
         sout_stream_id_t *id = p_sys->es[i];
@@ -736,10 +780,13 @@ static char *SDPGenerate( sout_stream_t *p_stream, char *psz_destination, vlc_bo
         }
     }
 
+    ipv = ( strchr( psz_destination, ':' ) != NULL ) ? '6' : '4';
+
     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_sys->i_sdp_id, p_sys->i_sdp_version );
+    p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
+                  p_sys->i_sdp_id, p_sys->i_sdp_version,
+                  ipv, ipv == '6' ? "::" : "127.0.0.1" );
     if( *p_sys->psz_session_name )
         p += sprintf( p, "s=%s\r\n", p_sys->psz_session_name );
     if( *p_sys->psz_session_description )
@@ -752,8 +799,18 @@ static char *SDPGenerate( sout_stream_t *p_stream, char *psz_destination, vlc_bo
     p += sprintf( p, "t=0 0\r\n" ); /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
     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_sys->i_ttl );
+    p += sprintf( p, "c=IN IP%c %s", ipv, psz_destination );
+
+    if( net_AddressIsMulticast( (vlc_object_t *)p_stream, psz_destination ) )
+    {
+        /* Add the ttl if it is a multicast address */
+        p += sprintf( p, "/%d\r\n", p_sys->i_ttl ? p_sys->i_ttl :
+                config_GetInt( p_sout, "ttl" ) );
+    }
+    else
+    {
+        p += sprintf( p, "\r\n" );
+    }
 
     for( i = 0; i < p_sys->i_es; i++ )
     {
@@ -803,6 +860,7 @@ static int rtp_packetize_ac3  ( sout_stream_t *, sout_stream_id_t *, block_t * )
 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
+static int rtp_packetize_amr  ( sout_stream_t *, sout_stream_id_t *, block_t * );
 
 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
 {
@@ -871,19 +929,24 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
 
     if( p_sys->psz_destination )
     {
-        char access[100];
-        char url[strlen( p_sys->psz_destination ) + 1 + 12 + 1];
+        char access[17];
+        char url[NI_MAXHOST + 8];
 
         /* first try to create the access out */
         if( p_sys->i_ttl > 0 )
         {
-            sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
+            snprintf( access, sizeof( access ), "udp{raw,ttl=%d}",
+                      p_sys->i_ttl );
+            access[sizeof( access ) - 1] = '\0';
         }
         else
-        {
-            sprintf( access, "udp{raw}" );
-        }
-        sprintf( url, "%s:%d", p_sys->psz_destination, i_port );
+            strcpy( access, "udp{raw}" );
+
+        snprintf( url, sizeof( url ), (( p_sys->psz_destination[0] != '[' ) &&
+                 strchr( p_sys->psz_destination, ':' )) ? "[%s]:%d" : "%s:%d",
+                 p_sys->psz_destination, i_port );
+        url[sizeof( url ) - 1] = '\0';
+
         if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
         {
             msg_Err( p_stream, "cannot create the access out for %s://%s",
@@ -997,6 +1060,22 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
                      "IndexDeltaLength=3; Profile=1;", hexa );
             break;
         }
+        case VLC_FOURCC( 's', 'a', 'm', 'r' ):
+            id->i_payload_type = p_sys->i_payload_type++;
+            id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
+                                     "AMR/8000/2" : "AMR/8000" );
+            id->psz_fmtp = strdup( "octet-align=1" );
+            id->i_clock_rate = p_fmt->audio.i_rate;
+            id->pf_packetize = rtp_packetize_amr;
+            break; 
+        case VLC_FOURCC( 's', 'a', 'w', 'b' ):
+            id->i_payload_type = p_sys->i_payload_type++;
+            id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
+                                     "AMR-WB/16000/2" : "AMR-WB/16000" );
+            id->psz_fmtp = strdup( "octet-align=1" );
+            id->i_clock_rate = p_fmt->audio.i_rate;
+            id->pf_packetize = rtp_packetize_amr;
+            break; 
 
         default:
             msg_Err( p_stream, "cannot add this stream (unsupported "
@@ -1018,12 +1097,13 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
     id->i_timestamp_start = rand()&0xffffffff;
 
     id->i_mtu    = config_GetInt( p_stream, "mtu" );  /* XXX beuk */
-    if( id->i_mtu <= 16 )
+    if( id->i_mtu <= 16 + MTU_REDUCE )
     {
         /* better than nothing */
         id->i_mtu = 1500;
     }
-    msg_Dbg( p_stream, "using mtu=%d", id->i_mtu );
+    id->i_mtu -= MTU_REDUCE;
+    msg_Dbg( p_stream, "maximum RTP packet size: %d bytes", id->i_mtu );
 
     if( p_sys->p_rtsp_url )
     {
@@ -1031,7 +1111,7 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
 
         sprintf( psz_urlc, "%s/trackid=%d", p_sys->psz_rtsp_path, p_sys->i_es );
         fprintf( stderr, "rtsp: adding %s\n", psz_urlc );
-        id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL );
+        id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL, NULL );
 
         if( id->p_rtsp_url )
         {
@@ -1108,7 +1188,7 @@ static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
     if( id->rtsp_access ) free( id->rtsp_access );
 
     /* Update SDP (sap/file) */
-    if( p_sys->b_export_sap ) SapSetup( p_stream );
+    if( p_sys->b_export_sap && !p_sys->p_mux ) SapSetup( p_stream );
     if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
 
     free( id );
@@ -1153,7 +1233,7 @@ static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
     unsigned int    i_data  = p_buffer->i_buffer;
     unsigned int    i_max   = p_sys->i_mtu - 12;
 
-    int i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
+    unsigned i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
 
     while( i_data > 0 )
     {
@@ -1192,7 +1272,8 @@ static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
             p_sys->i_sequence++;
         }
 
-        i_size = __MIN( i_data, p_sys->i_mtu - p_sys->packet->i_buffer );
+        i_size = __MIN( i_data,
+                        (unsigned)(p_sys->i_mtu - p_sys->packet->i_buffer) );
 
         memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
                 p_data, i_size );
@@ -1233,8 +1314,7 @@ static int SapSetup( sout_stream_t *p_stream )
 {
     sout_stream_sys_t *p_sys = p_stream->p_sys;
     sout_instance_t   *p_sout = p_stream->p_sout;
-    announce_method_t *p_method = (announce_method_t *)
-                                  malloc(sizeof(announce_method_t));
+    announce_method_t *p_method = sout_AnnounceMethodCreate( METHOD_TYPE_SAP );
 
     /* Remove the previous session */
     if( p_sys->p_session != NULL)
@@ -1243,13 +1323,11 @@ static int SapSetup( sout_stream_t *p_stream )
         sout_AnnounceSessionDestroy( p_sys->p_session );
         p_sys->p_session = NULL;
     }
-    p_method->i_type = METHOD_TYPE_SAP;
-    p_method->psz_address = NULL; /* FIXME */
-    p_method->i_ip_version = 4; /* FIXME ! */
 
-    if( p_sys->i_es > 0 && p_sys->psz_sdp && *p_sys->psz_sdp )
+    if( ( p_sys->i_es > 0 || p_sys->p_mux ) && p_sys->psz_sdp && *p_sys->psz_sdp )
     {
         p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, p_sys->psz_sdp,
+                                                     p_sys->psz_destination,
                                                      p_method );
     }
 
@@ -1289,13 +1367,13 @@ static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
 {
     sout_stream_sys_t *p_sys = p_stream->p_sys;
 
-    p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port );
+    p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 80 );
     if( p_sys->p_httpd_host )
     {
         p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
                                              url->psz_path ? url->psz_path : "/",
                                              "application/sdp",
-                                             NULL, NULL,
+                                             NULL, NULL, NULL,
                                              HttpCallback, (void*)p_sys );
     }
     if( p_sys->p_httpd_file == NULL )
@@ -1393,7 +1471,7 @@ static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
     sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
              url->psz_host,  url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
 
-    p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL );
+    p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL, NULL );
     if( p_sys->p_rtsp_url == 0 )
     {
         return VLC_EGENERIC;
@@ -1435,7 +1513,7 @@ static int  RtspCallback( httpd_callback_sys_t *p_args,
             answer->psz_status = strdup( "OK" );
             httpd_MsgAdd( answer, "Content-type",  "%s", "application/sdp" );
 
-            answer->p_body = psz_sdp;
+            answer->p_body = (uint8_t *)psz_sdp;
             answer->i_body = strlen( psz_sdp );
             break;
         }
@@ -1542,6 +1620,7 @@ static int  RtspCallbackId( httpd_callback_sys_t *p_args,
 {
     sout_stream_id_t *id = (sout_stream_id_t*)p_args;
     sout_stream_t    *p_stream = id->p_stream;
+    sout_instance_t    *p_sout = p_stream->p_sout;
     sout_stream_sys_t *p_sys = p_stream->p_sys;
     char          *psz_session = NULL;
 
@@ -1578,23 +1657,22 @@ static int  RtspCallbackId( httpd_callback_sys_t *p_args,
                 }
                 httpd_MsgAdd( answer, "Transport",
                               "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
-                              id->psz_destination, id->i_port,id->i_port+1, p_sys->i_ttl );
+                              id->psz_destination, id->i_port,id->i_port+1,
+                              p_sys->i_ttl ? p_sys->i_ttl : 
+                              config_GetInt( p_sout, "ttl" ) );
             }
             else if( strstr( psz_transport, "unicast" ) && strstr( psz_transport, "client_port=" ) )
             {
                 int  i_port = atoi( strstr( psz_transport, "client_port=" ) + strlen("client_port=") );
-                char *ip    = httpd_ClientIP( cl );
-
-                char psz_access[100];
-                char psz_url[100];
+                char ip[NI_MAXNUMERICHOST], psz_access[17], psz_url[NI_MAXNUMERICHOST + 8];
 
                 sout_access_out_t *p_access;
 
                 rtsp_client_t *rtsp = NULL;
 
-                if( ip == NULL )
+                if( httpd_ClientIP( cl, ip ) == NULL )
                 {
-                    answer->i_status = 400;
+                    answer->i_status = 500;
                     answer->psz_status = strdup( "Internal server error" );
                     answer->i_body = 0;
                     answer->p_body = NULL;
@@ -1617,30 +1695,32 @@ static int  RtspCallbackId( httpd_callback_sys_t *p_args,
                     rtsp = RtspClientGet( p_stream, psz_session );
                     if( rtsp == NULL )
                     {
-                        /* FIXME right error code */
-                        answer->i_status = 400;
+                        answer->i_status = 454;
                         answer->psz_status = strdup( "Unknown session id" );
                         answer->i_body = 0;
                         answer->p_body = NULL;
-                        free( ip );
                         break;
                     }
                 }
 
                 /* first try to create the access out */
                 if( p_sys->i_ttl > 0 )
-                    sprintf( psz_access, "udp{raw,ttl=%d}", p_sys->i_ttl );
+                    snprintf( psz_access, sizeof( psz_access ),
+                              "udp{raw,ttl=%d}", p_sys->i_ttl );
                 else
-                    sprintf( psz_access, "udp{raw}" );
-                sprintf( psz_url, "%s:%d", ip, i_port );
-                free( ip );
-                
+                    strncpy( psz_access, "udp{raw}", sizeof( psz_access ) );
+                psz_access[sizeof( psz_access ) - 1] = '\0';
+
+                snprintf( psz_url, sizeof( psz_url ),
+                         ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
+                         ip, i_port );
+
                 if( ( p_access = sout_AccessOutNew( p_stream->p_sout, psz_access, psz_url ) ) == NULL )
                 {
                     msg_Err( p_stream, "cannot create the access out for %s://%s",
                              psz_access, psz_url );
-                    answer->i_status = 400;
-                    answer->psz_status = strdup( "Server internal error" );
+                    answer->i_status = 500;
+                    answer->psz_status = strdup( "Internal server error" );
                     answer->i_body = 0;
                     answer->p_body = NULL;
                     break;
@@ -1659,8 +1739,8 @@ static int  RtspCallbackId( httpd_callback_sys_t *p_args,
             }
             else /* TODO  strstr( psz_transport, "interleaved" ) ) */
             {
-                answer->i_status = 400;
-                answer->psz_status = strdup( "Bad Request" );
+                answer->i_status = 461;
+                answer->psz_status = strdup( "Unsupported Transport" );
                 answer->i_body = 0;
                 answer->p_body = NULL;
             }
@@ -2112,3 +2192,41 @@ static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
     return VLC_SUCCESS;
 }
 
+static int rtp_packetize_amr( sout_stream_t *p_stream, sout_stream_id_t *id,
+                              block_t *in )
+{
+    int     i_max   = id->i_mtu - 14; /* payload max in one packet */
+    int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
+
+    uint8_t *p_data = in->p_buffer;
+    int     i_data  = in->i_buffer;
+    int     i;
+
+    /* Only supports octet-aligned mode */
+    for( i = 0; i < i_count; i++ )
+    {
+        int           i_payload = __MIN( i_max, i_data );
+        block_t *out = block_New( p_stream, 14 + i_payload );
+
+        /* rtp common header */
+        rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
+                              (in->i_pts > 0 ? in->i_pts : in->i_dts) );
+        /* Payload header */
+        out->p_buffer[12] = 0xF0; /* CMR */
+        out->p_buffer[13] = 0x00; /* ToC */ /* FIXME: frame type */
+
+        /* FIXME: are we fed multiple frames ? */
+        memcpy( &out->p_buffer[14], p_data, i_payload );
+
+        out->i_buffer   = 14 + i_payload;
+        out->i_dts    = in->i_dts + i * in->i_length / i_count;
+        out->i_length = in->i_length / i_count;
+
+        rtp_packetize_send( id, out );
+
+        p_data += i_payload;
+        i_data -= i_payload;
+    }
+
+    return VLC_SUCCESS;
+}