• Stars
    star
    324
  • Rank 129,708 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple parser/writer for the Session Description Protocol

SDP Transform

npm status build status dependency status coverage status

A simple parser and writer of SDP. Defines internal grammar based on RFC4566 - SDP, RFC5245 - ICE, and many more.

For simplicity it will force values that are integers to integers and leave everything else as strings when parsing. The module should be simple to extend or build upon, and is constructed rigorously.

Installation

$ npm install sdp-transform

TypeScript Definitions

Available in the @types/sdp-transform package:

$ npm install -D @types/sdp-transform

Usage

Load using CommonJS syntax or ES6 syntax:

// CommonJS
const sdpTransform = require('sdp-transform');

// ES6
import * as sdpTransform from 'sdp-transform';

Usage - Parser

Pass it an unprocessed SDP string.

const sdpStr = "v=0\r\n\
o=- 20518 0 IN IP4 203.0.113.1\r\n\
s= \r\n\
t=0 0\r\n\
c=IN IP4 203.0.113.1\r\n\
a=ice-ufrag:F7gI\r\n\
a=ice-pwd:x9cml/YzichV2+XlhiMu8g\r\n\
a=fingerprint:sha-1 42:89:c5:c6:55:9d:6e:c8:e8:83:55:2a:39:f9:b6:eb:e9:a3:a9:e7\r\n\
m=audio 54400 RTP/SAVPF 0 96\r\n\
a=rtpmap:0 PCMU/8000\r\n\
a=rtpmap:96 opus/48000\r\n\
a=ptime:20\r\n\
a=sendrecv\r\n\
a=candidate:0 1 UDP 2113667327 203.0.113.1 54400 typ host\r\n\
a=candidate:1 2 UDP 2113667326 203.0.113.1 54401 typ host\r\n\
m=video 55400 RTP/SAVPF 97 98\r\n\
a=rtpmap:97 H264/90000\r\n\
a=fmtp:97 profile-level-id=4d0028;packetization-mode=1\r\n\
a=rtpmap:98 VP8/90000\r\n\
a=sendrecv\r\n\
a=candidate:0 1 UDP 2113667327 203.0.113.1 55400 typ host\r\n\
a=candidate:1 2 UDP 2113667326 203.0.113.1 55401 typ host\r\n\
";

const res = sdpTransform.parse(sdpStr);
// =>
{ version: 0,
  origin:
   { username: '-',
     sessionId: 20518,
     sessionVersion: 0,
     netType: 'IN',
     ipVer: 4,
     address: '203.0.113.1' },
  name: '',
  timing: { start: 0, stop: 0 },
  connection: { version: 4, ip: '203.0.113.1' },
  iceUfrag: 'F7gI',
  icePwd: 'x9cml/YzichV2+XlhiMu8g',
  fingerprint:
   { type: 'sha-1',
     hash: '42:89:c5:c6:55:9d:6e:c8:e8:83:55:2a:39:f9:b6:eb:e9:a3:a9:e7' },
  media:
   [ { rtp: [Object],
       fmtp: [],
       type: 'audio',
       port: 54400,
       protocol: 'RTP/SAVPF',
       payloads: '0 96',
       ptime: 20,
       direction: 'sendrecv',
       candidates: [Object] },
     { rtp: [Object],
       fmtp: [Object],
       type: 'video',
       port: 55400,
       protocol: 'RTP/SAVPF',
       payloads: '97 98',
       direction: 'sendrecv',
       candidates: [Object] } ] }


// each media line is parsed into the following format
res.media[1];
// =>
{ rtp:
   [ { payload: 97,
       codec: 'H264',
       rate: 90000 },
     { payload: 98,
       codec: 'VP8',
       rate: 90000 } ],
  fmtp:
   [ { payload: 97,
       config: 'profile-level-id=4d0028;packetization-mode=1' } ],
  type: 'video',
  port: 55400,
  protocol: 'RTP/SAVPF',
  payloads: '97 98',
  direction: 'sendrecv',
  candidates:
   [ { foundation: 0,
       component: 1,
       transport: 'UDP',
       priority: 2113667327,
       ip: '203.0.113.1',
       port: 55400,
       type: 'host' },
     { foundation: 1,
       component: 2,
       transport: 'UDP',
       priority: 2113667326,
       ip: '203.0.113.1',
       port: 55401,
       type: 'host' } ] }

