-
Notifications
You must be signed in to change notification settings - Fork 3
/
ffmpeg.ts
168 lines (145 loc) · 3.66 KB
/
ffmpeg.ts
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
import { Consumer } from 'mediasoup/lib/Consumer';
import FFmpegStatic from 'ffmpeg-static';
import Process from 'child_process';
import fs from 'fs';
import path from 'path';
import { Producer } from 'mediasoup/lib/Producer';
import config from './config';
import { PlainTransport } from 'mediasoup/lib/PlainTransport';
import { getCodecInfoFromRtpParameters, getSdpText } from './sdp';
export interface FFMpegServiceOptions {
transport: PlainTransport;
producer: Producer;
consumer: Consumer;
}
export class FFMpegService {
private _transport: PlainTransport;
private _producer: Producer;
private _consumer: Consumer;
private _process: Process.ChildProcessWithoutNullStreams;
private readonly _sdpDir = path.resolve(__dirname, './sdp');
constructor({ transport, producer, consumer }: FFMpegServiceOptions) {
this._transport = transport;
this._producer = producer;
this._consumer = consumer;
this._process = null;
}
public run() {
this._createSdpFile();
this._createProcess();
this._handleProcess();
}
public async kill() {
return new Promise<void>((resolve) => {
if (!this._process) return resolve();
this._process.kill('SIGINT');
resolve();
});
}
private _getSdpFile() {
return `${this._sdpDir}/${this._producer.id}.sdp`;
}
private _createSdpFile() {
fs.mkdirSync(this._sdpDir, {
recursive: true
});
fs.writeFileSync(
this._getSdpFile(),
getSdpText(this._transport, this._producer, this._consumer)
);
}
private get _video() {
return this._producer.id === 'video';
}
private get _audio() {
return this._producer.id === 'audio';
}
private get _rtmpVideoArgs() {
if (!this._video) return [];
return [
'-map',
'0:v:0',
'-c:v',
'libx264',
'-tune',
'zerolatency',
'-preset',
'ultrafast',
'-b:v',
'600k',
'-g', // gop
'24',
'-r', // rate
'24'
];
}
private get _rtmpAudioArgs(): any[] {
if (!this._audio) return [];
const codec = getCodecInfoFromRtpParameters(
'audio',
this._consumer.rtpParameters
);
return ['-map', '0:a:0', '-c:a', 'aac', '-ar', codec.clockRate];
}
private get _rtmpLocation() {
return [
'-f',
'flv',
`rtmp://${config.rtmp.host}/${this._producer.kind}/${this._producer.id}`
];
}
private get _commandArgs() {
let args = [
'-vsync',
'1',
'-async',
'1',
'-loglevel',
'info',
'-analyzeduration',
'50000', // 50ms
'-probesize',
'1k',
'-protocol_whitelist',
'file,rtp,udp',
'-fflags',
'+genpts',
'-i',
this._getSdpFile()
];
args = args.concat(this._rtmpVideoArgs);
args = args.concat(this._rtmpAudioArgs);
args = args.concat(this._rtmpLocation);
return args;
}
private _createProcess() {
this._process = Process.spawn(FFmpegStatic, this._commandArgs, {
shell: false
});
}
private _handleProcess() {
this._process.on('error', (err) => {
console.error('publish process error:', err);
});
this._process.on('exit', (code, signal) => {
if (!signal || signal === 'SIGINT' || signal === 'SIGKILL') {
console.log('process stopped');
} else {
console.warn("process didn't exit cleanly");
}
this._process = null;
});
this._process.stderr.on('data', (chunk) => {
chunk
.toString()
.split(/\r?\n/g)
.filter(Boolean)
.forEach((line: any) => {
console.log(line);
});
});
process.on('exit', () => {
console.log('process existed');
});
}
}