Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rustfmt.toml configuration and re-format everything + fix basic warnings #279

Merged
merged 2 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Rust Formatting

on:
workflow_dispatch:
push:
branches: [main]
pull_request:
branches: [main]
types:
- opened
- reopened
- synchronize
- ready_for_review

env:
CARGO_TERM_COLOR: always

jobs:
formatting:
name: Check Formatting
runs-on: ubuntu-latest
if: github.event_name == 'push' || !github.event.pull_request.draft

steps:
- name: Checkout the repo
uses: actions/checkout@v3

- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly-2023-12-30
components: rustfmt

- name: Cargo fmt
run: |
cargo fmt -- --check
10 changes: 5 additions & 5 deletions libwebrtc/src/audio_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::imp::audio_source as imp_as;
use livekit_protocol::enum_dispatch;

use crate::imp::audio_source as imp_as;

#[derive(Default, Debug)]
pub struct AudioSourceOptions {
pub echo_cancellation: bool,
Expand All @@ -41,9 +42,10 @@ impl RtcAudioSource {

#[cfg(not(target_arch = "wasm32"))]
pub mod native {
use std::fmt::{Debug, Formatter};

use super::*;
use crate::{audio_frame::AudioFrame, RtcError};
use std::fmt::{Debug, Formatter};

#[derive(Clone)]
pub struct NativeAudioSource {
Expand All @@ -62,9 +64,7 @@ pub mod native {
sample_rate: u32,
num_channels: u32,
) -> NativeAudioSource {
Self {
handle: imp_as::NativeAudioSource::new(options, sample_rate, num_channels),
}
Self { handle: imp_as::NativeAudioSource::new(options, sample_rate, num_channels) }
}

pub async fn capture_frame(&self, frame: &AudioFrame<'_>) -> Result<(), RtcError> {
Expand Down
23 changes: 11 additions & 12 deletions libwebrtc/src/audio_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,30 @@ use crate::imp::audio_stream as stream_imp;

#[cfg(not(target_arch = "wasm32"))]
pub mod native {
use super::stream_imp;
use crate::audio_frame::AudioFrame;
use crate::audio_track::RtcAudioTrack;
use std::fmt::{Debug, Formatter};
use std::pin::Pin;
use std::task::{Context, Poll};
use std::{
fmt::{Debug, Formatter},
pin::Pin,
task::{Context, Poll},
};

use tokio_stream::Stream;

use super::stream_imp;
use crate::{audio_frame::AudioFrame, audio_track::RtcAudioTrack};

pub struct NativeAudioStream {
pub(crate) handle: stream_imp::NativeAudioStream,
}

impl Debug for NativeAudioStream {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
f.debug_struct("NativeAudioStream")
.field("track", &self.track())
.finish()
f.debug_struct("NativeAudioStream").field("track", &self.track()).finish()
}
}

impl NativeAudioStream {
pub fn new(audio_track: RtcAudioTrack) -> Self {
Self {
handle: stream_imp::NativeAudioStream::new(audio_track),
}
Self { handle: stream_imp::NativeAudioStream::new(audio_track) }
}

pub fn track(&self) -> RtcAudioTrack {
Expand Down
8 changes: 5 additions & 3 deletions libwebrtc/src/audio_track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::imp::audio_track as imp_at;
use crate::media_stream_track::media_stream_track;
use crate::media_stream_track::RtcTrackState;
use std::fmt::Debug;

use crate::{
imp::audio_track as imp_at,
media_stream_track::{media_stream_track, RtcTrackState},
};

#[derive(Clone)]
pub struct RtcAudioTrack {
pub(crate) handle: imp_at::RtcAudioTrack,
Expand Down
6 changes: 4 additions & 2 deletions libwebrtc/src/data_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{imp::data_channel as dc_imp, rtp_parameters::Priority};
use serde::Deserialize;
use std::{fmt::Debug, str::Utf8Error};

use serde::Deserialize;
use thiserror::Error;

use crate::{imp::data_channel as dc_imp, rtp_parameters::Priority};

#[derive(Clone, Debug)]
pub struct DataChannelInit {
pub ordered: bool,
Expand Down
7 changes: 3 additions & 4 deletions libwebrtc/src/ice_candidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{imp::ice_candidate as imp_ic, session_description::SdpParseError};
use std::fmt::Debug;

use crate::{imp::ice_candidate as imp_ic, session_description::SdpParseError};

pub struct IceCandidate {
pub(crate) handle: imp_ic::IceCandidate,
}
Expand Down Expand Up @@ -49,8 +50,6 @@ impl ToString for IceCandidate {

impl Debug for IceCandidate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("IceCandidate")
.field("candidate", &self.to_string())
.finish()
f.debug_struct("IceCandidate").field("candidate", &self.to_string()).finish()
}
}
5 changes: 2 additions & 3 deletions libwebrtc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ pub mod video_track;

#[cfg(not(target_arch = "wasm32"))]
pub mod native {
pub use crate::imp::audio_resampler;
pub use crate::imp::frame_cryptor;
pub use crate::imp::yuv_helper;
pub use webrtc_sys::webrtc::ffi::create_random_uuid;

pub use crate::imp::{audio_resampler, frame_cryptor, yuv_helper};
}

#[cfg(target_os = "android")]
Expand Down
5 changes: 2 additions & 3 deletions libwebrtc/src/media_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::audio_track::RtcAudioTrack;
use crate::imp::media_stream as imp_ms;
use crate::video_track::RtcVideoTrack;
use std::fmt::Debug;

use crate::{audio_track::RtcAudioTrack, imp::media_stream as imp_ms, video_track::RtcVideoTrack};

#[derive(Clone)]
pub struct MediaStream {
pub(crate) handle: imp_ms::MediaStream,
Expand Down
4 changes: 2 additions & 2 deletions libwebrtc/src/media_stream_track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::audio_track::RtcAudioTrack;
use crate::video_track::RtcVideoTrack;
use livekit_protocol::enum_dispatch;

use crate::{audio_track::RtcAudioTrack, video_track::RtcVideoTrack};

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum RtcTrackState {
Live,
Expand Down
9 changes: 2 additions & 7 deletions libwebrtc/src/native/audio_resampler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ pub struct AudioResampler {

impl Default for AudioResampler {
fn default() -> Self {
Self {
sys_handle: sys_ar::ffi::create_audio_resampler(),
}
Self { sys_handle: sys_ar::ffi::create_audio_resampler() }
}
}

Expand All @@ -37,10 +35,7 @@ impl AudioResampler {
dst_num_channels: u32,
dst_sample_rate: u32,
) -> &'a [i16] {
assert!(
src.len() >= (samples_per_channel * num_channels) as usize,
"src buffer too small"
);
assert!(src.len() >= (samples_per_channel * num_channels) as usize, "src buffer too small");

unsafe {
let len = self.sys_handle.pin_mut().remix_and_resample(
Expand Down
30 changes: 12 additions & 18 deletions libwebrtc/src/native/audio_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{audio_frame::AudioFrame, audio_source::AudioSourceOptions, RtcError, RtcErrorType};
use cxx::SharedPtr;
use std::{sync::Arc, time::Duration};

use cxx::SharedPtr;
use tokio::{
sync::{oneshot, Mutex as AsyncMutex, MutexGuard},
time::{interval, Instant, MissedTickBehavior},
};
use webrtc_sys::audio_track as sys_at;

use crate::{audio_frame::AudioFrame, audio_source::AudioSourceOptions, RtcError, RtcErrorType};

#[derive(Clone)]
pub struct NativeAudioSource {
sys_handle: SharedPtr<sys_at::ffi::AudioTrackSource>,
Expand Down Expand Up @@ -107,8 +109,7 @@ impl NativeAudioSource {
}

pub fn set_audio_options(&self, options: AudioSourceOptions) {
self.sys_handle
.set_audio_options(&sys_at::ffi::AudioSourceOptions::from(options))
self.sys_handle.set_audio_options(&sys_at::ffi::AudioSourceOptions::from(options))
}

pub fn audio_options(&self) -> AudioSourceOptions {
Expand All @@ -126,7 +127,8 @@ impl NativeAudioSource {
// Implemented inside another functions to allow unit testing
fn next_frame<'a>(
&self,
inner: &'a mut MutexGuard<'_, AudioSourceInner>, // The lock musts be guarded by capture_frame
inner: &'a mut MutexGuard<'_, AudioSourceInner>, /* The lock musts be guarded by
* capture_frame */
frame: &'a AudioFrame<'_>,
) -> Option<&'a [i16]> {
let available_data = inner.len + frame.data.len() - inner.read_offset;
Expand All @@ -147,7 +149,8 @@ impl NativeAudioSource {
&frame.data[start..end]
})
} else {
// Save to buf and wait for the next capture_frame to give enough data to complete a 10ms frame
// Save to buf and wait for the next capture_frame to give enough data to complete a
// 10ms frame
let remaining_data = frame.data.len() - inner.read_offset; // remaining data from frame.data
let start = inner.len;
let end = start + remaining_data;
Expand Down Expand Up @@ -296,18 +299,9 @@ mod tests {
let mut inner = source.inner.lock().await;

let samples_10ms = source.sample_rate() as usize / 100 * source.num_channels() as usize;
assert_eq!(
source.next_frame(&mut inner, &audio_frame).unwrap().len(),
samples_10ms
);
assert_eq!(
source.next_frame(&mut inner, &audio_frame).unwrap().len(),
samples_10ms
);
assert_eq!(
source.next_frame(&mut inner, &audio_frame).unwrap().len(),
samples_10ms
);
assert_eq!(source.next_frame(&mut inner, &audio_frame).unwrap().len(), samples_10ms);
assert_eq!(source.next_frame(&mut inner, &audio_frame).unwrap().len(), samples_10ms);
assert_eq!(source.next_frame(&mut inner, &audio_frame).unwrap().len(), samples_10ms);
assert!(source.next_frame(&mut inner, &audio_frame).is_none());

let samples_5ms = source.sample_rate() as usize / 1000 * 5 * source.num_channels() as usize;
Expand Down
19 changes: 9 additions & 10 deletions libwebrtc/src/native/audio_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::audio_frame::AudioFrame;
use crate::audio_track::RtcAudioTrack;
use std::{
pin::Pin,
sync::Arc,
task::{Context, Poll},
};

use cxx::SharedPtr;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use tokio::sync::mpsc;
use tokio_stream::Stream;
use webrtc_sys::audio_track as sys_at;

use crate::{audio_frame::AudioFrame, audio_track::RtcAudioTrack};

pub struct NativeAudioStream {
native_sink: SharedPtr<sys_at::ffi::NativeAudioSink>,
audio_track: RtcAudioTrack,
Expand All @@ -39,11 +42,7 @@ impl NativeAudioStream {
let audio = unsafe { sys_at::ffi::media_to_audio(audio_track.sys_handle()) };
audio.add_sink(&native_sink);

Self {
native_sink,
audio_track,
frame_rx,
}
Self { native_sink, audio_track, frame_rx }
}

pub fn track(&self) -> RtcAudioTrack {
Expand Down
5 changes: 3 additions & 2 deletions libwebrtc/src/native/audio_track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use super::media_stream_track::impl_media_stream_track;
use crate::media_stream_track::RtcTrackState;
use cxx::SharedPtr;
use sys_at::ffi::audio_to_media;
use webrtc_sys::audio_track as sys_at;

use super::media_stream_track::impl_media_stream_track;
use crate::media_stream_track::RtcTrackState;

#[derive(Clone)]
pub struct RtcAudioTrack {
pub(crate) sys_handle: SharedPtr<sys_at::ffi::AudioTrack>,
Expand Down
Loading
Loading