-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
131 lines (106 loc) · 3.37 KB
/
app.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
/**
* Server startup
*/
var kurento = require('kurento-client');
var express = require('express');
var path = require('path');
var wsm = require('ws');
var app = express();
var activePipeline;
const ws_uri = "ws://"+ (process.env.KURENTO || "127.0.0.1:8888") +"/kurento";
const rtsp_uri = "rtsp://" + (process.env.RTSP || "127.0.0.1:8086");
app.set('port', process.env.PORT || 8080);
var port = app.get('port');
var server = app.listen(port, function() {
console.log('Express server started');
console.log('Kurento URL: ' + ws_uri);
console.log('RTSP URL: ' + rtsp_uri);
console.log('Connect to http://<host_name>:' + port + '/');
});
var WebSocketServer = wsm.Server, wss = new WebSocketServer({
server : server,
path : '/call'
});
function wsError(ws, error) {
console.error(error);
ws.send(JSON.stringify({
id : 'viewerResponse',
response : 'rejected',
message : error
}));
return false;
}
/*
* Management of WebSocket messages
*/
wss.on('connection', function(ws) {
console.log('WS Connection received');
ws.on('error', function(error) {
console.log('WS Connection error: ' + error);
});
ws.on('close', function() {
console.log('WS Connection closed');
});
ws.on('message', function(_message) {
var message = JSON.parse(_message);
console.log('WS Connection received message ', message);
switch (message.id) {
case 'viewer':
console.log('Creating kurentoClient');
kurento(ws_uri, function(error, kurentoClient) {
if (error) return wsError(ws, "ERROR 1: Could not find media server at address" + ws_uri + ". Exiting with error " + error);
// Create pipline
console.log('Creating MediaPipline');
kurentoClient.create('MediaPipeline', function(error, pipeline) {
if (error) return wsError(ws, "ERROR 2: " + error);
activePipeline = pipeline;
// Create player
console.log('Creating PlayerEndpoint');
pipeline.create('PlayerEndpoint', {uri: rtsp_uri, useEncodedMedia: false}, function(error, playerEndpoint) {
if (error) return wsError(ws, "ERROR 3: " + error);
playerEndpoint.on('EndOfStream', function() {
console.log('END OF STREAM');
pipeline.release();
});
console.log('Now Playing');
playerEndpoint.play(function(error) {
if (error) return wsError(ws, "ERROR 4: " + error);
// Create WebRtc
console.log('Creating WebRTCEndpoint');
pipeline.create('WebRtcEndpoint', function(error, webRtcEndpoint) {
if (error) return wsError(ws, "ERROR 5: " + error);
console.log('Processing SDP offer');
webRtcEndpoint.processOffer(message.sdpOffer, function(error, sdpAnswer) {
if (error) return wsError(ws, "ERROR 6: " + error);
console.log('Connecting to endpoint');
playerEndpoint.connect(webRtcEndpoint, function(error) {
if (error) return wsError(ws, "ERROR 7: " + error);
console.log('Connected!');
ws.send(JSON.stringify({
id : 'viewerResponse',
response : 'accepted',
sdpAnswer : sdpAnswer
}));
});
});
});
});
});
});
});
break;
case 'stop':
if (activePipeline) {
activePipeline.release();
}
break;
default:
ws.send(JSON.stringify({
id : 'error',
message : 'Invalid message ' + message
}));
break;
}
});
});
app.use(express.static(path.join(__dirname, 'static')));