Skip to main content

gosuto_libwebrtc/
audio_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::audio_stream as stream_imp;
16
17#[cfg(not(target_arch = "wasm32"))]
18pub mod native {
19    use std::{
20        fmt::{Debug, Formatter},
21        pin::Pin,
22        task::{Context, Poll},
23    };
24
25    use livekit_runtime::Stream;
26
27    use super::stream_imp;
28    use crate::{audio_frame::AudioFrame, audio_track::RtcAudioTrack};
29
30    pub struct NativeAudioStream {
31        pub(crate) handle: stream_imp::NativeAudioStream,
32    }
33
34    impl Debug for NativeAudioStream {
35        fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
36            f.debug_struct("NativeAudioStream").field("track", &self.track()).finish()
37        }
38    }
39
40    impl NativeAudioStream {
41        pub fn new(audio_track: RtcAudioTrack, sample_rate: i32, num_channels: i32) -> Self {
42            Self {
43                handle: stream_imp::NativeAudioStream::new(audio_track, sample_rate, num_channels),
44            }
45        }
46
47        pub fn track(&self) -> RtcAudioTrack {
48            self.handle.track()
49        }
50
51        pub fn close(&mut self) {
52            self.handle.close()
53        }
54    }
55
56    impl Stream for NativeAudioStream {
57        type Item = AudioFrame<'static>;
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}