-
-
-/*
- * NOTE on RTCP implementation:
- * - there is a single sender (us), no conferencing here! => n = sender = 1,
- * - as such we need not bother to include Receiver Reports,
- * - in unicast case, there is a single receiver => members = 1 + 1 = 2,
- * and obviously n > 25% of members,
- * - in multicast case, we do not want to maintain the number of receivers
- * and we assume it is big (i.e. than 3) because that's what broadcasting is
- * all about,
- * - it is assumed we_sent = true (could be wrong), since we are THE sender,
- * - we always send SR + SDES, while running,
- * - FIXME: we do not implement separate rate limiting for SDES,
- * - 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)
-{
- sout_access_out_sys_t *p_sys = obj->p_sys;
- uint8_t *ptr;
- int fd;
-
- char src[NI_MAXNUMERICHOST], dst[NI_MAXNUMERICHOST];
- int sport;
-
- fd = obj->p_sys->p_thread->i_handle;
- if (net_GetSockAddress (fd, src, &sport)
- || net_GetPeerAddress (fd, dst, NULL))
- return VLC_EGENERIC;
-
- sport++;
- fd = net_OpenDgram (obj, src, sport, dst, dport, AF_UNSPEC, proto);
- if (fd == -1)
- return VLC_EGENERIC;
-
- obj->p_sys->p_thread->rtcp_handle = fd;
-
- ptr = (uint8_t *)strchr (src, '%');
- if (ptr != NULL)
- *ptr = '\0'; /* remove scope ID frop IPv6 addresses */
-
- ptr = obj->p_sys->p_thread->rtcp_data;
-
- /* Sender report */
- ptr[0] = 2 << 6; /* V = 2, P = RC = 0 */
- ptr[1] = 200; /* payload type: Sender Report */
- SetWBE (ptr + 2, 6); /* length = 6 (7 double words) */
- SetDWBE (ptr + 4, p_sys->i_ssrc);
- SetQWBE (ptr + 8, NTPtime64 ());
- ptr += 28;
- /* timestamp and counter are handled later */
-
- /* Source description */
- uint8_t *sdes = ptr;
- ptr[0] = (2 << 6) | 1; /* V = 2, P = 0, SC = 1 */
- ptr[1] = 202; /* payload type: Source Description */
- uint8_t *lenptr = ptr + 2;
- SetDWBE (ptr + 4, p_sys->i_ssrc);
- ptr += 8;
-
- ptr[0] = 1; /* CNAME - mandatory */
- assert (NI_MAXNUMERICHOST <= 256);
- ptr[1] = strlen (src);
- memcpy (ptr + 2, src, ptr[1]);
- ptr += ptr[1] + 2;
-
- static const char tool[] = PACKAGE_STRING;
- ptr[0] = 6; /* TOOL */
- ptr[1] = (sizeof (tool) > 256) ? 255 : (sizeof (tool) - 1);
- memcpy (ptr + 2, tool, ptr[1]);
- ptr += ptr[1] + 2;
-
- while ((ptr - sdes) & 3) /* 32-bits padding */
- *ptr++ = 0;
- SetWBE (lenptr, ptr - sdes);
-
- obj->p_sys->p_thread->rtcp_size = ptr - obj->p_sys->p_thread->rtcp_data;
- return VLC_SUCCESS;
-}
-
-static void CloseRTCP (sout_access_thread_t *obj)
-{
- if (obj->rtcp_handle == -1)
- return;
-
- uint8_t *ptr = obj->rtcp_data;
- /* Bye */
- ptr[0] = (2 << 6) | 1; /* V = 2, P = 0, SC = 1 */
- ptr[1] = 203; /* payload type: Bye */
- SetWBE (ptr + 2, 1);
- /* SSRC is already there :) */
-
- /* We are THE sender, so we are more important than anybody else, so
- * we can afford not to check bandwidth constraints here. */
- send (obj->rtcp_handle, obj->rtcp_data, 8, 0);
- net_Close (obj->rtcp_handle);
-}
-
-static void SendRTCP (sout_access_thread_t *obj, uint32_t timestamp)
-{
- uint8_t *ptr = obj->rtcp_data;
-
- uint32_t last = GetDWBE (ptr + 8); // last RTCP SR send time
- uint64_t now64 = NTPtime64 ();
- if ((now64 >> 32) < (last + 5))
- return; // no more than one SR every 5 seconds
-
- SetQWBE (ptr + 8, now64);
- SetDWBE (ptr + 16, timestamp);
- SetDWBE (ptr + 20, obj->sent_pkts);
- SetDWBE (ptr + 24, obj->sent_bytes);
-
- send (obj->rtcp_handle, ptr, obj->rtcp_size, 0);
-}