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

API for pre-encoded audio tracks #341

Closed
wants to merge 3 commits into from
Closed
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
12 changes: 6 additions & 6 deletions examples/play_from_disk/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion examples/play_from_disk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
header.num_channels as u32,
);

let track = LocalAudioTrack::create_audio_track("file", RtcAudioSource::Native(source.clone()));
let native_source = RtcAudioSource::Native(source.clone());
// ...just to show an API and emphasis the source is *not* pre-encoded,
// otherwise set pre_encoded = true
native_source.set_audio_options(AudioSourceOptions{
pre_encoded: false,
..Default::default()
});

let track = LocalAudioTrack::create_audio_track("file", native_source);

room.local_participant()
.publish_track(
Expand Down
2 changes: 2 additions & 0 deletions examples/save_to_disk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ livekit = { path = "../../livekit", version = "0.3.2" }
bytes = "1.4.0"
tokio-util = "0.7.8"
futures = "0.3.28"

[workspace]
9 changes: 5 additions & 4 deletions libwebrtc/src/audio_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct AudioSourceOptions {
pub echo_cancellation: bool,
pub noise_suppression: bool,
pub auto_gain_control: bool,
pub pre_encoded: bool,
}

#[non_exhaustive]
Expand All @@ -33,10 +34,10 @@ pub enum RtcAudioSource {
impl RtcAudioSource {
enum_dispatch!(
[Native];
fn set_audio_options(self: &Self, options: AudioSourceOptions) -> ();
fn audio_options(self: &Self) -> AudioSourceOptions;
fn sample_rate(self: &Self) -> u32;
fn num_channels(self: &Self) -> u32;
pub fn set_audio_options(self: &Self, options: AudioSourceOptions) -> ();
pub fn audio_options(self: &Self) -> AudioSourceOptions;
pub fn sample_rate(self: &Self) -> u32;
pub fn num_channels(self: &Self) -> u32;
);
}

Expand Down
2 changes: 2 additions & 0 deletions libwebrtc/src/native/audio_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ impl From<sys_at::ffi::AudioSourceOptions> for AudioSourceOptions {
echo_cancellation: options.echo_cancellation,
noise_suppression: options.noise_suppression,
auto_gain_control: options.auto_gain_control,
pre_encoded: options.pre_encoded,
}
}
}
Expand All @@ -187,6 +188,7 @@ impl From<AudioSourceOptions> for sys_at::ffi::AudioSourceOptions {
echo_cancellation: options.echo_cancellation,
noise_suppression: options.noise_suppression,
auto_gain_control: options.auto_gain_control,
pre_encoded: options.pre_encoded,
}
}
}
7 changes: 4 additions & 3 deletions livekit-ffi/protocol/audio_frame.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ message NewAudioSourceRequest {
}
message NewAudioSourceResponse { OwnedAudioSource source = 1; }

