1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#[cfg(all(
feature = "stream",
not(any(feature = "gstreamer", feature = "ffmpeg"))
))]
compile_error!("Cannot enable feature \"stream\" without a backend (\"gstreamer\" or \"ffmpeg\").");
use crate::server::Config;
use eframe::egui::mutex::RwLock;
use eframe::egui::{TextureId, Vec2};
use eframe::epaint::TextureManager;
use eyre::{eyre, Context, Result};
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Formatter};
use std::fs::File;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Duration;
#[cfg(feature = "ffmpeg")]
mod ffmpeg;
#[cfg(feature = "gstreamer")]
mod gst;
pub type FrameBuffer = Arc<Vec<(TextureId, Vec2)>>;
#[derive(Clone)]
pub enum Stream {
None,
#[cfg(feature = "gstreamer")]
Gst(gst::Stream),
#[cfg(feature = "ffmpeg")]
Ffmpeg(ffmpeg::Stream),
}
pub fn stream_from_file(
tex_manager: Arc<RwLock<TextureManager>>,
path: &Path,
config: &Config,
) -> Result<Stream> {
Stream::new(tex_manager, path, config)
}
pub fn video_from_file(
tex_manager: Arc<RwLock<TextureManager>>,
path: &Path,
config: &Config,
) -> Result<(FrameBuffer, f64)> {
Stream::new(tex_manager, path, config)?.pull_samples()
}
pub trait MediaStream
where
Self: Sized,
{
fn new(tex_manager: Arc<RwLock<TextureManager>>, path: &Path, config: &Config) -> Result<Self>;
fn cloned(
&self,
frame: Arc<Mutex<Option<(TextureId, Vec2)>>>,
media_mode: StreamMode,
volume: f32,
) -> Result<Self>;
fn eos(&self) -> bool;
fn size(&self) -> [u32; 2];
fn framerate(&self) -> f64;
fn channels(&self) -> u16;
fn duration(&self) -> Duration;
fn has_video(&self) -> bool {
self.size().iter().sum::<u32>() > 0
}
fn has_audio(&self) -> bool {
self.channels() > 0
}
fn start(&mut self) -> Result<()>;
fn restart(&mut self) -> Result<()>;
fn pause(&mut self) -> Result<()>;
fn pull_samples(&self) -> Result<(FrameBuffer, f64)>;
fn process_bus(&mut self, looping: bool) -> Result<bool>;
}
#[derive(Debug, Clone)]
pub enum StreamMode {
Query,
Normal,
Muted,
SansIntTrigger,
WithExtTrigger(PathBuf),
}
#[derive(Debug, Copy, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum StreamBackend {
None,
Inherit,
#[cfg(feature = "gstreamer")]
Gst,
#[cfg(feature = "ffmpeg")]
Ffmpeg,
}
impl Default for StreamBackend {
#[inline(always)]
fn default() -> Self {
StreamBackend::Inherit
}
}
impl StreamBackend {
pub fn or(&self, other: &Self) -> Self {
if let Self::Inherit = self {
*other
} else {
*self
}
}
}
impl Debug for Stream {
#[inline(always)]
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "[video::Handle]")
}
}
impl Stream {
#[allow(unused_variables)]
pub fn new(
tex_manager: Arc<RwLock<TextureManager>>,
path: &Path,
config: &Config,
) -> Result<Self> {
{
File::open(path).wrap_err_with(|| format!("Failed to open stream file ({path:?})."))?;
}
let media_backend = config.stream_backend();
match media_backend {
StreamBackend::None => Err(eyre!("Cannot init a stream with backend=None.")),
StreamBackend::Inherit => Err(eyre!("Cannot init a stream with backend=Inherit.")),
#[cfg(feature = "ffmpeg")]
StreamBackend::Ffmpeg => {
ffmpeg::Stream::new(tex_manager, path, config).map(Stream::Ffmpeg)
}
#[cfg(feature = "gstreamer")]
StreamBackend::Gst => gst::Stream::new(tex_manager, path, config).map(Stream::Gst),
}
}
#[inline(always)]
pub fn eos(&self) -> bool {
match self {
Stream::None => true,
#[cfg(feature = "gstreamer")]
Stream::Gst(stream) => stream.eos(),
#[cfg(feature = "ffmpeg")]
Stream::Ffmpeg(stream) => stream.eos(),
}
}
#[inline(always)]
pub fn size(&self) -> [u32; 2] {
match self {
Stream::None => [0, 0],
#[cfg(feature = "gstreamer")]
Stream::Gst(stream) => stream.size(),
#[cfg(feature = "ffmpeg")]
Stream::Ffmpeg(stream) => stream.size(),
}
}
#[inline(always)]
pub fn framerate(&self) -> f64 {
match self {
Stream::None => 0.0,
#[cfg(feature = "gstreamer")]
Stream::Gst(stream) => stream.framerate(),
#[cfg(feature = "ffmpeg")]
Stream::Ffmpeg(stream) => stream.framerate(),
}
}
#[inline(always)]
pub fn channels(&self) -> u16 {
match self {
Stream::None => 0,
#[cfg(feature = "gstreamer")]
Stream::Gst(stream) => stream.channels(),
#[cfg(feature = "ffmpeg")]
Stream::Ffmpeg(stream) => stream.channels(),
}
}
#[inline(always)]
pub fn duration(&self) -> Duration {
match self {
Stream::None => Duration::default(),
#[cfg(feature = "gstreamer")]
Stream::Gst(stream) => stream.duration(),
#[cfg(feature = "ffmpeg")]
Stream::Ffmpeg(stream) => stream.duration(),
}
}
#[inline(always)]
pub fn has_video(&self) -> bool {
match self {
Stream::None => false,
#[cfg(feature = "gstreamer")]
Stream::Gst(stream) => stream.has_video(),
#[cfg(feature = "ffmpeg")]
Stream::Ffmpeg(stream) => stream.has_video(),
}
}
#[inline(always)]
pub fn has_audio(&self) -> bool {
match self {
Stream::None => false,
#[cfg(feature = "gstreamer")]
Stream::Gst(stream) => stream.has_audio(),
#[cfg(feature = "ffmpeg")]
Stream::Ffmpeg(stream) => stream.has_audio(),
}
}
pub fn start(&mut self) -> Result<()> {
match self {
Stream::None => Err(eyre!("Cannot start stream with backend=None.")),
#[cfg(feature = "gstreamer")]
Stream::Gst(stream) => stream.start(),
#[cfg(feature = "ffmpeg")]
Stream::Ffmpeg(stream) => stream.start(),
}
}
pub fn restart(&mut self) -> Result<()> {
match self {
Stream::None => Err(eyre!("Cannot restart stream with backend=None.")),
#[cfg(feature = "gstreamer")]
Stream::Gst(stream) => stream.restart(),
#[cfg(feature = "ffmpeg")]
Stream::Ffmpeg(stream) => stream.restart(),
}
}
pub fn pause(&mut self) -> Result<()> {
match self {
Stream::None => Err(eyre!("Cannot pause stream with backend=None.")),
#[cfg(feature = "gstreamer")]
Stream::Gst(stream) => stream.pause(),
#[cfg(feature = "ffmpeg")]
Stream::Ffmpeg(stream) => stream.pause(),
}
}
#[allow(unused_variables)]
pub fn process_bus(&mut self, looping: bool) -> Result<bool> {
match self {
Stream::None => Err(eyre!("Cannot process bus for stream with backend=None.")),
#[cfg(feature = "gstreamer")]
Stream::Gst(stream) => stream.process_bus(looping),
#[cfg(feature = "ffmpeg")]
Stream::Ffmpeg(stream) => stream.process_bus(looping),
}
}
#[allow(unused_variables)]
pub fn cloned(
&self,
frame: Arc<Mutex<Option<(TextureId, Vec2)>>>,
mode: StreamMode,
volume: f32,
) -> Result<Self> {
match self {
Stream::None => Err(eyre!("Cloning stream with backend=None is pointless.")),
#[cfg(feature = "gstreamer")]
Stream::Gst(stream) => stream.cloned(frame, mode, volume).map(Stream::Gst),
#[cfg(feature = "ffmpeg")]
Stream::Ffmpeg(stream) => stream.cloned(frame, mode, volume).map(Stream::Ffmpeg),
}
}
pub fn pull_samples(&self) -> Result<(FrameBuffer, f64)> {
match self {
Stream::None => Err(eyre!("Cannot pull samples from stream with backend=None.")),
#[cfg(feature = "gstreamer")]
Stream::Gst(stream) => stream.pull_samples(),
#[cfg(feature = "ffmpeg")]
Stream::Ffmpeg(stream) => stream.pull_samples(),
}
}
}