sheave_core/messages/
set_playlist.rs

1use std::io::Result as IOResult;
2use crate::{
3    ByteBuffer,
4    Decoder,
5    Encoder,
6    messages::{
7        Channel,
8        ChunkData,
9        Command,
10        amf::v0::{
11            EcmaArray,
12            Null
13        },
14        headers::MessageType
15    },
16};
17
18/// The command to tell the Playlist of streams.
19#[derive(Debug, Clone, Default, PartialEq, Eq)]
20pub struct SetPlaylist(EcmaArray);
21
22impl SetPlaylist {
23    /// Constructs a SetPlaylist command.
24    pub fn new(playpaths: EcmaArray) -> Self {
25        Self(playpaths)
26    }
27
28    /// Gets the playlist.
29    pub fn get_playpaths(&self) -> &EcmaArray {
30        &self.0
31    }
32}
33
34impl From<SetPlaylist> for EcmaArray {
35    fn from(set_playlist: SetPlaylist) -> Self {
36        set_playlist.0
37    }
38}
39
40impl ChunkData for SetPlaylist {
41    const CHANNEL: Channel = Channel::Video;
42    const MESSAGE_TYPE: MessageType = MessageType::Command;
43}
44
45impl Command for SetPlaylist {}
46
47impl Decoder<SetPlaylist> for ByteBuffer {
48    /// Decodes bytes into a SetPlaylist command.
49    ///
50    /// # Errors
51    ///
52    /// * [`InsufficientBufferLength`]
53    ///
54    /// When some field misses.
55    ///
56    /// * [`InconsistentMarker`]
57    ///
58    /// When some value is inconsistent with its marker.
59    ///
60    /// # Examples
61    ///
62    /// ```rust
63    /// use sheave_core::{
64    ///     ByteBuffer,
65    ///     Decoder,
66    ///     Encoder,
67    ///     messages::{
68    ///         SetPlaylist,
69    ///         amf::v0::{
70    ///             Null,
71    ///             EcmaArray
72    ///         },
73    ///     }
74    /// };
75    ///
76    /// let mut buffer = ByteBuffer::default();
77    /// buffer.encode(&Null);
78    /// buffer.encode(&EcmaArray::default());
79    /// assert!(Decoder::<SetPlaylist>::decode(&mut buffer).is_ok());
80    ///
81    /// let mut buffer = ByteBuffer::default();
82    /// assert!(Decoder::<SetPlaylist>::decode(&mut buffer).is_err())
83    /// ```
84    ///
85    /// [`InsufficientBufferLength`]: crate::byte_buffer::InsufficientBufferLength
86    /// [`InconsistentMarker`]: crate::messages::amf::InconsistentMarker
87    fn decode(&mut self) -> IOResult<SetPlaylist> {
88        Decoder::<Null>::decode(self)?;
89        let playpaths: EcmaArray = self.decode()?;
90        Ok(SetPlaylist(playpaths))
91    }
92}
93
94impl Encoder<SetPlaylist> for ByteBuffer {
95    /// Encodes a SetPlaylist command into bytes.
96    fn encode(&mut self, set_playlist: &SetPlaylist) {
97        self.encode(&Null);
98        self.encode(set_playlist.get_playpaths());
99    }
100}
101
102#[cfg(test)]
103mod tests {
104    use crate::{
105        ecma_array,
106        messages::amf::v0::AmfString,
107    };
108    use super::*;
109
110    #[test]
111    fn decode_set_playlist() {
112        let mut buffer = ByteBuffer::default();
113        buffer.encode(&Null);
114        buffer.encode(
115            &ecma_array!(
116                "0" => AmfString::default()
117            )
118        );
119        let result: IOResult<SetPlaylist> = buffer.decode();
120        assert!(result.is_ok());
121        let actual = result.unwrap();
122        let expected = SetPlaylist::new(
123            ecma_array!(
124                "0" => AmfString::default()
125            )
126        );
127        assert_eq!(expected, actual)
128    }
129
130    #[test]
131    fn encode_set_playlist() {
132        let mut buffer = ByteBuffer::default();
133        let expected_playpaths = ecma_array!(
134            "0" => AmfString::default()
135        );
136        let expected = SetPlaylist::new(expected_playpaths.clone());
137        buffer.encode(&expected);
138        Decoder::<Null>::decode(&mut buffer).unwrap();
139        let actual_playpaths: EcmaArray = buffer.decode().unwrap();
140        assert_eq!(expected_playpaths, actual_playpaths)
141    }
142}