-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwock_test.js
190 lines (151 loc) · 4.09 KB
/
wock_test.js
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
if(typeof QUnit == 'undefined')
{
QUnit = require('qunit-cli');
var wock = require('wock');
// var RTCPeerConnection = require('wrtc').RTCPeerConnection;
var KwsMedia = require('..');
};
function processOffer(peerConnection, offer, onsuccess, onerror)
{
peerConnection.setRemoteDescription(offer, function()
{
peerConnection.createAnswer(function(answer)
{
peerConnection.setLocalDescription(answer, function()
{
onsuccess(answer);
},
onerror);
},
onerror);
},
onerror);
};
function onerror(error)
{
console.error(error);
};
/* Proxy */
var objects = {};
function proxy(data)
{
console.log('< '+data);
var message = JSON.parse(data);
var method = message.method;
var id = message.id;
var params = message.params;
var result = undefined;
var error = undefined;
switch(method)
{
case 'create':
{
var type = params.type;
switch(type)
{
case 'MediaPipeline':
{
objects[id] = 'MediaPipeline';
result = {id: id};
}
break;
case 'JackVaderFilter':
case 'PlayerEndpoint':
case 'WebRtcEndpoint':
{
var constructorParams = params.constructorParams;
var pipeline_id = constructorParams.mediaPipeline;
var pipeline = objects[pipeline_id];
if(pipeline)
{
objects[id] = params.type;
result = {id: id};
}
else
error = {message: "Unknown pipeline: "+pipeline_id};
}
break;
default:
error = {message: "Unknown type: "+type};
}
};
break;
case 'invoke':
{
var operation = params.operation;
switch(operation)
{
case 'connectElements':
return;
};
};
break;
default:
error = {message: "Unknown method: "+method};
};
data = JSON.stringify(
{
jsonrpc: "2.0",
id: message.id,
result: result,
error: error
});
console.log('> '+data);
this.emit('message', data, {});
};
/* Proxy */
//var WebSocket = wock(proxy);
var WebSocket = require('ws');
KwsMedia(new WebSocket('ws://130.206.81.87/thrift/ws/websocket'),
function(kwsMedia)
{
// Create pipeline
kwsMedia.createMediaPipeline(function(error, pipeline)
{
if(error) return console.error(error);
// Create pipeline media elements (endpoints & filters)
var type = ['PlayerEndpoint', 'JackVaderFilter', 'WebRtcEndpoint'];
var params = [{uri: "http://localhost:8000/video.avi"}, null, null];
pipeline.createMediaElement(type, params,
function(error, mediaElements)
{
if(error) return console.error(error);
var playerEndpoint = mediaElements[0];
var jackVaderFilter = mediaElements[1];
var webRtcEndpoint = mediaElements[2];
// Connect media element between them
pipeline.connect(playerEndpoint, jackVaderFilter, webRtcEndpoint);
// Subscribe to PlayerEndpoint EOS event
playerEndpoint.on('EndOfStream', function()
{
console.log("EndOfStream");
});
// Create a PeerConnection client in the browser
var peerConnection = new RTCPeerConnection
(
{iceServers: [{url: 'stun:stun.l.google.com:19302'}]},
{optional: [{DtlsSrtpKeyAgreement: true}]}
);
// Connect the pipeline to the PeerConnection client
webRtcEndpoint.invoke("generateSdpOffer", function(error, offer)
{
if(error) return console.error(error);
processOffer(peerConnection, offer, function(answer)
{
webRtcEndpoint.invoke("processSdpAnswer", {answer: answer},
function(error)
{
if(error) return console.error(error);
var stream = peerConnection.getLocalStreams()[0];
// Set the stream on the video tag
var videoOutput = document.getElementById("videoOutput");
videoOutput.src = URL.createObjectURL(stream);
// Start player
playerEndpoint.start();
});
},
onerror);
});
});
});
});