webrtc icon indicating copy to clipboard operation
webrtc copied to clipboard

[Interceptor] add example of interceptor

Open rainliu opened this issue 3 years ago • 2 comments

rainliu avatar Oct 23 '21 18:10 rainliu

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?

Pascualex avatar Dec 15 '21 22:12 Pascualex

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:

  • get_interceptor_registry() is a placeholder function assumed to be provided by the crate to obtain a reference to the interceptor registry. This function may or may not be directly accessible outside the crate, depending on how the API is designed.
  • MyRtpInterceptor is a custom struct that implements the RtpInterceptor trait (the actual trait name might be different in your specific API).
  • You create an instance of MyRtpInterceptor and pass it to the function create_rtp_sender_with_interceptor(), which internally registers the interceptor with the registry obtained from the crate and then creates the RTP sender.

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

sahilwep avatar Mar 16 '24 16:03 sahilwep