Skip to main content

gosuto_libwebrtc/
video_stream.rs

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,
24        pin::Pin,
25        task::{Context, Poll},
26    };
27
28    use super::stream_imp;
29    use crate::{video_frame::BoxVideoFrame, video_track::RtcVideoTrack};
30    use livekit_runtime::Stream;
31
32    pub struct NativeVideoStream {
33        pub(crate) handle: stream_imp::NativeVideoStream,
34    }
35
36    impl Debug for NativeVideoStream {
37        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
38            f.debug_struct("NativeVideoStream").field("track", &self.track()).finish()
39        }
40    }
41
42    impl NativeVideoStream {
43        pub fn new(video_track: RtcVideoTrack) -> Self {
44            Self { handle: stream_imp::NativeVideoStream::new(video_track) }
45        }
46
47        pub fn track(&self) -> RtcVideoTrack {
48            self.handle.track()
49        }
50
51        pub fn close(&mut self) {
52            self.handle.close();
53        }
54    }
55
56    impl Stream for NativeVideoStream {
57        type Item = BoxVideoFrame;
58
59        fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
60            Pin::new(&mut self.get_mut().handle).poll_next(cx)
61        }
62    }
63}
64
65#[cfg(target_arch = "wasm32")]
66pub mod web {}