diff --git a/libwebrtc/src/native/rtp_receiver.rs b/libwebrtc/src/native/rtp_receiver.rs index b0fe64bc3..ce59e92b4 100644 --- a/libwebrtc/src/native/rtp_receiver.rs +++ b/libwebrtc/src/native/rtp_receiver.rs @@ -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)] @@ -33,6 +36,27 @@ impl RtpReceiver { Some(new_media_stream_track(track_handle)) } + pub async fn get_stats(&self) -> Result, RtcError> { + let (tx, rx) = oneshot::channel::, RtcError>>(); + let ctx = Box::new(sys_rr::ReceiverContext(Box::new(tx))); + + self.sys_handle.get_stats(ctx, |ctx, stats| { + let tx = ctx + .0 + .downcast::, 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() } diff --git a/libwebrtc/src/native/rtp_sender.rs b/libwebrtc/src/native/rtp_sender.rs index 1af9537de..fcb7b06f4 100644 --- a/libwebrtc/src/native/rtp_sender.rs +++ b/libwebrtc/src/native/rtp_sender.rs @@ -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; @@ -34,6 +36,27 @@ impl RtpSender { Some(new_media_stream_track(track_handle)) } + pub async fn get_stats(&self) -> Result, RtcError> { + let (tx, rx) = oneshot::channel::, RtcError>>(); + let ctx = Box::new(sys_rs::SenderContext(Box::new(tx))); + + self.sys_handle.get_stats(ctx, |ctx, stats| { + let tx = ctx + .0 + .downcast::, 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) -> Result<(), RtcError> { if !self .sys_handle diff --git a/libwebrtc/src/rtp_receiver.rs b/libwebrtc/src/rtp_receiver.rs index 765b329ed..e8b297ffa 100644 --- a/libwebrtc/src/rtp_receiver.rs +++ b/libwebrtc/src/rtp_receiver.rs @@ -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)] @@ -29,6 +29,10 @@ impl RtpReceiver { self.handle.track() } + pub async fn get_stats(&self) -> Result, RtcError> { + self.handle.get_stats().await + } + pub fn parameters(&self) -> RtpParameters { self.handle.parameters() } diff --git a/libwebrtc/src/rtp_sender.rs b/libwebrtc/src/rtp_sender.rs index 300c6f87e..3301e2532 100644 --- a/libwebrtc/src/rtp_sender.rs +++ b/libwebrtc/src/rtp_sender.rs @@ -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)] @@ -29,6 +29,10 @@ impl RtpSender { self.handle.track() } + pub async fn get_stats(&self) -> Result, RtcError> { + self.handle.get_stats().await + } + pub fn set_track(&self, track: Option) -> Result<(), RtcError> { self.handle.set_track(track) } diff --git a/webrtc-sys/include/livekit/jsep.h b/webrtc-sys/include/livekit/jsep.h index 90092321d..3cb59a0c1 100644 --- a/webrtc-sys/include/livekit/jsep.h +++ b/webrtc-sys/include/livekit/jsep.h @@ -132,7 +132,7 @@ class NativeSetRemoteSdpObserver }; -template +template // Context type class NativeRtcStatsCollector : public webrtc::RTCStatsCollectorCallback { public: NativeRtcStatsCollector(rust::Box ctx,