]> git.sesse.net Git - vlc/blobdiff - modules/stream_out/rtp.c
FSF address change.
[vlc] / modules / stream_out / rtp.c
index 26c5bd7fdf3e3d618cb1eea9e1ec7bf100d4bf02..1ae12e7d9829a49773a5054b1d29378c36fe069f 100644 (file)
@@ -18,7 +18,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.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -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,9 +77,9 @@
 #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 (TTL)")
 #define TTL_LONGTEXT N_( \
-    "Allows you to specify the time to live for the output stream." )
+    "Allows you to specify the Time-To-Live for the output stream." )
 
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
@@ -107,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 );
@@ -352,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;
@@ -389,11 +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 psz_ttl[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 ) )
@@ -408,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;
         }
 
@@ -420,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",
@@ -429,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 =
@@ -471,42 +497,39 @@ static int Open( vlc_object_t *p_this )
            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 ) );
-
         if( net_AddressIsMulticast( (vlc_object_t *)p_stream, p_sys->psz_destination ) )
         {
-            sprintf( psz_ttl, "/%d", p_sys->i_ttl );
+            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'; 
         }
 
-        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%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,
-                 p_sys->psz_session_name,
-                 p_sys->psz_session_description,
-                 p_sys->psz_session_url,
-                 p_sys->psz_session_email,
-                 p_sys->psz_destination, psz_ttl,
-                 p_sys->i_port, p_sys->i_payload_type,
-                 p_sys->i_payload_type, psz_rtpmap );
+        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 */
@@ -576,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 )
@@ -710,23 +738,29 @@ static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
            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];
@@ -746,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 )
@@ -762,12 +799,13 @@ 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", psz_destination ? psz_destination : "0.0.0.0" );
+    p += sprintf( p, "c=IN IP%c %s", ipv, psz_destination );
 
-    if( net_AddressIsMulticast( (vlc_object_t *)p_stream, psz_destination ? psz_destination : "0.0.0.0" ) )
+    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 += sprintf( p, "/%d\r\n", p_sys->i_ttl ? p_sys->i_ttl :
+                config_GetInt( p_sout, "ttl" ) );
     }
     else
     {
@@ -891,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",
@@ -1054,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 )
     {
@@ -1144,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 );
@@ -1189,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 )
     {
@@ -1228,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 );
@@ -1282,6 +1327,7 @@ static int SapSetup( sout_stream_t *p_stream )
     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 );
     }
 
@@ -1321,7 +1367,7 @@ 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,
@@ -1467,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;
         }
@@ -1574,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;
 
@@ -1610,21 +1657,20 @@ 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 = 500;
                     answer->psz_status = strdup( "Internal server error" );
@@ -1653,19 +1699,22 @@ static int  RtspCallbackId( httpd_callback_sys_t *p_args,
                         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",