private void BuildBasicReceiverTables(VenueState venueState, out DataTable thptTable, out DataTable lossTable,
                                              IDictionary <IPEndPoint, SenderData> senderSummaries)
        {
            thptTable = new DataTable();
            lossTable = new DataTable();

            thptTable.Columns.Add("Receiver IP", Type.GetType("System.String"));
            thptTable.Columns.Add("CName", Type.GetType("System.String"));
            lossTable.Columns.Add("Receiver IP", Type.GetType("System.String"));
            lossTable.Columns.Add("CName", Type.GetType("System.String"));

            // add a column for each sender...
            foreach (IPEndPoint endPoint in senderSummaries.Keys)
            {
                thptTable.Columns.Add(endPoint.ToString(), Type.GetType("System.String"));
                lossTable.Columns.Add(endPoint.ToString(), Type.GetType("System.String"));
            }


            lock (venueState.ReceiverData)
            {
                // add a row for each receiver
                foreach (IPEndPoint receiverEndpoint in getSortedReceivers(venueState))
                {
                    ReceiverData receiverData = venueState.ReceiverData[receiverEndpoint];
                    if (receiverData == null)
                    {
                        continue; // shouldn't happen...
                    }
                    // process receiver summaries
                    ICollection <ReceiverSummary> summaries = receiverData.ReceiverSummaries;
                    if (summaries == null || summaries.Count == 0)
                    {
                        continue;
                    }

                    DataRow thptRow = thptTable.NewRow();
                    DataRow lossRow = lossTable.NewRow();

                    thptRow["Receiver IP"] = receiverEndpoint.ToString();
                    thptRow["CName"]       = receiverData.CName;

                    lossRow["Receiver IP"] = receiverEndpoint.ToString();
                    lossRow["CName"]       = receiverData.CName;

                    // initialize sender columns...
                    foreach (IPEndPoint endPoint in senderSummaries.Keys)
                    {
                        lossRow[endPoint.ToString()] = "no data";
                        thptRow[endPoint.ToString()] = "no data";
                    }

                    // process receiver summaries, while merging data from co-located data streams
                    // thus, we should end up with one column per sending host, regardless of its
                    // number of output streams...
                    IDictionary <IPEndPoint, ReceiverSummary> mergedReceiverSummaries =
                        new Dictionary <IPEndPoint, ReceiverSummary>();

                    // this is the merge phase...
                    foreach (ReceiverSummary summary in summaries)
                    {
                        SenderData senderData;
                        if (!venueState.SenderData.TryGetValue(summary.SSRC, out senderData))
                        {
                            continue; // no sender data for this receiver summary
                        }
                        IPEndPoint senderEndpoint = senderData.Source;
                        if (senderEndpoint == null)
                        {
                            continue;
                        }

                        if (mergedReceiverSummaries.ContainsKey(senderEndpoint))
                        {
                            // merge the summary data with existing data from this sender
                            ReceiverSummary existingSummary = mergedReceiverSummaries[senderEndpoint];
                            ReceiverSummary updatedSummary  = new ReceiverSummary(existingSummary, summary);
                            mergedReceiverSummaries[senderEndpoint] = updatedSummary;
                        }
                        else
                        {
                            // otherwise, create a new summary entry for this EndPoint
                            mergedReceiverSummaries[senderEndpoint] = summary;
                        }
                    }

                    // At this point, we have merged data from co-located data streams.  Populate
                    // the columns corresponding to source IP addreses.
                    foreach (IPEndPoint senderEndpoint in mergedReceiverSummaries.Keys)
                    {
                        if (thptTable.Columns.Contains(senderEndpoint.ToString()))
                        {
                            ReceiverSummary summary    = mergedReceiverSummaries[senderEndpoint];
                            SenderData      senderData = senderSummaries[senderEndpoint];

                            double throughputDifferential = summary.PacketRate - senderData.PacketRate;

                            try
                            {
                                thptRow[senderEndpoint.ToString()] = String.Format("{0:N} pk/sec", throughputDifferential);

                                //String.Format("{1:N} - {2:N} = {0:N} pk/sec", throughputDifferential,
                                //summary.PacketRate , senderData.PacketRate);

                                lossRow[senderEndpoint.ToString()] = String.Format("{0:P}", summary.LossRate);
                            }
                            catch (Exception)
                            {
                                // these operations can fail when sender and receiver tables are out of sync
                            }
                        }
                    }

                    // mark "loopback" data as such
                    try
                    {
                        thptRow[receiverEndpoint.ToString()] = "loopback";
                        lossRow[receiverEndpoint.ToString()] = "loopback";
                    }
                    catch (Exception)
                    {
                        // these operations can fail if sender, receiver tables are out of sync...
                    }

                    thptTable.Rows.Add(thptRow);
                    lossTable.Rows.Add(lossRow);
                } // for each receiver in the venue
            }     // venue.receiverData lock
        }
        private void BuildAdvancedReceiverTables(VenueState venueState, out DataTable thptTable, out DataTable lossTable)
        {
            thptTable = new DataTable();
            lossTable = new DataTable();

            thptTable.Columns.Add("Receiver IP", Type.GetType("System.String"));
            thptTable.Columns.Add("CName", Type.GetType("System.String"));
            lossTable.Columns.Add("Receiver IP", Type.GetType("System.String"));
            lossTable.Columns.Add("CName", Type.GetType("System.String"));

            // keep track of the packet sending rate for each sender; this avoids having
            // to hold multiple locks at the same time...

            IDictionary <uint, double> sendingRateMap = new Dictionary <uint, double>();

            lock (venueState.SenderData)
            {
                // add a column for each sender... Keep the key set sorted so that elements don't
                // bounce around between rows

                foreach (uint ssrc in getSortedSenders(venueState))
                {
                    thptTable.Columns.Add(ssrc.ToString(), Type.GetType("System.String"));
                    lossTable.Columns.Add(ssrc.ToString(), Type.GetType("System.String"));

                    sendingRateMap[ssrc] = venueState.SenderData[ssrc].PacketRate;
                }
            }

            lock (venueState.ReceiverData)
            {
                // add a row for each receiver

                foreach (IPEndPoint endPoint in getSortedReceivers(venueState))
                {
                    ReceiverData receiverData = venueState.ReceiverData[endPoint];
                    if (receiverData == null)
                    {
                        continue; // shouldn't happen...
                    }
                    // process receiver summaries
                    ICollection <ReceiverSummary> summaries = receiverData.ReceiverSummaries;
                    if (summaries == null || summaries.Count == 0)
                    {
                        continue;
                    }

                    DataRow thptRow = thptTable.NewRow();
                    DataRow lossRow = lossTable.NewRow();

                    thptRow["Receiver IP"] = endPoint.ToString();
                    thptRow["CName"]       = receiverData.CName;

                    lossRow["Receiver IP"] = endPoint.ToString();
                    lossRow["CName"]       = receiverData.CName;


                    // initialize sender columns...
                    foreach (uint ssrc in getSortedSenders(venueState))
                    {
                        lossRow[ssrc.ToString()] = "no data";
                        thptRow[ssrc.ToString()] = "no data";
                    }

                    // process receiver summaries
                    //ICollection<ReceiverSummary> summaries = hostState.ReceiverSummaries;
                    foreach (ReceiverSummary summary in summaries)
                    {
                        uint reporteeSSRC = summary.SSRC;
                        if (thptTable.Columns.Contains(reporteeSSRC.ToString()))
                        {
                            double throughputDifferential = summary.PacketRate - sendingRateMap[reporteeSSRC];
                            thptRow[reporteeSSRC.ToString()] = String.Format("{0:N} pk/sec", throughputDifferential);

                            lossRow[reporteeSSRC.ToString()] = String.Format("{0:P}", summary.LossRate);
                        }
                    } // for each receiver summary...

                    thptTable.Rows.Add(thptRow);
                    lossTable.Rows.Add(lossRow);
                } // for each receiver in the venue
            }     // venue.receiverData lock
        }
