Skip to content

Commit

Permalink
ffi: be more flexible with optional options (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
theomonnom authored Oct 25, 2024
1 parent 2aa9500 commit a556863
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 93 deletions.
12 changes: 6 additions & 6 deletions livekit-ffi/protocol/audio_frame.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ import "track.proto";
message NewAudioStreamRequest {
required uint64 track_handle = 1;
required AudioStreamType type = 2;
required uint32 sample_rate = 3;
required uint32 num_channels = 4;
optional uint32 sample_rate = 3;
optional uint32 num_channels = 4;
}
message NewAudioStreamResponse { required OwnedAudioStream stream = 1; }

message AudioStreamFromParticipantRequest {
required uint64 participant_handle = 1;
required AudioStreamType type = 2;
optional TrackSource track_source = 3;
required uint32 sample_rate = 5;
required uint32 num_channels = 6;
optional uint32 sample_rate = 5;
optional uint32 num_channels = 6;
}

message AudioStreamFromParticipantResponse { required OwnedAudioStream stream = 1; }
Expand All @@ -46,7 +46,7 @@ message NewAudioSourceRequest {
optional AudioSourceOptions options = 2;
required uint32 sample_rate = 3;
required uint32 num_channels = 4;
required uint32 queue_size_ms = 5;
optional uint32 queue_size_ms = 5;
}
message NewAudioSourceResponse { required OwnedAudioSource source = 1; }

Expand Down Expand Up @@ -97,7 +97,7 @@ message NewSoxResamplerRequest {
required SoxResamplerDataType input_data_type = 4;
required SoxResamplerDataType output_data_type = 5;
required SoxQualityRecipe quality_recipe = 6;
required uint32 flags = 7;
optional uint32 flags = 7;
}
message NewSoxResamplerResponse {
oneof message {
Expand Down
20 changes: 10 additions & 10 deletions livekit-ffi/protocol/room.proto
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,12 @@ message TrackPublishOptions {
// encodings are optional
optional VideoEncoding video_encoding = 1;
optional AudioEncoding audio_encoding = 2;
required VideoCodec video_codec = 3;
required bool dtx = 4;
required bool red = 5;
required bool simulcast = 6;
required TrackSource source = 7;
required string stream = 8;
optional VideoCodec video_codec = 3;
optional bool dtx = 4;
optional bool red = 5;
optional bool simulcast = 6;
optional TrackSource source = 7;
optional string stream = 8;
}

enum IceTransportType {
Expand All @@ -286,12 +286,12 @@ message RtcConfig {
}

message RoomOptions {
required bool auto_subscribe = 1;
required bool adaptive_stream = 2;
required bool dynacast = 3;
optional bool auto_subscribe = 1;
optional bool adaptive_stream = 2;
optional bool dynacast = 3;
optional E2eeOptions e2ee = 4;
optional RtcConfig rtc_config = 5; // allow to setup a custom RtcConfiguration
required uint32 join_retries = 6;
optional uint32 join_retries = 6;
}

//
Expand Down
6 changes: 3 additions & 3 deletions livekit-ffi/protocol/video_frame.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ message NewVideoStreamRequest {
required VideoStreamType type = 2;
// Get the frame on a specific format
optional VideoBufferType format = 3;
required bool normalize_stride = 4; // if true, stride will be set to width/chroma_width
optional bool normalize_stride = 4; // if true, stride will be set to width/chroma_width
}
message NewVideoStreamResponse { required OwnedVideoStream stream = 1; }

Expand All @@ -37,7 +37,7 @@ message VideoStreamFromParticipantRequest {
required VideoStreamType type = 2;
required TrackSource track_source = 3;
optional VideoBufferType format = 4;
required bool normalize_stride = 5;
optional bool normalize_stride = 5;
}

message VideoStreamFromParticipantResponse { required OwnedVideoStream stream = 1;}
Expand All @@ -63,7 +63,7 @@ message CaptureVideoFrameRequest {
message CaptureVideoFrameResponse {}

message VideoConvertRequest {
required bool flip_y = 1;
optional bool flip_y = 1;
required VideoBufferInfo buffer = 2;
required VideoBufferType dst_type = 3;
}
Expand Down
37 changes: 23 additions & 14 deletions livekit-ffi/src/conversion/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,12 @@ impl From<proto::RoomOptions> for RoomOptions {
value.rtc_config.map(Into::into).unwrap_or(RoomOptions::default().rtc_config);

let mut options = RoomOptions::default();
options.adaptive_stream = value.adaptive_stream;
options.auto_subscribe = value.auto_subscribe;
options.dynacast = value.dynacast;
options.e2ee = e2ee;
options.adaptive_stream = value.adaptive_stream.unwrap_or(options.adaptive_stream);
options.auto_subscribe = value.auto_subscribe.unwrap_or(options.auto_subscribe);
options.dynacast = value.dynacast.unwrap_or(options.dynacast);
options.rtc_config = rtc_config;
options.join_retries = value.join_retries;
options.sdk_options = RoomSdkOptions::default();
options.join_retries = value.join_retries.unwrap_or(options.join_retries);
options.e2ee = e2ee;
options
}
}
Expand All @@ -208,15 +207,25 @@ impl From<DataPacketKind> for proto::DataPacketKind {

impl From<proto::TrackPublishOptions> for TrackPublishOptions {
fn from(opts: proto::TrackPublishOptions) -> Self {
let default_publish_options = TrackPublishOptions::default();
let video_codec = opts.video_codec.map(|x| proto::VideoCodec::try_from(x).ok()).flatten();
let source = opts.source.map(|x| proto::TrackSource::try_from(x).ok()).flatten();

Self {
video_codec: opts.video_codec().into(),
source: opts.source().into(),
video_encoding: opts.video_encoding.map(Into::into),
audio_encoding: opts.audio_encoding.map(Into::into),
dtx: opts.dtx,
red: opts.red,
simulcast: opts.simulcast,
stream: opts.stream,
video_codec: video_codec.map(Into::into).unwrap_or(default_publish_options.video_codec),
source: source.map(Into::into).unwrap_or(default_publish_options.source),
video_encoding: opts
.video_encoding
.map(Into::into)
.or(default_publish_options.video_encoding),
audio_encoding: opts
.audio_encoding
.map(Into::into)
.or(default_publish_options.audio_encoding),
dtx: opts.dtx.unwrap_or(default_publish_options.dtx),
red: opts.red.unwrap_or(default_publish_options.red),
simulcast: opts.simulcast.unwrap_or(default_publish_options.simulcast),
stream: opts.stream.unwrap_or(default_publish_options.stream),
}
}
}
Expand Down
76 changes: 38 additions & 38 deletions livekit-ffi/src/livekit.proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1601,8 +1601,8 @@ pub struct NewVideoStreamRequest {
#[prost(enumeration="VideoBufferType", optional, tag="3")]
pub format: ::core::option::Option<i32>,
/// if true, stride will be set to width/chroma_width
#[prost(bool, required, tag="4")]
pub normalize_stride: bool,
#[prost(bool, optional, tag="4")]
pub normalize_stride: ::core::option::Option<bool>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand All @@ -1622,8 +1622,8 @@ pub struct VideoStreamFromParticipantRequest {
pub track_source: i32,
#[prost(enumeration="VideoBufferType", optional, tag="4")]
pub format: ::core::option::Option<i32>,
#[prost(bool, required, tag="5")]
pub normalize_stride: bool,
#[prost(bool, optional, tag="5")]
pub normalize_stride: ::core::option::Option<bool>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down Expand Up @@ -1670,8 +1670,8 @@ pub struct CaptureVideoFrameResponse {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct VideoConvertRequest {
#[prost(bool, required, tag="1")]
pub flip_y: bool,
#[prost(bool, optional, tag="1")]
pub flip_y: ::core::option::Option<bool>,
#[prost(message, required, tag="2")]
pub buffer: VideoBufferInfo,
#[prost(enumeration="VideoBufferType", required, tag="3")]
Expand Down Expand Up @@ -2423,18 +2423,18 @@ pub struct TrackPublishOptions {
pub video_encoding: ::core::option::Option<VideoEncoding>,
#[prost(message, optional, tag="2")]
pub audio_encoding: ::core::option::Option<AudioEncoding>,
#[prost(enumeration="VideoCodec", required, tag="3")]
pub video_codec: i32,
#[prost(bool, required, tag="4")]
pub dtx: bool,
#[prost(bool, required, tag="5")]
pub red: bool,
#[prost(bool, required, tag="6")]
pub simulcast: bool,
#[prost(enumeration="TrackSource", required, tag="7")]
pub source: i32,
#[prost(string, required, tag="8")]
pub stream: ::prost::alloc::string::String,
#[prost(enumeration="VideoCodec", optional, tag="3")]
pub video_codec: ::core::option::Option<i32>,
#[prost(bool, optional, tag="4")]
pub dtx: ::core::option::Option<bool>,
#[prost(bool, optional, tag="5")]
pub red: ::core::option::Option<bool>,
#[prost(bool, optional, tag="6")]
pub simulcast: ::core::option::Option<bool>,
#[prost(enumeration="TrackSource", optional, tag="7")]
pub source: ::core::option::Option<i32>,
#[prost(string, optional, tag="8")]
pub stream: ::core::option::Option<::prost::alloc::string::String>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand All @@ -2460,19 +2460,19 @@ pub struct RtcConfig {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct RoomOptions {
#[prost(bool, required, tag="1")]
pub auto_subscribe: bool,
#[prost(bool, required, tag="2")]
pub adaptive_stream: bool,
#[prost(bool, required, tag="3")]
pub dynacast: bool,
#[prost(bool, optional, tag="1")]
pub auto_subscribe: ::core::option::Option<bool>,
#[prost(bool, optional, tag="2")]
pub adaptive_stream: ::core::option::Option<bool>,
#[prost(bool, optional, tag="3")]
pub dynacast: ::core::option::Option<bool>,
#[prost(message, optional, tag="4")]
pub e2ee: ::core::option::Option<E2eeOptions>,
/// allow to setup a custom RtcConfiguration
#[prost(message, optional, tag="5")]
pub rtc_config: ::core::option::Option<RtcConfig>,
#[prost(uint32, required, tag="6")]
pub join_retries: u32,
#[prost(uint32, optional, tag="6")]
pub join_retries: ::core::option::Option<u32>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down Expand Up @@ -3069,10 +3069,10 @@ pub struct NewAudioStreamRequest {
pub track_handle: u64,
#[prost(enumeration="AudioStreamType", required, tag="2")]
pub r#type: i32,
#[prost(uint32, required, tag="3")]
pub sample_rate: u32,
#[prost(uint32, required, tag="4")]
pub num_channels: u32,
#[prost(uint32, optional, tag="3")]
pub sample_rate: ::core::option::Option<u32>,
#[prost(uint32, optional, tag="4")]
pub num_channels: ::core::option::Option<u32>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand All @@ -3089,10 +3089,10 @@ pub struct AudioStreamFromParticipantRequest {
pub r#type: i32,
#[prost(enumeration="TrackSource", optional, tag="3")]
pub track_source: ::core::option::Option<i32>,
#[prost(uint32, required, tag="5")]
pub sample_rate: u32,
#[prost(uint32, required, tag="6")]
pub num_channels: u32,
#[prost(uint32, optional, tag="5")]
pub sample_rate: ::core::option::Option<u32>,
#[prost(uint32, optional, tag="6")]
pub num_channels: ::core::option::Option<u32>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand All @@ -3112,8 +3112,8 @@ pub struct NewAudioSourceRequest {
pub sample_rate: u32,
#[prost(uint32, required, tag="4")]
pub num_channels: u32,
#[prost(uint32, required, tag="5")]
pub queue_size_ms: u32,
#[prost(uint32, optional, tag="5")]
pub queue_size_ms: ::core::option::Option<u32>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down Expand Up @@ -3202,8 +3202,8 @@ pub struct NewSoxResamplerRequest {
pub output_data_type: i32,
#[prost(enumeration="SoxQualityRecipe", required, tag="6")]
pub quality_recipe: i32,
#[prost(uint32, required, tag="7")]
pub flags: u32,
#[prost(uint32, optional, tag="7")]
pub flags: ::core::option::Option<u32>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down
2 changes: 1 addition & 1 deletion livekit-ffi/src/server/audio_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl FfiAudioSource {
new_source.options.map(Into::into).unwrap_or_default(),
new_source.sample_rate,
new_source.num_channels,
new_source.queue_size_ms,
new_source.queue_size_ms.unwrap_or(1000),
);
RtcAudioSource::Native(audio_source)
}
Expand Down
19 changes: 6 additions & 13 deletions livekit-ffi/src/server/audio_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,8 @@ impl FfiAudioStream {
#[cfg(not(target_arch = "wasm32"))]
proto::AudioStreamType::AudioStreamNative => {
let audio_stream = Self { handle_id, stream_type, self_dropped_tx };

let sample_rate =
if new_stream.sample_rate == 0 { 48000 } else { new_stream.sample_rate as i32 };

let num_channels =
if new_stream.num_channels == 0 { 1 } else { new_stream.num_channels as i32 };
let sample_rate = new_stream.sample_rate.unwrap_or(48000);
let num_channels = new_stream.num_channels.unwrap_or(1);

let native_stream =
NativeAudioStream::new(rtc_track, sample_rate as i32, num_channels as i32);
Expand All @@ -85,7 +81,7 @@ impl FfiAudioStream {
let info = proto::AudioStreamInfo::from(&audio_stream);
server.store_handle(handle_id, audio_stream);

Ok(proto::OwnedAudioStream { handle: proto::FfiOwnedHandle { id: handle_id }, info: info })
Ok(proto::OwnedAudioStream { handle: proto::FfiOwnedHandle { id: handle_id }, info })
}

pub fn from_participant(
Expand Down Expand Up @@ -116,7 +112,7 @@ impl FfiAudioStream {
let info = proto::AudioStreamInfo::from(&audio_stream);
server.store_handle(handle_id, audio_stream);

Ok(proto::OwnedAudioStream { handle: proto::FfiOwnedHandle { id: handle_id }, info: info })
Ok(proto::OwnedAudioStream { handle: proto::FfiOwnedHandle { id: handle_id }, info })
}

async fn participant_audio_stream_task(
Expand Down Expand Up @@ -156,11 +152,8 @@ impl FfiAudioStream {
let (c_tx, c_rx) = oneshot::channel::<()>();
let (handle_dropped_tx, handle_dropped_rx) = oneshot::channel::<()>();
let (done_tx, mut done_rx) = oneshot::channel::<()>();
let sample_rate =
if request.sample_rate == 0 { 48000 } else { request.sample_rate as i32 };

let num_channels =
if request.num_channels == 0 { 1 } else { request.num_channels as i32 };
let sample_rate = request.sample_rate.unwrap_or(48000) as i32;
let num_channels = request.num_channels.unwrap_or(1) as i32;

let mut track_finished_rx = track_finished_tx.subscribe();
server.async_runtime.spawn(async move {
Expand Down
19 changes: 19 additions & 0 deletions livekit-ffi/src/server/colorcvt/cvtimpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ pub unsafe fn cvt_rgba(
);
Ok((dst, info))
}
proto::VideoBufferType::Bgra => {
let mut dst = vec![0u8; (width * height * 4) as usize].into_boxed_slice();
let stride = width * 4;

colorcvt::abgr_to_argb(data, stride, &mut dst, stride, width, height, flip_y);

let info = rgba_info(dst.as_ptr(), dst_type, width, height);
Ok((dst, info))
}
_ => {
Err(FfiError::InvalidRequest(format!("rgba to {:?} is not supported", dst_type).into()))
}
Expand Down Expand Up @@ -197,6 +206,16 @@ pub unsafe fn cvt_bgra(
chroma_w,
chroma_w,
);

Ok((dst, info))
}
proto::VideoBufferType::Rgba => {
let mut dst = vec![0u8; (width * height * 4) as usize].into_boxed_slice();
let stride = width * 4;

colorcvt::argb_to_abgr(data, stride, &mut dst, stride, width, height, flip_y);

let info = rgba_info(dst.as_ptr(), dst_type, width, height);
Ok((dst, info))
}
_ => {
Expand Down
Loading

0 comments on commit a556863

Please sign in to comment.