2020-04-13 17:39:52 +00:00
|
|
|
// Copyright (C) 2020 Mathieu Duponchelle <mathieu@centricular.com>
|
|
|
|
//
|
2022-01-15 18:40:12 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0.
|
|
|
|
// If a copy of the MPL was not distributed with this file, You can obtain one at
|
|
|
|
// <https://mozilla.org/MPL/2.0/>.
|
2020-04-13 17:39:52 +00:00
|
|
|
//
|
2022-01-15 18:40:12 +00:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
2020-04-13 17:39:52 +00:00
|
|
|
|
2021-06-03 18:20:54 +00:00
|
|
|
use gst::glib;
|
2020-04-13 17:39:52 +00:00
|
|
|
use gst::prelude::*;
|
|
|
|
use gst::subclass::prelude::*;
|
|
|
|
|
|
|
|
use std::default::Default;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io;
|
2020-12-07 18:44:50 +00:00
|
|
|
use std::mem;
|
2020-04-13 17:39:52 +00:00
|
|
|
use std::sync::Mutex;
|
|
|
|
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
|
|
|
use hyphenation::{Load, Standard};
|
|
|
|
|
|
|
|
static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
|
|
|
|
gst::DebugCategory::new(
|
|
|
|
"textwrap",
|
|
|
|
gst::DebugColorFlags::empty(),
|
|
|
|
Some("Text wrapper element"),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
const DEFAULT_DICTIONARY: Option<String> = None;
|
|
|
|
const DEFAULT_COLUMNS: u32 = 32; /* CEA 608 max columns */
|
|
|
|
const DEFAULT_LINES: u32 = 0;
|
2021-06-23 14:30:39 +00:00
|
|
|
const DEFAULT_ACCUMULATE: gst::ClockTime = gst::ClockTime::ZERO;
|
2020-04-13 17:39:52 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Settings {
|
|
|
|
dictionary: Option<String>,
|
|
|
|
columns: u32,
|
|
|
|
lines: u32,
|
2021-06-23 14:30:39 +00:00
|
|
|
accumulate_time: gst::ClockTime,
|
2020-04-13 17:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Settings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
dictionary: DEFAULT_DICTIONARY,
|
|
|
|
columns: DEFAULT_COLUMNS, /* CEA 608 max columns */
|
|
|
|
lines: DEFAULT_LINES,
|
2021-06-23 14:30:39 +00:00
|
|
|
accumulate_time: DEFAULT_ACCUMULATE,
|
2020-04-13 17:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 10:25:47 +00:00
|
|
|
struct State {
|
2022-02-27 09:00:30 +00:00
|
|
|
options: Option<textwrap::Options<'static>>,
|
2020-12-07 18:44:50 +00:00
|
|
|
|
|
|
|
current_text: String,
|
2021-05-26 17:03:09 +00:00
|
|
|
start_ts: Option<gst::ClockTime>,
|
|
|
|
end_ts: Option<gst::ClockTime>,
|
2020-04-13 17:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for State {
|
|
|
|
fn default() -> Self {
|
2020-12-07 18:44:50 +00:00
|
|
|
Self {
|
|
|
|
options: None,
|
|
|
|
|
|
|
|
current_text: "".to_string(),
|
2021-05-26 17:03:09 +00:00
|
|
|
start_ts: None,
|
|
|
|
end_ts: None,
|
2020-12-07 18:44:50 +00:00
|
|
|
}
|
2020-04-13 17:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 17:41:56 +00:00
|
|
|
pub struct TextWrap {
|
2020-04-13 17:39:52 +00:00
|
|
|
srcpad: gst::Pad,
|
|
|
|
sinkpad: gst::Pad,
|
|
|
|
settings: Mutex<Settings>,
|
|
|
|
state: Mutex<State>,
|
|
|
|
}
|
|
|
|
|
2021-02-10 15:58:09 +00:00
|
|
|
fn is_punctuation(word: &str) -> bool {
|
|
|
|
word == "." || word == "," || word == "?" || word == "!" || word == ";" || word == ":"
|
|
|
|
}
|
|
|
|
|
2020-04-13 17:39:52 +00:00
|
|
|
impl TextWrap {
|
2022-10-09 13:06:59 +00:00
|
|
|
fn update_wrapper(&self) {
|
2020-04-13 17:39:52 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
let mut state = self.state.lock().unwrap();
|
|
|
|
|
2020-12-11 10:25:47 +00:00
|
|
|
if state.options.is_some() {
|
2020-04-13 17:39:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-27 09:00:30 +00:00
|
|
|
let mut options = textwrap::Options::new(settings.columns as usize);
|
|
|
|
|
|
|
|
if let Some(dictionary) = &settings.dictionary {
|
2020-04-13 17:39:52 +00:00
|
|
|
let dict_file = match File::open(dictionary) {
|
|
|
|
Err(err) => {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::error!(CAT, imp: self, "Failed to open dictionary file: {}", err);
|
2020-04-13 17:39:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Ok(dict_file) => dict_file,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut reader = io::BufReader::new(dict_file);
|
|
|
|
let standard = match Standard::any_from_reader(&mut reader) {
|
|
|
|
Err(err) => {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::error!(CAT, imp: self, "Failed to load standard from file: {}", err);
|
2020-04-13 17:39:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Ok(standard) => standard,
|
|
|
|
};
|
|
|
|
|
2022-02-27 09:00:30 +00:00
|
|
|
options.word_splitter = textwrap::WordSplitter::Hyphenation(standard);
|
2020-04-13 17:39:52 +00:00
|
|
|
} else {
|
2022-02-27 09:00:30 +00:00
|
|
|
options.word_splitter = textwrap::WordSplitter::NoHyphenation;
|
|
|
|
}
|
|
|
|
|
|
|
|
state.options = Some(options);
|
2020-04-13 17:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn sink_chain(
|
|
|
|
&self,
|
|
|
|
_pad: &gst::Pad,
|
|
|
|
buffer: gst::Buffer,
|
|
|
|
) -> Result<gst::FlowSuccess, gst::FlowError> {
|
2022-10-09 13:06:59 +00:00
|
|
|
self.update_wrapper();
|
2020-04-13 17:39:52 +00:00
|
|
|
|
2021-05-26 17:03:09 +00:00
|
|
|
let mut pts = buffer.pts().ok_or_else(|| {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::error!(CAT, imp: self, "Need timestamped buffers");
|
2021-05-26 17:03:09 +00:00
|
|
|
gst::FlowError::Error
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let duration = buffer.duration().ok_or_else(|| {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::error!(CAT, imp: self, "Need buffers with duration");
|
2021-05-26 17:03:09 +00:00
|
|
|
gst::FlowError::Error
|
|
|
|
})?;
|
2020-04-13 17:39:52 +00:00
|
|
|
|
|
|
|
let data = buffer.map_readable().map_err(|_| {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::error!(CAT, imp: self, "Can't map buffer readable");
|
2020-04-13 17:39:52 +00:00
|
|
|
gst::FlowError::Error
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let data = std::str::from_utf8(&data).map_err(|err| {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::error!(CAT, imp: self, "Can't decode utf8: {}", err);
|
2020-04-13 17:39:52 +00:00
|
|
|
|
|
|
|
gst::FlowError::Error
|
|
|
|
})?;
|
|
|
|
|
2020-12-07 18:44:50 +00:00
|
|
|
let accumulate_time = self.settings.lock().unwrap().accumulate_time;
|
|
|
|
let mut state = self.state.lock().unwrap();
|
2020-04-13 17:39:52 +00:00
|
|
|
|
2021-06-23 14:30:39 +00:00
|
|
|
if !accumulate_time.is_zero() {
|
2020-04-13 17:39:52 +00:00
|
|
|
let mut bufferlist = gst::BufferList::new();
|
2021-02-10 16:01:32 +00:00
|
|
|
let n_lines = std::cmp::max(self.settings.lock().unwrap().lines, 1);
|
2020-12-07 18:44:50 +00:00
|
|
|
|
2021-09-08 12:33:31 +00:00
|
|
|
let add_buffer = state
|
2021-05-26 17:03:09 +00:00
|
|
|
.start_ts
|
2021-10-09 10:17:05 +00:00
|
|
|
.opt_add(accumulate_time)
|
|
|
|
.opt_lt(pts)
|
|
|
|
.unwrap_or(false);
|
2021-09-08 12:33:31 +00:00
|
|
|
|
|
|
|
if add_buffer {
|
2021-06-04 22:16:05 +00:00
|
|
|
let mut buf =
|
|
|
|
gst::Buffer::from_mut_slice(mem::take(&mut state.current_text).into_bytes());
|
2020-12-07 18:44:50 +00:00
|
|
|
{
|
|
|
|
let buf_mut = buf.get_mut().unwrap();
|
|
|
|
buf_mut.set_pts(state.start_ts);
|
2021-10-09 10:17:05 +00:00
|
|
|
buf_mut
|
|
|
|
.set_duration(state.end_ts.opt_checked_sub(state.start_ts).ok().flatten());
|
2020-12-07 18:44:50 +00:00
|
|
|
}
|
|
|
|
bufferlist.get_mut().unwrap().add(buf);
|
|
|
|
|
2021-05-26 17:03:09 +00:00
|
|
|
state.start_ts = None;
|
|
|
|
state.end_ts = None;
|
2020-12-07 18:44:50 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 17:39:52 +00:00
|
|
|
let duration_per_word: gst::ClockTime =
|
|
|
|
duration / data.split_whitespace().count() as u64;
|
|
|
|
|
2020-12-07 18:44:50 +00:00
|
|
|
if state.start_ts.is_none() {
|
2021-04-12 12:49:54 +00:00
|
|
|
state.start_ts = buffer.pts();
|
2020-12-07 18:44:50 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 12:49:54 +00:00
|
|
|
state.end_ts = buffer.pts();
|
2020-12-07 18:44:50 +00:00
|
|
|
|
|
|
|
let words = data.split_whitespace();
|
|
|
|
let mut current_text = state.current_text.to_string();
|
|
|
|
|
|
|
|
for word in words {
|
2021-02-10 15:58:09 +00:00
|
|
|
if !current_text.is_empty() && !is_punctuation(word) {
|
2020-12-07 18:44:50 +00:00
|
|
|
current_text.push(' ');
|
|
|
|
}
|
|
|
|
current_text.push_str(word);
|
|
|
|
|
|
|
|
let options = state
|
|
|
|
.options
|
|
|
|
.as_ref()
|
|
|
|
.expect("We should have a wrapper by now");
|
|
|
|
|
2022-06-30 12:44:07 +00:00
|
|
|
let lines = textwrap::wrap(¤t_text, options);
|
2021-02-10 16:01:32 +00:00
|
|
|
let mut chunks = lines.chunks(n_lines as usize).peekable();
|
2020-12-07 18:44:50 +00:00
|
|
|
let mut trailing = "".to_string();
|
|
|
|
|
2021-02-10 16:01:32 +00:00
|
|
|
while let Some(chunk) = chunks.next() {
|
|
|
|
if chunks.peek().is_none() {
|
|
|
|
trailing = chunk
|
|
|
|
.iter()
|
|
|
|
.map(|l| l.to_string())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join("\n");
|
2020-12-07 18:44:50 +00:00
|
|
|
} else {
|
2021-10-09 10:17:05 +00:00
|
|
|
let duration = state.end_ts.opt_checked_sub(state.start_ts).ok().flatten();
|
2021-02-10 16:01:32 +00:00
|
|
|
let contents = chunk
|
|
|
|
.iter()
|
|
|
|
.map(|l| l.to_string())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join("\n");
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::info!(
|
2021-02-10 16:01:32 +00:00
|
|
|
CAT,
|
2022-10-09 13:06:59 +00:00
|
|
|
imp: self,
|
2021-02-10 16:01:32 +00:00
|
|
|
"Outputting contents {}, ts: {}, duration: {}",
|
|
|
|
contents.to_string(),
|
2021-05-26 17:03:09 +00:00
|
|
|
state.start_ts.display(),
|
|
|
|
duration.display(),
|
2021-02-10 16:01:32 +00:00
|
|
|
);
|
|
|
|
let mut buf = gst::Buffer::from_mut_slice(contents.into_bytes());
|
2020-12-07 18:44:50 +00:00
|
|
|
{
|
|
|
|
let buf_mut = buf.get_mut().unwrap();
|
|
|
|
buf_mut.set_pts(state.start_ts);
|
2021-05-26 17:03:09 +00:00
|
|
|
buf_mut.set_duration(duration);
|
2020-12-07 18:44:50 +00:00
|
|
|
}
|
|
|
|
bufferlist.get_mut().unwrap().add(buf);
|
|
|
|
state.start_ts = state.end_ts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
current_text = trailing;
|
2021-10-09 10:17:05 +00:00
|
|
|
state.end_ts = state.end_ts.opt_add(duration_per_word);
|
2020-12-07 18:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
state.current_text = current_text;
|
|
|
|
|
|
|
|
if state.current_text.is_empty() {
|
2021-05-26 17:03:09 +00:00
|
|
|
state.start_ts = None;
|
|
|
|
state.end_ts = None;
|
2020-12-07 18:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
drop(state);
|
|
|
|
|
|
|
|
if bufferlist.is_empty() {
|
|
|
|
Ok(gst::FlowSuccess::Ok)
|
|
|
|
} else {
|
|
|
|
self.srcpad.push_list(bufferlist)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let lines = self.settings.lock().unwrap().lines;
|
|
|
|
|
|
|
|
let data = {
|
|
|
|
let options = state
|
|
|
|
.options
|
|
|
|
.as_ref()
|
|
|
|
.expect("We should have a wrapper by now");
|
2022-06-30 12:44:07 +00:00
|
|
|
textwrap::fill(data, options)
|
2020-12-07 18:44:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// If the lines property was set, we want to split the result into buffers
|
|
|
|
// of at most N lines. We compute the duration for each of those based on
|
|
|
|
// the total number of words, and the number of words in each of the split-up
|
|
|
|
// buffers.
|
|
|
|
if lines > 0 {
|
|
|
|
let mut bufferlist = gst::BufferList::new();
|
|
|
|
let duration_per_word: gst::ClockTime =
|
|
|
|
duration / data.split_whitespace().count() as u64;
|
|
|
|
|
|
|
|
for chunk in data.lines().collect::<Vec<&str>>().chunks(lines as usize) {
|
|
|
|
let data = chunk.join("\n");
|
|
|
|
let duration: gst::ClockTime =
|
|
|
|
duration_per_word * data.split_whitespace().count() as u64;
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::info!(CAT, "Pushing lines {}", data);
|
2020-12-07 18:44:50 +00:00
|
|
|
let mut buf = gst::Buffer::from_mut_slice(data.into_bytes());
|
|
|
|
|
|
|
|
{
|
|
|
|
let buf = buf.get_mut().unwrap();
|
|
|
|
|
|
|
|
buf.set_pts(pts);
|
|
|
|
buf.set_duration(duration);
|
|
|
|
pts += duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufferlist.get_mut().unwrap().add(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
drop(state);
|
|
|
|
|
|
|
|
self.srcpad.push_list(bufferlist)
|
|
|
|
} else {
|
2020-04-13 17:39:52 +00:00
|
|
|
let mut buf = gst::Buffer::from_mut_slice(data.into_bytes());
|
|
|
|
|
|
|
|
{
|
|
|
|
let buf = buf.get_mut().unwrap();
|
|
|
|
|
|
|
|
buf.set_pts(pts);
|
|
|
|
buf.set_duration(duration);
|
|
|
|
}
|
|
|
|
|
2020-12-07 18:44:50 +00:00
|
|
|
drop(state);
|
|
|
|
|
|
|
|
self.srcpad.push(buf)
|
2020-04-13 17:39:52 +00:00
|
|
|
}
|
2020-12-07 18:44:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-13 17:39:52 +00:00
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn sink_event(&self, pad: &gst::Pad, event: gst::Event) -> bool {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::log!(CAT, obj: pad, "Handling event {:?}", event);
|
2020-04-13 17:39:52 +00:00
|
|
|
|
2020-12-07 18:44:50 +00:00
|
|
|
use gst::EventView;
|
2020-04-13 17:39:52 +00:00
|
|
|
|
2020-12-07 18:44:50 +00:00
|
|
|
match event.view() {
|
|
|
|
EventView::Gap(_) => {
|
|
|
|
let state = self.state.lock().unwrap();
|
|
|
|
/* We are currently accumulating text, no need to forward the gap */
|
|
|
|
if state.start_ts.is_some() {
|
|
|
|
true
|
|
|
|
} else {
|
2022-10-12 14:48:47 +00:00
|
|
|
gst::Pad::event_default(pad, Some(&*self.instance()), event)
|
2020-12-07 18:44:50 +00:00
|
|
|
}
|
2020-04-13 17:39:52 +00:00
|
|
|
}
|
2020-12-07 18:44:50 +00:00
|
|
|
EventView::FlushStart(_) => {
|
|
|
|
let mut state = self.state.lock().unwrap();
|
|
|
|
let options = state.options.take();
|
|
|
|
*state = State::default();
|
|
|
|
state.options = options;
|
|
|
|
drop(state);
|
2022-10-12 14:48:47 +00:00
|
|
|
gst::Pad::event_default(pad, Some(&*self.instance()), event)
|
2020-12-07 18:44:50 +00:00
|
|
|
}
|
|
|
|
EventView::Eos(_) => {
|
|
|
|
let mut state = self.state.lock().unwrap();
|
|
|
|
if !state.current_text.is_empty() {
|
|
|
|
let mut buf = gst::Buffer::from_mut_slice(
|
2021-06-04 22:16:05 +00:00
|
|
|
mem::take(&mut state.current_text).into_bytes(),
|
2020-12-07 18:44:50 +00:00
|
|
|
);
|
|
|
|
{
|
|
|
|
let buf_mut = buf.get_mut().unwrap();
|
|
|
|
buf_mut.set_pts(state.start_ts);
|
2021-05-26 17:03:09 +00:00
|
|
|
buf_mut.set_duration(
|
2021-10-09 10:17:05 +00:00
|
|
|
state.end_ts.opt_checked_sub(state.start_ts).ok().flatten(),
|
2021-05-26 17:03:09 +00:00
|
|
|
);
|
2020-12-07 18:44:50 +00:00
|
|
|
}
|
|
|
|
|
2021-05-26 17:03:09 +00:00
|
|
|
state.start_ts = None;
|
|
|
|
state.end_ts = None;
|
2020-12-07 18:44:50 +00:00
|
|
|
|
|
|
|
drop(state);
|
|
|
|
let _ = self.srcpad.push(buf);
|
|
|
|
} else {
|
|
|
|
drop(state);
|
|
|
|
}
|
2022-10-12 14:48:47 +00:00
|
|
|
gst::Pad::event_default(pad, Some(&*self.instance()), event)
|
2020-12-07 18:44:50 +00:00
|
|
|
}
|
2022-10-12 14:48:47 +00:00
|
|
|
_ => gst::Pad::event_default(pad, Some(&*self.instance()), event),
|
2020-04-13 17:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-10 16:04:11 +00:00
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn src_query(&self, pad: &gst::Pad, query: &mut gst::QueryRef) -> bool {
|
2022-01-19 13:07:45 +00:00
|
|
|
use gst::QueryViewMut;
|
2021-02-10 16:04:11 +00:00
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::log!(CAT, obj: pad, "Handling query {:?}", query);
|
2021-02-10 16:04:11 +00:00
|
|
|
|
|
|
|
match query.view_mut() {
|
2022-01-19 13:07:45 +00:00
|
|
|
QueryViewMut::Latency(q) => {
|
2021-02-10 16:04:11 +00:00
|
|
|
let mut peer_query = gst::query::Latency::new();
|
|
|
|
|
|
|
|
let ret = self.sinkpad.peer_query(&mut peer_query);
|
|
|
|
|
|
|
|
if ret {
|
2021-04-12 12:49:54 +00:00
|
|
|
let (live, min, _) = peer_query.result();
|
2021-06-23 14:30:39 +00:00
|
|
|
let our_latency: gst::ClockTime = self.settings.lock().unwrap().accumulate_time;
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::info!(
|
2021-02-10 16:04:11 +00:00
|
|
|
CAT,
|
2022-10-09 13:06:59 +00:00
|
|
|
imp: self,
|
2021-02-10 16:04:11 +00:00
|
|
|
"Reporting our latency {} + {}",
|
|
|
|
our_latency,
|
|
|
|
min
|
|
|
|
);
|
2021-05-26 17:03:09 +00:00
|
|
|
q.set(live, our_latency + min, gst::ClockTime::NONE);
|
2021-02-10 16:04:11 +00:00
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
2022-10-12 14:48:47 +00:00
|
|
|
_ => gst::Pad::query_default(pad, Some(&*self.instance()), query),
|
2021-02-10 16:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-13 17:39:52 +00:00
|
|
|
}
|
|
|
|
|
2021-03-07 16:22:24 +00:00
|
|
|
#[glib::object_subclass]
|
2020-04-13 17:39:52 +00:00
|
|
|
impl ObjectSubclass for TextWrap {
|
|
|
|
const NAME: &'static str = "RsTextWrap";
|
2020-11-14 17:41:56 +00:00
|
|
|
type Type = super::TextWrap;
|
2020-04-13 17:39:52 +00:00
|
|
|
type ParentType = gst::Element;
|
|
|
|
|
2020-11-14 17:41:56 +00:00
|
|
|
fn with_class(klass: &Self::Class) -> Self {
|
2021-04-20 12:58:11 +00:00
|
|
|
let templ = klass.pad_template("sink").unwrap();
|
2020-06-22 08:03:52 +00:00
|
|
|
let sinkpad = gst::Pad::builder_with_template(&templ, Some("sink"))
|
|
|
|
.chain_function(|pad, parent, buffer| {
|
|
|
|
TextWrap::catch_panic_pad_function(
|
|
|
|
parent,
|
|
|
|
|| Err(gst::FlowError::Error),
|
2022-10-09 13:06:59 +00:00
|
|
|
|textwrap| textwrap.sink_chain(pad, buffer),
|
2020-06-22 08:03:52 +00:00
|
|
|
)
|
|
|
|
})
|
2020-12-07 18:44:50 +00:00
|
|
|
.event_function(|pad, parent, event| {
|
|
|
|
TextWrap::catch_panic_pad_function(
|
|
|
|
parent,
|
|
|
|
|| false,
|
2022-10-09 13:06:59 +00:00
|
|
|
|textwrap| textwrap.sink_event(pad, event),
|
2020-12-07 18:44:50 +00:00
|
|
|
)
|
|
|
|
})
|
2020-06-22 08:03:52 +00:00
|
|
|
.flags(gst::PadFlags::PROXY_CAPS | gst::PadFlags::FIXED_CAPS)
|
|
|
|
.build();
|
2020-04-13 17:39:52 +00:00
|
|
|
|
2021-04-20 12:58:11 +00:00
|
|
|
let templ = klass.pad_template("src").unwrap();
|
2020-06-22 08:03:52 +00:00
|
|
|
let srcpad = gst::Pad::builder_with_template(&templ, Some("src"))
|
2021-02-10 16:04:11 +00:00
|
|
|
.query_function(|pad, parent, query| {
|
|
|
|
TextWrap::catch_panic_pad_function(
|
|
|
|
parent,
|
|
|
|
|| false,
|
2022-10-09 13:06:59 +00:00
|
|
|
|textwrap| textwrap.src_query(pad, query),
|
2021-02-10 16:04:11 +00:00
|
|
|
)
|
|
|
|
})
|
2020-06-22 08:03:52 +00:00
|
|
|
.flags(gst::PadFlags::PROXY_CAPS | gst::PadFlags::FIXED_CAPS)
|
|
|
|
.build();
|
2020-04-13 17:39:52 +00:00
|
|
|
|
|
|
|
let settings = Mutex::new(Settings::default());
|
|
|
|
let state = Mutex::new(State::default());
|
|
|
|
|
|
|
|
Self {
|
|
|
|
srcpad,
|
|
|
|
sinkpad,
|
|
|
|
settings,
|
|
|
|
state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ObjectImpl for TextWrap {
|
2021-01-21 18:21:29 +00:00
|
|
|
fn properties() -> &'static [glib::ParamSpec] {
|
2022-08-18 12:04:15 +00:00
|
|
|
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> =
|
|
|
|
Lazy::new(|| {
|
|
|
|
vec![
|
|
|
|
glib::ParamSpecString::builder("dictionary")
|
|
|
|
.nick("Dictionary")
|
|
|
|
.blurb("Path to a dictionary to load at runtime to perform hyphenation, see \
|
|
|
|
<https://docs.rs/crate/hyphenation/0.7.1> for more information")
|
|
|
|
.mutable_playing()
|
|
|
|
.build(),
|
|
|
|
glib::ParamSpecUInt::builder("columns")
|
|
|
|
.nick("Columns")
|
|
|
|
.blurb("Maximum number of columns for any given line")
|
|
|
|
.minimum(1)
|
|
|
|
.default_value(DEFAULT_COLUMNS)
|
|
|
|
.mutable_playing()
|
|
|
|
.build(),
|
|
|
|
glib::ParamSpecUInt::builder("lines")
|
|
|
|
.nick("Lines")
|
|
|
|
.blurb("Split input buffer into output buffers with max lines (0=do not split)")
|
|
|
|
.default_value(DEFAULT_LINES)
|
|
|
|
.mutable_playing()
|
|
|
|
.build(),
|
|
|
|
glib::ParamSpecUInt64::builder("accumulate-time")
|
|
|
|
.nick("accumulate-time")
|
|
|
|
.blurb("Cut-off time for input text accumulation (0=do not accumulate)")
|
|
|
|
.maximum(u64::MAX - 1)
|
|
|
|
.default_value(DEFAULT_ACCUMULATE.nseconds())
|
|
|
|
.mutable_playing()
|
|
|
|
.build(),
|
2021-01-21 18:21:29 +00:00
|
|
|
]
|
2022-08-18 12:04:15 +00:00
|
|
|
});
|
2021-01-21 18:21:29 +00:00
|
|
|
|
|
|
|
PROPERTIES.as_ref()
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn constructed(&self) {
|
|
|
|
self.parent_constructed();
|
2020-04-13 17:39:52 +00:00
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
let obj = self.instance();
|
2020-11-14 17:41:56 +00:00
|
|
|
obj.add_pad(&self.sinkpad).unwrap();
|
|
|
|
obj.add_pad(&self.srcpad).unwrap();
|
2020-04-13 17:39:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
|
2021-04-12 12:49:54 +00:00
|
|
|
match pspec.name() {
|
2021-01-21 18:21:29 +00:00
|
|
|
"dictionary" => {
|
2020-04-13 17:39:52 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
|
|
|
let mut state = self.state.lock().unwrap();
|
|
|
|
settings.dictionary = value.get().expect("type checked upstream");
|
2020-12-11 10:25:47 +00:00
|
|
|
state.options = None;
|
2020-04-13 17:39:52 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"columns" => {
|
2020-04-13 17:39:52 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
|
|
|
let mut state = self.state.lock().unwrap();
|
2021-04-25 12:41:22 +00:00
|
|
|
settings.columns = value.get().expect("type checked upstream");
|
2020-12-11 10:25:47 +00:00
|
|
|
state.options = None;
|
2020-04-13 17:39:52 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"lines" => {
|
2020-04-13 17:39:52 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
2021-04-25 12:41:22 +00:00
|
|
|
settings.lines = value.get().expect("type checked upstream");
|
2020-04-13 17:39:52 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"accumulate-time" => {
|
2020-12-07 18:44:50 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
2021-02-10 16:04:11 +00:00
|
|
|
let old_accumulate_time = settings.accumulate_time;
|
2022-10-17 17:48:43 +00:00
|
|
|
settings.accumulate_time = value.get::<u64>().unwrap().nseconds();
|
2021-02-10 16:04:11 +00:00
|
|
|
if settings.accumulate_time != old_accumulate_time {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(
|
2021-02-10 16:04:11 +00:00
|
|
|
CAT,
|
2022-10-09 13:06:59 +00:00
|
|
|
imp: self,
|
2021-02-10 16:04:11 +00:00
|
|
|
"Accumulate time changed: {}",
|
2021-05-26 17:03:09 +00:00
|
|
|
settings.accumulate_time.display(),
|
2021-02-10 16:04:11 +00:00
|
|
|
);
|
|
|
|
drop(settings);
|
2022-10-09 13:06:59 +00:00
|
|
|
let _ = self.instance().post_message(
|
|
|
|
gst::message::Latency::builder()
|
|
|
|
.src(&*self.instance())
|
|
|
|
.build(),
|
|
|
|
);
|
2021-02-10 16:04:11 +00:00
|
|
|
}
|
2020-12-07 18:44:50 +00:00
|
|
|
}
|
2020-04-13 17:39:52 +00:00
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
|
2021-04-12 12:49:54 +00:00
|
|
|
match pspec.name() {
|
2021-01-21 18:21:29 +00:00
|
|
|
"dictionary" => {
|
2020-04-13 17:39:52 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
2020-11-19 15:55:57 +00:00
|
|
|
settings.dictionary.to_value()
|
2020-04-13 17:39:52 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"columns" => {
|
2020-04-13 17:39:52 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
2020-11-19 15:55:57 +00:00
|
|
|
settings.columns.to_value()
|
2020-04-13 17:39:52 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"lines" => {
|
2020-04-13 17:39:52 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
2020-11-19 15:55:57 +00:00
|
|
|
settings.lines.to_value()
|
2020-04-13 17:39:52 +00:00
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"accumulate-time" => {
|
2020-12-07 18:44:50 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
2021-06-23 14:30:39 +00:00
|
|
|
settings.accumulate_time.nseconds().to_value()
|
2020-12-07 18:44:50 +00:00
|
|
|
}
|
2020-04-13 17:39:52 +00:00
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 08:57:31 +00:00
|
|
|
impl GstObjectImpl for TextWrap {}
|
|
|
|
|
2020-04-13 17:39:52 +00:00
|
|
|
impl ElementImpl for TextWrap {
|
2021-01-21 18:21:29 +00:00
|
|
|
fn metadata() -> Option<&'static gst::subclass::ElementMetadata> {
|
|
|
|
static ELEMENT_METADATA: Lazy<gst::subclass::ElementMetadata> = Lazy::new(|| {
|
|
|
|
gst::subclass::ElementMetadata::new(
|
|
|
|
"Text Wrapper",
|
|
|
|
"Text/Filter",
|
|
|
|
"Breaks text into fixed-size lines, with optional hyphenation",
|
|
|
|
"Mathieu Duponchelle <mathieu@centricular.com>",
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
Some(&*ELEMENT_METADATA)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pad_templates() -> &'static [gst::PadTemplate] {
|
|
|
|
static PAD_TEMPLATES: Lazy<Vec<gst::PadTemplate>> = Lazy::new(|| {
|
|
|
|
let caps = gst::Caps::builder("text/x-raw")
|
2021-11-06 07:34:10 +00:00
|
|
|
.field("format", "utf8")
|
2021-01-21 18:21:29 +00:00
|
|
|
.build();
|
|
|
|
let src_pad_template = gst::PadTemplate::new(
|
|
|
|
"src",
|
|
|
|
gst::PadDirection::Src,
|
|
|
|
gst::PadPresence::Always,
|
|
|
|
&caps,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let sink_pad_template = gst::PadTemplate::new(
|
|
|
|
"sink",
|
|
|
|
gst::PadDirection::Sink,
|
|
|
|
gst::PadPresence::Always,
|
|
|
|
&caps,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
vec![src_pad_template, sink_pad_template]
|
|
|
|
});
|
|
|
|
|
|
|
|
PAD_TEMPLATES.as_ref()
|
|
|
|
}
|
|
|
|
|
2020-04-13 17:39:52 +00:00
|
|
|
fn change_state(
|
|
|
|
&self,
|
|
|
|
transition: gst::StateChange,
|
|
|
|
) -> Result<gst::StateChangeSuccess, gst::StateChangeError> {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::info!(CAT, imp: self, "Changing state {:?}", transition);
|
2020-04-13 17:39:52 +00:00
|
|
|
|
2020-12-07 18:44:50 +00:00
|
|
|
if let gst::StateChange::PausedToReady = transition {
|
|
|
|
let mut state = self.state.lock().unwrap();
|
|
|
|
*state = State::default();
|
2020-04-13 17:39:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
let success = self.parent_change_state(transition)?;
|
2020-04-13 17:39:52 +00:00
|
|
|
|
|
|
|
Ok(success)
|
|
|
|
}
|
|
|
|
}
|