10 releases

0.1.3 Sep 6, 2024
0.1.2 Jun 13, 2024
0.1.1 Feb 14, 2024
0.1.0 Jun 30, 2023
0.0.0 Jun 30, 2019

#195 in Network programming

Download history 1760/week @ 2024-08-19 1283/week @ 2024-08-26 1676/week @ 2024-09-02 1866/week @ 2024-09-09 1889/week @ 2024-09-16 2446/week @ 2024-09-23 2444/week @ 2024-09-30 2278/week @ 2024-10-07 3078/week @ 2024-10-14 3220/week @ 2024-10-21 3383/week @ 2024-10-28 2610/week @ 2024-11-04 3027/week @ 2024-11-11 2592/week @ 2024-11-18 2695/week @ 2024-11-25 2488/week @ 2024-12-02

11,036 downloads per month
Used in 5 crates (3 directly)

MIT license

270KB
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

~3–4.5MB
~86K SLoC