From f8d1d4207d6266509026371b4d98f67dca43a02f Mon Sep 17 00:00:00 2001 From: lc Date: Fri, 14 Nov 2025 01:09:08 +0800 Subject: [PATCH] move out dummy webrtc mod --- examples/webrtc.rs | 22 +++++++--- examples/webrtc_dummy.rs | 46 ++++++++++++++++++++ src/lib.rs | 4 -- src/stream.rs | 15 ++++++- src/webrtc_dummy.rs | 91 ---------------------------------------- 5 files changed, 77 insertions(+), 101 deletions(-) create mode 100644 examples/webrtc_dummy.rs delete mode 100644 src/webrtc_dummy.rs diff --git a/examples/webrtc.rs b/examples/webrtc.rs index b7f3f06..40d4cdf 100644 --- a/examples/webrtc.rs +++ b/examples/webrtc.rs @@ -1,5 +1,12 @@ extern crate hbb_common; +#[cfg(feature = "webrtc")] +use hbb_common::webrtc::WebRTCStream; +#[cfg(not(feature = "webrtc"))] +mod webrtc_dummy; +#[cfg(not(feature = "webrtc"))] +use crate::webrtc_dummy::WebRTCStream; + use std::io::Write; use bytes::Bytes; @@ -11,6 +18,11 @@ use webrtc::peer_connection::math_rand_alpha; #[tokio::main] async fn main() -> Result<()> { + #[cfg(not(feature = "webrtc"))] + if true { + println!("The webrtc feature is not enabled. Please enable the webrtc feature to run this example."); + return Ok(()); + } let app = Command::new("webrtc-stream") .about("An example of webrtc stream using hbb_common and webrtc-rs") .arg( @@ -54,7 +66,7 @@ async fn main() -> Result<()> { "".to_string() }; - let webrtc_stream = hbb_common::webrtc::WebRTCStream::new(&remote_endpoint, 30000).await?; + let webrtc_stream = WebRTCStream::new(&remote_endpoint, 30000).await?; // Print the offer to be sent to the other peer let local_endpoint = webrtc_stream.get_local_endpoint().await?; @@ -75,12 +87,12 @@ async fn main() -> Result<()> { println!("Copy local endpoint and paste to the other peer: \n{}", local_endpoint); } - let s1 = hbb_common::Stream::WebRTC(webrtc_stream.clone()); + let s1 = webrtc_stream.clone(); tokio::spawn(async move { let _ = read_loop(s1).await; }); - let s2 = hbb_common::Stream::WebRTC(webrtc_stream.clone()); + let s2 = webrtc_stream.clone(); tokio::spawn(async move { let _ = write_loop(s2).await; }); @@ -96,7 +108,7 @@ async fn main() -> Result<()> { } // read_loop shows how to read from the datachannel directly -async fn read_loop(mut stream: hbb_common::Stream) -> Result<()> { +async fn read_loop(mut stream: WebRTCStream) -> Result<()> { loop { let Some(res) = stream.next().await else { println!("WebRTC stream closed; Exit the read_loop"); @@ -115,7 +127,7 @@ async fn read_loop(mut stream: hbb_common::Stream) -> Result<()> { } // write_loop shows how to write to the webrtc stream directly -async fn write_loop(mut stream: hbb_common::Stream) -> Result<()> { +async fn write_loop(mut stream: WebRTCStream) -> Result<()> { let mut result = Result::<()>::Ok(()); while result.is_ok() { let timeout = tokio::time::sleep(Duration::from_secs(5)); diff --git a/examples/webrtc_dummy.rs b/examples/webrtc_dummy.rs new file mode 100644 index 0000000..cd8d1b0 --- /dev/null +++ b/examples/webrtc_dummy.rs @@ -0,0 +1,46 @@ +use std::io::Error; + +use bytes::BytesMut; + +use hbb_common::ResultType; + +pub struct WebRTCStream { + // mock struct +} + +impl Clone for WebRTCStream { + fn clone(&self) -> Self { + WebRTCStream { + } + } +} + +impl WebRTCStream { + + pub async fn new( + _: &str, + _: u64, + ) -> ResultType { + Ok(Self {}) + } + + #[inline] + pub async fn get_local_endpoint(&self) -> ResultType { + Ok(String::new()) + } + + #[inline] + pub async fn set_remote_endpoint(&self, _: &str) -> ResultType<()> { + Ok(()) + } + + #[inline] + pub async fn send_bytes(&mut self, _: bytes::Bytes) -> ResultType<()> { + Ok(()) + } + + #[inline] + pub async fn next(&mut self) -> Option> { + None + } +} diff --git a/src/lib.rs b/src/lib.rs index 5d3e600..372f4ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,10 +61,6 @@ pub mod stream; pub mod websocket; #[cfg(feature = "webrtc")] pub mod webrtc; -#[cfg(not(feature = "webrtc"))] -pub mod webrtc_dummy; -#[cfg(not(feature = "webrtc"))] -pub use webrtc_dummy as webrtc; #[cfg(any(target_os = "android", target_os = "ios"))] pub use rustls_platform_verifier; pub use stream::Stream; diff --git a/src/stream.rs b/src/stream.rs index 16db2c5..6a5ac85 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -1,10 +1,13 @@ -use crate::{config, tcp, websocket, webrtc, ResultType}; +use crate::{config, tcp, websocket, ResultType}; +#[cfg(feature = "webrtc")] +use crate::webrtc; use sodiumoxide::crypto::secretbox::Key; use std::net::SocketAddr; use tokio::net::TcpStream; // support Websocket and tcp. pub enum Stream { + #[cfg(feature = "webrtc")] WebRTC(webrtc::WebRTCStream), WebSocket(websocket::WsFramedStream), Tcp(tcp::FramedStream), @@ -14,6 +17,7 @@ impl Stream { #[inline] pub fn set_send_timeout(&mut self, ms: u64) { match self { + #[cfg(feature = "webrtc")] Stream::WebRTC(s) => s.set_send_timeout(ms), Stream::WebSocket(s) => s.set_send_timeout(ms), Stream::Tcp(s) => s.set_send_timeout(ms), @@ -23,6 +27,7 @@ impl Stream { #[inline] pub fn set_raw(&mut self) { match self { + #[cfg(feature = "webrtc")] Stream::WebRTC(s) => s.set_raw(), Stream::WebSocket(s) => s.set_raw(), Stream::Tcp(s) => s.set_raw(), @@ -32,6 +37,7 @@ impl Stream { #[inline] pub async fn send_bytes(&mut self, bytes: bytes::Bytes) -> ResultType<()> { match self { + #[cfg(feature = "webrtc")] Stream::WebRTC(s) => s.send_bytes(bytes).await, Stream::WebSocket(s) => s.send_bytes(bytes).await, Stream::Tcp(s) => s.send_bytes(bytes).await, @@ -41,6 +47,7 @@ impl Stream { #[inline] pub async fn send_raw(&mut self, bytes: Vec) -> ResultType<()> { match self { + #[cfg(feature = "webrtc")] Stream::WebRTC(s) => s.send_raw(bytes).await, Stream::WebSocket(s) => s.send_raw(bytes).await, Stream::Tcp(s) => s.send_raw(bytes).await, @@ -50,6 +57,7 @@ impl Stream { #[inline] pub fn set_key(&mut self, key: Key) { match self { + #[cfg(feature = "webrtc")] Stream::WebRTC(s) => s.set_key(key), Stream::WebSocket(s) => s.set_key(key), Stream::Tcp(s) => s.set_key(key), @@ -59,6 +67,7 @@ impl Stream { #[inline] pub fn is_secured(&self) -> bool { match self { + #[cfg(feature = "webrtc")] Stream::WebRTC(s) => s.is_secured(), Stream::WebSocket(s) => s.is_secured(), Stream::Tcp(s) => s.is_secured(), @@ -71,6 +80,7 @@ impl Stream { timeout: u64, ) -> Option> { match self { + #[cfg(feature = "webrtc")] Stream::WebRTC(s) => s.next_timeout(timeout).await, Stream::WebSocket(s) => s.next_timeout(timeout).await, Stream::Tcp(s) => s.next_timeout(timeout).await, @@ -95,6 +105,7 @@ impl Stream { #[inline] pub async fn send(&mut self, msg: &impl protobuf::Message) -> ResultType<()> { match self { + #[cfg(feature = "webrtc")] Self::WebRTC(s) => s.send(msg).await, Self::WebSocket(ws) => ws.send(msg).await, Self::Tcp(tcp) => tcp.send(msg).await, @@ -105,6 +116,7 @@ impl Stream { #[inline] pub async fn next(&mut self) -> Option> { match self { + #[cfg(feature = "webrtc")] Self::WebRTC(s) => s.next().await, Self::WebSocket(ws) => ws.next().await, Self::Tcp(tcp) => tcp.next().await, @@ -114,6 +126,7 @@ impl Stream { #[inline] pub fn local_addr(&self) -> SocketAddr { match self { + #[cfg(feature = "webrtc")] Self::WebRTC(s) => s.local_addr(), Self::WebSocket(ws) => ws.local_addr(), Self::Tcp(tcp) => tcp.local_addr(), diff --git a/src/webrtc_dummy.rs b/src/webrtc_dummy.rs deleted file mode 100644 index 7e2e142..0000000 --- a/src/webrtc_dummy.rs +++ /dev/null @@ -1,91 +0,0 @@ -use std::net::{IpAddr, Ipv4Addr, SocketAddr}; -use std::io::Error; - -use bytes::{Bytes, BytesMut}; - -use crate::{ - protobuf::Message, - sodiumoxide::crypto::secretbox::Key, - ResultType, -}; - -pub struct WebRTCStream { - // mock struct -} - -impl Clone for WebRTCStream { - fn clone(&self) -> Self { - WebRTCStream { - } - } -} - -impl WebRTCStream { - - pub async fn new( - _: &str, - _: u64, - ) -> ResultType { - Ok(Self {}) - } - - #[inline] - pub async fn get_local_endpoint(&self) -> ResultType { - Ok(String::new()) - } - - #[inline] - pub async fn set_remote_endpoint(&self, _: &str) -> ResultType<()> { - Ok(()) - } - - #[inline] - pub fn set_raw(&mut self) { - } - - #[inline] - pub fn local_addr(&self) -> SocketAddr { - SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0) - } - - #[inline] - pub fn set_send_timeout(&mut self, _ms: u64) { - } - - #[inline] - pub fn set_key(&mut self, _key: Key) { - } - - #[inline] - pub fn is_secured(&self) -> bool { - false - } - - #[inline] - pub async fn send(&mut self, _msg: &impl Message) -> ResultType<()> { - Ok(()) - } - - #[inline] - pub async fn send_raw(&mut self, _msg: Vec) -> ResultType<()> { - Ok(()) - } - - pub async fn send_bytes(&mut self, _bytes: Bytes) -> ResultType<()> { - Ok(()) - } - - #[inline] - pub async fn next(&mut self) -> Option> { - None - } - - #[inline] - pub async fn next_timeout(&mut self, _ms: u64) -> Option> { - None - } -} - -pub fn is_webrtc_endpoint(_endpoint: &str) -> bool { - false -}