gtk4/auto/
media_file.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ffi, MediaStream};
6use glib::{
7    prelude::*,
8    signal::{connect_raw, SignalHandlerId},
9    translate::*,
10};
11use std::boxed::Box as Box_;
12
13glib::wrapper! {
14    /// Implements the [`MediaStream`][crate::MediaStream] interface for files.
15    ///
16    /// This provides a simple way to play back video files with GTK.
17    ///
18    /// GTK provides a GIO extension point for [`MediaFile`][crate::MediaFile] implementations
19    /// to allow for external implementations using various media frameworks.
20    ///
21    /// GTK itself includes an implementation using GStreamer.
22    ///
23    /// This is an Abstract Base Class, you cannot instantiate it.
24    ///
25    /// ## Properties
26    ///
27    ///
28    /// #### `file`
29    ///  The file being played back or [`None`] if not playing a file.
30    ///
31    /// Readable | Writeable
32    ///
33    ///
34    /// #### `input-stream`
35    ///  The stream being played back or [`None`] if not playing a stream.
36    ///
37    /// This is [`None`] when playing a file.
38    ///
39    /// Readable | Writeable
40    /// <details><summary><h4>MediaStream</h4></summary>
41    ///
42    ///
43    /// #### `duration`
44    ///  The stream's duration in microseconds or 0 if unknown.
45    ///
46    /// Readable
47    ///
48    ///
49    /// #### `ended`
50    ///  Set when playback has finished.
51    ///
52    /// Readable
53    ///
54    ///
55    /// #### `error`
56    ///  [`None`] for a properly working stream or the `GError`
57    /// that the stream is in.
58    ///
59    /// Readable
60    ///
61    ///
62    /// #### `has-audio`
63    ///  Whether the stream contains audio.
64    ///
65    /// Readable
66    ///
67    ///
68    /// #### `has-video`
69    ///  Whether the stream contains video.
70    ///
71    /// Readable
72    ///
73    ///
74    /// #### `loop`
75    ///  Try to restart the media from the beginning once it ended.
76    ///
77    /// Readable | Writeable
78    ///
79    ///
80    /// #### `muted`
81    ///  Whether the audio stream should be muted.
82    ///
83    /// Readable | Writeable
84    ///
85    ///
86    /// #### `playing`
87    ///  Whether the stream is currently playing.
88    ///
89    /// Readable | Writeable
90    ///
91    ///
92    /// #### `prepared`
93    ///  Whether the stream has finished initializing and existence of
94    /// audio and video is known.
95    ///
96    /// Readable | Writeable
97    ///
98    ///
99    /// #### `seekable`
100    ///  Set unless the stream is known to not support seeking.
101    ///
102    /// Readable
103    ///
104    ///
105    /// #### `seeking`
106    ///  Set while a seek is in progress.
107    ///
108    /// Readable
109    ///
110    ///
111    /// #### `timestamp`
112    ///  The current presentation timestamp in microseconds.
113    ///
114    /// Readable
115    ///
116    ///
117    /// #### `volume`
118    ///  Volume of the audio stream.
119    ///
120    /// Readable | Writeable
121    /// </details>
122    ///
123    /// # Implements
124    ///
125    /// [`MediaFileExt`][trait@crate::prelude::MediaFileExt], [`MediaStreamExt`][trait@crate::prelude::MediaStreamExt], [`trait@glib::ObjectExt`], [`trait@gdk::prelude::PaintableExt`], [`MediaStreamExtManual`][trait@crate::prelude::MediaStreamExtManual]
126    #[doc(alias = "GtkMediaFile")]
127    pub struct MediaFile(Object<ffi::GtkMediaFile, ffi::GtkMediaFileClass>) @extends MediaStream, @implements gdk::Paintable;
128
129    match fn {
130        type_ => || ffi::gtk_media_file_get_type(),
131    }
132}
133
134impl MediaFile {
135    pub const NONE: Option<&'static MediaFile> = None;
136
137    /// Creates a new empty media file.
138    ///
139    /// # Returns
140    ///
141    /// a new [`MediaFile`][crate::MediaFile]
142    #[doc(alias = "gtk_media_file_new")]
143    pub fn new() -> MediaFile {
144        assert_initialized_main_thread!();
145        unsafe { from_glib_full(ffi::gtk_media_file_new()) }
146    }
147
148    /// Creates a new media file to play @file.
149    /// ## `file`
150    /// The file to play
151    ///
152    /// # Returns
153    ///
154    /// a new [`MediaFile`][crate::MediaFile] playing @file
155    #[doc(alias = "gtk_media_file_new_for_file")]
156    #[doc(alias = "new_for_file")]
157    pub fn for_file(file: &impl IsA<gio::File>) -> MediaFile {
158        assert_initialized_main_thread!();
159        unsafe {
160            from_glib_full(ffi::gtk_media_file_new_for_file(
161                file.as_ref().to_glib_none().0,
162            ))
163        }
164    }
165
166    /// Creates a new media file for the given filename.
167    ///
168    /// This is a utility function that converts the given @filename
169    /// to a `GFile` and calls [`for_file()`][Self::for_file()].
170    /// ## `filename`
171    /// filename to open
172    ///
173    /// # Returns
174    ///
175    /// a new [`MediaFile`][crate::MediaFile] playing @filename
176    #[doc(alias = "gtk_media_file_new_for_filename")]
177    #[doc(alias = "new_for_filename")]
178    pub fn for_filename(filename: impl AsRef<std::path::Path>) -> MediaFile {
179        assert_initialized_main_thread!();
180        unsafe {
181            from_glib_full(ffi::gtk_media_file_new_for_filename(
182                filename.as_ref().to_glib_none().0,
183            ))
184        }
185    }
186
187    /// Creates a new media file to play @stream.
188    ///
189    /// If you want the resulting media to be seekable,
190    /// the stream should implement the `GSeekable` interface.
191    /// ## `stream`
192    /// The stream to play
193    ///
194    /// # Returns
195    ///
196    /// a new [`MediaFile`][crate::MediaFile]
197    #[doc(alias = "gtk_media_file_new_for_input_stream")]
198    #[doc(alias = "new_for_input_stream")]
199    pub fn for_input_stream(stream: &impl IsA<gio::InputStream>) -> MediaFile {
200        assert_initialized_main_thread!();
201        unsafe {
202            from_glib_full(ffi::gtk_media_file_new_for_input_stream(
203                stream.as_ref().to_glib_none().0,
204            ))
205        }
206    }
207
208    /// Creates a new new media file for the given resource.
209    ///
210    /// This is a utility function that converts the given @resource
211    /// to a `GFile` and calls [`for_file()`][Self::for_file()].
212    /// ## `resource_path`
213    /// resource path to open
214    ///
215    /// # Returns
216    ///
217    /// a new [`MediaFile`][crate::MediaFile] playing @resource_path
218    #[doc(alias = "gtk_media_file_new_for_resource")]
219    #[doc(alias = "new_for_resource")]
220    pub fn for_resource(resource_path: &str) -> MediaFile {
221        assert_initialized_main_thread!();
222        unsafe {
223            from_glib_full(ffi::gtk_media_file_new_for_resource(
224                resource_path.to_glib_none().0,
225            ))
226        }
227    }
228}
229
230impl Default for MediaFile {
231    fn default() -> Self {
232        Self::new()
233    }
234}
235
236/// Trait containing all [`struct@MediaFile`] methods.
237///
238/// # Implementors
239///
240/// [`MediaFile`][struct@crate::MediaFile]
241pub trait MediaFileExt: IsA<MediaFile> + 'static {
242    /// Resets the media file to be empty.
243    #[doc(alias = "gtk_media_file_clear")]
244    fn clear(&self) {
245        unsafe {
246            ffi::gtk_media_file_clear(self.as_ref().to_glib_none().0);
247        }
248    }
249
250    /// Returns the file that @self is currently playing from.
251    ///
252    /// When @self is not playing or not playing from a file,
253    /// [`None`] is returned.
254    ///
255    /// # Returns
256    ///
257    /// The currently playing file
258    #[doc(alias = "gtk_media_file_get_file")]
259    #[doc(alias = "get_file")]
260    fn file(&self) -> Option<gio::File> {
261        unsafe { from_glib_none(ffi::gtk_media_file_get_file(self.as_ref().to_glib_none().0)) }
262    }
263
264    /// Returns the stream that @self is currently playing from.
265    ///
266    /// When @self is not playing or not playing from a stream,
267    /// [`None`] is returned.
268    ///
269    /// # Returns
270    ///
271    /// The currently playing stream
272    #[doc(alias = "gtk_media_file_get_input_stream")]
273    #[doc(alias = "get_input_stream")]
274    #[doc(alias = "input-stream")]
275    fn input_stream(&self) -> Option<gio::InputStream> {
276        unsafe {
277            from_glib_none(ffi::gtk_media_file_get_input_stream(
278                self.as_ref().to_glib_none().0,
279            ))
280        }
281    }
282
283    /// Sets the [`MediaFile`][crate::MediaFile] to play the given file.
284    ///
285    /// If any file is still playing, stop playing it.
286    /// ## `file`
287    /// the file to play
288    #[doc(alias = "gtk_media_file_set_file")]
289    #[doc(alias = "file")]
290    fn set_file(&self, file: Option<&impl IsA<gio::File>>) {
291        unsafe {
292            ffi::gtk_media_file_set_file(
293                self.as_ref().to_glib_none().0,
294                file.map(|p| p.as_ref()).to_glib_none().0,
295            );
296        }
297    }
298
299    /// Sets the [`MediaFile`][crate::MediaFile] to play the given file.
300    ///
301    /// This is a utility function that converts the given @filename
302    /// to a `GFile` and calls [`set_file()`][Self::set_file()].
303    /// ## `filename`
304    /// name of file to play
305    #[doc(alias = "gtk_media_file_set_filename")]
306    fn set_filename(&self, filename: Option<impl AsRef<std::path::Path>>) {
307        unsafe {
308            ffi::gtk_media_file_set_filename(
309                self.as_ref().to_glib_none().0,
310                filename.as_ref().map(|p| p.as_ref()).to_glib_none().0,
311            );
312        }
313    }
314
315    /// Sets the [`MediaFile`][crate::MediaFile] to play the given stream.
316    ///
317    /// If anything is still playing, stop playing it.
318    ///
319    /// Full control about the @stream is assumed for the duration of
320    /// playback. The stream will not be closed.
321    /// ## `stream`
322    /// the stream to play from
323    #[doc(alias = "gtk_media_file_set_input_stream")]
324    #[doc(alias = "input-stream")]
325    fn set_input_stream(&self, stream: Option<&impl IsA<gio::InputStream>>) {
326        unsafe {
327            ffi::gtk_media_file_set_input_stream(
328                self.as_ref().to_glib_none().0,
329                stream.map(|p| p.as_ref()).to_glib_none().0,
330            );
331        }
332    }
333
334    /// Sets the [`MediaFile`][crate::MediaFile] to play the given resource.
335    ///
336    /// This is a utility function that converts the given @resource_path
337    /// to a `GFile` and calls [`set_file()`][Self::set_file()].
338    /// ## `resource_path`
339    /// path to resource to play
340    #[doc(alias = "gtk_media_file_set_resource")]
341    fn set_resource(&self, resource_path: Option<&str>) {
342        unsafe {
343            ffi::gtk_media_file_set_resource(
344                self.as_ref().to_glib_none().0,
345                resource_path.to_glib_none().0,
346            );
347        }
348    }
349
350    #[doc(alias = "file")]
351    fn connect_file_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
352        unsafe extern "C" fn notify_file_trampoline<P: IsA<MediaFile>, F: Fn(&P) + 'static>(
353            this: *mut ffi::GtkMediaFile,
354            _param_spec: glib::ffi::gpointer,
355            f: glib::ffi::gpointer,
356        ) {
357            let f: &F = &*(f as *const F);
358            f(MediaFile::from_glib_borrow(this).unsafe_cast_ref())
359        }
360        unsafe {
361            let f: Box_<F> = Box_::new(f);
362            connect_raw(
363                self.as_ptr() as *mut _,
364                c"notify::file".as_ptr() as *const _,
365                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
366                    notify_file_trampoline::<Self, F> as *const (),
367                )),
368                Box_::into_raw(f),
369            )
370        }
371    }
372
373    #[doc(alias = "input-stream")]
374    fn connect_input_stream_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
375        unsafe extern "C" fn notify_input_stream_trampoline<
376            P: IsA<MediaFile>,
377            F: Fn(&P) + 'static,
378        >(
379            this: *mut ffi::GtkMediaFile,
380            _param_spec: glib::ffi::gpointer,
381            f: glib::ffi::gpointer,
382        ) {
383            let f: &F = &*(f as *const F);
384            f(MediaFile::from_glib_borrow(this).unsafe_cast_ref())
385        }
386        unsafe {
387            let f: Box_<F> = Box_::new(f);
388            connect_raw(
389                self.as_ptr() as *mut _,
390                c"notify::input-stream".as_ptr() as *const _,
391                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
392                    notify_input_stream_trampoline::<Self, F> as *const (),
393                )),
394                Box_::into_raw(f),
395            )
396        }
397    }
398}
399
400impl<O: IsA<MediaFile>> MediaFileExt for O {}