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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! This module contains functionality for storing and retrieving metadata about individual
//! media payloads.

mod keys;
mod klv;

use crate::workflows::metadata::klv::{KlvItem, KlvStore};
use bytes::{Buf, BufMut, Bytes, BytesMut};
use tracing::error;

pub use keys::{MetadataKey, MetadataKeyMap};

/// Allows storing arbitrary attributes and value pairs that can are relevant to an individual
/// media payload/packet. These are stored in a relatively efficient way to make it cheap to
/// clone and attempt to minimize per-packet heap allocations. Once a metadata collection has been
/// created it cannot be modified.
///
/// The metadata currently relies on being passed in a `BytesMut` buffer that it will use for
/// storage. This allows for the creator of media payloads to maintain an arena style memory
/// buffer that persists across media payloads, which should eventually cause each media payload
/// to no longer require its own heap allocation and efficiently re-use unreserved parts of the
/// memory buffer.
///
/// The trade off for cloning and allocation efficiency is that iterating through metadata is an
/// O(N) operation, which means if you need to look for a specific type of metadata you may have to
/// iterate through all other metadata items first. This tradeoff was deemed acceptable for now
/// with the idea that each payload would only have a small amount of metadata attached to it.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct MediaPayloadMetadataCollection {
    data: KlvStore,
}

/// Declares what type of data is being stored in a metadata entry
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub enum MetadataValueType {
    U8,
    U16,
    U32,
    U64,
    I8,
    I16,
    I32,
    I64,
    Bytes,
    Bool,
}

/// An actual value stored in a metadata entry
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum MetadataValue {
    U8(u8),
    U16(u16),
    U32(u32),
    U64(u64),
    I8(i8),
    I16(i16),
    I32(i32),
    I64(i64),
    Bytes(Bytes),
    Bool(bool),
}

/// An individual key/value paired stored as metadata
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct MetadataEntry {
    key: MetadataKey,
    raw_value: Bytes,
}

/// Errors that can occur when creating a metadata entry
#[derive(thiserror::Error, Debug)]
pub enum MetadataEntryError {
    #[error(
        "Metadata entry's value was {value:?} but the type was expected to be {expected_type:?}"
    )]
    ValueDoesNotMatchType {
        value: MetadataValue,
        expected_type: MetadataValueType,
    },

    #[error("Entry's data was too large, and must be under 65,535")]
    ValueTooLarge,
}

impl MediaPayloadMetadataCollection {
    /// Creates a new collection of metadata based on the provided entries. A buffer is passed in
    /// which can allow the creators of the collection to maintain an arena to reduce allocations
    /// for each new metadata collection that is created.
    pub fn new(entries: impl Iterator<Item = MetadataEntry>, buffer: &mut BytesMut) -> Self {
        let mut klv_buffer = buffer.split_off(buffer.len());
        let klv_items = entries.map(|e| KlvItem {
            key: e.key.klv_id,
            value: e.raw_value,
        });

        let klv_data = KlvStore::from_iter(&mut klv_buffer, klv_items).unwrap();

        MediaPayloadMetadataCollection { data: klv_data }
    }

    /// Provides a non-consuming iterator that allows reading of entries within the collection
    pub fn iter(&self) -> impl Iterator<Item = MetadataEntry> {
        self.data.iter().map(|item| MetadataEntry {
            key: MetadataKey::from_klv_id(item.key),
            raw_value: item.value,
        })
    }
}

