webrtc状态获取
简单调用
回调类
class NRPlayerStatsObserver : virtual public webrtc::RTCStatsCollectorCallback
{
public:
NRPlayerStatsObserver();
virtual ~NRPlayerStatsObserver();
virtual void OnStatsDelivered(const rtc::scoped_refptr<const RTCStatsReport>& report);
};
需要的类变量
rtc::scoped_refptr<NRPlayerStatsObserver> rtc_stats_observer_;
初始化
rtc_stats_observer_ = new rtc::RefCountedObject<NRPlayerStatsObserver>();
调用
int NRWebrtcPlayer::GetStatus()
{
if (peer_connection_ != nullptr)
{
peer_connection_->GetStats(rtc_stats_observer_);
}
return 0;
}
- 这事一个异步接口
void NRPlayerStatsObserver::OnStatsDelivered(const rtc::scoped_refptr<const RTCStatsReport>& report)
{
std::string json = report->ToJson();
cout << json << endl;
string videoKey = "RTCInboundRTPVideoStream_" + std::to_string(video_ssrc_);
string audioKey = "RTCInboundRTPAudioStream_" + std::to_string(audio_ssrc_);
const RTCInboundRTPStreamStats* videoStats = (const RTCInboundRTPStreamStats*)(report->Get(videoKey));
const RTCInboundRTPStreamStats* audioStats = (const RTCInboundRTPStreamStats*)(report->Get(audioKey));
if (videoStats != NULL)
{
int lost = *videoStats->nack_count - last_lost_video_;
int lost = *videoStats->packets_lost -last_lost_video_;
recv = (recv == 0) ? 1 : recv;
int lost_rate = lost * 100 / recv;
if (lost_rate > 100)
{
lost_rate = 100;
}
last_recv_video_ = *videoStats->packets_received;
last_lost_video_ = *videoStats->packets_lost ;
}
这里面会有不同类型的状态信息
根据需要取出信息,上边是以RTCInboundRTPVideoStream 为例子,
report 中的key是 RTCInboundRTPVideoStream_2473626177
当然也可以解析json获取,但感觉效率低些
...
{
"type":"inbound-rtp",
"id":"RTCInboundRTPVideoStream_2473626177",
"timestamp":1644990473721000,
"ssrc":2473626177,
"kind":"video",
"trackId":"RTCMediaStreamTrack_receiver_4",
"transportId":"RTCTransport_0_1",
"codecId":"RTCCodec_1_Inbound_100",
"mediaType":"video",
"jitter":0.006,
"packetsLost":0,
"packetsReceived":873,
"bytesReceived":925690,
"headerBytesReceived":20952,
"lastPacketReceivedTimestamp":1644990473711,
"jitterBufferDelay":7.316,
"jitterBufferEmittedCount":101,
"framesReceived":105,
"frameWidth":1280,
"frameHeight":720,
"framesPerSecond":30,
"framesDecoded":102,
"keyFramesDecoded":1,
"framesDropped":0,
"totalDecodeTime":0.236,
"totalInterFrameDelay":3.324999999999999,
"totalSquaredInterFrameDelay":0.1143710000000001,
"decoderImplementation":"FFmpeg",
"firCount":0,
"pliCount":2,
"nackCount":0,
"qpSum":2878
},
...