webrtc-rs / webrtc

A pure Rust implementation of WebRTC
https://webrtc.rs
Apache License 2.0
4.04k stars 359 forks source link

[Interceptor] add example of interceptor #123

Open rainliu opened 2 years ago

Pascualex commented 2 years ago

Hi, I'm trying to use the ORTC API to create an RTCRtpSender, but the ORT example only covers the RTCDtlsTransport.

The API requires me to pass an interceptor to new_rtp_sender(...), but I don't see the right way to create one. The API has an interceptor_registry, but is only accesible by the crate.

Do I need to keep a reference to the registry outside the API? Is this what this example should cover?

sahilwep commented 6 months ago

If the ORTC API requires you to pass an interceptor to new_rtp_sender(), and the interceptor registry is only accessible within the crate, you likely won't be able to directly create an interceptor outside the crate. In such a scenario, you would need to use the provided mechanism to obtain access to the interceptor registry.

Here's how you might handle this situation in Rust:

// Assuming there's a function provided by the crate to obtain the interceptor registry
fn get_interceptor_registry() -> InterceptorRegistry {
    // This function may be provided by the crate and may not be directly accessible outside
    // If it's not provided, you would need to use whatever mechanism the API offers to access the registry
    // This is just a placeholder example
    // Example:
    crate::get_interceptor_registry()
}

// Define a struct for your interceptor
struct MyRtpInterceptor {}

// Implement the required trait for the interceptor
impl RtpInterceptor for MyRtpInterceptor {
    // Implement the methods required by the trait
    fn intercept_outgoing_rtp(&self, packet: &mut RtpPacket) {
        // Modify the outgoing RTP packet if needed
        // For example:
        // packet.payload[0] = packet.payload[0] ^ 0xFF; // Invert the first byte
    }

    fn intercept_incoming_rtp(&self, packet: &mut RtpPacket) {
        // Modify the incoming RTP packet if needed
    }
}

// Function to create an RTCRtpSender with the provided interceptor
fn create_rtp_sender_with_interceptor(interceptor: Box<dyn RtpInterceptor>) -> RTCRtpSender {
    // Obtain a reference to the interceptor registry
    let interceptor_registry = get_interceptor_registry();

    // Register the interceptor with the registry
    interceptor_registry.register_interceptor(interceptor);

    // Now you can create the RTP sender using the interceptor
    new_rtp_sender(/* other parameters */)
}

// Usage example
fn main() {
    // Create an instance of your interceptor
    let interceptor = Box::new(MyRtpInterceptor {});

    // Create an RTCRtpSender with the provided interceptor
    let sender = create_rtp_sender_with_interceptor(interceptor);
}

In this example:

Please adjust the code according to the actual names and signatures provided by the ORTC API you are using.