In this example, only slightly dodgy string coercion case here is for candidates[i].foundation, which can be a string, but in this case can be equally parsed as an integer.

Parser Postprocessing

No excess parsing is done to the raw strings apart from maybe coercing to ints, because the writer is built to be the inverse of the parser. That said, a few helpers have been built in:

parseParams()

Parses fmtp.config and others such as rid.params and returns an object with all the params in a key/value fashion.

// to parse the fmtp.config from the previous example
sdpTransform.parseParams(res.media[1].fmtp[0].config);
// =>
{ 'profile-level-id': '4d0028',
  'packetization-mode': 1 }

parsePayloads()

Returns an array with all the payload advertised in the main m-line.

// what payloads where actually advertised in the main m-line ?
sdpTransform.parsePayloads(res.media[1].payloads);
// =>
[97, 98]

parseImageAttributes()

Parses Generic Image Attributes. Must be provided with the attrs1 or attrs2 string of a a=imageattr line. Returns an array of key/value objects.

// a=imageattr:97 send [x=1280,y=720] recv [x=1280,y=720] [x=320,y=180]
sdpTransform.parseImageAttributes(res.media[1].imageattrs[0].attrs2)
// =>
[ {'x': 1280, 'y': 720}, {'x': 320, 'y': 180} ]

parseSimulcastStreamList()

Parses simulcast streams/formats. Must be provided with the list1 or list2 string of the a=simulcast line.

Returns an array of simulcast streams. Each entry is an array of alternative simulcast formats, which are objects with two keys:

  • scid: Simulcast identifier
  • paused: Whether the simulcast format is paused
// a=simulcast:send 1,~4;2;3 recv c
sdpTransform.parseSimulcastStreamList(res.media[1].simulcast.list1);
// =>
[
  // First simulcast stream (two alternative formats)
  [ {scid: 1, paused: false}, {scid: 4, paused: true} ],
  // Second simulcast stream
  [ {scid: 2, paused: false} ],
  // Third simulcast stream
  [ {scid: 3, paused: false} ]
]

Usage - Writer

The writer is the inverse of the parser, and will need a struct equivalent to the one returned by it.

sdpTransform.write(res).split('\r\n'); // res parsed above
// =>
[ 'v=0',
  'o=- 20518 0 IN IP4 203.0.113.1',
  's= ',
  'c=IN IP4 203.0.113.1',
  't=0 0',
  'a=ice-ufrag:F7gI',
  'a=ice-pwd:x9cml/YzichV2+XlhiMu8g',
  'a=fingerprint:sha-1 42:89:c5:c6:55:9d:6e:c8:e8:83:55:2a:39:f9:b6:eb:e9:a3:a9:e7',
  'm=audio 54400 RTP/SAVPF 0 96',
  'a=rtpmap:0 PCMU/8000',
  'a=rtpmap:96 opus/48000',
  'a=ptime:20',
  'a=sendrecv',
  'a=candidate:0 1 UDP 2113667327 203.0.113.1 54400 typ host',
  'a=candidate:1 2 UDP 2113667326 203.0.113.1 54401 typ host',
  'm=video 55400 RTP/SAVPF 97 98',
  'a=rtpmap:97 H264/90000',
  'a=rtpmap:98 VP8/90000',
  'a=fmtp:97 profile-level-id=4d0028;packetization-mode=1',
  'a=sendrecv',
  'a=candidate:0 1 UDP 2113667327 203.0.113.1 55400 typ host',
  'a=candidate:1 2 UDP 2113667326 203.0.113.1 55401 typ host' ]

The only thing different from the original input is we follow the order specified by the SDP RFC, and we will always do so.

License

MIT-Licensed. See LICENSE file for details.

More Repositories

1

muslrust

Docker environment for building musl based static linux rust binaries
Rust
946
star
2

decay

Famous sorting algorithms based on vote popularity and time implemented for nodejs
JavaScript
374
star
3

webapp-rs

Rust microservice setup with postgres, and diesel + rocket in docker
Rust
58
star
4

logule

A multi-transport, peer-dependent logging library for nodejs - UNMAINTAINED
JavaScript
35
star
5

symlink

npm link helper for npm@^2
JavaScript
33
star
6

modul8

NO LONGER MAINTAINED - browserify is too similar and is better (see issues)
JavaScript
22
star
7

