Skip to content

Commit

Permalink
fix: invalid stats panic (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
theomonnom authored Nov 3, 2023
1 parent cb2697b commit ad40081
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 16 deletions.
5 changes: 5 additions & 0 deletions libwebrtc/src/native/peer_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,11 @@ impl PeerConnection {
.downcast::<oneshot::Sender<Result<Vec<RtcStats>, RtcError>>>()
.unwrap();

if stats.is_empty() {
let _ = tx.send(Ok(vec![]));
return;
}

// Unwrap because it should not happens
let vec = serde_json::from_str(&stats).unwrap();
let _ = tx.send(Ok(vec));
Expand Down
5 changes: 5 additions & 0 deletions libwebrtc/src/native/rtp_receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ impl RtpReceiver {
.downcast::<oneshot::Sender<Result<Vec<RtcStats>, RtcError>>>()
.unwrap();

if stats.is_empty() {
let _ = tx.send(Ok(vec![]));
return;
}

// Unwrap because it should not happens
let vec = serde_json::from_str(&stats).unwrap();
let _ = tx.send(Ok(vec));
Expand Down
5 changes: 5 additions & 0 deletions libwebrtc/src/native/rtp_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ impl RtpSender {
.downcast::<oneshot::Sender<Result<Vec<RtcStats>, RtcError>>>()
.unwrap();

if stats.is_empty() {
let _ = tx.send(Ok(vec![]));
return;
}

// Unwrap because it should not happens
let vec = serde_json::from_str(&stats).unwrap();
let _ = tx.send(Ok(vec));
Expand Down
2 changes: 1 addition & 1 deletion livekit/src/room/track/local_audio_track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl LocalAudioTrack {
}

pub async fn get_stats(&self) -> RoomResult<Vec<RtcStats>> {
super::get_stats(&self.inner).await
super::local_track::get_stats(&self.inner).await
}

pub(crate) fn on_muted(&self, f: impl Fn(Track) + Send + 'static) {
Expand Down
14 changes: 13 additions & 1 deletion livekit/src/room/track/local_track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use super::track_dispatch;
use std::sync::Arc;

use super::{track_dispatch, TrackInner};
use crate::prelude::*;
use libwebrtc::prelude::*;
use libwebrtc::stats::RtcStats;
use livekit_protocol as proto;
use livekit_protocol::enum_dispatch;

Expand Down Expand Up @@ -61,3 +64,12 @@ impl TryFrom<Track> for LocalTrack {
}
}
}

pub(super) async fn get_stats(inner: &Arc<TrackInner>) -> RoomResult<Vec<RtcStats>> {
let transceiver = inner.info.read().transceiver.clone();
let Some(transceiver) = transceiver.as_ref() else {
return Err(RoomError::Internal("no transceiver found for track".into()));
};

Ok(transceiver.sender().get_stats().await?)
}
2 changes: 1 addition & 1 deletion livekit/src/room/track/local_video_track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl LocalVideoTrack {
}

pub async fn get_stats(&self) -> RoomResult<Vec<RtcStats>> {
super::get_stats(&self.inner).await
super::local_track::get_stats(&self.inner).await
}

pub(crate) fn on_muted(&self, f: impl Fn(Track) + Send + 'static) {
Expand Down
10 changes: 0 additions & 10 deletions livekit/src/room/track/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,6 @@ pub(super) fn new_inner(
}
}

pub(super) async fn get_stats(inner: &Arc<TrackInner>) -> RoomResult<Vec<RtcStats>> {
let transceiver = inner.info.read().transceiver.clone();
let Some(transceiver) = transceiver.as_ref() else {
return Err(RoomError::Internal("no transceiver found for track".into()));
};

let rtp_receiver = transceiver.receiver();
Ok(rtp_receiver.get_stats().await?)
}

/// This is only called for local tracks
pub(super) fn set_muted(inner: &Arc<TrackInner>, track: &Track, muted: bool) {
let info = inner.info.read();
Expand Down
2 changes: 1 addition & 1 deletion livekit/src/room/track/remote_audio_track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl RemoteAudioTrack {
}

pub async fn get_stats(&self) -> RoomResult<Vec<RtcStats>> {
super::get_stats(&self.inner).await
super::remote_track::get_stats(&self.inner).await
}

pub(crate) fn on_muted(&self, f: impl Fn(Track) + Send + 'static) {
Expand Down
10 changes: 9 additions & 1 deletion livekit/src/room/track/remote_track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use super::track_dispatch;
use super::TrackInner;
use crate::prelude::*;
use libwebrtc::prelude::*;
use libwebrtc::stats::InboundRtpStats;
use libwebrtc::stats::RtcStats;
use livekit_protocol as proto;
use livekit_protocol::enum_dispatch;
Expand All @@ -40,6 +39,15 @@ impl RemoteTrack {
}
}

pub(super) async fn get_stats(inner: &Arc<TrackInner>) -> RoomResult<Vec<RtcStats>> {
let transceiver = inner.info.read().transceiver.clone();
let Some(transceiver) = transceiver.as_ref() else {
return Err(RoomError::Internal("no transceiver found for track".into()));
};

Ok(transceiver.receiver().get_stats().await?)
}

pub(super) fn update_info(inner: &Arc<TrackInner>, track: &Track, new_info: proto::TrackInfo) {
super::update_info(inner, track, new_info.clone());
super::set_muted(inner, track, new_info.muted);
Expand Down
2 changes: 1 addition & 1 deletion livekit/src/room/track/remote_video_track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl RemoteVideoTrack {
}

pub async fn get_stats(&self) -> RoomResult<Vec<RtcStats>> {
super::get_stats(&self.inner).await
super::remote_track::get_stats(&self.inner).await
}

pub(crate) fn on_muted(&self, f: impl Fn(Track) + Send + 'static) {
Expand Down

0 comments on commit ad40081

Please sign in to comment.