CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-webrtc-adapter

A shim to insulate apps from WebRTC spec changes and browser prefix differences

Overall
score

98%

Overview
Eval results
Files

task.mdevals/scenario-1/

WebRTC Perfect Negotiation Helper

Build a utility library that helps implement the "perfect negotiation" pattern for WebRTC peer connections in a cross-browser compatible way. Perfect negotiation is a design pattern that allows two peers to negotiate connection details symmetrically, without explicitly designating one peer as "caller" and the other as "callee".

Background

In WebRTC, the negotiationneeded event fires when the peer connection needs to renegotiate (e.g., when adding/removing tracks). However, certain browser implementations have timing issues where spurious negotiationneeded events can fire during the middle of an ongoing offer/answer exchange, which can cause negotiation to fail or enter an unstable state.

A robust implementation should only act on negotiationneeded events when the connection is in a stable state and ready for negotiation.

Requirements

Your library should provide a utility function that wraps a RTCPeerConnection instance to ensure reliable negotiation event handling. Specifically:

  1. The function should accept a RTCPeerConnection instance as input
  2. It should set up proper event handling that prevents negotiation attempts when the signaling state is not 'stable'
  3. The implementation should ensure that negotiationneeded events that fire during ongoing offer/answer exchanges are properly handled or suppressed
  4. The solution should work correctly with the webrtc-adapter library to provide cross-browser compatibility

Test Cases

@generates

  • When negotiationneeded fires and signalingState is 'stable', the negotiation callback is invoked @test
  • When negotiationneeded fires and signalingState is 'have-local-offer', the negotiation callback is NOT invoked @test
  • When negotiationneeded fires and signalingState is 'have-remote-offer', the negotiation callback is NOT invoked @test
  • The helper correctly integrates with webrtc-adapter for cross-browser compatibility @test

API

/**
 * Sets up reliable negotiation handling for a peer connection.
 * Ensures negotiation only proceeds when the connection is in a stable state.
 *
 * @param {RTCPeerConnection} pc - The peer connection to wrap
 * @param {Function} onNegotiationNeeded - Callback to invoke when negotiation is safe
 * @returns {void}
 */
function setupReliableNegotiation(pc, onNegotiationNeeded) {
  // IMPLEMENTATION HERE
}

module.exports = {
  setupReliableNegotiation
};

Dependencies { .dependencies }

webrtc-adapter { .dependency }

Provides cross-browser compatibility for WebRTC APIs

Install with Tessl CLI

npx tessl i tessl/npm-webrtc-adapter

tile.json