moq_transport/data/
track.rs

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
use crate::coding::{Decode, DecodeError, Encode, EncodeError};
use crate::data::ObjectStatus;

#[derive(Clone, Debug)]
pub struct TrackHeader {
	// The subscribe ID.
	pub subscribe_id: u64,

	// The track ID.
	pub track_alias: u64,

	// Publisher priority, where **smaller** values are sent first.
	pub publisher_priority: u8,
}

impl Decode for TrackHeader {
	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
		Ok(Self {
			subscribe_id: u64::decode(r)?,
			track_alias: u64::decode(r)?,
			publisher_priority: u8::decode(r)?,
		})
	}
}

impl Encode for TrackHeader {
	fn encode<W: bytes::BufMut>(&self, w: &mut W) -> Result<(), EncodeError> {
		self.subscribe_id.encode(w)?;
		self.track_alias.encode(w)?;
		self.publisher_priority.encode(w)?;

		Ok(())
	}
}

#[derive(Clone, Debug)]
pub struct TrackObject {
	pub group_id: u64,
	pub object_id: u64,
	pub size: usize,
	pub status: ObjectStatus,
}

impl Decode for TrackObject {
	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
		let group_id = u64::decode(r)?;

		let object_id = u64::decode(r)?;
		let size = usize::decode(r)?;

		// If the size is 0, then the status is sent explicitly.
		// Otherwise, the status is assumed to be 0x0 (Object).
		let status = if size == 0 {
			ObjectStatus::decode(r)?
		} else {
			ObjectStatus::Object
		};

		Ok(Self {
			group_id,
			object_id,
			size,
			status,
		})
	}
}

impl Encode for TrackObject {
	fn encode<W: bytes::BufMut>(&self, w: &mut W) -> Result<(), EncodeError> {
		self.group_id.encode(w)?;
		self.object_id.encode(w)?;
		self.size.encode(w)?;

		// If the size is 0, then the status is sent explicitly.
		// Otherwise, the status is assumed to be 0x0 (Object).
		if self.size == 0 {
			self.status.encode(w)?;
		}

		Ok(())
	}
}