mutsumi 0.2.0

wl-proxy based GTK MPV embedder
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
use glib::Object;
use gtk::{gdk::prelude::PaintableExt, glib, subclass::prelude::*};
use tracing::info;

use crate::{
    PlayParams,
    video::{
        backend::{TrackKind, TrackSelection},
        mpv::contexted::ContextedMPV,
    },
};

use gtk::gdk;

mod imp {
    use crate::{
        FRAME_CHANNEL, create_mpv_proxy,
        video::{MutsumiMpvError, mpv::contexted::ContextedMPV},
    };
    use std::{cell::RefCell, os::fd::AsRawFd, sync::OnceLock};

    use super::*;

    use glib::subclass::Signal;
    use gtk::{glib, prelude::*};

    #[derive(Default)]
    pub struct MutsumiVideoSink {
        pub mpv: ContextedMPV,
        pub texture: RefCell<Option<gdk::Texture>>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for MutsumiVideoSink {
        const NAME: &'static str = "MutsumiVideoSink";
        type Type = super::MutsumiVideoSink;
        type Interfaces = (gdk::Paintable,);
    }

    impl ObjectImpl for MutsumiVideoSink {
        fn constructed(&self) {
            self.parent_constructed();

            let obj = self.obj();

            self.setup_mpv();

            glib::spawn_future_local(glib::clone!(
                #[weak]
                obj,
                async move {
                    while let Ok(frame) = FRAME_CHANNEL.rx.recv_async().await {
                        let mut builder = gdk::DmabufTextureBuilder::new()
                            .set_display(&gdk::Display::default().unwrap())
                            .set_width(frame.width)
                            .set_height(frame.height)
                            .set_fourcc(frame.format)
                            .set_modifier(frame.modifier)
                            .set_n_planes(frame.planes.len() as u32);

                        for (i, plane) in frame.planes.iter().enumerate() {
                            builder = unsafe { builder.set_fd(i as u32, plane.fd.as_raw_fd()) }
                                .set_offset(i as u32, plane.offset)
                                .set_stride(i as u32, plane.stride);
                        }

                        match unsafe { builder.build_with_release_func(move || drop(frame)) } {
                            Ok(texture) => {
                                obj.imp().texture.replace(Some(texture));

                                obj.invalidate_contents();
                            }
                            Err(e) => {
                                tracing::error!("dmabuf build failed: {e}");
                            }
                        }
                    }
                }
            ));
        }

        fn dispose(&self) {
            self.texture.take();
            self.mpv.shutdown();
        }

        fn signals() -> &'static [Signal] {
            static SIGNALS: OnceLock<Vec<Signal>> = OnceLock::new();
            SIGNALS.get_or_init(|| {
                vec![
                    Signal::builder("mutsumi-error")
                        .param_types([glib::Type::I32])
                        .build(),
                ]
            })
        }
    }

    impl PaintableImpl for MutsumiVideoSink {
        fn intrinsic_width(&self) -> i32 {
            self.texture.borrow().as_ref().map_or(0, |t| t.width())
        }

        fn intrinsic_height(&self) -> i32 {
            self.texture.borrow().as_ref().map_or(0, |t| t.height())
        }

        fn snapshot(&self, snapshot: &gdk::Snapshot, width: f64, height: f64) {
            if let Some(texture) = self.texture.borrow().as_ref() {
                snapshot.append_texture(
                    texture,
                    &gtk::graphene::Rect::new(0.0, 0.0, width as f32, height as f32),
                );
            }
        }
    }

