Skip to content

Repository files navigation

SDP Compact

npm version

Shorten WebRTC Session Description Protocol (SDP) based on Unified Plan SDP

Why?

A WebRTC SDP can remove some of the attributes to compress/compact and share config on both the offer and answer sides.

Features

  • Shorten WebRTC SDP.
  • Options to fixed parameters for both the offer and answer side.
  • Compress with zlib deflate.
  • Bytes based allow choosing any encoding.

Tri It Online

https://ntsd.github.io/sdp-compact

Installation

npm install sdp-compact

Examples

import * as spdCompact from "sdp-compact";

const sessDesc: RTCSessionDescriptionInit = {
  type: "offer",
  sdp: `v=0\r\no=- 4109260023080860376 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=extmap-allow-mixed\r\na=msid-semantic: WMS\r\n`,
};


const options: spdCompact.Options = { compress: true };
// When compress is enabled, sdp-compact use base64 encoding by default. You can switch to base92 for more compact size:
// const options: spdCompact.Options = { compress: 'base92' };

// compact the `RTCSessionDescriptionInit`
const compactedSessDesc = spdCompact.compact(sessDesc, options);
const decompactedSessDesc = spdCompact.decompact(compactedSessDesc, options);

// compact only the SDP string, will return base64 encoded if compress is enabled.
const compactedSPD = spdCompact.compactSDP(sessDesc.sdp, options);
// for decompact need to specify if it's offer or answer because it's not include in SDP
const decompactedSPD = spdCompact.decompactSDP(compactedSPD, true, options);

// compact only the SDP string to bytes
const compactedSPDBytes = spdCompact.compactSDPBytes(sessDesc.sdp, options);
// decompact the compacted SDP bytes to decompacted string
const decompactedSPDBytes = spdCompact.decompactSDPBytes(compactedSPDBytes, true, options);

Supported SDP Types

compact() supports all four RTCSdpType values and encodes the type as the first character of the compacted string: O (offer), A (answer), P (pranswer), and R (rollback). decompact() decodes the prefix back to the original type. An unsupported or missing type prefix throws a descriptive error instead of silently defaulting to answer.

import * as spdCompact from "sdp-compact";

// pranswer/rollback are supported:
const compacted = spdCompact.compact({ type: "pranswer", sdp });
console.log(spdCompact.decompact(compacted).type); // "pranswer"

// Invalid input throws instead of silently corrupting:
spdCompact.decompact(""); // throws: Invalid compacted SDP string
spdCompact.decompact("Xgarbage"); // throws: Invalid compacted SDP type prefix

Options

You can override the default options to suit your application's requirements, while still keeping the default values for any unspecified properties.

Default Options

const DefaultOptions: Options = {
  compress: 'base64',
  replaceFieldNames: true,
  sdpVersion: 0,
  sessionName: "-",
  origin: {
    username: "-",
    sessionId: "4109260023080860376",
    netType: "IN",
    addrtype: "IP4",
    unicastAddress: "127.0.0.1",
  },
  timing: "0 0",
  extmapAllowMixed: true,
  msidSemantic: "WMS",
  mediaOptions: {
    removeMediaID: true,
    removeSetup: true,
    replaceCandidateString: true,
    replaceMediaString: true,
    forceTrickle: true,
    compressFingerprint: true,
    compressConnection: true,
    compressExtmap: true,
    compressRtcpFb: true,
  },
};

Here is an explanation of each option and its default value:

compress (default: 'base64')

Enables compression using zlib deflate, followed by base64/base92 encoding.

replaceFieldNames (default: true)

Replaces field names using FieldReplaceMap and FieldReplaceMapReverse.

sdpVersion (default: 0)

The fixed SDP version (v=), which is usually 0 for now.

sessionName (default: "-")

The fixed session name (s=), which is usually "-".

origin (default: see below)

Fixed origin options (o=). This includes the following properties:

  • username: Fixed username, can be "-" for an undefined user. (default: "-")
  • sessionId: Fixed session ID, can be the same value on all peers. (default: "4109260023080860376")
  • netType: Fixed network type, must be "IN" for the internet. (default: "IN")
  • addrtype: Fixed address type, must be either "IP4" or "IP6". Usually, it will be "IP4". (default: "IP4")
  • unicastAddress: Fixed unicast address, usually "127.0.0.1". (default: "127.0.0.1")

timing (default: "0 0")

The fixed timing (t=). It can be "0 0" for unbounded.

extmapAllowMixed (default: true)

Allows mixing attributes (a=extmap-allow-mixed), usually allowed by default.

msidSemantic (default: "WMS")

The fixed msid semantic (a=msid-semantic:). It should be "WMS" for WebRTC Media Stream.

mediaOptions (default: see below)

Customize media options. This includes the following properties:

  • removeMediaID: Remove media ID (a=mid:) and group (a=group:) (a=group:BUNDLE) to use sequence medias instead. (default: true)
  • removeSetup: Remove DTLS role (a=setup:). It will set to actpass for offer and active for answer. (default: true)
  • replaceCandidateString: replace strings in ice candidate (a=candidate:) following candidateEncodeMap/candidateDecodeMap in src/dict.ts. (default: true)
  • replaceMediaString: replace strings in media (m=) following mediaEncodeMap/mediaDecodeMap in src/dict.ts. (default: true)
  • forceTrickle: force ice-options to trickle (a=ice-options:trickle). (default: true)
  • compressFingerprint: compress fingerprint (a=fingerprint:). (default: true)
  • compressConnection: compress media connection (c=). (default: true)
  • compressExtmap: compress extmap URIs by replacing common URNs with short identifiers (a=extmap:). (default: true)
  • compressRtcpFb: compress RTCP feedback types by replacing common types with short identifiers (a=rtcp-fb:). (default: true)

WebRTC SDP Anatomy

You can read the WebRTC SDP Anatomy Here for how it's works.

Projects using SDP Compact

  • Zero Share - A client-side secure P2P file sharing using WebRTC.

About

shorten WebRTC Session Description Protocol (SDP) based on Unified Plan SDP

Topics

Resources

Stars

11 stars

Watchers

1 watching

Forks

Used by

Contributors

Languages