From 4e167838243997064e62fac038f42974275173c3 Mon Sep 17 00:00:00 2001 From: lc Date: Thu, 20 Nov 2025 14:58:10 +0800 Subject: [PATCH] remove dummy webrtc stream, add api to get webrtc stream --- examples/webrtc.rs | 12 +++-------- examples/webrtc_dummy.rs | 45 ---------------------------------------- src/stream.rs | 9 ++++++++ 3 files changed, 12 insertions(+), 54 deletions(-) delete mode 100644 examples/webrtc_dummy.rs diff --git a/examples/webrtc.rs b/examples/webrtc.rs index 1771663..2c993ca 100644 --- a/examples/webrtc.rs +++ b/examples/webrtc.rs @@ -2,21 +2,13 @@ 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 anyhow::Result; use bytes::Bytes; use clap::{Arg, Command}; use tokio::time::Duration; -#[cfg(feature = "webrtc")] -use webrtc::peer_connection::math_rand_alpha; - #[cfg(not(feature = "webrtc"))] #[tokio::main] async fn main() -> Result<()> { @@ -121,6 +113,7 @@ async fn main() -> Result<()> { } // read_loop shows how to read from the datachannel directly +#[cfg(feature = "webrtc")] async fn read_loop(mut stream: WebRTCStream) -> Result<()> { loop { let Some(res) = stream.next().await else { @@ -140,6 +133,7 @@ async fn read_loop(mut stream: WebRTCStream) -> Result<()> { } // write_loop shows how to write to the webrtc stream directly +#[cfg(feature = "webrtc")] async fn write_loop(mut stream: WebRTCStream) -> Result<()> { let mut result = Result::<()>::Ok(()); while result.is_ok() { @@ -148,7 +142,7 @@ async fn write_loop(mut stream: WebRTCStream) -> Result<()> { tokio::select! { _ = timeout.as_mut() =>{ - let message = math_rand_alpha(15); + let message = webrtc::peer_connection::math_rand_alpha(15); result = stream.send_bytes(Bytes::from(message.clone())).await; println!("Sent '{message}' {}", result.is_ok()); } diff --git a/examples/webrtc_dummy.rs b/examples/webrtc_dummy.rs deleted file mode 100644 index a4c5b1e..0000000 --- a/examples/webrtc_dummy.rs +++ /dev/null @@ -1,45 +0,0 @@ -use std::io::Error; - -use bytes::BytesMut; - -use hbb_common::ResultType; - -/// Dummy implementation of WebRTCStream used when the `webrtc` feature is disabled. -/// This struct allows the code to compile and run without actual WebRTC functionality. -pub struct WebRTCStream { - // mock struct -} - -impl Clone for WebRTCStream { - fn clone(&self) -> Self { - WebRTCStream {} - } -} - -impl WebRTCStream { - pub async fn new(_: &str, _: bool, _: 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/stream.rs b/src/stream.rs index 6a5ac85..a8e6b6c 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -137,4 +137,13 @@ impl Stream { pub fn from(stream: TcpStream, stream_addr: SocketAddr) -> Self { Self::Tcp(tcp::FramedStream::from(stream, stream_addr)) } + + #[inline] + #[cfg(feature = "webrtc")] + pub fn get_webrtc_stream(&self) -> Option { + match self { + Self::WebRTC(s) => Some(s.clone()), + _ => None, + } + } }