一、概述

视频通话受网络状态影响很大,网络丢包延时都会导致视频通话质量。能够实时监控当前网络状态,查看当前网络的丢包、抖动、延时,在维护定位视频通话问题中,非常有必要。

webrtc的GetStats提供了这个功能。具体实现原理,参见:https://www.jianshu.com/p/41856118f833

C++版本peerconnection.cc文件PeerConnection类中提供了两个GetStats查询接口。

bool PeerConnection::GetStats(StatsObserver* observer,
                              MediaStreamTrackInterface* track,
                              StatsOutputLevel level) {
  TRACE_EVENT0("webrtc", "PeerConnection::GetStats");
  RTC_DCHECK(signaling_thread()->IsCurrent());
  if (!observer) {
    LOG(LS_ERROR) << "GetStats - observer is NULL.";
    return false;
  }

  stats_->UpdateStats(level);
  // The StatsCollector is used to tell if a track is valid because it may
  // remember tracks that the PeerConnection previously removed.
  if (track && !stats_->IsValidTrack(track->id())) {
    LOG(LS_WARNING) << "GetStats is called with an invalid track: "
                    << track->id();
    return false;
  }
  signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS,
                           new GetStatsMsg(observer, track));
  return true;
}

void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) {
  RTC_DCHECK(stats_collector_);
  stats_collector_->GetStatsReport(callback);
}

Track状态查询

接口函数:

bool PeerConnection::GetStats(StatsObserver* observer,MediaStreamTrackInterface* track,StatsOutputLevel level)

查询参数

我们比较关心的是type为kStatsReportTypeSsrc里面的接收和发送的统计信息。在StatsReport->id()->ToString()有区分是接收统计还是发送统计。

  • 发送统计参数列表:

 

 

 

  • 接收统计参数列表

系统参数查询

接口函数:

void PeerConnection::GetStats(RTCStatsCollectorCallback* callback)

查询参数:

