Skip to content

Commit

Permalink
Merge branch 'master' into gh-pages
Browse files Browse the repository at this point in the history
  • Loading branch information
sipsorcery committed Nov 4, 2024
2 parents 2a855a7 + 39896a2 commit 8f11666
Show file tree
Hide file tree
Showing 18 changed files with 323 additions and 669 deletions.
61 changes: 42 additions & 19 deletions examples/webrtccmdline/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public class Options
public bool UseSecureWebSocket { get; set; }

[Option("wsport", Required = false,
HelpText = "The port to use for the web socket server. If not set defualt of 8081 will be used. Format \"--wsport 18081\".")]
HelpText = "The port to use for the web socket server. If not set default of 8081 will be used. Format \"--wsport 18081\".")]
public int WebSocketServerPort { get; set; }

[Option("wsserver", Required = false,
HelpText = "The address of a web socket server to connect to establish a WebRTC connection. Format \"--wsserver=ws://127.0.0.1:8081\".")]
[Option("wsclient", Required = false,
HelpText = "The address of a web socket server to connect to establish a WebRTC connection. Format \"--wsclient ws://127.0.0.1:8081\".")]
public string WebSocketServer { get; set; }

[Option("offer", Required = false,
Expand Down Expand Up @@ -309,32 +309,55 @@ private static async Task RunCommand(Options options, bool noOptions)
}
else if (options.EchoServerUrl != null)
{
ManualResetEvent gatheringComplete = new ManualResetEvent(false);

// Create offer and send to echo server.
var pc = await Createpc(null, _stunServer, _relayOnly, _noAudioTrack, _noDataChannel, _useRsa, _rtpPort);

var signaler = new HttpClient();
pc.onicegatheringstatechange += (iceGatheringState) =>
{
logger.LogDebug($"ICE gathering state changed to {iceGatheringState}.");
var offer = pc.createOffer(null);
await pc.setLocalDescription(offer);
if (iceGatheringState == RTCIceGatheringState.complete)
{
gatheringComplete.Set();
}
};

var content = new StringContent(offer.toJSON(), Encoding.UTF8, "application/json");
var response = await signaler.PostAsync($"{options.EchoServerUrl}", content);
var answerStr = await response.Content.ReadAsStringAsync();
logger.LogDebug("Waiting for ICE gaterhing to complete.");

if (RTCSessionDescriptionInit.TryParse(answerStr, out var answerInit))
if (!gatheringComplete.WaitOne(TimeSpan.FromSeconds(10)))
{
var setAnswerResult = pc.setRemoteDescription(answerInit);
if (setAnswerResult != SetDescriptionResultEnum.OK)
{
Console.WriteLine($"Set remote description failed {setAnswerResult}.");
}
logger.LogWarning($"Gave up after 10s waiting for ICE gathering to complete.");
}
else
{
Console.WriteLine("Failed to parse SDP answer from echo server.");
logger.LogDebug($"Attempting to send offer to echo server at {options.EchoServerUrl}.");

var signaler = new HttpClient();

var offer = pc.createOffer(null);
await pc.setLocalDescription(offer);

var content = new StringContent(offer.toJSON(), Encoding.UTF8, "application/json");
var response = await signaler.PostAsync($"{options.EchoServerUrl}", content);
var answerStr = await response.Content.ReadAsStringAsync();

if (RTCSessionDescriptionInit.TryParse(answerStr, out var answerInit))
{
var setAnswerResult = pc.setRemoteDescription(answerInit);
if (setAnswerResult != SetDescriptionResultEnum.OK)
{
Console.WriteLine($"Set remote description failed {setAnswerResult}.");
}
}
else
{
Console.WriteLine("Failed to parse SDP answer from echo server.");
}
}
}
else if(options.ActAsEchoServer)
else if (options.ActAsEchoServer)
{
var echoServer = new EchoServer();
echoServer.Start();
Expand Down Expand Up @@ -492,7 +515,7 @@ private async static Task ProcessInput(CancellationTokenSource cts)
{
byte dtmfByte = 0x03;

if(x.Length >= 6)
if (x.Length >= 6)
{
dtmfByte = (byte)x.Substring(5).ToCharArray()[0];
}
Expand Down Expand Up @@ -704,7 +727,7 @@ private async static Task<RTCPeerConnection> Createpc(WebSocketContext context,

_peerConnection.OnRtpEvent += (ep, ev, hdr) =>
{
if(_rtpEventSsrc == 0)
if (_rtpEventSsrc == 0)
{
if (ev.EndOfEvent && hdr.MarkerBit == 1)
{
Expand Down
87 changes: 19 additions & 68 deletions src/net/DtlsSrtp/DtlsSrtpTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
//-----------------------------------------------------------------------------

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Linq;
using Microsoft.Extensions.Logging;
using Org.BouncyCastle.Crypto.Tls;
using Org.BouncyCastle.Security;
Expand Down Expand Up @@ -400,51 +398,27 @@ public int UnprotectRTP(byte[] payload, int length, out int outLength)
}

public byte[] ProtectRTP(byte[] packet, int offset, int length)
{
var buffer=ArrayPool<byte>.Shared.Rent(packet.Length * 2);
try
{
var resultLength = ProtectRTP(packet, offset, length, buffer, packet.Length * 2);
var segment=new ArraySegment<byte>(buffer, 0, resultLength);
return segment.ToArray();
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}

}
public int ProtectRTP(byte[] packet, int offset, int length,byte[] buffer,int bufferLength)
{
lock (this.srtpEncoder)
{
return this.srtpEncoder.Transform(packet, offset, length,buffer,bufferLength);
return this.srtpEncoder.Transform(packet, offset, length);
}
}

public int ProtectRTP(byte[] payload, int length, out int outLength)
{
var resultBuf=ArrayPool<byte>.Shared.Rent(length * 2);
try
{
var resultSize = ProtectRTP(payload, 0, length,resultBuf,length * 2);

if (resultSize <1)
{
outLength = 0;
return -1;
}

System.Buffer.BlockCopy(resultBuf, 0, payload, 0, resultSize);
outLength = resultSize;
var result = ProtectRTP(payload, 0, length);

return 0; //No Errors
}
finally
if (result == null)
{
ArrayPool<byte>.Shared.Return(resultBuf);
outLength = 0;
return -1;
}


System.Buffer.BlockCopy(result, 0, payload, 0, result.Length);
outLength = result.Length;

return 0; //No Errors
}

public byte[] UnprotectRTCP(byte[] packet, int offset, int length)
Expand All @@ -471,49 +445,26 @@ public int UnprotectRTCP(byte[] payload, int length, out int outLength)
}

public byte[] ProtectRTCP(byte[] packet, int offset, int length)
{
var buffer=ArrayPool<byte>.Shared.Rent(packet.Length * 2);
try
{
var resultLength = ProtectRTCP(packet, offset, length, buffer, packet.Length * 2);
var segment=new ArraySegment<byte>(buffer, 0, resultLength);
return segment.ToArray();
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
}
public int ProtectRTCP(byte[] packet, int offset, int length,byte[] buffer,int bufferLength)
{
lock (this.srtcpEncoder)
{
return this.srtcpEncoder.Transform(packet, offset, length,buffer,bufferLength);
return this.srtcpEncoder.Transform(packet, offset, length);
}
}

public int ProtectRTCP(byte[] payload, int length, out int outLength)
{
var buff=ArrayPool<byte>.Shared.Rent(length * 2);
try
var result = ProtectRTCP(payload, 0, length);
if (result == null)
{
var resultSize = ProtectRTCP(payload, 0, length,buff,length * 2);
if (resultSize<0)
{
outLength = 0;
return -1;
}
outLength = 0;
return -1;
}

System.Buffer.BlockCopy(buff, 0, payload, 0, resultSize);
outLength = resultSize;
System.Buffer.BlockCopy(result, 0, payload, 0, result.Length);
outLength = result.Length;

return 0; //No Errors
}
finally
{
ArrayPool<byte>.Shared.Return(buff);
}

return 0; //No Errors
}

/// <summary>
Expand Down
90 changes: 20 additions & 70 deletions src/net/DtlsSrtp/SrtpHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
//-----------------------------------------------------------------------------

using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -190,53 +189,29 @@ public int UnprotectRTP(byte[] payload, int length, out int outLength)

return 0; //No Errors
}

public byte[] ProtectRTP(byte[] packet, int offset, int length)
{
var buffer=ArrayPool<byte>.Shared.Rent(packet.Length * 2);
try
{
var resultLength = ProtectRTP(packet, offset, length, buffer, packet.Length * 2);
var segment=new ArraySegment<byte>(buffer, 0, resultLength);
return segment.ToArray();
}
finally
lock (SrtpEncoder)
{
ArrayPool<byte>.Shared.Return(buffer);
}
}

public int ProtectRTP(byte[] packet, int offset, int length,byte[] buffer,int bufferLength)
{
lock (this.SrtpEncoder)
{
return this.SrtpEncoder.Transform(packet, offset, length,buffer,bufferLength);
return SrtpEncoder.Transform(packet, offset, length);
}
}

public int ProtectRTP(byte[] payload, int length, out int outLength)
{
var resultBuf=ArrayPool<byte>.Shared.Rent(length * 2);
try
{
var resultSize = ProtectRTP(payload, 0, length,resultBuf,length * 2);

if (resultSize <1)
{
outLength = 0;
return -1;
}
var result = ProtectRTP(payload, 0, length);

System.Buffer.BlockCopy(resultBuf, 0, payload, 0, resultSize);
outLength = resultSize;

return 0; //No Errors
}
finally
if (result == null)
{
ArrayPool<byte>.Shared.Return(resultBuf);
outLength = 0;
return -1;
}

System.Buffer.BlockCopy(result, 0, payload, 0, result.Length);
outLength = result.Length;

return 0; //No Errors
}

public byte[] UnprotectRTCP(byte[] packet, int offset, int length)
Expand All @@ -263,51 +238,26 @@ public int UnprotectRTCP(byte[] payload, int length, out int outLength)
}

public byte[] ProtectRTCP(byte[] packet, int offset, int length)
{
var buffer=ArrayPool<byte>.Shared.Rent(packet.Length * 2);
try
{
var resultLength = ProtectRTCP(packet, offset, length, buffer, packet.Length * 2);
var segment=new ArraySegment<byte>(buffer, 0, resultLength);
return segment.ToArray();
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
}
public int ProtectRTCP(byte[] packet, int offset, int length, byte[] buffer,int bufferLength)
{
lock (SrtcpEncoder)
{
return SrtcpEncoder.Transform(packet, offset, length,buffer,bufferLength);
return SrtcpEncoder.Transform(packet, offset, length);
}
}

public int ProtectRTCP(byte[] payload, int length, out int outLength)
{
var resultBuf=ArrayPool<byte>.Shared.Rent(length * 2);
try
var result = ProtectRTCP(payload, 0, length);
if (result == null)
{
var resultSize = ProtectRTCP(payload, 0, length,resultBuf,length * 2);

if (resultSize <1)
{
outLength = 0;
return -1;
}
outLength = 0;
return -1;
}

System.Buffer.BlockCopy(resultBuf, 0, payload, 0, resultSize);
outLength = resultSize;
System.Buffer.BlockCopy(result, 0, payload, 0, result.Length);
outLength = result.Length;

return 0; //No Errors
}
finally
{
ArrayPool<byte>.Shared.Return(resultBuf);
}


return 0; //No Errors
}
}
}
Loading

0 comments on commit 8f11666

Please sign in to comment.