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
// Take a look at the license at the top of the repository in the LICENSE file.

use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
use serde::ser::{Serialize, SerializeSeq, Serializer};

use std::fmt;

use crate::Buffer;
use crate::BufferList;
use crate::BufferListRef;

impl Serialize for BufferListRef {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let iter = self.iter();
        let (remaining, _) = iter.size_hint();
        if remaining > 0 {
            let mut seq = serializer.serialize_seq(Some(remaining))?;
            for buffer in iter {
                seq.serialize_element(buffer)?;
            }
            seq.end()
        } else {
            let seq = serializer.serialize_seq(None)?;
            seq.end()
        }
    }
}

impl Serialize for BufferList {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.as_ref().serialize(serializer)
    }
}

struct BufferListVisitor;
impl<'de> Visitor<'de> for BufferListVisitor {
    type Value = BufferList;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a sequence of Buffers")
    }

    fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
        let mut buffer_list = BufferList::new();
        {
            let buffer_list = buffer_list.get_mut().unwrap();
            while let Some(buffer) = seq.next_element::<Buffer>()? {
                buffer_list.add(buffer);
            }
        }
        Ok(buffer_list)
    }
}

impl<'de> Deserialize<'de> for BufferList {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        skip_assert_initialized!();
        deserializer.deserialize_seq(BufferListVisitor)
    }
}

#[cfg(test)]
mod tests {
    use crate::BufferList;

    #[test]
    fn test_serialize() {
        use crate::Buffer;

        crate::init().unwrap();

        let mut buffer_list = BufferList::new();
        {
            let buffer_list = buffer_list.get_mut().unwrap();

            let mut buffer = Buffer::from_slice(vec![1, 2, 3, 4]);
            {
                let buffer = buffer.get_mut().unwrap();
                buffer.set_pts(1.into());
                buffer.set_offset(0);
                buffer.set_offset_end(4);
                buffer.set_duration(4.into());
            }
            buffer_list.add(buffer);

            let mut buffer = Buffer::from_slice(vec![5, 6]);
            {
                let buffer = buffer.get_mut().unwrap();
                buffer.set_pts(5.into());
                buffer.set_offset(4);
                buffer.set_offset_end(6);
                buffer.set_duration(2.into());
            }
            buffer_list.add(buffer);
        }

        let pretty_config = ron::ser::PrettyConfig::new().with_new_line("".to_string());

        let res = ron::ser::to_string_pretty(&buffer_list, pretty_config);
        assert_eq!(
            Ok(concat!(
                "[",
                "    (",
                "        pts: Some(1),",
                "        dts: None,",
                "        duration: Some(4),",
                "        offset: 0,",
                "        offset_end: 4,",
                "        flags: (",
                "            bits: 0,",
                "        ),",
                "        buffer: \"AQIDBA==\",",
                "    ),",
                "    (",
                "        pts: Some(5),",
                "        dts: None,",
                "        duration: Some(2),",
                "        offset: 4,",
                "        offset_end: 6,",
                "        flags: (",
                "            bits: 0,",
                "        ),",
                "        buffer: \"BQY=\",",
                "    ),",
                "]"
            )
            .to_owned()),
            res,
        );
    }

    #[test]
    fn test_deserialize() {
        crate::init().unwrap();

        let buffer_list_ron = r#"
            [
                (
                    pts: Some(1),
                    dts: None,
                    duration: Some(4),
                    offset: 0,
                    offset_end: 4,
                    flags: (
                        bits: 0,
                    ),
                    buffer: "AQIDBA==",
                ),
                (
                    pts: Some(5),
                    dts: None,
                    duration: Some(2),
                    offset: 4,
                    offset_end: 6,
                    flags: (
                        bits: 0,
                    ),
                    buffer: "BQY=",
                ),
            ]
        "#;

        let buffer_list: BufferList = ron::de::from_str(buffer_list_ron).unwrap();
        let mut iter = buffer_list.iter();
        let buffer = iter.next().unwrap();
        assert_eq!(buffer.pts(), 1.into());
        assert_eq!(buffer.dts(), None.into());
        {
            let data = buffer.map_readable().unwrap();
            assert_eq!(data.as_slice(), vec![1, 2, 3, 4].as_slice());
        }

        let buffer = iter.next().unwrap();
        assert_eq!(buffer.pts(), 5.into());
        {
            let data = buffer.map_readable().unwrap();
            assert_eq!(data.as_slice(), vec![5, 6].as_slice());
        }
    }

    #[test]
    fn test_serde_roundtrip() {
        use crate::Buffer;

        crate::init().unwrap();

        let mut buffer_list = BufferList::new();
        {
            let buffer_list = buffer_list.get_mut().unwrap();

            let mut buffer = Buffer::from_slice(vec![1, 2, 3, 4]);
            {
                let buffer = buffer.get_mut().unwrap();
                buffer.set_pts(1.into());
                buffer.set_offset(0);
                buffer.set_offset_end(4);
                buffer.set_duration(4.into());
            }
            buffer_list.add(buffer);

            let mut buffer = Buffer::from_slice(vec![5, 6]);
            {
                let buffer = buffer.get_mut().unwrap();
                buffer.set_pts(5.into());
                buffer.set_offset(4);
                buffer.set_offset_end(6);
                buffer.set_duration(2.into());
            }
            buffer_list.add(buffer);
        }
        let buffer_list_ser = ron::ser::to_string(&buffer_list).unwrap();

        let buffer_list: BufferList = ron::de::from_str(buffer_list_ser.as_str()).unwrap();
        let mut iter = buffer_list.iter();
        let buffer = iter.next().unwrap();
        assert_eq!(buffer.pts(), 1.into());
        assert_eq!(buffer.dts(), None.into());
        {
            let data = buffer.map_readable().unwrap();
            assert_eq!(data.as_slice(), vec![1, 2, 3, 4].as_slice());
        }

        let buffer = iter.next().unwrap();
        assert_eq!(buffer.pts(), 5.into());
        {
            let data = buffer.map_readable().unwrap();
            assert_eq!(data.as_slice(), vec![5, 6].as_slice());
        }
    }
}