86 lines
4.2 KiB
Rust
86 lines
4.2 KiB
Rust
//! The DESCRIBE answer (`docs/PROTOCOL.md` 2.3, 2.5): what the host offers, written as the SDP lines the client
|
|
//! reads with [`crate::rtsp::HostCapabilities::from_sdp`].
|
|
|
|
use crate::rtsp::sdp::{EncryptionFlags, FeatureFlags, MAX_OPUS_CONFIGS, OpusConfig, parse_surround_params};
|
|
use crate::util::{cap, malformed};
|
|
use crate::{Error, Result};
|
|
|
|
/// The `sprop-parameter-sets` line a known host sends; its value is not used by clients (`docs/PROTOCOL.md` 2.3).
|
|
const SPROP_LINE: &str = "sprop-parameter-sets=AAAAAU";
|
|
|
|
/// What the host offers: written into the DESCRIBE answer, and checked against the client's ANNOUNCE.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct HostOffer {
|
|
/// `x-ss-general.featureFlags` (pen/touch, controller touch).
|
|
pub feature_flags: FeatureFlags,
|
|
/// `x-ss-general.encryptionSupported`.
|
|
pub encryption_supported: EncryptionFlags,
|
|
/// `x-nv-video[0].refPicInvalidation:1`: the host can invalidate reference frames.
|
|
pub reference_frame_invalidation: bool,
|
|
/// H.264: `a=fmtp:96 packetization-mode=1`.
|
|
pub h264: bool,
|
|
/// HEVC. No DESCRIBE line is known for it (a host advertises it in serverinfo, `docs/PROTOCOL.md` 1.2, U25);
|
|
/// used only to check ANNOUNCE.
|
|
pub hevc: bool,
|
|
/// AV1: `a=rtpmap:98 AV1/90000`.
|
|
pub av1: bool,
|
|
/// HDR. No DESCRIBE line is known for it (serverinfo again); used only to check ANNOUNCE: without it, a
|
|
/// `dynamicRangeMode` other than 0 (SDR \[M\]) is refused.
|
|
pub hdr: bool,
|
|
/// 4:4:4 chroma. No DESCRIBE line is known; used only to check ANNOUNCE.
|
|
pub yuv444: bool,
|
|
/// One `a=fmtp:97 surround-params=…` line each, in order (at most
|
|
/// [`crate::rtsp::sdp::MAX_OPUS_CONFIGS`]). The mappings are written as given: whether a host must rotate the
|
|
/// 5.1/7.1 normal-quality mappings is UNKNOWN U26.
|
|
pub opus_configs: Vec<OpusConfig>,
|
|
}
|
|
|
|
/// Builds the DESCRIBE payload for `offer`, in the line order a known host uses (`docs/PROTOCOL.md` 2.5 \[M\]):
|
|
/// feature flags, encryption, `sprop-parameter-sets`, reference invalidation, AV1, H.264, then one
|
|
/// `surround-params` line per Opus configuration. CRLF line ends.
|
|
///
|
|
/// # Errors
|
|
/// [`Error::Malformed`] for an Opus configuration that the client's `surround-params` parser would refuse (more
|
|
/// than 8 channels, a mapping entry or count above 9, a wrong mapping length, no streams, more coupled streams than
|
|
/// streams); [`Error::TooLarge`] over [`crate::rtsp::sdp::MAX_OPUS_CONFIGS`]; [`Error::Unsupported`] when no codec
|
|
/// is offered.
|
|
pub fn build_describe(offer: &HostOffer) -> Result<Vec<u8>> {
|
|
if !(offer.h264 || offer.hevc || offer.av1) {
|
|
return Err(Error::Unsupported { what: "a video codec in the host offer" });
|
|
}
|
|
cap("describe opus configurations", MAX_OPUS_CONFIGS, offer.opus_configs.len())?;
|
|
let mut out = String::with_capacity(256 + 48 * offer.opus_configs.len());
|
|
let mut line = |text: &str| {
|
|
out.push_str(text);
|
|
out.push_str("\r\n");
|
|
};
|
|
line(&format!("a=x-ss-general.featureFlags:{}", offer.feature_flags.0));
|
|
line(&format!("a=x-ss-general.encryptionSupported:{}", offer.encryption_supported.0));
|
|
line(SPROP_LINE);
|
|
if offer.reference_frame_invalidation {
|
|
line("a=x-nv-video[0].refPicInvalidation:1");
|
|
}
|
|
if offer.av1 {
|
|
line("a=rtpmap:98 AV1/90000");
|
|
}
|
|
if offer.h264 {
|
|
line("a=fmtp:96 packetization-mode=1");
|
|
}
|
|
for config in &offer.opus_configs {
|
|
line(&format!("a=fmtp:97 surround-params={}", surround_digits(config)?));
|
|
}
|
|
Ok(out.into_bytes())
|
|
}
|
|
|
|
/// `surround-params` digits for `config` (`docs/PROTOCOL.md` 2.3): channel count, stream count, coupled count, then
|
|
/// one mapping digit per channel. The client's own parser checks the result, so nothing it refuses is written.
|
|
fn surround_digits(config: &OpusConfig) -> Result<String> {
|
|
let mut digits = String::with_capacity(3 + config.mapping.len());
|
|
for n in [config.channels, config.streams, config.coupled_streams].iter().chain(&config.mapping) {
|
|
let d = char::from_digit(u32::from(*n), 10).ok_or(malformed("surround-params: a value above 9"))?;
|
|
digits.push(d);
|
|
}
|
|
parse_surround_params(&digits)?;
|
|
Ok(digits)
|
|
}
|