Skip to content

Commit

Permalink
rtpsender & rtpreceiver stats
Browse files Browse the repository at this point in the history
  • Loading branch information
theomonnom committed Nov 2, 2023
1 parent 6dc6b99 commit b65a0e7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
24 changes: 24 additions & 0 deletions libwebrtc/src/native/rtp_receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
use crate::imp::media_stream_track::new_media_stream_track;
use crate::media_stream_track::MediaStreamTrack;
use crate::rtp_parameters::RtpParameters;
use crate::stats::RtcStats;
use crate::{RtcError, RtcErrorType};
use cxx::SharedPtr;
use tokio::sync::oneshot;
use webrtc_sys::rtp_receiver as sys_rr;

#[derive(Clone)]
Expand All @@ -33,6 +36,27 @@ impl RtpReceiver {
Some(new_media_stream_track(track_handle))
}

pub async fn get_stats(&self) -> Result<Vec<RtcStats>, RtcError> {
let (tx, rx) = oneshot::channel::<Result<Vec<RtcStats>, RtcError>>();
let ctx = Box::new(sys_rr::ReceiverContext(Box::new(tx)));

self.sys_handle.get_stats(ctx, |ctx, stats| {
let tx = ctx
.0
.downcast::<oneshot::Sender<Result<Vec<RtcStats>, RtcError>>>()
.unwrap();

// Unwrap because it should not happens
let vec = serde_json::from_str(&stats).unwrap();
let _ = tx.send(Ok(vec));
});

rx.await.map_err(|_| RtcError {
error_type: RtcErrorType::Internal,
message: "get_stats cancelled".to_owned(),
})?
}

pub fn parameters(&self) -> RtpParameters {
self.sys_handle.get_parameters().into()
}
Expand Down
23 changes: 23 additions & 0 deletions libwebrtc/src/native/rtp_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

use super::media_stream_track::new_media_stream_track;
use crate::media_stream_track::MediaStreamTrack;
use crate::stats::RtcStats;
use crate::{rtp_parameters::RtpParameters, RtcError, RtcErrorType};
use cxx::SharedPtr;
use tokio::sync::oneshot;
use webrtc_sys::rtc_error as sys_err;
use webrtc_sys::rtp_sender as sys_rs;

Expand All @@ -34,6 +36,27 @@ impl RtpSender {
Some(new_media_stream_track(track_handle))
}

pub async fn get_stats(&self) -> Result<Vec<RtcStats>, RtcError> {
let (tx, rx) = oneshot::channel::<Result<Vec<RtcStats>, RtcError>>();
let ctx = Box::new(sys_rs::SenderContext(Box::new(tx)));

self.sys_handle.get_stats(ctx, |ctx, stats| {
let tx = ctx
.0
.downcast::<oneshot::Sender<Result<Vec<RtcStats>, RtcError>>>()
.unwrap();

// Unwrap because it should not happens
let vec = serde_json::from_str(&stats).unwrap();
let _ = tx.send(Ok(vec));
});

rx.await.map_err(|_| RtcError {
error_type: RtcErrorType::Internal,
message: "get_stats cancelled".to_owned(),
})?
}

pub fn set_track(&self, track: Option<MediaStreamTrack>) -> Result<(), RtcError> {
if !self
.sys_handle
Expand Down
6 changes: 5 additions & 1 deletion libwebrtc/src/rtp_receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::fmt::Debug;

use crate::{
imp::rtp_receiver as imp_rr, media_stream_track::MediaStreamTrack,
rtp_parameters::RtpParameters,
rtp_parameters::RtpParameters, stats::RtcStats, RtcError,
};

#[derive(Clone)]
Expand All @@ -29,6 +29,10 @@ impl RtpReceiver {
self.handle.track()
}

pub async fn get_stats(&self) -> Result<Vec<RtcStats>, RtcError> {
self.handle.get_stats().await
}

pub fn parameters(&self) -> RtpParameters {
self.handle.parameters()
}
Expand Down
6 changes: 5 additions & 1 deletion libwebrtc/src/rtp_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::fmt::Debug;

use crate::{
imp::rtp_sender as imp_rs, media_stream_track::MediaStreamTrack, rtp_parameters::RtpParameters,
RtcError,
stats::RtcStats, RtcError,
};

#[derive(Clone)]
Expand All @@ -29,6 +29,10 @@ impl RtpSender {
self.handle.track()
}

pub async fn get_stats(&self) -> Result<Vec<RtcStats>, RtcError> {
self.handle.get_stats().await
}

pub fn set_track(&self, track: Option<MediaStreamTrack>) -> Result<(), RtcError> {
self.handle.set_track(track)
}
Expand Down
2 changes: 1 addition & 1 deletion webrtc-sys/include/livekit/jsep.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class NativeSetRemoteSdpObserver
};


template<class T>
template<class T> // Context type
class NativeRtcStatsCollector : public webrtc::RTCStatsCollectorCallback {
public:
NativeRtcStatsCollector(rust::Box<T> ctx,
Expand Down

0 comments on commit b65a0e7

Please sign in to comment.