{"type":"certificate","id":"RTCCertificate_XXX", "timestamp":1536815737177000,"fingerprint":"XXX","fingerprintAlgorithm":"sha-256","base64Certificate":"XXX"},
{"type":"certificate","id":"RTCCertificate_XXX", "timestamp":1536815737177000,"fingerprint":"XXX","fingerprintAlgorithm":"sha-256","base64Certificate":"XXX"},
{"type":"codec","id":"RTCCodec_InboundAudio_0",  "timestamp":1536815737177000,"payloadType":0,"mimeType":"audio/PCMU","clockRate":8000},
{"type":"codec","id":"RTCCodec_InboundAudio_102","timestamp":1536815737177000,"payloadType":102,"mimeType":"audio/ILBC","clockRate":8000},
{"type":"codec","id":"RTCCodec_InboundAudio_103","timestamp":1536815737177000,"payloadType":103,"mimeType":"audio/ISAC","clockRate":16000},
{"type":"codec","id":"RTCCodec_InboundAudio_104","timestamp":1536815737177000,"payloadType":104,"mimeType":"audio/ISAC","clockRate":32000},
{"type":"codec","id":"RTCCodec_InboundAudio_105","timestamp":1536815737177000,"payloadType":105,"mimeType":"audio/CN","clockRate":16000},
{"type":"codec","id":"RTCCodec_InboundAudio_106","timestamp":1536815737177000,"payloadType":106,"mimeType":"audio/CN","clockRate":32000},
{"type":"codec","id":"RTCCodec_InboundAudio_110","timestamp":1536815737177000,"payloadType":110,"mimeType":"audio/telephone-event","clockRate":48000},
{"type":"codec","id":"RTCCodec_InboundAudio_111","timestamp":1536815737177000,"payloadType":111,"mimeType":"audio/opus","clockRate":48000},
{"type":"codec","id":"RTCCodec_InboundAudio_112","timestamp":1536815737177000,"payloadType":112,"mimeType":"audio/telephone-event","clockRate":32000},
{"type":"codec","id":"RTCCodec_InboundAudio_113","timestamp":1536815737177000,"payloadType":113,"mimeType":"audio/telephone-event","clockRate":16000},
{"type":"codec","id":"RTCCodec_InboundAudio_126","timestamp":1536815737177000,"payloadType":126,"mimeType":"audio/telephone-event","clockRate":8000},
{"type":"codec","id":"RTCCodec_InboundAudio_13","timestamp":1536815737177000,"payloadType":13,"mimeType":"audio/CN","clockRate":8000},
{"type":"codec","id":"RTCCodec_InboundAudio_8","timestamp":1536815737177000,"payloadType":8,"mimeType":"audio/PCMA","clockRate":8000},
{"type":"codec","id":"RTCCodec_InboundAudio_9","timestamp":1536815737177000,"payloadType":9,"mimeType":"audio/G722","clockRate":8000},
{"type":"codec","id":"RTCCodec_InboundVideo_100","timestamp":1536815737177000,"payloadType":100,"mimeType":"video/VP9","clockRate":90000},
{"type":"codec","id":"RTCCodec_InboundVideo_101","timestamp":1536815737177000,"payloadType":101,"mimeType":"video/rtx","clockRate":90000},
{"type":"codec","id":"RTCCodec_InboundVideo_124","timestamp":1536815737177000,"payloadType":124,"mimeType":"video/rtx","clockRate":90000},
{"type":"codec","id":"RTCCodec_InboundVideo_125","timestamp":1536815737177000,"payloadType":125,"mimeType":"video/ulpfec","clockRate":90000},
{"type":"codec","id":"RTCCodec_InboundVideo_127","timestamp":1536815737177000,"payloadType":127,"mimeType":"video/red","clockRate":90000},
{"type":"codec","id":"RTCCodec_InboundVideo_96","timestamp":1536815737177000,"payloadType":96,"mimeType":"video/H264","clockRate":90000},
{"type":"codec","id":"RTCCodec_InboundVideo_97","timestamp":1536815737177000,"payloadType":97,"mimeType":"video/rtx","clockRate":90000},
{"type":"codec","id":"RTCCodec_InboundVideo_98","timestamp":1536815737177000,"payloadType":98,"mimeType":"video/VP8","clockRate":90000},
{"type":"codec","id":"RTCCodec_InboundVideo_99","timestamp":1536815737177000,"payloadType":99,"mimeType":"video/rtx","clockRate":90000},
{"type":"codec","id":"RTCCodec_OutboundAudio_0","timestamp":1536815737177000,"payloadType":0,"mimeType":"audio/PCMU","clockRate":8000},
{"type":"codec","id":"RTCCodec_OutboundAudio_102","timestamp":1536815737177000,"payloadType":102,"mimeType":"audio/ILBC","clockRate":8000},
{"type":"codec","id":"RTCCodec_OutboundAudio_103","timestamp":1536815737177000,"payloadType":103,"mimeType":"audio/ISAC","clockRate":16000},
{"type":"codec","id":"RTCCodec_OutboundAudio_104","timestamp":1536815737177000,"payloadType":104,"mimeType":"audio/ISAC","clockRate":32000},
{"type":"codec","id":"RTCCodec_OutboundAudio_105","timestamp":1536815737177000,"payloadType":105,"mimeType":"audio/CN","clockRate":16000},
{"type":"codec","id":"RTCCodec_OutboundAudio_106","timestamp":1536815737177000,"payloadType":106,"mimeType":"audio/CN","clockRate":32000},
{"type":"codec","id":"RTCCodec_OutboundAudio_110","timestamp":1536815737177000,"payloadType":110,"mimeType":"audio/telephone-event","clockRate":48000},
{"type":"codec","id":"RTCCodec_OutboundAudio_111","timestamp":1536815737177000,"payloadType":111,"mimeType":"audio/opus","clockRate":48000},
{"type":"codec","id":"RTCCodec_OutboundAudio_112","timestamp":1536815737177000,"payloadType":112,"mimeType":"audio/telephone-event","clockRate":32000},
{"type":"codec","id":"RTCCodec_OutboundAudio_113","timestamp":1536815737177000,"payloadType":113,"mimeType":"audio/telephone-event","clockRate":16000},
{"type":"codec","id":"RTCCodec_OutboundAudio_126","timestamp":1536815737177000,"payloadType":126,"mimeType":"audio/telephone-event","clockRate":8000},
{"type":"codec","id":"RTCCodec_OutboundAudio_13","timestamp":1536815737177000,"payloadType":13,"mimeType":"audio/CN","clockRate":8000},
{"type":"codec","id":"RTCCodec_OutboundAudio_8","timestamp":1536815737177000,"payloadType":8,"mimeType":"audio/PCMA","clockRate":8000},
{"type":"codec","id":"RTCCodec_OutboundAudio_9","timestamp":1536815737177000,"payloadType":9,"mimeType":"audio/G722","clockRate":8000},
{"type":"codec","id":"RTCCodec_OutboundVideo_100","timestamp":1536815737177000,"payloadType":100,"mimeType":"video/VP9","clockRate":90000},
{"type":"codec","id":"RTCCodec_OutboundVideo_101","timestamp":1536815737177000,"payloadType":101,"mimeType":"video/rtx","clockRate":90000},
{"type":"codec","id":"RTCCodec_OutboundVideo_124","timestamp":1536815737177000,"payloadType":124,"mimeType":"video/rtx","clockRate":90000},
{"type":"codec","id":"RTCCodec_OutboundVideo_125","timestamp":1536815737177000,"payloadType":125,"mimeType":"video/ulpfec","clockRate":90000},
{"type":"codec","id":"RTCCodec_OutboundVideo_127","timestamp":1536815737177000,"payloadType":127,"mimeType":"video/red","clockRate":90000},
{"type":"codec","id":"RTCCodec_OutboundVideo_96","timestamp":1536815737177000,"payloadType":96,"mimeType":"video/H264","clockRate":90000},
{"type":"codec","id":"RTCCodec_OutboundVideo_97","timestamp":1536815737177000,"payloadType":97,"mimeType":"video/rtx","clockRate":90000},
{"type":"codec","id":"RTCCodec_OutboundVideo_98","timestamp":1536815737177000,"payloadType":98,"mimeType":"video/VP8","clockRate":90000},
{"type":"codec","id":"RTCCodec_OutboundVideo_99","timestamp":1536815737177000,"payloadType":99,"mimeType":"video/rtx","clockRate":90000},
{"type":"candidate-pair","id":"RTCIceCandidatePair_XXX","timestamp":1536815737177000,"transportId":"RTCTransport_audio_1",
         "localCandidateId":"RTCIceCandidate_XXX","remoteCandidateId":"RTCIceCandidate_XXX","state":"succeeded",
		 "priority":9.114756780654476e+18,"nominated":true,"writable":true,
		 "bytesSent":5492295,"bytesReceived":6029282,"totalRoundTripTime":0.08699999999999999,
		 "currentRoundTripTime":0.003,"availableOutgoingBitrate":4710835,
		 "requestsReceived":15,"requestsSent":1,"responsesReceived":15,"responsesSent":15,"consentRequestsSent":14},
{"type":"local-candidate","id":"RTCIceCandidate_XXX","timestamp":1536815737177000,"transportId":"RTCTransport_audio_1",
         "isRemote":false,"ip":"172.18.5.38","port":57374,"protocol":"udp","candidateType":"host","priority":2122194687,"deleted":false},

