示例#1
0
        private void ParseStats(RTCStats stats, StatsData statsData)
        {
            try
            {
                switch (stats.StatsType)
                {
                case RTCStatsType.InboundRtp:
                    //Debug.WriteLine("RTCStatsType.InboundRtp:" + statId);
                    RTCInboundRtpStreamStats inboundRtpStreamStats = stats.ToInboundRtp();
                    if (inboundRtpStreamStats != null)
                    {
                        TrackStatsData tsd =
                            statsData.GetTrackStatsData(inboundRtpStreamStats.RtpStreamStats.MediaTrackId, false);

                        if (tsd != null)
                        {
                            if (statsData.TimeToSetupCall.Milliseconds == 0 && inboundRtpStreamStats.PacketsReceived > 0)
                            {
                                statsData.TimeToSetupCall = DateTime.Now - statsData.StarTime;
                            }

                            //if (!tsd.IsAudio)
                            //    TestReceivedBytes.Add(inboundRtpStreamStats.BytesReceived);
                            tsd.AddAverage(RtcStatsValueName.StatsValueNameBytesReceived,
                                           inboundRtpStreamStats.BytesReceived);

                            //if (!tsd.IsAudio)
                            //TestReceivedPackets.Add(inboundRtpStreamStats.PacketsReceived);
                            tsd.AddAverage(RtcStatsValueName.StatsValueNamePacketsReceived,
                                           inboundRtpStreamStats.PacketsReceived);

                            tsd.AddAverage(RtcStatsValueName.StatsValueNamePacketsLost, inboundRtpStreamStats.PacketsLost);

                            tsd.AddData(RtcStatsValueName.StatsValueNameCurrentEndToEndDelayMs,
                                        inboundRtpStreamStats.EndToEndDelay.TotalMilliseconds);
                        }
                    }
                    break;

                case RTCStatsType.OutboundRtp:
                    RTCOutboundRtpStreamStats outboundRtpStreamStats = stats.ToOutboundRtp();
                    if (outboundRtpStreamStats != null)
                    {
                        TrackStatsData tsd =
                            statsData.GetTrackStatsData(outboundRtpStreamStats.RtpStreamStats.MediaTrackId);

                        if (tsd != null)
                        {
                            tsd.AddAverage(RtcStatsValueName.StatsValueNameBytesSent, outboundRtpStreamStats.BytesSent);

                            tsd.AddAverage(RtcStatsValueName.StatsValueNamePacketsSent, outboundRtpStreamStats.PacketsSent);
                        }
                    }
                    break;

                case RTCStatsType.Track:
                    RTCMediaStreamTrackStats mediaStreamTrackStats = stats.ToTrack();
                    if (mediaStreamTrackStats != null)
                    {
                        try
                        {
                            TrackStatsData tsd =
                                statsData.GetTrackStatsData(mediaStreamTrackStats.TrackId, !mediaStreamTrackStats.RemoteSource);

                            if (tsd != null && !tsd.IsAudio)
                            {
                                if (mediaStreamTrackStats.RemoteSource)
                                {
                                    tsd.AddData(RtcStatsValueName.StatsValueNameFrameRateReceived,
                                                mediaStreamTrackStats.FramesPerSecond);
                                    tsd.AddData(RtcStatsValueName.StatsValueNameFrameWidthReceived,
                                                mediaStreamTrackStats.FrameWidth);
                                    tsd.AddData(RtcStatsValueName.StatsValueNameFrameHeightReceived,
                                                mediaStreamTrackStats.FrameHeight);
                                    FramesPerSecondChanged?.Invoke("PEER", mediaStreamTrackStats.FramesPerSecond.ToString("0.#"));
                                    ResolutionChanged?.Invoke("PEER", mediaStreamTrackStats.FrameWidth, mediaStreamTrackStats.FrameHeight);
                                }
                                else
                                {
                                    tsd.AddData(RtcStatsValueName.StatsValueNameFrameRateSent,
                                                mediaStreamTrackStats.FramesPerSecond);
                                    tsd.AddData(RtcStatsValueName.StatsValueNameFrameWidthSent,
                                                mediaStreamTrackStats.FrameWidth);
                                    tsd.AddData(RtcStatsValueName.StatsValueNameFrameHeightSent,
                                                mediaStreamTrackStats.FrameHeight);
                                    FramesPerSecondChanged?.Invoke("SELF", mediaStreamTrackStats.FramesPerSecond.ToString("0.#"));
                                    ResolutionChanged?.Invoke("SELF", mediaStreamTrackStats.FrameWidth, mediaStreamTrackStats.FrameHeight);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.Write(e);
                        }
                    }
                    break;
                }
            }
            catch (Exception e)
            {
                Debug.Write(e);
            }
        }