#ifdef HAVE_UNISTD_H
# include <sys/types.h>
# include <unistd.h>
-# include <fcntl.h>
-# include <sys/stat.h>
+#endif
+#ifdef HAVE_ARPA_INET_H
+# include <arpa/inet.h>
#endif
#ifdef HAVE_LINUX_DCCP_H
# include <linux/dccp.h>
"This sends and receives RTCP packet multiplexed over the same port " \
"as RTP packets." )
+#define CACHING_TEXT N_("Caching value (ms)")
+#define CACHING_LONGTEXT N_( \
+ "Default caching value for outbound RTP streams. This " \
+ "value should be set in milliseconds." )
+
#define PROTO_TEXT N_("Transport protocol")
#define PROTO_LONGTEXT N_( \
"This selects which transport protocol to use for RTP." )
TTL_LONGTEXT, true )
add_bool( SOUT_CFG_PREFIX "rtcp-mux", false, NULL,
RTCP_MUX_TEXT, RTCP_MUX_LONGTEXT, false )
+ add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
+ CACHING_TEXT, CACHING_LONGTEXT, true )
#ifdef HAVE_SRTP
add_string( SOUT_CFG_PREFIX "key", "", NULL,
static const char *const ppsz_sout_options[] = {
"dst", "name", "port", "port-audio", "port-video", "*sdp", "ttl", "mux",
"sap", "description", "url", "email", "phone",
- "proto", "rtcp-mux", "key", "salt",
+ "proto", "rtcp-mux", "caching", "key", "salt",
"mp4a-latm", NULL
};
/* RTSP */
rtsp_stream_t *rtsp;
+ /* RTSP NPT and timestamp computations */
+ mtime_t i_npt_zero; /* when NPT=0 packet is sent */
+ int64_t i_pts_zero; /* predicts PTS of NPT=0 packet */
+ int64_t i_pts_offset; /* matches actual PTS to prediction */
+ vlc_mutex_t lock_ts;
+
/* */
char *psz_destination;
uint32_t payload_bitmap;
/* rtp field */
uint16_t i_sequence;
uint8_t i_payload_type;
+ bool b_ts_init;
+ uint32_t i_ts_offset;
uint8_t ssrc[4];
+ /* for rtsp */
+ uint16_t i_seq_sent_next;
+
/* for sdp */
const char *psz_enc;
char *psz_fmtp;
if( p_sys->i_ttl == -1 )
{
/* Normally, we should let the default hop limit up to the core,
- * but we have to know it to build our SDP properly, which is why
- * we ask the core. FIXME: broken when neither sout-rtp-ttl nor
- * ttl are set. */
- p_sys->i_ttl = config_GetInt( p_stream, "ttl" );
+ * but we have to know it to write our RTSP headers properly,
+ * which is why we ask the core. FIXME: broken when neither
+ * sout-rtp-ttl nor ttl are set. */
+ p_sys->i_ttl = var_InheritInteger( p_stream, "ttl" );
}
p_sys->b_latm = var_GetBool( p_stream, SOUT_CFG_PREFIX "mp4a-latm" );
+ /* NPT=0 time will be determined when we packetize the first packet
+ * (of any ES). But we want to be able to report rtptime in RTSP
+ * without waiting. So until then, we use an arbitrary reference
+ * PTS for timestamp computations, and then actual PTS will catch
+ * up using offsets. */
+ p_sys->i_npt_zero = VLC_TS_INVALID;
+ p_sys->i_pts_zero = mdate(); /* arbitrary value, could probably be
+ * random */
p_sys->payload_bitmap = 0;
p_sys->i_es = 0;
p_sys->es = NULL;
p_stream->p_sys = p_sys;
vlc_mutex_init( &p_sys->lock_sdp );
+ vlc_mutex_init( &p_sys->lock_ts );
vlc_mutex_init( &p_sys->lock_es );
psz = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "mux" );
RtspUnsetup( p_sys->rtsp );
vlc_mutex_destroy( &p_sys->lock_sdp );
+ vlc_mutex_destroy( &p_sys->lock_ts );
vlc_mutex_destroy( &p_sys->lock_es );
if( p_sys->p_httpd_file )
if( p_sys->p_mux != NULL )
{
sout_stream_id_t *id = p_sys->es[0];
- id->rtsp_id = RtspAddId( p_sys->rtsp, id, 0, GetDWBE( id->ssrc ),
+ id->rtsp_id = RtspAddId( p_sys->rtsp, id, GetDWBE( id->ssrc ),
p_sys->psz_destination, p_sys->i_ttl,
id->i_port, id->i_port + 1 );
}
if( rtsp_url != NULL )
sdp_AddAttribute ( &psz_sdp, "control", "%s", rtsp_url );
+ const char *proto = "RTP/AVP"; /* protocol */
+ if( rtsp_url == NULL )
+ {
+ switch( p_sys->proto )
+ {
+ case IPPROTO_UDP:
+ break;
+ case IPPROTO_TCP:
+ proto = "TCP/RTP/AVP";
+ break;
+ case IPPROTO_DCCP:
+ proto = "DCCP/RTP/AVP";
+ break;
+ case IPPROTO_UDPLITE:
+ return psz_sdp;
+ }
+ }
+
/* FIXME: locking?! */
for( i = 0; i < p_sys->i_es; i++ )
{
sout_stream_id_t *id = p_sys->es[i];
const char *mime_major; /* major MIME type */
- const char *proto = "RTP/AVP"; /* protocol */
switch( id->i_cat )
{
continue;
}
- if( rtsp_url == NULL )
- {
- switch( p_sys->proto )
- {
- case IPPROTO_UDP:
- break;
- case IPPROTO_TCP:
- proto = "TCP/RTP/AVP";
- break;
- case IPPROTO_DCCP:
- proto = "DCCP/RTP/AVP";
- break;
- case IPPROTO_UDPLITE:
- continue;
- }
- }
-
sdp_AddMedia( &psz_sdp, mime_major, proto, inclport * id->i_port,
id->i_payload_type, false, id->i_bitrate,
id->psz_enc, id->i_clock_rate, id->i_channels,
if( rtsp_url != NULL )
{
- assert( strlen( rtsp_url ) > 0 );
- bool addslash = ( rtsp_url[strlen( rtsp_url ) - 1] != '/' );
- sdp_AddAttribute ( &psz_sdp, "control",
- addslash ? "%s/trackID=%u" : "%strackID=%u",
- rtsp_url, i );
+ char *track_url = RtspAppendTrackPath( id->rtsp_id, rtsp_url );
+ if( track_url != NULL )
+ {
+ sdp_AddAttribute ( &psz_sdp, "control", "%s", track_url );
+ free( track_url );
+ }
}
else
{
id->i_mtu = 12 + (((id->i_mtu - 12) / bytes) * bytes);
}
+uint32_t rtp_compute_ts( const sout_stream_id_t *id, int64_t i_pts )
+{
+ /* NOTE: this plays nice with offsets because the calculations are
+ * linear. */
+ return i_pts * (int64_t)id->i_clock_rate / CLOCK_FREQ;
+}
+
/** Add an ES as a new RTP stream */
static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
{
vlc_rand_bytes (&id->i_sequence, sizeof (id->i_sequence));
vlc_rand_bytes (id->ssrc, sizeof (id->ssrc));
+ id->i_seq_sent_next = id->i_sequence;
+
id->psz_enc = NULL;
id->psz_fmtp = NULL;
id->i_clock_rate = 90000; /* most common case for video */
id->i_bitrate = 0;
}
- id->i_mtu = config_GetInt( p_stream, "mtu" );
+ id->i_mtu = var_InheritInteger( p_stream, "mtu" );
if( id->i_mtu <= 12 + 16 )
id->i_mtu = 576 - 20 - 8; /* pessimistic */
msg_Dbg( p_stream, "maximum RTP packet size: %d bytes", id->i_mtu );
msg_Err( p_stream, "cannot create RTP socket" );
goto error;
}
- rtp_add_sink( id, fd, p_sys->rtcp_mux );
+ /* Ignore any unexpected incoming packet (including RTCP-RR
+ * packets in case of rtcp-mux) */
+ setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0 },
+ sizeof (int));
+ rtp_add_sink( id, fd, p_sys->rtcp_mux, NULL );
}
}
case VLC_CODEC_MP4V:
{
- char hexa[2*p_fmt->i_extra +1];
-
id->psz_enc = "MP4V-ES";
id->pf_packetize = rtp_packetize_split;
if( p_fmt->i_extra > 0 )
{
+ char hexa[2*p_fmt->i_extra +1];
sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
if( asprintf( &id->psz_fmtp,
"profile-level-id=3; config=%s;", hexa ) == -1 )
net_SetCSCov( id->sinkv[0].rtp_fd, cscov, -1 );
#endif
+ vlc_mutex_lock( &p_sys->lock_ts );
+ id->b_ts_init = ( p_sys->i_npt_zero != VLC_TS_INVALID );
+ vlc_mutex_unlock( &p_sys->lock_ts );
+ if( id->b_ts_init )
+ id->i_ts_offset = rtp_compute_ts( id, p_sys->i_pts_offset );
+
if( p_sys->rtsp != NULL )
- id->rtsp_id = RtspAddId( p_sys->rtsp, id, p_sys->i_es,
+ id->rtsp_id = RtspAddId( p_sys->rtsp, id,
GetDWBE( id->ssrc ),
p_sys->psz_destination,
p_sys->i_ttl, id->i_port, id->i_port + 1 );
deadv[deadc++] = id->sinkv[i].rtp_fd;
}
+ id->i_seq_sent_next = ntohs(((uint16_t *) out->p_buffer)[1]) + 1;
vlc_mutex_unlock( &id->lock_sink );
block_Release( out );
if( fd == -1 )
continue;
int canc = vlc_savecancel( );
- rtp_add_sink( id, fd, true );
+ rtp_add_sink( id, fd, true, NULL );
vlc_restorecancel( canc );
}
}
-int rtp_add_sink( sout_stream_id_t *id, int fd, bool rtcp_mux )
+int rtp_add_sink( sout_stream_id_t *id, int fd, bool rtcp_mux, uint16_t *seq )
{
rtp_sink_t sink = { fd, NULL };
sink.rtcp = OpenRTCP( VLC_OBJECT( id->p_stream ), fd, IPPROTO_UDP,
vlc_mutex_lock( &id->lock_sink );
INSERT_ELEM( id->sinkv, id->sinkc, id->sinkc, sink );
+ if( seq != NULL )
+ *seq = id->i_seq_sent_next;
vlc_mutex_unlock( &id->lock_sink );
return VLC_SUCCESS;
}
net_Close( sink.rtp_fd );
}
-uint16_t rtp_get_seq( const sout_stream_id_t *id )
+uint16_t rtp_get_seq( sout_stream_id_t *id )
{
- /* This will return values for the next packet.
- * Accounting for caching would not be totally trivial. */
- return id->i_sequence;
+ /* This will return values for the next packet. */
+ uint16_t seq;
+
+ vlc_mutex_lock( &id->lock_sink );
+ seq = id->i_seq_sent_next;
+ vlc_mutex_unlock( &id->lock_sink );
+
+ return seq;
}
-/* FIXME: this is pretty bad - if we remove and then insert an ES
- * the number will get unsynched from inside RTSP */
-unsigned rtp_get_num( const sout_stream_id_t *id )
+/* Return a timestamp corresponding to packets being sent now, and that
+ * can be passed to rtp_compute_ts() to get rtptime values for each ES. */
+int64_t rtp_get_ts( const sout_stream_t *p_stream )
{
- sout_stream_sys_t *p_sys = id->p_stream->p_sys;
- int i;
+ sout_stream_sys_t *p_sys = p_stream->p_sys;
+ mtime_t i_npt_zero;
+ vlc_mutex_lock( &p_sys->lock_ts );
+ i_npt_zero = p_sys->i_npt_zero;
+ vlc_mutex_unlock( &p_sys->lock_ts );
- vlc_mutex_lock( &p_sys->lock_es );
- for( i = 0; i < p_sys->i_es; i++ )
- {
- if( id == p_sys->es[i] )
- break;
- }
- vlc_mutex_unlock( &p_sys->lock_es );
+ if( i_npt_zero == VLC_TS_INVALID )
+ return p_sys->i_pts_zero;
- return i;
-}
+ mtime_t now = mdate();
+ if( now < i_npt_zero )
+ return p_sys->i_pts_zero;
+ return p_sys->i_pts_zero + (now - i_npt_zero);
+}
void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
int b_marker, int64_t i_pts )
{
- uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / CLOCK_FREQ;
+ if( !id->b_ts_init )
+ {
+ sout_stream_sys_t *p_sys = id->p_stream->p_sys;
+ vlc_mutex_lock( &p_sys->lock_ts );
+ if( p_sys->i_npt_zero == VLC_TS_INVALID )
+ {
+ /* This is the first packet of any ES. We initialize the
+ * NPT=0 time reference, and the offset to match the
+ * arbitrary PTS reference. */
+ p_sys->i_npt_zero = i_pts + id->i_caching;
+ p_sys->i_pts_offset = p_sys->i_pts_zero - i_pts;
+ }
+ vlc_mutex_unlock( &p_sys->lock_ts );
+
+ /* And in any case this is the first packet of this ES, so we
+ * initialize the offset for this ES. */
+ id->i_ts_offset = rtp_compute_ts( id, p_sys->i_pts_offset );
+ id->b_ts_init = true;
+ }
+
+ uint32_t i_timestamp = rtp_compute_ts( id, i_pts ) + id->i_ts_offset;
out->p_buffer[0] = 0x80;
out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;