impl MetadataEntry {
    /// Creates a new media metadata payload entry for the key and value pair
    pub fn new(
        key: MetadataKey,
        value: MetadataValue,
        buffer: &mut BytesMut,
    ) -> Result<Self, MetadataEntryError> {
        let mut buffer = buffer.split_off(buffer.len());
        match value {
            MetadataValue::U8(num) => {
                if key.value_type != MetadataValueType::U8 {
                    return Err(MetadataEntryError::ValueDoesNotMatchType {
                        value,
                        expected_type: MetadataValueType::U8,
                    });
                }

                buffer.put_u8(num);
            }

            MetadataValue::U16(num) => {
                if key.value_type != MetadataValueType::U16 {
                    return Err(MetadataEntryError::ValueDoesNotMatchType {
                        value,
                        expected_type: MetadataValueType::U16,
                    });
                }

                buffer.put_u16(num);
            }

            MetadataValue::U32(num) => {
                if key.value_type != MetadataValueType::U32 {
                    return Err(MetadataEntryError::ValueDoesNotMatchType {
                        value,
                        expected_type: MetadataValueType::U32,
                    });
                }

                buffer.put_u32(num);
            }

            MetadataValue::U64(num) => {
                if key.value_type != MetadataValueType::U64 {
                    return Err(MetadataEntryError::ValueDoesNotMatchType {
                        value,
                        expected_type: MetadataValueType::U64,
                    });
                }

                buffer.put_u64(num);
            }

            MetadataValue::I8(num) => {
                if key.value_type != MetadataValueType::I8 {
                    return Err(MetadataEntryError::ValueDoesNotMatchType {
                        value,
                        expected_type: MetadataValueType::I8,
                    });
                }

                buffer.put_i8(num);
            }

            MetadataValue::I16(num) => {
                if key.value_type != MetadataValueType::I16 {
                    return Err(MetadataEntryError::ValueDoesNotMatchType {
                        value,
                        expected_type: MetadataValueType::I16,
                    });
                }

                buffer.put_i16(num);
            }

            MetadataValue::I32(num) => {
                if key.value_type != MetadataValueType::I32 {
                    return Err(MetadataEntryError::ValueDoesNotMatchType {
                        value,
                        expected_type: MetadataValueType::I32,
                    });
                }

                buffer.put_i32(num);
            }

            MetadataValue::I64(num) => {
                if key.value_type != MetadataValueType::I64 {
                    return Err(MetadataEntryError::ValueDoesNotMatchType {
                        value,
                        expected_type: MetadataValueType::I64,
                    });
                }

                buffer.put_i64(num);
            }

            MetadataValue::Bool(boolean) => {
                if key.value_type != MetadataValueType::Bool {
                    return Err(MetadataEntryError::ValueDoesNotMatchType {
                        value,
                        expected_type: MetadataValueType::Bool,
                    });
                }

                buffer.put_u8(boolean.into());
            }

            MetadataValue::Bytes(bytes) => {
                if key.value_type != MetadataValueType::Bytes {
                    return Err(MetadataEntryError::ValueDoesNotMatchType {
                        value: MetadataValue::Bytes(bytes),
                        expected_type: MetadataValueType::Bytes,
                    });
                }

                buffer.put(bytes);
            }
        }

        if buffer.len() >= u16::MAX as usize {
            return Err(MetadataEntryError::ValueTooLarge);
        }

        Ok(MetadataEntry {
            key,
            raw_value: buffer.freeze(),
        })
    }

    /// Retrieves the key from the entry
    pub fn key(&self) -> MetadataKey {
        self.key
    }

