examples: ensure pipeline is destroyed when application is shutting down

This commit is contained in:
Guillaume Desmottes 2021-05-25 10:52:26 +02:00
parent 10573d882f
commit 37a352dc35
2 changed files with 22 additions and 8 deletions

View file

@ -134,12 +134,19 @@ fn create_ui(app: &gtk::Application) {
// Pipeline reference is owned by the closure below, so will be // Pipeline reference is owned by the closure below, so will be
// destroyed once the app is destroyed // destroyed once the app is destroyed
let timeout_id = RefCell::new(Some(timeout_id)); let timeout_id = RefCell::new(Some(timeout_id));
let pipeline = RefCell::new(Some(pipeline));
app.connect_shutdown(move |_| { app.connect_shutdown(move |_| {
// 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 pipeline
.set_state(gst::State::Null) .set_state(gst::State::Null)
.expect("Unable to set the pipeline to the `Null` state"); .expect("Unable to set the pipeline to the `Null` state");
pipeline.bus().unwrap().remove_watch().unwrap();
}
bus.remove_watch().unwrap();
if let Some(timeout_id) = timeout_id.borrow_mut().take() { if let Some(timeout_id) = timeout_id.borrow_mut().take() {
glib::source_remove(timeout_id); glib::source_remove(timeout_id);
} }

View file

@ -239,12 +239,19 @@ fn create_ui(app: &gtk::Application) {
// Pipeline reference is owned by the closure below, so will be // Pipeline reference is owned by the closure below, so will be
// destroyed once the app is destroyed // destroyed once the app is destroyed
let timeout_id = RefCell::new(Some(timeout_id)); let timeout_id = RefCell::new(Some(timeout_id));
let pipeline = RefCell::new(Some(pipeline));
app.connect_shutdown(move |_| { app.connect_shutdown(move |_| {
// 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 pipeline
.set_state(gst::State::Null) .set_state(gst::State::Null)
.expect("Unable to set the pipeline to the `Null` state"); .expect("Unable to set the pipeline to the `Null` state");
pipeline.bus().unwrap().remove_watch().unwrap();
}
bus.remove_watch().unwrap();
if let Some(timeout_id) = timeout_id.borrow_mut().take() { if let Some(timeout_id) = timeout_id.borrow_mut().take() {
glib::source_remove(timeout_id); glib::source_remove(timeout_id);
} }