From 8e1e4fd04a8f24756aa8fea225b19cf070c976df Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 21 Sep 2017 16:15:44 -0400 Subject: [PATCH] fix overlapping recordings --- .../HdHomerun/HdHomerunHttpStream.cs | 36 ++++++++++--------- .../HdHomerun/HdHomerunUdpStream.cs | 32 ++++++++--------- 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHttpStream.cs b/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHttpStream.cs index af064755d0..17b3713c48 100644 --- a/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHttpStream.cs +++ b/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHttpStream.cs @@ -96,7 +96,9 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun BufferContent = false, // Increase a little bit - TimeoutMs = 30000 + TimeoutMs = 30000, + + EnableHttpCompression = false }, "GET").ConfigureAwait(false)) { @@ -146,35 +148,35 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun }); } - public Task CopyToAsync(Stream stream, CancellationToken cancellationToken) - { - return CopyFileTo(_tempFilePath, stream, cancellationToken); - } - - protected async Task CopyFileTo(string path, Stream outputStream, CancellationToken cancellationToken) + public async Task CopyToAsync(Stream stream, CancellationToken cancellationToken) { - long startPosition = -20000; - - _logger.Info("Live stream starting position is {0} bytes", startPosition.ToString(CultureInfo.InvariantCulture)); - var allowAsync = false;//Environment.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows; // use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039 - using (var inputStream = (FileStream)GetInputStream(path, allowAsync)) + using (var inputStream = (FileStream)GetInputStream(_tempFilePath, allowAsync)) { - if (startPosition > 0) - { - inputStream.Seek(-20000, SeekOrigin.End); - } + TrySeek(inputStream, -20000); while (!cancellationToken.IsCancellationRequested) { - StreamHelper.CopyTo(inputStream, outputStream, 81920, cancellationToken); + StreamHelper.CopyTo(inputStream, stream, 81920, cancellationToken); //var position = fs.Position; //_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path); } } } + + private void TrySeek(FileStream stream, long offset) + { + try + { + stream.Seek(offset, SeekOrigin.End); + } + catch (Exception ex) + { + _logger.ErrorException("Error seeking stream", ex); + } + } } } diff --git a/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunUdpStream.cs b/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunUdpStream.cs index 6c21066fb0..c530d48c2a 100644 --- a/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunUdpStream.cs +++ b/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunUdpStream.cs @@ -168,30 +168,18 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun }); } - public Task CopyToAsync(Stream stream, CancellationToken cancellationToken) + public async Task CopyToAsync(Stream stream, CancellationToken cancellationToken) { - return CopyFileTo(_tempFilePath, stream, cancellationToken); - } - - protected async Task CopyFileTo(string path, Stream outputStream, CancellationToken cancellationToken) - { - long startPosition = -20000; - - _logger.Info("Live stream starting position is {0} bytes", startPosition.ToString(CultureInfo.InvariantCulture)); - var allowAsync = false;//Environment.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows; // use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039 - using (var inputStream = (FileStream)GetInputStream(path, allowAsync)) + using (var inputStream = (FileStream)GetInputStream(_tempFilePath, allowAsync)) { - if (startPosition > 0) - { - inputStream.Seek(-20000, SeekOrigin.End); - } + TrySeek(inputStream, -20000); while (!cancellationToken.IsCancellationRequested) { - StreamHelper.CopyTo(inputStream, outputStream, 81920, cancellationToken); + StreamHelper.CopyTo(inputStream, stream, 81920, cancellationToken); //var position = fs.Position; //_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path); @@ -199,6 +187,18 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun } } + private void TrySeek(FileStream stream, long offset) + { + try + { + stream.Seek(offset, SeekOrigin.End); + } + catch (Exception ex) + { + _logger.ErrorException("Error seeking stream", ex); + } + } + private static int RtpHeaderBytes = 12; private void CopyTo(ISocket udpClient, Stream target, TaskCompletionSource openTaskCompletionSource, CancellationToken cancellationToken) {