oximedia-container 0.1.5

Container demuxer/muxer for OxiMedia
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! `WebVTT` subtitle demuxer.
//!
//! This module provides a demuxer for `WebVTT` (Web Video Text Tracks)
//! subtitle format. `WebVTT` is a text-based format used primarily for
//! web video subtitles.
//!
//! # Format
//!
//! `WebVTT` files start with "WEBVTT" signature and contain cues with
//! timestamps and text content:
//!
//! ```text
//! WEBVTT
//!
//! 00:00:00.000 --> 00:00:02.000
//! This is the first subtitle
//!
//! 00:00:02.500 --> 00:00:05.000
//! This is the second subtitle
//! ```

use async_trait::async_trait;
use bytes::Bytes;
use oximedia_core::{CodecId, OxiError, OxiResult, Rational, Timestamp};
use oximedia_io::MediaSource;

use crate::demux::Demuxer;
use crate::{ContainerFormat, Metadata, Packet, PacketFlags, ProbeResult, StreamInfo};

/// `WebVTT` demuxer.
///
/// Parses `WebVTT` subtitle files and extracts subtitle cues as packets.
pub struct WebVttDemuxer<R> {
    /// The underlying reader.
    source: R,
    /// Stream information.
    stream_info: Option<StreamInfo>,
    /// Buffer for reading data.
    buffer: Vec<u8>,
    /// Current position in file.
    #[allow(dead_code)]
    position: usize,
    /// Whether we've reached EOF.
    eof: bool,
    /// Whether header has been parsed.
    header_parsed: bool,
    /// Parsed cues ready to be returned.
    cues: Vec<WebVttCue>,
    /// Current cue index.
    cue_index: usize,
}

impl<R> WebVttDemuxer<R> {
    /// Creates a new `WebVTT` demuxer.
    #[must_use]
    pub fn new(source: R) -> Self {
        Self {
            source,
            stream_info: None,
            buffer: Vec::new(),
            position: 0,
            eof: false,
            header_parsed: false,
            cues: Vec::new(),
            cue_index: 0,
        }
    }
}

impl<R: MediaSource> WebVttDemuxer<R> {
    /// Reads all data from the source.
    async fn read_all(&mut self) -> OxiResult<()> {
        loop {
            let mut temp = vec![0u8; 8192];
            let n = self.source.read(&mut temp).await?;
            if n == 0 {
                self.eof = true;
                break;
            }
            self.buffer.extend_from_slice(&temp[..n]);
        }
        Ok(())
    }

    /// Parses the `WebVTT` header and cues.
    async fn parse_file(&mut self) -> OxiResult<()> {
        // Read entire file
        self.read_all().await?;

        // Convert to string
        let content = String::from_utf8(self.buffer.clone()).map_err(|_| OxiError::Parse {
            offset: 0,
            message: "Invalid UTF-8 in WebVTT file".to_string(),
        })?;

        // Check signature
        if !content.starts_with("WEBVTT") {
            return Err(OxiError::Parse {
                offset: 0,
                message: "Missing WEBVTT signature".to_string(),
            });
        }

        // Parse cues
        self.cues = parse_webvtt_cues(&content)?;

        // Create stream info
        let timebase = Rational::new(1, 1000); // WebVTT uses milliseconds
        let mut stream = StreamInfo::new(0, CodecId::WebVtt, timebase);
        stream.metadata = Metadata::new().with_entry("format", "webvtt");
        self.stream_info = Some(stream);

        self.header_parsed = true;
        Ok(())
    }
}

#[async_trait]
impl<R: MediaSource> Demuxer for WebVttDemuxer<R> {
    async fn probe(&mut self) -> OxiResult<ProbeResult> {
        if !self.header_parsed {
            self.parse_file().await?;
        }

        Ok(ProbeResult::new(ContainerFormat::WebVtt, 1.0))
    }

    async fn read_packet(&mut self) -> OxiResult<Packet> {
        if !self.header_parsed {
            self.parse_file().await?;
        }

        if self.cue_index >= self.cues.len() {
            return Err(OxiError::Eof);
        }

        let cue = &self.cues[self.cue_index];
        self.cue_index += 1;

        // Create packet with cue text
        let data = Bytes::from(cue.text.clone().into_bytes());
        let timestamp = Timestamp::new(cue.start_ms, Rational::new(1, 1000));

        Ok(Packet::new(0, data, timestamp, PacketFlags::KEYFRAME))
    }

