#[cfg(any(
all(feature = "vpl", target_os = "linux"),
feature = "amf",
feature = "nvcodec"
))]
use std::collections::HashMap;
use shiguredo_webrtc::{VideoFrameType, VideoFrameTypeVectorRef};
#[cfg(any(
all(feature = "vpl", target_os = "linux"),
feature = "amf",
feature = "nvcodec"
))]
use shiguredo_webrtc::{ScalabilityMode, SdpVideoFormat, VideoCodecType};
pub(crate) fn requested_frame_type(
frame_types: Option<VideoFrameTypeVectorRef<'_>>,
) -> Option<VideoFrameType> {
frame_types.and_then(|frame_types| frame_types.get(0))
}
#[cfg(any(
all(feature = "vpl", target_os = "linux"),
feature = "amf",
feature = "nvcodec"
))]
pub(crate) fn supported_formats_for_codec(codec_type: VideoCodecType) -> Vec<SdpVideoFormat> {
match codec_type {
VideoCodecType::H264 => vec![SdpVideoFormat::new_with_parameters(
"H264",
&HashMap::from([
(String::from("level-asymmetry-allowed"), String::from("1")),
(String::from("packetization-mode"), String::from("1")),
]),
&[ScalabilityMode::L1T1],
)],
VideoCodecType::H265 => vec![SdpVideoFormat::new("H265")],
VideoCodecType::Vp9 => vec![SdpVideoFormat::new_with_parameters(
"VP9",
&HashMap::from([(String::from("profile-id"), String::from("0"))]),
&[],
)],
VideoCodecType::Av1 => vec![SdpVideoFormat::new("AV1")],
VideoCodecType::Vp8 => vec![SdpVideoFormat::new("VP8")],
_ => Vec::new(),
}
}
#[cfg(any(all(feature = "vpl", target_os = "linux"), feature = "amf"))]
pub(crate) fn target_kbps_from_bps(target_bitrate_bps: u32) -> u32 {
(target_bitrate_bps.max(1) as u64).div_ceil(1000) as u32
}
#[cfg(test)]
mod tests {
use super::*;
use shiguredo_webrtc::VideoFrameTypeVector;
#[test]
fn requested_frame_type_uses_first_entry() {
assert_eq!(requested_frame_type(None), None);
let mut frame_types = VideoFrameTypeVector::new(2);
frame_types.push(VideoFrameType::Empty);
frame_types.push(VideoFrameType::Key);
assert_eq!(
requested_frame_type(Some(frame_types.as_ref())),
Some(VideoFrameType::Empty)
);
}
#[cfg(any(
all(feature = "vpl", target_os = "linux"),
feature = "amf",
feature = "nvcodec"
))]
#[test]
fn supported_formats_for_codec_covers_all_codecs() {
let mut h264 = supported_formats_for_codec(VideoCodecType::H264);
assert_eq!(h264.len(), 1);
let params = h264[0]
.parameters_mut()
.iter()
.collect::<HashMap<String, String>>();
assert_eq!(
params.get("packetization-mode").map(String::as_str),
Some("1")
);
assert_eq!(
params.get("level-asymmetry-allowed").map(String::as_str),
Some("1")
);
assert_eq!(
supported_formats_for_codec(VideoCodecType::H265)[0]
.name()
.expect("format name の取得に失敗"),
"H265"
);
let mut vp9 = supported_formats_for_codec(VideoCodecType::Vp9);
let vp9_params = vp9[0]
.parameters_mut()
.iter()
.collect::<HashMap<String, String>>();
assert_eq!(vp9_params.get("profile-id").map(String::as_str), Some("0"));
assert_eq!(
supported_formats_for_codec(VideoCodecType::Av1)[0]
.name()
.expect("format name の取得に失敗"),
"AV1"
);
assert_eq!(
supported_formats_for_codec(VideoCodecType::Vp8)[0]
.name()
.expect("format name の取得に失敗"),
"VP8"
);
}
#[cfg(any(all(feature = "vpl", target_os = "linux"), feature = "amf"))]
#[test]
fn target_kbps_from_bps_rounds_up_and_clamps_zero() {
assert_eq!(target_kbps_from_bps(1_000), 1);
assert_eq!(target_kbps_from_bps(999), 1);
assert_eq!(target_kbps_from_bps(1_000_001), 1001);
assert_eq!(target_kbps_from_bps(0), 1);
}
}