{"type":"remote-candidate","id":"RTCIceCandidate_XXX","timestamp":1536815737177000,"transportId":"RTCTransport_audio_1",
         "isRemote":true,"ip":"172.18.4.70","port":49377,"protocol":"udp","candidateType":"host","priority":2122260223,"deleted":false},
		 
{"type":"inbound-rtp","id":"RTCInboundRTPAudioStream_3044279419","timestamp":1536815737177000,"ssrc":3044279419,
         "isRemote":false,"mediaType":"audio","trackId":"RTCMediaStreamTrack_remote_audio_audio_label_3044279419",
		 "transportId":"RTCTransport_audio_1","codecId":"RTCCodec_InboundAudio_111",
		 "packetsReceived":1361,"bytesReceived":106875,"packetsLost":1,"jitter":0.003,"fractionLost":0},

{"type":"inbound-rtp","id":"RTCInboundRTPVideoStream_2146622137","timestamp":1536815737177000,"ssrc":2146622137,
         "isRemote":false,"mediaType":"video","trackId":"RTCMediaStreamTrack_remote_video_camera_0_2146622137",
		 "transportId":"RTCTransport_audio_1","codecId":"RTCCodec_InboundVideo_96",
		 "firCount":0,"pliCount":0,"nackCount":1,"qpSum":15133,
		 "packetsReceived":5442,"bytesReceived":5846050,"packetsLost":1,"fractionLost":0,"framesDecoded":803},
		 