// Push a frame to an AudioSource
// Push a frame to an AudioSource
// The data provided must be available as long as the client receive the callback.
message CaptureAudioFrameRequest {
message CaptureAudioFrameRequest {
uint64 source_handle = 1;
AudioFrameBufferInfo buffer = 2;
}
Expand Down Expand Up @@ -104,7 +104,7 @@ message OwnedAudioStream {

message AudioStreamEvent {
uint64 stream_handle = 1;
oneof message {
oneof message {
AudioFrameReceived frame_received = 2;
AudioStreamEOS eos = 3;
}
Expand All @@ -124,6 +124,7 @@ message AudioSourceOptions {
bool echo_cancellation = 1;
bool noise_suppression = 2;
bool auto_gain_control = 3;
bool pre_encoded = 4;
}

enum AudioSourceType {
Expand Down
1 change: 1 addition & 0 deletions livekit-ffi/src/conversion/audio_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl From<proto::AudioSourceOptions> for AudioSourceOptions {
echo_cancellation: opts.echo_cancellation,
auto_gain_control: opts.auto_gain_control,
noise_suppression: opts.noise_suppression,
pre_encoded: opts.pre_encoded,
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions livekit-ffi/src/livekit.proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ impl EncryptionState {
/// # Safety
/// The foreign language is responsable for disposing handles
/// Forgetting to dispose the handle may lead to memory leaks
///
///
/// Dropping a handle doesn't necessarily mean that the object is destroyed if it is still used
/// on the FfiServer (Atomic reference counting)
///
/// When refering to a handle without owning it, we just use a uint32 without this message.
///
/// When refering to a handle without owning it, we just use a uint32 without this message.
/// (the variable name is suffixed with "_handle")
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down Expand Up @@ -1549,7 +1549,7 @@ pub struct NewVideoSourceRequest {
#[prost(enumeration="VideoSourceType", tag="1")]
pub r#type: i32,
/// Used to determine which encodings to use + simulcast layers
/// Most of the time it corresponds to the source resolution
/// Most of the time it corresponds to the source resolution
#[prost(message, optional, tag="2")]
pub resolution: ::core::option::Option<VideoSourceResolution>,
}
Expand Down Expand Up @@ -2716,7 +2716,7 @@ pub struct NewAudioSourceResponse {
#[prost(message, optional, tag="1")]
pub source: ::core::option::Option<OwnedAudioSource>,
}
/// Push a frame to an AudioSource
/// Push a frame to an AudioSource
/// The data provided must be available as long as the client receive the callback.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down Expand Up @@ -2851,6 +2851,8 @@ pub struct AudioSourceOptions {
pub noise_suppression: bool,
#[prost(bool, tag="3")]
pub auto_gain_control: bool,
#[prost(bool, tag="4")]
pub pre_encoded: bool,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down Expand Up @@ -2955,7 +2957,7 @@ impl AudioSourceType {
// that it receives from the server.
//
// Therefore, the ffi client is easier to implement if there is less handles to manage.
//
//
// - We are mainly using FfiHandle on info messages (e.g: RoomInfo, TrackInfo, etc...)
// For this reason, info are only sent once, at creation (We're not using them for updates, we can infer them from
// events on the client implementation).
Expand Down
4 changes: 2 additions & 2 deletions livekit-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ description = "Livekit protocol and utilities for the Rust SDK"
repository = "https://github.com/livekit/rust-sdks"

[dependencies]
livekit-runtime = { path = "../livekit-runtime", version = "0.3.0" }
tokio = { version = "1", default-features = false, features = [ "sync", "macros" ] }
livekit-runtime = { path = "../livekit-runtime", version = "0.3.0", features = [ "tokio" ] }
tokio = { version = "1", default-features = false, features = [ "sync", "macros", "time", "net" ] }
futures-util = { version = "0.3", features = ["sink"] }
parking_lot = "0.12"
prost = "0.12"
Expand Down
2 changes: 1 addition & 1 deletion webrtc-sys/src/audio_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

#include "livekit/audio_device.h"

const int kBytesPerSample = 2;
const int kSampleRate = 48000;
const int kChannels = 2;
const int kBytesPerSample = kChannels * sizeof(int16_t);
const int kSamplesPer10Ms = kSampleRate / 100;

namespace livekit {
Expand Down
2 changes: 2 additions & 0 deletions webrtc-sys/src/audio_track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ inline cricket::AudioOptions to_native_audio_options(
rtc_options.echo_cancellation = options.echo_cancellation;
rtc_options.noise_suppression = options.noise_suppression;
rtc_options.auto_gain_control = options.auto_gain_control;
rtc_options.pre_encoded = options.pre_encoded;
return rtc_options;
}

Expand All @@ -48,6 +49,7 @@ inline AudioSourceOptions to_rust_audio_options(
options.echo_cancellation = rtc_options.echo_cancellation.value_or(false);
options.noise_suppression = rtc_options.noise_suppression.value_or(false);
options.auto_gain_control = rtc_options.auto_gain_control.value_or(false);
options.pre_encoded = rtc_options.pre_encoded.value_or(false);
return options;
}

Expand Down
1 change: 1 addition & 0 deletions webrtc-sys/src/audio_track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod ffi {
pub echo_cancellation: bool,
pub noise_suppression: bool,
pub auto_gain_control: bool,
pub pre_encoded: bool,
}

extern "C++" {
Expand Down
Loading