What is the correct way to call GetStats() in examples/peerconnection?

263 views
Skip to first unread message

Fang Yu

unread,
May 19, 2021, 8:39:07 PM5/19/21
to discuss-webrtc
Hi WebRTC experts,

I want to call PeerConnectionInterface::GetStats() every N milliseconds, within the native example app `peerconnection`.

Currently, I am attempting to do this in the following way, in examples/peerconnection/client/conductor.cc
- Create a task_queue_ (rtc::TaskQueue), using which I do something like
   task_queue_->PostDelayedTask(
std::make_unique<StatsCollectorTask>(
my_interval_ms, peer_connection_.get(), stats_observer_.get()),
0);
- StatsCollectorTask is a subclass of webrtc::QueuedTask whose run() does something like
  peer_connection_->GetStats(stats_collector_callback_);
  webrtc::TaskQueueBase::Current()->PostDelayedTask(absl::WrapUnique(this), my_interval_ms)
- stats_observer_ is an object of a subclass of webrtc::RTCStatsCollectorCallback, which implements OnStatsDelivered()

The problem I am facing right now is that after streaming a while (1 / 2 mins) this seems to cause a deadlock. I.e., the GetStats calls gets blocked at WebRtcVideoChannel::FillReceiverStats. This only happens if the peerconnection app calls GetStats() periodically, also constantly sending data via the Data Channel.

Looking at RTCStatsCollector seems GetStats is supposed to be invoked from the signaling thread. But I don't know how to get the signaling thread (not seeing such method from PeerConnectionInterface).

I have tried a few alternatives but no luck so far, such as
- Call GetStats in my own pthread which has a loop and sleep logic
- Explicitly creating a rtc::Thread signaling thread, passing it to  a CreatePeerConnectionFactory, and do something like
  g_signaling_thread->PostTask( RTC_FROM_HERE, rtc::Bind(&Conductor::TestGetStats, this)); 
  where TestGetStats will make the GetStats() call

Please help! Please provide suggestion on what is a right way of calling GetStats every N milliseconds in such a native app.

Thank a lot in advance!

Fang Yu

unread,
May 21, 2021, 12:08:11 AM5/21/21
to discuss-webrtc
[Update] Turns out it was a stupid bug on my side that caused the deadlock. It had nothing to do with how I called GetStats().

Per https://webrtc.github.io/webrtc-org/native-code/native-apis/ "Calls to the Stream APIs and the PeerConnection APIs will be proxied to the signaling thread, which means that an application can call those APIs from whatever thread." so I was asking the wrong question.

Sorry for the noise.
Reply all
Reply to author
Forward
0 new messages