{"type":"track","id":"RTCMediaStreamTrack_local_audio_audio_label_1034877248","timestamp":1536815737177000,"trackIdentifier":"audio_label",
         "remoteSource":false,"ended":false,"detached":false,"kind":"audio","audioLevel":0.03271584215826899,"totalAudioEnergy":1.007311366033154,
		 "totalSamplesDuration":27.40000000000148},
		 
{"type":"track","id":"RTCMediaStreamTrack_local_video_camera_0_3312359113","timestamp":1536815737177000,"trackIdentifier":"camera_0",
         "remoteSource":false,"ended":false,"detached":false,"kind":"video","frameWidth":640,"frameHeight":480,"framesSent":445},
		 
{"type":"track","id":"RTCMediaStreamTrack_remote_audio_audio_label_3044279419","timestamp":1536815737177000,"trackIdentifier":"audio_label",
         "remoteSource":true,"ended":false,"detached":false,"kind":"audio","audioLevel":0.0008239997558519242,
		 "totalAudioEnergy":0.001281306865108578,"totalSamplesReceived":1311680,"totalSamplesDuration":27.44000000000149,"concealedSamples":23473},
		 
{"type":"track","id":"RTCMediaStreamTrack_remote_video_camera_0_2146622137","timestamp":1536815737177000,"trackIdentifier":"camera_0",
         "remoteSource":true,"ended":false,"detached":false,"kind":"video","frameWidth":640,"frameHeight":480,
		 "framesReceived":805,"framesDecoded":803,"framesDropped":2},
		 
{"type":"stream","id":"RTCMediaStream_local_stream_label","timestamp":1536815737177000,"streamIdentifier":"stream_label",
        "trackIds":["RTCMediaStreamTrack_local_audio_audio_label_1034877248","RTCMediaStreamTrack_local_video_camera_0_3312359113"]},
		
{"type":"stream","id":"RTCMediaStream_remote_stream_label","timestamp":1536815737177000,"streamIdentifier":"stream_label",
        "trackIds":["RTCMediaStreamTrack_remote_audio_audio_label_3044279419","RTCMediaStreamTrack_remote_video_camera_0_2146622137"]},
		
{"type":"outbound-rtp","id":"RTCOutboundRTPAudioStream_1034877248","timestamp":1536815737177000,"ssrc":1034877248,"isRemote":false,"mediaType":"audio",
        "trackId":"RTCMediaStreamTrack_local_audio_audio_label_1034877248","transportId":"RTCTransport_audio_1",
		"codecId":"RTCCodec_OutboundAudio_111","packetsSent":1369,"bytesSent":132966},
		
{"type":"outbound-rtp","id":"RTCOutboundRTPVideoStream_3312359113","timestamp":1536815737177000,
         "ssrc":3312359113,"isRemote":false,"mediaType":"video","trackId":"RTCMediaStreamTrack_local_video_camera_0_3312359113",
		 "transportId":"RTCTransport_audio_1","codecId":"RTCCodec_OutboundVideo_96",
		 "firCount":0,"pliCount":0,"nackCount":0,"qpSum":8030,"packetsSent":4813,"bytesSent":5297788,"framesEncoded":445},

{"type":"peer-connection","id":"RTCPeerConnection","timestamp":1536815737177000,"dataChannelsOpened":0,"dataChannelsClosed":0},

{"type":"transport","id":"RTCTransport_audio_1","timestamp":1536815737177000,"bytesSent":5492295,"bytesReceived":6029282,"dtlsState":"connected",
         "selectedCandidatePairId":"RTCIceCandidatePair_XXX","localCertificateId":"RTCCertificate_XXX",
		 "remoteCertificateId":"RTCCertificate_XXX"}]

解读ICE协商结果

1、candidate-pair中的nominated为TRUE,state为succeeded,说明选中该链接。

2、candidate-pair中的ID标识出选中的remote、local的ID值,见上面高亮标志,可以看出对应关系。

3、使用协议通过protocol看出,使用通讯的TYPE,通过candidateType看出。

更多参数,参见:https://www.w3.org/TR/webrtc-stats/

Logo

致力于链接即构和开发者,提供实时互动和元宇宙领域的前沿洞察、技术分享和丰富的开发者活动,共建实时互动世界。

更多推荐