Skip to content

Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.

8.3 KB
Raw Blame
// Adapted from Serein (https://github.com/ViceVerse-cz/Serein) at commit a0e72cc,
// file `crates/discord-voice/src/timer.rs`.
// Copyright (c) 2026 Serein contributors. Licensed under MIT OR Apache-2.0; RustCord's
// modifications are under the same terms. See crates/voice/NOTICE.md.
//! Media threads with a precise OS timer.
//!
//! Windows rounds every timed wait up to its 15.6 ms default tick, which turns 20 ms Opus
//! ticks into jitter and 1-2 ms video pacing sleeps into a frame-rate cap. Like browsers
//! during calls, raise the resolution to 1 ms only while a media loop is running.
//!
//! Windows 11 also drops that request, and may run the process on efficiency cores, once
//! every Serein window is minimized or covered (a streamer's game). Media loops opt the
//! process out of that power throttling until the last one ends.
use std::future::Future;

pub(crate) struct Resolution {
    #[cfg(target_os = "windows")]
    raised: bool,
}

#[cfg(target_os = "windows")]
static THROTTLING_OPT_OUTS: std::sync::Mutex<usize> = std::sync::Mutex::new(0);

/// Opts the process out of (`true`) or back into (`false`) Windows power throttling.
#[cfg(target_os = "windows")]
#[allow(unsafe_code)]
fn exempt_from_power_throttling(exempt: bool) {
    use windows::Win32::System::Threading::{
        GetCurrentProcess, PROCESS_POWER_THROTTLING_CURRENT_VERSION,
        PROCESS_POWER_THROTTLING_EXECUTION_SPEED, PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION,
        PROCESS_POWER_THROTTLING_STATE, ProcessPowerThrottling, SetProcessInformation,
    };
    // A zero control mask hands both policies back to the system.
    let state = PROCESS_POWER_THROTTLING_STATE {
        Version: PROCESS_POWER_THROTTLING_CURRENT_VERSION,
        ControlMask: if exempt {
            PROCESS_POWER_THROTTLING_EXECUTION_SPEED
                | PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION
        } else {
            0
        },
        StateMask: 0,
    };
    // SAFETY: The pseudo-handle needs no closing; the struct outlives the call and its size
    // is passed exactly. Older Windows rejects unknown flags, which only keeps the default.
    let _ = unsafe {
        SetProcessInformation(
            GetCurrentProcess(),
            ProcessPowerThrottling,
            (&raw const state).cast(),
            size_of::<PROCESS_POWER_THROTTLING_STATE>() as u32,
        )
    };
}

impl Resolution {
    pub(crate) fn acquire() -> Self {
        #[cfg(target_os = "windows")]
        if let Ok(mut count) = THROTTLING_OPT_OUTS.lock() {
            if *count == 0 {
                exempt_from_power_throttling(true);
            }
            *count += 1;
        }
        #[cfg(target_os = "windows")]
        #[allow(unsafe_code)]
        // SAFETY: A process-scoped request, balanced by timeEndPeriod in Drop on success.
        let raised = unsafe { windows::Win32::Media::timeBeginPeriod(1) } == 0;
        Self {
            #[cfg(target_os = "windows")]
            raised,
        }
    }
}

impl Drop for Resolution {
    fn drop(&mut self) {
        #[cfg(target_os = "windows")]
        if self.raised {
            #[allow(unsafe_code)]
            // SAFETY: Balances the successful timeBeginPeriod(1) in acquire.
            unsafe {
                windows::Win32::Media::timeEndPeriod(1);
            }
        }
        #[cfg(target_os = "windows")]
        if let Ok(mut count) = THROTTLING_OPT_OUTS.lock() {
            *count = count.saturating_sub(1);
            if *count == 0 {
                exempt_from_power_throttling(false);
            }
        }
    }
}

/// The calling thread's registration with Windows' multimedia scheduler; reverted on drop.
pub(crate) struct Realtime {
    #[cfg(target_os = "windows")]
    task: Option<windows::Win32::Foundation::HANDLE>,
}

