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

#180 in Network programming

Download history 2577/week @ 2023-12-18 116/week @ 2023-12-25 395/week @ 2024-01-01 542/week @ 2024-01-08 391/week @ 2024-01-15 1142/week @ 2024-01-22 610/week @ 2024-01-29 834/week @ 2024-02-05 1439/week @ 2024-02-12 1959/week @ 2024-02-19 1790/week @ 2024-02-26 2018/week @ 2024-03-04 1249/week @ 2024-03-11 1195/week @ 2024-03-18 1115/week @ 2024-03-25 1919/week @ 2024-04-01

5,582 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
~78K SLoC