-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathupgrade.js
More file actions
156 lines (135 loc) · 4.65 KB
/
Copy pathupgrade.js
File metadata and controls
156 lines (135 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* webrtc interop testing using using selenium
* Copyright (c) 2016, Philipp Hancke
*/
var test = require('tape');
var buildDriver = require('./webdriver').buildDriver;
var getTestpage = require('./webdriver').getTestpage;
var WebRTCClient = require('./webrtcclient');
var SDPUtils = require('sdp');
// add MSIDs the other party understands.
function mangle(sdp) {
var mediaSections = SDPUtils.splitSections(sdp);
for (var i = 1; i < mediaSections.length; i++) {
var parts;
var ssrclines = SDPUtils.matchPrefix(mediaSections[i], 'a=ssrc');
var chromeMsid = ssrclines.filter(line => line.split(' ')[1].indexOf('msid:') === 0);
var cnames = ssrclines.filter(line => line.split(' ')[1].indexOf('cname:') === 0);
var specMsid = SDPUtils.matchPrefix(mediaSections[i], 'a=msid:');
if (!specMsid.length && chromeMsid.length > 0) {
parts = chromeMsid[0].split(' ');
parts.shift();
mediaSections[i] += 'a=' + parts.join(' ') + '\r\n';
} else if (specMsid.length > 0 && cnames.length && !chromeMsid.length) {
mediaSections[i] += cnames[0].split(' ', 1)[0] + ' ' +
specMsid[0].substr(2) + '\r\n';
}
}
return mediaSections.join('');
}
// we use addStream twice and pretend to be a single stream to
// work around FF bugs.
function replaceSecondStreamId(sdp) {
var mediaSections = SDPUtils.splitSections(sdp);
var firstMsid = SDPUtils.matchPrefix(mediaSections[1], 'a=msid:')[0]
.split(' ')[0].substr(7);
var secondMsid = SDPUtils.matchPrefix(mediaSections[2], 'a=msid:')[0]
.split(' ')[0].substr(7);
return sdp.replace(new RegExp(secondMsid, 'g'), firstMsid);
}
function upgrade(t, browserA, browserB) {
var driverA = buildDriver(browserA);
var driverB = buildDriver(browserB);
var clientA = new WebRTCClient(driverA);
var clientB = new WebRTCClient(driverB);
getTestpage(driverA)
.then(() => getTestpage(driverB))
.then(() => clientA.create())
.then(() => clientB.create())
.then(() => clientA.getUserMedia({audio: true, video: false}))
.then((stream) => {
t.pass('got user media');
return clientA.addStream(stream);
})
.then(() => clientA.createOffer())
.then(offer => {
t.pass('created offer');
return clientA.setLocalDescription(offer);
})
.then(offerWithCandidates => {
t.pass('offer ready to signal');
// mangle interoperable msids.
offerWithCandidates.sdp = mangle(offerWithCandidates.sdp);
return clientB.setRemoteDescription(offerWithCandidates);
})
.then(() => clientB.createAnswer())
.then(answer => {
t.pass('created answer');
return clientB.setLocalDescription(answer); // modify answer here?
})
.then(answerWithCandidates => {
t.pass('answer ready to signal');
// mangle interoperable msids.
answerWithCandidates.sdp = mangle(answerWithCandidates.sdp);
return clientA.setRemoteDescription(answerWithCandidates);
})
.then(() => // wait for the iceConnectionState to become either connected/completed
// or failed.
clientA.waitForIceConnectionStateChange())
.then(iceConnectionState => {
t.ok(iceConnectionState !== 'failed', 'ICE connection is established');
})
.then(() => {
driverA.sleep(3000);
return clientA.getUserMedia({audio: false, video: true});
})
.then(() => {
t.pass('got user media');
return clientA.addStream();
})
.then(() => clientA.createOffer())
.then(offer => {
t.pass('created offer');
return clientA.setLocalDescription(offer);
})
.then(offerWithCandidates => {
t.pass('offer ready to signal');
// mangle interoperable msids.
offerWithCandidates.sdp = mangle(offerWithCandidates.sdp);
// we mangle it so it looks like adding to the stream at B.
offerWithCandidates.sdp = replaceSecondStreamId(offerWithCandidates.sdp);
return clientB.setRemoteDescription(offerWithCandidates);
})
.then(() => clientB.createAnswer())
.then(answer => {
t.pass('created answer');
return clientB.setLocalDescription(answer); // modify answer here?
})
.then(answerWithCandidates => {
t.pass('answer ready to signal');
// mangle interoperable msids.
answerWithCandidates.sdp = mangle(answerWithCandidates.sdp);
return clientA.setRemoteDescription(answerWithCandidates);
})
.then(() => {
driverA.sleep(3000);
})
.then(() => Promise.all([driverA.quit(), driverB.quit()])
.then(() => {
t.end();
}))
.catch(err => {
t.fail(err);
});
}
test('Chrome-Chrome', t => {
upgrade(t, 'chrome', 'chrome');
});
test('Firefox-Firefox', t => {
upgrade(t, 'firefox', 'firefox');
});
test('Chrome-Firefox', t => {
upgrade(t, 'chrome', 'firefox');
});
test('Firefox-Chrome', t => {
upgrade(t, 'firefox', 'chrome');
});