|
@@ -0,0 +1,97 @@
|
|
1
|
+var transform = require('sdp-transform');
|
|
2
|
+
|
|
3
|
+exports.write = function(session, opts) {
|
|
4
|
+
|
|
5
|
+ if (typeof session !== 'undefined' &&
|
|
6
|
+ typeof session.media !== 'undefined' &&
|
|
7
|
+ Array.isArray(session.media)) {
|
|
8
|
+
|
|
9
|
+ session.media.forEach(function (mLine) {
|
|
10
|
+ // expand sources to ssrcs
|
|
11
|
+ if (typeof mLine.sources !== 'undefined' &&
|
|
12
|
+ Object.keys(mLine.sources).length !== 0) {
|
|
13
|
+ mLine.ssrcs = [];
|
|
14
|
+ Object.keys(mLine.sources).forEach(function (ssrc) {
|
|
15
|
+ var source = mLine.sources[ssrc];
|
|
16
|
+ Object.keys(source).forEach(function (attribute) {
|
|
17
|
+ mLine.ssrcs.push({
|
|
18
|
+ id: ssrc,
|
|
19
|
+ attribute: attribute,
|
|
20
|
+ value: source[attribute]
|
|
21
|
+ });
|
|
22
|
+ });
|
|
23
|
+ });
|
|
24
|
+ delete mLine.sources;
|
|
25
|
+ }
|
|
26
|
+
|
|
27
|
+ // join ssrcs in ssrc groups
|
|
28
|
+ if (typeof mLine.ssrcGroups !== 'undefined' &&
|
|
29
|
+ Array.isArray(mLine.ssrcGroups)) {
|
|
30
|
+ mLine.ssrcGroups.forEach(function (ssrcGroup) {
|
|
31
|
+ if (typeof ssrcGroup.ssrcs !== 'undefined' &&
|
|
32
|
+ Array.isArray(ssrcGroup.ssrcs)) {
|
|
33
|
+ ssrcGroup.ssrcs = ssrcGroup.ssrcs.join(' ');
|
|
34
|
+ }
|
|
35
|
+ });
|
|
36
|
+ }
|
|
37
|
+ });
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+ // join group mids
|
|
41
|
+ if (typeof session !== 'undefined' &&
|
|
42
|
+ typeof session.groups !== 'undefined' && Array.isArray(session.groups)) {
|
|
43
|
+
|
|
44
|
+ session.groups.forEach(function (g) {
|
|
45
|
+ if (typeof g.mids !== 'undefined' && Array.isArray(g.mids)) {
|
|
46
|
+ g.mids = g.mids.join(' ');
|
|
47
|
+ }
|
|
48
|
+ });
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ return transform.write(session, opts);
|
|
52
|
+};
|
|
53
|
+
|
|
54
|
+exports.parse = function(sdp) {
|
|
55
|
+ var session = transform.parse(sdp);
|
|
56
|
+
|
|
57
|
+ if (typeof session !== 'undefined' && typeof session.media !== 'undefined' &&
|
|
58
|
+ Array.isArray(session.media)) {
|
|
59
|
+
|
|
60
|
+ session.media.forEach(function (mLine) {
|
|
61
|
+ // group sources attributes by ssrc
|
|
62
|
+ if (typeof mLine.ssrcs !== 'undefined' && Array.isArray(mLine.ssrcs)) {
|
|
63
|
+ mLine.sources = {};
|
|
64
|
+ mLine.ssrcs.forEach(function (ssrc) {
|
|
65
|
+ if (!mLine.sources[ssrc.id])
|
|
66
|
+ mLine.sources[ssrc.id] = {};
|
|
67
|
+ mLine.sources[ssrc.id][ssrc.attribute] = ssrc.value;
|
|
68
|
+ });
|
|
69
|
+
|
|
70
|
+ delete mLine.ssrcs;
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ // split ssrcs in ssrc groups
|
|
74
|
+ if (typeof mLine.ssrcGroups !== 'undefined' &&
|
|
75
|
+ Array.isArray(mLine.ssrcGroups)) {
|
|
76
|
+ mLine.ssrcGroups.forEach(function (ssrcGroup) {
|
|
77
|
+ if (typeof ssrcGroup.ssrcs === 'string') {
|
|
78
|
+ ssrcGroup.ssrcs = ssrcGroup.ssrcs.split(' ');
|
|
79
|
+ }
|
|
80
|
+ });
|
|
81
|
+ }
|
|
82
|
+ });
|
|
83
|
+ }
|
|
84
|
+ // split group mids
|
|
85
|
+ if (typeof session !== 'undefined' &&
|
|
86
|
+ typeof session.groups !== 'undefined' && Array.isArray(session.groups)) {
|
|
87
|
+
|
|
88
|
+ session.groups.forEach(function (g) {
|
|
89
|
+ if (typeof g.mids === 'string') {
|
|
90
|
+ g.mids = g.mids.split(' ');
|
|
91
|
+ }
|
|
92
|
+ });
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ return session;
|
|
96
|
+};
|
|
97
|
+
|