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
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::MediaStream;
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::{boxed::Box as Box_, fmt, mem::transmute};

glib::wrapper! {
    /// [`MediaFile`][crate::MediaFile] implements [`MediaStream`][crate::MediaStream] for files.
    ///
    /// This provides a simple way to play back video files with GTK.
    ///
    /// GTK provides a GIO extension point for [`MediaFile`][crate::MediaFile] implementations
    /// to allow for external implementations using various media frameworks.
    ///
    /// GTK itself includes implementations using GStreamer and ffmpeg.
    ///
    /// This is an Abstract Base Class, you cannot instantiate it.
    ///
    /// ## Properties
    ///
    ///
    /// #### `file`
    ///  The file being played back or [`None`] if not playing a file.
    ///
    /// Readable | Writeable
    ///
    ///
    /// #### `input-stream`
    ///  The stream being played back or [`None`] if not playing a stream.
    ///
    /// This is [`None`] when playing a file.
    ///
    /// Readable | Writeable
    /// <details><summary><h4>MediaStream</h4></summary>
    ///
    ///
    /// #### `duration`
    ///  The stream's duration in microseconds or 0 if unknown.
    ///
    /// Readable
    ///
    ///
    /// #### `ended`
    ///  Set when playback has finished.
    ///
    /// Readable
    ///
    ///
    /// #### `error`
    ///  [`None`] for a properly working stream or the `GError`
    /// that the stream is in.
    ///
    /// Readable
    ///
    ///
    /// #### `has-audio`
    ///  Whether the stream contains audio.
    ///
    /// Readable
    ///
    ///
    /// #### `has-video`
    ///  Whether the stream contains video.
    ///
    /// Readable
    ///
    ///
    /// #### `loop`
    ///  Try to restart the media from the beginning once it ended.
    ///
    /// Readable | Writeable
    ///
    ///
    /// #### `muted`
    ///  Whether the audio stream should be muted.
    ///
    /// Readable | Writeable
    ///
    ///
    /// #### `playing`
    ///  Whether the stream is currently playing.
    ///
    /// Readable | Writeable
    ///
    ///
    /// #### `prepared`
    ///  Whether the stream has finished initializing and existence of
    /// audio and video is known.
    ///
    /// Readable | Writeable
    ///
    ///
    /// #### `seekable`
    ///  Set unless the stream is known to not support seeking.
    ///
    /// Readable
    ///
    ///
    /// #### `seeking`
    ///  Set while a seek is in progress.
    ///
    /// Readable
    ///
    ///
    /// #### `timestamp`
    ///  The current presentation timestamp in microseconds.
    ///
    /// Readable
    ///
    ///
    /// #### `volume`
    ///  Volume of the audio stream.
    ///
    /// Readable | Writeable
    /// </details>
    ///
    /// # Implements
    ///
    /// [`MediaFileExt`][trait@crate::prelude::MediaFileExt], [`MediaStreamExt`][trait@crate::prelude::MediaStreamExt], [`trait@glib::ObjectExt`], [`trait@gdk::prelude::PaintableExt`], [`MediaStreamExtManual`][trait@crate::prelude::MediaStreamExtManual]
    #[doc(alias = "GtkMediaFile")]
    pub struct MediaFile(Object<ffi::GtkMediaFile, ffi::GtkMediaFileClass>) @extends MediaStream, @implements gdk::Paintable;

    match fn {
        type_ => || ffi::gtk_media_file_get_type(),
    }
}

impl MediaFile {
    pub const NONE: Option<&'static MediaFile> = None;

