Skip to content

Commit 1cdfaf3

Browse files
Killing the ffmpeg process if it will not cancel within 1 second
Since the cancellation token does not stop the Stream.ReadAsync when the inputsource is offline, we manually kill the ffmpeg process after 1 second of lenience. This causes the ReadAsync method to return 0 bytes, allowing the stream to end.
1 parent 385024e commit 1cdfaf3

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/Nager.VideoStream/VideoStreamClient.cs

+12-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public async Task StartFrameReaderAsync(
5454
};
5555

5656
using (var ffmpegProcess = new Process { StartInfo = startInfo })
57+
using (cancellationToken.Register(() => WaitBeforeKill(ffmpegProcess)))
5758
{
5859
ffmpegProcess.ErrorDataReceived += this.ProcessDataReceived;
5960

@@ -99,18 +100,23 @@ public async Task StartFrameReaderAsync(
99100

100101
ffmpegProcess.ErrorDataReceived -= this.ProcessDataReceived;
101102

102-
ffmpegProcess.WaitForExit(1000);
103-
104-
if (!ffmpegProcess.HasExited)
105-
{
106-
ffmpegProcess.Kill();
107-
}
103+
WaitBeforeKill(ffmpegProcess);
108104
}
109105
}
110106

111107
private void ProcessDataReceived(object sender, DataReceivedEventArgs e)
112108
{
113109
this.FFmpegInfoReceived?.Invoke(e.Data);
114110
}
111+
112+
private static void WaitBeforeKill(Process process, int waitForExitMilliseconds = 1000)
113+
{
114+
process.WaitForExit(waitForExitMilliseconds);
115+
116+
if (!process.HasExited)
117+
{
118+
process.Kill();
119+
}
120+
}
115121
}
116122
}

0 commit comments

Comments
 (0)