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
//! Track mapping and routing.
//!
//! Provides flexible track remapping for complex workflows.

#![forbid(unsafe_code)]

use oximedia_core::{OxiError, OxiResult};
use std::collections::HashMap;

use crate::{Packet, StreamInfo};

/// Mapping from input track to output track.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TrackMapping {
    /// Input stream index.
    pub input_index: usize,
    /// Output stream index.
    pub output_index: usize,
}

impl TrackMapping {
    /// Creates a new track mapping.
    #[must_use]
    pub const fn new(input_index: usize, output_index: usize) -> Self {
        Self {
            input_index,
            output_index,
        }
    }

    /// Creates an identity mapping (input == output).
    #[must_use]
    pub const fn identity(index: usize) -> Self {
        Self {
            input_index: index,
            output_index: index,
        }
    }
}

/// Router for remapping tracks.
#[derive(Debug, Clone)]
pub struct TrackRouter {
    mappings: Vec<TrackMapping>,
    reverse_map: HashMap<usize, usize>,
}

impl TrackRouter {
    /// Creates a new track router.
    #[must_use]
    pub fn new() -> Self {
        Self {
            mappings: Vec::new(),
            reverse_map: HashMap::new(),
        }
    }

    /// Adds a mapping.
    pub fn add_mapping(&mut self, mapping: TrackMapping) -> &mut Self {
        self.mappings.push(mapping);
        self.reverse_map
            .insert(mapping.input_index, mapping.output_index);
        self
    }

    /// Adds an identity mapping for a stream.
    pub fn add_identity(&mut self, index: usize) -> &mut Self {
        self.add_mapping(TrackMapping::identity(index))
    }

    /// Returns all mappings.
    #[must_use]
    pub fn mappings(&self) -> &[TrackMapping] {
        &self.mappings
    }

    /// Maps an input index to an output index.
    #[must_use]
    pub fn map(&self, input_index: usize) -> Option<usize> {
        self.reverse_map.get(&input_index).copied()
    }

    /// Remaps a packet to its output index.
    ///
    /// # Errors
    ///
    /// Returns `Err` if there is no mapping for the packet's stream index.
    pub fn remap_packet(&self, mut packet: Packet) -> OxiResult<Packet> {
        let output_index = self.map(packet.stream_index).ok_or_else(|| {
            OxiError::InvalidData(format!("No mapping for stream {}", packet.stream_index))
        })?;

        packet.stream_index = output_index;
        Ok(packet)
    }

    /// Creates a router from a simple index array.
    ///
    /// The array maps input indices to output indices.
    /// Use `None` to skip a stream.
    #[must_use]
    pub fn from_array(mappings: &[Option<usize>]) -> Self {
        let mut router = Self::new();

        for (input_index, output_index) in mappings.iter().enumerate() {
            if let Some(output_index) = output_index {
                router.add_mapping(TrackMapping::new(input_index, *output_index));
            }
        }

        router
    }

    /// Creates an identity router for the given number of streams.
    #[must_use]
    pub fn identity(stream_count: usize) -> Self {
        let mut router = Self::new();
        for i in 0..stream_count {
            router.add_identity(i);
        }
        router
    }

    /// Clears all mappings.
    pub fn clear(&mut self) {
        self.mappings.clear();
        self.reverse_map.clear();
    }

    /// Returns the number of mappings.
    #[must_use]
    pub fn len(&self) -> usize {
        self.mappings.len()
    }

    /// Returns true if there are no mappings.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.mappings.is_empty()
    }
}

impl Default for TrackRouter {
    fn default() -> Self {
        Self::new()
    }
}

/// Builder for complex track routing.
pub struct TrackRoutingBuilder {
    input_streams: Vec<StreamInfo>,
    output_mappings: Vec<Option<usize>>,
}

impl TrackRoutingBuilder {
    /// Creates a new routing builder.
    #[must_use]
    pub fn new() -> Self {
        Self {
            input_streams: Vec::new(),
            output_mappings: Vec::new(),
        }
    }

    /// Sets the input streams.
    pub fn with_input_streams(&mut self, streams: Vec<StreamInfo>) -> &mut Self {
        self.input_streams = streams;
        self.output_mappings = vec![None; self.input_streams.len()];
        self
    }

    /// Maps an input stream to an output index.
    ///
    /// # Errors
    ///
    /// Returns `Err` if `input_index` is out of range.
    pub fn map_stream(&mut self, input_index: usize, output_index: usize) -> OxiResult<&mut Self> {
        if input_index >= self.output_mappings.len() {
            return Err(OxiError::InvalidData(format!(
                "Input index {input_index} out of range"
            )));
        }

        self.output_mappings[input_index] = Some(output_index);
        Ok(self)
    }

    /// Skips an input stream (excludes it from output).
    ///
    /// # Errors
    ///
    /// Returns `Err` if `input_index` is out of range.
    pub fn skip_stream(&mut self, input_index: usize) -> OxiResult<&mut Self> {
        if input_index >= self.output_mappings.len() {
            return Err(OxiError::InvalidData(format!(
                "Input index {input_index} out of range"
            )));
        }

        self.output_mappings[input_index] = None;
        Ok(self)
    }

    /// Builds the track router.
    #[must_use]
    pub fn build(&self) -> TrackRouter {
        TrackRouter::from_array(&self.output_mappings)
    }

