Fix GStreamer cleanup on exit

This commit is contained in:
Roderick van Domburg 2022-01-09 16:28:14 +01:00
parent 59d00787c9
commit d2c377d14b
No known key found for this signature in database
GPG key ID: FE2585E713F9F30A
2 changed files with 35 additions and 10 deletions

View file

@ -1,17 +1,20 @@
use super::{Open, Sink, SinkAsBytes, SinkResult}; use std::{
use crate::config::AudioFormat; ops::Drop,
use crate::convert::Converter; sync::mpsc::{sync_channel, SyncSender},
use crate::decoder::AudioPacket; thread,
use crate::{NUM_CHANNELS, SAMPLE_RATE}; };
use gstreamer as gst; use gstreamer as gst;
use gstreamer_app as gst_app; use gstreamer_app as gst_app;
use gst::prelude::*; use gst::{prelude::*, State};
use zerocopy::AsBytes; use zerocopy::AsBytes;
use std::sync::mpsc::{sync_channel, SyncSender}; use super::{Open, Sink, SinkAsBytes, SinkError, SinkResult};
use std::thread;
use crate::{
config::AudioFormat, convert::Converter, decoder::AudioPacket, NUM_CHANNELS, SAMPLE_RATE,
};
#[allow(dead_code)] #[allow(dead_code)]
pub struct GstreamerSink { pub struct GstreamerSink {
@ -115,8 +118,8 @@ impl Open for GstreamerSink {
}); });
pipeline pipeline
.set_state(gst::State::Playing) .set_state(State::Ready)
.expect("unable to set the pipeline to the `Playing` state"); .expect("unable to set the pipeline to the `Ready` state");
Self { Self {
tx, tx,
@ -127,9 +130,29 @@ impl Open for GstreamerSink {
} }
impl Sink for GstreamerSink { impl Sink for GstreamerSink {
fn start(&mut self) -> SinkResult<()> {
self.pipeline
.set_state(State::Playing)
.map_err(|e| SinkError::StateChange(e.to_string()))?;
Ok(())
}
fn stop(&mut self) -> SinkResult<()> {
self.pipeline
.set_state(State::Paused)
.map_err(|e| SinkError::StateChange(e.to_string()))?;
Ok(())
}
sink_as_bytes!(); sink_as_bytes!();
} }
impl Drop for GstreamerSink {
fn drop(&mut self) {
let _ = self.pipeline.set_state(State::Null);
}
}
impl SinkAsBytes for GstreamerSink { impl SinkAsBytes for GstreamerSink {
fn write_bytes(&mut self, data: &[u8]) -> SinkResult<()> { fn write_bytes(&mut self, data: &[u8]) -> SinkResult<()> {
// Copy expensively (in to_vec()) to avoid thread synchronization // Copy expensively (in to_vec()) to avoid thread synchronization

View file

@ -13,6 +13,8 @@ pub enum SinkError {
OnWrite(String), OnWrite(String),
#[error("Audio Sink Error Invalid Parameters: {0}")] #[error("Audio Sink Error Invalid Parameters: {0}")]
InvalidParams(String), InvalidParams(String),
#[error("Audio Sink Error Changing State: {0}")]
StateChange(String),
} }
pub type SinkResult<T> = Result<T, SinkError>; pub type SinkResult<T> = Result<T, SinkError>;