WebRTC通信时获取速率(每秒帧数)相关信息

发布时间 2023-03-30 19:10:39作者: Brisk

在用WebRTC进行通信时,可以通过RTCPeerConnection对象的getStats方法获取相关的连接统计信息,以此获取每秒帧数。

-- By Brisk yu

1 getStats的使用方法

const pc = new RTCPeerConnection()

// 获取视频流对象
var selector = pc.getRemoteStreams()[0].getAudioTracks()[0]

// 获取视频流通信信息
pc.getStats(selector).then(report => {
    report.forEach(stats => {
      console.log(stats.type);
      console.log(stats);
    });
}).catch(err => {
    console.error(err);
});    

 

2 getStats可以获取的信息

getStats可以获取的信息如下:

enum RTCStatsType {
"codec",
"inbound-rtp",
"outbound-rtp",
"remote-inbound-rtp",
"remote-outbound-rtp",
"media-source",
"media-playout",
"peer-connection",
"data-channel",
"stream",
"track",
"transport",
"candidate-pair",
"local-candidate",
"remote-candidate",
"certificate"
};

其中"inbound-rtp"表示接收到的RTP流相关统计信息,其结构如下:

dictionary RTCInboundRtpStreamStats : RTCReceivedRtpStreamStats {
             required DOMString   trackIdentifier;
             required DOMString   kind;
             DOMString            mid;
             DOMString            remoteId;
             unsigned long        framesDecoded;
             unsigned long        keyFramesDecoded;
             unsigned long        framesRendered;
             unsigned long        framesDropped;
             unsigned long        frameWidth;
             unsigned long        frameHeight;
             double               framesPerSecond;
             unsigned long long   qpSum;
             double               totalDecodeTime;
             double               totalInterFrameDelay;
             double               totalSquaredInterFrameDelay;
             unsigned long        pauseCount;
             double               totalPausesDuration;
             unsigned long        freezeCount;
             double               totalFreezesDuration;
             DOMHighResTimeStamp  lastPacketReceivedTimestamp;
             unsigned long long   headerBytesReceived;
             unsigned long long   packetsDiscarded;
             unsigned long long   fecPacketsReceived;
             unsigned long long   fecPacketsDiscarded;
             unsigned long long   bytesReceived;
             unsigned long        nackCount;
             unsigned long        firCount;
             unsigned long        pliCount;
             double               totalProcessingDelay;
             DOMHighResTimeStamp  estimatedPlayoutTimestamp;
             double               jitterBufferDelay;
             double               jitterBufferTargetDelay;
             unsigned long long   jitterBufferEmittedCount;
             double               jitterBufferMinimumDelay;
             unsigned long long   totalSamplesReceived;
             unsigned long long   concealedSamples;
             unsigned long long   silentConcealedSamples;
             unsigned long long   concealmentEvents;
             unsigned long long   insertedSamplesForDeceleration;
             unsigned long long   removedSamplesForAcceleration;
             double               audioLevel;
             double               totalAudioEnergy;
             double               totalSamplesDuration;
             unsigned long        framesReceived;
             DOMString            decoderImplementation;
             DOMString            playoutId;
             boolean              powerEfficientDecoder;
             unsigned long        framesAssembledFromMultiplePackets;
             double               totalAssemblyTime;
            };

其中framesPerSecond表示接收到的RTP流的最后一秒的帧数,可以用于展示每秒帧数。