Crate rtsp_types

source ·
Expand description

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.

Re-exports§

Modules§

Structs§

Enums§