    impl MutsumiVideoSink {
        fn setup_mpv(&self) {
            let display = gdk::Display::default().expect("Could not connect to display");
            let formats = display.dmabuf_formats();

            // GTK < 4.24 has no memory-format mapping for 10-bit packed RGB
            // formats (AR30/XR30/AB30/XB30 = *RGB/*BGR 2101010), but the host
            // compositor may still advertise them in dmabuf_formats().
            // See https://gitlab.gnome.org/GNOME/gtk/-/issues/8148
            const PACKED_10BIT: &[u32] = &[
                0x30335241, // DRM_FORMAT_ARGB2101010 (AR30)
                0x30335258, // DRM_FORMAT_XRGB2101010 (XR30)
                0x30334241, // DRM_FORMAT_ABGR2101010 (AB30)
                0x30334258, // DRM_FORMAT_XBGR2101010 (XB30)
            ];
            let skip_packed_10bit = gtk::minor_version() < 24;

            let format_pairs: Vec<(u32, u64)> = (0..formats.n_formats())
                .map(|i| formats.format(i))
                .filter(|(fourcc, _)| !skip_packed_10bit || !PACKED_10BIT.contains(fourcc))
                .collect();

            create_mpv_proxy(format_pairs);

            self.mpv.mpv.set_property("vo", "gpu-next".to_owned());
        }

        pub fn throw_error(&self, code: MutsumiMpvError) {
            self.obj().emit_by_name::<()>("mutsumi-error", &[&code]);
        }
    }
}

glib::wrapper! {
    pub struct MutsumiVideoSink(ObjectSubclass<imp::MutsumiVideoSink>)
        @implements gdk::Paintable;
}

impl Default for MutsumiVideoSink {
    fn default() -> Self {
        Self::new()
    }
}

impl MutsumiVideoSink {
    pub fn new() -> Self {
        Object::builder().build()
    }

    pub fn mpv(&self) -> &ContextedMPV {
        &self.imp().mpv
    }

    pub fn play(&self, source: &PlayParams) {
        let url = source.url().into_owned();
        let start_time = source.start_time;

        glib::spawn_future_local(glib::clone!(
            #[weak(rename_to = obj)]
            self,
            async move {
                let mpv = obj.mpv();

                info!("Now Playing: {}", url);
                mpv.load_video(&url);

                if let Some(start_time) = start_time {
                    mpv.set_start_time(start_time);
                }

                mpv.pause(false);
            }
        ));
    }

    // This would be useful for clearing the video area when stopping playback, to avoid showing the last frame.
    pub fn push_an_empty_texture(&self) {
        let bytes = glib::Bytes::from_static(&[0, 0, 0, 0]);

        let texture = gdk::MemoryTexture::new(1, 1, gdk::MemoryFormat::R8g8b8a8, &bytes, 4);

        let texture = gdk::Texture::from(texture);

        self.imp().texture.replace(Some(texture));
        self.invalidate_contents();
    }

    pub fn set_loop_playlist(&self, loop_: &str) {
        self.mpv().set_loop_playlist(loop_);
    }

    pub fn set_loop_file(&self, loop_: &str) {
        self.mpv().set_loop_file(loop_);
    }

    pub fn playlist_shuffle(&self) {
        self.mpv().playlist_shuffle();
    }

    pub fn playlist_unshuffle(&self) {
        self.mpv().playlist_unshuffle();
    }

    pub fn set_playlist(&self, urls: &[String]) {
        self.mpv().set_playlist(urls);
    }

    pub fn set_playlist_pos(&self, pos: i64) {
        self.mpv().set_playlist_pos(pos);
    }

    pub fn playlist_add(&self, url: &str, index: i64) {
        self.mpv().playlist_add(url, index);
    }

    pub fn playlist_remove(&self, index: i64) {
        self.mpv().playlist_remove(index);
    }

    pub fn playlist_move(&self, from: i64, to: i64) {
        self.mpv().playlist_move(from, to);
    }

    pub fn press_key(&self, key: u32, state: gtk::gdk::ModifierType) {
        self.mpv().press_key(key, state)
    }

    pub fn release_key(&self, key: u32, state: gtk::gdk::ModifierType) {
        self.mpv().release_key(key, state)
    }

