Skip to main content

moq_rtc/client/
mod.rs

1//! HTTP-client side: dial a remote WHIP/WHEP endpoint over an SDP exchange.
2//!
3//! Counterpart to [`crate::server`]. Whereas the server accepts POSTed
4//! offers, the client mints the offer with `str0m::Rtc::sdp_api` and POSTs
5//! it to the remote URL. Once the answer arrives the same
6//! [`crate::session::Session`] driver takes over, so the per-codec bridges
7//! and UDP socket loop are shared.
8
9pub mod whep;
10pub mod whip;
11
12use std::net::SocketAddr;
13
14use url::Url;
15
16/// Configuration shared by both `client publish` and `client subscribe`.
17#[derive(Clone, Debug, Default)]
18#[non_exhaustive]
19pub struct Config {
20	/// Public UDP socket addresses to advertise as ICE host candidates in
21	/// our outbound offer. Same semantics as [`crate::server::Config::ice_candidates`].
22	pub ice_candidates: Vec<SocketAddr>,
23}
24
25/// Outbound WHIP/WHEP dialer.
26///
27/// Owns a [`reqwest::Client`] reused across calls so connection pooling and
28/// rustls config survive between resources.
29#[derive(Clone)]
30pub struct Client {
31	config: Config,
32	http: reqwest::Client,
33}
34
35impl Client {
36	pub fn new(config: Config) -> Self {
37		Self {
38			config,
39			http: reqwest::Client::new(),
40		}
41	}
42
43	pub(crate) fn config(&self) -> &Config {
44		&self.config
45	}
46
47	pub(crate) fn http(&self) -> &reqwest::Client {
48		&self.http
49	}
50
51	/// `client subscribe`: pull a remote WHEP feed and publish it as
52	/// `broadcast` on the local origin. Returns once the session is
53	/// running in the background.
54	pub async fn subscribe(&self, url: Url, broadcast: moq_net::BroadcastProducer) -> crate::Result<()> {
55		whep::dial(self, url, broadcast).await
56	}
57
58	/// `client publish`: pull a local broadcast and push it to a remote
59	/// WHIP endpoint. Gated on the per-codec re-packetizer.
60	pub async fn publish(&self, url: Url, broadcast: moq_net::BroadcastConsumer) -> crate::Result<()> {
61		whip::dial(self, url, broadcast).await
62	}
63}