8 releases

0.1.1 Feb 14, 2024
0.1.0 Jun 30, 2023
0.0.5 Feb 2, 2023
0.0.4 Oct 27, 2022
0.0.0 Jun 30, 2019

#162 in Network programming

Download history 1069/week @ 2024-01-24 396/week @ 2024-01-31 1287/week @ 2024-02-07 1758/week @ 2024-02-14 1909/week @ 2024-02-21 1550/week @ 2024-02-28 1910/week @ 2024-03-06 1130/week @ 2024-03-13 1266/week @ 2024-03-20 1248/week @ 2024-03-27 1955/week @ 2024-04-03 1846/week @ 2024-04-10 2096/week @ 2024-04-17 2253/week @ 2024-04-24 1409/week @ 2024-05-01 1432/week @ 2024-05-08

7,558 downloads per month
Used in 4 crates (2 directly)

MIT license

260KB
6.5K SLoC

rtsp-types crates.io Actions Status docs.rs

Crate for handling RTSP (RFC 7826) messages, including a parser and serializer and support for parsing and generating well-known headers.

See the documentation for details.

LICENSE

rtsp-types is licensed under the MIT license (LICENSE or http://opensource.org/licenses/MIT).

Contribution

Any kinds of contributions are welcome as a pull request.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in rtsp-types by you shall be licensed under the MIT license as above, without any additional terms or conditions.


lib.rs:

Crate for handling RTSP (RFC 7826) messages, including a parser and serializer and support for parsing and generating well-known headers.

Creating an OPTIONS request

let request = rtsp_types::Request::builder(
        rtsp_types::Method::Options,
        rtsp_types::Version::V2_0
    )
    .header(rtsp_types::headers::CSEQ, "1")
    .empty();

This request contains an empty body.

Creating a SET_PARAMETER request with a request body

let request = rtsp_types::Request::builder(
        rtsp_types::Method::SetParameter,
        rtsp_types::Version::V2_0
    )
    .request_uri(rtsp_types::Url::parse("rtsp://example.com/test").expect("Invalid URI"))
    .header(rtsp_types::headers::CSEQ, "2")
    .header(rtsp_types::headers::CONTENT_TYPE, "text/parameters")
    .build(Vec::from(&b"barparam: barstuff"[..]));

The body is passed to the build() function and a Content-Length header is automatically inserted into the request headers.

Creating an OK response

let response = rtsp_types::Response::builder(
        rtsp_types::Version::V2_0,
        rtsp_types::StatusCode::Ok,
    )
    .header(rtsp_types::headers::CSEQ, "1")
    .empty();

This response contains an empty body. A non-empty body can be added in the same way as for requests.

Creating a data message

let data = rtsp_types::Data::new(0, vec![0, 1, 2, 3, 4]);

This creates a new data message for channel id 0 with the given Vec<u8>.

Parsing an RTSP message

let data = b"OPTIONS * RTSP/2.0\r\n\
             CSeq: 1\r\n\
             Supported: play.basic, play.scale\r\n\
             User-Agent: PhonyClient/1.2\r\n\
             \r\n";

let (message, consumed): (rtsp_types::Message<Vec<u8>>, _) =
    rtsp_types::Message::parse(data).expect("Failed to parse data");

assert_eq!(consumed, data.len());
match message {
    rtsp_types::Message::Request(ref request) => {
        assert_eq!(request.method(), rtsp_types::Method::Options);
    },
    _ => unreachable!(),
}

Messages can be parsed from any AsRef<[u8]> and to any borrowed or owned body type that implements From<&[u8]>.

More details about parsing can be found at Message::parse.

Serializing an RTSP message

let request = rtsp_types::Request::builder(
        rtsp_types::Method::SetParameter,
        rtsp_types::Version::V2_0
    )
    .request_uri(rtsp_types::Url::parse("rtsp://example.com/test").expect("Invalid URI"))
    .header(rtsp_types::headers::CSEQ, "2")
    .header(rtsp_types::headers::CONTENT_TYPE, "text/parameters")
    .build(Vec::from(&b"barparam: barstuff"[..]));

 let mut data = Vec::new();
 request.write(&mut data).expect("Failed to serialize request");

 assert_eq!(
    data,
    b"SET_PARAMETER rtsp://example.com/test RTSP/2.0\r\n\
      Content-Length: 18\r\n\
      Content-Type: text/parameters\r\n\
      CSeq: 2\r\n\
      \r\n\
      barparam: barstuff",
 );

Serializing can be done to any type that implements std::io::Write.

More details about serializing can be found at Message::write.

Dependencies

~3MB
~81K SLoC