Debian Codegraph @

Xtransform/README.md

raw
1 # SDP Transform
2
3 [![npm status](http://img.shields.io/npm/v/sdp-transform.svg)](https://www.npmjs.org/package/sdp-transform)
4 [![build status](https://secure.travis-ci.org/clux/sdp-transform.svg)](http://travis-ci.org/clux/sdp-transform)
5 [![dependency status](https://david-dm.org/clux/sdp-transform.svg)](https://david-dm.org/clux/sdp-transform)
6 [![coverage status](http://img.shields.io/coveralls/clux/sdp-transform.svg)](https://coveralls.io/r/clux/sdp-transform)
7
8 A simple parser and writer of SDP. Defines internal grammar based on [RFC4566 - SDP](http://tools.ietf.org/html/rfc4566), [RFC5245 - ICE](http://tools.ietf.org/html/rfc5245), and many more.
9
10 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.
11
12
13 ## Installation
14
15 ```bash
16 $ npm install sdp-transform
17 ```
18
19
20 ## TypeScript Definitions
21
22 Available in the [@types/sdp-transform](https://www.npmjs.com/package/@types/sdp-transform) package:
23
24 ```bash
25 $ npm install -D @types/sdp-transform
26 ```
27
28
29 ## Usage
30
31 Load using CommonJS syntax or ES6 syntax:
32
33 ```js
34 // CommonJS
35 const sdpTransform = require('sdp-transform');
36
37 // ES6
38 import * as sdpTransform from 'sdp-transform';
39 ```
40
41
42 ## Usage - Parser
43
44 Pass it an unprocessed SDP string.
45
46 ```js
47 const sdpStr = "v=0\r\n\
48 o=- 20518 0 IN IP4 203.0.113.1\r\n\
49 s= \r\n\
50 t=0 0\r\n\
51 c=IN IP4 203.0.113.1\r\n\
52 a=ice-ufrag:F7gI\r\n\
53 a=ice-pwd:x9cml/YzichV2+XlhiMu8g\r\n\
54 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\
55 m=audio 54400 RTP/SAVPF 0 96\r\n\
56 a=rtpmap:0 PCMU/8000\r\n\
57 a=rtpmap:96 opus/48000\r\n\
58 a=ptime:20\r\n\
59 a=sendrecv\r\n\
60 a=candidate:0 1 UDP 2113667327 203.0.113.1 54400 typ host\r\n\
61 a=candidate:1 2 UDP 2113667326 203.0.113.1 54401 typ host\r\n\
62 m=video 55400 RTP/SAVPF 97 98\r\n\
63 a=rtpmap:97 H264/90000\r\n\
64 a=fmtp:97 profile-level-id=4d0028;packetization-mode=1\r\n\
65 a=rtpmap:98 VP8/90000\r\n\
66 a=sendrecv\r\n\
67 a=candidate:0 1 UDP 2113667327 203.0.113.1 55400 typ host\r\n\
68 a=candidate:1 2 UDP 2113667326 203.0.113.1 55401 typ host\r\n\
69 ";
70
71 const res = sdpTransform.parse(sdpStr);
72 // =>
73 { version: 0,
74 origin:
75 { username: '-',
76 sessionId: 20518,
77 sessionVersion: 0,
78 netType: 'IN',
79 ipVer: 4,
80 address: '203.0.113.1' },
81 name: '',
82 timing: { start: 0, stop: 0 },
83 connection: { version: 4, ip: '203.0.113.1' },
84 iceUfrag: 'F7gI',
85 icePwd: 'x9cml/YzichV2+XlhiMu8g',
86 fingerprint:
87 { type: 'sha-1',
88 hash: '42:89:c5:c6:55:9d:6e:c8:e8:83:55:2a:39:f9:b6:eb:e9:a3:a9:e7' },
89 media:
90 [ { rtp: [Object],
91 fmtp: [],
92 type: 'audio',
93 port: 54400,
94 protocol: 'RTP/SAVPF',
95 payloads: '0 96',
96 ptime: 20,
97 direction: 'sendrecv',
98 candidates: [Object] },
99 { rtp: [Object],
100 fmtp: [Object],
101 type: 'video',
102 port: 55400,
103 protocol: 'RTP/SAVPF',
104 payloads: '97 98',
105 direction: 'sendrecv',
106 candidates: [Object] } ] }
107
108
109 // each media line is parsed into the following format
110 res.media[1];
111 // =>
112 { rtp:
113 [ { payload: 97,
114 codec: 'H264',
115 rate: 90000 },
116 { payload: 98,
117 codec: 'VP8',
118 rate: 90000 } ],
119 fmtp:
120 [ { payload: 97,
121 config: 'profile-level-id=4d0028;packetization-mode=1' } ],
122 type: 'video',
123 port: 55400,
124 protocol: 'RTP/SAVPF',
125 payloads: '97 98',
126 direction: 'sendrecv',
127 candidates:
128 [ { foundation: 0,
129 component: 1,
130 transport: 'UDP',
131 priority: 2113667327,
132 ip: '203.0.113.1',
133 port: 55400,
134 type: 'host' },
135 { foundation: 1,
136 component: 2,
137 transport: 'UDP',
138 priority: 2113667326,
139 ip: '203.0.113.1',
140 port: 55401,
141 type: 'host' } ] }
142 ```
143
144 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.
145
146 ### Parser Postprocessing
147
148 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:
149
150 #### parseParams()
151
152 Parses `fmtp.config` and others such as `rid.params` and returns an object with all the params in a key/value fashion.
153
154 ```js
155 // to parse the fmtp.config from the previous example
156 sdpTransform.parseParams(res.media[1].fmtp[0].config);
157 // =>
158 { 'profile-level-id': '4d0028',
159 'packetization-mode': 1 }
160 ```
161
162 #### parsePayloads()
163
164 Returns an array with all the payload advertised in the main m-line.
165
166 ```js
167 // what payloads where actually advertised in the main m-line ?
168 sdpTransform.parsePayloads(res.media[1].payloads);
169 // =>
170 [97, 98]
171 ```
172
173 #### parseImageAttributes()
174
175 Parses [Generic Image Attributes](https://tools.ietf.org/html/rfc6236). Must be provided with the `attrs1` or `attrs2` string of a `a=imageattr` line. Returns an array of key/value objects.
176
177 ```js
178 // a=imageattr:97 send [x=1280,y=720] recv [x=1280,y=720] [x=320,y=180]
179 sdpTransform.parseImageAttributes(res.media[1].imageattrs[0].attrs2)
180 // =>
181 [ {'x': 1280, 'y': 720}, {'x': 320, 'y': 180} ]
182 ```
183
184 #### parseSimulcastStreamList()
185
186 Parses [simulcast](https://tools.ietf.org/html/draft-ietf-mmusic-sdp-simulcast) streams/formats. Must be provided with the `attrs1` or `attrs2` string of the `a=simulcast` line.
187
188 Returns an array of simulcast streams. Each entry is an array of alternative simulcast formats, which are objects with two keys:
189
190 * `scid`: Simulcast identifier
191 * `paused`: Whether the simulcast format is paused
192
193 ```js
194 // a=simulcast:send 1,~4;2;3 recv c
195 sdpTransform.parseSimulcastStreamList(res.media[1].simulcast.attrs1);
196 // =>
197 [
198 // First simulcast stream (two alternative formats)
199 [ {scid: 1, paused: false}, {scid: 4, paused: true} ],
200 // Second simulcast stream
201 [ {scid: 2, paused: false} ],
202 // Third simulcast stream
203 [ {scid: 3, paused: false} ]
204 ]
205 ```
206
207 ## Usage - Writer
208
209 The writer is the inverse of the parser, and will need a struct equivalent to the one returned by it.
210
211 ```js
212 sdpTransform.write(res).split('\r\n'); // res parsed above
213 // =>
214 [ 'v=0',
215 'o=- 20518 0 IN IP4 203.0.113.1',
216 's= ',
217 'c=IN IP4 203.0.113.1',
218 't=0 0',
219 'a=ice-ufrag:F7gI',
220 'a=ice-pwd:x9cml/YzichV2+XlhiMu8g',
221 'a=fingerprint:sha-1 42:89:c5:c6:55:9d:6e:c8:e8:83:55:2a:39:f9:b6:eb:e9:a3:a9:e7',
222 'm=audio 54400 RTP/SAVPF 0 96',
223 'a=rtpmap:0 PCMU/8000',
224 'a=rtpmap:96 opus/48000',
225 'a=ptime:20',
226 'a=sendrecv',
227 'a=candidate:0 1 UDP 2113667327 203.0.113.1 54400 typ host',
228 'a=candidate:1 2 UDP 2113667326 203.0.113.1 54401 typ host',
229 'm=video 55400 RTP/SAVPF 97 98',
230 'a=rtpmap:97 H264/90000',
231 'a=rtpmap:98 VP8/90000',
232 'a=fmtp:97 profile-level-id=4d0028;packetization-mode=1',
233 'a=sendrecv',
234 'a=candidate:0 1 UDP 2113667327 203.0.113.1 55400 typ host',
235 'a=candidate:1 2 UDP 2113667326 203.0.113.1 55401 typ host' ]
236 ```
237
238 The only thing different from the original input is we follow the order specified by the SDP RFC, and we will always do so.
239
240
241 ## License
242
243 MIT-Licensed. See LICENSE file for details.