X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fstream_out%2Frtp.c;h=1820846d16579b602e373d3b0da241f0c1057700;hb=a1a819ef409cd7dc8c84d932c4db34d481f67145;hp=19ed74197d9aa4da8611d561b8509ff3b34c6146;hpb=e6a2e27bbb326744bbcfe0e464c32068dc41cbb4;p=vlc diff --git a/modules/stream_out/rtp.c b/modules/stream_out/rtp.c index 19ed74197d..1820846d16 100644 --- a/modules/stream_out/rtp.c +++ b/modules/stream_out/rtp.c @@ -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." ) @@ -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,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 ) ) @@ -407,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; } @@ -419,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", @@ -428,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 = @@ -465,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 */ @@ -566,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 ) @@ -695,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]; @@ -737,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 ) @@ -753,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++ ) { @@ -873,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", @@ -1036,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 ) { @@ -1126,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 ); @@ -1171,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 ) { @@ -1210,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 ); @@ -1251,9 +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; - /* FIXME: use sout_AnnounceMethodCreate */ - 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) @@ -1262,12 +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 */ - 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 ); } @@ -1307,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, @@ -1453,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; } @@ -1560,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; @@ -1596,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; @@ -1635,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; @@ -1677,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; }