BlueGit

ict rusty-teams

Files

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

audio_stream.rs

audio_stream.rs 3.7 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::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 tokio_stream::Stream;
26
27 use super::stream_imp;
28 use crate::{audio_frame::AudioFrame, audio_track::RtcAudioTrack};
29
30 const DEFAULT_QUEUE_SIZE_FRAMES: usize = 10;
31
32 #[derive(Clone, Debug, Default)]
33 pub struct NativeAudioStreamOptions {
34 /// Maximum number of queued WebRTC sink frames after the audio callback.
35 ///
36 /// Each queued frame corresponds to roughly 10 ms of decoded PCM audio
37 /// on the WebRTC sink path.
38 ///
39 /// `None` uses the default bounded queue size of 10 frames. `Some(0)`
40 /// opts into unbounded buffering. Positive values bound the queue, and
41 /// the stream drops the oldest queued frames on overflow so latency
42 /// stays bounded.
43 ///
44 /// If your application consumes both audio and video, keep the queue
45 /// sizing strategy coordinated across both streams. Using a much larger
46 /// queue, or unbounded buffering, for only one of them can increase
47 /// end-to-end latency for that stream and cause audio/video drift.
48 pub queue_size_frames: Option<usize>,
49 }
50
51 pub struct NativeAudioStream {
52 pub(crate) handle: stream_imp::NativeAudioStream,
53 }
54
55 impl Debug for NativeAudioStream {
56 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
57 f.debug_struct("NativeAudioStream").field("track", &self.track()).finish()
58 }
59 }
60
61 impl NativeAudioStream {
62 pub fn new(audio_track: RtcAudioTrack, sample_rate: i32, num_channels: i32) -> Self {
63 Self {
64 handle: stream_imp::NativeAudioStream::new(
65 audio_track,
66 sample_rate,
67 num_channels,
68 Some(DEFAULT_QUEUE_SIZE_FRAMES),
69 ),
70 }
71 }
72
73 pub fn with_options(
74 audio_track: RtcAudioTrack,
75 sample_rate: i32,
76 num_channels: i32,
77 options: NativeAudioStreamOptions,
78 ) -> Self {
79 Self {
80 handle: stream_imp::NativeAudioStream::new(
81 audio_track,
82 sample_rate,
83 num_channels,
84 normalize_queue_size_frames(options.queue_size_frames),
85 ),
86 }
87 }
88
89 pub fn track(&self) -> RtcAudioTrack {
90 self.handle.track()
91 }
92
93 pub fn close(&mut self) {
94 self.handle.close()
95 }
96 }
97
98 impl Stream for NativeAudioStream {
99 type Item = AudioFrame<'static>;
100
101 fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
102 Pin::new(&mut self.get_mut().handle).poll_next(cx)
103 }
104 }
105
106 fn normalize_queue_size_frames(queue_size_frames: Option<usize>) -> Option<usize> {
107 match queue_size_frames {
108 None => Some(DEFAULT_QUEUE_SIZE_FRAMES),
109 Some(0) => None,
110 Some(value) => Some(value),
111 }
112 }
113}