Skip to content

Conversation

@onorua
Copy link
Contributor

@onorua onorua commented Jul 13, 2025

This implementation adds comprehensive KLV (Key-Length-Value) metadata support to MediaMTX's WebRTC functionality. KLV metadata is transmitted via WebRTC data channels, allowing real-time delivery of telemetry and metadata alongside video and audio streams.

Implementation Details

Data Channel Configuration

dataChannelInit := &webrtc.DataChannelInit{
    Ordered:        &[]bool{true}[0],  // Ensure ordered delivery
    MaxRetransmits: &[]uint16{3}[0],   // Allow some retransmissions
}

KLV Format Detection Logic

// Check for Generic format with KLV RTPMap
if genericFmt, ok := forma.(*format.Generic); ok {
    if genericFmt.RTPMap() == "KLV/90000" {
        klvFormat = genericFmt
        klvMedia = media
        break
    }
}

// Also check for internal KLV format
if forma.Codec() == "KLV" {
    klvFormat = forma
    klvMedia = media
    break
}

JavaScript KLV Data Handling

dataChannel.onmessage = (event) => {
    if (this.conf.onKLVData !== undefined) {
        const klvData = new Uint8Array(event.data);
        this.conf.onKLVData(klvData);
    }
};

Usage

Basic WebRTC with KLV

new MediaMTXWebRTCReader({
    url: new URL('whep', window.location.href),
    onTrack: (evt) => {
        video.srcObject = evt.streams[0];
    },
    onKLVData: (klvData) => {
        console.log('KLV data received:', klvData);
        // Process KLV metadata
    }
});

@codecov
Copy link

codecov bot commented Jul 13, 2025

Codecov Report

Attention: Patch coverage is 40.65041% with 73 lines in your changes missing coverage. Please review.

Project coverage is 54.55%. Comparing base (97d2fff) to head (7808bce).
Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
internal/protocols/webrtc/from_stream.go 37.17% 41 Missing and 8 partials ⚠️
internal/protocols/webrtc/peer_connection.go 46.66% 20 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4722      +/-   ##
==========================================
- Coverage   54.61%   54.55%   -0.06%     
==========================================
  Files         209      210       +1     
  Lines       21785    21913     +128     
==========================================
+ Hits        11897    11955      +58     
- Misses       8978     9037      +59     
- Partials      910      921      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@onorua onorua force-pushed the webrtc-klv branch 3 times, most recently from eb0e47b to c24827f Compare July 18, 2025 10:26
This implementation adds comprehensive KLV (Key-Length-Value) metadata support
to MediaMTX's WebRTC functionality. KLV metadata is transmitted via WebRTC data
channels, allowing real-time delivery of telemetry and metadata alongside video and audio streams.
@llamafilm
Copy link

llamafilm commented Oct 27, 2025

How is this meant to be used? I don't see anything in mediamtx.yml about how to provide this data.
Edit: never mind! Looking at older issues I see the KLV data comes from the source stream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants