feat: add ffi datachannel & mute events (#88)

This commit is contained in:
Théo Monnom
2023-06-18 22:45:05 +02:00
committed by GitHub
parent 33dfcb27b0
commit 25f4c9a075
69 changed files with 1934 additions and 1546 deletions
+33 -6
View File
@@ -1,9 +1,29 @@
use crate::audio_frame::AudioFrame;
use crate::{audio_frame::AudioFrame, audio_source::AudioSourceOptions};
use cxx::SharedPtr;
use parking_lot::Mutex;
use std::sync::Arc;
use webrtc_sys::audio_track as sys_at;
impl From<sys_at::ffi::AudioSourceOptions> for AudioSourceOptions {
fn from(options: sys_at::ffi::AudioSourceOptions) -> Self {
Self {
echo_cancellation: options.echo_cancellation,
noise_suppression: options.noise_suppression,
auto_gain_control: options.auto_gain_control,
}
}
}
impl From<AudioSourceOptions> for sys_at::ffi::AudioSourceOptions {
fn from(options: AudioSourceOptions) -> Self {
Self {
echo_cancellation: options.echo_cancellation,
noise_suppression: options.noise_suppression,
auto_gain_control: options.auto_gain_control,
}
}
}
#[derive(Clone)]
pub struct NativeAudioSource {
sys_handle: SharedPtr<sys_at::ffi::AudioTrackSource>,
@@ -18,20 +38,27 @@ struct AudioSourceInner {
num_channels: u32,
}
impl Default for NativeAudioSource {
fn default() -> Self {
impl NativeAudioSource {
pub fn new(options: AudioSourceOptions) -> NativeAudioSource {
Self {
sys_handle: sys_at::ffi::new_audio_track_source(),
sys_handle: sys_at::ffi::new_audio_track_source(options.into()),
inner: Default::default(),
}
}
}
impl NativeAudioSource {
pub fn sys_handle(&self) -> SharedPtr<sys_at::ffi::AudioTrackSource> {
self.sys_handle.clone()
}
pub fn set_audio_options(&self, options: AudioSourceOptions) {
self.sys_handle
.set_audio_options(&sys_at::ffi::AudioSourceOptions::from(options))
}
pub fn audio_options(&self) -> AudioSourceOptions {
self.sys_handle.audio_options().into()
}
pub fn capture_frame(&self, frame: &AudioFrame) {
let mut inner = self.inner.lock();
let samples_10ms = (frame.sample_rate / 100 * frame.num_channels) as usize;
+38 -5
View File
@@ -1,22 +1,42 @@
use crate::video_frame::{VideoFrame, VideoFrameBuffer};
use crate::video_source::VideoResolution;
use cxx::SharedPtr;
use std::time::{SystemTime, UNIX_EPOCH};
use webrtc_sys::video_frame as vf_sys;
use webrtc_sys::video_track as vt_sys;
impl From<vt_sys::ffi::VideoResolution> for VideoResolution {
fn from(res: vt_sys::ffi::VideoResolution) -> Self {
Self {
width: res.width,
height: res.height,
}
}
}
impl From<VideoResolution> for vt_sys::ffi::VideoResolution {
fn from(res: VideoResolution) -> Self {
Self {
width: res.width,
height: res.height,
}
}
}
#[derive(Clone)]
pub struct NativeVideoSource {
sys_handle: SharedPtr<vt_sys::ffi::VideoTrackSource>,
}
impl Default for NativeVideoSource {
fn default() -> Self {
impl NativeVideoSource {
pub fn new(resolution: VideoResolution) -> NativeVideoSource {
Self {
sys_handle: vt_sys::ffi::new_video_track_source(),
sys_handle: vt_sys::ffi::new_video_track_source(&vt_sys::ffi::VideoResolution::from(
resolution,
)),
}
}
}
impl NativeVideoSource {
pub fn sys_handle(&self) -> SharedPtr<vt_sys::ffi::VideoTrackSource> {
self.sys_handle.clone()
}
@@ -27,7 +47,20 @@ impl NativeVideoSource {
builder
.pin_mut()
.set_video_frame_buffer(frame.buffer.as_ref().sys_handle());
if frame.timestamp_us == 0 {
// If the timestamp is set to 0, default to now
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
builder.pin_mut().set_timestamp_us(now.as_micros() as i64);
} else {
builder.pin_mut().set_timestamp_us(frame.timestamp_us);
}
self.sys_handle
.on_captured_frame(&builder.pin_mut().build());
}
pub fn video_resolution(&self) -> VideoResolution {
self.sys_handle.video_resolution().into()
}
}
+1 -1
View File
@@ -67,7 +67,7 @@ impl sys_vt::VideoSink for VideoTrackObserver {
fn on_frame(&self, frame: UniquePtr<webrtc_sys::video_frame::ffi::VideoFrame>) {
let _ = self.frame_tx.send(VideoFrame {
rotation: frame.rotation().into(),
timestamp: frame.timestamp_us(),
timestamp_us: frame.timestamp_us(),
buffer: new_video_frame_buffer(unsafe { frame.video_frame_buffer() }),
});
}