Skip to content

Commit 10e1d62

Browse files
Fix mirroring port leak causing reconnection failure and reduce ffplay latency
Co-authored-by: YimingZhanshen <76594627+YimingZhanshen@users.noreply.github.com>
1 parent 2f95e94 commit 10e1d62

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

AirPlay/Listeners/Bases/BaseTcpListener.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public override Task StartAsync(CancellationToken cancellationToken)
4242
public override Task StopAsync()
4343
{
4444
_cancellationTokenSource.Cancel();
45+
try { _listener.Stop(); } catch { }
4546
return Task.CompletedTask;
4647
}
4748

@@ -59,17 +60,23 @@ private async Task AcceptClientsAsync(CancellationToken cancellationToken)
5960
{
6061
_listener.Start();
6162

62-
while (!cancellationToken.IsCancellationRequested)
63+
try
6364
{
64-
var client = await _listener.AcceptTcpClientAsync().ConfigureAwait(false);
65-
var task = HandleClientAsync(client, cancellationToken);
66-
67-
var remoteEndpoint = client.Client.RemoteEndPoint.ToString();
68-
if (!_connections.TryAdd(remoteEndpoint, task))
65+
while (!cancellationToken.IsCancellationRequested)
6966
{
70-
client.Close();
67+
var client = await _listener.AcceptTcpClientAsync().ConfigureAwait(false);
68+
var task = HandleClientAsync(client, cancellationToken);
69+
70+
var remoteEndpoint = client.Client.RemoteEndPoint.ToString();
71+
if (!_connections.TryAdd(remoteEndpoint, task))
72+
{
73+
client.Close();
74+
}
7175
}
7276
}
77+
catch (ObjectDisposedException) { }
78+
catch (InvalidOperationException) { }
79+
catch (System.Net.Sockets.SocketException) { }
7380
}
7481

7582
private async Task HandleClientAsync(TcpClient client, CancellationToken cancellationToken)

AirPlay/Services/VideoOutputService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,14 @@ private void LaunchFfplay(string pipePath)
139139
if (ffplayPath == null)
140140
{
141141
Console.WriteLine("ffplay not found. Please ensure ffplay is in the application directory or PATH.");
142-
Console.WriteLine($" You can manually connect with: ffplay -f h264 -probesize 2048000 -fflags nobuffer -flags low_delay -framedrop {pipePath}");
142+
Console.WriteLine($" You can manually connect with: ffplay -f h264 -probesize 32768 -analyzeduration 1000000 -fflags nobuffer -flags low_delay -framedrop {pipePath}");
143143
return;
144144
}
145145

146146
var psi = new ProcessStartInfo
147147
{
148148
FileName = ffplayPath,
149-
Arguments = $"-f h264 -probesize 2048000 -fflags nobuffer -flags low_delay -framedrop \"{pipePath}\"",
149+
Arguments = $"-f h264 -probesize 32768 -analyzeduration 1000000 -fflags nobuffer -flags low_delay -framedrop \"{pipePath}\"",
150150
UseShellExecute = false,
151151
CreateNoWindow = false
152152
};
@@ -160,7 +160,7 @@ private void LaunchFfplay(string pipePath)
160160
catch (Exception ex)
161161
{
162162
Console.WriteLine($"Failed to launch ffplay: {ex.Message}");
163-
Console.WriteLine($" You can manually connect with: ffplay -f h264 -probesize 2048000 -fflags nobuffer -flags low_delay -framedrop {pipePath}");
163+
Console.WriteLine($" You can manually connect with: ffplay -f h264 -probesize 32768 -analyzeduration 1000000 -fflags nobuffer -flags low_delay -framedrop {pipePath}");
164164
}
165165
}
166166

0 commit comments

Comments
 (0)