    fn streams(&self) -> &[StreamInfo] {
        self.stream_info.as_slice()
    }
}

/// A `WebVTT` cue.
#[derive(Debug, Clone)]
struct WebVttCue {
    /// Cue identifier (optional).
    #[allow(dead_code)]
    id: Option<String>,
    /// Start time in milliseconds.
    start_ms: i64,
    /// End time in milliseconds.
    #[allow(dead_code)]
    end_ms: i64,
    /// Cue text content.
    text: String,
    /// Cue settings (positioning, etc.).
    #[allow(dead_code)]
    settings: Option<String>,
}

/// Parses `WebVTT` cues from content.
fn parse_webvtt_cues(content: &str) -> OxiResult<Vec<WebVttCue>> {
    let mut cues = Vec::new();
    let lines: Vec<&str> = content.lines().collect();
    let mut i = 0;

    // Skip header
    while i < lines.len() {
        let line = lines[i].trim();
        if line.is_empty() {
            i += 1;
            break;
        }
        if !line.starts_with("WEBVTT") && !line.starts_with("NOTE") {
            break;
        }
        i += 1;
    }

    // Parse cues
    while i < lines.len() {
        // Skip empty lines
        while i < lines.len() && lines[i].trim().is_empty() {
            i += 1;
        }

        if i >= lines.len() {
            break;
        }

        // Skip NOTE blocks
        if lines[i].trim().starts_with("NOTE") {
            i += 1;
            while i < lines.len() && !lines[i].trim().is_empty() {
                i += 1;
            }
            continue;
        }

        // Try to parse cue
        let cue = parse_cue(&lines, &mut i)?;
        cues.push(cue);
    }

    Ok(cues)
}

/// Parses a single `WebVTT` cue.
fn parse_cue(lines: &[&str], index: &mut usize) -> OxiResult<WebVttCue> {
    let i = *index;

    if i >= lines.len() {
        return Err(OxiError::Parse {
            offset: i as u64,
            message: "Unexpected end of file".to_string(),
        });
    }

    // Check if this line contains a timestamp (cue timing line)
    let mut cue_id = None;
    let mut timing_line_idx = i;

    if !lines[i].contains("-->") {
        // This is a cue identifier
        cue_id = Some(lines[i].trim().to_string());
        timing_line_idx = i + 1;

        if timing_line_idx >= lines.len() {
            return Err(OxiError::Parse {
                offset: timing_line_idx as u64,
                message: "Missing timing line after cue identifier".to_string(),
            });
        }
    }

    // Parse timing line
    let timing_line = lines[timing_line_idx];
    let (start_ms, end_ms, settings) = parse_timing_line(timing_line)?;

    // Parse cue text (everything until next empty line)
    let mut text_lines = Vec::new();
    let mut text_idx = timing_line_idx + 1;

    while text_idx < lines.len() && !lines[text_idx].trim().is_empty() {
        text_lines.push(lines[text_idx]);
        text_idx += 1;
    }

    let text = text_lines.join("\n");

    *index = text_idx;

    Ok(WebVttCue {
        id: cue_id,
        start_ms,
        end_ms,
        text,
        settings,
    })
}

/// Parses a `WebVTT` timing line.
///
/// Format: `00:00:00.000 --> 00:00:02.000 [settings]`
fn parse_timing_line(line: &str) -> OxiResult<(i64, i64, Option<String>)> {
    let parts: Vec<&str> = line.split("-->").collect();
    if parts.len() < 2 {
        return Err(OxiError::Parse {
            offset: 0,
            message: format!("Invalid timing line: {line}"),
        });
    }

    let start_str = parts[0].trim();
    let end_and_settings = parts[1].trim();

    // Split end time and settings
    let end_parts: Vec<&str> = end_and_settings.split_whitespace().collect();
    let end_str = end_parts[0];
    let settings = if end_parts.len() > 1 {
        Some(end_parts[1..].join(" "))
    } else {
        None
    };

    let start_ms = parse_timestamp(start_str)?;
    let end_ms = parse_timestamp(end_str)?;

    Ok((start_ms, end_ms, settings))
}

