textwrap: use the lines property in accumulate mode too

In that mode, textwrap accumulates and pushs out lines of text
up to accumulate-time. We can still make use of the lines property
in that mode, and accumulate as many lines of text as specified.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/474>
This commit is contained in:
Mathieu Duponchelle 2021-02-10 17:01:32 +01:00 committed by GStreamer Merge Bot
parent ab251ec573
commit 75170e5162

View file

@ -180,6 +180,7 @@ impl TextWrap {
if accumulate_time.is_some() {
let mut bufferlist = gst::BufferList::new();
let n_lines = std::cmp::max(self.settings.lock().unwrap().lines, 1);
if state.end_ts.is_some() && state.end_ts + accumulate_time < buffer.get_pts() {
let mut buf = gst::Buffer::from_mut_slice(
@ -220,14 +221,31 @@ impl TextWrap {
.expect("We should have a wrapper by now");
let lines = textwrap::wrap(&current_text, options);
let len = lines.len();
let mut chunks = lines.chunks(n_lines as usize).peekable();
let mut trailing = "".to_string();
for (i, line) in lines.iter().enumerate() {
if i + 1 == len {
trailing = line.to_string();
while let Some(chunk) = chunks.next() {
if chunks.peek().is_none() {
trailing = chunk
.iter()
.map(|l| l.to_string())
.collect::<Vec<String>>()
.join("\n");
} else {
let mut buf = gst::Buffer::from_mut_slice(line.to_string().into_bytes());
let contents = chunk
.iter()
.map(|l| l.to_string())
.collect::<Vec<String>>()
.join("\n");
gst_info!(
CAT,
obj: element,
"Outputting contents {}, ts: {}, duration: {}",
contents.to_string(),
state.start_ts,
state.end_ts - state.start_ts
);
let mut buf = gst::Buffer::from_mut_slice(contents.into_bytes());
{
let buf_mut = buf.get_mut().unwrap();
buf_mut.set_pts(state.start_ts);
@ -280,6 +298,7 @@ impl TextWrap {
let data = chunk.join("\n");
let duration: gst::ClockTime =
duration_per_word * data.split_whitespace().count() as u64;
gst_info!(CAT, "Pushing lines {}", data);
let mut buf = gst::Buffer::from_mut_slice(data.into_bytes());
{