-
Notifications
You must be signed in to change notification settings - Fork 1
/
MediasoupOptions.cs
217 lines (208 loc) · 8.79 KB
/
MediasoupOptions.cs
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
211
212
213
214
215
216
217
using Antelcat.MediasoupSharp.AspNetCore.Extensions;
using Antelcat.MediasoupSharp.FBS.RtpParameters;
using Antelcat.MediasoupSharp.FBS.Transport;
namespace Antelcat.MediasoupSharp.Demo;
public abstract class MediasoupOptions
{
public const string MEDIASOUP_LISTEN_IP = nameof(MEDIASOUP_LISTEN_IP);
public const string MEDIASOUP_ANNOUNCED_IP = nameof(MEDIASOUP_ANNOUNCED_IP);
public const string MEDIASOUP_MIN_PORT = nameof(MEDIASOUP_MIN_PORT);
public const string MEDIASOUP_MAX_PORT = nameof(MEDIASOUP_MAX_PORT);
public const string MEDIASOUP_WORKER_NUM = nameof(MEDIASOUP_WORKER_NUM);
}
public class MediasoupOptions<T> : MediasoupOptions
{
public int? NumWorkers { get; set; } = Environment.ProcessorCount;
public WorkerSettings<T>? WorkerSettings { get; init; }
public RouterOptions<T>? RouterOptions { get; init; }
public WebRtcServerOptions<T>? WebRtcServerOptions { get; init; }
public WebRtcTransportOptions<T>? WebRtcTransportOptions { get; init; }
public PlainTransportOptions<T>? PlainTransportOptions { get; init; }
// mediasoup settings.
public static MediasoupOptions<T> Default => new()
{
// Number of mediasoup workers to launch.
NumWorkers = Env<int>(MEDIASOUP_WORKER_NUM) ?? Environment.ProcessorCount,
// mediasoup WorkerSettings.
// See https://mediasoup.org/documentation/v3/mediasoup/api/#WorkerSettings
WorkerSettings = new()
{
LogLevel = WorkerLogLevel.Warn,
LogTags =
[
WorkerLogTag.Info,
WorkerLogTag.Ice,
WorkerLogTag.Dtls,
WorkerLogTag.Rtp,
WorkerLogTag.Srtp,
WorkerLogTag.Rtcp,
WorkerLogTag.Rtx,
WorkerLogTag.Bwe,
WorkerLogTag.Score,
WorkerLogTag.Simulcast,
WorkerLogTag.Svc,
WorkerLogTag.Sctp,
],
DisableLiburing = false
},
// mediasoup Router options.
// See https://mediasoup.org/documentation/v3/mediasoup/api/#RouterOptions
RouterOptions = new()
{
MediaCodecs =
[
new()
{
Kind = MediaKind.AUDIO,
MimeType = "audio/opus",
ClockRate = 48000,
Channels = 2
},
new()
{
Kind = MediaKind.VIDEO,
MimeType = "video/VP8",
ClockRate = 90000,
Parameters = new()
{
{ "x-google-start-bitrate", 1000 }
}
},
new()
{
Kind = MediaKind.VIDEO,
MimeType = "video/VP9",
ClockRate = 90000,
Parameters = new()
{
{ "profile-id", 2 },
{ "x-google-start-bitrate", 1000 }
}
},
new()
{
Kind = MediaKind.VIDEO,
MimeType = "video/h264",
ClockRate = 90000,
Parameters = new()
{
{ "packetization-mode", 1 },
{ "profile-level-id", "4d0032" },
{ "level-asymmetry-allowed", 1 },
{ "x-google-start-bitrate", 1000 }
}
},
new()
{
Kind = MediaKind.VIDEO,
MimeType = "video/h264",
ClockRate = 90000,
Parameters = new()
{
{ "packetization-mode", 1 },
{ "profile-level-id", "42e01f" },
{ "level-asymmetry-allowed", 1 },
{ "x-google-start-bitrate", 1000 }
}
}
],
},
// mediasoup WebRtcServer options for WebRTC endpoints (mediasoup-client,
// libmediasoupclient).
// See https://mediasoup.org/documentation/v3/mediasoup/api/#WebRtcServerOptions
// NOTE: mediasoup-demo/server/lib/Room.js will increase this port for
// each mediasoup Worker since each Worker is a separate process.
WebRtcServerOptions = new()
{
ListenInfos =
[
new()
{
Protocol = Protocol.UDP,
Ip = Env(MEDIASOUP_LISTEN_IP) ?? "0.0.0.0",
AnnouncedAddress = Env(MEDIASOUP_ANNOUNCED_IP) ?? IPAddressExtensions.GetLocalIPv4String(),
Port = 44444,
Flags = new(),
PortRange = new()
},
new()
{
Protocol = Protocol.TCP,
Ip = Env(MEDIASOUP_LISTEN_IP) ?? "0.0.0.0",
AnnouncedAddress = Env(MEDIASOUP_ANNOUNCED_IP) ?? IPAddressExtensions.GetLocalIPv4String(),
Port = 44444,
Flags = new(),
PortRange = new()
}
]
},
// mediasoup WebRtcTransport options for WebRTC endpoints (mediasoup-client,
// libmediasoupclient).
// See https://mediasoup.org/documentation/v3/mediasoup/api/#WebRtcTransportOptions
WebRtcTransportOptions = new AdditionalWebRtcTransportOptions<T>
{
// listenInfos is not needed since webRtcServer is used.
// However passing MEDIASOUP_USE_WEBRTC_SERVER=false will change it.
ListenInfos =
[
new()
{
Protocol = Protocol.UDP,
Ip = Env(MEDIASOUP_LISTEN_IP) ?? "0.0.0.0",
AnnouncedAddress = Env(MEDIASOUP_ANNOUNCED_IP) ?? IPAddressExtensions.GetLocalIPv4String(),
Flags = new(),
PortRange = new()
{
Min = Env<ushort>(MEDIASOUP_MIN_PORT) ?? 40000,
Max = Env<ushort>(MEDIASOUP_MAX_PORT) ?? 49999
}
},
new()
{
Protocol = Protocol.TCP,
Ip = Env(MEDIASOUP_LISTEN_IP) ?? "0.0.0.0",
AnnouncedAddress = Env(MEDIASOUP_ANNOUNCED_IP) ?? IPAddressExtensions.GetLocalIPv4String(),
Flags = new(),
PortRange = new()
{
Min = Env<ushort>(MEDIASOUP_MIN_PORT) ?? 40000,
Max = Env<ushort>(MEDIASOUP_MAX_PORT) ?? 49999
}
}
],
InitialAvailableOutgoingBitrate = 1_000_000,
MinimumAvailableOutgoingBitrate = 600_000,
MaxSctpMessageSize = 262_144,
// Additional options that are not part of WebRtcTransportOptions.
MaxIncomingBitrate = 1_500_000,
},
// mediasoup PlainTransport options for legacy RTP endpoints (FFmpeg,
// GStreamer).
// See https://mediasoup.org/documentation/v3/mediasoup/api/#PlainTransportOptions
PlainTransportOptions = new()
{
ListenInfo = new()
{
Protocol = Protocol.UDP,
Ip = Env(MEDIASOUP_LISTEN_IP) ?? "0.0.0.0",
AnnouncedAddress = Env(MEDIASOUP_ANNOUNCED_IP) ?? IPAddressExtensions.GetLocalIPv4String(),
Flags = new(),
PortRange = new()
{
Min = Env<ushort>(MEDIASOUP_MIN_PORT) ?? 40000,
Max = Env<ushort>(MEDIASOUP_MAX_PORT) ?? 49999
}
},
MaxSctpMessageSize = 262144
}
};
private static TNumber? Env<TNumber>(string variable) where TNumber : struct, IParsable<TNumber> =>
Environment.GetEnvironmentVariable(variable) is { } str ? TNumber.Parse(str, null) : null;
private static string? Env(string variable) => Environment.GetEnvironmentVariable(variable);
}
public record AdditionalWebRtcTransportOptions<T> : WebRtcTransportOptions<T>
{
public uint? MinimumAvailableOutgoingBitrate { get; set; }
// Additional options that are not part of WebRtcTransportOptions.
public uint? MaxIncomingBitrate { get; set; }
}