-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
63 lines (55 loc) · 1.64 KB
/
index.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
import { PlainTransport } from 'mediasoup/lib/PlainTransport';
import { Producer } from 'mediasoup/lib/Producer';
import { Router } from 'mediasoup/lib/Router';
import config from './config';
import { FFMpegService } from './ffmpeg';
const createTransport = async (router: Router) => {
const transport = await router.createPlainTransport({
...config.mediasoup.plainTransportOptions,
rtcpMux: false,
comedia: false
});
// you should dynamic apply rtpPort and rtpPort
// tip: rtcpPort = rtpPort + 1
const rtpPort = 5000;
const rtcpPort = 5001;
await transport.connect({
ip: config.mediasoup.plainTransportOptions.listenIp.ip,
port: rtpPort,
rtcpPort: rtcpPort
});
return transport;
};
const createConsumer = async (
router: Router,
transport: PlainTransport,
producer: Producer
) => {
const rtpCapabilities = {
codecs: router.rtpCapabilities.codecs.filter((codec) => {
if (codec.kind !== producer.kind) return false;
if (producer.kind === 'video') {
return codec.mimeType.toLowerCase().indexOf('h264') > -1;
}
return true;
}),
rtcpFeedback: []
};
return await transport.consume({
producerId: producer.id,
rtpCapabilities,
paused: true
});
};
const run = async (router: Router, producer: Producer) => {
const transport = await createTransport(router);
const consumer = await createConsumer(router, transport, producer);
const service = new FFMpegService({ transport, producer, consumer });
service.run();
await Promise.resolve((resolve: () => void) => {
setTimeout(async () => {
await consumer.resume();
resolve();
}, 1000);
});
};