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

Compile flatbuffers for Rust in build.rs #1202

Merged
merged 1 commit into from
Oct 29, 2023
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
657 changes: 635 additions & 22 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ version = "1.6.0"

[dependencies.serde]
features = ["derive"]
version = "1.0.147"
version = "1.0.190"

[dependencies.uuid]
features = ["serde", "v4"]
Expand Down
2 changes: 1 addition & 1 deletion rust/src/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[cfg(test)]
mod tests;

use crate::fbs::{common, rtp_packet, sctp_association, transport, web_rtc_transport};
use mediasoup_sys::fbs::{common, rtp_packet, sctp_association, transport, web_rtc_transport};
use serde::de::{MapAccess, Visitor};
use serde::ser::SerializeStruct;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
Expand Down
80,106 changes: 0 additions & 80,106 deletions rust/src/fbs.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming this fbs.es file is being generated on compile time, is it gitignored?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, it doesn't exist in source tree at all, we write it into OUT_DIR that is specifically designed for that purpose just like the rest of stuff, see build.rs.

Read notes on https://doc.rust-lang.org/cargo/reference/environment-variables.html that contain OUT_DIR for details, there are a few of them there.

This file was deleted.

1 change: 0 additions & 1 deletion rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
//! and are a good place to start until we have demo apps built in Rust).

