diff --git a/RtspClientSharp/ConnectionParameters.cs b/RtspClientSharp/ConnectionParameters.cs index 57a4319..f38988d 100644 --- a/RtspClientSharp/ConnectionParameters.cs +++ b/RtspClientSharp/ConnectionParameters.cs @@ -1,6 +1,7 @@ using System; using System.Net; using RtspClientSharp.Rtsp; +using RtspClientSharp.Utils; namespace RtspClientSharp { @@ -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); diff --git a/RtspClientSharp/Rtsp/RtspClientInternal.cs b/RtspClientSharp/Rtsp/RtspClientInternal.cs index 5784033..0e68536 100644 --- a/RtspClientSharp/Rtsp/RtspClientInternal.cs +++ b/RtspClientSharp/Rtsp/RtspClientInternal.cs @@ -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 { diff --git a/RtspClientSharp/Rtsp/RtspHttpTransportClient.cs b/RtspClientSharp/Rtsp/RtspHttpTransportClient.cs index 4816f8e..e10650c 100644 --- a/RtspClientSharp/Rtsp/RtspHttpTransportClient.cs +++ b/RtspClientSharp/Rtsp/RtspHttpTransportClient.cs @@ -26,9 +26,12 @@ 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) @@ -36,7 +39,7 @@ 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; @@ -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; diff --git a/RtspClientSharp/Rtsp/RtspTcpTransportClient.cs b/RtspClientSharp/Rtsp/RtspTcpTransportClient.cs index d225e1a..2049339 100644 --- a/RtspClientSharp/Rtsp/RtspTcpTransportClient.cs +++ b/RtspClientSharp/Rtsp/RtspTcpTransportClient.cs @@ -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; diff --git a/RtspClientSharp/Utils/NetworkClientFactory.cs b/RtspClientSharp/Utils/DefaultSocketFactory.cs similarity index 61% rename from RtspClientSharp/Utils/NetworkClientFactory.cs rename to RtspClientSharp/Utils/DefaultSocketFactory.cs index c402b81..4ef2d14 100644 --- a/RtspClientSharp/Utils/NetworkClientFactory.cs +++ b/RtspClientSharp/Utils/DefaultSocketFactory.cs @@ -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; } } -} \ No newline at end of file +} diff --git a/RtspClientSharp/Utils/ISocketFactory.cs b/RtspClientSharp/Utils/ISocketFactory.cs new file mode 100644 index 0000000..f1a64f0 --- /dev/null +++ b/RtspClientSharp/Utils/ISocketFactory.cs @@ -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(); + } +}