-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo-streamer.js
210 lines (174 loc) · 5.39 KB
/
video-streamer.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const spawn = require('child_process').spawn,
exec = require('child_process').exec,
execFile = require('child_process').execFile,
config = require('./config.json'),
utils = require('./utils.js'),
defaultStreamPort = 5054,
defaultWidth = 640,
defaultHeight = 480,
defaultRotation = config.rotation || 0,
TAG = '[VideoStreamer]';
let audioStreamProcess,
videoStreamProcess,
fileStreamProcess,
audioStreamId,
videoStreamId,
audioStreamToken = "init_audio_token",
videoStreamToken = "init_video_token";
class VideoStreamer {
getStreamUrl (streamId, streamToken) {
const url = config.relay_server + ':' + (config.video_stream_port || defaultStreamPort) + '/' + streamId + '/' + streamToken + '/';
if (config.use_ssl) {
return 'https://' + url;
} else {
return 'http://' + url;
}
}
getAudioVideoStreamUrl (streamId) {
let audioUrl = config.relay_server + ':' + (config.video_stream_port || defaultStreamPort) + '/' + streamId + '/' + audioStreamToken + '/',
videoUrl = config.relay_server + ':' + (config.video_stream_port || defaultStreamPort) + '/' + streamId + '/' + videoStreamToken + '/';
if (config.use_ssl) {
audioUrl = 'https://' + audioUrl;
videoUrl = 'https://' + videoUrl;
} else {
audioUrl = 'http://' + audioUrl;
videoUrl = 'http://' + videoUrl;
}
let url = "[f=mpegts:select=v]" + videoUrl + "|" + "[f=mpegts:select=a]" + audioUrl;
return url;
}
streamLive (streamId, streamToken, videoDevice, {
audioDevice,
width = defaultWidth,
height = defaultHeight,
rotation = defaultRotation
} = {}) {
videoStreamToken = streamToken;
// ffmpeg -s 1280x720 -r 30 -f v4l2 -i /dev/video10 -f mpegts -vf transpose=2,transpose=1 -codec:a mp2 -ar 44100 -ac 1 -b:a 128k -codec:v mpeg1video -b:v 2000k -strict -1 test.avi
let options = [
'-f', 'v4l2',
'-r', '15',
'-s', width + 'x' + height,
'-i', videoDevice,
'-f', 'mpegts',
'-vf', this.getRotationFromDegrees(rotation),
'-codec:v', 'mpeg1video',
'-s', width + 'x' + height,
'-b:v', '1000k',
'-q:v', '0',
'-strict', '-1',
this.getStreamUrl(streamId, streamToken)
];
this.printFFmpegOptions(options);
if (videoStreamProcess) videoStreamProcess.kill();
console.log(TAG, 'Starting audio stream stream. Stream ID:', streamId);
videoStreamProcess = spawn('ffmpeg', options);
videoStreamProcess.on('close', (code) => {
console.log(TAG, `Audio stream exited with code ${code}. Stream ID:`, streamId);
});
}
streamLiveAudio (streamId, streamToken, audioDevice) {
audioStreamToken = streamToken;
let options = [
'-f', 'alsa',
'-ar', '44100',
// '-ac', '1',
'-i', audioDevice,
'-f', 'mpegts',
'-codec:a', 'mp2',
'-b:a', '128k',
'-strict', '-1',
this.getStreamUrl(streamId, streamToken)
];
this.printFFmpegOptions(options);
if (audioStreamProcess) audioStreamProcess.kill();
console.log(TAG, 'Starting live audio stream. Stream ID:', streamId);
audioStreamProcess = spawn('ffmpeg', options);
audioStreamProcess.on('close', (code) => {
console.log(TAG, `Audio stream exited with code ${code}. Stream ID:`, streamId);
});
}
streamAudioFile (streamId, streamToken, file) {
audioStreamToken = streamToken;
let options = [
'-loglevel', 'panic',
'-re',
'-i', file,
'-f', 'tee',
'-codec:v', 'mpeg1video',
'-q:v', '0',
'-b:v', '900k',
'-c:a', 'mp2',
'-flags', '+global_header',
'-b:a', '128k',
'-map', '0:a',
'-map', '0:v',
'-r', '20',
this.getAudioVideoStreamUrl(streamId)
];
let command = this.printFFmpegOptions(options);
if (fileStreamProcess) fileStreamProcess.kill();
console.log(TAG, 'Starting audio/video file stream. Stream ID:', command);
fileStreamProcess = spawn('ffmpeg', options);
fileStreamProcess.on('close', (code) => {
console.log(TAG, `Audio/video file stream exited with code ${code}. Stream ID:`, streamId);
});
}
streamFile (streamId, streamToken, file) {
videoStreamToken = streamToken;
let options = [
'-loglevel', 'panic',
'-re',
'-i', file,
'-f', 'tee',
'-codec:v', 'mpeg1video',
'-q:v', '0',
'-b:v', '900k',
'-c:a', 'mp2',
'-flags', '+global_header',
'-b:a', '128k',
'-map', '0:a',
'-map', '0:v',
'-r', '20',
this.getAudioVideoStreamUrl(streamId)
];
let command = this.printFFmpegOptions(options);
if (fileStreamProcess) fileStreamProcess.kill();
console.log(TAG, 'Starting file stream. Stream ID:', streamId);
// fileStreamProcess = exec(command);
fileStreamProcess = spawn('ffmpeg', options);
fileStreamProcess.on('close', (code) => {
console.log(TAG, `File stream exited with code ${code}. Stream ID:`, streamId);
});
}
streamFiles (streamId, streamToken, files) {
// TODO
}
stop (process) {
if (audioStreamProcess) audioStreamProcess.kill();
if (videoStreamProcess) videoStreamProcess.kill();
if (fileStreamProcess) fileStreamProcess.kill();
}
printFFmpegOptions (options) {
let options_str = 'ffmpeg';
for (let i = 0; i < options.length; i++) {
options_str += ' ' + options[i];
}
// console.log("printFFmpegOptions", options_str);
return options_str;
}
getRotationFromDegrees (degree) {
switch (Number(degree)) {
case 90:
return 'transpose=2';
case 180:
return 'transpose=2,transpose=2';
case 270:
return 'transpose=1';
case 0:
default:
return 'transpose=2,transpose=1';
}
}
}
module.exports = new VideoStreamer();