forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBe12d32fTahinli6 days ago3982 commits

knobs.rs104 lines, 3.4 KiBHistoryRawDownload
1//! Debug switches the AAC/SBR decoder reads from the environment, with a
2//! per-thread override that tests set instead of editing the process
3//! environment (which is `unsafe` in Rust 2024).
4//!
5//! The decoder reads these on the thread that calls `decode`, so an override
6//! set on a test's own thread covers exactly that test's decodes and nothing
7//! running in parallel. With no override set, each reader answers from the
8//! environment exactly as it always has, so manual runs with `EC_AAC_*` set
9//! behave as before.
10
11use std::cell::Cell;
12use std::thread::LocalKey;
13
14type Slot = Cell<Option<bool>>;
15
16thread_local! {
17 static TOOL_SIDEINFO_DEBUG: Slot = const { Cell::new(None) };
18 static SBR_SIDEINFO_DEBUG: Slot = const { Cell::new(None) };
19 static SBR_HF_BYPASS: Slot = const { Cell::new(None) };
20 static SBR_NOISE_ZERO: Slot = const { Cell::new(None) };
21 static SBR_NOISE_FRACTION: Slot = const { Cell::new(None) };
22}
23
24/// The override on this thread if one is set, else whether `var` is set.
25fn enabled(slot: &'static LocalKey<Slot>, var: &str) -> bool {
26 slot.with(Cell::get)
27 .unwrap_or_else(|| std::env::var(var).is_ok())
28}
29
30fn force(slot: &'static LocalKey<Slot>, on: Option<bool>) -> Override<Option<bool>> {
31 let prev = slot.with(|c| c.replace(on));
32 Override { slot, prev }
33}
34
35/// Restores the value a knob had before [`set_tool_sideinfo_debug`] and its
36/// siblings were called, when dropped.
37#[doc(hidden)]
38#[must_use = "the override ends as soon as this guard is dropped"]
39pub struct Override<T: Copy + 'static> {
40 slot: &'static LocalKey<Cell<T>>,
41 prev: T,
42}
43
44impl<T: Copy + 'static> Drop for Override<T> {
45 fn drop(&mut self) {
46 self.slot.with(|c| c.set(self.prev));
47 }
48}
49
50/// `EC_AAC_TOOL_SIDEINFO_DEBUG`.
51pub(crate) fn tool_sideinfo_debug() -> bool {
52 enabled(&TOOL_SIDEINFO_DEBUG, "EC_AAC_TOOL_SIDEINFO_DEBUG")
53}
54
55/// `EC_AAC_SBR_SIDEINFO_DEBUG`.
56pub(crate) fn sbr_sideinfo_debug() -> bool {
57 enabled(&SBR_SIDEINFO_DEBUG, "EC_AAC_SBR_SIDEINFO_DEBUG")
58}
59
60/// `EC_AAC_SBR_HF_BYPASS`.
61pub(crate) fn sbr_hf_bypass() -> bool {
62 enabled(&SBR_HF_BYPASS, "EC_AAC_SBR_HF_BYPASS")
63}
64
65/// `EC_AAC_SBR_NOISE_ZERO`.
66pub(crate) fn sbr_noise_zero() -> bool {
67 enabled(&SBR_NOISE_ZERO, "EC_AAC_SBR_NOISE_ZERO")
68}
69
70/// `EC_AAC_SBR_NOISE_FRACTION`.
71pub(crate) fn sbr_noise_fraction() -> bool {
72 enabled(&SBR_NOISE_FRACTION, "EC_AAC_SBR_NOISE_FRACTION")
73}
74
75/// Forces `EC_AAC_TOOL_SIDEINFO_DEBUG` on (`Some(true)`), off (`Some(false)`)
76/// or back to the environment (`None`) on this thread, until the guard drops.
77#[doc(hidden)]
78pub fn set_tool_sideinfo_debug(on: Option<bool>) -> Override<Option<bool>> {
79 force(&TOOL_SIDEINFO_DEBUG, on)
80}
81
82/// [`set_tool_sideinfo_debug`] for `EC_AAC_SBR_SIDEINFO_DEBUG`.
83#[doc(hidden)]
84pub fn set_sbr_sideinfo_debug(on: Option<bool>) -> Override<Option<bool>> {
85 force(&SBR_SIDEINFO_DEBUG, on)
86}
87
88/// [`set_tool_sideinfo_debug`] for `EC_AAC_SBR_HF_BYPASS`.
89#[doc(hidden)]
90pub fn set_sbr_hf_bypass(on: Option<bool>) -> Override<Option<bool>> {
91 force(&SBR_HF_BYPASS, on)
92}
93
94/// [`set_tool_sideinfo_debug`] for `EC_AAC_SBR_NOISE_ZERO`.
95#[doc(hidden)]
96pub fn set_sbr_noise_zero(on: Option<bool>) -> Override<Option<bool>> {
97 force(&SBR_NOISE_ZERO, on)
98}
99
100/// [`set_tool_sideinfo_debug`] for `EC_AAC_SBR_NOISE_FRACTION`.
101#[doc(hidden)]
102pub fn set_sbr_noise_fraction(on: Option<bool>) -> Override<Option<bool>> {
103 force(&SBR_NOISE_FRACTION, on)
104}