| 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 | |
| 15 | use crate::audio_frame::AudioFrame; |
| 16 | use cxx::UniquePtr; |
| 17 | use std::sync::Arc; |
| 18 | use webrtc_sys::audio_mixer as sys; |
| 19 | use webrtc_sys::audio_mixer::ffi; |
| 20 | |
| 21 | pub struct AudioMixer { |
| 22 | sys_handle: UniquePtr<ffi::AudioMixer>, |
| 23 | } |
| 24 | |
| 25 | pub use ffi::AudioFrameInfo; |
| 26 | |
| 27 | pub trait AudioMixerSource { |
| 28 | fn ssrc(&self) -> i32; |
| 29 | fn preferred_sample_rate(&self) -> u32; |
| 30 | fn get_audio_frame_with_info(&self, target_sample_rate: u32) -> Option<AudioFrame<'_>>; |
| 31 | } |
| 32 | |
| 33 | struct AudioMixerSourceImpl<T> { |
| 34 | inner: T, |
| 35 | } |
| 36 | impl<T: AudioMixerSource> sys::AudioMixerSource for AudioMixerSourceImpl<T> { |
| 37 | fn ssrc(&self) -> i32 { |
| 38 | self.inner.ssrc() |
| 39 | } |
| 40 | |
| 41 | fn preferred_sample_rate(&self) -> i32 { |
| 42 | self.inner.preferred_sample_rate() as i32 |
| 43 | } |
| 44 | |
| 45 | fn get_audio_frame_with_info( |
| 46 | &self, |
| 47 | target_sample_rate: i32, |
| 48 | native_frame: sys::NativeAudioFrame, |
| 49 | ) -> AudioFrameInfo { |
| 50 | if let Some(frame) = self.inner.get_audio_frame_with_info(target_sample_rate as u32) { |
| 51 | let samples_count = (frame.sample_rate as usize / 100) as usize; |
| 52 | assert_eq!( |
| 53 | frame.sample_rate, target_sample_rate as u32, |
| 54 | "sample rate must match target_sample_rate" |
| 55 | ); |
| 56 | assert_eq!( |
| 57 | frame.samples_per_channel as usize, samples_count, |
| 58 | "frame must contain 10ms of samples" |
| 59 | ); |
| 60 | assert_eq!( |
| 61 | frame.data.len(), |
| 62 | samples_count * frame.num_channels as usize, |
| 63 | "slice must contain 10ms of samples" |
| 64 | ); |
| 65 | |
| 66 | unsafe { |
| 67 | native_frame.update_frame( |
| 68 | 0, |
| 69 | frame.data.as_ptr(), |
| 70 | frame.samples_per_channel as usize, |
| 71 | frame.sample_rate as i32, |
| 72 | frame.num_channels as usize, |
| 73 | ); |
| 74 | } |
| 75 | return ffi::AudioFrameInfo::Normal; |
| 76 | } else { |
| 77 | return ffi::AudioFrameInfo::Muted; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | impl AudioMixer { |
| 83 | pub fn new() -> Self { |
| 84 | let sys_handle = ffi::create_audio_mixer(); |
| 85 | Self { sys_handle } |
| 86 | } |
| 87 | |
| 88 | pub fn add_source(&mut self, source: impl AudioMixerSource + 'static) { |
| 89 | let source_impl = AudioMixerSourceImpl { inner: source }; |
| 90 | let wrapper = Box::new(sys::AudioMixerSourceWrapper::new(Arc::new(source_impl))); |
| 91 | unsafe { |
| 92 | self.sys_handle.pin_mut().add_source(wrapper); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | pub fn remove_source(&mut self, ssrc: i32) { |
| 97 | unsafe { |
| 98 | self.sys_handle.pin_mut().remove_source(ssrc); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | pub fn mix(&mut self, num_channels: usize) -> &[i16] { |
| 103 | unsafe { |
| 104 | let len = self.sys_handle.pin_mut().mix(num_channels); |
| 105 | std::slice::from_raw_parts(self.sys_handle.data(), len) |
| 106 | } |
| 107 | } |
| 108 | } |