Commit 9160f1e0 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Cleanup RTCP SR support a little bit

parent 01f85e92
...@@ -171,6 +171,17 @@ static void ThreadWrite( vlc_object_t * ); ...@@ -171,6 +171,17 @@ static void ThreadWrite( vlc_object_t * );
static block_t *NewUDPPacket( sout_access_out_t *, mtime_t ); static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
static const char *MakeRandMulticast (int family, char *buf, size_t buflen); static const char *MakeRandMulticast (int family, char *buf, size_t buflen);
typedef struct rtcp_sender_t
{
size_t length; /* RTCP packet length */
uint8_t payload[28 + 8 + (2 * 257)];
int handle; /* RTCP socket handler */
uint32_t packets; /* RTP packets sent */
uint32_t bytes; /* RTP bytes sent */
unsigned counter; /* RTP packets sent since last RTCP packet */
} rtcp_sender_t;
typedef struct sout_access_thread_t typedef struct sout_access_thread_t
{ {
VLC_COMMON_MEMBERS VLC_COMMON_MEMBERS
...@@ -186,12 +197,7 @@ typedef struct sout_access_thread_t ...@@ -186,12 +197,7 @@ typedef struct sout_access_thread_t
block_fifo_t *p_empty_blocks; block_fifo_t *p_empty_blocks;
uint32_t sent_pkts; rtcp_sender_t rtcp;
uint32_t sent_bytes;
size_t rtcp_size;
int rtcp_handle;
uint8_t rtcp_data[28 + 8 + (2 * 257)];
} sout_access_thread_t; } sout_access_thread_t;
struct sout_access_out_sys_t struct sout_access_out_sys_t
...@@ -212,9 +218,10 @@ struct sout_access_out_sys_t ...@@ -212,9 +218,10 @@ struct sout_access_out_sys_t
#define DEFAULT_PORT 1234 #define DEFAULT_PORT 1234
#define RTP_HEADER_LENGTH 12 #define RTP_HEADER_LENGTH 12
static int OpenRTCP (sout_access_out_t *obj, int proto, uint16_t dport); static int OpenRTCP (vlc_object_t *obj, rtcp_sender_t *rtcp, int rtp_fd,
static void SendRTCP (sout_access_thread_t *obj, uint32_t timestamp); int proto, uint16_t dport);
static void CloseRTCP (sout_access_thread_t *obj); static void SendRTCP (rtcp_sender_t *obj, const block_t *rtp);
static void CloseRTCP (rtcp_sender_t *obj);
/***************************************************************************** /*****************************************************************************
* Open: open the file * Open: open the file
...@@ -382,7 +389,8 @@ static int Open( vlc_object_t *p_this ) ...@@ -382,7 +389,8 @@ static int Open( vlc_object_t *p_this )
p_sys->i_sequence_number = rand()&0xffff; p_sys->i_sequence_number = rand()&0xffff;
p_sys->i_ssrc = rand()&0xffffffff; p_sys->i_ssrc = rand()&0xffffffff;
if (i_rtcp_port && OpenRTCP (p_access, proto, i_rtcp_port)) if (i_rtcp_port && OpenRTCP (VLC_OBJECT (p_access), &p_sys->p_thread->rtcp,
i_handle, proto, i_rtcp_port))
{ {
msg_Err (p_access, "cannot initialize RTCP sender"); msg_Err (p_access, "cannot initialize RTCP sender");
net_Close (i_handle); net_Close (i_handle);
...@@ -441,7 +449,7 @@ static void Close( vlc_object_t * p_this ) ...@@ -441,7 +449,7 @@ static void Close( vlc_object_t * p_this )
if( p_sys->p_buffer ) block_Release( p_sys->p_buffer ); if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
net_Close( p_sys->p_thread->i_handle ); net_Close( p_sys->p_thread->i_handle );
CloseRTCP (p_sys->p_thread); CloseRTCP (&p_sys->p_thread->rtcp);
vlc_object_detach( p_sys->p_thread ); vlc_object_detach( p_sys->p_thread );
vlc_object_destroy( p_sys->p_thread ); vlc_object_destroy( p_sys->p_thread );
...@@ -629,7 +637,6 @@ static void ThreadWrite( vlc_object_t *p_this ) ...@@ -629,7 +637,6 @@ static void ThreadWrite( vlc_object_t *p_this )
char strerror_buf[WINSOCK_STRERROR_SIZE]; char strerror_buf[WINSOCK_STRERROR_SIZE];
# define strerror( x ) winsock_strerror( strerror_buf ) # define strerror( x ) winsock_strerror( strerror_buf )
#endif #endif
size_t rtcp_counter = 0;
while( !p_thread->b_die ) while( !p_thread->b_die )
{ {
...@@ -684,12 +691,6 @@ static void ThreadWrite( vlc_object_t *p_this ) ...@@ -684,12 +691,6 @@ static void ThreadWrite( vlc_object_t *p_this )
{ {
msg_Warn( p_thread, "send error: %s", strerror(errno) ); msg_Warn( p_thread, "send error: %s", strerror(errno) );
} }
else
{
p_thread->sent_pkts++;
p_thread->sent_bytes += val;
rtcp_counter += val;
}
if( i_dropped_packets ) if( i_dropped_packets )
{ {
...@@ -706,14 +707,7 @@ static void ThreadWrite( vlc_object_t *p_this ) ...@@ -706,14 +707,7 @@ static void ThreadWrite( vlc_object_t *p_this )
} }
#endif #endif
if ((p_thread->rtcp_handle != -1) && (p_pk->i_buffer >= 8)) SendRTCP (&p_thread->rtcp, p_pk);
{ // 1.25% rate limit:
if ((rtcp_counter / 80) >= p_thread->rtcp_size)
{
SendRTCP (p_thread, GetDWBE (p_pk->p_buffer + 4));
rtcp_counter = 0;
}
}
block_FifoPut( p_thread->p_empty_blocks, p_pk ); block_FifoPut( p_thread->p_empty_blocks, p_pk );
...@@ -769,18 +763,19 @@ static const char *MakeRandMulticast (int family, char *buf, size_t buflen) ...@@ -769,18 +763,19 @@ static const char *MakeRandMulticast (int family, char *buf, size_t buflen)
* - FIXME: we do not implement separate rate limiting for SDES, * - FIXME: we do not implement separate rate limiting for SDES,
* - we do not implement any profile-specific extensions for the time being. * - we do not implement any profile-specific extensions for the time being.
*/ */
static int OpenRTCP (sout_access_out_t *obj, int proto, uint16_t dport) static int OpenRTCP (vlc_object_t *obj, rtcp_sender_t *rtcp, int rtp_fd,
int proto, uint16_t dport)
{ {
sout_access_out_sys_t *p_sys = obj->p_sys;
uint8_t *ptr; uint8_t *ptr;
int fd; int fd;
char src[NI_MAXNUMERICHOST], dst[NI_MAXNUMERICHOST]; char src[NI_MAXNUMERICHOST], dst[NI_MAXNUMERICHOST];
int sport; int sport;
fd = obj->p_sys->p_thread->i_handle; rtcp->bytes = rtcp->packets = rtcp->counter = 0;
if (net_GetSockAddress (fd, src, &sport)
|| net_GetPeerAddress (fd, dst, NULL)) if (net_GetSockAddress (rtp_fd, src, &sport)
|| net_GetPeerAddress (rtp_fd, dst, NULL))
return VLC_EGENERIC; return VLC_EGENERIC;
sport++; sport++;
...@@ -788,29 +783,29 @@ static int OpenRTCP (sout_access_out_t *obj, int proto, uint16_t dport) ...@@ -788,29 +783,29 @@ static int OpenRTCP (sout_access_out_t *obj, int proto, uint16_t dport)
if (fd == -1) if (fd == -1)
return VLC_EGENERIC; return VLC_EGENERIC;
obj->p_sys->p_thread->rtcp_handle = fd; rtcp->handle = fd;
ptr = (uint8_t *)strchr (src, '%'); ptr = (uint8_t *)strchr (src, '%');
if (ptr != NULL) if (ptr != NULL)
*ptr = '\0'; /* remove scope ID frop IPv6 addresses */ *ptr = '\0'; /* remove scope ID frop IPv6 addresses */
ptr = obj->p_sys->p_thread->rtcp_data; ptr = rtcp->payload;
/* Sender report */ /* Sender report */
ptr[0] = 2 << 6; /* V = 2, P = RC = 0 */ ptr[0] = 2 << 6; /* V = 2, P = RC = 0 */
ptr[1] = 200; /* payload type: Sender Report */ ptr[1] = 200; /* payload type: Sender Report */
SetWBE (ptr + 2, 6); /* length = 6 (7 double words) */ SetWBE (ptr + 2, 6); /* length = 6 (7 double words) */
SetDWBE (ptr + 4, p_sys->i_ssrc); memset (ptr + 4, 0, 4); /* SSRC unknown yet */
SetQWBE (ptr + 8, NTPtime64 ()); SetQWBE (ptr + 8, NTPtime64 ());
memset (ptr + 16, 0, 12); /* timestamp and counters */
ptr += 28; ptr += 28;
/* timestamp and counter are handled later */
/* Source description */ /* Source description */
uint8_t *sdes = ptr; uint8_t *sdes = ptr;
ptr[0] = (2 << 6) | 1; /* V = 2, P = 0, SC = 1 */ ptr[0] = (2 << 6) | 1; /* V = 2, P = 0, SC = 1 */
ptr[1] = 202; /* payload type: Source Description */ ptr[1] = 202; /* payload type: Source Description */
uint8_t *lenptr = ptr + 2; uint8_t *lenptr = ptr + 2;
SetDWBE (ptr + 4, p_sys->i_ssrc); memset (ptr + 4, 0, 4); /* SSRC unknown yet */
ptr += 8; ptr += 8;
ptr[0] = 1; /* CNAME - mandatory */ ptr[0] = 1; /* CNAME - mandatory */
...@@ -829,16 +824,16 @@ static int OpenRTCP (sout_access_out_t *obj, int proto, uint16_t dport) ...@@ -829,16 +824,16 @@ static int OpenRTCP (sout_access_out_t *obj, int proto, uint16_t dport)
*ptr++ = 0; *ptr++ = 0;
SetWBE (lenptr, ptr - sdes); SetWBE (lenptr, ptr - sdes);
obj->p_sys->p_thread->rtcp_size = ptr - obj->p_sys->p_thread->rtcp_data; rtcp->length = ptr - rtcp->payload;
return VLC_SUCCESS; return VLC_SUCCESS;
} }
static void CloseRTCP (sout_access_thread_t *obj) static void CloseRTCP (rtcp_sender_t *rtcp)
{ {
if (obj->rtcp_handle == -1) if (rtcp->handle == -1)
return; return;
uint8_t *ptr = obj->rtcp_data; uint8_t *ptr = rtcp->payload;
/* Bye */ /* Bye */
ptr[0] = (2 << 6) | 1; /* V = 2, P = 0, SC = 1 */ ptr[0] = (2 << 6) | 1; /* V = 2, P = 0, SC = 1 */
ptr[1] = 203; /* payload type: Bye */ ptr[1] = 203; /* payload type: Bye */
...@@ -847,23 +842,39 @@ static void CloseRTCP (sout_access_thread_t *obj) ...@@ -847,23 +842,39 @@ static void CloseRTCP (sout_access_thread_t *obj)
/* We are THE sender, so we are more important than anybody else, so /* We are THE sender, so we are more important than anybody else, so
* we can afford not to check bandwidth constraints here. */ * we can afford not to check bandwidth constraints here. */
send (obj->rtcp_handle, obj->rtcp_data, 8, 0); send (rtcp->handle, rtcp->payload, 8, 0);
net_Close (obj->rtcp_handle); net_Close (rtcp->handle);
} }
static void SendRTCP (sout_access_thread_t *obj, uint32_t timestamp) static void SendRTCP (rtcp_sender_t *rtcp, const block_t *rtp)
{ {
uint8_t *ptr = obj->rtcp_data; uint8_t *ptr = rtcp->payload;
if ((rtcp->handle == -1) /* RTCP sender off */
|| (rtp->i_buffer < 12)) /* too short RTP packet */
return;
/* Updates statistics */
rtcp->packets++;
rtcp->bytes += rtp->i_buffer;
rtcp->counter += rtp->i_buffer;
/* 1.25% rate limit */
if ((rtcp->counter / 80) < rtcp->length)
return;
uint32_t last = GetDWBE (ptr + 8); // last RTCP SR send time uint32_t last = GetDWBE (ptr + 8); // last RTCP SR send time
uint64_t now64 = NTPtime64 (); uint64_t now64 = NTPtime64 ();
if ((now64 >> 32) < (last + 5)) if ((now64 >> 32) < (last + 5))
return; // no more than one SR every 5 seconds return; // no more than one SR every 5 seconds
memcpy (ptr + 4, rtp->p_buffer + 8, 4); /* SR SSRC */
SetQWBE (ptr + 8, now64); SetQWBE (ptr + 8, now64);
SetDWBE (ptr + 16, timestamp); memcpy (ptr + 16, rtp->p_buffer + 4, 4); /* RTP timestamp */
SetDWBE (ptr + 20, obj->sent_pkts); SetDWBE (ptr + 20, rtcp->packets);
SetDWBE (ptr + 24, obj->sent_bytes); SetDWBE (ptr + 24, rtcp->bytes);
memcpy (ptr + 28 + 4, rtp->p_buffer + 8, 4); /* SDES SSRC */
send (obj->rtcp_handle, ptr, obj->rtcp_size, 0); if (send (rtcp->handle, ptr, rtcp->length, 0) == (ssize_t)rtcp->length)
rtcp->counter = 0;
} }
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