Skip to main content

moq_ext_sources/
lib.rs

1//! Media sources for [`moq_video`] and [`moq_audio`] that upstream moq lacks.
2//!
3//! A source is an opened device, file or generator that produces raw frames,
4//! on a thread of its own, for a publisher to encode. A [`VideoSource`] can
5//! feed several encoders at once and offers a latest-wins preview through
6//! [`VideoFrames`]. An [`AudioSource`] fans PCM out to every broadcast that
7//! reads it.
8//!
9//! The crate has no dependency on iroh and none on a transport. Its [`Error`]
10//! is the one the player and the publisher return too, so an error passes
11//! from a source to a broadcast unchanged. [`SlotState`] is the state of a
12//! media slot on a broadcast and on a player, so a UI reports both the same
13//! way, and [`stats`] counts rates the same way on both sides.
14//!
15//! The sources, their constructors and the features they need:
16//!
17//! | Source | Constructor | Feature |
18//! |---|---|---|
19//! | Test picture | [`VideoSource::test_pattern`], [`AudioSource::test_pattern`] | `pattern` |
20//! | Moving gradient | [`VideoSource::test_gradient`] | `pattern` |
21//! | Bouncing square, also drawn on demand | [`VideoSource::test_bounce`], [`BouncePattern`] | `pattern` for the source |
22//! | Sine tone | [`AudioSource::tone`] | `pattern` |
23//! | WAV, MP3 and MP4 audio file | [`AudioSource::file`] | `file` |
24//! | MP4 file, decoded or passed through | [`MediaFile`] | `file` |
25//! | Camera, display, window | [`VideoSource::capture`], [`VideoSource::camera`], [`VideoSource::screen`] | `capture` |
26//! | Microphone | [`AudioSource::microphone`] | `capture` |
27//! | Raspberry Pi camera through `rpicam-vid` | [`VideoSource::rpicam`], `rpicam_h264` | `rpicam` (Linux) |
28//! | Pushed by the application | [`VideoSource::push`], [`AudioSource::push`], [`VideoSource::spawn`], [`AudioSource::spawn`] | always |
29//! | Any of the above, switched while published | [`VideoSource::switchable`] | always |
30//!
31//! [`EncodedVideo`] and [`EncodedAudio`] are media that arrives encoded, from
32//! [`MediaFile`] or `rpicam_h264`, for a broadcast to publish without an
33//! encoder.
34//!
35//! [`VideoSource::scaled`] scales any of them to a fixed size as CPU I420,
36//! not for publishing.
37//!
38//! [`PortalGrant`] keeps the desktop portal's grant in a file, so a later run
39//! can skip the picker (Linux, `pipewire` feature).
40//!
41//! [`Backend`] is a choice of video encoder or decoder backend, as a
42//! `--encoder` or `--decoder` flag takes it.
43//!
44//! [`spec`] parses text specifiers such as `cam:2` or `test:gradient`, as a
45//! `--video` or `--audio` flag takes them, and opens the source they name.
46//!
47//! The crate README lists every feature flag. [`local_task::spawn`] runs a
48//! future on a thread of its own for capture streams that are not `Send`.
49//!
50//! Most of this belongs upstream. The pattern and tone generators fit in
51//! `moq-video`, the audio file source in `moq-audio`, and the rpicam source
52//! as a `moq-video` capture backend.
53//!
54//! Every constructor that is `async` is cancellation safe. Dropping the
55//! future stops the thread or subprocess it started and releases the device.
56//! [`VideoFrames::next`] and [`FrameSender::closed`] lose nothing when dropped.
57//!
58//! # Examples
59//!
60//! ```
61//! # #[cfg(feature = "pattern")]
62//! # async fn example() {
63//! use moq_ext_sources::VideoSource;
64//!
65//! let source = VideoSource::test_pattern(
66//!     moq_video::Size::new(640, 360),
67//!     moq_video::FrameRate::new(30, 1).expect("a valid rate"),
68//! )
69//! .expect("the pattern thread starts");
70//! let mut frames = source.frames();
71//! while let Some(frame) = frames.next().await {
72//!     // draw `frame`
73//! #   let _ = frame;
74//! #   break;
75//! }
76//! # }
77//! ```
78
79#![forbid(unsafe_code)]
80
81mod backend;
82mod bounce;
83mod encoded;
84mod error;
85pub mod frames;
86#[cfg(any(feature = "capture", all(target_os = "linux", feature = "rpicam")))]
87pub mod local_task;
88#[cfg(all(target_os = "linux", feature = "pipewire"))]
89mod portal_grant;
90mod source;
91pub mod spec;
92pub mod stats;
93mod status;
94
95use moq_audio as audio;
96use moq_video as video;
97
98#[cfg(all(target_os = "linux", feature = "pipewire"))]
99pub use self::portal_grant::PortalGrant;
100#[cfg(feature = "capture")]
101pub use self::source::screen_source;
102#[cfg(feature = "file")]
103pub use self::source::{AudioInfo, MediaFile, MediaInfo, VideoFileConfig, VideoInfo};
104#[cfg(all(target_os = "linux", feature = "rpicam"))]
105pub use self::source::{RpicamConfig, rpicam_h264};
106pub use self::{
107    backend::{Backend, UnknownBackend},
108    bounce::BouncePattern,
109    encoded::{EncodedAudio, EncodedFrame, EncodedVideo},
110    error::{Error, SourceClosed},
111    frames::VideoFrames,
112    source::{
113        AudioFormat, AudioSource, FileEnd, FrameSender, VideoFormat, VideoSource, VideoSwitch,
114    },
115    status::{ByPtr, SlotState},
116};
117// Shared with moq-ext-publisher and moq-ext-player, not public API.
118#[doc(hidden)]
119pub use self::{
120    source::{AudioKind, Demand, DemandGuard, PcmFanout, WeakPcmFanout},
121    status::send_if_changed,
122};