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 creating and passing in your own socket creators (needed for Android/iOS) #111

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Examples/libffmpeghelper/libffmpeghelper.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,32 @@
<ProjectGuid>{3C96BD24-3212-4CD8-86E3-63D1E3E38155}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>libffmpeghelper</RootNamespace>
<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
Expand Down
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
18 changes: 9 additions & 9 deletions RtspClientSharp/Rtsp/RtspClientInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ sealed class RtspClientInternal : IDisposable
private readonly RtspRequestMessageFactory _requestMessageFactory;

private readonly Dictionary<int, ITransportStream> _streamsMap = new Dictionary<int, ITransportStream>();
private readonly ConcurrentDictionary<int, Socket> _udpClientsMap = new ConcurrentDictionary<int, Socket>();
private readonly ConcurrentDictionary<int, IRtspSocket> _udpClientsMap = new ConcurrentDictionary<int, IRtspSocket>();

private readonly Dictionary<int, RtcpReceiverReportsProvider> _reportProvidersMap =
new Dictionary<int, RtcpReceiverReportsProvider>();
Expand Down Expand Up @@ -185,13 +185,13 @@ private async Task SetupTrackAsync(RtspMediaTrackInfo track, CancellationToken t

int rtpChannelNumber;
int rtcpChannelNumber;
Socket rtpClient = null;
Socket rtcpClient = null;
IRtspSocket rtpClient = null;
IRtspSocket rtcpClient = null;

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

try
{
Expand Down Expand Up @@ -523,10 +523,10 @@ private Task ReceiveOverUdpAsync(CancellationToken token)
{
var waitList = new List<Task>(_udpClientsMap.Count / 2);

foreach (KeyValuePair<int, Socket> pair in _udpClientsMap)
foreach (KeyValuePair<int, IRtspSocket> pair in _udpClientsMap)
{
int channelNumber = pair.Key;
Socket client = pair.Value;
IRtspSocket client = pair.Value;

ITransportStream transportStream = _streamsMap[channelNumber];

Expand All @@ -546,7 +546,7 @@ private Task ReceiveOverUdpAsync(CancellationToken token)
return Task.WhenAll(waitList);
}

private async Task ReceiveRtpFromUdpAsync(Socket client, RtpStream rtpStream,
private async Task ReceiveRtpFromUdpAsync(IRtspSocket client, RtpStream rtpStream,
RtcpReceiverReportsProvider reportsProvider,
CancellationToken token)
{
Expand Down Expand Up @@ -578,7 +578,7 @@ private async Task ReceiveRtpFromUdpAsync(Socket client, RtpStream rtpStream,
}
}

private static async Task ReceiveRtcpFromUdpAsync(Socket client, ITransportStream stream,
private static async Task ReceiveRtcpFromUdpAsync(IRtspSocket client, ITransportStream stream,
CancellationToken token)
{
var readBuffer = new byte[Constants.UdpReceiveBufferSize];
Expand Down
13 changes: 8 additions & 5 deletions RtspClientSharp/Rtsp/RtspHttpTransportClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace RtspClientSharp.Rtsp
{
class RtspHttpTransportClient : RtspTransportClient
{
private Socket _streamDataClient;
private Socket _commandsClient;
private IRtspSocket _streamDataClient;
private IRtspSocket _commandsClient;
private string _sessionCookie;
private Authenticator _authenticator;
private Stream _dataNetworkStream;
Expand All @@ -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 All @@ -45,7 +48,7 @@ public override async Task ConnectAsync(CancellationToken token)
await _streamDataClient.ConnectAsync(connectionUri.Host, httpPort);

_remoteEndPoint = _streamDataClient.RemoteEndPoint;
_dataNetworkStream = new NetworkStream(_streamDataClient, false);
_dataNetworkStream = _streamDataClient.CreateNetworkStream();

string request = ComposeGetRequest();
byte[] requestBytes = Encoding.ASCII.GetBytes(request);
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
9 changes: 6 additions & 3 deletions RtspClientSharp/Rtsp/RtspTcpTransportClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@ namespace RtspClientSharp.Rtsp
{
class RtspTcpTransportClient : RtspTransportClient
{
private Socket _tcpClient;
private IRtspSocket _tcpClient;
private Stream _networkStream;
private EndPoint _remoteEndPoint = new IPEndPoint(IPAddress.None, 0);
private int _disposed;

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 All @@ -34,7 +37,7 @@ public override async Task ConnectAsync(CancellationToken token)
await _tcpClient.ConnectAsync(connectionUri.Host, rtspPort);

_remoteEndPoint = _tcpClient.RemoteEndPoint;
_networkStream = new NetworkStream(_tcpClient, false);
_networkStream = _tcpClient.CreateNetworkStream();
}

public override Stream GetStream()
Expand Down
2 changes: 1 addition & 1 deletion RtspClientSharp/RtspClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ await Task.Run(async () =>
}
catch (Exception e)
{
_rtspClientInternal.Dispose();
_rtspClientInternal?.Dispose();
Volatile.Write(ref _rtspClientInternal, null);

if (e is TimeoutException)
Expand Down
10 changes: 6 additions & 4 deletions RtspClientSharp/RtspClientSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Authors>Kirill Bogdanov</Authors>
<Authors>Kirill Bogdanov, Kyle Hooks</Authors>
<RepositoryUrl>https://github.com/BogdanovKirill/RtspClientSharp</RepositoryUrl>
<PackageTags>rtsp, rtp, rtcp</PackageTags>
<Description>C# RTSP Client for .NET
Expand All @@ -17,18 +17,21 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIconUrl>https://github.com/BogdanovKirill/RtspClientSharp/blob/master/Images/package_icon.png?raw=true</PackageIconUrl>
<Copyright>Copyright ©2019 Kirill Bogdanov</Copyright>
<Version>1.3.3</Version>

<PackageReleaseNotes>-Bug fix: Additional check for NAL start marker was added. That should resolve issue with some cameras which sends one NAL for one frame without start bytes (0 0 0 1)

More info: https://github.com/BogdanovKirill/RtspClientSharp/issues/49</PackageReleaseNotes>
<RepositoryType>git</RepositoryType>
<Platforms>AnyCPU;x64;x86</Platforms>
<Configurations>Debug;Release;Publish</Configurations>
<AssemblyOriginatorKeyFile>signkey.pfx</AssemblyOriginatorKeyFile>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageId>RtspClientSharp</PackageId>
<PackageVersion>1.3.4</PackageVersion>
<PackOnBuild>true</PackOnBuild>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType></DebugType>
<DocumentationFile>bin\Release\netstandard2.0\RtspClientSharp.xml</DocumentationFile>
</PropertyGroup>

Expand All @@ -53,5 +56,4 @@ More info: https://github.com/BogdanovKirill/RtspClientSharp/issues/49</PackageR
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Publish|x64'">
<DocumentationFile>bin\Release\netstandard2.0\RtspClientSharp.xml</DocumentationFile>
</PropertyGroup>

</Project>
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 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()
private static ISocketFactory _instance;
public static ISocketFactory Instance => _instance = (_instance ?? new DefaultSocketFactory());
private DefaultSocketFactory()
{
var socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)
{
ReceiveBufferSize = TcpReceiveBufferDefaultSize,
DualMode = true,
NoDelay = true
};
return socket;
}

public static Socket CreateUdpClient()
private const int UdpReceiveBufferDefaultSize = 128 * 1024;
private const int SIO_UDP_CONNRESET = -1744830452;
private static readonly byte[] EmptyOptionInValue = { 0, 0, 0, 0 };
public IRtspSocket CreateUdpSocket()
{
var socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)
{
ReceiveBufferSize = UdpReceiveBufferDefaultSize,
DualMode = true
};
socket.IOControl((IOControlCode)SIO_UDP_CONNRESET, EmptyOptionInValue, null);
return socket;
return new RtspSocketWrapper(socket);
}

private const int TcpReceiveBufferDefaultSize = 64 * 1024;
public IRtspSocket CreateTcpSocket()
{
return new RtspSocketWrapper(new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)
{
ReceiveBufferSize = TcpReceiveBufferDefaultSize,
DualMode = true,
NoDelay = true
});
}
}
}
}
Loading