Skip to content

Instantly share code, notes, and snippets.

@jneem
Created Aug 21, 2020
Embed
What would you like to do?
[package]
name = "gst-test"
version = "0.1.0"
authors = ["Joe Neeman <joeneeman@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
gstreamer = "0.15.4"
anyhow = "1.0.27"
thiserror = "1.0.14"
gstreamer-audio = "0.15.3"
console = { version = "0.12.0", default-features = false }
use anyhow::Error;
use gst::prelude::*;
use gst::{ClockTime, Element, ElementFactory, Pipeline, SeekFlags, SeekType, State};
use gstreamer as gst;
#[derive(Debug, thiserror::Error)]
#[error("Missing element {0}")]
struct MissingElement(&'static str);
fn create_pipeline() -> Result<Pipeline, Error> {
gst::init()?;
let pipeline = Pipeline::new(None);
let src = ElementFactory::make("audiotestsrc", None).map_err(|_| MissingElement("testsrc"))?;
let scale = ElementFactory::make("scaletempo", Some("scale"))
.map_err(|_| MissingElement("scaletempo"))?;
let sink = ElementFactory::make("autoaudiosink", Some("sink"))
.map_err(|_| MissingElement("autoaudiosink"))?;
pipeline.add_many(&[&src, &scale, &sink])?;
Element::link_many(&[&src, &scale, &sink])?;
Ok(pipeline)
}
fn main_loop(pipeline: Pipeline) -> Result<(), Error> {
pipeline.set_state(State::Playing)?;
let mut velocity = 1.0;
let term = console::Term::stdout();
let sink = pipeline.get_by_name("sink").unwrap();
use console::Key;
while let Ok(b) = term.read_key() {
let mut new_vel = velocity;
match b {
Key::ArrowRight => new_vel += 2.0,
Key::ArrowLeft => new_vel -= 2.0,
_ => {}
}
if new_vel != velocity {
let pos = dbg!(pipeline.query_position::<ClockTime>()).unwrap();
velocity = dbg!(new_vel);
if velocity > 0.0 {
sink.seek(
velocity,
SeekFlags::FLUSH | SeekFlags::ACCURATE,
SeekType::Set,
pos,
SeekType::Set,
ClockTime::none(),
)
.unwrap();
} else {
sink.seek(
velocity,
SeekFlags::FLUSH | SeekFlags::ACCURATE,
SeekType::Set,
ClockTime::from_nseconds(0),
SeekType::Set,
pos,
)
.unwrap();
}
}
}
pipeline.set_state(State::Null)?;
Ok(())
}
fn main() {
match create_pipeline().and_then(main_loop) {
Ok(r) => r,
Err(e) => eprintln!("Error! {}", e),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment