diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d705f45..bb3fb710 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,8 @@ - fix deb and rpm bundle [79e4d5d](https://github.com/ffplayout/ffplayout/pull/217/commits/79e4d5dda05e715df96a38070466ea7a4c8378b2) - rearrange custom filters (fix missing output mapping on multiple outputs) [9cb3a62](https://github.com/ffplayout/ffplayout/pull/217/commits/9cb3a6206938adcf1fbe4ce0ec763cad9e812c76) -- use mp2 for loudnorm_ingest to prevent number of samples in frame too big error [45197c2](https://github.com/ffplayout/ffplayout/pull/218/commits/45197c276df1c0394c26b030991b7e0e5490b0ce) +- switch decoder audio codec to pcm_bluray [8b3a80f](https://github.com/ffplayout/ffplayout/pull/218/commits/8b3a80f5602eda240c6a59178c33886c9e81cb1d) +- deserialize drawtext message with struct object and add single quotes around values ## Development diff --git a/ffplayout-engine/src/rpc/mod.rs b/ffplayout-engine/src/rpc/mod.rs index f92c6782..08a99043 100644 --- a/ffplayout-engine/src/rpc/mod.rs +++ b/ffplayout-engine/src/rpc/mod.rs @@ -1,4 +1,4 @@ -use std::{process::exit, sync::atomic::Ordering}; +use std::{fmt, process::exit, sync::atomic::Ordering}; mod zmq_cmd; @@ -8,16 +8,92 @@ use jsonrpc_http_server::{ jsonrpc_core::{IoHandler, Params, Value}, AccessControlAllowOrigin, DomainsValidation, Response, RestApi, ServerBuilder, }; +use serde::Deserialize; use serde_json::{json, Map}; use simplelog::*; use ffplayout_lib::utils::{ - get_delta, get_filter_from_json, get_sec, sec_to_time, write_status, Ingest, Media, - OutputMode::*, PlayerControl, PlayoutConfig, PlayoutStatus, ProcessControl, + get_delta, get_sec, sec_to_time, write_status, Ingest, Media, OutputMode::*, PlayerControl, + PlayoutConfig, PlayoutStatus, ProcessControl, }; use zmq_cmd::zmq_send; +#[derive(Default, Deserialize, Clone)] +struct TextFilter { + text: Option, + x: Option, + y: Option, + fontsize: Option, + line_spacing: Option, + fontcolor: Option, + alpha: Option, + r#box: Option, + boxcolor: Option, + boxborderw: Option, +} + +impl fmt::Display for TextFilter { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let mut s = format!("text='{}'", self.text.clone().unwrap_or_default()); + + if let Some(v) = &self.x { + if !v.is_empty() { + s.push_str(&format!(":x='{v}'")); + } + } + if let Some(v) = &self.y { + if !v.is_empty() { + s.push_str(&format!(":y='{v}'")); + } + } + if let Some(v) = &self.fontsize { + if !v.is_empty() { + s.push_str(&format!(":fontsize={v}")); + } + } + if let Some(v) = &self.line_spacing { + if !v.is_empty() { + s.push_str(&format!(":line_spacing={v}")); + } + } + if let Some(v) = &self.fontcolor { + if !v.is_empty() { + s.push_str(&format!(":fontcolor={v}")); + } + } + if let Some(v) = &self.alpha { + if !v.is_empty() { + s.push_str(&format!(":alpha='{v}'")); + } + } + if let Some(v) = &self.r#box { + if !v.is_empty() { + s.push_str(&format!(":box={v}")); + } + } + if let Some(v) = &self.boxcolor { + if !v.is_empty() { + s.push_str(&format!(":boxcolor={v}")); + } + } + if let Some(v) = &self.boxborderw { + if !v.is_empty() { + s.push_str(&format!(":boxborderw={v}")); + } + } + + write!(f, "{}", s) + } +} + +/// Covert JSON string to ffmpeg filter command. +fn filter_from_json(raw_text: serde_json::Value) -> String { + let filter: TextFilter = serde_json::from_value(raw_text).unwrap_or_default(); + + filter.to_string() +} + /// map media struct to json object fn get_media_map(media: Media) -> Value { json!({ @@ -83,7 +159,7 @@ pub fn json_rpc_server( && &map["control"] == "text" && map.contains_key("message") { - let filter = get_filter_from_json(map["message"].to_string()); + let filter = filter_from_json(map["message"].clone()); // TODO: in Rust 1.65 use let_chains instead if !filter.is_empty() && config.text.zmq_stream_socket.is_some() { diff --git a/lib/src/utils/mod.rs b/lib/src/utils/mod.rs index 45d9d629..9a5caca4 100644 --- a/lib/src/utils/mod.rs +++ b/lib/src/utils/mod.rs @@ -282,21 +282,6 @@ pub fn json_writer(path: &PathBuf, data: JsonPlaylist) -> Result<(), Error> { Ok(()) } -/// Covert JSON string to ffmpeg filter command. -pub fn get_filter_from_json(raw_text: String) -> String { - let re1 = Regex::new(r#""|}|\{"#).unwrap(); - let re2 = Regex::new(r#"id:[0-9]+,?|name:[^,]?,?"#).unwrap(); - let re3 = Regex::new(r#"text:([^,]*)"#).unwrap(); - let text = re1.replace_all(&raw_text, ""); - let text = re2.replace_all(&text, "").clone(); - let filter = re3 - .replace_all(&text, "text:'$1'") - .replace(':', "=") - .replace(',', ":"); - - filter -} - /// Write current status to status file in temp folder. /// /// The status file is init in main function and mostly modified in RPC server.