Skip to content

Commit

Permalink
fix: audioplayer freezing on close when disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
flxzt committed Jan 4, 2022
1 parent ce5496b commit 0a0124f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
45 changes: 27 additions & 18 deletions src/audioplayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rand::Rng;

#[derive(Debug)]
pub struct RnoteAudioPlayer {
pub enabled: bool,
enabled: bool,
marker_file_srcs: Vec<gst::Element>,
marker_pipeline: Option<gst::Pipeline>,
brush_pipeline: Option<gst::Pipeline>,
Expand All @@ -35,6 +35,17 @@ impl RnoteAudioPlayer {
pub const MARKER_N_FILES: usize = 15;
pub const BRUSH_SEEK_TIMES_SEC: [f64; 5] = [0.0, 0.91, 4.129, 6.0, 8.56];

pub fn enabled(&self) -> bool {
self.enabled
}

pub fn set_enabled(&mut self, enabled: bool) {
if !enabled {
self.set_states_null();
}
self.enabled = enabled;
}

pub fn init(&mut self, _appwindow: &RnoteAppWindow) -> Result<(), anyhow::Error> {
let system_data_dirs = glib::system_data_dirs();

Expand Down Expand Up @@ -311,25 +322,23 @@ impl RnoteAudioPlayer {
}

/// Stop all pipelines by setting their state to Null. Must be called when closing the application
pub fn stop(&self) {
if self.enabled {
if let Some(marker_pipeline) = &self.marker_pipeline {
if let Err(e) = marker_pipeline.set_state(gst::State::Null) {
log::error!(
"audioplayer pipeline set_state(Playing) failed in stop() with Err {}",
e
);
};
pub fn set_states_null(&self) {
if let Some(marker_pipeline) = &self.marker_pipeline {
if let Err(e) = marker_pipeline.set_state(gst::State::Null) {
log::error!(
"audioplayer pipeline set_state(Playing) failed in stop() with Err {}",
e
);
};
if let Some(brush_pipeline) = &self.brush_pipeline {
if let Err(e) = brush_pipeline.set_state(gst::State::Null) {
log::error!(
"audioplayer pipeline set_state(Playing) failed in stop() with Err {}",
e
);
};
};
if let Some(brush_pipeline) = &self.brush_pipeline {
if let Err(e) = brush_pipeline.set_state(gst::State::Null) {
log::error!(
"audioplayer pipeline set_state(Playing) failed in stop() with Err {}",
e
);
};
}
};
}

fn play_marker_sound(&self) {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ pub fn setup_actions(appwindow: &RnoteAppWindow) {
action_pen_sounds.connect_state_notify(clone!(@weak appwindow => move |action_pen_sounds| {
let state = action_pen_sounds.state().unwrap().get::<bool>().unwrap();

appwindow.audioplayer().borrow_mut().enabled = state;
appwindow.audioplayer().borrow_mut().set_enabled(state);
}));

// Righthanded
Expand Down
2 changes: 1 addition & 1 deletion src/ui/appsettings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ pub fn load_settings(appwindow: &RnoteAppWindow) {

// Pen sounds
let enabled = appwindow.app_settings().boolean("pen-sounds");
appwindow.audioplayer().borrow_mut().enabled = enabled;
appwindow.audioplayer().borrow_mut().set_enabled(enabled);

// Sheet margin
let sheet_margin = appwindow.app_settings().double("sheet-margin");
Expand Down
2 changes: 1 addition & 1 deletion src/ui/appwindow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ mod imp {
// Save window state right before the window will be closed
fn close_request(&self, obj: &Self::Type) -> Inhibit {
// Setting all gstreamer pipelines state to Null
obj.audioplayer().borrow_mut().stop();
obj.audioplayer().borrow_mut().set_states_null();

if let Err(err) = obj.save_window_size() {
log::error!("Failed to save window state, {}", &err);
Expand Down

0 comments on commit 0a0124f

Please sign in to comment.