2020-12-15 10:53:31 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
|
2023-01-03 18:58:25 +00:00
|
|
|
use serde::{
|
|
|
|
de::{Deserialize, Deserializer, SeqAccess, Visitor},
|
|
|
|
ser::{Serialize, SerializeSeq, Serializer},
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{Buffer, BufferList, BufferListRef};
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
impl Serialize for BufferListRef {
|
|
|
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
2019-02-21 17:30:36 +00:00
|
|
|
let iter = self.iter();
|
2018-07-16 19:46:10 +00:00
|
|
|
let (remaining, _) = iter.size_hint();
|
|
|
|
if remaining > 0 {
|
|
|
|
let mut seq = serializer.serialize_seq(Some(remaining))?;
|
2019-02-21 17:30:36 +00:00
|
|
|
for buffer in iter {
|
2018-07-16 19:46:10 +00:00
|
|
|
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> {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2018-07-16 19:46:10 +00:00
|
|
|
deserializer.deserialize_seq(BufferListVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-01-03 18:58:25 +00:00
|
|
|
use crate::{BufferList, ClockTime};
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialize() {
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::Buffer;
|
2018-07-16 19:46:10 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
let mut buffer_list = BufferList::new();
|
|
|
|
{
|
|
|
|
let buffer_list = buffer_list.get_mut().unwrap();
|
|
|
|
|
2018-12-26 07:50:44 +00:00
|
|
|
let mut buffer = Buffer::from_slice(vec![1, 2, 3, 4]);
|
2018-07-16 19:46:10 +00:00
|
|
|
{
|
|
|
|
let buffer = buffer.get_mut().unwrap();
|
2020-10-27 17:27:16 +00:00
|
|
|
buffer.set_pts(ClockTime::NSECOND);
|
2018-07-16 19:46:10 +00:00
|
|
|
buffer.set_offset(0);
|
|
|
|
buffer.set_offset_end(4);
|
2020-10-27 17:27:16 +00:00
|
|
|
buffer.set_duration(4 * ClockTime::NSECOND);
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
buffer_list.add(buffer);
|
|
|
|
|
2018-12-26 07:50:44 +00:00
|
|
|
let mut buffer = Buffer::from_slice(vec![5, 6]);
|
2018-07-16 19:46:10 +00:00
|
|
|
{
|
|
|
|
let buffer = buffer.get_mut().unwrap();
|
2020-10-27 17:27:16 +00:00
|
|
|
buffer.set_pts(5 * ClockTime::NSECOND);
|
2018-07-16 19:46:10 +00:00
|
|
|
buffer.set_offset(4);
|
|
|
|
buffer.set_offset_end(6);
|
2020-10-27 17:27:16 +00:00
|
|
|
buffer.set_duration(2 * ClockTime::NSECOND);
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
buffer_list.add(buffer);
|
|
|
|
}
|
|
|
|
|
2021-10-23 07:15:29 +00:00
|
|
|
let pretty_config = ron::ser::PrettyConfig::new().new_line("".to_string());
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
let res = ron::ser::to_string_pretty(&buffer_list, pretty_config);
|
|
|
|
assert_eq!(
|
2018-07-22 14:55:24 +00:00
|
|
|
Ok(concat!(
|
|
|
|
"[",
|
|
|
|
" (",
|
|
|
|
" pts: Some(1),",
|
|
|
|
" dts: None,",
|
|
|
|
" duration: Some(4),",
|
|
|
|
" offset: 0,",
|
|
|
|
" offset_end: 4,",
|
2022-05-05 10:40:07 +00:00
|
|
|
" flags: \"\",",
|
2018-07-22 14:55:24 +00:00
|
|
|
" buffer: \"AQIDBA==\",",
|
|
|
|
" ),",
|
|
|
|
" (",
|
|
|
|
" pts: Some(5),",
|
|
|
|
" dts: None,",
|
|
|
|
" duration: Some(2),",
|
|
|
|
" offset: 4,",
|
|
|
|
" offset_end: 6,",
|
2022-05-05 10:40:07 +00:00
|
|
|
" flags: \"\",",
|
2018-07-22 14:55:24 +00:00
|
|
|
" buffer: \"BQY=\",",
|
|
|
|
" ),",
|
|
|
|
"]"
|
2018-10-08 12:02:23 +00:00
|
|
|
)
|
|
|
|
.to_owned()),
|
2018-07-16 19:46:10 +00:00
|
|
|
res,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_deserialize() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
let buffer_list_ron = r#"
|
|
|
|
[
|
|
|
|
(
|
|
|
|
pts: Some(1),
|
|
|
|
dts: None,
|
|
|
|
duration: Some(4),
|
|
|
|
offset: 0,
|
|
|
|
offset_end: 4,
|
2022-05-05 10:40:07 +00:00
|
|
|
flags: "",
|
2018-07-16 19:46:10 +00:00
|
|
|
buffer: "AQIDBA==",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
pts: Some(5),
|
|
|
|
dts: None,
|
|
|
|
duration: Some(2),
|
|
|
|
offset: 4,
|
|
|
|
offset_end: 6,
|
2022-05-05 10:40:07 +00:00
|
|
|
flags: "",
|
2018-07-16 19:46:10 +00:00
|
|
|
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();
|
2020-10-27 17:27:16 +00:00
|
|
|
assert_eq!(buffer.pts(), Some(ClockTime::NSECOND));
|
|
|
|
assert_eq!(buffer.dts(), None);
|
2018-07-16 19:46:10 +00:00
|
|
|
{
|
|
|
|
let data = buffer.map_readable().unwrap();
|
|
|
|
assert_eq!(data.as_slice(), vec![1, 2, 3, 4].as_slice());
|
|
|
|
}
|
|
|
|
|
|
|
|
let buffer = iter.next().unwrap();
|
2020-10-27 17:27:16 +00:00
|
|
|
assert_eq!(buffer.pts(), Some(5 * ClockTime::NSECOND));
|
2018-07-16 19:46:10 +00:00
|
|
|
{
|
|
|
|
let data = buffer.map_readable().unwrap();
|
|
|
|
assert_eq!(data.as_slice(), vec![5, 6].as_slice());
|
|
|
|
}
|
|
|
|
}
|
2018-07-29 16:18:24 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serde_roundtrip() {
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::Buffer;
|
2018-07-29 16:18:24 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2018-07-29 16:18:24 +00:00
|
|
|
|
|
|
|
let mut buffer_list = BufferList::new();
|
|
|
|
{
|
|
|
|
let buffer_list = buffer_list.get_mut().unwrap();
|
|
|
|
|
2018-12-26 07:50:44 +00:00
|
|
|
let mut buffer = Buffer::from_slice(vec![1, 2, 3, 4]);
|
2018-07-29 16:18:24 +00:00
|
|
|
{
|
|
|
|
let buffer = buffer.get_mut().unwrap();
|
2020-10-27 17:27:16 +00:00
|
|
|
buffer.set_pts(ClockTime::NSECOND);
|
2018-07-29 16:18:24 +00:00
|
|
|
buffer.set_offset(0);
|
|
|
|
buffer.set_offset_end(4);
|
2020-10-27 17:27:16 +00:00
|
|
|
buffer.set_duration(4 * ClockTime::NSECOND);
|
2018-07-29 16:18:24 +00:00
|
|
|
}
|
|
|
|
buffer_list.add(buffer);
|
|
|
|
|
2018-12-26 07:50:44 +00:00
|
|
|
let mut buffer = Buffer::from_slice(vec![5, 6]);
|
2018-07-29 16:18:24 +00:00
|
|
|
{
|
|
|
|
let buffer = buffer.get_mut().unwrap();
|
2020-10-27 17:27:16 +00:00
|
|
|
buffer.set_pts(5 * ClockTime::NSECOND);
|
2018-07-29 16:18:24 +00:00
|
|
|
buffer.set_offset(4);
|
|
|
|
buffer.set_offset_end(6);
|
2020-10-27 17:27:16 +00:00
|
|
|
buffer.set_duration(2 * ClockTime::NSECOND);
|
2018-07-29 16:18:24 +00:00
|
|
|
}
|
|
|
|
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();
|
2020-10-27 17:27:16 +00:00
|
|
|
assert_eq!(buffer.pts(), Some(ClockTime::NSECOND));
|
|
|
|
assert_eq!(buffer.dts(), None);
|
2018-07-29 16:18:24 +00:00
|
|
|
{
|
|
|
|
let data = buffer.map_readable().unwrap();
|
|
|
|
assert_eq!(data.as_slice(), vec![1, 2, 3, 4].as_slice());
|
|
|
|
}
|
|
|
|
|
|
|
|
let buffer = iter.next().unwrap();
|
2020-10-27 17:27:16 +00:00
|
|
|
assert_eq!(buffer.pts(), Some(5 * ClockTime::NSECOND));
|
2018-07-29 16:18:24 +00:00
|
|
|
{
|
|
|
|
let data = buffer.map_readable().unwrap();
|
|
|
|
assert_eq!(data.as_slice(), vec![5, 6].as_slice());
|
|
|
|
}
|
|
|
|
}
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|