loggerv

A minimalistic stdout/stderr logger for rust
Rust
21
star
8

whyq

jq compatible yq implementation in rust
Rust
17
star
9

tap-pessimist

A tap consumer that filters on failed tests
JavaScript
15
star
10

diesel-cli

Minimal docker image with disel cli for migrations
Makefile
14
star
11

npm-graph

Prints a dependency graph of modules that is actually required
JavaScript
14
star
12

badgify

Rebuild readme markdown badges from package.json
JavaScript
13
star
13

cleverbot-irc

An IRC bot that defers to Cleverbot.
JavaScript
11
star
14

dotfiles

Symlinked dotfiles
Shell
11
star
15

blog-docker

Git and Markdown powered, containerised blog experiment
Rust
10
star
16

shipcat

fork mirror of shipcat up until july'21 since babylonhealth closed it soon after
Rust
10
star
17

jenq

Query jenkins job history or console output from a terminal
Rust
8
star
18

irc-stream

A duplex streaming IRC bot library built on top of `irc`
JavaScript
7
star
19

zalgolize

Uniform-clustered zalgolizer
JavaScript
6
star
20

tub

Lax streaming tap parser
JavaScript
6
star
21

provision

Arch Linux + Mac provisioning scripts
Shell
6
star
22

topiary

Produce pretty representations of tree-structured objects
JavaScript
6
star
23

magic-forest

CPU bound benchmarking of languages using the magic forest problem
Fortran
6
star
24

tournament.hs

Tournament related algorithms in Haskell
Haskell
6
star
25

wolfram-irc

An IRC bot that defers to Wolfram Alpha
JavaScript
6
star
26

dye

Minimalistic terminal coloring library
JavaScript
5
star
27

bandage

Simple generator based test library
JavaScript
4
star
28

trials

Statistical trial generator
JavaScript
4
star
29

yr-cli

Fetch specific weather forecasts from your terminal
JavaScript
4
star
30

confortable

Makes nodejs config location a little more.. pleasant.
JavaScript
3
star
31

splitter

Stream split library using new streams (node >= 0.10)
JavaScript
3
star
32

vitae

Personal HTML5 CV
HTML
3
star
33

quadratic

Quadratic reciprocity and the Jacobi Symbol for rust
Rust
3
star
34

facemaulers

face maulers 2018-2021
Just
3
star
35

gu

Streaming bot makers library with regex handlers
JavaScript
3
star
36

fsx

Simple recursive file and directory reader using synchronous node fs calls
JavaScript
2
star
37

.textadept

JavaScript and CoffeeScript settings for the textadept editor (updated at v4.2)
Lua
2
star
38

kubecon2022

kube-rs office hour slides
2
star
39

food

Food framework for future fulfilment
JavaScript
2
star
40

sulfur

Absorb log smells in your application
JavaScript
2
star
41

mumble-bot

Experiment with using the mumble client
JavaScript
2
star
42

curvefever-bot

A curvefever signup and match making bot for your chat channel
JavaScript
2
star
43

icebreaker

Deus Ex style ICE breaker web component
HTML
1
star
44

combustion

A primitive, but efficient micro-templating javascript engine
JavaScript
1
star
45

m8-templation

NOT MAINTAINED. See combustion instead.
JavaScript
1
star
46

m8-mongoose

NOT MAINTAINED. Bad/hard idea to get right. Not worth it.
CoffeeScript
1
star
47

linkr-components

Polymer component experiments for a personal link sharing site
HTML
1
star
48

deus-login

Deus Ex styled login component
HTML
1
star
49

unitednationsoftheworld

An updated and complete Nations of the World song ala Animaniacs
JavaScript
1
star
50

kubecon2020

Talk for KubeCon 2020
JavaScript
1
star
51

smell

Smelly log emitter
JavaScript
1
star
52

curvefever-stats

A statistics library and match maker for the curvefever game
JavaScript
1
star
53

tl-datademo

TrueLayer Data API test app.
Rust
1
star
54

posts

blog posts for new blog
1
star
55

jquery-timeslider

The easier, keyboard friendly way to pick the time of day.
JavaScript
1
star
56

flight-stream

A readable stream that tracks nearby aircrafts.
JavaScript
1
star
57

mddocs

A simple doc generator for gh-pages when writing documentation in markdown
JavaScript
1
star