File
Blob: firmware/vendor/str0m/tests/drv_input.rs
| 1 | #![cfg(feature = "drv")] |
| 2 | //! Verifies the `drv` feature derives `drv::Input` on str0m's public |
| 3 | //! identity types so downstream `#[drv::memo]` queries can take them by |
| 4 | //! value. `Frequency` exercises the `NonZeroU32` path specifically. |
| 5 | |
| 6 | use std::collections::HashMap; |
| 7 | |
| 8 | use str0m::format::Codec; |
| 9 | use str0m::media::{Frequency, MediaKind, MediaTime, Mid}; |
| 10 | |
| 11 | #[derive(drv::Input)] |
| 12 | struct TrackFacts { |
| 13 | pub mid: Mid, |
| 14 | pub kind: MediaKind, |
| 15 | pub codec: Codec, |
| 16 | pub clock_rate: Frequency, |
| 17 | } |
| 18 | |
| 19 | #[drv::memo(single)] |
| 20 | fn is_opus_audio(input: TrackFacts) -> bool { |
| 21 | matches!(input.kind, MediaKind::Audio) && matches!(input.codec, Codec::Opus) |
| 22 | } |
| 23 | |
| 24 | #[test] |
| 25 | fn identity_types_are_valid_memo_inputs() { |
| 26 | let mid = Mid::from("0"); |
| 27 | let audio = TrackFacts { |
| 28 | mid, |
| 29 | kind: MediaKind::Audio, |
| 30 | codec: Codec::Opus, |
| 31 | clock_rate: Frequency::FORTY_EIGHT_KHZ, |
| 32 | }; |
| 33 | assert!(is_opus_audio(audio)); |
| 34 | |
| 35 | let video = TrackFacts { |
| 36 | mid, |
| 37 | kind: MediaKind::Video, |
| 38 | codec: Codec::Vp8, |
| 39 | clock_rate: Frequency::NINETY_KHZ, |
| 40 | }; |
| 41 | assert!(!is_opus_audio(video)); |
| 42 | } |
| 43 | |
| 44 | #[derive(drv::Input)] |
| 45 | struct TimeFacts { |
| 46 | pub at: MediaTime, |
| 47 | } |
| 48 | |
| 49 | #[drv::memo(single)] |
| 50 | fn micros(input: TimeFacts) -> i64 { |
| 51 | // identity work; the point is that MediaTime is a valid memo input |
| 52 | input.at.rebase(Frequency::MICROS).numer() as i64 |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn media_time_is_a_valid_memo_input() { |
| 57 | // Same instant expressed in two timebases — MediaTime's PartialEq |
| 58 | // (which the ToStatic impl defers to) treats them equal. |
| 59 | let a = MediaTime::new(2_000, Frequency::MILLIS); |
| 60 | let b = MediaTime::new(2_000_000, Frequency::MICROS); |
| 61 | assert_eq!(a, b); |
| 62 | assert_eq!(micros(TimeFacts { at: a }), 2_000_000); |
| 63 | assert_eq!(micros(TimeFacts { at: b }), 2_000_000); |
| 64 | } |
| 65 | |
| 66 | #[derive(drv::Input)] |
| 67 | struct MidMapInput<'a> { |
| 68 | pub by_mid: &'a HashMap<Mid, u32>, |
| 69 | } |
| 70 | |
| 71 | #[drv::memo(single)] |
| 72 | fn mid_count(input: MidMapInput<'_>) -> usize { |
| 73 | input.by_mid.len() |
| 74 | } |
| 75 | |
| 76 | #[test] |
| 77 | fn mid_works_as_a_projected_hashmap_key() { |
| 78 | let mut by_mid = HashMap::new(); |
| 79 | by_mid.insert(Mid::from("0"), 1u32); |
| 80 | by_mid.insert(Mid::from("1"), 2u32); |
| 81 | assert_eq!(mid_count(MidMapInput { by_mid: &by_mid }), 2); |
| 82 | } |