This repository has been archived by the owner on Aug 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
96 lines (83 loc) · 2.36 KB
/
index.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
'use strict';
const ws = require('ws').Server;
const Server = require('./server')(ws);
const kurento = require('kurento-client');
const kurentoPipeline = require('./kurento-pipeline');
const kmsUrl = 'ws://kurento:8888/kurento';
console.log(`Using KMS WebSocket server at ${kmsUrl}`);
// Establish connection with Kurento Media Server using its URL
kurento(kmsUrl)
.catch(error => {
console.error(
`Could not find media server at address ${kmsUrl}. Exiting:`,
error
);
process.exit(1);
})
.then(kurentoClient => {
const createPipeline = kurentoPipeline(
kurentoClient,
// We need to manually give this constructor to pipeline so it doesn't
// have to import Kurento.
kurento.register.complexTypes.IceCandidate
);
const server = Server({ port: 7000 });
console.log('WS Server listening on port 7000');
server.onConnection(client => {
const pipeline = createPipeline(client);
client.onMessage(handleMessages(client, pipeline));
client.onError(handleError(pipeline));
client.onClose(handleClose(pipeline));
});
});
function handleMessages(client, pipeline) {
return message => {
switch (message.id) {
case 'start':
const { rtspUri, sdpOffer } = message;
handleStart(client, pipeline)(rtspUri, sdpOffer);
break;
case 'stop':
console.log('Client stopped stream');
pipeline.stop();
break;
case 'onIceCandidate':
pipeline.handleIceCandidate(message.candidate);
break;
default:
client.send({
id: 'error',
message: `Invalid message ID: ${message.id}`,
});
break;
}
};
}
function handleStart(client, pipeline) {
return async (rtspUri, sdpOffer) => {
console.log('START', rtspUri);
try {
console.log('Launching pipeline for RTSP URL:', rtspUri);
pipeline.start(rtspUri, sdpOffer);
} catch (error) {
const [message, reason] = error;
console.error(message);
client.send({
id: 'error',
error: reason,
});
}
};
}
function handleError(pipeline) {
return error => {
console.error('WebSocket error:', error);
pipeline.stop();
};
}
function handleClose(pipeline) {
return (code, reason) => {
console.info(`WebSocket closed with code ${code}: ${reason}`);
pipeline.stop();
};
}