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 serde::de::{Deserialize, Deserializer};
|
2018-07-16 20:05:50 +00:00
|
|
|
use serde::ser;
|
2018-07-22 14:55:24 +00:00
|
|
|
use serde::ser::{Serialize, SerializeStruct, Serializer};
|
|
|
|
use serde_bytes::{ByteBuf, Bytes};
|
2018-07-16 19:46:10 +00:00
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::Buffer;
|
|
|
|
use crate::BufferFlags;
|
|
|
|
use crate::BufferRef;
|
|
|
|
use crate::ClockTime;
|
2018-07-16 19:46:10 +00:00
|
|
|
|
2018-07-29 20:26:52 +00:00
|
|
|
// TODO: try `Either<ByteBuf, Bytes>` to merge the base reprensentations for ser and de
|
|
|
|
// while avoiding unneeded copy
|
|
|
|
|
2018-07-16 19:46:10 +00:00
|
|
|
impl<'a> Serialize for BufferRef {
|
|
|
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
|
|
|
let mut buffer = serializer.serialize_struct("Buffer", 6)?;
|
|
|
|
buffer.serialize_field("pts", &self.get_pts())?;
|
|
|
|
buffer.serialize_field("dts", &self.get_dts())?;
|
|
|
|
buffer.serialize_field("duration", &self.get_duration())?;
|
|
|
|
buffer.serialize_field("offset", &self.get_offset())?;
|
|
|
|
buffer.serialize_field("offset_end", &self.get_offset_end())?;
|
|
|
|
buffer.serialize_field("flags", &self.get_flags())?;
|
|
|
|
{
|
2018-07-16 20:05:50 +00:00
|
|
|
let data = self
|
|
|
|
.map_readable()
|
2019-12-17 19:00:42 +00:00
|
|
|
.map_err(|_| ser::Error::custom("Couldn't map `buffer` as readable"))?;
|
2018-07-16 19:46:10 +00:00
|
|
|
buffer.serialize_field("buffer", &Bytes::new(data.as_slice()))?;
|
|
|
|
}
|
|
|
|
buffer.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Serialize for Buffer {
|
|
|
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
|
|
|
self.as_ref().serialize(serializer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
#[derive(serde::Deserialize)]
|
2018-07-16 19:46:10 +00:00
|
|
|
struct BufferDe {
|
|
|
|
pts: ClockTime,
|
|
|
|
dts: ClockTime,
|
|
|
|
duration: ClockTime,
|
|
|
|
offset: u64,
|
|
|
|
offset_end: u64,
|
|
|
|
flags: BufferFlags,
|
|
|
|
buffer: ByteBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<BufferDe> for Buffer {
|
2018-07-16 19:57:17 +00:00
|
|
|
fn from(buf_de: BufferDe) -> Self {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2018-12-26 07:50:44 +00:00
|
|
|
let mut buffer = Buffer::from_mut_slice(buf_de.buffer.to_vec());
|
2018-07-16 19:46:10 +00:00
|
|
|
{
|
|
|
|
let buffer = buffer.get_mut().unwrap();
|
|
|
|
buffer.set_pts(buf_de.pts);
|
|
|
|
buffer.set_dts(buf_de.dts);
|
|
|
|
buffer.set_duration(buf_de.duration);
|
|
|
|
buffer.set_offset(buf_de.offset);
|
|
|
|
buffer.set_offset_end(buf_de.offset_end);
|
|
|
|
buffer.set_flags(buf_de.flags);
|
|
|
|
}
|
|
|
|
buffer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for Buffer {
|
|
|
|
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2018-07-22 14:55:24 +00:00
|
|
|
BufferDe::deserialize(deserializer).map(|buffer_de| buffer_de.into())
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::Buffer;
|
|
|
|
use crate::BufferFlags;
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialize() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2018-07-16 19:46:10 +00:00
|
|
|
|
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();
|
|
|
|
buffer.set_pts(1.into());
|
|
|
|
buffer.set_offset(3);
|
|
|
|
buffer.set_offset_end(4);
|
|
|
|
buffer.set_duration(5.into());
|
2020-06-29 21:12:46 +00:00
|
|
|
buffer.set_flags(BufferFlags::LIVE | BufferFlags::DISCONT);
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 19:35:43 +00:00
|
|
|
let pretty_config = ron::ser::PrettyConfig::new().with_new_line("".to_string());
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
let res = ron::ser::to_string_pretty(&buffer, pretty_config);
|
|
|
|
assert_eq!(
|
2018-07-22 14:55:24 +00:00
|
|
|
Ok(concat!(
|
|
|
|
"(",
|
|
|
|
" pts: Some(1),",
|
|
|
|
" dts: None,",
|
|
|
|
" duration: Some(5),",
|
|
|
|
" offset: 3,",
|
|
|
|
" offset_end: 4,",
|
|
|
|
" flags: (",
|
2020-06-29 21:12:46 +00:00
|
|
|
" bits: 80,",
|
2018-07-22 14:55:24 +00:00
|
|
|
" ),",
|
|
|
|
" buffer: \"AQIDBA==\",",
|
|
|
|
")"
|
2018-10-08 12:02:23 +00:00
|
|
|
)
|
|
|
|
.to_owned()),
|
2018-07-16 19:46:10 +00:00
|
|
|
res
|
|
|
|
);
|
|
|
|
|
|
|
|
let res = serde_json::to_string(&buffer).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
concat!(
|
|
|
|
"{",
|
2018-07-22 14:55:24 +00:00
|
|
|
"\"pts\":1,",
|
|
|
|
"\"dts\":null,",
|
|
|
|
"\"duration\":5,",
|
|
|
|
"\"offset\":3,",
|
|
|
|
"\"offset_end\":4,",
|
2020-06-29 21:12:46 +00:00
|
|
|
"\"flags\":{\"bits\":80},",
|
2018-07-22 14:55:24 +00:00
|
|
|
"\"buffer\":[1,2,3,4]",
|
2018-07-16 19:46:10 +00:00
|
|
|
"}"
|
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_ron = r#"
|
|
|
|
(
|
|
|
|
pts: Some(1),
|
|
|
|
dts: None,
|
|
|
|
duration: Some(5),
|
|
|
|
offset: 3,
|
|
|
|
offset_end: 4,
|
|
|
|
flags: (
|
2020-06-29 21:12:46 +00:00
|
|
|
bits: 80,
|
2018-07-16 19:46:10 +00:00
|
|
|
),
|
|
|
|
buffer: "AQIDBA==",
|
|
|
|
)
|
|
|
|
"#;
|
|
|
|
let buffer: Buffer = ron::de::from_str(buffer_ron).unwrap();
|
|
|
|
assert_eq!(buffer.get_pts(), 1.into());
|
|
|
|
assert_eq!(buffer.get_dts(), None.into());
|
|
|
|
assert_eq!(buffer.get_offset(), 3);
|
|
|
|
assert_eq!(buffer.get_offset_end(), 4);
|
|
|
|
assert_eq!(buffer.get_duration(), 5.into());
|
2020-06-29 21:12:46 +00:00
|
|
|
assert_eq!(buffer.get_flags(), BufferFlags::LIVE | BufferFlags::DISCONT);
|
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_json = r#"
|
|
|
|
{
|
|
|
|
"pts":1,
|
|
|
|
"dts":null,
|
|
|
|
"duration":5,
|
|
|
|
"offset":3,
|
|
|
|
"offset_end":4,
|
2020-06-29 21:12:46 +00:00
|
|
|
"flags":{"bits":80},
|
2018-07-16 19:46:10 +00:00
|
|
|
"buffer":[1,2,3,4]
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
let buffer: Buffer = serde_json::from_str(buffer_json).unwrap();
|
|
|
|
assert_eq!(buffer.get_pts(), 1.into());
|
|
|
|
assert_eq!(buffer.get_dts(), None.into());
|
|
|
|
assert_eq!(buffer.get_offset(), 3);
|
|
|
|
assert_eq!(buffer.get_offset_end(), 4);
|
|
|
|
assert_eq!(buffer.get_duration(), 5.into());
|
2020-06-29 21:12:46 +00:00
|
|
|
assert_eq!(buffer.get_flags(), BufferFlags::LIVE | BufferFlags::DISCONT);
|
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());
|
|
|
|
}
|
|
|
|
}
|
2018-07-29 16:18:24 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serde_roundtrip() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2018-07-29 16:18:24 +00:00
|
|
|
|
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();
|
|
|
|
buffer.set_pts(1.into());
|
|
|
|
buffer.set_offset(3);
|
|
|
|
buffer.set_offset_end(4);
|
|
|
|
buffer.set_duration(5.into());
|
2020-06-29 21:12:46 +00:00
|
|
|
buffer.set_flags(BufferFlags::LIVE | BufferFlags::DISCONT);
|
2018-07-29 16:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ron
|
|
|
|
let buffer_ser = ron::ser::to_string(&buffer).unwrap();
|
|
|
|
let buffer_de: Buffer = ron::de::from_str(buffer_ser.as_str()).unwrap();
|
|
|
|
assert_eq!(buffer_de.get_pts(), buffer.get_pts());
|
|
|
|
assert_eq!(buffer_de.get_dts(), buffer.get_dts());
|
|
|
|
assert_eq!(buffer_de.get_offset(), buffer.get_offset());
|
|
|
|
assert_eq!(buffer_de.get_offset_end(), buffer.get_offset_end());
|
|
|
|
assert_eq!(buffer_de.get_duration(), buffer.get_duration());
|
|
|
|
assert_eq!(buffer_de.get_flags(), buffer.get_flags());
|
|
|
|
{
|
|
|
|
let data = buffer_de.map_readable().unwrap();
|
|
|
|
assert_eq!(data.as_slice(), vec![1, 2, 3, 4].as_slice());
|
|
|
|
}
|
|
|
|
}
|
2018-07-22 14:55:24 +00:00
|
|
|
}
|