    pub fn volume_scroll(&self, value: i64) {
        self.mpv().volume_scroll(value)
    }

    pub fn set_slang(&self, value: String) {
        self.mpv().set_slang(value)
    }

    pub fn shutdown(&self) {
        self.mpv().shutdown();
    }

    pub fn stop(&self) {
        self.mpv().stop();
    }

    pub fn load_video(&self, url: &str) {
        self.mpv().load_video(url);
    }

    pub fn pause(&self, pause: bool) {
        self.mpv().pause(pause);
    }

    pub fn command_pause(&self) {
        self.mpv().command_pause();
    }

    pub fn set_percent_position(&self, value: f64) {
        self.mpv().set_percent_position(value);
    }

    pub fn set_start_time(&self, second: u64) {
        self.mpv().set_start_time(second);
    }

    pub fn set_start(&self, second: f64) {
        self.mpv().set_start(second);
    }

    pub fn set_aid(&self, value: TrackSelection) {
        self.mpv().set_aid(value);
    }

    pub fn set_sid(&self, value: TrackSelection) {
        self.mpv().set_sid(value);
    }

    pub fn disable_aid(&self) {
        self.mpv().set_aid(TrackSelection::None);
    }

    pub fn disable_sid(&self) {
        self.mpv().set_sid(TrackSelection::None);
    }

    pub fn set_brightness(&self, value: f64) {
        self.mpv().mpv.set_property("brightness", value);
    }

    pub fn set_contrast(&self, value: f64) {
        self.mpv().mpv.set_property("contrast", value);
    }

    pub fn set_gamma(&self, value: f64) {
        self.mpv().mpv.set_property("gamma", value);
    }

    pub fn set_hue(&self, value: f64) {
        self.mpv().mpv.set_property("hue", value);
    }

    pub fn set_saturation(&self, value: f64) {
        self.mpv().mpv.set_property("saturation", value);
    }

    pub fn set_sub_pos(&self, value: f64) {
        self.mpv().mpv.set_property("sub-pos", value);
    }

    pub fn set_sub_font_size(&self, value: f64) {
        self.mpv().mpv.set_property("sub-font-size", value);
    }

    pub fn set_sub_scale(&self, value: f64) {
        self.mpv().mpv.set_property("sub-scale", value);
    }

    pub fn set_sub_speed(&self, value: f64) {
        self.mpv().mpv.set_property("sub-speed", value);
    }

    pub fn set_sub_delay(&self, value: f64) {
        self.mpv().mpv.set_property("sub-delay", value);
    }

    pub fn set_sub_justify(&self, value: &str) {
        self.mpv().mpv.set_property("sub-justify", value.to_owned());
    }

    pub fn set_sub_bold(&self, value: bool) {
        self.mpv().mpv.set_property("sub-bold", value);
    }

    pub fn set_sub_italic(&self, value: bool) {
        self.mpv().mpv.set_property("sub-italic", value);
    }

    pub fn set_sub_font(&self, value: &str) {
        self.mpv().mpv.set_property("sub-font", value.to_owned());
    }

    pub fn set_sub_color(&self, value: &str) {
        self.mpv().mpv.set_property("sub-color", value.to_owned());
    }

    pub fn set_sub_border_color(&self, value: &str) {
        self.mpv()
            .mpv
            .set_property("sub-border-color", value.to_owned());
    }

    pub fn set_sub_back_color(&self, value: &str) {
        self.mpv()
            .mpv
            .set_property("sub-back-color", value.to_owned());
    }

    pub fn set_sub_border_style(&self, value: &str) {
        self.mpv()
            .mpv
            .set_property("sub-border-style", value.to_owned());
    }

    pub fn set_sub_border_size(&self, value: f64) {
        self.mpv().mpv.set_property("sub-border-size", value);
    }

    pub fn set_sub_shadow_offset(&self, value: f64) {
        self.mpv().mpv.set_property("sub-shadow-offset", value);
    }

