+
+
+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 (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);
+ ptr += 28;
+ /* timestamps 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;
+ SetQWBE (ptr + 8, NTPtime64 ());
+ 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);
+}