egui-sharkplayer 0.3.0

A hardware accelerated video player for egui using libmpv
Documentation
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
use eframe::glow::{self, HasContext as _};
use libmpv2::render::{OpenGLInitParams, RenderContext, RenderParam, RenderParamApiType, mpv_render_update};
use libmpv2::{Mpv, mpv_error};
use std::cell::RefCell;
use std::ffi::{CStr, CString, c_void};
use std::rc::Rc;
use std::sync::{Arc, OnceLock};
use tracing::{debug, error, trace};

/// Get Proc Address -- Store eframe's `get_proc_address` wrapper so the FFI
/// function can access it.
#[expect(clippy::type_complexity)]
static GPA: OnceLock<Arc<dyn Fn(&CStr) -> *const c_void + Send + Sync>> = OnceLock::new();

fn gl_get_proc_address(_ctx: &(), name: &str) -> *mut c_void {
    // SAFETY: It's NOT okay to panic here. We must return a null pointer instead.

    let Some(gpa) = GPA.get() else {
        return std::ptr::null_mut();
    };
    let Ok(cname) = CString::new(name) else {
        return std::ptr::null_mut();
    };
    gpa(&cname).cast_mut()
}

#[derive(Debug, thiserror::Error, PartialEq, Eq, Hash)]
pub enum BackendError {
    #[error("This library only functions when egui is using glow, but the GL context was unavailable.")]
    ExpectedGlow,
    #[error("libmpv2: {0}")]
    Mpv(#[from] libmpv2::Error),
    #[error("glow: {0}")]
    Glow(String),
    #[error("Incomplete framebuffer (0x{0:x}); Ensure RGBA8 support.")]
    IncompleteFramebuffer(u32),
    #[error("Tried to use GL resoures after they were destroyed by PlayerState::destroy_gl_resources.")]
    UseAfterDestroy,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct FramebufferSize {
    pub width:  i32,
    pub height: i32,
}

struct MpvContainer {
    // SAFETY: `render_ctx` MUST be dropped prior to `mpv`. Which means that `render_ctx` MUST be prior to
    // `mpv` in the struct ordering.
    ctx: RenderContext<'static>,
    mpv: Mpv,
}

#[derive(Clone, Copy)]
pub(crate) struct GlResources {
    texture:          glow::NativeTexture,
    framebuffer:      glow::NativeFramebuffer,
    framebuffer_size: FramebufferSize,
}

impl GlResources {
    pub(crate) fn framebuffer(&self) -> glow::NativeFramebuffer { self.framebuffer }

    pub(crate) fn framebuffer_size(&self) -> FramebufferSize { self.framebuffer_size }
}

#[derive(Debug, Default)]
struct CachedProperty<T: Clone + std::fmt::Debug>(RefCell<Option<T>>);
impl<T: Clone + std::fmt::Debug> CachedProperty<T> {
    fn clear(&self) { self.0.replace(None); }

    fn get_or_try_init<E, I, S>(&self, init: I, store: S) -> Result<T, E>
    where
        I: FnOnce() -> Result<T, E>,
        S: FnOnce(&T) -> bool, {
        if let Some(v) = self.0.borrow().clone() {
            return Ok(v);
        }

        match init() {
            Ok(val) if store(&val) => {
                debug!("Caching property: {val:?}");
                self.0.replace(Some(val.clone()));
                Ok(val)
            }
            Ok(val) => Ok(val),
            Err(e) => Err(e),
        }
    }
}

type OptionalStr = Option<Rc<str>>;
type OptionF64 = Option<f64>;

#[derive(Debug, Default)]
struct Properties {
    paused: CachedProperty<bool>,
    volume: CachedProperty<f64>,
    muted:  CachedProperty<bool>,

    // --- File Specific Properties ---
    colormatrix:     CachedProperty<OptionalStr>,
    container_fps:   CachedProperty<Option<f64>>,
    current_demuxer: CachedProperty<OptionalStr>,
    dimensions:      CachedProperty<Option<(i64, i64)>>,
    duration:        CachedProperty<Option<f64>>,
    file_format:     CachedProperty<OptionalStr>,
    filename:        CachedProperty<OptionalStr>,
    hwdec_current:   CachedProperty<OptionalStr>,
    media_title:     CachedProperty<OptionalStr>,
    video_codec:     CachedProperty<OptionalStr>,
    video_format:    CachedProperty<OptionalStr>,
}

impl Properties {
    fn clear(&self) {
        self.colormatrix.clear();
        self.container_fps.clear();
        self.current_demuxer.clear();
        self.dimensions.clear();
        self.duration.clear();
        self.file_format.clear();
        self.filename.clear();
        self.hwdec_current.clear();
        self.media_title.clear();
        self.video_codec.clear();
        self.video_format.clear();
    }
}

macro_rules! cached_prop {
    ($state:expr, $field:ident, $prop:expr, OptionalStr, $validation:expr) => {{
        cached_prop!(
            $state,
            $field,
            $prop,
            || {
                let $field: Option<Rc<str>> = $state.get_optional_property::<String>($prop)?.map(Rc::from);
                Ok($field)
            },
            $validation
        )
    }};
    ($state:expr, $field:ident, $prop:expr, RcStr, $validation:expr) => {{
        cached_prop!(
            $state,
            $field,
            $prop,
            || { Ok(Rc::from($state.mpv.mpv.get_property::<String>($prop)?)) },
            $validation
        )
    }};
    ($state:expr, $field:ident, $prop:expr, OptionF64, $validation:expr) => {{
        cached_prop!(
            $state,
            $field,
            $prop,
            || { $state.get_optional_property::<f64>($prop) },
            $validation
        )
    }};
    ($state:expr, $field:ident, $prop:expr, f64, $validation:expr) => {{
        cached_prop!(
            $state,
            $field,
            $prop,
            || { Ok($state.mpv.mpv.get_property::<f64>($prop)?) },
            $validation
        )
    }};
    ($state:expr, $field:ident, $prop:expr, bool, $validation:expr) => {{
        cached_prop!(
            $state,
            $field,
            $prop,
            || { Ok($state.mpv.mpv.get_property::<bool>($prop)?) },
            $validation
        )
    }};

    ($state:expr, $field:ident, $prop:expr, $init:expr, $validation:expr) => {{ $state.properties.$field.get_or_try_init($init, $validation) }};
}

macro_rules! def_cached_getters {
    (
        $(
            $(#[$attr:meta])*
            $field:ident, $prop:expr, $type:tt $(, $validation:expr)?
        );+ $(;)?
    ) => {
        $(
            $(#[$attr])*
            /// # Errors
            ///
            /// This function returns `Ok(None)` if the property is
            /// unavailable, and returns an `Err(_)` if a libmpv error occured.
            pub fn $field(&self) -> Result<$type, BackendError> {
                def_cached_getters!(@internal self, $field, $prop, $type $(, $validation)?)
            }
        )+
    };

    (@internal $self:ident, $field:ident, $prop:expr, $type:tt, $validation:expr) => {
        cached_prop!($self, $field, $prop, $type, $validation)
    };
    (@internal $self:ident, $field:ident, $prop:expr, $type:tt) => {
        cached_prop!($self, $field, $prop, $type, |_| true)
    };
}

/// The persistant state for the video player. This must be created on the UI
/// thread and persist across frames.
pub struct PlayerState {
    mpv:          MpvContainer,
    gl:           Arc<glow::Context>,
    gl_resources: Option<GlResources>,
    properties:   Properties,
}

impl PlayerState {
    def_cached_getters!(
        colormatrix, "colormatrix", OptionalStr;
        container_fps, "container-fps", OptionF64;
        current_demuxer, "current-demuxer", OptionalStr;
        /// Get the file format of the currently loaded media.
        file_format, "file-format", OptionalStr;
        /// Get the filename of the currently loaded media.
        filename, "filename", OptionalStr;
        /// Get which hardware decoder is being used, if any.
        hwdec_current, "hwdec-current", OptionalStr;
        /// Get the title of the current media.
        media_title, "media_title", OptionalStr;
        /// Get whether playback is muted.
        muted, "mute", bool;
        /// Get the codec of the current video.
        video_codec, "video-codec", OptionalStr;
        /// Get the format of the current video.
        video_format, "video-format", OptionalStr;
        /// Get the current playback volume
        volume, "volume", f64;
        /// Get whether playback is paused.
        paused, "pause", bool;
        /// Get the total duration of the current video in seconds.
        duration, "duration", OptionF64, |dur| dur.is_some_and(|dur| dur > 0.);
    );

    /// Get the dimensions of the loaded video.
    ///
    /// Upon success, this function returns `Ok(Some(width, height))`.
    ///
    /// # Errors
    ///
    /// If the dimensions are unavailable, it returns `Ok(None)`. An `Err(_)` is
    /// returned in the event of a [`BackendError`].
    pub fn dimensions(&self) -> Result<Option<(i64, i64)>, BackendError> {
        self.properties.dimensions.get_or_try_init(
            || {
                let w = self.get_optional_property::<i64>("video-out-params/dw")?;
                let h = self.get_optional_property::<i64>("video-out-params/dh")?;
                Ok(w.zip(h))
            },
            |dimensions|
            // Only cache valid dimensions. If we request prior to the first frame being
            // decoded, we won't get anything.
            dimensions.is_some_and(|(w, h)| w > 0 && h > 0),
        )
    }

    /// Get the aspect ratio of the loaded video.
    /// # Errors
    ///
    /// If the dimensions are unavailable, it returns `Ok(None)`. An `Err(_)` is
    /// returned in the event of a [`BackendError`].
    #[expect(clippy::cast_precision_loss)]
    pub fn aspect_ratio(&self) -> Result<Option<f64>, BackendError> {
        Ok(self
            .dimensions()?
            .and_then(|(w, h)| if h != 0 { Some(w as f64 / h as f64) } else { None }))
    }

    /// Create a new [`PlayerState`] using an [`eframe::CreationContext`].
    ///
    /// # Panics
    ///
    /// This function should **never** panic. If it does, this is a bug.
    ///
    /// # Errors
    ///
    /// An `Err(_)` is returned if there was an issue initializing the libmpv
    /// backend.
    pub fn new(cc: &eframe::CreationContext<'_>) -> Result<Self, BackendError> {
        let gl = cc.gl.clone().ok_or(BackendError::ExpectedGlow)?;

        let _ = GPA.get_or_init(|| {
            cc.get_proc_address
                .clone()
                .expect("egui's get_proc_address wrapper is unavailable despite using glow")
        });

        let mpv = Mpv::new()?;
        if cfg!(debug_assertions) {
            mpv.set_property("terminal", "yes")?;
        }
        mpv.set_property("vo", "libmpv")?;
        mpv.set_property("hwdec", "auto-safe")?;
        // Support HDR video
        mpv.set_property("tone-mapping", "auto")?;
        mpv.set_property("video-timing-offset", 0.0_f64)?;
        // Ensure that completed videos are rewatchable.
        mpv.set_property("keep-open", "yes")?;

        let render_ctx = mpv.create_render_context([
            RenderParam::ApiType(RenderParamApiType::OpenGl),
            RenderParam::InitParams(OpenGLInitParams {
                get_proc_address: gl_get_proc_address,
                ctx:              (),
            }),
        ])?;

        // SAFETY: This transmutation is done to erase the lifetime, but it's safe so
        // long as the render context is dropped before `mpv`. This is the case because
        // it's placed in `MpvContainer`, which is a private struct with the correct
        // drop order.
        let mut render_ctx: RenderContext<'static> = unsafe { std::mem::transmute(render_ctx) };
        let egui_ctx = cc.egui_ctx.clone();
        // This update callback is what ensures we show frames when we need to.
        render_ctx.set_update_callback(move || {
            egui_ctx.request_repaint();
        });

        Ok(Self {
            mpv: MpvContainer { ctx: render_ctx, mpv },
            gl,
            gl_resources: None,
            properties: Properties::default(),
        })
    }

    /// Load the file at `path`.
    ///
    /// # Errors
    ///
    /// An `Err(_)` is returned in the event of a [`BackendError`].
    pub fn load_file(&self, path: impl AsRef<str>) -> Result<(), BackendError> {
        let path = path.as_ref();

        self.properties.clear();

        self.mpv.mpv.command("loadfile", &[path, "replace"])?;
        Ok(())
    }

    /// Toggle playback status.
    ///
    /// # Errors
    ///
    /// An `Err(_)` is returned in the event of a [`BackendError`].
    pub fn toggle_pause(&self) -> Result<(), BackendError> {
        let paused = self.paused()?;
        self.mpv.mpv.set_property("pause", !paused)?;
        self.properties.paused.clear();
        trace!("Toggled playback: {}", if paused { "playing" } else { "paused" });
        Ok(())
    }

    /// # Errors
    ///
    /// An `Err(_)` is returned in the event of a [`BackendError`].
    pub fn play(&self) -> Result<(), BackendError> {
        self.mpv.mpv.set_property("pause", false)?;
        self.properties.paused.clear();
        Ok(())
    }

    /// # Errors
    ///
    /// An `Err(_)` is returned in the event of a [`BackendError`].
    pub fn pause(&self) -> Result<(), BackendError> {
        self.mpv.mpv.set_property("pause", true)?;
        self.properties.paused.clear();
        Ok(())
    }

    /// # Errors
    ///
    /// An `Err(_)` is returned in the event of a [`BackendError`].
    pub fn toggle_mute(&self) -> Result<(), BackendError> {
        let is_muted = self.muted()?;
        self.mpv.mpv.set_property("mute", !is_muted)?;
        self.properties.muted.clear();
        Ok(())
    }

    /// Set the playback volume as a percentage in the range `0.0..=100.0`.
    ///
    /// # Errors
    ///
    /// An `Err(_)` is returned in the event of a [`BackendError`].
    pub fn set_volume(&self, volume: f64) -> Result<(), BackendError> {
        self.mpv.mpv.set_property("volume", volume.clamp(0., 100.))?;
        self.properties.volume.clear();
        trace!("Set volume to {volume}%.");
        Ok(())
    }

    /// Get the current playback position in seconds.
    ///
    /// # Errors
    ///
    /// This function returns `Ok(None)` if the `time-pos` property is
    /// unavailable. An `Err(_)` is returned in the event of a [`BackendError`].
    pub fn time_pos(&self) -> Result<Option<f64>, BackendError> { self.get_optional_property("time-pos") }

    /// Seek to an exact timestamp in the video.
    ///  
    /// # Errors
    ///
    /// An `Err(_)` is returned in the event of a [`BackendError`].
    pub fn seek_to(&self, seconds: f64) -> Result<(), BackendError> {
        self.mpv.mpv.set_property("time-pos", seconds)?;
        trace!("Seeked to {seconds:.3}.");
        Ok(())
    }

    /// Seek to an offset relative to the current position.
    ///
    /// # Errors
    ///
    /// An `Err(_)` is returned in the event of a [`BackendError`].
    pub fn seek_relative(&self, seconds: f64) -> Result<(), BackendError> {
        let seconds = seconds.to_string();
        self.mpv.mpv.command("seek", &[&seconds, "relative+exact"])?;
        trace!("Seeked {seconds:.3}.");
        Ok(())
    }

    /// Get the frame drop count.
    ///
    /// # Errors
    ///
    /// An `Err(_)` is returned in the event of a [`BackendError`].
    pub fn frame_drop_count(&self) -> Result<Option<i64>, BackendError> {
        self.get_optional_property("frame-drop-count")
    }

    /// Call this in your app's [`App::on_exit`][eframe::App::on_exit]
    /// implementation to clean up OpenGL resources.
    ///
    /// ```rust
    /// # use egui_sharkplayer::PlayerState;
    /// struct App {
    ///     player: PlayerState,
    /// }
    ///
    /// impl eframe::App for App {
    ///     fn on_exit(&mut self, _gl: Option<&eframe::glow::Context>) {
    ///         self.player.destroy_gl_resources();
    ///     }
    ///     // ...
    /// #   fn ui(&mut self, _ui: &mut eframe::egui::Ui, _frame: &mut eframe::Frame) { unimplemented!(); }
    /// }
    /// ```
    pub fn destroy_gl_resources(&mut self) {
        if let Some(GlResources {
            framebuffer,
            texture,
            framebuffer_size: _,
        }) = self.gl_resources.take()
        {
            unsafe {
                self.gl.delete_framebuffer(framebuffer);
                self.gl.delete_texture(texture);
            }
        }
    }

    /// Get the framebuffer associated with the current frame, resizing if
    /// necessary.
    pub(crate) fn get_current_framebuffer(
        &mut self,
        size: FramebufferSize,
    ) -> Result<GlResources, BackendError> {
        let (res, reallocated) = if let Some(res) = self.gl_resources.take() {
            // Reallocate framebuffer
            if res.framebuffer_size != size && size.width > 0 && size.height > 0 {
                // SAFETY:
                //
                // 1. This is on the UI thread because `Self::new` guarantees as much.
                unsafe {
                    self.gl.delete_framebuffer(res.framebuffer);
                    self.gl.delete_texture(res.texture);
                }

                // SAFETY: If allocation fails, then gl_resources is `None` so there are no
                // use-after-free errors.
                let new_res = unsafe { Self::allocate_framebuffer(&self.gl, size)? };
                self.gl_resources = Some(new_res);
                (new_res, true)
            } else {
                self.gl_resources = Some(res);
                (res, false)
            }
        } else {
            let res = unsafe { Self::allocate_framebuffer(&self.gl, size)? };
            self.gl_resources = Some(res);
            (res, true)
        };

        let flags = self.mpv.ctx.update().unwrap_or(0);
        if reallocated || flags & mpv_render_update::Frame != 0 {
            match res.framebuffer.0.get().try_into() {
                Ok(fb) => self.mpv.ctx.render::<()>(fb, size.width, size.height, true)?,
                Err(e) => {
                    error!("GL framebuffer ID is too large for libmpv2: {e}");
                }
            }
        }

        Ok(res)
    }

    fn get_optional_property<T: libmpv2::GetData>(&self, name: &str) -> Result<Option<T>, BackendError> {
        match self.mpv.mpv.get_property(name) {
            Ok(value) => Ok(Some(value)),
            Err(libmpv2::Error::Raw(e)) if e == mpv_error::PropertyUnavailable => Ok(None),
            Err(e) => Err(BackendError::Mpv(e)),
        }
    }

    /// Allocate an RGBA8 texture and framebuffer.
    ///
    /// # Safety
    ///
    /// 1. GL context MUST be located on the current thread.
    #[expect(unsafe_op_in_unsafe_fn)]
    unsafe fn allocate_framebuffer(
        gl: &glow::Context,
        size: FramebufferSize,
    ) -> Result<GlResources, BackendError> {
        let FramebufferSize { width, height } = size;

        let texture = gl.create_texture().map_err(BackendError::Glow)?;
        gl.bind_texture(glow::TEXTURE_2D, Some(texture));
        gl.tex_image_2d(
            glow::TEXTURE_2D,
            0,
            glow::RGBA8.cast_signed(),
            width,
            height,
            0,
            glow::RGBA,
            glow::UNSIGNED_BYTE,
            glow::PixelUnpackData::Slice(None),
        );

        for (name, val) in [
            (glow::TEXTURE_MIN_FILTER, glow::LINEAR.cast_signed()),
            (glow::TEXTURE_MAG_FILTER, glow::LINEAR.cast_signed()),
            (glow::TEXTURE_WRAP_S, glow::CLAMP_TO_EDGE.cast_signed()),
            (glow::TEXTURE_WRAP_T, glow::CLAMP_TO_EDGE.cast_signed()),
        ] {
            gl.tex_parameter_i32(glow::TEXTURE_2D, name, val);
        }
        gl.bind_texture(glow::TEXTURE_2D, None);

        let framebuffer = gl.create_framebuffer().map_err(BackendError::Glow)?;
        gl.bind_framebuffer(glow::FRAMEBUFFER, Some(framebuffer));

        gl.framebuffer_texture_2d(
            glow::FRAMEBUFFER,
            glow::COLOR_ATTACHMENT0,
            glow::TEXTURE_2D,
            Some(texture),
            0,
        );

        let status = gl.check_framebuffer_status(glow::FRAMEBUFFER);
        if status != glow::FRAMEBUFFER_COMPLETE {
            gl.bind_framebuffer(glow::FRAMEBUFFER, None);
            // NOTE: dellocate to prevent memory leaks.
            gl.delete_framebuffer(framebuffer);
            gl.delete_texture(texture);
            return Err(BackendError::IncompleteFramebuffer(status));
        }

        gl.bind_framebuffer(glow::FRAMEBUFFER, None);

        Ok(GlResources {
            texture,
            framebuffer,
            framebuffer_size: size,
        })
    }
}