-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (55 loc) · 1.72 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
import path from 'path'
import http from 'http'
import express from 'express'
import {Server} from 'socket.io'
import dotenv from 'dotenv'
import Ffmpeg from './utils/ffmpeg.js'
const app = express();
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
allowedHeaders: ["my-custom-header"],
credentials: true
}
});
const port = process.env.PORT || 3000;
let ffmpegProcess = null;
io.on("connection", socket => {
console.log("New WebSocket connection");
socket.on("start", (event, callback) => {
console.log('start streaming')
const options = {
framerate: event.framerate,
audioSampleRate: event.audioSampleRate,
rtmp: event.rtmp,
}
ffmpegProcess = new Ffmpeg(options);
ffmpegProcess.on("destroyed", () => ffmpegProcess = undefined);
ffmpegProcess.on('ffmpegOutput', (e) => socket.emit('ffmpegOutput', e));
// this.ffmpeg.on('error', (e: ServerError) => this.onFfmpegError(e));
ffmpegProcess.start();
if (callback) callback();
});
socket.on("stop", (callback) => {
console.log('stop streaming')
ffmpegProcess?.destroy();
});
socket.on("error", (event) => {
console.log('error')
});
socket.on("binarystream", (binaryData, callback) => {
ffmpegProcess?.sendData(binaryData)
.then(() => callback())
.catch((err) => {
console.log('sending error')
console.log(err)
socket.emit("error", err);
callback(err);
});
});
});
server.listen(port, () => {
console.log(`Server is up on port ${port}!`);
});