@@ -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+ }
0 commit comments