-
Notifications
You must be signed in to change notification settings - Fork 3
/
sdp.ts
78 lines (67 loc) · 2.43 KB
/
sdp.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
import { Consumer } from 'mediasoup/lib/Consumer';
import { PlainTransport } from 'mediasoup/lib/PlainTransport';
import { Producer } from 'mediasoup/lib/Producer';
import { MediaKind, RtpParameters } from 'mediasoup/lib/RtpParameters';
import config from './config';
const parseParams = (obj: any) => {
if (!obj) return;
const keys = Object.keys(obj);
if (!keys.length) return;
const params = [];
// level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f
keys.forEach((key) => {
params.push(`${key}=${obj[key]}`);
});
return params.join(';');
};
export const getCodecInfoFromRtpParameters = (
kind: MediaKind,
rtpParameters: RtpParameters
) => {
const codec = rtpParameters.codecs[0];
const encoding = rtpParameters.encodings[0];
return {
payloadType: codec.payloadType,
codecName: codec.mimeType.replace(`${kind}/`, '').toLowerCase(),
clockRate: codec.clockRate,
parameters: parseParams(codec.parameters),
channels: kind === 'audio' ? codec.channels : undefined,
ssrc: encoding.ssrc
};
};
export const getSdpText = (
transport: PlainTransport,
producer: Producer,
consumer: Consumer
) => {
let sdpText = '';
const streamIp = config.mediasoup.plainTransportOptions.listenIp.ip;
sdpText += 'v=0\n';
sdpText += `o=- 0 0 IN IP4 ${streamIp}\n`;
sdpText += 's=FFmpeg\n';
sdpText += `c=IN IP4 ${streamIp}\n`;
sdpText += 't=0 0\n';
const remoteRtpPort = transport.tuple.remotePort;
const remoteRtcpPort = transport.rtcpTuple.remotePort;
const rtpParameters = consumer.rtpParameters;
const codec = getCodecInfoFromRtpParameters(producer.kind, rtpParameters);
if (producer.kind === 'video') {
sdpText += `m=video ${remoteRtpPort} RTP/AVPF ${codec.payloadType}\n`;
sdpText += `a=ssrc:${codec.ssrc}\n`;
sdpText += `a=rtcp:${remoteRtcpPort}\n`;
sdpText += `a=rtpmap:${codec.payloadType} ${codec.codecName}/${codec.clockRate}\n`;
if (codec.parameters) {
sdpText += `a=fmtp:${codec.payloadType} ${codec.parameters}\n`;
}
}
if (producer.kind === 'audio') {
sdpText += `m=audio ${remoteRtpPort} RTP/AVPF ${codec.payloadType}\n`;
sdpText += `a=ssrc:${codec.ssrc}\n`;
sdpText += `a=rtcp:${remoteRtcpPort}\n`;
sdpText += `a=rtpmap:${codec.payloadType} ${codec.codecName}/${codec.clockRate}/${codec.channels}\n`;
if (codec.parameters) {
sdpText += `a=fmtp:${codec.payloadType} ${codec.parameters}\n`;
}
}
return sdpText;
};