function extractMediaStatsFromStats()

in release/connect-rtc.js [10445:10539]


function extractMediaStatsFromStats(timestamp, stats, streamType) {
    var extractedStats = null;
    var reportType = null;
    var packetsSent = null;

    stats.forEach(function (statsReport) {
        if (statsReport) {
            if (statsReport.type === 'ssrc') {
                reportType = statsReport.type;
                // Legacy report. Legacy report names stats with google specific names.
                if (parseInt(statsReport.stat('packetsSent')) && statsReport.stat('mediaType') == 'audio' && streamType === 'audio_output') {
                    extractedStats = {
                        timestamp: timestamp,
                        packetsCount: parseInt(statsReport.stat('packetsSent')),
                        bytesSent: parseInt(statsReport.stat('bytesSent')),
                        audioLevel: (0, _utils.when_defined)(parseInt(statsReport.stat('audioInputLevel'))),
                        packetsLost: (0, _utils.is_defined)(statsReport.stat('packetsLost')) ? Math.max(0, statsReport.stat('packetsLost')) : 0,
                        procMilliseconds: (0, _utils.when_defined)(parseInt(statsReport.stat('googCurrentDelayMs'))),
                        rttMilliseconds: (0, _utils.when_defined)(parseInt(statsReport.stat('googRtt'))),
                        jbMilliseconds: (0, _utils.when_defined)(parseInt(statsReport.stat('googJitterReceived')))
                    };
                } else if (parseInt(statsReport.stat('packetsReceived')) && statsReport.stat('mediaType') == 'audio' && streamType === 'audio_input') {
                    extractedStats = {
                        timestamp: timestamp,
                        packetsCount: parseInt(statsReport.stat('packetsReceived')),
                        bytesReceived: parseInt(statsReport.stat('bytesReceived')),
                        audioLevel: (0, _utils.when_defined)(parseInt(statsReport.stat('audioOutputLevel'))),
                        packetsLost: (0, _utils.is_defined)(parseInt(statsReport.stat('packetsLost'))) ? Math.max(0, statsReport.stat('packetsLost')) : 0,
                        procMilliseconds: (0, _utils.when_defined)(parseInt(statsReport.stat('googCurrentDelayMs'))),
                        jbMilliseconds: (0, _utils.when_defined)(parseInt(statsReport.stat('googJitterReceived')))
                    };
                } else if ((0, _utils.is_defined)(statsReport.packetsSent) && statsReport.mediaType == 'video' && streamType === 'video_input') {
                    extractedStats = {
                        timestamp: timestamp,
                        packetsCount: parseInt(statsReport.stat('packetsSent')),
                        bytesSent: parseInt(statsReport.stat('bytesSent')),
                        packetsLost: (0, _utils.is_defined)(statsReport.stat('packetsLost')) ? Math.max(0, statsReport.stat('packetsLost')) : 0,
                        rttMilliseconds: (0, _utils.when_defined)(parseInt(statsReport.stat('googRtt'))),
                        procMilliseconds: (0, _utils.when_defined)(parseInt(statsReport.stat('googCurrentDelayMs'))),
                        frameRateSent: (0, _utils.when_defined)(parseFloat(statsReport.stat('googFrameRateSent')))
                    };
                } else if (typeof statsReport.packetsReceived !== 'undefined' && statsReport.mediaType == 'video' && streamType === 'video_output') {
                    extractedStats = {
                        timestamp: timestamp,
                        packetsCount: parseInt(statsReport.stat('packetsSent')),
                        bytesReceived: parseInt(statsReport.stat('bytesReceived')),
                        packetsLost: (0, _utils.is_defined)(parseInt(statsReport.stat('packetsLost'))) ? Math.max(0, statsReport.stat('packetsLost')) : 0,
                        frameRateReceived: (0, _utils.when_defined)(parseFloat(statsReport.stat('statsReport.googFrameRateReceived'))),
                        procMilliseconds: (0, _utils.when_defined)(parseInt(statsReport.stat('googCurrentDelayMs'))),
                        jbMilliseconds: (0, _utils.when_defined)(parseInt(statsReport.stat('googJitterReceived')))
                    };
                }
                // Standardized report for input stream stats
            } else if (statsReport.type === 'inbound-rtp' && !statsReport.isRemote && (streamType === 'audio_input' || streamType === 'video_input')) {
                reportType = statsReport.type;
                extractedStats = {
                    timestamp: timestamp,
                    packetsLost: statsReport.packetsLost,
                    packetsCount: statsReport.packetsReceived,
                    jbMilliseconds: (0, _utils.when_defined)(statsReport.jitter, 0) * 1000
                };
                // Standardized report for packets sent
            } else if (statsReport.type === 'outbound-rtp' && !statsReport.isRemote && (streamType === 'audio_output' || streamType === 'video_output')) {
                // outbound-rtp report can appear either before or after extractedStats object is created
                if (extractedStats && !extractedStats.packetsCount) {
                    extractedStats.packetsCount = statsReport.packetsSent;
                } else {
                    packetsSent = statsReport.packetsSent;
                }
                // Standardized report for remaining output stream stats
            } else if (statsReport.type === 'remote-inbound-rtp' && (streamType === 'audio_output' || streamType === 'video_output')) {
                reportType = statsReport.type;
                extractedStats = {
                    timestamp: timestamp,
                    packetsLost: statsReport.packetsLost,
                    packetsCount: packetsSent,
                    rttMilliseconds: Number.isInteger(statsReport.roundTripTime) ? statsReport.roundTripTime : (0, _utils.is_defined)(statsReport.roundTripTime) ? statsReport.roundTripTime * 1000 : null,
                    jbMilliseconds: (0, _utils.when_defined)(statsReport.jitter, 0) * 1000
                };
                // Case for Firefox 65 and below for getting remaining output stream stats
            } else if (statsReport.type === 'inbound-rtp' && statsReport.isRemote && (streamType === 'audio_output' || streamType === 'video_output')) {
                reportType = statsReport.type;
                extractedStats = {
                    timestamp: timestamp,
                    packetsLost: statsReport.packetsLost,
                    packetsCount: packetsSent,
                    rttMilliseconds: Number.isInteger(statsReport.roundTripTime) ? statsReport.roundTripTime : (0, _utils.is_defined)(statsReport.roundTripTime) ? statsReport.roundTripTime * 1000 : null,
                    jbMilliseconds: (0, _utils.when_defined)(statsReport.jitter, 0) * 1000
                };
            }
        }
    });

    return extractedStats ? new MediaRtpStats(extractedStats, reportType, streamType) : null;
}