    pub fn set_audio_delay(&self, value: f64) {
        self.mpv().mpv.set_property("audio-delay", value);
    }

    pub fn set_audio_channels(&self, value: &str) {
        self.mpv()
            .mpv
            .set_property("audio-channels", value.to_owned());
    }

    pub fn set_audio_pan(&self, value: &str) {
        self.mpv().mpv.set_property("af", value.to_owned());
    }

    pub fn clear_audio_pan(&self) {
        self.mpv().mpv.set_property("af", String::new());
    }

    pub fn set_scale(&self, value: &str) {
        self.mpv().mpv.set_property("scale", value.to_owned());
    }

    pub fn set_deband(&self, value: bool) {
        self.mpv().mpv.set_property("deband", value);
    }

    pub fn set_deband_iterations(&self, value: i64) {
        self.mpv().mpv.set_property("deband-iterations", value);
    }

    pub fn set_deband_threshold(&self, value: i64) {
        self.mpv().mpv.set_property("deband-threshold", value);
    }

    pub fn set_deband_range(&self, value: i64) {
        self.mpv().mpv.set_property("deband-range", value);
    }

    pub fn set_deband_grain(&self, value: i64) {
        self.mpv().mpv.set_property("deband-grain", value);
    }

    pub fn set_deinterlace(&self, value: bool) {
        self.mpv().mpv.set_property("deinterlace", value);
    }

    pub fn set_hwdec(&self, value: &str) {
        self.mpv().mpv.set_property("hwdec", value.to_owned());
    }

    pub fn set_panscan(&self, value: f64) {
        self.mpv().mpv.set_property("panscan", value);
    }

    pub fn set_stretch_image_subs_to_screen(&self, value: bool) {
        self.mpv()
            .mpv
            .set_property("stretch-image-subs-to-screen", value);
    }

    pub fn set_demuxer_max_bytes(&self, value: &str) {
        self.mpv()
            .mpv
            .set_property("demuxer-max-bytes", value.to_owned());
    }

    pub fn set_cache_secs(&self, value: f64) {
        self.mpv().mpv.set_property("cache-secs", value);
    }

    pub fn set_vo(&self, value: &str) {
        self.mpv().mpv.set_property("vo", value.to_owned());
    }

    pub fn set_property<V>(&self, property: &str, value: V)
    where
        V: Into<crate::video::MpvValue>,
    {
        self.mpv().mpv.set_property(property, value);
    }

    pub fn display_stats_toggle(&self) {
        self.mpv().display_stats_toggle();
    }

    pub fn add_sub(&self, url: &str) {
        self.mpv().add_sub(url);
    }

    pub fn set_position(&self, position: f64) {
        self.mpv().set_position(position);
    }

    pub fn set_volume(&self, volume: i64) {
        self.mpv().set_volume(volume);
    }

    pub fn seek_forward(&self, seconds: i64) {
        self.mpv().seek_forward(seconds);
    }

    pub fn seek_backward(&self, seconds: i64) {
        self.mpv().seek_backward(seconds);
    }

    pub fn set_speed(&self, speed: f64) {
        self.mpv().set_speed(speed);
    }

    pub fn set_keep_aspect_ratio(&self, value: bool) {
        self.mpv().mpv.set_property("keepaspect", value);
    }

    pub async fn position(&self) -> f64 {
        self.mpv().position().await
    }

    pub async fn paused(&self) -> bool {
        self.mpv().paused().await
    }

    pub async fn duration(&self) -> f64 {
        self.mpv().duration().await
    }

    pub async fn get_track_id(&self, kind: TrackKind) -> i64 {
        let type_ = match kind {
            TrackKind::Video => "vid",
            TrackKind::Audio => "aid",
            TrackKind::Subtitle => "sid",
        };

        self.mpv().get_track_id(type_).await
    }
}