1use tracing::{trace, warn};
17use bytes::Bytes;
18use crate::DashMpdError;
19
20
21#[derive(Clone, Debug)]
22pub struct VttDocument {
23 contents: Vec<String>,
24 warned_binary_contents: bool,
25}
26
27impl Default for VttDocument {
28 fn default() -> Self {
29 Self::new()
30 }
31}
32
33impl VttDocument {
34 #[must_use]
35 pub fn new() -> VttDocument {
36 VttDocument {
37 contents: Vec::new(),
38 warned_binary_contents: false,
39 }
40 }
41
42 pub fn add_bytes(&mut self, bytes: &Bytes) -> Result<(), DashMpdError> {
44 if let Ok(s) = str::from_utf8(bytes) {
45 self.add_content(s)?;
46 } else {
47 if !self.warned_binary_contents {
48 warn!("Ignoring invalid UTF-8 in VTT subs: {}", String::from_utf8_lossy(bytes));
49 self.warned_binary_contents = true;
50 }
51 }
52 Ok(())
53 }
54
55 pub fn add_content(&mut self, content: &str) -> Result<(), DashMpdError> {
56 trace!("adding VTT content {content}");
57 self.contents.push(content.to_string());
58 Ok(())
59 }
60
61 #[allow(clippy::inherent_to_string)]
65 pub fn to_string(&mut self) -> String {
66 self.contents.concat()
67 }
68}
69