Commit 10b3709f authored by Laurent Aimar's avatar Laurent Aimar

* rtp: - added TS/PS support. (ex: #rtp{dst=ip,port=port,mux=ts} )

       PS will work if there is only one stream as the muxer doesn't set
       correctly dts/length for muxed packets. (so pretty useless ;)
        - added mpeg1/2 video. (how overcomplicated is rfc2250),
       tested with mplayer+live.com (but I'm not sure that my ES parsing
       is ok.)
        - btw, there is a ttl option.
parent 0905f6ee
......@@ -2,7 +2,7 @@
* rtp.c
*****************************************************************************
* Copyright (C) 2003 VideoLAN
* $Id: rtp.c,v 1.2 2003/11/01 00:15:49 fenrir Exp $
* $Id: rtp.c,v 1.3 2003/11/01 04:17:43 fenrir Exp $
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
*
......@@ -62,11 +62,18 @@ struct sout_stream_sys_t
int i_port;
int i_ttl;
/* when need to use a private one */
/* when need to use a private one or when using muxer */
int i_payload_type;
/* in case we do TS/PS over rtp */
sout_mux_t *p_mux;
sout_access_out_t *p_access;
int i_mtu;
sout_access_out_t *p_grab;
uint16_t i_sequence;
uint32_t i_timestamp_start;
uint8_t ssrc[4];
sout_buffer_t *packet;
/* */
int i_es;
......@@ -94,12 +101,12 @@ struct sout_stream_id_t
/* Packetizer specific fields */
pf_rtp_packetizer_t pf_packetize;
int i_mtu;
sout_buffer_t *packet;
/* for sending the packets */
sout_access_out_t *p_access;
sout_input_t *p_input;
};
static int AccessOutGrabberWrite( sout_access_out_t *, sout_buffer_t * );
/*****************************************************************************
* Open:
......@@ -107,11 +114,11 @@ struct sout_stream_id_t
static int Open( vlc_object_t *p_this )
{
sout_stream_t *p_stream = (sout_stream_t*)p_this;
sout_instance_t *p_sout = p_stream->p_sout;
sout_stream_sys_t *p_sys;
char *val;
p_sys = malloc( sizeof( sout_stream_sys_t ) );
p_sys->psz_destination = sout_cfg_find_value( p_stream->p_cfg, "dst" );
if( ( val = sout_cfg_find_value( p_stream->p_cfg, "port" ) ) )
......@@ -132,15 +139,7 @@ static int Open( vlc_object_t *p_this )
{
p_sys->i_ttl = config_GetInt( p_stream, "ttl" );
}
if( ( val = sout_cfg_find_value( p_stream->p_cfg, "mux" ) ) )
{
/* p_sout->i_preheader = __MAX( p_sout->i_preheader, p_mux->i_preheader ); */
msg_Err( p_stream, "muxer not yet supported" );
free( p_sys );
return VLC_EGENERIC;
}
p_sys->p_mux = NULL;
p_sys->i_payload_type = 96;
p_sys->i_es = 0;
p_sys->es = NULL;
......@@ -148,7 +147,109 @@ static int Open( vlc_object_t *p_this )
p_sys->i_sdp_id = mdate();
p_sys->i_sdp_version = 1;
p_sys->psz_sdp = NULL;
if( ( val = sout_cfg_find_value( p_stream->p_cfg, "mux" ) ) )
{
sout_access_out_t *p_grab;
char *psz_rtpmap;
char access[100];
char url[strlen( p_sys->psz_destination ) + 1 + 12 + 1];
/* Check muxer type */
if( !strncasecmp( val, "ps", 2 ) || !strncasecmp( val, "mpeg1", 5 ) )
{
psz_rtpmap = "MP2P/90000";
}
else if( !strncasecmp( val, "ts", 2 ) )
{
psz_rtpmap = "MP2T/90000";
p_sys->i_payload_type = 33;
}
else
{
msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
return VLC_EGENERIC;
}
/* create the access out */
if( p_sys->i_ttl > 0 )
{
sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
}
else
{
sprintf( access, "udp{raw}" );
}
sprintf( url, "%s:%d", p_sys->psz_destination, p_sys->i_port );
if( ( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
{
msg_Err( p_stream, "cannot create the access out for %s://%s",
access, url );
free( p_sys );
return VLC_EGENERIC;
}
p_sys->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beurk */
if( p_sys->i_mtu <= 16 )
{
/* better than nothing */
p_sys->i_mtu = 1500;
}
/* the access out grabber TODO export it as sout_AccessOutGrabberNew */
p_grab = p_sys->p_grab = vlc_object_create( p_sout, sizeof( sout_access_out_t ) );
p_grab->p_module = NULL;
p_grab->p_sout = p_sout;
p_grab->psz_access = strdup( "grab" );
p_grab->p_cfg = NULL;
p_grab->psz_name = strdup( "" );
p_grab->p_sys = (sout_access_out_sys_t*)p_stream;
p_grab->pf_seek = NULL;
p_grab->pf_write = AccessOutGrabberWrite;
/* the muxer */
if( ( p_sys->p_mux = sout_MuxNew( p_sout, val, p_sys->p_grab ) ) == NULL )
{
msg_Err( p_stream, "cannot create the muxer (%s)", val );
sout_AccessOutDelete( p_sys->p_grab );
sout_AccessOutDelete( p_sys->p_access );
free( p_sys );
return VLC_EGENERIC;
}
p_sout->i_preheader = __MAX( p_sout->i_preheader, p_sys->p_mux->i_preheader );
/* create the SDP only once */
p_sys->psz_sdp = malloc( 200 + 20 + 10 + strlen( p_sys->psz_destination ) + 10 + 10 + 10 + 10 + strlen( psz_rtpmap ) );
sprintf( p_sys->psz_sdp,
"v=0\n"
"o=- "I64Fd" %d IN IP4 127.0.0.1\n"
"s=NONE\n"
"c=IN IP4 %s/%d\n"
"m=video %d RTP/AVP %d\n"
"a=rtpmap:%d %s\n",
p_sys->i_sdp_id, p_sys->i_sdp_version,
p_sys->psz_destination, p_sys->i_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 */
p_sys->ssrc[0] = rand()&0xff;
p_sys->ssrc[1] = rand()&0xff;
p_sys->ssrc[2] = rand()&0xff;
p_sys->ssrc[3] = rand()&0xff;
p_sys->i_sequence = rand()&0xffff;
p_sys->i_timestamp_start = rand()&0xffffffff;
p_sys->packet = NULL;
}
else
{
p_sys->p_mux = NULL;
p_sys->p_access = NULL;
p_sys->p_grab = NULL;
}
p_stream->pf_add = Add;
p_stream->pf_del = Del;
......@@ -167,6 +268,17 @@ static void Close( vlc_object_t * p_this )
sout_stream_t *p_stream = (sout_stream_t*)p_this;
sout_stream_sys_t *p_sys = p_stream->p_sys;
if( p_sys->p_mux )
{
sout_MuxDelete( p_sys->p_mux );
sout_AccessOutDelete( p_sys->p_access );
sout_AccessOutDelete( p_sys->p_grab );
if( p_sys->packet )
{
sout_BufferDelete( p_stream->p_sout, p_sys->packet );
}
}
if( p_sys->psz_sdp )
{
free( p_sys->psz_sdp );
......@@ -243,7 +355,11 @@ static void SDPGenerate( sout_stream_t *p_stream )
p = p_sys->psz_sdp;
p_sys->psz_sdp = psz_sdp; /* I hope this avoid a lock (when we export it by http) */
if( p )
{
free( p );
}
p_sys->i_sdp_version++;
fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
}
......@@ -253,6 +369,7 @@ static void SDPGenerate( sout_stream_t *p_stream )
*****************************************************************************/
static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
......@@ -275,11 +392,28 @@ static sout_stream_id_t * Add ( sout_stream_t *p_stream, sout_format_t *p_f
sout_instance_t *p_sout = p_stream->p_sout;
sout_stream_sys_t *p_sys = p_stream->p_sys;
sout_stream_id_t *id;
sout_access_out_t *p_access = NULL;
sout_access_out_t *p_access;
char access[100];
char url[strlen( p_sys->psz_destination ) + 1 + 12 + 1];
if( p_sys->p_mux != NULL )
{
sout_input_t *p_input = NULL;
if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
{
msg_Err( p_stream, "cannot add this stream to the muxer" );
return NULL;
}
id = malloc( sizeof( sout_stream_id_t ) );
id->p_access = NULL;
id->p_input = p_input;
id->pf_packetize= NULL;
return id;
}
/* first try to create the access out */
if( p_sys->i_ttl > 0 )
{
......@@ -292,7 +426,7 @@ static sout_stream_id_t * Add ( sout_stream_t *p_stream, sout_format_t *p_f
sprintf( url, "%s:%d", p_sys->psz_destination, p_sys->i_port );
if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
{
msg_Err( p_stream, "cannot create the access out for %s:%s",
msg_Err( p_stream, "cannot create the access out for %s://%s",
access, url );
return NULL;
}
......@@ -300,6 +434,7 @@ static sout_stream_id_t * Add ( sout_stream_t *p_stream, sout_format_t *p_f
/* not create the rtp specific stuff */
id = malloc( sizeof( sout_stream_id_t ) );
id->p_access = p_access;
id->p_input = NULL;
id->psz_rtpmap = NULL;
id->psz_fmtp = NULL;
id->psz_destination = strdup( p_sys->psz_destination );
......@@ -313,6 +448,12 @@ static sout_stream_id_t * Add ( sout_stream_t *p_stream, sout_format_t *p_f
id->psz_rtpmap = strdup( "MPA/90000" );
id->pf_packetize = rtp_packetize_mpa;
break;
case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
id->i_payload_type = 32;
id->i_clock_rate = 90000;
id->psz_rtpmap = strdup( "MPV/90000" );
id->pf_packetize = rtp_packetize_mpv;
break;
case VLC_FOURCC( 'a', '5', '2', ' ' ):
id->i_payload_type = p_sys->i_payload_type++;
id->i_clock_rate = 90000;
......@@ -373,15 +514,18 @@ static sout_stream_id_t * Add ( sout_stream_t *p_stream, sout_format_t *p_f
/* better than nothing */
id->i_mtu = 1500;
}
id->packet = NULL;
msg_Dbg( p_stream, "access out %s:%s mtu=%d", access, url, id->i_mtu );
/* Update p_sys context */
/* update port used (2 -> 1 rtp, 1 rtcp )*/
p_sys->i_port += 2;
TAB_APPEND( p_sys->i_es, p_sys->es, id );
if( p_sys->p_mux == NULL )
{
p_sys->i_port += 2;
SDPGenerate( p_stream );
}
return id;
}
......@@ -391,10 +535,8 @@ static int Del ( sout_stream_t *p_stream, sout_stream_id_t *id )
TAB_REMOVE( p_sys->i_es, p_sys->es, id );
if( id->packet )
if( id->p_access )
{
sout_BufferDelete( p_stream->p_sout, id->packet );
}
if( id->psz_rtpmap )
{
free( id->psz_rtpmap );
......@@ -403,7 +545,13 @@ static int Del ( sout_stream_t *p_stream, sout_stream_id_t *id )
{
free( id->psz_fmtp );
}
free( id->psz_destination );
sout_AccessOutDelete( id->p_access );
}
else if( id->p_input )
{
sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
}
free( id );
return VLC_SUCCESS;
}
......@@ -412,6 +560,12 @@ static int Send ( sout_stream_t *p_stream, sout_stream_id_t *id, sout_bu
{
sout_buffer_t *p_next;
if( p_stream->p_sys->p_mux )
{
sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
}
else
{
while( p_buffer )
{
p_next = p_buffer->p_next;
......@@ -422,9 +576,96 @@ static int Send ( sout_stream_t *p_stream, sout_stream_id_t *id, sout_bu
sout_BufferDelete( p_stream->p_sout, p_buffer );
p_buffer = p_next;
}
}
return VLC_SUCCESS;
}
static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream, sout_buffer_t *p_buffer )
{
sout_stream_sys_t *p_sys = p_stream->p_sys;
int64_t i_dts = p_buffer->i_dts;
uint32_t i_timestamp = i_dts * 9 / 100;
uint8_t *p_data = p_buffer->p_buffer;
unsigned int i_data = p_buffer->i_size;
unsigned int i_max = p_sys->i_mtu - 12;
int i_packet = ( p_buffer->i_size + i_max - 1 ) / i_max;
while( i_data > 0 )
{
unsigned int i_size;
/* output complete packet */
if( p_sys->packet &&
p_sys->packet->i_size + i_data > i_max )
{
sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
p_sys->packet = NULL;
}
if( p_sys->packet == NULL )
{
/* allocate a new packet */
p_sys->packet = sout_BufferNew( p_stream->p_sout, p_sys->i_mtu );
p_sys->packet->p_buffer[ 0] = 0x80;
p_sys->packet->p_buffer[ 1] = p_sys->i_payload_type;
p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence )&0xff;
p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
p_sys->packet->p_buffer[ 6] = ( i_timestamp >> 8 )&0xff;
p_sys->packet->p_buffer[ 7] = ( i_timestamp )&0xff;
p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
p_sys->packet->i_size = 12;
p_sys->packet->i_dts = i_dts;
p_sys->packet->i_length = p_buffer->i_length / i_packet;
i_dts += p_sys->packet->i_length;
p_sys->i_sequence++;
}
i_size = __MIN( i_data, p_sys->i_mtu - p_sys->packet->i_size );
memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_size],
p_data,
i_size );
p_sys->packet->i_size += i_size;
p_data += i_size;
i_data -= i_size;
}
return VLC_SUCCESS;
}
static int AccessOutGrabberWrite( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
{
sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
fprintf( stderr, "received buffer size=%d\n", p_buffer->i_size );
while( p_buffer )
{
sout_buffer_t *p_next;
AccessOutGrabberWriteBuffer( p_stream, p_buffer );
p_next = p_buffer->p_next;
sout_BufferDelete( p_access->p_sout, p_buffer );
p_buffer = p_next;
}
return VLC_SUCCESS;
}
static void rtp_packetize_common( sout_stream_id_t *id, sout_buffer_t *out, int b_marker, int64_t i_pts )
{
uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / (int64_t)1000000;
......@@ -484,6 +725,105 @@ static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id, sou
return VLC_SUCCESS;
}
/* rfc2250 */
static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id, sout_buffer_t *in )
{
int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
int i_count = ( in->i_size + i_max - 1 ) / i_max;
uint8_t *p_data = in->p_buffer;
int i_data = in->i_size;
int i;
int b_sequence_start = 0;
int i_temporal_ref = 0;
int i_picture_coding_type = 0;
int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
int b_start_slice = 0;
/* preparse this packet to get some info */
if( in->i_size > 4 )
{
uint8_t *p = p_data;
int i_rest = in->i_size;
for( ;; )
{
while( i_rest > 4 &&
( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
{
p++;
i_rest--;
}
if( i_rest <= 4 )
{
break;
}
p += 3;
i_rest -= 4;
if( *p == 0xb3 )
{
/* sequence start code */
b_sequence_start = 1;
}
else if( *p == 0x00 && i_rest >= 4 )
{
/* picture */
i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
i_picture_coding_type = (p[2] >> 3)&0x07;
if( i_rest > 4 && ( i_picture_coding_type == 2 || i_picture_coding_type == 3 ) )
{
i_ffc = (p[3] >> 2)&0x01;
i_bfc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
if( i_picture_coding_type == 3 )
{
i_fbv = (p[4]>>6)&0x01;
i_bfc = (p[4]>>3)&0x07;
}
}
}
else if( *p <= 0xaf )
{
b_start_slice = 1;
}
}
}
for( i = 0; i < i_count; i++ )
{
int i_payload = __MIN( i_max, i_data );
sout_buffer_t *out = sout_BufferNew( p_stream->p_sout, 16 + i_payload );
uint32_t h = ( i_temporal_ref << 16 )|
( b_sequence_start << 13 )|
( b_start_slice << 12 )|
( i == i_count - 1 ? 1 << 11 : 0 )|
( i_picture_coding_type << 8 )|
( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
/* rtp common header */
rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
/* MBZ:5 T:1 TR:10 AN:1 N:1 S:1 B:1 E:1 P:3 FBV:1 BFC:3 FFV:1 FFC:3 */
out->p_buffer[12] = ( h >> 24 )&0xff;
out->p_buffer[13] = ( h >> 16 )&0xff;
out->p_buffer[14] = ( h >> 8 )&0xff;
out->p_buffer[15] = ( h )&0xff;
memcpy( &out->p_buffer[16], p_data, i_payload );
out->i_size = 16 + i_payload;
out->i_dts = in->i_dts + i * in->i_length / i_count;
out->i_length = in->i_length / i_count;
sout_AccessOutWrite( id->p_access, out );
p_data += i_payload;
i_data -= i_payload;
}
return VLC_SUCCESS;
}
static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id, sout_buffer_t *in )
{
int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
......@@ -589,4 +929,3 @@ static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id, so
return VLC_SUCCESS;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment