-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
141 lines (119 loc) · 4.19 KB
/
server.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
const express = require('express');
const fs = require('fs');
const http = require('http');
const socketIO = require('socket.io');
const ffmpeg = require('fluent-ffmpeg');
const { PassThrough } = require('stream');
const { spawn } = require('child_process');
const cors = require('cors');
// Create Express app
const app = express();
app.use(cors());
const server = http.createServer(app);
let videoStream;
// Set up Socket.IO
const io = require('socket.io')(server,
{
cors: {
origin: ['https://devapp.egame-studio.com','https://demoapp.egame-studio.com'], // Replace with the appropriate client origin
methods: ['GET', 'POST'],
allowedHeaders: ['Content-Type'],
credentials: true,
},
});
// Event triggered when a client connects
io.on('connection', (socket) => {
console.log('New client connected');
// Event triggered when the client disconnects
videoStream = new PassThrough();
socket.on('disconnect', () => {
console.log('Client disconnected');
videoStream.end();
if(command != undefined){
command.kill('SIGINT'); // Terminate FFmpeg process
}
});
socket.on('error', (error) => {
// Handle the error
console.error('Socket error:', error);
});
socket.on('videoChunk', (stream,streamURL) => {
console.log("videoChunk",stream)
// videoStream.write(stream);
if (Buffer.isBuffer(stream)) {
// Data is a valid buffer
if (videoStream.writable) {
videoStream.write(stream);
console.error('writing Video stream');
} else {
console.error('Video stream is not writable.');
}
} else {
console.error('Invalid data format. Expected a buffer.');
}
// videoStream.pipe(command.stdin);
// videoStream.on('error', (error) => {
// console.error('videoStream error:', error);
// });
// // Log the number of bytes written to the videoStream
// videoStream.on('data', (chunk) => {
// console.log('Bytes written to videoStream:', chunk.length);
// });
// videoStream.on('end', () => {
// console.log('Video strea end:');
// });
// videoStream.on('error', (error) => {
// console.log('Video stream error:',error);
// });
})
videoStream.on('drain', () => {
console.log('Video stream drained');
videoStream.resume();
});
videoStream.on('finish', () => {
console.log('Video stream finished writing');
videoStream.destroy();
});
videoStream.on('error', (error) => {
console.error('Video stream error:', error);
});
videoStream.setMaxListeners(1000);
socket.on('startStreamingToYouTube',(rmtpurl)=>{
startStreaming(rmtpurl)
})
let command;
// Function to start streaming to YouTube using FFmpeg
const startStreaming = (rtmpUrl) => {
// const rtmpUrl = "rtmp://live.twitch.tv/app/live_938994094_leJ6Skyqg8bdsWyia5YBXuQ1VkSb69"
// const rtmpUrl = 'rtmp://a.rtmp.youtube.com/live2/8mg7-ze8d-b6ps-ck6t-5mur';
// const rtmpUrl = "rtmps://live-api-s.facebook.com:443/rtmp/FB-122100403328001814-0-Aby4F71qXw4JlfAn"
// const rtmpUrl = "rtmps://fa723fc1b171.global-contribute.live-video.net/sk_us-west-2_EjpVaJb4OyJe_dd3u67DhkmFuJzlLGoSrmFXcZ96xpz"
console.log('Video Streaming Started',rtmpUrl);
// Create an FFmpeg command and set the input stream
command = spawn('ffmpeg', [
'-i', // Input from stdin
'-', // Use hyphen to specify stdin as input source
'-c:v', 'libx264', // Video codec
'-preset', 'ultrafast', // Video encoding preset
'-tune', 'zerolatency', // Tune for real-time streaming
'-c:a', 'aac', // Audio codec
'-b:a', '128k', // Audio bitrate
'-vf', 'scale=1280:720', // Adjust the resolution as needed
'-f', 'flv', // Output format (FLV for YouTube streaming)
rtmpUrl, // RTMP URL for YouTube live stream
]);
// Pipe the input stream to the FFmpeg process
videoStream.pipe(command.stdin);
// Handle FFmpeg process events
command.on('error', (error) => {
console.error('FFmpeg process encountered an error:', error);
});
command.on('exit', (code, signal) => {
console.log('FFmpeg process exited with code:', code, 'and signal:', signal);
});
};
});
// Start the server
server.listen(3412, () => {
console.log('Socket server listening on port 3412');
});