| 1 | // Copyright 2025 LiveKit, Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | use std::fmt::Debug; |
| 16 | |
| 17 | use crate::{ |
| 18 | imp::rtp_receiver as imp_rr, media_stream::MediaStream, media_stream_track::MediaStreamTrack, |
| 19 | rtp_parameters::RtpParameters, stats::RtcStats, RtcError, |
| 20 | }; |
| 21 | |
| 22 | #[derive(Clone)] |
| 23 | pub struct RtpReceiver { |
| 24 | pub(crate) handle: imp_rr::RtpReceiver, |
| 25 | } |
| 26 | |
| 27 | impl RtpReceiver { |
| 28 | pub fn track(&self) -> Option<MediaStreamTrack> { |
| 29 | self.handle.track() |
| 30 | } |
| 31 | |
| 32 | pub fn stream_ids(&self) -> Vec<String> { |
| 33 | self.handle.stream_ids() |
| 34 | } |
| 35 | |
| 36 | pub fn streams(&self) -> Vec<MediaStream> { |
| 37 | self.handle.streams() |
| 38 | } |
| 39 | |
| 40 | pub async fn get_stats(&self) -> Result<Vec<RtcStats>, RtcError> { |
| 41 | self.handle.get_stats().await |
| 42 | } |
| 43 | |
| 44 | pub fn parameters(&self) -> RtpParameters { |
| 45 | self.handle.parameters() |
| 46 | } |
| 47 | |
| 48 | /// The SSRC and CSRC sources of the last few seconds, like the browser's getSynchronizationSources and getContributingSources. |
| 49 | pub fn sources(&self) -> Vec<RtpSource> { |
| 50 | self.handle.sources() |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /// One RTP source; `audio_level` is in -dBov (0 loudest, 127 silent) when the packet carried it. |
| 55 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 56 | pub struct RtpSource { |
| 57 | pub source_id: u32, |
| 58 | pub contributing: bool, |
| 59 | pub audio_level: Option<u8>, |
| 60 | pub timestamp_ms: i64, |
| 61 | } |
| 62 | |
| 63 | impl Debug for RtpReceiver { |
| 64 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 65 | f.debug_struct("RtpReceiver") |
| 66 | .field("track", &self.track()) |
| 67 | .field("cname", &self.parameters().rtcp.cname) |
| 68 | .finish() |
| 69 | } |
| 70 | } |