BlueGit

ict rusty-teams

Files

rusty-teams / vendor / libwebrtc / src / video_stream.rs

video_stream.rs

video_stream.rs 4.2 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 crate::imp::video_stream as stream_imp;
16
17// There is no shared sink between native and web platforms.
18// Each platform requires different configuration (e.g: WebGlContext, ..)
19
20#[cfg(not(target_arch = "wasm32"))]
21pub mod native {
22 use std::{
23 fmt::{Debug, Formatter},
24 pin::Pin,
25 task::{Context, Poll},
26 };
27
28 use super::stream_imp;
29 use crate::{
30 native::packet_trailer::PacketTrailerHandler, video_frame::BoxVideoFrame,
31 video_track::RtcVideoTrack,
32 };
33 use tokio_stream::Stream;
34
35 const DEFAULT_QUEUE_SIZE_FRAMES: usize = 1;
36
37 #[derive(Clone, Debug, Default)]
38 pub struct NativeVideoStreamOptions {
39 /// Maximum number of queued WebRTC sink frames after the video callback.
40 ///
41 /// `None` uses the default bounded queue size of 1 frame. `Some(0)`
42 /// opts into unbounded buffering. Positive values bound the queue, and
43 /// the stream drops the oldest queued frames on overflow so render
44 /// latency stays bounded.
45 ///
46 /// If your application consumes both audio and video, keep the queue
47 /// sizing strategy coordinated across both streams. Using a much larger
48 /// queue, or unbounded buffering, for only one of them can increase
49 /// end-to-end latency for that stream and cause audio/video drift.
50 pub queue_size_frames: Option<usize>,
51 }
52
53 pub struct NativeVideoStream {
54 pub(crate) handle: stream_imp::NativeVideoStream,
55 }
56
57 impl Debug for NativeVideoStream {
58 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
59 f.debug_struct("NativeVideoStream").field("track", &self.track()).finish()
60 }
61 }
62
63 impl NativeVideoStream {
64 pub fn new(video_track: RtcVideoTrack) -> Self {
65 Self {
66 handle: stream_imp::NativeVideoStream::new(
67 video_track,
68 Some(DEFAULT_QUEUE_SIZE_FRAMES),
69 ),
70 }
71 }
72
73 pub fn with_options(video_track: RtcVideoTrack, options: NativeVideoStreamOptions) -> Self {
74 Self {
75 handle: stream_imp::NativeVideoStream::new(
76 video_track,
77 normalize_queue_size_frames(options.queue_size_frames),
78 ),
79 }
80 }
81
82 /// Set the packet trailer handler for this stream.
83 ///
84 /// When set, each frame produced by this stream will have its
85 /// `user_timestamp` field populated by looking up the user
86 /// timestamp for each frame's RTP timestamp.
87 ///
88 /// Note: If the handler was already set on the `RtcVideoTrack`
89 /// before creating this stream, it is automatically wired up.
90 /// This method is only needed to override or set the handler
91 /// after construction.
92 pub fn set_packet_trailer_handler(&self, handler: PacketTrailerHandler) {
93 self.handle.set_packet_trailer_handler(handler);
94 }
95
96 pub fn track(&self) -> RtcVideoTrack {
97 self.handle.track()
98 }
99
100 pub fn close(&mut self) {
101 self.handle.close();
102 }
103 }
104
105 impl Stream for NativeVideoStream {
106 type Item = BoxVideoFrame;
107
108 fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
109 Pin::new(&mut self.get_mut().handle).poll_next(cx)
110 }
111 }
112
113 fn normalize_queue_size_frames(queue_size_frames: Option<usize>) -> Option<usize> {
114 match queue_size_frames {
115 None => Some(DEFAULT_QUEUE_SIZE_FRAMES),
116 Some(0) => None,
117 Some(value) => Some(value),
118 }
119 }
120}
121
122#[cfg(target_arch = "wasm32")]
123pub mod web {}