    /// Retrieves the value from the entry
    pub fn value(&self) -> MetadataValue {
        // Clone to not advance the original buffer
        let mut buffer = self.raw_value.clone();

        // We shouldn't have to worry about validation as consumers should have only been able
        // to create an entry via a `new()` call, and therefore we are sure the raw value is
        // correct and matches.
        match self.key.value_type {
            MetadataValueType::U8 => MetadataValue::U8(buffer.get_u8()),
            MetadataValueType::U16 => MetadataValue::U16(buffer.get_u16()),
            MetadataValueType::U32 => MetadataValue::U32(buffer.get_u32()),
            MetadataValueType::U64 => MetadataValue::U64(buffer.get_u64()),
            MetadataValueType::I8 => MetadataValue::I8(buffer.get_i8()),
            MetadataValueType::I16 => MetadataValue::I16(buffer.get_i16()),
            MetadataValueType::I32 => MetadataValue::I32(buffer.get_i32()),
            MetadataValueType::I64 => MetadataValue::I64(buffer.get_i64()),
            MetadataValueType::Bytes => MetadataValue::Bytes(buffer),
            MetadataValueType::Bool => match buffer.get_u8() {
                0 => MetadataValue::Bool(false),
                1 => MetadataValue::Bool(true),
                x => panic!("Invalid boolean value of {}", x),
            },
        }
    }
}

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

    #[test]
    fn can_create_and_get_value_from_u8_metadata_entry() {
        let value = MetadataValue::U8(5);
        let key = MetadataKey {
            klv_id: 15,
            value_type: MetadataValueType::U8,
        };
        let entry = MetadataEntry::new(key, value.clone(), &mut BytesMut::new()).unwrap();
        let returned_value = entry.value();

        assert_eq!(returned_value, value);
    }

    #[test]
    fn can_create_and_get_value_from_u16_metadata_entry() {
        let value = MetadataValue::U16(5);
        let key = MetadataKey {
            klv_id: 15,
            value_type: MetadataValueType::U16,
        };
        let entry = MetadataEntry::new(key, value.clone(), &mut BytesMut::new()).unwrap();
        let returned_value = entry.value();

        assert_eq!(returned_value, value);
    }

    #[test]
    fn can_create_and_get_value_from_u32_metadata_entry() {
        let value = MetadataValue::U32(5);
        let key = MetadataKey {
            klv_id: 15,
            value_type: MetadataValueType::U32,
        };
        let entry = MetadataEntry::new(key, value.clone(), &mut BytesMut::new()).unwrap();
        let returned_value = entry.value();

        assert_eq!(returned_value, value);
    }

    #[test]
    fn can_create_and_get_value_from_u64_metadata_entry() {
        let value = MetadataValue::U64(5);
        let key = MetadataKey {
            klv_id: 15,
            value_type: MetadataValueType::U64,
        };
        let entry = MetadataEntry::new(key, value.clone(), &mut BytesMut::new()).unwrap();
        let returned_value = entry.value();

        assert_eq!(returned_value, value);
    }

    #[test]
    fn can_create_and_get_value_from_i8_metadata_entry() {
        let value = MetadataValue::I8(5);
        let key = MetadataKey {
            klv_id: 15,
            value_type: MetadataValueType::I8,
        };
        let entry = MetadataEntry::new(key, value.clone(), &mut BytesMut::new()).unwrap();
        let returned_value = entry.value();

        assert_eq!(returned_value, value);
    }

    #[test]
    fn can_create_and_get_value_from_i16_metadata_entry() {
        let value = MetadataValue::I16(5);
        let key = MetadataKey {
            klv_id: 15,
            value_type: MetadataValueType::I16,
        };
        let entry = MetadataEntry::new(key, value.clone(), &mut BytesMut::new()).unwrap();
        let returned_value = entry.value();

        assert_eq!(returned_value, value);
    }

    #[test]
    fn can_create_and_get_value_from_i32_metadata_entry() {
        let value = MetadataValue::I32(5);
        let key = MetadataKey {
            klv_id: 15,
            value_type: MetadataValueType::I32,
        };
        let entry = MetadataEntry::new(key, value.clone(), &mut BytesMut::new()).unwrap();
        let returned_value = entry.value();

        assert_eq!(returned_value, value);
    }

    #[test]
    fn can_create_and_get_value_from_i64_metadata_entry() {
        let value = MetadataValue::I64(5);
        let key = MetadataKey {
            klv_id: 15,
            value_type: MetadataValueType::I64,
        };
        let entry = MetadataEntry::new(key, value.clone(), &mut BytesMut::new()).unwrap();
        let returned_value = entry.value();

        assert_eq!(returned_value, value);
    }

    #[test]
    fn can_create_and_get_value_from_bool_metadata_entry() {
        let value = MetadataValue::Bool(true);
        let key = MetadataKey {
            klv_id: 15,
            value_type: MetadataValueType::Bool,
        };
        let entry = MetadataEntry::new(key, value.clone(), &mut BytesMut::new()).unwrap();
        let returned_value = entry.value();

        assert_eq!(returned_value, value);
    }

    #[test]
    fn can_create_and_get_value_from_bytes_metadata_entry() {
        let value = MetadataValue::Bytes(Bytes::from_static(&[1, 2, 3, 4, 5]));
        let key = MetadataKey {
            klv_id: 15,
            value_type: MetadataValueType::Bytes,
        };
        let entry = MetadataEntry::new(key, value.clone(), &mut BytesMut::new()).unwrap();
        let returned_value = entry.value();

        assert_eq!(returned_value, value);
    }

    #[test]
    fn can_create_and_retrieve_media_payload_metadata() {
        let mut buffer = BytesMut::new();
        let mut map = MetadataKeyMap::default();

        let keys = [
            map.register("first", MetadataValueType::U8),
            map.register("second", MetadataValueType::Bool),
            map.register("third", MetadataValueType::Bytes),
        ];

        let values = [
            MetadataValue::U8(5),
            MetadataValue::Bool(true),
            MetadataValue::Bytes(Bytes::from_static(&[1, 2, 3, 4])),
        ];

        let entries = vec![
            MetadataEntry::new(keys[0], values[0].clone(), &mut buffer).unwrap(),
            MetadataEntry::new(keys[1], values[1].clone(), &mut buffer).unwrap(),
            MetadataEntry::new(keys[2], values[2].clone(), &mut buffer).unwrap(),
        ];

        let metadata =
            MediaPayloadMetadataCollection::new(entries.clone().into_iter(), &mut buffer);
        let mut iterator = metadata.iter();

        assert_eq!(
            iterator.next(),
            Some(entries[0].clone()),
            "Unexpected first entry"
        );
        assert_eq!(
            iterator.next(),
            Some(entries[1].clone()),
            "Unexpected second entry"
        );
        assert_eq!(
            iterator.next(),
            Some(entries[2].clone()),
            "Unexpected third entry"
        );
        assert_eq!(iterator.next(), None, "Unexpected fourth entry");
    }

    #[test]
    fn media_payload_metadata_can_be_iterated_multiple_times() {
        let mut buffer = BytesMut::new();
        let mut map = MetadataKeyMap::default();

        let keys = [
            map.register("first", MetadataValueType::U8),
            map.register("second", MetadataValueType::Bool),
        ];

        let values = [MetadataValue::U8(5), MetadataValue::Bool(true)];

        let entries = vec![
            MetadataEntry::new(keys[0], values[0].clone(), &mut buffer).unwrap(),
            MetadataEntry::new(keys[1], values[1].clone(), &mut buffer).unwrap(),
        ];

        let metadata = MediaPayloadMetadataCollection::new(entries.into_iter(), &mut buffer);

        assert_eq!(
            metadata.iter().count(),
            2,
            "Unexpected number of items in iterator"
        );
        assert_eq!(
            metadata.iter().count(),
            2,
            "Unexpected number of items in iterator"
        );
    }
}