提交 dfd418eb authored 作者: Nazar Mokrynskyi's avatar Nazar Mokrynskyi

Refactor payload channel notifications to use synchronous API, refactor…

Refactor payload channel notifications to use synchronous API, refactor benchmark to use it, remove send benchmark as it is not representative and just uses a lot of memory
上级 f2978ead
......@@ -65,12 +65,9 @@ version = "0.8.2"
actix = "0.12.0"
actix-web = "4.0.0-beta.8"
actix-web-actors = "4.0.0-beta.6"
criterion = "0.3.5"
env_logger = "0.9.0"
[dev-dependencies.criterion]
version = "0.3.5"
features = ["async_futures"]
[[bench]]
name = "direct_data"
harness = false
use criterion::async_executor::FuturesExecutor;
use criterion::{criterion_group, criterion_main, Criterion};
use futures_lite::StreamExt;
use mediasoup::prelude::*;
use std::borrow::Cow;
use std::sync::mpsc;
async fn create_data_producer_consumer_pair(
) -> Result<(DataProducer, DataConsumer), Box<dyn std::error::Error>> {
......@@ -33,26 +32,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {
.take(512)
.collect::<Vec<u8>>();
{
let (data_producer, _data_consumer) = futures_lite::future::block_on(async {
create_data_producer_consumer_pair().await.unwrap()
});
let direct_data_producer =
if let DataProducer::Direct(direct_data_producer) = data_producer.clone() {
direct_data_producer
} else {
unreachable!()
};
group.bench_with_input("send", &data, |b, data| {
b.to_async(FuturesExecutor).iter(|| async {
let _ = direct_data_producer
.send(WebRtcMessage::Binary(Cow::from(data)))
.await;
})
});
}
{
let (data_producer, data_consumer) = futures_lite::future::block_on(async {
create_data_producer_consumer_pair().await.unwrap()
......@@ -66,17 +45,15 @@ pub fn criterion_benchmark(c: &mut Criterion) {
};
group.bench_with_input("recv", &data, |b, data| {
b.to_async(FuturesExecutor).iter(|| async {
let (sender, mut receiver) = async_channel::unbounded::<()>();
b.iter(|| {
let (sender, receiver) = mpsc::sync_channel(1);
let _handler_id = data_consumer.on_message(move |_message| {
let _ = sender.try_send(());
let _ = sender.send(());
});
let _ = direct_data_producer
.send(WebRtcMessage::Binary(Cow::from(data)))
.await;
let _ = direct_data_producer.send(WebRtcMessage::Binary(Cow::from(data)));
let _ = receiver.next().await;
let _ = receiver.recv();
})
});
}
......
......@@ -440,24 +440,21 @@ impl DataProducer {
}
impl DirectDataProducer {
/// Sends direct messages from the Rust process.
pub async fn send(&self, message: WebRtcMessage<'_>) -> Result<(), NotificationError> {
/// Sends direct messages from the Rust to the worker.
pub fn send(&self, message: WebRtcMessage<'_>) -> Result<(), NotificationError> {
let (ppid, payload) = message.into_ppid_and_payload();
self.inner
.payload_channel
.notify(
DataProducerSendNotification {
internal: DataProducerInternal {
router_id: self.inner.transport.router_id(),
transport_id: self.inner.transport.id(),
data_producer_id: self.inner.id,
},
data: DataProducerSendData { ppid },
self.inner.payload_channel.notify(
DataProducerSendNotification {
internal: DataProducerInternal {
router_id: self.inner.transport.router_id(),
transport_id: self.inner.transport.id(),
data_producer_id: self.inner.id,
},
payload.into_owned(),
)
.await
data: DataProducerSendData { ppid },
},
payload.into_owned(),
)
}
}
......
......@@ -534,16 +534,13 @@ impl DirectTransport {
/// Send a RTCP packet from the Rust process.
///
/// * `rtcp_packet` - Bytes containing a valid RTCP packet (can be a compound packet).
pub async fn send_rtcp(&self, rtcp_packet: Vec<u8>) -> Result<(), NotificationError> {
self.inner
.payload_channel
.notify(
TransportSendRtcpNotification {
internal: self.get_internal(),
},
rtcp_packet,
)
.await
pub fn send_rtcp(&self, rtcp_packet: Vec<u8>) -> Result<(), NotificationError> {
self.inner.payload_channel.notify(
TransportSendRtcpNotification {
internal: self.get_internal(),
},
rtcp_packet,
)
}
/// Callback is called when the direct transport receives a RTCP packet from its router.
......
......@@ -765,20 +765,17 @@ impl Producer {
impl DirectProducer {
/// Sends a RTP packet from the Rust process.
pub async fn send(&self, rtp_packet: Vec<u8>) -> Result<(), NotificationError> {
self.inner
.payload_channel
.notify(
ProducerSendNotification {
internal: ProducerInternal {
router_id: self.inner.transport.router_id(),
transport_id: self.inner.transport.id(),
producer_id: self.inner.id,
},
pub fn send(&self, rtp_packet: Vec<u8>) -> Result<(), NotificationError> {
self.inner.payload_channel.notify(
ProducerSendNotification {
internal: ProducerInternal {
router_id: self.inner.transport.router_id(),
transport_id: self.inner.transport.id(),
producer_id: self.inner.id,
},
rtp_packet,
)
.await
},
rtp_packet,
)
}
}
......
......@@ -308,7 +308,7 @@ impl PayloadChannel {
})
}
pub(crate) async fn notify<N>(
pub(crate) fn notify<N>(
&self,
notification: N,
payload: Vec<u8>,
......
......@@ -247,7 +247,6 @@ fn send_succeeds() {
direct_data_producer
.send(message)
.await
.expect("Failed to send message");
if id == num_messages {
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论