mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer-rs.git
synced 2024-09-28 23:02:16 +00:00
c23498039d
For moving it elsewhere later as public API.
189 lines
5 KiB
Rust
189 lines
5 KiB
Rust
extern crate gstreamer as gst;
|
|
use gst::*;
|
|
|
|
extern crate glib;
|
|
use glib::*;
|
|
|
|
extern crate gio;
|
|
use gio::ApplicationExt;
|
|
|
|
extern crate gtk;
|
|
use gtk::prelude::*;
|
|
use gtk::{Window, WindowType};
|
|
use gtk::ApplicationExt as GtkApplicationExt;
|
|
|
|
use std::env;
|
|
|
|
fn create_ui(app: >k::Application) {
|
|
let pipeline = Pipeline::new(None);
|
|
let src = ElementFactory::make("videotestsrc", None).unwrap();
|
|
let (sink, widget) = if let Some(gtkglsink) = ElementFactory::make("gtkglsink", None) {
|
|
let glsinkbin = ElementFactory::make("glsinkbin", None).unwrap();
|
|
glsinkbin
|
|
.set_property("sink", >kglsink.to_value())
|
|
.unwrap();
|
|
|
|
let widget = gtkglsink.get_property("widget").unwrap();
|
|
(glsinkbin, widget.get::<gtk::Widget>().unwrap())
|
|
} else {
|
|
let sink = ElementFactory::make("gtksink", None).unwrap();
|
|
let widget = sink.get_property("widget").unwrap();
|
|
(sink, widget.get::<gtk::Widget>().unwrap())
|
|
};
|
|
|
|
pipeline.add_many(&[&src, &sink]).unwrap();
|
|
src.link(&sink).unwrap();
|
|
|
|
let window = Window::new(WindowType::Toplevel);
|
|
window.set_default_size(320, 240);
|
|
let vbox = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
|
vbox.pack_start(&widget, true, true, 0);
|
|
let label = gtk::Label::new("Position: 00:00:00");
|
|
vbox.pack_start(&label, true, true, 5);
|
|
window.add(&vbox);
|
|
window.show_all();
|
|
|
|
app.add_window(&window);
|
|
|
|
let pipeline_clone = pipeline.clone();
|
|
gtk::timeout_add(500, move || {
|
|
let pipeline = &pipeline_clone;
|
|
let position = pipeline.query_position(Format::Time);
|
|
|
|
if let Some(position) = position {
|
|
let mut seconds = position / 1_000_000_000;
|
|
let mut minutes = seconds / 60;
|
|
let hours = minutes / 60;
|
|
|
|
seconds -= hours * 60 * 60 + minutes * 60;
|
|
minutes -= hours * 60;
|
|
|
|
label.set_text(&format!(
|
|
"Position: {:02}:{:02}:{:02}",
|
|
hours,
|
|
minutes,
|
|
seconds
|
|
));
|
|
} else {
|
|
label.set_text("Position: 00:00:00");
|
|
}
|
|
|
|
glib::Continue(true)
|
|
});
|
|
|
|
let app_clone = app.clone();
|
|
window.connect_delete_event(move |_, _| {
|
|
let app = &app_clone;
|
|
app.quit();
|
|
Inhibit(false)
|
|
});
|
|
|
|
let bus = pipeline.get_bus().unwrap();
|
|
|
|
let ret = pipeline.set_state(gst::State::Playing);
|
|
assert_ne!(ret, gst::StateChangeReturn::Failure);
|
|
|
|
let app_clone = SendCell::new(app.clone());
|
|
bus.add_watch(move |_, msg| {
|
|
let app = &app_clone;
|
|
match msg.view() {
|
|
MessageView::Eos(..) => gtk::main_quit(),
|
|
MessageView::Error(err) => {
|
|
println!(
|
|
"Error from {}: {} ({:?})",
|
|
msg.get_src().get_path_string(),
|
|
err.get_error(),
|
|
err.get_debug()
|
|
);
|
|
app.get().quit();
|
|
}
|
|
_ => (),
|
|
};
|
|
|
|
glib::Continue(true)
|
|
});
|
|
|
|
let pipeline_clone = pipeline.clone();
|
|
app.connect_shutdown(move |_| {
|
|
let pipeline = &pipeline_clone;
|
|
let ret = pipeline.set_state(gst::State::Null);
|
|
assert_ne!(ret, gst::StateChangeReturn::Failure);
|
|
});
|
|
}
|
|
|
|
fn main() {
|
|
gst::init().unwrap();
|
|
gtk::init().unwrap();
|
|
|
|
let app = gtk::Application::new(None, gio::APPLICATION_FLAGS_NONE).unwrap();
|
|
|
|
app.connect_activate(create_ui);
|
|
let args = env::args().collect::<Vec<_>>();
|
|
let args_ref = args.iter().map(|a| a.as_str()).collect::<Vec<_>>();
|
|
app.run(&args_ref);
|
|
}
|
|
|
|
// Workaround for GTK objects not implementing Send (nor Sync)
|
|
// but us having to use them in a closure that requires Send
|
|
use std::thread;
|
|
use std::cmp;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct SendCell<T> {
|
|
data: T,
|
|
thread_id: thread::ThreadId,
|
|
}
|
|
|
|
impl<T> SendCell<T> {
|
|
pub fn new(data: T) -> Self {
|
|
SendCell {
|
|
data: data,
|
|
thread_id: thread::current().id(),
|
|
}
|
|
}
|
|
|
|
pub fn get(&self) -> &T {
|
|
assert_eq!(thread::current().id(), self.thread_id);
|
|
&self.data
|
|
}
|
|
|
|
pub fn try_get(&self) -> Option<&T> {
|
|
if thread::current().id() == self.thread_id {
|
|
Some(&self.data)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T> From<T> for SendCell<T> {
|
|
fn from(t: T) -> SendCell<T> {
|
|
SendCell::new(t)
|
|
}
|
|
}
|
|
|
|
impl<T: Default> Default for SendCell<T> {
|
|
fn default() -> SendCell<T> {
|
|
SendCell::new(T::default())
|
|
}
|
|
}
|
|
|
|
impl<T: PartialEq> PartialEq<SendCell<T>> for SendCell<T> {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
self.data.eq(&other.data)
|
|
}
|
|
}
|
|
impl<T: Eq> Eq for SendCell<T> {}
|
|
|
|
impl<T: PartialOrd> PartialOrd<SendCell<T>> for SendCell<T> {
|
|
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
|
|
self.data.partial_cmp(&other.data)
|
|
}
|
|
}
|
|
impl<T: Ord> Ord for SendCell<T> {
|
|
fn cmp(&self, other: &Self) -> cmp::Ordering {
|
|
self.data.cmp(&other.data)
|
|
}
|
|
}
|
|
|
|
unsafe impl<T> Send for SendCell<T> {}
|