gtksink.rs 7.47 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
// This example demonstrates how to use gstreamer in conjunction with the gtk widget toolkit.
// This example shows the video produced by a videotestsrc within a small gtk gui.
// For this, the gtkglsink is used, which creates a gtk widget one can embed the gtk gui.
// For this, there multiple types of widgets. gtkglsink uses OpenGL to render frames, and
// gtksink uses the CPU to render the frames (which is way slower).
// So the example application first tries to use OpenGL, and when that fails, fall back.
// The pipeline looks like the following:

// gtk-gui:          {gtkglsink}-widget
//                      (|)
// {videotestsrc} - {glsinkbin}

13
use gst::prelude::*;
Sebastian Dröge's avatar
Sebastian Dröge committed
14

15
use gio::prelude::*;
16

Sebastian Dröge's avatar
Sebastian Dröge committed
17
18
use gtk::prelude::*;

19
use std::cell::RefCell;
20

21
fn create_ui(app: &gtk::Application) {
22
23
    let pipeline = gst::Pipeline::new(None);
    let src = gst::ElementFactory::make("videotestsrc", None).unwrap();
24
25
26
27
    // Create the gtk sink and retrieve the widget from it. The sink element will be used
    // in the pipeline, and the widget will be embedded in our gui.
    // Gstreamer then displays frames in the gtk widget.
    // First, we try to use the OpenGL version - and if that fails, we fall back to non-OpenGL.
28
    let (sink, widget) = if let Ok(gtkglsink) = gst::ElementFactory::make("gtkglsink", None) {
29
30
31
32
33
34
        // Using the OpenGL widget succeeded, so we are in for a nice playback experience with
        // low cpu usage. :)
        // The gtkglsink essentially allocates an OpenGL texture on the GPU, that it will display.
        // Now we create the glsinkbin element, which is responsible for conversions and for uploading
        // video frames to our texture (if they are not already in the GPU). Now we tell the OpenGL-sink
        // about our gtkglsink element, form where it will retrieve the OpenGL texture to fill.
35
        let glsinkbin = gst::ElementFactory::make("glsinkbin", None).unwrap();
36
        glsinkbin.set_property("sink", &gtkglsink);
37
38
        // The gtkglsink creates the gtk widget for us. This is accessible through a property.
        // So we get it and use it later to add it to our gui.
39
40
        let widget = gtkglsink.property::<gtk::Widget>("widget");
        (glsinkbin, widget)
Sebastian Dröge's avatar
Sebastian Dröge committed
41
    } else {
42
43
44
        // Unfortunately, using the OpenGL widget didn't work out, so we will have to render
        // our frames manually, using the CPU. An example why this may fail is, when
        // the PC doesn't have proper graphics drivers installed.
45
        let sink = gst::ElementFactory::make("gtksink", None).unwrap();
46
47
        // The gtksink creates the gtk widget for us. This is accessible through a property.
        // So we get it and use it later to add it to our gui.
48
49
        let widget = sink.property::<gtk::Widget>("widget");
        (sink, widget)
Sebastian Dröge's avatar
Sebastian Dröge committed
50
51
52
53
54
    };

    pipeline.add_many(&[&src, &sink]).unwrap();
    src.link(&sink).unwrap();

55
    // Create a simple gtk gui window to place our widget into.
56
    let window = gtk::Window::new(gtk::WindowType::Toplevel);
Sebastian Dröge's avatar
Sebastian Dröge committed
57
58
    window.set_default_size(320, 240);
    let vbox = gtk::Box::new(gtk::Orientation::Vertical, 0);
59
    // Add our widget to the gui
Sebastian Dröge's avatar
Sebastian Dröge committed
60
    vbox.pack_start(&widget, true, true, 0);
61
    let label = gtk::Label::new(Some("Position: 00:00:00"));
Sebastian Dröge's avatar
Sebastian Dröge committed
62
63
64
65
    vbox.pack_start(&label, true, true, 5);
    window.add(&vbox);
    window.show_all();

66
67
    app.add_window(&window);

68
69
70
71
72
73
74
75
76
    // Need to move a new reference into the closure.
    // !!ATTENTION!!:
    // It might seem appealing to use pipeline.clone() here, because that greatly
    // simplifies the code within the callback. What this actually does, however, is creating
    // a memory leak. The clone of a pipeline is a new strong reference on the pipeline.
    // Storing this strong reference of the pipeline within the callback (we are moving it in!),
    // which is in turn stored in another strong reference on the pipeline is creating a
    // reference cycle.
    // DO NOT USE pipeline.clone() TO USE THE PIPELINE WITHIN A CALLBACK
77
    let pipeline_weak = pipeline.downgrade();
78
79
80
81
82
    // Add a timeout to the main loop that will periodically (every 500ms) be
    // executed. This will query the current position within the stream from
    // the underlying pipeline, and display it in our gui.
    // Since this closure is called by the mainloop thread, we are allowed
    // to modify the gui widgets here.
83
    let timeout_id = glib::timeout_add_local(std::time::Duration::from_millis(500), move || {
84
85
        // Here we temporarily retrieve a strong reference on the pipeline from the weak one
        // we moved into this callback.
86
87
88
89
90
        let pipeline = match pipeline_weak.upgrade() {
            Some(pipeline) => pipeline,
            None => return glib::Continue(true),
        };

91
        // Query the current playing position from the underlying pipeline.
92
        let position = pipeline.query_position::<gst::ClockTime>();
93
        // Display the playing position in the gui.
François Laignel's avatar
François Laignel committed
94
        label.set_text(&format!("Position: {:.0}", position.display()));
95
        // Tell the callback to continue calling this closure.
Sebastian Dröge's avatar
Sebastian Dröge committed
96
97
98
        glib::Continue(true)
    });

99
    let bus = pipeline.bus().unwrap();
Sebastian Dröge's avatar
Sebastian Dröge committed
100

101
102
103
    pipeline
        .set_state(gst::State::Playing)
        .expect("Unable to set the pipeline to the `Playing` state");
Sebastian Dröge's avatar
Sebastian Dröge committed
104

105
106
    let app_weak = app.downgrade();
    bus.add_watch_local(move |_, msg| {
107
108
        use gst::MessageView;

109
110
111
112
113
        let app = match app_weak.upgrade() {
            Some(app) => app,
            None => return glib::Continue(false),
        };

Sebastian Dröge's avatar
Sebastian Dröge committed
114
        match msg.view() {
115
            MessageView::Eos(..) => app.quit(),
Sebastian Dröge's avatar
Sebastian Dröge committed
116
117
            MessageView::Error(err) => {
                println!(
118
                    "Error from {:?}: {} ({:?})",
119
120
121
                    err.src().map(|s| s.path_string()),
                    err.error(),
                    err.debug()
Sebastian Dröge's avatar
Sebastian Dröge committed
122
                );
123
                app.quit();
Sebastian Dröge's avatar
Sebastian Dröge committed
124
125
126
127
128
            }
            _ => (),
        };

        glib::Continue(true)
129
130
    })
    .expect("Failed to add bus watch");
Sebastian Dröge's avatar
Sebastian Dröge committed
131

132
133
134
    // Pipeline reference is owned by the closure below, so will be
    // destroyed once the app is destroyed
    let timeout_id = RefCell::new(Some(timeout_id));
135
    let pipeline = RefCell::new(Some(pipeline));
136
    app.connect_shutdown(move |_| {
137
138
139
140
141
142
143
        // Optional, by manually destroying the window here we ensure that
        // the gst element is destroyed when shutting down instead of having to wait
        // for the process to terminate, allowing us to use the leaks tracer.
        unsafe {
            window.destroy();
        }

144
145
146
147
148
149
150
151
152
153
        // GTK will keep the Application alive for the whole process lifetime.
        // Wrapping the pipeline in a RefCell<Option<_>> and removing it from it here
        // ensures the pipeline is actually destroyed when shutting down, allowing us
        // to use the leaks tracer for example.
        if let Some(pipeline) = pipeline.borrow_mut().take() {
            pipeline
                .set_state(gst::State::Null)
                .expect("Unable to set the pipeline to the `Null` state");
            pipeline.bus().unwrap().remove_watch().unwrap();
        }
154
155

        if let Some(timeout_id) = timeout_id.borrow_mut().take() {
156
            timeout_id.remove();
157
        }
158
159
160
161
    });
}

fn main() {
162
    // Initialize gstreamer and the gtk widget toolkit libraries.
163
164
165
    gst::init().unwrap();
    gtk::init().unwrap();

166
167
    {
        let app = gtk::Application::new(None, gio::ApplicationFlags::FLAGS_NONE);
Sebastian Dröge's avatar
Sebastian Dröge committed
168

169
170
171
172
173
174
175
176
        app.connect_activate(create_ui);
        app.run();
    }

    // Optional, can be used to detect leaks using the leaks tracer
    unsafe {
        gst::deinit();
    }
Sebastian Dröge's avatar
Sebastian Dröge committed
177
}