Skip to content
HN On Hacker News ↗

Pion Interceptor: WebRTC for Hackers

▲ 24 points • 7 comments • by Sean-Der • 5d ago • HN discussion ↗

Pangram verdict · v3.3

We believe that this entire text is human-written.

0 %

AI likelihood · overall

Human
100% human-written 0% AI-generated
SEGMENTS · HUMAN 7 of 7
SEGMENTS · AI 0 of 7
WORD COUNT 771
PEAK AI % 0% · §6
Analyzed
Nov 22
backend: pangram/v3.3
Segments scanned
7 windows
avg 110 words each
Distribution
100 / 0%
human / AI fraction
Verdict
Human
Pangram v3.3

Article text · 771 words · 7 segments analyzed

Human AI-generated
§1 Human · 0%

Pion was started with unique use cases in mind. Existing WebRTC software was very good at handling popular use cases. It was difficult to build unique/niche things with it though. This idea has always been at the core of Pion. Pion Interceptor was designed with the idea that we make it easy for developers to build their cool ideas. Without the frustration of forking/modifying Pion. Prior Art Pion's Interceptor design was heavily influenced by GStreamer Pipelines. With GStreamer pipelines you have a pipeline of elements that modify media. Things like resizing, croping or rotating media. A simple visualization like this. Video source --> Resize --> Crop --> Rotate --> Video output We wanted the same for Pion. Provide a library of elements for users to build pipelines. The default interceptors would give a good experience. Or you can bring your own to have custom behavior if you need it Interceptor API An Interceptor is started when Bind is called on it. Bind passes a StreamInfo that contains all the metadata about the stream. When a Interceptor is no longer Unbind is called. Then each RTP/RTCP packet is passed through in the Reader/Writer methods. This allows a user to process incoming/outgoing packets. The following is the full API as generated by go doc type Interceptor interface { // BindRTCPReader lets you modify any incoming RTCP packets. It is called once per sender/receiver, however this might // change in the future. The returned method will be called once per packet batch. BindRTCPReader(reader RTCPReader) RTCPReader // BindRTCPWriter lets you modify any outgoing RTCP packets. It is called once per PeerConnection. The returned method // will be called once per packet batch. BindRTCPWriter(writer RTCPWriter) RTCPWriter // BindLocalStream lets you modify any outgoing RTP packets. It is called once for per LocalStream. The returned method // will be called once per rtp packet. BindLocalStream(info *StreamInfo, writer RTPWriter) RTPWriter // UnbindLocalStream is called when the Stream is removed. It can be used to clean up any data related to that track. UnbindLocalStream(info *StreamInfo) // BindRemoteStream lets you modify any incoming RTP packets. // It is called once for per RemoteStream. The returned method // will be called once per rtp packet. BindRemoteStream(info *StreamInfo, reader RTPReader) RTPReader // UnbindRemoteStream is called when the Stream is removed. It can be used to clean up any data related to that track. UnbindRemoteStream(info *StreamInfo) io.Closer }NACK Example A NACK is how WebRTC handles packet loss. A receiver sends a NACK so that the sender knows to re-send a packet. The following is a minimal example to show how it could be implemented. type nackGenerator struct { interceptor.NoOp writer interceptor.RTCPWriter } func (g *nackGenerator) BindRTCPWriter(writer interceptor.RTCPWriter) interceptor.RTCPWriter { g.writer = writer return writer } func (g *nackGenerator) BindRemoteStream(info *interceptor.StreamInfo, reader interceptor.RTPReader) interceptor.RTPReader { var last uint16 seen := false // Read incoming RTP Packets return interceptor.RTPReaderFunc(func(b []byte, a interceptor.Attributes) (int, interceptor.Attributes, error) { n, a, err := reader.Read(b, a) if err != nil { return n, a, err } header, err := a.GetRTPHeader(b[:n]) if err != nil { return n, a, err } // Check if we have a gap in packets seen if seen && int16(header.SequenceNumber-last) > 1 { // Send a NACK back to sender _, err = g.writer.Write([]rtcp.Packet{&rtcp.TransportLayerNack{ MediaSSRC: info.SSRC, Nacks: []rtcp.NackPair{ {PacketID: last + 1}, }, }}, nil) } last, seen = header.SequenceNumber, true return n, a, err }) } This simple NACK implementation detects gaps between packets and sends NACKs back to senders to fix a stream with packet loss. Pre-Made Interceptors Today we provide these pre-made interceptors: NACK Generator/Responder - Requests missing packets and retransmits them. Sender and Receiver Reports - Generates RTCP reports about sent and received media. Transport-Wide Congestion Control - Adds transport sequence numbers and generates congestion feedback. Congestion Control Feedback - Generates congestion feedback reports defined by RFC 8888. RTP Feedback - Converts TWCC and CCFB feedback into per-packet reports. Pacing - Spreads outgoing packets over time to reduce bursts.

§2 Human · 0%

Jitter Buffer - Buffers and reorders incoming RTP packets.

§3 Human · 0%

Packet Dump - Logs incoming and outgoing RTP/RTCP packets.

§4 Human · 0%

Stats - Collects RTP stream statistics. Interval PLI - Requests keyframes at a fixed interval. FlexFEC - Generates repair packets for forward error correction.

§5 Human · 0%

Foot Guns When building your interceptors keep these in mind. Order Matters - Register the stats interceptor before the NACK responder so it can count retransmitted RTP packets.

§6 Human · 0%

Registering stats after the responder means retransmissions bypass it. Incoming NACKs are counted in either order.

§7 Human · 0%

Don't forget to read - Interceptors are fired by you calling Read. If your application doesn't do a Read things like Stats or NACKs won't happen.