Beispiel #3
0
        private void Start()
        {
            //BufferChunk chunk = new BufferChunk(2048);
            CompoundPacket compoundPacket = new CompoundPacket();
            EndPoint       endPoint       = null;

            while (isRunning)
            {
                try
                {
                    compoundPacket.Reset();
                    udpListener.ReceiveFrom(compoundPacket.Buffer, out endPoint);

                    compoundPacket.ParseBuffer();
                    //IPAddress ipAddress = ((IPEndPoint)endPoint).Address;
                    IPEndPoint ipEndpoint = (IPEndPoint)endPoint;

                    // The compound packet enumerator destroys its list during enumeration,
                    // so we keep track of packets that have yet to be processed
                    IList <RtcpPacket> yetToBeProcessed = new List <RtcpPacket>();


                    String venueName = null;
                    //uint ssrc = 0;
                    long when = 0; // in units of "ticks"

                    // scan through the compound packet, looking for key pieces of meta-data
                    // first, look for the app packet that specifies the venue
                    // also, obtain the ssrc and the time stamp

                    foreach (RtcpPacket packet in compoundPacket)
                    {
                        if (packet.PacketType == (byte)Rtcp.PacketType.APP)
                        {
                            AppPacket appPacket = new AppPacket(packet);
                            if (appPacket.Name.Equals(Rtcp.APP_PACKET_NAME) &&
                                appPacket.Subtype == Rtcp.VENUE_APP_PACKET_SUBTYPE)
                            {
                                BufferChunk chunk = new BufferChunk(appPacket.Data);
                                when      = chunk.NextInt64();
                                venueName = chunk.NextUtf8String(chunk.Length);
                                int padIndex = venueName.IndexOf((char)0);
                                if (padIndex > 0)
                                {
                                    venueName = venueName.Substring(0, padIndex);
                                }
                            }
                        }
                        else
                        {
                            yetToBeProcessed.Add(packet);
                        }
                    }

                    if (venueName == null)
                    {
                        continue; // can't do anything if we don't know the venue for this packet
                    }
                    if (when == 0)
                    {
                        continue; // need a timestamp
                    }
                    VenueState venueState = null;

                    // compound operations must always be locked...
                    lock (venueStateMap)
                    {
                        if (!venueStateMap.ContainsKey(venueName))
                        {
                            venueState = new VenueState(venueName);
                        }
                        else
                        {
                            venueState = venueStateMap[venueName];
                        }
                    }


                    // scan again, this time processing the RTCP packets
                    foreach (RtcpPacket packet in yetToBeProcessed)
                    {
                        switch (packet.PacketType)
                        {
                        case (byte)Rtcp.PacketType.SR:
                        {
                            SrPacket sr = new SrPacket(packet);

                            SenderData senderData = venueState.GetSenderState(sr.SSRC);
                            senderData.Source = ipEndpoint;

                            senderData.updateSenderState(sr.SenderReport, when);

                            // this "refreshes" the host state (so that it won't expire)
                            venueState.SenderData[sr.SSRC] = senderData;
                            break;
                        }

                        case (byte)Rtcp.PacketType.RR:
                        {
                            RrPacket     rr           = new RrPacket(packet);
                            ReceiverData receiverData = venueState.GetReceiverData(ipEndpoint);

                            // currently, we replace all receiver summaries with the data
                            // from a single RR packet
                            receiverData.updateReceiverState(rr.ReceiverReports, when, venueState);


                            // this "refreshes" the host state (so that it won't expire)
                            venueState.ReceiverData[ipEndpoint] = receiverData;
                            break;
                        }

                        case (byte)Rtcp.PacketType.SDES:
                        {
                            SdesPacket sdp = new SdesPacket(packet);

                            foreach (SdesReport report in sdp.Reports())
                            {
                                SenderData senderData = venueState.GetSenderState(report.SSRC);
                                senderData.CName  = report.SdesData.CName;
                                senderData.Source = ipEndpoint;

                                // this "refreshes" the host state (so that it won't expire)
                                venueState.SenderData[report.SSRC] = senderData;

                                ReceiverData receiverData =
                                    venueState.GetReceiverDataWithoutCreating(ipEndpoint);

                                if (receiverData != null)
                                {
                                    receiverData.CName = report.SdesData.CName;
                                }
                            }
                            break;
                        }

                        case (byte)Rtcp.PacketType.BYE:
                        {
                            //BYE packets occur when capabilities stop.  Clean out sender data only for the
                            //ssrc's affected.  We leave receiver reports alone for now.
                            ByePacket byePacket = new ByePacket(packet);
                            foreach (uint ssrc in byePacket.SSRCs)
                            {
                                venueState.SenderData.Remove(ssrc);
                            }
                            //Set a flag to cause the matrix for this venue to be rebuilt on the next request.
                            venueState.ParticipantChange = true;
                            continue;
                        }

                        case (byte)Rtcp.PacketType.APP:
                        {
                            // ignored

                            break;
                        }
                        }
                    }   // foreach packet...

                    // refresh the venue state
                    venueStateMap[venueName] = venueState;
                }
                catch (Exception e)
                {
                    Console.Out.WriteLine("Exception : " + e.ToString());

                    writeEventLog("Exception in receive thread: " + e.ToString(), EventLogEntryType.Warning);
                }
            } // loop forever...
        }