impl Realtime {
    /// Give the calling thread real-time scheduling for audio: MMCSS "Pro Audio" at high
    /// priority (what browsers and Discord's own client use for their audio threads), or
    /// time-critical priority if MMCSS refuses. Without it a call's threads run at normal
    /// priority and wait behind a game that keeps every core busy, which is heard as
    /// pauses in what you hear and in your microphone.
    pub(crate) fn acquire(what: &'static str) -> Self {
        #[cfg(target_os = "windows")]
        #[allow(unsafe_code)]
        {
            use windows::Win32::System::Threading::{
                AVRT_PRIORITY_HIGH, AvSetMmThreadCharacteristicsW, AvSetMmThreadPriority,
                GetCurrentThread, SetThreadPriority, THREAD_PRIORITY_TIME_CRITICAL,
            };
            let mut index = 0u32;
            // SAFETY: A static, NUL-terminated task name and a live out-pointer; the
            // returned handle is released by AvRevertMmThreadCharacteristics in Drop.
            let task = unsafe { AvSetMmThreadCharacteristicsW(windows::core::w!("Pro Audio"), &mut index) };
            match task {
                Ok(task) => {
                    // SAFETY: `task` is the handle MMCSS just returned for this thread.
                    let _ = unsafe { AvSetMmThreadPriority(task, AVRT_PRIORITY_HIGH) };
                    log::info!("voice audio: {what} thread runs at real-time priority (MMCSS Pro Audio)");
                    Self { task: Some(task) }
                }
                Err(error) => {
                    // SAFETY: The pseudo-handle of the calling thread needs no closing.
                    let raised = unsafe { SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL) }.is_ok();
                    log::warn!("voice audio: MMCSS refused the {what} thread ({error}); time-critical priority: {raised}");
                    Self { task: None }
                }
            }
        }
        #[cfg(not(target_os = "windows"))]
        {
            let _ = what;
            Self {}
        }
    }

    /// Once per thread, for threads this crate does not own (the device callbacks): the
    /// registration lives until that thread ends.
    pub(crate) fn for_this_thread(what: &'static str) {
        thread_local! {
            static REALTIME: std::cell::OnceCell<Realtime> = const { std::cell::OnceCell::new() };
        }
        let _ = REALTIME.try_with(|slot| {
            slot.get_or_init(|| Realtime::acquire(what));
        });
    }
}

impl Drop for Realtime {
    fn drop(&mut self) {
        #[cfg(target_os = "windows")]
        if let Some(task) = self.task.take() {
            #[allow(unsafe_code)]
            // SAFETY: Balances the successful AvSetMmThreadCharacteristicsW on this thread.
            let _ = unsafe { windows::Win32::System::Threading::AvRevertMmThreadCharacteristics(task) };
        }
    }
}

/// Run one media loop on its own thread and single-threaded runtime, so gateway, image and
/// UI tasks on the shared application runtime cannot delay audio ticks or paced video.
/// Dropping the returned future (a task abort) stops the loop at its next await.
pub(crate) async fn isolated<F, Fut>(name: &'static str, start: F) -> Result<(), &'static str>
where
    F: FnOnce() -> Fut + Send + 'static,
    Fut: Future<Output = Result<(), &'static str>>,
{
    let (mut done, result) = tokio::sync::oneshot::channel();
    std::thread::Builder::new()
        .name(name.into())
        .spawn(move || {
            let _resolution = Resolution::acquire();
            // The call's own loop carries the microphone and everything you hear; stream
            // loops pace video and stay at normal priority.
            let _realtime = (name == "rustcord-voice").then(|| Realtime::acquire("media"));
            let Ok(runtime) = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
            else {
                let _ = done.send(Err("Media runtime could not start"));
                return;
            };
            let outcome = runtime.block_on(async {
                let work = start();
                tokio::pin!(work);
                tokio::select! {
                    outcome = &mut work => Some(outcome),
                    () = done.closed() => None,
                }
            });
            if let Some(outcome) = outcome {
                let _ = done.send(outcome);
            }
        })
        .map_err(|_| "Media thread could not start")?;
    result
        .await
        .unwrap_or(Err("Media thread stopped unexpectedly"))
}