    /// Creates a new empty media file.
    ///
    /// # Returns
    ///
    /// a new [`MediaFile`][crate::MediaFile]
    #[doc(alias = "gtk_media_file_new")]
    pub fn new() -> MediaFile {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ffi::gtk_media_file_new()) }
    }

    /// Creates a new media file to play @file.
    /// ## `file`
    /// The file to play
    ///
    /// # Returns
    ///
    /// a new [`MediaFile`][crate::MediaFile] playing @file
    #[doc(alias = "gtk_media_file_new_for_file")]
    #[doc(alias = "new_for_file")]
    pub fn for_file(file: &impl IsA<gio::File>) -> MediaFile {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(ffi::gtk_media_file_new_for_file(
                file.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Creates a new media file for the given filename.
    ///
    /// This is a utility function that converts the given @filename
    /// to a `GFile` and calls [`for_file()`][Self::for_file()].
    /// ## `filename`
    /// filename to open
    ///
    /// # Returns
    ///
    /// a new [`MediaFile`][crate::MediaFile] playing @filename
    #[doc(alias = "gtk_media_file_new_for_filename")]
    #[doc(alias = "new_for_filename")]
    pub fn for_filename(filename: impl AsRef<std::path::Path>) -> MediaFile {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(ffi::gtk_media_file_new_for_filename(
                filename.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Creates a new media file to play @stream.
    ///
    /// If you want the resulting media to be seekable,
    /// the stream should implement the `GSeekable` interface.
    /// ## `stream`
    /// The stream to play
    ///
    /// # Returns
    ///
    /// a new [`MediaFile`][crate::MediaFile]
    #[doc(alias = "gtk_media_file_new_for_input_stream")]
    #[doc(alias = "new_for_input_stream")]
    pub fn for_input_stream(stream: &impl IsA<gio::InputStream>) -> MediaFile {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(ffi::gtk_media_file_new_for_input_stream(
                stream.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Creates a new new media file for the given resource.
    ///
    /// This is a utility function that converts the given @resource
    /// to a `GFile` and calls [`for_file()`][Self::for_file()].
    /// ## `resource_path`
    /// resource path to open
    ///
    /// # Returns
    ///
    /// a new [`MediaFile`][crate::MediaFile] playing @resource_path
    #[doc(alias = "gtk_media_file_new_for_resource")]
    #[doc(alias = "new_for_resource")]
    pub fn for_resource(resource_path: &str) -> MediaFile {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(ffi::gtk_media_file_new_for_resource(
                resource_path.to_glib_none().0,
            ))
        }
    }
}

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

/// Trait containing all [`struct@MediaFile`] methods.
///
/// # Implementors
///
/// [`MediaFile`][struct@crate::MediaFile]
pub trait MediaFileExt: 'static {
    /// Resets the media file to be empty.
    #[doc(alias = "gtk_media_file_clear")]
    fn clear(&self);

    /// Returns the file that @self is currently playing from.
    ///
    /// When @self is not playing or not playing from a file,
    /// [`None`] is returned.
    ///
    /// # Returns
    ///
    /// The currently playing file
    #[doc(alias = "gtk_media_file_get_file")]
    #[doc(alias = "get_file")]
    fn file(&self) -> Option<gio::File>;

    /// Returns the stream that @self is currently playing from.
    ///
    /// When @self is not playing or not playing from a stream,
    /// [`None`] is returned.
    ///
    /// # Returns
    ///
    /// The currently playing stream
    #[doc(alias = "gtk_media_file_get_input_stream")]
    #[doc(alias = "get_input_stream")]
    fn input_stream(&self) -> Option<gio::InputStream>;

    /// Sets the [`MediaFile`][crate::MediaFile] to play the given file.
    ///
    /// If any file is still playing, stop playing it.
    /// ## `file`
    /// the file to play
    #[doc(alias = "gtk_media_file_set_file")]
    fn set_file(&self, file: Option<&impl IsA<gio::File>>);

    /// Sets the `GtkMediaFile to play the given file.
    ///
    /// This is a utility function that converts the given @filename
    /// to a `GFile` and calls [`set_file()`][Self::set_file()].
    /// ## `filename`
    /// name of file to play
    #[doc(alias = "gtk_media_file_set_filename")]
    fn set_filename(&self, filename: Option<impl AsRef<std::path::Path>>);

    /// Sets the [`MediaFile`][crate::MediaFile] to play the given stream.
    ///
    /// If anything is still playing, stop playing it.
    ///
    /// Full control about the @stream is assumed for the duration of
    /// playback. The stream will not be closed.
    /// ## `stream`
    /// the stream to play from
    #[doc(alias = "gtk_media_file_set_input_stream")]
    fn set_input_stream(&self, stream: Option<&impl IsA<gio::InputStream>>);

    /// Sets the `GtkMediaFile to play the given resource.
    ///
    /// This is a utility function that converts the given @resource_path
    /// to a `GFile` and calls [`set_file()`][Self::set_file()].
    /// ## `resource_path`
    /// path to resource to play
    #[doc(alias = "gtk_media_file_set_resource")]
    fn set_resource(&self, resource_path: Option<&str>);

    #[doc(alias = "file")]
    fn connect_file_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    #[doc(alias = "input-stream")]
    fn connect_input_stream_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<MediaFile>> MediaFileExt for O {
    fn clear(&self) {
        unsafe {
            ffi::gtk_media_file_clear(self.as_ref().to_glib_none().0);
        }
    }

    fn file(&self) -> Option<gio::File> {
        unsafe { from_glib_none(ffi::gtk_media_file_get_file(self.as_ref().to_glib_none().0)) }
    }

    fn input_stream(&self) -> Option<gio::InputStream> {
        unsafe {
            from_glib_none(ffi::gtk_media_file_get_input_stream(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn set_file(&self, file: Option<&impl IsA<gio::File>>) {
        unsafe {
            ffi::gtk_media_file_set_file(
                self.as_ref().to_glib_none().0,
                file.map(|p| p.as_ref()).to_glib_none().0,
            );
        }
    }

    fn set_filename(&self, filename: Option<impl AsRef<std::path::Path>>) {
        unsafe {
            ffi::gtk_media_file_set_filename(
                self.as_ref().to_glib_none().0,
                filename.as_ref().map(|p| p.as_ref()).to_glib_none().0,
            );
        }
    }

    fn set_input_stream(&self, stream: Option<&impl IsA<gio::InputStream>>) {
        unsafe {
            ffi::gtk_media_file_set_input_stream(
                self.as_ref().to_glib_none().0,
                stream.map(|p| p.as_ref()).to_glib_none().0,
            );
        }
    }

    fn set_resource(&self, resource_path: Option<&str>) {
        unsafe {
            ffi::gtk_media_file_set_resource(
                self.as_ref().to_glib_none().0,
                resource_path.to_glib_none().0,
            );
        }
    }

    fn connect_file_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_file_trampoline<P: IsA<MediaFile>, F: Fn(&P) + 'static>(
            this: *mut ffi::GtkMediaFile,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(MediaFile::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::file\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_file_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_input_stream_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_input_stream_trampoline<
            P: IsA<MediaFile>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::GtkMediaFile,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(MediaFile::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::input-stream\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_input_stream_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for MediaFile {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("MediaFile")
    }
}