Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow passing in custom socket factory #96

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions RtspClientSharp/ConnectionParameters.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Net;
using RtspClientSharp.Rtsp;
using RtspClientSharp.Utils;

namespace RtspClientSharp
{
Expand All @@ -26,8 +27,11 @@ public class ConnectionParameters
public TimeSpan ReceiveTimeout { get; set; } = TimeSpan.FromSeconds(10);
public TimeSpan CancelTimeout { get; set; } = TimeSpan.FromSeconds(5);
public string UserAgent { get; set; } = DefaultUserAgent;

public RtpTransportProtocol RtpTransport { get; set; } = RtpTransportProtocol.TCP;

public ISocketFactory SocketFactory { get; set; } = DefaultSocketFactory.Instance;

public ConnectionParameters(Uri connectionUri)
{
ValidateUri(connectionUri);
Expand Down
4 changes: 2 additions & 2 deletions RtspClientSharp/Rtsp/RtspClientInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ private async Task SetupTrackAsync(RtspMediaTrackInfo track, CancellationToken t

if (_connectionParameters.RtpTransport == RtpTransportProtocol.UDP)
{
rtpClient = NetworkClientFactory.CreateUdpClient();
rtcpClient = NetworkClientFactory.CreateUdpClient();
rtpClient = _connectionParameters.SocketFactory.CreateUdpSocket();
rtcpClient = _connectionParameters.SocketFactory.CreateUdpSocket();

try
{
Expand Down
7 changes: 5 additions & 2 deletions RtspClientSharp/Rtsp/RtspHttpTransportClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ class RtspHttpTransportClient : RtspTransportClient

public override EndPoint RemoteEndPoint => _remoteEndPoint;

public readonly ISocketFactory _tcpSocketFactory;

public RtspHttpTransportClient(ConnectionParameters connectionParameters)
: base(connectionParameters)
{
_tcpSocketFactory = connectionParameters.SocketFactory;
}

public override async Task ConnectAsync(CancellationToken token)
{
_commandCounter = 0;
_sessionCookie = Guid.NewGuid().ToString("N").Substring(0, 10);

_streamDataClient = NetworkClientFactory.CreateTcpClient();
_streamDataClient = _tcpSocketFactory.CreateTcpSocket();

Uri connectionUri = ConnectionParameters.ConnectionUri;

Expand Down Expand Up @@ -114,7 +117,7 @@ public override void Dispose()

protected override async Task WriteAsync(byte[] buffer, int offset, int count)
{
using (_commandsClient = NetworkClientFactory.CreateTcpClient())
using (_commandsClient = _tcpSocketFactory.CreateTcpSocket())
{
Uri connectionUri = ConnectionParameters.ConnectionUri;

Expand Down
5 changes: 4 additions & 1 deletion RtspClientSharp/Rtsp/RtspTcpTransportClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ class RtspTcpTransportClient : RtspTransportClient

public override EndPoint RemoteEndPoint => _remoteEndPoint;

public readonly ISocketFactory _tcpSocketFactory;

public RtspTcpTransportClient(ConnectionParameters connectionParameters)
: base(connectionParameters)
{
_tcpSocketFactory = connectionParameters.SocketFactory;
}

public override async Task ConnectAsync(CancellationToken token)
{
_tcpClient = NetworkClientFactory.CreateTcpClient();
_tcpClient = _tcpSocketFactory.CreateTcpSocket();

Uri connectionUri = ConnectionParameters.ConnectionUri;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
using System.Net.Sockets;
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;

namespace RtspClientSharp.Utils
{
static class NetworkClientFactory
public class DefaultSocketFactory : ISocketFactory
{
private const int TcpReceiveBufferDefaultSize = 64 * 1024;
private static ISocketFactory _instance;
public static ISocketFactory Instance => _instance = (_instance ?? new DefaultSocketFactory());
private DefaultSocketFactory()
{
}

private const int UdpReceiveBufferDefaultSize = 128 * 1024;
private const int SIO_UDP_CONNRESET = -1744830452;
private static readonly byte[] EmptyOptionInValue = { 0, 0, 0, 0 };

public static Socket CreateTcpClient()
public Socket CreateUdpSocket()
{
var socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)
var socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)
{
ReceiveBufferSize = TcpReceiveBufferDefaultSize,
DualMode = true,
NoDelay = true
ReceiveBufferSize = UdpReceiveBufferDefaultSize,
DualMode = true
};
socket.IOControl((IOControlCode)SIO_UDP_CONNRESET, EmptyOptionInValue, null);
return socket;
}

public static Socket CreateUdpClient()
private const int TcpReceiveBufferDefaultSize = 64 * 1024;
public Socket CreateTcpSocket()
{
var socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)
return new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)
{
ReceiveBufferSize = UdpReceiveBufferDefaultSize,
DualMode = true
ReceiveBufferSize = TcpReceiveBufferDefaultSize,
DualMode = true,
NoDelay = true
};
socket.IOControl((IOControlCode)SIO_UDP_CONNRESET, EmptyOptionInValue, null);
return socket;
}
}
}
}
13 changes: 13 additions & 0 deletions RtspClientSharp/Utils/ISocketFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;

namespace RtspClientSharp.Utils
{
public interface ISocketFactory
{
Socket CreateTcpSocket();
Socket CreateUdpSocket();
}
}