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

Rust: port transport notifications to FBS #1158

Merged
merged 4 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 33 additions & 0 deletions rust/src/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,15 @@ pub enum TraceEventDirection {
Out,
}

impl TraceEventDirection {
pub(crate) fn from_fbs(event_type: transport::TraceDirection) -> Self {
match event_type {
transport::TraceDirection::DirectionIn => TraceEventDirection::In,
transport::TraceDirection::DirectionOut => TraceEventDirection::Out,
}
}
}

/// Container used for sending/receiving messages using `DirectTransport` data producers and data
/// consumers.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -1163,6 +1172,15 @@ pub enum BweType {
Remb,
}

impl BweType {
pub(crate) fn from_fbs(info: transport::BweType) -> Self {
match info {
transport::BweType::TransportCc => BweType::TransportCc,
transport::BweType::Remb => BweType::Remb,
}
}
}

/// BWE info in trace event.
#[derive(Debug, Copy, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -1184,3 +1202,18 @@ pub struct BweTraceInfo {
/// Available bitrate.
available_bitrate: u32,
}

impl BweTraceInfo {
pub(crate) fn from_fbs(info: transport::BweTraceInfo) -> Self {
Self {
r#type: BweType::from_fbs(info.bwe_type),
desired_bitrate: info.desired_bitrate,
effective_desired_bitrate: info.effective_desired_bitrate,
min_bitrate: info.min_bitrate,
max_bitrate: info.max_bitrate,
start_bitrate: info.start_bitrate,
max_padding_bitrate: info.max_padding_bitrate,
available_bitrate: info.available_bitrate,
}
}
}
399 changes: 258 additions & 141 deletions rust/src/fbs.rs

Large diffs are not rendered by default.

