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::EcmaArray,
70    ///     }
71    /// };
72    ///
73    /// let mut buffer = ByteBuffer::default();
74    /// buffer.encode(&EcmaArray::default());
75    /// assert!(Decoder::<SetPlaylist>::decode(&mut buffer).is_ok());
76    ///
77    /// let mut buffer = ByteBuffer::default();
78    /// assert!(Decoder::<SetPlaylist>::decode(&mut buffer).is_err())
79    /// ```
80    ///
81    /// [`InsufficientBufferLength`]: crate::byte_buffer::InsufficientBufferLength
82    /// [`InconsistentMarker`]: crate::messages::amf::InconsistentMarker
83    fn decode(&mut self) -> IOResult<SetPlaylist> {
84        Decoder::<Null>::decode(self)?;
85        let playpaths: EcmaArray = self.decode()?;
86        Ok(SetPlaylist(playpaths))
87    }
88}
89
90impl Encoder<SetPlaylist> for ByteBuffer {
91    /// Encodes a SetPlaylist command into bytes.
92    fn encode(&mut self, set_playlist: &SetPlaylist) {
93        self.encode(&Null);
94        self.encode(set_playlist.get_playpaths());
95    }
96}
97
98#[cfg(test)]
99mod tests {
100    use crate::{
101        ecma_array,
102        messages::amf::v0::AmfString,
103    };
104    use super::*;
105
106    #[test]
107    fn decode_set_playlist() {
108        let mut buffer = ByteBuffer::default();
109        buffer.encode(
110            &ecma_array!(
111                "0" => AmfString::default()
112            )
113        );
114        let result: IOResult<SetPlaylist> = buffer.decode();
115        assert!(result.is_ok());
116        let actual = result.unwrap();
117        let expected = SetPlaylist::new(
118            ecma_array!(
119                "0" => AmfString::default()
120            )
121        );
122        assert_eq!(expected, actual)
123    }
124
125    #[test]
126    fn encode_set_playlist() {
127        let mut buffer = ByteBuffer::default();
128        let expected_playpaths = ecma_array!(
129            "0" => AmfString::default()
130        );
131        let expected = SetPlaylist::new(expected_playpaths.clone());
132        buffer.encode(&expected);
133        Decoder::<Null>::decode(&mut buffer).unwrap();
134        let actual_playpaths: EcmaArray = buffer.decode().unwrap();
135        assert_eq!(expected_playpaths, actual_playpaths)
136    }
137}