    /// Returns the output streams in the mapped order.
    #[must_use]
    pub fn output_streams(&self) -> Vec<StreamInfo> {
        let mut output_streams = vec![None; self.count_outputs()];

        for (input_index, output_index) in self.output_mappings.iter().enumerate() {
            if let Some(output_index) = output_index {
                if *output_index < output_streams.len() {
                    output_streams[*output_index] = Some(self.input_streams[input_index].clone());
                }
            }
        }

        output_streams.into_iter().flatten().collect()
    }

    /// Returns the number of output streams.
    fn count_outputs(&self) -> usize {
        self.output_mappings
            .iter()
            .filter_map(|&x| x)
            .max()
            .map_or(0, |max| max + 1)
    }
}

impl Default for TrackRoutingBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Common routing patterns.
pub struct RoutingPresets;

impl RoutingPresets {
    /// Creates a router that swaps two tracks.
    #[must_use]
    pub fn swap(index1: usize, index2: usize, total_streams: usize) -> TrackRouter {
        let mut router = TrackRouter::new();

        for i in 0..total_streams {
            let output = if i == index1 {
                index2
            } else if i == index2 {
                index1
            } else {
                i
            };
            router.add_mapping(TrackMapping::new(i, output));
        }

        router
    }

    /// Creates a router that reorders tracks to a specific order.
    #[must_use]
    pub fn reorder(new_order: &[usize]) -> TrackRouter {
        let mut router = TrackRouter::new();

        for (output_index, &input_index) in new_order.iter().enumerate() {
            router.add_mapping(TrackMapping::new(input_index, output_index));
        }

        router
    }

    /// Creates a router that filters tracks by indices.
    #[must_use]
    pub fn filter(indices: &[usize]) -> TrackRouter {
        let mut router = TrackRouter::new();

        for (output_index, &input_index) in indices.iter().enumerate() {
            router.add_mapping(TrackMapping::new(input_index, output_index));
        }

        router
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bytes::Bytes;
    use oximedia_core::{CodecId, Rational, Timestamp};

    fn create_test_stream(index: usize) -> StreamInfo {
        let mut stream = StreamInfo::new(index, CodecId::Opus, Rational::new(1, 48000));
        stream.codec_params = crate::stream::CodecParams::audio(48000, 2);
        stream
    }

    fn create_test_packet(stream_index: usize) -> Packet {
        Packet::new(
            stream_index,
            Bytes::new(),
            Timestamp::new(0, Rational::new(1, 1000)),
            crate::PacketFlags::empty(),
        )
    }

    #[test]
    fn test_track_mapping() {
        let mapping = TrackMapping::new(0, 1);
        assert_eq!(mapping.input_index, 0);
        assert_eq!(mapping.output_index, 1);

        let identity = TrackMapping::identity(5);
        assert_eq!(identity.input_index, 5);
        assert_eq!(identity.output_index, 5);
    }

    #[test]
    fn test_track_router() {
        let mut router = TrackRouter::new();
        router.add_mapping(TrackMapping::new(0, 1));
        router.add_mapping(TrackMapping::new(1, 0));

        assert_eq!(router.map(0), Some(1));
        assert_eq!(router.map(1), Some(0));
        assert_eq!(router.map(2), None);

        assert_eq!(router.len(), 2);
        assert!(!router.is_empty());
    }

    #[test]
    fn test_remap_packet() {
        let mut router = TrackRouter::new();
        router.add_mapping(TrackMapping::new(0, 5));

        let packet = create_test_packet(0);
        let remapped = router
            .remap_packet(packet)
            .expect("operation should succeed");

        assert_eq!(remapped.stream_index, 5);
    }

    #[test]
    fn test_router_from_array() {
        let mappings = vec![Some(2), None, Some(0), Some(1)];
        let router = TrackRouter::from_array(&mappings);

        assert_eq!(router.map(0), Some(2));
        assert_eq!(router.map(1), None);
        assert_eq!(router.map(2), Some(0));
        assert_eq!(router.map(3), Some(1));
    }

    #[test]
    fn test_identity_router() {
        let router = TrackRouter::identity(3);

        assert_eq!(router.map(0), Some(0));
        assert_eq!(router.map(1), Some(1));
        assert_eq!(router.map(2), Some(2));
        assert_eq!(router.len(), 3);
    }

    #[test]
    fn test_routing_builder() {
        let streams = vec![
            create_test_stream(0),
            create_test_stream(1),
            create_test_stream(2),
        ];

        let mut builder = TrackRoutingBuilder::new();
        builder.with_input_streams(streams);
        builder.map_stream(0, 1).expect("operation should succeed");
        builder.map_stream(1, 0).expect("operation should succeed");
        builder.skip_stream(2).expect("operation should succeed");

        let router = builder.build();
        assert_eq!(router.map(0), Some(1));
        assert_eq!(router.map(1), Some(0));
        assert_eq!(router.map(2), None);
    }

    #[test]
    fn test_routing_presets_swap() {
        let router = RoutingPresets::swap(0, 2, 3);

        assert_eq!(router.map(0), Some(2));
        assert_eq!(router.map(1), Some(1));
        assert_eq!(router.map(2), Some(0));
    }

    #[test]
    fn test_routing_presets_reorder() {
        let new_order = vec![2, 0, 1];
        let router = RoutingPresets::reorder(&new_order);

        assert_eq!(router.map(2), Some(0));
        assert_eq!(router.map(0), Some(1));
        assert_eq!(router.map(1), Some(2));
    }

    #[test]
    fn test_routing_presets_filter() {
        let indices = vec![1, 3];
        let router = RoutingPresets::filter(&indices);

        assert_eq!(router.map(1), Some(0));
        assert_eq!(router.map(3), Some(1));
        assert_eq!(router.map(0), None);
    }
}