remove dummy webrtc stream,

add api to get webrtc stream
This commit is contained in:
lc
2025-11-20 14:58:10 +08:00
parent e4224a19bc
commit 4e16783824
3 changed files with 12 additions and 54 deletions

View File

@@ -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());
}

View File

@@ -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<Self> {
Ok(Self {})
}
#[inline]
pub async fn get_local_endpoint(&self) -> ResultType<String> {
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<Result<BytesMut, Error>> {
None
}
}

View File

@@ -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<webrtc::WebRTCStream> {
match self {
Self::WebRTC(s) => Some(s.clone()),
_ => None,
}
}
}