BlueGit

ict rusty-teams

Files

rusty-teams / vendor / libwebrtc / src / native / rtp_receiver.rs

rtp_receiver.rs

rtp_receiver.rs 2.9 KiB Download Raw
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
15use cxx::SharedPtr;
16use tokio::sync::oneshot;
17use webrtc_sys::rtp_receiver as sys_rr;
18
19use crate::{
20 imp::{media_stream as imp_ms, media_stream_track::new_media_stream_track},
21 media_stream::MediaStream,
22 media_stream_track::MediaStreamTrack,
23 rtp_parameters::RtpParameters,
24 stats::RtcStats,
25 RtcError, RtcErrorType,
26};
27
28#[derive(Clone)]
29pub struct RtpReceiver {
30 pub(crate) sys_handle: SharedPtr<sys_rr::ffi::RtpReceiver>,
31}
32
33impl RtpReceiver {
34 pub fn sources(&self) -> Vec<crate::rtp_receiver::RtpSource> {
35 self.sys_handle
36 .get_sources()
37 .into_iter()
38 .map(|entry| crate::rtp_receiver::RtpSource {
39 source_id: entry.source_id,
40 contributing: entry.is_csrc,
41 audio_level: u8::try_from(entry.audio_level).ok(),
42 timestamp_ms: entry.timestamp_ms,
43 })
44 .collect()
45 }
46
47 pub fn track(&self) -> Option<MediaStreamTrack> {
48 let track_handle = self.sys_handle.track();
49 if track_handle.is_null() {
50 return None;
51 }
52
53 Some(new_media_stream_track(track_handle))
54 }
55
56 pub fn stream_ids(&self) -> Vec<String> {
57 self.sys_handle.stream_ids()
58 }
59
60 pub fn streams(&self) -> Vec<MediaStream> {
61 self.sys_handle
62 .streams()
63 .into_iter()
64 .map(|s| MediaStream { handle: imp_ms::MediaStream { sys_handle: s.ptr } })
65 .collect()
66 }
67
68 pub async fn get_stats(&self) -> Result<Vec<RtcStats>, RtcError> {
69 let (tx, rx) = oneshot::channel::<Result<Vec<RtcStats>, RtcError>>();
70 let ctx = Box::new(sys_rr::ReceiverContext(Box::new(tx)));
71
72 self.sys_handle.get_stats(ctx, |ctx, stats| {
73 let tx = ctx.0.downcast::<oneshot::Sender<Result<Vec<RtcStats>, RtcError>>>().unwrap();
74
75 if stats.is_empty() {
76 let _ = tx.send(Ok(vec![]));
77 return;
78 }
79
80 // Unwrap because it should not happens
81 let vec = serde_json::from_str(&stats).unwrap();
82 let _ = tx.send(Ok(vec));
83 });
84
85 rx.await.map_err(|_| RtcError {
86 error_type: RtcErrorType::Internal,
87 message: "get_stats cancelled".to_owned(),
88 })?
89 }
90
91 pub fn parameters(&self) -> RtpParameters {
92 self.sys_handle.get_parameters().into()
93 }
94}