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

View file

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