#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>
/* 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 */
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 );
}
{
inclport = 0;
+ /* Check against URL format rtsp://[<ipv6>]:<port>/<path> */
+ bool ipv6 = rtsp_url != NULL && strlen( rtsp_url ) > 7
+ && rtsp_url[7] == '[';
+
/* Dummy destination address for RTSP */
- memset (&dst, 0, sizeof( struct sockaddr_in ) );
- dst.ss_family = AF_INET;
+ dstlen = ipv6 ? sizeof( struct sockaddr_in6 )
+ : sizeof( struct sockaddr_in );
+ memset (&dst, 0, dstlen);
+ dst.ss_family = ipv6 ? AF_INET6 : AF_INET;
#ifdef HAVE_SA_LEN
- dst.ss_len =
+ dst.ss_len = dstlen;
#endif
- dstlen = sizeof( struct sockaddr_in );
}
psz_sdp = vlc_sdp_Start( VLC_OBJECT( p_stream ), SOUT_CFG_PREFIX,
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 )
{
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 );
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 );
if( id->rtsp_id )
RtspDelId( p_sys->rtsp, id->rtsp_id );
- if( id->sinkc > 0 )
- rtp_del_sink( id, id->sinkv[0].rtp_fd ); /* sink for explicit dst= */
if( id->listen.fd != NULL )
{
vlc_cancel( id->listen.thread );
vlc_join( id->listen.thread, NULL );
net_ListenClose( id->listen.fd );
}
+ /* Delete remaining sinks (incoming connections or explicit
+ * outgoing dst=) */
+ while( id->sinkc > 0 )
+ rtp_del_sink( id, id->sinkv[0].rtp_fd );
#ifdef HAVE_SRTP
if( id->srtp != NULL )
srtp_destroy( id->srtp );
if( p_sys->b_export_sap && !p_sys->p_mux ) SapSetup( p_stream );
if( p_sys->psz_sdp_file != NULL ) FileSetup( p_stream );
- vlc_object_detach( id );
vlc_object_release( id );
return VLC_SUCCESS;
}
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;