55 changes: 41 additions & 14 deletions rust/src/router/direct_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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, response};
use crate::fbs::{direct_transport, notification, response, transport};
use crate::messages::{TransportCloseRequestFbs, TransportSendRtcpNotification};
use crate::producer::{Producer, ProducerId, ProducerOptions};
use crate::router::transport::{TransportImpl, TransportType};
Expand All @@ -16,7 +16,9 @@ use crate::transport::{
RtpListener, SctpListener, Transport, TransportGeneric, TransportId, TransportTraceEventData,
TransportTraceEventType,
};
use crate::worker::{Channel, NotificationError, RequestError, SubscriptionHandler};
use crate::worker::{
Channel, NotificationError, NotificationParseError, RequestError, SubscriptionHandler,
};
use async_executor::Executor;
use async_trait::async_trait;
use event_listener_primitives::{Bag, BagOnce, HandlerId};
Expand Down Expand Up @@ -227,8 +229,37 @@ struct Handlers {
#[serde(tag = "event", rename_all = "lowercase", content = "data")]
enum Notification {
Trace(TransportTraceEventData),
// TODO.
// Rtcp,
Rtcp,
}

impl Notification {
pub(crate) fn from_fbs(
notification: notification::NotificationRef<'_>,
) -> Result<Self, NotificationParseError> {
match notification.event().unwrap() {
notification::Event::TransportTrace => {
let Ok(Some(notification::BodyRef::TransportTraceNotification(body))) =
notification.body()
else {
panic!("Wrong message from worker: {notification:?}");
};

let trace_notification_fbs = transport::TraceNotification::try_from(body).unwrap();
let trace_notification = TransportTraceEventData::from_fbs(trace_notification_fbs);

Ok(Notification::Trace(trace_notification))
}
notification::Event::DirecttransportRtcp => {
let Ok(Some(notification::BodyRef::RtcpNotification(_body))) = notification.body()
else {
panic!("Wrong message from worker: {notification:?}");
};

Ok(Notification::Rtcp)
}
_ => Err(NotificationParseError::InvalidEvent),
}
}
}

struct Inner {
Expand Down Expand Up @@ -533,19 +564,15 @@ impl DirectTransport {
let subscription_handler = {
let handlers = Arc::clone(&handlers);

channel.subscribe_to_notifications(id.into(), move |notification| {
match serde_json::from_slice::<Notification>(notification) {
channel.subscribe_to_fbs_notifications(id.into(), move |notification| {
match Notification::from_fbs(notification) {
jmillan marked this conversation as resolved.
Show resolved Hide resolved
Ok(notification) => match notification {
Notification::Trace(trace_event_data) => {
handlers.trace.call_simple(&trace_event_data);
} /*
* TODO.
Notification::Rtcp => {
handlers.rtcp.call(|callback| {
callback(notification);
});
}
*/
}
Notification::Rtcp => {
// TODO: Implement RTCP notification handler.
}
},
Err(error) => {
error!("Failed to parse notification: {}", error);
Expand Down
62 changes: 33 additions & 29 deletions rust/src/router/pipe_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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::{pipe_transport, response};
use crate::fbs::{notification, pipe_transport, response, transport};
use crate::messages::{PipeTransportConnectRequest, PipeTransportData, TransportCloseRequestFbs};
use crate::producer::{Producer, ProducerId, ProducerOptions};
use crate::router::transport::{TransportImpl, TransportType};
Expand Down Expand Up @@ -281,16 +281,6 @@ struct Handlers {
close: BagOnce<Box<dyn FnOnce() + Send>>,
}

#[derive(Debug, Deserialize)]
#[serde(tag = "event", rename_all = "lowercase", content = "data")]
enum Notification {
#[serde(rename_all = "camelCase")]
SctpStateChange {
sctp_state: SctpState,
},
Trace(TransportTraceEventData),
}

struct Inner {
id: TransportId,
next_mid_for_consumers: AtomicUsize,
Expand Down Expand Up @@ -585,25 +575,39 @@ impl PipeTransport {
let handlers = Arc::clone(&handlers);
let data = Arc::clone(&data);

channel.subscribe_to_notifications(id.into(), move |notification| {
match serde_json::from_slice::<Notification>(notification) {
Ok(notification) => match notification {
Notification::SctpStateChange { sctp_state } => {
data.sctp_state.lock().replace(sctp_state);

handlers.sctp_state_change.call(|callback| {
callback(sctp_state);
});
}
Notification::Trace(trace_event_data) => {
handlers.trace.call_simple(&trace_event_data);
}
},
Err(error) => {
error!("Failed to parse notification: {}", error);
channel.subscribe_to_fbs_notifications(
id.into(),
move |notification| match notification.event().unwrap() {
notification::Event::TransportSctpStateChange => {
let Ok(Some(notification::BodyRef::SctpStateChangeNotification(body))) =
notification.body()
else {
panic!("Wrong message from worker: {notification:?}");
};

let sctp_state = SctpState::from_fbs(&body.sctp_state().unwrap());

data.sctp_state.lock().replace(sctp_state);
handlers.sctp_state_change.call(|callback| {
callback(sctp_state);
});
}
}
})
notification::Event::TransportTrace => {
let Ok(Some(notification::BodyRef::TransportTraceNotification(body))) =
notification.body()
else {
panic!("Wrong message from worker: {notification:?}");
};

let trace_event_data =
transport::TraceNotification::try_from(body).unwrap();
handlers
.trace
.call_simple(&TransportTraceEventData::from_fbs(trace_event_data));
}
_ => unimplemented!(),
jmillan marked this conversation as resolved.
Show resolved Hide resolved
},
)
};

let next_mid_for_consumers = AtomicUsize::default();
Expand Down
103 changes: 60 additions & 43 deletions rust/src/router/plain_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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::{plain_transport, response};
use crate::fbs::{notification, plain_transport, response, transport};
use crate::messages::{PlainTransportData, TransportCloseRequestFbs, TransportConnectPlainRequest};
use crate::producer::{Producer, ProducerId, ProducerOptions};
use crate::router::transport::{TransportImpl, TransportType};
Expand Down Expand Up @@ -323,23 +323,6 @@ struct Handlers {
close: BagOnce<Box<dyn FnOnce() + Send>>,
}

#[derive(Debug, Deserialize)]
#[serde(tag = "event", rename_all = "lowercase", content = "data")]
enum Notification {
Tuple {
tuple: TransportTuple,
},
#[serde(rename_all = "camelCase")]
RtcpTuple {
rtcp_tuple: TransportTuple,
},
#[serde(rename_all = "camelCase")]
SctpStateChange {
sctp_state: SctpState,
},
Trace(TransportTraceEventData),
}

struct Inner {
id: TransportId,
next_mid_for_consumers: AtomicUsize,
Expand Down Expand Up @@ -627,35 +610,69 @@ impl PlainTransport {
let handlers = Arc::clone(&handlers);
let data = Arc::clone(&data);

channel.subscribe_to_notifications(id.into(), move |notification| {
match serde_json::from_slice::<Notification>(notification) {
Ok(notification) => match notification {
Notification::Tuple { tuple } => {
*data.tuple.lock() = tuple;
channel.subscribe_to_fbs_notifications(
id.into(),
move |notification| match notification.event().unwrap() {
notification::Event::PlaintransportTuple => {
let Ok(Some(notification::BodyRef::TupleNotification(body))) =
notification.body()
else {
panic!("Wrong message from worker: {notification:?}");
};

handlers.tuple.call_simple(&tuple);
}
Notification::RtcpTuple { rtcp_tuple } => {
data.rtcp_tuple.lock().replace(rtcp_tuple);
let tuple = transport::Tuple::try_from(body.tuple().unwrap()).unwrap();

handlers.rtcp_tuple.call_simple(&rtcp_tuple);
}
Notification::SctpStateChange { sctp_state } => {
data.sctp_state.lock().replace(sctp_state);
*data.tuple.lock() = TransportTuple::from_fbs(&tuple);

handlers.sctp_state_change.call(|callback| {
callback(sctp_state);
});
}
Notification::Trace(trace_event_data) => {
handlers.trace.call_simple(&trace_event_data);
}
},
Err(error) => {
error!("Failed to parse notification: {}", error);
handlers
.tuple
.call_simple(&TransportTuple::from_fbs(&tuple));
}
}
})
notification::Event::PlaintransportRtcpTuple => {
let Ok(Some(notification::BodyRef::RtcpTupleNotification(body))) =
notification.body()
else {
panic!("Wrong message from worker: {notification:?}");
};

let tuple = transport::Tuple::try_from(body.tuple().unwrap()).unwrap();

*data.rtcp_tuple.lock() = Some(TransportTuple::from_fbs(&tuple));

handlers
.rtcp_tuple
.call_simple(&TransportTuple::from_fbs(&tuple));
}
notification::Event::TransportSctpStateChange => {
let Ok(Some(notification::BodyRef::SctpStateChangeNotification(body))) =
notification.body()
else {
panic!("Wrong message from worker: {notification:?}");
};

let sctp_state = SctpState::from_fbs(&body.sctp_state().unwrap());

data.sctp_state.lock().replace(sctp_state);
handlers.sctp_state_change.call(|callback| {
callback(sctp_state);
});
}
notification::Event::TransportTrace => {
let Ok(Some(notification::BodyRef::TransportTraceNotification(body))) =
notification.body()
else {
panic!("Wrong message from worker: {notification:?}");
};

let trace_event_data =
transport::TraceNotification::try_from(body).unwrap();
handlers
.trace
.call_simple(&TransportTraceEventData::from_fbs(trace_event_data));
}
_ => unimplemented!(),
jmillan marked this conversation as resolved.
Show resolved Hide resolved
},
)
};

let next_mid_for_consumers = AtomicUsize::default();
Expand Down
19 changes: 19 additions & 0 deletions rust/src/router/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ pub enum TransportTraceEventData {
},
}

impl TransportTraceEventData {
pub(crate) fn from_fbs(data: transport::TraceNotification) -> Self {
match data.type_ {
transport::TraceEventType::Probation => unimplemented!(),
transport::TraceEventType::Bwe => TransportTraceEventData::Bwe {
timestamp: data.timestamp,
direction: TraceEventDirection::from_fbs(data.direction),
info: {
let Some(transport::TraceInfo::BweTraceInfo(info)) = data.info else {
panic!("Wrong message from worker: {data:?}");
};

BweTraceInfo::from_fbs(*info)
},
},
}
}
}

/// Valid types for "trace" event.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
Expand Down
Loading