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
// Copyright (C) 2018 François Laignel <fengalin@free.fr>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

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

use std::fmt;

use Buffer;
use BufferList;
use 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 {
    extern crate ron;

    use BufferList;

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

        ::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 mut pretty_config = ron::ser::PrettyConfig::default();
        pretty_config.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() {
        ::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.get_pts(), 1.into());
        assert_eq!(buffer.get_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.get_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 Buffer;

        ::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.get_pts(), 1.into());
        assert_eq!(buffer.get_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.get_pts(), 5.into());
        {
            let data = buffer.map_readable().unwrap();
            assert_eq!(data.as_slice(), vec![5, 6].as_slice());
        }
    }
}