pub mod data_structures;
pub mod fbs;
mod macros;
mod messages;
#[doc(hidden)]
Expand Down
10 changes: 5 additions & 5 deletions rust/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ use crate::data_structures::{
ListenInfo, SctpState, TransportTuple,
};
use crate::direct_transport::DirectTransportOptions;
use crate::fbs::{
active_speaker_observer, audio_level_observer, consumer, data_consumer, data_producer,
direct_transport, message, notification, pipe_transport, plain_transport, producer, request,
response, router, rtp_observer, transport, web_rtc_server, web_rtc_transport, worker,
};
use crate::ortc::RtpMapping;
use crate::pipe_transport::PipeTransportOptions;
use crate::plain_transport::PlainTransportOptions;
Expand All @@ -35,6 +30,11 @@ use crate::webrtc_transport::{
WebRtcTransportListen, WebRtcTransportListenInfos, WebRtcTransportOptions,
};
use crate::worker::{ChannelMessageHandlers, WorkerDump, WorkerUpdateSettings};
use mediasoup_sys::fbs::{
active_speaker_observer, audio_level_observer, consumer, data_consumer, data_producer,
direct_transport, message, notification, pipe_transport, plain_transport, producer, request,
response, router, rtp_observer, transport, web_rtc_server, web_rtc_transport, worker,
};
use parking_lot::Mutex;
use planus::Builder;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion rust/src/ortc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::fbs::rtp_parameters;
use crate::rtp_parameters::{
MediaKind, MimeType, MimeTypeAudio, MimeTypeVideo, RtcpFeedback, RtcpParameters,
RtpCapabilities, RtpCapabilitiesFinalized, RtpCodecCapability, RtpCodecCapabilityFinalized,
Expand All @@ -8,6 +7,7 @@ use crate::rtp_parameters::{
};
use crate::scalability_modes::ScalabilityMode;
use crate::supported_rtp_capabilities;
use mediasoup_sys::fbs::rtp_parameters;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::collections::BTreeMap;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/router/active_speaker_observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
mod tests;

use crate::data_structures::AppData;
use crate::fbs::notification;
use crate::messages::{
RtpObserverAddProducerRequest, RtpObserverCloseRequest, RtpObserverPauseRequest,
RtpObserverRemoveProducerRequest, RtpObserverResumeRequest,
Expand All @@ -15,6 +14,7 @@ use async_executor::Executor;
use async_trait::async_trait;
use event_listener_primitives::{Bag, BagOnce, HandlerId};
use log::{debug, error};
use mediasoup_sys::fbs::notification;
use parking_lot::Mutex;
use serde::Deserialize;
use std::fmt;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/router/audio_level_observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
mod tests;

use crate::data_structures::AppData;
use crate::fbs::{audio_level_observer, notification};
use crate::messages::{
RtpObserverAddProducerRequest, RtpObserverCloseRequest, RtpObserverPauseRequest,
RtpObserverRemoveProducerRequest, RtpObserverResumeRequest,
Expand All @@ -15,6 +14,7 @@ use async_executor::Executor;
use async_trait::async_trait;
use event_listener_primitives::{Bag, BagOnce, HandlerId};
use log::{debug, error};
use mediasoup_sys::fbs::{audio_level_observer, notification};
use parking_lot::Mutex;
use serde::Deserialize;
use std::fmt;
Expand Down
4 changes: 3 additions & 1 deletion rust/src/router/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
mod tests;

use crate::data_structures::{AppData, RtpPacketTraceInfo, SsrcTraceInfo, TraceEventDirection};
use crate::fbs::{consumer, notification, response, rtp_parameters, rtp_stream, rtx_stream};
use crate::messages::{
ConsumerCloseRequest, ConsumerDumpRequest, ConsumerEnableTraceEventRequest,
ConsumerGetStatsRequest, ConsumerPauseRequest, ConsumerRequestKeyFrameRequest,
Expand All @@ -18,6 +17,9 @@ use crate::worker::{Channel, NotificationParseError, RequestError, SubscriptionH
use async_executor::Executor;
use event_listener_primitives::{Bag, BagOnce, HandlerId};
use log::{debug, error};
use mediasoup_sys::fbs::{
consumer, notification, response, rtp_parameters, rtp_stream, rtx_stream,
};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use std::error::Error;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/router/data_consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod tests;

use crate::data_producer::{DataProducer, DataProducerId, WeakDataProducer};
use crate::data_structures::{AppData, WebRtcMessage};
use crate::fbs::{data_consumer, data_producer, notification, response};
use crate::messages::{
DataConsumerCloseRequest, DataConsumerDumpRequest, DataConsumerGetBufferedAmountRequest,
DataConsumerGetStatsRequest, DataConsumerPauseRequest, DataConsumerResumeRequest,
Expand All @@ -17,6 +16,7 @@ use crate::worker::{Channel, NotificationParseError, RequestError, SubscriptionH
use async_executor::Executor;
use event_listener_primitives::{Bag, BagOnce, HandlerId};
use log::{debug, error};
use mediasoup_sys::fbs::{data_consumer, data_producer, notification, response};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/router/data_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
mod tests;

use crate::data_structures::{AppData, WebRtcMessage};
use crate::fbs::{data_producer, response};
use crate::messages::{
DataProducerCloseRequest, DataProducerDumpRequest, DataProducerGetStatsRequest,
DataProducerPauseRequest, DataProducerResumeRequest, DataProducerSendNotification,
Expand All @@ -14,6 +13,7 @@ use crate::worker::{Channel, NotificationError, RequestError};
use async_executor::Executor;
use event_listener_primitives::{Bag, BagOnce, HandlerId};
use log::{debug, error};
use mediasoup_sys::fbs::{data_producer, response};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use std::error::Error;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/router/direct_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::consumer::{Consumer, ConsumerId, ConsumerOptions};
use crate::data_consumer::{DataConsumer, DataConsumerId, DataConsumerOptions, DataConsumerType};
use crate::data_producer::{DataProducer, DataProducerId, DataProducerOptions, DataProducerType};
use crate::data_structures::{AppData, SctpState};
use crate::fbs::{direct_transport, notification, response, transport};
use crate::messages::{TransportCloseRequest, TransportSendRtcpNotification};
use crate::producer::{Producer, ProducerId, ProducerOptions};
use crate::router::transport::{TransportImpl, TransportType};
Expand All @@ -23,6 +22,7 @@ use async_executor::Executor;
use async_trait::async_trait;
use event_listener_primitives::{Bag, BagOnce, HandlerId};
use log::{debug, error};
use mediasoup_sys::fbs::{direct_transport, notification, response, transport};
use nohash_hasher::IntMap;
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion rust/src/router/pipe_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::consumer::{Consumer, ConsumerId, ConsumerOptions};
use crate::data_consumer::{DataConsumer, DataConsumerId, DataConsumerOptions, DataConsumerType};
use crate::data_producer::{DataProducer, DataProducerId, DataProducerOptions, DataProducerType};
use crate::data_structures::{AppData, ListenInfo, SctpState, TransportTuple};
use crate::fbs::{notification, pipe_transport, response, transport};
use crate::messages::{PipeTransportConnectRequest, PipeTransportData, TransportCloseRequest};
use crate::producer::{Producer, ProducerId, ProducerOptions};
use crate::router::transport::{TransportImpl, TransportType};
Expand All @@ -22,6 +21,7 @@ use async_executor::Executor;
use async_trait::async_trait;
use event_listener_primitives::{Bag, BagOnce, HandlerId};
use log::{debug, error};
use mediasoup_sys::fbs::{notification, pipe_transport, response, transport};
use nohash_hasher::IntMap;
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion rust/src/router/plain_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::consumer::{Consumer, ConsumerId, ConsumerOptions};
use crate::data_consumer::{DataConsumer, DataConsumerId, DataConsumerOptions, DataConsumerType};
use crate::data_producer::{DataProducer, DataProducerId, DataProducerOptions, DataProducerType};
use crate::data_structures::{AppData, ListenInfo, SctpState, TransportTuple};
use crate::fbs::{notification, plain_transport, response, transport};
use crate::messages::{PlainTransportData, TransportCloseRequest, TransportConnectPlainRequest};
use crate::producer::{Producer, ProducerId, ProducerOptions};
use crate::router::transport::{TransportImpl, TransportType};
Expand All @@ -22,6 +21,7 @@ use async_executor::Executor;
use async_trait::async_trait;
use event_listener_primitives::{Bag, BagOnce, HandlerId};
use log::{debug, error};
use mediasoup_sys::fbs::{notification, plain_transport, response, transport};
use nohash_hasher::IntMap;
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion rust/src/router/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod tests;

use crate::consumer::{RtpStreamParams, RtxStreamParams};
use crate::data_structures::{AppData, RtpPacketTraceInfo, SsrcTraceInfo, TraceEventDirection};
use crate::fbs::{notification, producer, response, rtp_parameters, rtp_stream};
use crate::messages::{
ProducerCloseRequest, ProducerDumpRequest, ProducerEnableTraceEventRequest,
ProducerGetStatsRequest, ProducerPauseRequest, ProducerResumeRequest, ProducerSendNotification,
Expand All @@ -18,6 +17,7 @@ use crate::worker::{
use async_executor::Executor;
use event_listener_primitives::{Bag, BagOnce, HandlerId};
use log::{debug, error};
use mediasoup_sys::fbs::{notification, producer, response, rtp_parameters, rtp_stream};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
Expand Down
2 changes: 1 addition & 1 deletion rust/src/router/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::consumer::{Consumer, ConsumerId, ConsumerOptions, ConsumerType};
use crate::data_consumer::{DataConsumer, DataConsumerId, DataConsumerOptions, DataConsumerType};
use crate::data_producer::{DataProducer, DataProducerId, DataProducerOptions, DataProducerType};
use crate::data_structures::{AppData, BweTraceInfo, RtpPacketTraceInfo, TraceEventDirection};
use crate::fbs::{response, transport};
use crate::messages::{
TransportConsumeDataRequest, TransportConsumeRequest, TransportDumpRequest,
TransportEnableTraceEventRequest, TransportGetStatsRequest, TransportProduceDataRequest,
Expand All @@ -22,6 +21,7 @@ use async_executor::Executor;
use async_trait::async_trait;
use event_listener_primitives::HandlerId;
use log::{error, warn};
use mediasoup_sys::fbs::{response, transport};
use nohash_hasher::IntMap;
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion rust/src/router/webrtc_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::data_structures::{
AppData, DtlsParameters, DtlsState, IceCandidate, IceParameters, IceRole, IceState, ListenInfo,
SctpState, TransportTuple,
};
use crate::fbs::{notification, response, transport, web_rtc_transport};
use crate::messages::{
TransportCloseRequest, TransportRestartIceRequest, WebRtcTransportConnectRequest,
WebRtcTransportData,
Expand All @@ -28,6 +27,7 @@ use async_executor::Executor;
use async_trait::async_trait;
use event_listener_primitives::{Bag, BagOnce, HandlerId};
use log::{debug, error};
use mediasoup_sys::fbs::{notification, response, transport, web_rtc_transport};
use nohash_hasher::IntMap;
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion rust/src/rtp_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#[cfg(test)]
mod tests;

use crate::fbs::rtp_parameters;
use crate::scalability_modes::ScalabilityMode;
use mediasoup_sys::fbs::rtp_parameters;
use serde::de::{MapAccess, Visitor};
use serde::ser::SerializeStruct;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
Expand Down
2 changes: 1 addition & 1 deletion rust/src/sctp_parameters.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Collection of SCTP-related data structures that are used to specify SCTP association parameters.

use crate::fbs::sctp_parameters;
use mediasoup_sys::fbs::sctp_parameters;
use serde::{Deserialize, Serialize};

/// Number of SCTP streams.
Expand Down
2 changes: 1 addition & 1 deletion rust/src/srtp_parameters.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Collection of SRTP-related data structures that are used to specify SRTP encryption/decryption
//! parameters.

use crate::fbs::srtp_parameters;
use mediasoup_sys::fbs::srtp_parameters;
use serde::{Deserialize, Serialize};

/// SRTP parameters.
Expand Down
2 changes: 1 addition & 1 deletion rust/src/webrtc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
mod tests;

use crate::data_structures::{AppData, ListenInfo};
use crate::fbs::transport;
use crate::messages::{WebRtcServerCloseRequest, WebRtcServerDumpRequest};
use crate::transport::TransportId;
use crate::uuid_based_wrapper_type;
Expand All @@ -21,6 +20,7 @@ use async_executor::Executor;
use event_listener_primitives::{BagOnce, HandlerId};
use hash_hasher::HashedSet;
use log::{debug, error};
use mediasoup_sys::fbs::transport;
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use std::fmt;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ mod common;
mod utils;

use crate::data_structures::AppData;
use crate::fbs;
use crate::messages::{
WorkerCloseRequest, WorkerCreateRouterRequest, WorkerCreateWebRtcServerRequest,
WorkerDumpRequest, WorkerUpdateSettingsRequest,
Expand All @@ -24,6 +23,7 @@ pub(crate) use common::{SubscriptionHandler, SubscriptionTarget};
use event_listener_primitives::{Bag, BagOnce, HandlerId};
use futures_lite::FutureExt;
use log::{debug, error, warn};
use mediasoup_sys::fbs;
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use std::error::Error;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/worker/channel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::fbs::{message, notification, request, response};
use crate::messages::{Notification, Request};
use crate::worker::common::{EventHandlers, SubscriptionTarget, WeakEventHandlers};
use crate::worker::utils;
Expand All @@ -8,6 +7,7 @@ use atomic_take::AtomicTake;
use hash_hasher::HashedMap;
use log::{debug, error, trace, warn};
use lru::LruCache;
use mediasoup_sys::fbs::{message, notification, request, response};
use mediasoup_sys::UvAsyncT;
use parking_lot::Mutex;
use planus::ReadAsRoot;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/worker/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::fbs::notification;
use hash_hasher::HashedMap;
use mediasoup_sys::fbs::notification;
use nohash_hasher::IntMap;
use parking_lot::Mutex;
use serde::Deserialize;
Expand Down
12 changes: 12 additions & 0 deletions worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ documentation = "https://docs.rs/mediasoup-sys"
repository = "https://github.com/versatica/mediasoup/tree/v3/worker"
include = [
"/deps/libwebrtc",
"/fbs",
"/fuzzer/include",
"/fuzzer/src",
"/include",
Expand All @@ -28,3 +29,14 @@ include = [
[package.metadata.docs.rs]
default-target = "x86_64-unknown-linux-gnu"
targets = []

[build-dependencies]
planus-codegen = "0.4.0"
planus-translation = "0.4.0"

[dependencies]
planus = "0.4.0"

[dependencies.serde]
features = ["derive"]
version = "1.0.190"
31 changes: 30 additions & 1 deletion worker/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::env;
use std::process::Command;
use std::{env, fs};

fn main() {
if env::var("DOCS_RS").is_ok() {
Expand All @@ -16,6 +16,35 @@ fn main() {
};

let out_dir = env::var("OUT_DIR").unwrap();

// Compile Rust flatbuffers
let flatbuffers_declarations = planus_translation::translate_files(
&fs::read_dir("fbs")
.expect("Failed to read `fbs` directory")
.filter_map(|maybe_entry| {
maybe_entry
.map(|entry| {
let path = entry.path();
if path.extension() == Some("fbs".as_ref()) {
Some(path)
} else {
None
}
})
.transpose()
})
jmillan marked this conversation as resolved.
Show resolved Hide resolved
.collect::<Result<Vec<_>, _>>()
.expect("Failed to collect flatbuffers files"),
)
.expect("Failed to translate flatbuffers files");

fs::write(
format!("{out_dir}/fbs.rs"),
planus_codegen::generate_rust(&flatbuffers_declarations)
.expect("Failed to generate Rust code from flatbuffers"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's all? No need to tricks in generated fbs.ts file to make clippy happy and so on?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also a bit surprised given the fact that we do literal include!(), but apparently Rust tracks where source code comes from and from path it is clear that file is auto-generated and it ignores all warnings from there 🤷‍♂️

I'm not sure why that happens and lazy to search, but it works out for well for us here 🙂

)
.expect("Failed to write generated Rust flatbuffers into fbs.rs");

// Force forward slashes on Windows too so that is plays well with our dumb `Makefile`.
let mediasoup_out_dir = format!("{}/out", out_dir.replace('\\', "/"));

Expand Down
2 changes: 2 additions & 0 deletions worker/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::os::raw::{c_char, c_int, c_void};

include!(concat!(env!("OUT_DIR"), "/fbs.rs"));

#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct UvAsyncT(pub *const c_void);
Expand Down
Loading