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
// Take a look at the license at the top of the repository in the LICENSE file.

// rustdoc-stripper-ignore-next
//! Traits intended for subclassing [`MediaStream`](crate::MediaStream).

use glib::translate::*;

use crate::{prelude::*, subclass::prelude::*, MediaStream};

pub trait MediaStreamImpl: MediaStreamImplExt + ObjectImpl {
    /// Pauses playback of the stream.
    ///
    /// If the stream is not playing, do nothing.
    fn pause(&self) {
        self.parent_pause()
    }

    fn play(&self) -> bool {
        self.parent_play()
    }

    /// Called by users to attach the media stream to a [`gdk::Surface`][crate::gdk::Surface] they manage.
    ///
    /// The stream can then access the resources of @surface for its
    /// rendering purposes. In particular, media streams might want to
    /// create a [`gdk::GLContext`][crate::gdk::GLContext] or sync to the [`gdk::FrameClock`][crate::gdk::FrameClock].
    ///
    /// Whoever calls this function is responsible for calling
    /// [`MediaStreamExt::unrealize()`][crate::prelude::MediaStreamExt::unrealize()] before either the stream
    /// or @surface get destroyed.
    ///
    /// Multiple calls to this function may happen from different
    /// users of the video, even with the same @surface. Each of these
    /// calls must be followed by its own call to
    /// [`MediaStreamExt::unrealize()`][crate::prelude::MediaStreamExt::unrealize()].
    ///
    /// It is not required to call this function to make a media stream work.
    /// ## `surface`
    /// a [`gdk::Surface`][crate::gdk::Surface]
    fn realize(&self, surface: gdk::Surface) {
        self.parent_realize(surface)
    }

    /// Start a seek operation on @self to @timestamp.
    ///
    /// If @timestamp is out of range, it will be clamped.
    ///
    /// Seek operations may not finish instantly. While a
    /// seek operation is in process, the [`seeking`][struct@crate::MediaStream#seeking]
    /// property will be set.
    ///
    /// When calling gtk_media_stream_seek() during an
    /// ongoing seek operation, the new seek will override
    /// any pending seek.
    /// ## `timestamp`
    /// timestamp to seek to.
    fn seek(&self, timestamp: i64) {
        self.parent_seek(timestamp)
    }

    /// Undoes a previous call to gtk_media_stream_realize().
    ///
    /// This causes the stream to release all resources it had
    /// allocated from @surface.
    /// ## `surface`
    /// the [`gdk::Surface`][crate::gdk::Surface] the stream was realized with
    fn unrealize(&self, surface: gdk::Surface) {
        self.parent_unrealize(surface)
    }

    fn update_audio(&self, muted: bool, volume: f64) {
        self.parent_update_audio(muted, volume)
    }
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::MediaStreamImplExt> Sealed for T {}
}

pub trait MediaStreamImplExt: sealed::Sealed + ObjectSubclass {
    fn parent_pause(&self) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkMediaStreamClass;
            let f = (*parent_class)
                .pause
                .expect("No parent class impl for \"pause\"");
            f(self.obj().unsafe_cast_ref::<MediaStream>().to_glib_none().0)
        }
    }

    // Returns true if successfully started playing
    fn parent_play(&self) -> bool {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkMediaStreamClass;
            if let Some(f) = (*parent_class).play {
                return from_glib(f(self
                    .obj()
                    .unsafe_cast_ref::<MediaStream>()
                    .to_glib_none()
                    .0));
            }
            false
        }
    }

    fn parent_realize(&self, surface: gdk::Surface) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkMediaStreamClass;
            let f = (*parent_class)
                .realize
                .expect("No parent class impl for \"realize\"");
            f(
                self.obj().unsafe_cast_ref::<MediaStream>().to_glib_none().0,
                surface.to_glib_none().0,
            )
        }
    }

    fn parent_seek(&self, timestamp: i64) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkMediaStreamClass;
            let f = (*parent_class)
                .seek
                .expect("No parent class impl for \"realize\"");
            f(
                self.obj().unsafe_cast_ref::<MediaStream>().to_glib_none().0,
                timestamp,
            )
        }
    }

    fn parent_unrealize(&self, surface: gdk::Surface) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkMediaStreamClass;
            let f = (*parent_class)
                .unrealize
                .expect("No parent class impl for \"unrealize\"");
            f(
                self.obj().unsafe_cast_ref::<MediaStream>().to_glib_none().0,
                surface.to_glib_none().0,
            )
        }
    }

    fn parent_update_audio(&self, muted: bool, volume: f64) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GtkMediaStreamClass;
            let f = (*parent_class)
                .update_audio
                .expect("No parent class impl for \"update_audio\"");
            f(
                self.obj().unsafe_cast_ref::<MediaStream>().to_glib_none().0,
                muted.into_glib(),
                volume,
            )
        }
    }
}

impl<T: MediaStreamImpl> MediaStreamImplExt for T {}

unsafe impl<T: MediaStreamImpl> IsSubclassable<T> for MediaStream {
    fn class_init(class: &mut glib::Class<Self>) {
        Self::parent_class_init::<T>(class);

        assert_initialized_main_thread!();

        let klass = class.as_mut();
        klass.pause = Some(media_stream_pause::<T>);
        klass.play = Some(media_stream_play::<T>);
        klass.realize = Some(media_stream_realize::<T>);
        klass.seek = Some(media_stream_seek::<T>);
        klass.unrealize = Some(media_stream_unrealize::<T>);
        klass.update_audio = Some(media_stream_update_audio::<T>);
    }
}

unsafe extern "C" fn media_stream_pause<T: MediaStreamImpl>(ptr: *mut ffi::GtkMediaStream) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.pause()
}

unsafe extern "C" fn media_stream_play<T: MediaStreamImpl>(
    ptr: *mut ffi::GtkMediaStream,
) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.play().into_glib()
}

unsafe extern "C" fn media_stream_realize<T: MediaStreamImpl>(
    ptr: *mut ffi::GtkMediaStream,
    surface: *mut gdk::ffi::GdkSurface,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.realize(from_glib_none(surface))
}

unsafe extern "C" fn media_stream_seek<T: MediaStreamImpl>(
    ptr: *mut ffi::GtkMediaStream,
    timestamp: i64,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.seek(timestamp)
}

unsafe extern "C" fn media_stream_unrealize<T: MediaStreamImpl>(
    ptr: *mut ffi::GtkMediaStream,
    surface: *mut gdk::ffi::GdkSurface,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.unrealize(from_glib_none(surface))
}

unsafe extern "C" fn media_stream_update_audio<T: MediaStreamImpl>(
    ptr: *mut ffi::GtkMediaStream,
    muted: glib::ffi::gboolean,
    volume: f64,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.update_audio(from_glib(muted), volume)
}