Skip to content

Commit 0f60bc3

Browse files
authored
Webrtc ffmpeg example turn (#1442)
* wip: add turn option to FFmpeg example. * Adjusted webrtc ffmpeg example to optioanlly use a TURN server.
1 parent 5849e4d commit 0f60bc3

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

examples/WebRTCExamples/WebRTCFFmpegGetStarted/Program.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Program
3636
private const string LINUX_FFMPEG_LIB_PATH = "/usr/local/lib/";
3737

3838
private static string _stunUrl = string.Empty;
39+
private static string _turnUrl = string.Empty;
3940
private static bool _waitForIceGatheringToSendOffer = false;
4041
private static int _webrtcBindPort = 0;
4142

@@ -46,6 +47,7 @@ static async Task Main()
4647
Console.WriteLine("WebRTC FFmpeg Get Started");
4748

4849
_stunUrl = Environment.GetEnvironmentVariable("STUN_URL");
50+
_turnUrl = Environment.GetEnvironmentVariable("TURN_URL");
4951
bool.TryParse(Environment.GetEnvironmentVariable("WAIT_FOR_ICE_GATHERING_TO_SEND_OFFER"), out _waitForIceGatheringToSendOffer);
5052
int.TryParse(Environment.GetEnvironmentVariable("BIND_PORT"), out _webrtcBindPort);
5153

@@ -70,6 +72,7 @@ static async Task Main()
7072
}
7173

7274
_logger.LogDebug(_stunUrl != null ? $"STUN URL: {_stunUrl}" : "No STUN URL provided.");
75+
_logger.LogDebug(_turnUrl != null ? $"TURN URL: {_turnUrl}" : "No TURN URL provided.");
7376
_logger.LogDebug($"Wait for ICE gathering to send offer: {_waitForIceGatheringToSendOffer}");
7477

7578
var builder = WebApplication.CreateBuilder();
@@ -97,7 +100,7 @@ static async Task Main()
97100

98101
RTCConfiguration config = new RTCConfiguration
99102
{
100-
X_ICEIncludeAllInterfaceAddresses = true
103+
X_ICEIncludeAllInterfaceAddresses = true,
101104
};
102105

103106
var webSocketPeer = new WebRTCWebSocketPeerAspNet(webSocket,
@@ -126,9 +129,16 @@ static async Task Main()
126129

127130
private static Task<RTCPeerConnection> CreatePeerConnection(RTCConfiguration config)
128131
{
132+
config.iceServers = new List<RTCIceServer>();
133+
129134
if (!string.IsNullOrWhiteSpace(_stunUrl))
130135
{
131-
config.iceServers = new List<RTCIceServer> { new RTCIceServer { urls = _stunUrl } };
136+
config.iceServers.Add(_stunUrl.ParseStunServer());
137+
}
138+
139+
if (!string.IsNullOrWhiteSpace(_turnUrl))
140+
{
141+
config.iceServers.Add(_turnUrl.ParseStunServer());
132142
}
133143

134144
var pc = new RTCPeerConnection(config, _webrtcBindPort);
@@ -192,3 +202,19 @@ private static Task<RTCPeerConnection> CreatePeerConnection(RTCConfiguration con
192202
return Task.FromResult(pc);
193203
}
194204
}
205+
206+
public static class StunServerExtensions
207+
{
208+
public static RTCIceServer ParseStunServer(this string stunServer)
209+
{
210+
var fields = stunServer.Split(';');
211+
212+
return new RTCIceServer
213+
{
214+
urls = fields[0],
215+
username = fields.Length > 1 ? fields[1] : null,
216+
credential = fields.Length > 2 ? fields[2] : null,
217+
credentialType = RTCIceCredentialType.password
218+
};
219+
}
220+
}

examples/WebRTCExamples/WebRTCFFmpegGetStarted/WebRTCFFmpegGetStarted.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010

1111
<ItemGroup>
1212
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
13+
<PackageReference Include="SIPSorcery" Version="8.0.23" />
1314
<PackageReference Include="SIPSorceryMedia.FFmpeg" Version="8.0.10" />
1415
</ItemGroup>
1516

1617
<ItemGroup>
17-
<ProjectReference Include="..\..\..\src\SIPSorcery.csproj" />
18+
<!--<ProjectReference Include="..\..\..\src\SIPSorcery.csproj" />-->
1819
</ItemGroup>
1920

2021
</Project>

0 commit comments

Comments
 (0)