Skip to main content

gstthreadshare/runtime/
mod.rs

1// Copyright (C) 2019-2022 François Laignel <fengalin@free.fr>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0.
4// If a copy of the MPL was not distributed with this file, You can obtain one at
5// <https://mozilla.org/MPL/2.0/>.
6//
7// SPDX-License-Identifier: MPL-2.0
8
9//! A `runtime` for the `threadshare` GStreamer plugins framework.
10//!
11//! Many `GStreamer` `Element`s internally spawn OS `thread`s. For most applications, this is not an
12//! issue. However, in applications which process many `Stream`s in parallel, the high number of
13//! `threads` leads to reduced efficiency due to:
14//!
15//! * context switches,
16//! * scheduler overhead,
17//! * most of the threads waiting for some resources to be available.
18//!
19//! The `threadshare` `runtime` is a framework to build `Element`s for such applications. It
20//! uses light-weight threading to allow multiple `Element`s share a reduced number of OS `thread`s.
21//!
22//! See this [talk] ([slides]) for a presentation of the motivations and principles,
23//! and this [blog post].
24//!
25//! Current implementation uses a custom executor mostly based on the [`smol`] ecosystem.
26//!
27//! Most `Element`s implementations should use the high-level features provided by [`PadSrc`] &
28//! [`PadSink`].
29//!
30//! [talk]: https://gstconf.ubicast.tv/videos/when-adding-more-threads-adds-more-problems-thread-sharing-between-elements-in-gstreamer/
31//! [slides]: https://gstreamer.freedesktop.org/data/events/gstreamer-conference/2018/Sebastian%20Dr%C3%B6ge%20-%20When%20adding%20more%20threads%20adds%20more%20problems:%20Thread-sharing%20between%20elements%20in%20GStreamer.pdf
32//! [blog post]: https://coaxion.net/blog/2018/04/improving-gstreamer-performance-on-a-high-number-of-network-streams-by-sharing-threads-between-elements-with-rusts-tokio-crate
33//! [`smol`]: https://github.com/smol-rs/
34//! [`PadSrc`]: pad/struct.PadSrc.html
35//! [`PadSink`]: pad/struct.PadSink.html
36
37pub mod executor;
38pub use executor::{Async, Context, JoinHandle, SubTaskOutput, timer};
39
40pub mod pad;
41pub use pad::{PadSink, PadSinkRef, PadSinkWeak, PadSrc, PadSrcRef, PadSrcWeak};
42
43pub mod task;
44pub use task::{Task, TaskState};
45
46pub mod prelude {
47    pub use super::pad::{PadSinkHandler, PadSrcHandler};
48    pub use super::task::TaskImpl;
49}
50
51use std::sync::LazyLock;
52
53static RUNTIME_CAT: LazyLock<gst::DebugCategory> = LazyLock::new(|| {
54    gst::DebugCategory::new(
55        "ts-runtime",
56        gst::DebugColorFlags::empty(),
57        Some("Thread-sharing Runtime"),
58    )
59});