2020-05-22 14:34:20 +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-05-22 14:34:20 +00:00
|
|
|
//
|
2022-01-15 18:40:12 +00:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
2020-05-22 14:34:20 +00:00
|
|
|
|
2021-06-03 18:20:54 +00:00
|
|
|
use gst::glib;
|
2020-05-22 14:34:20 +00:00
|
|
|
use gst::prelude::*;
|
|
|
|
use gst::subclass::prelude::*;
|
|
|
|
use gst_video::prelude::*;
|
|
|
|
|
2024-01-31 15:07:56 +00:00
|
|
|
use once_cell::sync::Lazy;
|
2020-11-22 17:21:45 +00:00
|
|
|
|
2020-05-22 14:34:20 +00:00
|
|
|
use std::sync::Mutex;
|
|
|
|
|
2021-02-12 01:08:07 +00:00
|
|
|
use crate::ccutils::extract_cdp;
|
2024-03-22 06:30:23 +00:00
|
|
|
use crate::cea608utils::Cea608Renderer;
|
2020-05-22 14:34:20 +00:00
|
|
|
|
2020-11-22 17:21:45 +00:00
|
|
|
static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
|
|
|
|
gst::DebugCategory::new(
|
|
|
|
"cea608overlay",
|
|
|
|
gst::DebugColorFlags::empty(),
|
|
|
|
Some("CEA 608 overlay element"),
|
|
|
|
)
|
|
|
|
});
|
2020-05-22 14:34:20 +00:00
|
|
|
|
2021-02-12 01:08:07 +00:00
|
|
|
const DEFAULT_FIELD: i32 = -1;
|
2021-06-16 21:43:30 +00:00
|
|
|
const DEFAULT_BLACK_BACKGROUND: bool = false;
|
2021-02-12 01:08:07 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Settings {
|
|
|
|
field: i32,
|
2021-06-16 21:43:30 +00:00
|
|
|
black_background: bool,
|
2021-10-25 23:11:35 +00:00
|
|
|
timeout: Option<gst::ClockTime>,
|
2021-02-12 01:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Settings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Settings {
|
|
|
|
field: DEFAULT_FIELD,
|
2021-06-16 21:43:30 +00:00
|
|
|
black_background: DEFAULT_BLACK_BACKGROUND,
|
2021-10-25 23:11:35 +00:00
|
|
|
timeout: gst::ClockTime::NONE,
|
2021-02-12 01:08:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 14:34:20 +00:00
|
|
|
struct State {
|
|
|
|
video_info: Option<gst_video::VideoInfo>,
|
2024-03-22 06:30:23 +00:00
|
|
|
renderer: Cea608Renderer,
|
2020-05-22 14:34:20 +00:00
|
|
|
composition: Option<gst_video::VideoOverlayComposition>,
|
|
|
|
attach: bool,
|
2021-02-12 01:08:07 +00:00
|
|
|
selected_field: Option<u8>,
|
2021-10-25 23:11:35 +00:00
|
|
|
last_cc_pts: Option<gst::ClockTime>,
|
2020-05-22 14:34:20 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 16:32:36 +00:00
|
|
|
// SAFETY: Required because `pango::Layout` is not `Send` but the whole `State` needs to be.
|
|
|
|
// We ensure that no additional references to the layout are ever created, which makes it safe
|
|
|
|
// to send it to other threads as long as only a single thread uses it concurrently.
|
|
|
|
unsafe impl Send for State {}
|
|
|
|
|
2020-05-22 14:34:20 +00:00
|
|
|
impl Default for State {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
video_info: None,
|
2024-03-22 06:30:23 +00:00
|
|
|
renderer: Cea608Renderer::new(),
|
2020-05-22 14:34:20 +00:00
|
|
|
composition: None,
|
|
|
|
attach: false,
|
2021-02-12 01:08:07 +00:00
|
|
|
selected_field: None,
|
2021-10-25 23:11:35 +00:00
|
|
|
last_cc_pts: gst::ClockTime::NONE,
|
2020-05-22 14:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 13:50:12 +00:00
|
|
|
pub struct Cea608Overlay {
|
2020-05-22 14:34:20 +00:00
|
|
|
srcpad: gst::Pad,
|
|
|
|
sinkpad: gst::Pad,
|
|
|
|
state: Mutex<State>,
|
2021-02-12 01:08:07 +00:00
|
|
|
settings: Mutex<Settings>,
|
2020-05-22 14:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Cea608Overlay {
|
2024-03-22 06:30:23 +00:00
|
|
|
fn overlay(&self, state: &mut State) {
|
|
|
|
if let Some(rect) = state.renderer.generate_rectangle() {
|
|
|
|
state.composition = gst_video::VideoOverlayComposition::new(Some(&rect)).ok();
|
2020-05-22 14:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn negotiate(&self, state: &mut State) -> Result<gst::FlowSuccess, gst::FlowError> {
|
2020-05-22 14:34:20 +00:00
|
|
|
let video_info = match state.video_info.as_ref() {
|
|
|
|
Some(video_info) => Ok(video_info),
|
|
|
|
None => {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::element_imp_error!(
|
|
|
|
self,
|
2020-05-22 14:34:20 +00:00
|
|
|
gst::CoreError::Negotiation,
|
|
|
|
["Element hasn't received valid video caps at negotiation time"]
|
|
|
|
);
|
|
|
|
Err(gst::FlowError::NotNegotiated)
|
|
|
|
}
|
|
|
|
}?;
|
|
|
|
|
|
|
|
let mut caps = video_info.to_caps().unwrap();
|
|
|
|
let mut downstream_accepts_meta = false;
|
|
|
|
|
|
|
|
let upstream_has_meta = caps
|
2021-04-20 12:58:11 +00:00
|
|
|
.features(0)
|
2023-01-15 20:57:14 +00:00
|
|
|
.map(|f| f.contains(gst_video::CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION))
|
2020-05-22 14:34:20 +00:00
|
|
|
.unwrap_or(false);
|
|
|
|
|
|
|
|
if !upstream_has_meta {
|
|
|
|
let mut caps_clone = caps.clone();
|
|
|
|
let overlay_caps = caps_clone.make_mut();
|
|
|
|
|
2021-04-20 12:58:11 +00:00
|
|
|
if let Some(features) = overlay_caps.features_mut(0) {
|
2023-01-15 20:57:14 +00:00
|
|
|
features.add(gst_video::CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION);
|
2020-12-05 18:29:50 +00:00
|
|
|
let peercaps = self.srcpad.peer_query_caps(Some(&caps_clone));
|
|
|
|
downstream_accepts_meta = !peercaps.is_empty();
|
|
|
|
if downstream_accepts_meta {
|
|
|
|
caps = caps_clone;
|
2020-05-22 14:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.attach = upstream_has_meta || downstream_accepts_meta;
|
|
|
|
|
2024-03-22 06:30:23 +00:00
|
|
|
state
|
|
|
|
.renderer
|
|
|
|
.set_video_size(video_info.width(), video_info.height());
|
|
|
|
state
|
|
|
|
.renderer
|
|
|
|
.set_channel(cea608_types::tables::Channel::ONE);
|
2020-05-22 14:34:20 +00:00
|
|
|
|
2020-06-24 22:33:57 +00:00
|
|
|
if !self.srcpad.push_event(gst::event::Caps::new(&caps)) {
|
2020-05-22 14:34:20 +00:00
|
|
|
Err(gst::FlowError::NotNegotiated)
|
|
|
|
} else {
|
|
|
|
Ok(gst::FlowSuccess::Ok)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 06:30:23 +00:00
|
|
|
fn decode_cc_data(&self, state: &mut State, data: &[u8], pts: gst::ClockTime) {
|
2021-02-12 01:08:07 +00:00
|
|
|
if data.len() % 3 != 0 {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::warning!(CAT, "cc_data length is not a multiple of 3, truncating");
|
2021-02-12 01:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for triple in data.chunks_exact(3) {
|
|
|
|
let cc_valid = (triple[0] & 0x04) == 0x04;
|
|
|
|
let cc_type = triple[0] & 0x03;
|
|
|
|
|
2024-03-22 06:30:23 +00:00
|
|
|
if !cc_valid {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if cc_type == 0x00 || cc_type == 0x01 {
|
|
|
|
if state.selected_field.is_none() {
|
|
|
|
state.selected_field = Some(cc_type);
|
|
|
|
gst::info!(CAT, imp: self, "Selected field {} automatically", cc_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if Some(cc_type) != state.selected_field {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
match state.renderer.push_pair([triple[1], triple[2]]) {
|
|
|
|
Err(e) => {
|
2024-04-08 08:09:07 +00:00
|
|
|
gst::warning!(CAT, imp: self, "Failed to parse incoming CEA-608 ({:x?}): {e:?}", [triple[1], triple[2]]);
|
2024-03-22 06:30:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Ok(true) => {
|
|
|
|
state.composition.take();
|
2021-02-12 01:08:07 +00:00
|
|
|
}
|
2024-03-22 06:30:23 +00:00
|
|
|
Ok(false) => continue,
|
|
|
|
};
|
2021-02-12 01:08:07 +00:00
|
|
|
|
2024-03-22 06:30:23 +00:00
|
|
|
self.overlay(state);
|
2021-10-25 23:11:35 +00:00
|
|
|
|
2024-03-22 06:30:23 +00:00
|
|
|
self.reset_timeout(state, pts);
|
|
|
|
} else {
|
|
|
|
break;
|
2021-02-12 01:08:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 06:30:23 +00:00
|
|
|
fn decode_s334_1a(&self, state: &mut State, data: &[u8], pts: gst::ClockTime) {
|
2021-02-12 01:08:07 +00:00
|
|
|
if data.len() % 3 != 0 {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::warning!(CAT, "cc_data length is not a multiple of 3, truncating");
|
2021-02-12 01:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for triple in data.chunks_exact(3) {
|
|
|
|
let cc_type = triple[0] & 0x01;
|
|
|
|
if state.selected_field.is_none() {
|
|
|
|
state.selected_field = Some(cc_type);
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::info!(CAT, imp: self, "Selected field {} automatically", cc_type);
|
2021-02-12 01:08:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-22 06:30:23 +00:00
|
|
|
if Some(cc_type) != state.selected_field {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
match state.renderer.push_pair([triple[1], triple[2]]) {
|
|
|
|
Err(e) => {
|
2024-04-08 08:09:07 +00:00
|
|
|
gst::warning!(CAT, imp: self, "Failed to parse incoming CEA-608 ({:x?}): {e:?}", [triple[1], triple[2]]);
|
2024-03-22 06:30:23 +00:00
|
|
|
continue;
|
2021-02-12 01:08:07 +00:00
|
|
|
}
|
2024-03-22 06:30:23 +00:00
|
|
|
Ok(true) => {
|
|
|
|
state.composition.take();
|
|
|
|
}
|
|
|
|
Ok(false) => continue,
|
|
|
|
};
|
|
|
|
self.overlay(state);
|
2021-10-25 23:11:35 +00:00
|
|
|
|
2024-03-22 06:30:23 +00:00
|
|
|
self.reset_timeout(state, pts);
|
2021-02-12 01:08:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 23:11:35 +00:00
|
|
|
fn reset_timeout(&self, state: &mut State, pts: gst::ClockTime) {
|
|
|
|
state.last_cc_pts = Some(pts);
|
|
|
|
}
|
|
|
|
|
2020-05-22 14:34:20 +00:00
|
|
|
fn sink_chain(
|
|
|
|
&self,
|
|
|
|
pad: &gst::Pad,
|
|
|
|
mut buffer: gst::Buffer,
|
|
|
|
) -> Result<gst::FlowSuccess, gst::FlowError> {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::log!(CAT, obj: pad, "Handling buffer {:?}", buffer);
|
2020-05-22 14:34:20 +00:00
|
|
|
|
2021-10-25 23:11:35 +00:00
|
|
|
let pts = buffer.pts().ok_or_else(|| {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::error!(CAT, obj: pad, "Require timestamped buffers");
|
2021-10-25 23:11:35 +00:00
|
|
|
gst::FlowError::Error
|
|
|
|
})?;
|
|
|
|
|
2020-05-22 14:34:20 +00:00
|
|
|
let mut state = self.state.lock().unwrap();
|
|
|
|
|
|
|
|
if self.srcpad.check_reconfigure() {
|
2022-10-09 13:06:59 +00:00
|
|
|
self.negotiate(&mut state)?;
|
2020-05-22 14:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for meta in buffer.iter_meta::<gst_video::VideoCaptionMeta>() {
|
2021-04-12 12:49:54 +00:00
|
|
|
if meta.caption_type() == gst_video::VideoCaptionType::Cea708Cdp {
|
|
|
|
match extract_cdp(meta.data()) {
|
2021-02-12 01:08:07 +00:00
|
|
|
Ok(data) => {
|
2024-03-22 06:30:23 +00:00
|
|
|
self.decode_cc_data(&mut state, data, pts);
|
2021-02-12 01:08:07 +00:00
|
|
|
}
|
|
|
|
Err(e) => {
|
2023-02-13 09:50:57 +00:00
|
|
|
gst::warning!(CAT, "{e}");
|
|
|
|
gst::element_imp_warning!(self, gst::StreamError::Decode, ["{e}"]);
|
2021-02-12 01:08:07 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-12 12:49:54 +00:00
|
|
|
} else if meta.caption_type() == gst_video::VideoCaptionType::Cea708Raw {
|
2024-03-22 06:30:23 +00:00
|
|
|
self.decode_cc_data(&mut state, meta.data(), pts);
|
2021-04-12 12:49:54 +00:00
|
|
|
} else if meta.caption_type() == gst_video::VideoCaptionType::Cea608S3341a {
|
2024-03-22 06:30:23 +00:00
|
|
|
self.decode_s334_1a(&mut state, meta.data(), pts);
|
2021-04-12 12:49:54 +00:00
|
|
|
} else if meta.caption_type() == gst_video::VideoCaptionType::Cea608Raw {
|
|
|
|
let data = meta.data();
|
2020-05-22 14:34:20 +00:00
|
|
|
assert!(data.len() % 2 == 0);
|
2024-03-22 06:30:23 +00:00
|
|
|
for pair in data.chunks_exact(2) {
|
|
|
|
match state.renderer.push_pair([pair[0], pair[1]]) {
|
|
|
|
Err(e) => {
|
2024-04-08 08:09:07 +00:00
|
|
|
gst::warning!(CAT, imp: self, "Failed to parse incoming CEA-608 ({:x?}): {e:?}", [pair[0], pair[1]]);
|
2024-03-22 06:30:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Ok(true) => {
|
|
|
|
state.composition.take();
|
|
|
|
}
|
|
|
|
Ok(false) => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.overlay(&mut state);
|
2021-10-25 23:11:35 +00:00
|
|
|
|
|
|
|
self.reset_timeout(&mut state, pts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(timeout) = self.settings.lock().unwrap().timeout {
|
|
|
|
if let Some(interval) = pts.opt_saturating_sub(state.last_cc_pts) {
|
|
|
|
if interval > timeout {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::info!(CAT, imp: self, "Reached timeout, clearing overlay");
|
2021-10-25 23:11:35 +00:00
|
|
|
state.composition.take();
|
|
|
|
state.last_cc_pts.take();
|
2020-05-22 14:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(composition) = &state.composition {
|
|
|
|
let buffer = buffer.make_mut();
|
|
|
|
if state.attach {
|
2021-07-30 10:53:35 +00:00
|
|
|
gst_video::VideoOverlayCompositionMeta::add(buffer, composition);
|
2020-05-22 14:34:20 +00:00
|
|
|
} else {
|
|
|
|
let mut frame = gst_video::VideoFrameRef::from_buffer_ref_writable(
|
|
|
|
buffer,
|
|
|
|
state.video_info.as_ref().unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-09-08 12:33:31 +00:00
|
|
|
|
|
|
|
if composition.blend(&mut frame).is_err() {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::error!(CAT, obj: pad, "Failed to blend composition");
|
2020-05-22 14:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
drop(state);
|
|
|
|
|
|
|
|
self.srcpad.push(buffer)
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn sink_event(&self, pad: &gst::Pad, event: gst::Event) -> bool {
|
2020-05-22 14:34:20 +00:00
|
|
|
use gst::EventView;
|
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::log!(CAT, obj: pad, "Handling event {:?}", event);
|
2020-05-22 14:34:20 +00:00
|
|
|
match event.view() {
|
|
|
|
EventView::Caps(c) => {
|
|
|
|
let mut state = self.state.lock().unwrap();
|
2021-04-12 12:49:54 +00:00
|
|
|
state.video_info = gst_video::VideoInfo::from_caps(c.caps()).ok();
|
2020-05-22 14:34:20 +00:00
|
|
|
self.srcpad.check_reconfigure();
|
2022-10-09 13:06:59 +00:00
|
|
|
match self.negotiate(&mut state) {
|
2020-05-22 14:34:20 +00:00
|
|
|
Ok(_) => true,
|
|
|
|
Err(_) => {
|
|
|
|
self.srcpad.mark_reconfigure();
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EventView::FlushStop(..) => {
|
2024-03-22 06:30:23 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
2020-05-22 14:34:20 +00:00
|
|
|
let mut state = self.state.lock().unwrap();
|
2024-03-22 06:30:23 +00:00
|
|
|
state.renderer = Cea608Renderer::new();
|
|
|
|
state
|
|
|
|
.renderer
|
|
|
|
.set_black_background(settings.black_background);
|
2020-05-22 14:34:20 +00:00
|
|
|
state.composition = None;
|
2024-03-22 06:30:23 +00:00
|
|
|
drop(state);
|
|
|
|
drop(settings);
|
2022-10-23 20:03:22 +00:00
|
|
|
gst::Pad::event_default(pad, Some(&*self.obj()), event)
|
2020-05-22 14:34:20 +00:00
|
|
|
}
|
2022-10-23 20:03:22 +00:00
|
|
|
_ => gst::Pad::event_default(pad, Some(&*self.obj()), event),
|
2020-05-22 14:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 16:22:24 +00:00
|
|
|
#[glib::object_subclass]
|
2020-05-22 14:34:20 +00:00
|
|
|
impl ObjectSubclass for Cea608Overlay {
|
2022-10-23 15:42:58 +00:00
|
|
|
const NAME: &'static str = "GstCea608Overlay";
|
2020-11-15 13:50:12 +00:00
|
|
|
type Type = super::Cea608Overlay;
|
2020-05-22 14:34:20 +00:00
|
|
|
type ParentType = gst::Element;
|
|
|
|
|
2020-11-15 13:50:12 +00:00
|
|
|
fn with_class(klass: &Self::Class) -> Self {
|
2021-04-20 12:58:11 +00:00
|
|
|
let templ = klass.pad_template("sink").unwrap();
|
2023-05-10 15:02:08 +00:00
|
|
|
let sinkpad = gst::Pad::builder_from_template(&templ)
|
2020-06-22 08:03:52 +00:00
|
|
|
.chain_function(|pad, parent, buffer| {
|
|
|
|
Cea608Overlay::catch_panic_pad_function(
|
|
|
|
parent,
|
|
|
|
|| Err(gst::FlowError::Error),
|
2022-10-09 13:06:59 +00:00
|
|
|
|overlay| overlay.sink_chain(pad, buffer),
|
2020-06-22 08:03:52 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.event_function(|pad, parent, event| {
|
|
|
|
Cea608Overlay::catch_panic_pad_function(
|
|
|
|
parent,
|
|
|
|
|| false,
|
2022-10-09 13:06:59 +00:00
|
|
|
|overlay| overlay.sink_event(pad, event),
|
2020-06-22 08:03:52 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.flags(gst::PadFlags::PROXY_CAPS)
|
2021-10-16 12:28:09 +00:00
|
|
|
.flags(gst::PadFlags::PROXY_ALLOCATION)
|
2020-06-22 08:03:52 +00:00
|
|
|
.build();
|
2020-05-22 14:34:20 +00:00
|
|
|
|
2021-04-20 12:58:11 +00:00
|
|
|
let templ = klass.pad_template("src").unwrap();
|
2023-05-10 15:02:08 +00:00
|
|
|
let srcpad = gst::Pad::builder_from_template(&templ)
|
2020-06-22 08:03:52 +00:00
|
|
|
.flags(gst::PadFlags::PROXY_CAPS)
|
2021-10-16 12:28:09 +00:00
|
|
|
.flags(gst::PadFlags::PROXY_ALLOCATION)
|
2020-06-22 08:03:52 +00:00
|
|
|
.build();
|
2020-05-22 14:34:20 +00:00
|
|
|
|
|
|
|
Self {
|
|
|
|
srcpad,
|
|
|
|
sinkpad,
|
|
|
|
state: Mutex::new(State::default()),
|
2021-02-12 01:08:07 +00:00
|
|
|
settings: Mutex::new(Settings::default()),
|
2020-05-22 14:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ObjectImpl for Cea608Overlay {
|
2021-02-12 01:08:07 +00:00
|
|
|
fn properties() -> &'static [glib::ParamSpec] {
|
|
|
|
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
|
2021-06-16 21:43:30 +00:00
|
|
|
vec![
|
2022-08-18 12:04:15 +00:00
|
|
|
glib::ParamSpecInt::builder("field")
|
|
|
|
.nick("Field")
|
|
|
|
.blurb("The field to render the caption for when available, (-1=automatic)")
|
|
|
|
.minimum(-1)
|
|
|
|
.maximum(1)
|
|
|
|
.default_value(DEFAULT_FIELD)
|
|
|
|
.mutable_playing()
|
|
|
|
.build(),
|
|
|
|
glib::ParamSpecBoolean::builder("black-background")
|
|
|
|
.nick("Black background")
|
|
|
|
.blurb("Whether a black background should be drawn behind text")
|
|
|
|
.default_value(DEFAULT_BLACK_BACKGROUND)
|
|
|
|
.mutable_playing()
|
|
|
|
.build(),
|
|
|
|
glib::ParamSpecUInt64::builder("timeout")
|
|
|
|
.nick("Timeout")
|
|
|
|
.blurb("Duration after which to erase overlay when no cc data has arrived for the selected field")
|
2022-10-17 17:48:43 +00:00
|
|
|
.minimum(16.seconds().nseconds())
|
2022-08-18 12:04:15 +00:00
|
|
|
.default_value(u64::MAX)
|
|
|
|
.mutable_playing()
|
|
|
|
.build(),
|
2021-06-16 21:43:30 +00:00
|
|
|
]
|
2021-02-12 01:08:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
PROPERTIES.as_ref()
|
|
|
|
}
|
|
|
|
|
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-02-12 01:08:07 +00:00
|
|
|
"field" => {
|
|
|
|
let mut settings = self.settings.lock().unwrap();
|
|
|
|
let mut state = self.state.lock().unwrap();
|
|
|
|
|
2021-04-25 12:41:22 +00:00
|
|
|
settings.field = value.get().expect("type checked upstream");
|
2021-02-12 01:08:07 +00:00
|
|
|
state.selected_field = match settings.field {
|
|
|
|
-1 => None,
|
|
|
|
val => Some(val as u8),
|
|
|
|
};
|
|
|
|
}
|
2021-06-16 21:43:30 +00:00
|
|
|
"black-background" => {
|
|
|
|
let mut settings = self.settings.lock().unwrap();
|
|
|
|
let mut state = self.state.lock().unwrap();
|
|
|
|
|
|
|
|
settings.black_background = value.get().expect("type checked upstream");
|
2024-03-22 06:30:23 +00:00
|
|
|
state
|
|
|
|
.renderer
|
|
|
|
.set_black_background(settings.black_background);
|
|
|
|
state.composition.take();
|
2021-06-16 21:43:30 +00:00
|
|
|
}
|
2021-10-25 23:11:35 +00:00
|
|
|
"timeout" => {
|
|
|
|
let mut settings = self.settings.lock().unwrap();
|
|
|
|
|
|
|
|
let timeout = value.get().expect("type checked upstream");
|
|
|
|
|
|
|
|
settings.timeout = match timeout {
|
|
|
|
u64::MAX => gst::ClockTime::NONE,
|
2022-10-17 17:48:43 +00:00
|
|
|
_ => Some(timeout.nseconds()),
|
2021-10-25 23:11:35 +00:00
|
|
|
};
|
|
|
|
}
|
2021-02-12 01:08:07 +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-02-12 01:08:07 +00:00
|
|
|
"field" => {
|
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
settings.field.to_value()
|
|
|
|
}
|
2021-06-16 21:43:30 +00:00
|
|
|
"black-background" => {
|
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
settings.black_background.to_value()
|
|
|
|
}
|
2021-10-25 23:11:35 +00:00
|
|
|
"timeout" => {
|
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
if let Some(timeout) = settings.timeout {
|
|
|
|
timeout.nseconds().to_value()
|
|
|
|
} else {
|
|
|
|
u64::MAX.to_value()
|
|
|
|
}
|
|
|
|
}
|
2021-02-12 01:08:07 +00:00
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
2022-10-09 13:06:59 +00:00
|
|
|
fn constructed(&self) {
|
|
|
|
self.parent_constructed();
|
2020-05-22 14:34:20 +00:00
|
|
|
|
2022-10-23 20:03:22 +00:00
|
|
|
let obj = self.obj();
|
2020-11-15 13:50:12 +00:00
|
|
|
obj.add_pad(&self.sinkpad).unwrap();
|
|
|
|
obj.add_pad(&self.srcpad).unwrap();
|
2020-05-22 14:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 08:57:31 +00:00
|
|
|
impl GstObjectImpl for Cea608Overlay {}
|
|
|
|
|
2020-05-22 14:34:20 +00:00
|
|
|
impl ElementImpl for Cea608Overlay {
|
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(
|
|
|
|
"Cea 608 overlay",
|
|
|
|
"Video/Overlay/Subtitle",
|
|
|
|
"Renders CEA 608 closed caption meta over raw video frames",
|
|
|
|
"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_video::VideoFormat::iter_raw()
|
|
|
|
.into_video_caps()
|
|
|
|
.unwrap()
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let sink_pad_template = gst::PadTemplate::new(
|
|
|
|
"sink",
|
|
|
|
gst::PadDirection::Sink,
|
|
|
|
gst::PadPresence::Always,
|
|
|
|
&caps,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let src_pad_template = gst::PadTemplate::new(
|
|
|
|
"src",
|
|
|
|
gst::PadDirection::Src,
|
|
|
|
gst::PadPresence::Always,
|
|
|
|
&caps,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
vec![src_pad_template, sink_pad_template]
|
|
|
|
});
|
|
|
|
|
|
|
|
PAD_TEMPLATES.as_ref()
|
|
|
|
}
|
|
|
|
|
2020-05-22 14:34:20 +00:00
|
|
|
fn change_state(
|
|
|
|
&self,
|
|
|
|
transition: gst::StateChange,
|
|
|
|
) -> Result<gst::StateChangeSuccess, gst::StateChangeError> {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::trace!(CAT, imp: self, "Changing state {:?}", transition);
|
2020-05-22 14:34:20 +00:00
|
|
|
|
|
|
|
match transition {
|
|
|
|
gst::StateChange::ReadyToPaused | gst::StateChange::PausedToReady => {
|
|
|
|
// Reset the whole state
|
|
|
|
let mut state = self.state.lock().unwrap();
|
|
|
|
*state = State::default();
|
2021-02-12 01:08:07 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
state.selected_field = match settings.field {
|
|
|
|
-1 => None,
|
|
|
|
val => Some(val as u8),
|
|
|
|
};
|
2020-05-22 14:34:20 +00:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
self.parent_change_state(transition)
|
2020-05-22 14:34:20 +00:00
|
|
|
}
|
|
|
|
}
|