janus_plugin/
rtcp.rs

1/// Utilities to manipulate RTCP packets. For reference, see:
2///
3/// <https://tools.ietf.org/html/rfc3605> (RTCP)
4/// <https://tools.ietf.org/html/rfc4585> (definition of PLI, others)
5/// <https://tools.ietf.org/html/rfc5104> (definition of FIR, others)
6/// <https://tools.ietf.org/html/draft-alvestrand-rmcat-remb-03> (definition of REMB)
7
8use janus_plugin_sys as ffi;
9
10/// Returns whether this RTCP packet is a FIR packet.
11pub fn has_fir(packet: &[i8]) -> bool {
12    unsafe { ffi::rtcp::janus_rtcp_has_fir(packet.as_ptr() as *mut _, packet.len() as i32) == 1 }
13}
14
15/// Returns whether this RTCP packet is a PLI packet.
16pub fn has_pli(packet: &[i8]) -> bool {
17    unsafe { ffi::rtcp::janus_rtcp_has_pli(packet.as_ptr() as *mut _, packet.len() as i32) == 1 }
18}
19
20/// If this RTCP packet is an REMB packet, returns the bitrate it contains; else None.
21pub fn get_remb(packet: &[i8]) -> Option<u32> {
22    unsafe {
23        match ffi::rtcp::janus_rtcp_get_remb(packet.as_ptr() as *mut _, packet.len() as i32) {
24            0 => None,
25            n => Some(n)
26        }
27    }
28}
29
30/// Increments the given sequence number, then allocates and writes a new FIR packet with the new sequence number.
31pub fn gen_fir(seq: &mut i32) -> Vec<i8> {
32    let mut packet = Vec::with_capacity(20);
33    let result = unsafe { ffi::rtcp::janus_rtcp_fir(packet.as_mut_ptr(), 20, seq) };
34    match result {
35        // errors should only be the result of invalid inputs to janus_rtcp_fir
36        err if err < 0 => unreachable!(format!("Error generating FIR packet (code {}) :(", err)),
37        len => {
38            unsafe { packet.set_len(len as usize) };
39            packet
40        }
41    }
42}
43
44/// Allocates and writes a new PLI packet.
45pub fn gen_pli() -> Vec<i8> {
46    let mut packet = Vec::with_capacity(12);
47    let result = unsafe { ffi::rtcp::janus_rtcp_pli(packet.as_mut_ptr(), 12) };
48    match result {
49        // errors should only be the result of invalid inputs to janus_rtcp_pli
50        err if err < 0 => unreachable!(format!("Error generating PLI packet (code {}) :(", err)),
51        len => {
52            unsafe { packet.set_len(len as usize) };
53            packet
54        }
55    }
56}
57
58/// Allocates and writes a new REMB packet with the given bitrate.
59pub fn gen_remb(bitrate: u32) -> Vec<i8> {
60    let mut packet = Vec::with_capacity(24);
61    let result = unsafe { ffi::rtcp::janus_rtcp_remb(packet.as_mut_ptr(), 24, bitrate) };
62    match result {
63        // errors should only be the result of invalid inputs to janus_rtcp_remb
64        err if err < 0 => unreachable!(format!("Error generating REMB packet (code {}).", err)),
65        len => {
66            unsafe { packet.set_len(len as usize) };
67            packet
68        }
69    }
70}