/// Parses a `WebVTT` timestamp.
///
/// Formats supported:
/// - `HH:MM:SS.mmm`
/// - `MM:SS.mmm`
fn parse_timestamp(s: &str) -> OxiResult<i64> {
    let parts: Vec<&str> = s.split(':').collect();

    let (hours, minutes, seconds) = match parts.len() {
        2 => {
            // MM:SS.mmm
            (0, parts[0], parts[1])
        }
        3 => {
            // HH:MM:SS.mmm
            (
                parts[0].parse::<i64>().map_err(|_| OxiError::Parse {
                    offset: 0,
                    message: format!("Invalid hour in timestamp: {s}"),
                })?,
                parts[1],
                parts[2],
            )
        }
        _ => {
            return Err(OxiError::Parse {
                offset: 0,
                message: format!("Invalid timestamp format: {s}"),
            });
        }
    };

    let minutes = minutes.parse::<i64>().map_err(|_| OxiError::Parse {
        offset: 0,
        message: format!("Invalid minutes in timestamp: {s}"),
    })?;

    // Parse seconds and milliseconds
    let sec_parts: Vec<&str> = seconds.split('.').collect();
    if sec_parts.len() != 2 {
        return Err(OxiError::Parse {
            offset: 0,
            message: format!("Invalid seconds format in timestamp: {s}"),
        });
    }

    let secs = sec_parts[0].parse::<i64>().map_err(|_| OxiError::Parse {
        offset: 0,
        message: format!("Invalid seconds in timestamp: {s}"),
    })?;

    let millis = sec_parts[1].parse::<i64>().map_err(|_| OxiError::Parse {
        offset: 0,
        message: format!("Invalid milliseconds in timestamp: {s}"),
    })?;

    Ok(hours * 3600 * 1000 + minutes * 60 * 1000 + secs * 1000 + millis)
}

#[cfg(test)]
mod tests {
    use super::*;
    use oximedia_io::MemorySource;

    #[test]
    fn test_parse_timestamp_mm_ss() {
        assert_eq!(
            parse_timestamp("00:01.500").expect("operation should succeed"),
            1500
        );
        assert_eq!(
            parse_timestamp("01:30.000").expect("operation should succeed"),
            90000
        );
    }

    #[test]
    fn test_parse_timestamp_hh_mm_ss() {
        assert_eq!(
            parse_timestamp("00:00:01.500").expect("operation should succeed"),
            1500
        );
        assert_eq!(
            parse_timestamp("01:30:00.000").expect("operation should succeed"),
            5400000
        );
    }

    #[test]
    fn test_parse_timing_line() {
        let line = "00:00:01.000 --> 00:00:03.500";
        let (start, end, settings) = parse_timing_line(line).expect("operation should succeed");
        assert_eq!(start, 1000);
        assert_eq!(end, 3500);
        assert!(settings.is_none());
    }

    #[test]
    fn test_parse_timing_line_with_settings() {
        let line = "00:00:01.000 --> 00:00:03.500 align:start position:10%";
        let (start, end, settings) = parse_timing_line(line).expect("operation should succeed");
        assert_eq!(start, 1000);
        assert_eq!(end, 3500);
        assert_eq!(settings, Some("align:start position:10%".to_string()));
    }

    #[tokio::test]
    async fn test_webvtt_demuxer_probe() {
        let content = "WEBVTT\n\n00:00:00.000 --> 00:00:02.000\nHello World\n";
        let source = MemorySource::new(Bytes::from(content));
        let mut demuxer = WebVttDemuxer::new(source);

        let result = demuxer.probe().await.expect("probe should succeed");
        assert_eq!(result.format, ContainerFormat::WebVtt);
        assert_eq!(result.confidence, 1.0);
    }

    #[tokio::test]
    async fn test_webvtt_demuxer_read_packet() {
        let content = "WEBVTT\n\n00:00:00.000 --> 00:00:02.000\nHello\n\n00:00:03.000 --> 00:00:05.000\nWorld\n";
        let source = MemorySource::new(Bytes::from(content));
        let mut demuxer = WebVttDemuxer::new(source);

        demuxer.probe().await.expect("probe should succeed");

        let packet1 = demuxer
            .read_packet()
            .await
            .expect("operation should succeed");
        assert_eq!(packet1.stream_index, 0);
        assert_eq!(
            String::from_utf8(packet1.data.to_vec()).expect("operation should succeed"),
            "Hello"
        );

        let packet2 = demuxer
            .read_packet()
            .await
            .expect("operation should succeed");
        assert_eq!(
            String::from_utf8(packet2.data.to_vec()).expect("operation should succeed"),
            "World"
        );

        let result = demuxer.read_packet().await;
        assert!(matches!(result, Err(OxiError::Eof)));
    }
}