2018-11-05 10:10:14 +00:00
|
|
|
// 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}
|
|
|
|
|
2017-08-17 14:58:15 +00:00
|
|
|
use gst::prelude::*;
|
2017-07-31 11:09:59 +00:00
|
|
|
|
2017-08-17 14:58:15 +00:00
|
|
|
use gio::prelude::*;
|
2017-08-04 14:41:21 +00:00
|
|
|
|
2017-07-31 11:09:59 +00:00
|
|
|
use gtk::prelude::*;
|
|
|
|
|
2018-07-27 10:07:24 +00:00
|
|
|
use std::cell::RefCell;
|
2017-08-04 19:57:12 +00:00
|
|
|
|
2017-08-04 14:41:21 +00:00
|
|
|
fn create_ui(app: >k::Application) {
|
2017-08-17 14:58:15 +00:00
|
|
|
let pipeline = gst::Pipeline::new(None);
|
|
|
|
let src = gst::ElementFactory::make("videotestsrc", None).unwrap();
|
2018-11-05 10:10:14 +00:00
|
|
|
// 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.
|
2019-12-17 19:00:42 +00:00
|
|
|
let (sink, widget) = if let Ok(gtkglsink) = gst::ElementFactory::make("gtkglsink", None) {
|
2018-11-05 10:10:14 +00:00
|
|
|
// 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.
|
2017-08-17 14:58:15 +00:00
|
|
|
let glsinkbin = gst::ElementFactory::make("glsinkbin", None).unwrap();
|
2020-10-20 13:14:10 +00:00
|
|
|
glsinkbin.set_property("sink", >kglsink).unwrap();
|
2018-11-05 10:10:14 +00:00
|
|
|
// 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.
|
2021-04-20 10:24:17 +00:00
|
|
|
let widget = gtkglsink.property("widget").unwrap();
|
2021-04-20 07:19:02 +00:00
|
|
|
(glsinkbin, widget.get::<gtk::Widget>().unwrap())
|
2017-07-31 11:09:59 +00:00
|
|
|
} else {
|
2018-11-05 10:10:14 +00:00
|
|
|
// 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.
|
2017-08-17 14:58:15 +00:00
|
|
|
let sink = gst::ElementFactory::make("gtksink", None).unwrap();
|
2018-11-05 10:10:14 +00:00
|
|
|
// 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.
|
2021-04-20 10:24:17 +00:00
|
|
|
let widget = sink.property("widget").unwrap();
|
2021-04-20 07:19:02 +00:00
|
|
|
(sink, widget.get::<gtk::Widget>().unwrap())
|
2017-07-31 11:09:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pipeline.add_many(&[&src, &sink]).unwrap();
|
|
|
|
src.link(&sink).unwrap();
|
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// Create a simple gtk gui window to place our widget into.
|
2017-08-07 11:18:24 +00:00
|
|
|
let window = gtk::Window::new(gtk::WindowType::Toplevel);
|
2017-07-31 11:09:59 +00:00
|
|
|
window.set_default_size(320, 240);
|
|
|
|
let vbox = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
2018-11-05 10:10:14 +00:00
|
|
|
// Add our widget to the gui
|
2017-07-31 11:09:59 +00:00
|
|
|
vbox.pack_start(&widget, true, true, 0);
|
2019-02-27 20:10:49 +00:00
|
|
|
let label = gtk::Label::new(Some("Position: 00:00:00"));
|
2017-07-31 11:09:59 +00:00
|
|
|
vbox.pack_start(&label, true, true, 5);
|
|
|
|
window.add(&vbox);
|
|
|
|
window.show_all();
|
|
|
|
|
2017-08-04 14:41:21 +00:00
|
|
|
app.add_window(&window);
|
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// 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
|
2018-07-27 10:07:24 +00:00
|
|
|
let pipeline_weak = pipeline.downgrade();
|
2018-11-05 10:10:14 +00:00
|
|
|
// 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.
|
2020-09-14 13:44:46 +00:00
|
|
|
let timeout_id = glib::timeout_add_local(std::time::Duration::from_millis(500), move || {
|
2018-11-05 10:10:14 +00:00
|
|
|
// Here we temporarily retrieve a strong reference on the pipeline from the weak one
|
|
|
|
// we moved into this callback.
|
2018-07-27 10:07:24 +00:00
|
|
|
let pipeline = match pipeline_weak.upgrade() {
|
|
|
|
Some(pipeline) => pipeline,
|
|
|
|
None => return glib::Continue(true),
|
|
|
|
};
|
|
|
|
|
2018-11-05 10:10:14 +00:00
|
|
|
// Query the current playing position from the underlying pipeline.
|
2017-12-09 16:20:21 +00:00
|
|
|
let position = pipeline
|
|
|
|
.query_position::<gst::ClockTime>()
|
2017-12-20 19:46:58 +00:00
|
|
|
.unwrap_or_else(|| 0.into());
|
2018-11-05 10:10:14 +00:00
|
|
|
// Display the playing position in the gui.
|
2017-11-11 10:21:55 +00:00
|
|
|
label.set_text(&format!("Position: {:.0}", position));
|
2018-11-05 10:10:14 +00:00
|
|
|
// Tell the callback to continue calling this closure.
|
2017-07-31 11:09:59 +00:00
|
|
|
glib::Continue(true)
|
|
|
|
});
|
|
|
|
|
2021-04-11 19:39:50 +00:00
|
|
|
let bus = pipeline.bus().unwrap();
|
2017-07-31 11:09:59 +00:00
|
|
|
|
2019-01-08 16:13:37 +00:00
|
|
|
pipeline
|
|
|
|
.set_state(gst::State::Playing)
|
|
|
|
.expect("Unable to set the pipeline to the `Playing` state");
|
2017-07-31 11:09:59 +00:00
|
|
|
|
2019-02-10 10:06:40 +00:00
|
|
|
let app_weak = app.downgrade();
|
|
|
|
bus.add_watch_local(move |_, msg| {
|
2017-08-17 14:58:15 +00:00
|
|
|
use gst::MessageView;
|
|
|
|
|
2018-07-27 10:07:24 +00:00
|
|
|
let app = match app_weak.upgrade() {
|
|
|
|
Some(app) => app,
|
|
|
|
None => return glib::Continue(false),
|
|
|
|
};
|
|
|
|
|
2017-07-31 11:09:59 +00:00
|
|
|
match msg.view() {
|
2021-05-22 15:36:31 +00:00
|
|
|
MessageView::Eos(..) => app.quit(),
|
2017-07-31 11:09:59 +00:00
|
|
|
MessageView::Error(err) => {
|
|
|
|
println!(
|
2017-11-16 11:58:56 +00:00
|
|
|
"Error from {:?}: {} ({:?})",
|
2021-04-11 19:39:50 +00:00
|
|
|
err.src().map(|s| s.path_string()),
|
|
|
|
err.error(),
|
|
|
|
err.debug()
|
2017-07-31 11:09:59 +00:00
|
|
|
);
|
2017-08-04 16:56:13 +00:00
|
|
|
app.quit();
|
2017-07-31 11:09:59 +00:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
};
|
|
|
|
|
|
|
|
glib::Continue(true)
|
2019-12-17 19:00:42 +00:00
|
|
|
})
|
|
|
|
.expect("Failed to add bus watch");
|
2017-07-31 11:09:59 +00:00
|
|
|
|
2018-07-27 10:07:24 +00:00
|
|
|
// 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));
|
2021-05-25 08:52:26 +00:00
|
|
|
let pipeline = RefCell::new(Some(pipeline));
|
2017-08-04 14:41:21 +00:00
|
|
|
app.connect_shutdown(move |_| {
|
2021-05-25 09:55:20 +00:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2021-05-25 08:52:26 +00:00
|
|
|
// 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();
|
|
|
|
}
|
2018-07-27 10:07:24 +00:00
|
|
|
|
|
|
|
if let Some(timeout_id) = timeout_id.borrow_mut().take() {
|
|
|
|
glib::source_remove(timeout_id);
|
|
|
|
}
|
2017-08-04 14:41:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2018-11-05 10:10:14 +00:00
|
|
|
// Initialize gstreamer and the gtk widget toolkit libraries.
|
2017-08-04 14:41:21 +00:00
|
|
|
gst::init().unwrap();
|
|
|
|
gtk::init().unwrap();
|
|
|
|
|
2021-05-22 15:36:31 +00:00
|
|
|
{
|
|
|
|
let app = gtk::Application::new(None, gio::ApplicationFlags::FLAGS_NONE);
|
2017-07-31 11:09:59 +00:00
|
|
|
|
2021-05-22 15:36:31 +00:00
|
|
|
app.connect_activate(create_ui);
|
|
|
|
app.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optional, can be used to detect leaks using the leaks tracer
|
|
|
|
unsafe {
|
|
|
|
gst::deinit();
|
|
|
|
}
|
2017-07-31 11:09:59 +00:00
|
|
|
}
|