-
-
-static const char *MakeRandMulticast (int family, char *buf, size_t buflen)
-{
- uint32_t rand = (getpid() & 0xffff)
- | (uint32_t)(((mdate () >> 10) & 0xffff) << 16);
-
- switch (family)
- {
-#ifdef AF_INET6
- case AF_INET6:
- {
- struct in6_addr addr;
- memcpy (&addr, "\xff\x38\x00\x00" "\x00\x00\x00\x00"
- "\x00\x00\x00\x00", 12);
- rand |= 0x80000000;
- memcpy (addr.s6_addr + 12, &(uint32_t){ htonl (rand) }, 4);
- return inet_ntop (family, &addr, buf, buflen);
- }
-#endif
-
- case AF_INET:
- {
- struct in_addr addr;
- addr.s_addr = htonl ((rand & 0xffffff) | 0xe8000000);
- return inet_ntop (family, &addr, buf, buflen);
- }
- }
-#ifdef EAFNOSUPPORT
- errno = EAFNOSUPPORT;
-#endif
- return NULL;
-}
-
-
-/*
- * 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 (vlc_object_t *obj, rtcp_sender_t *rtcp, int rtp_fd,
- int proto, uint16_t dport)
-{
- uint8_t *ptr;
- int fd;
-
- char src[NI_MAXNUMERICHOST], dst[NI_MAXNUMERICHOST];
- int sport;
-
- rtcp->bytes = rtcp->packets = rtcp->counter = 0;
-
- if (net_GetSockAddress (rtp_fd, src, &sport)
- || net_GetPeerAddress (rtp_fd, dst, NULL))
- return VLC_EGENERIC;
-
- sport++;
- fd = net_OpenDgram (obj, src, sport, dst, dport, AF_UNSPEC, proto);
- if (fd == -1)
- return VLC_EGENERIC;
-
- rtcp->handle = fd;
-
- ptr = (uint8_t *)strchr (src, '%');
- if (ptr != NULL)
- *ptr = '\0'; /* remove scope ID frop IPv6 addresses */
-
- ptr = rtcp->payload;
-
- /* 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) */
- memset (ptr + 4, 0, 4); /* SSRC unknown yet */
- SetQWBE (ptr + 8, NTPtime64 ());
- memset (ptr + 16, 0, 12); /* timestamp and counters */
- ptr += 28;
-
- /* 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;
- memset (ptr + 4, 0, 4); /* SSRC unknown yet */
- 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);
-
- rtcp->length = ptr - rtcp->payload;
- return VLC_SUCCESS;
-}
-
-static void CloseRTCP (rtcp_sender_t *rtcp)
-{
- if (rtcp->handle == -1)
- return;
-
- uint8_t *ptr = rtcp->payload;
- /* 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 (rtcp->handle, rtcp->payload, 8, 0);
- net_Close (rtcp->handle);
-}
-
-static void SendRTCP (rtcp_sender_t *rtcp, const block_t *rtp)
-{
- 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
- uint64_t now64 = NTPtime64 ();
- if ((now64 >> 32) < (last + 5))
- return; // no more than one SR every 5 seconds
-
- memcpy (ptr + 4, rtp->p_buffer + 8, 4); /* SR SSRC */
- SetQWBE (ptr + 8, now64);
- memcpy (ptr + 16, rtp->p_buffer + 4, 4); /* RTP timestamp */
- SetDWBE (ptr + 20, rtcp->packets);
- SetDWBE (ptr + 24, rtcp->bytes);
- memcpy (ptr + 28 + 4, rtp->p_buffer + 8, 4); /* SDES SSRC */
-
- if (send (rtcp->handle, ptr, rtcp->length, 0) == (ssize_t)rtcp->length)
- rtcp->counter = 0;
-}