From 386ed8d34a154766ff202ef86719bed5c89b2ff2 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 1 Jun 2017 01:05:36 -0400 Subject: [PATCH] update live stream buffers --- .../TunerHosts/HdHomerun/HdHomerunHost.cs | 2 +- .../HdHomerun/HdHomerunHttpStream.cs | 9 +++---- .../HdHomerun/HdHomerunUdpStream.cs | 26 +++++++------------ MediaBrowser.Controller/IO/StreamHelper.cs | 12 +++++++++ SharedVersion.cs | 2 +- 5 files changed, 27 insertions(+), 24 deletions(-) diff --git a/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHost.cs b/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHost.cs index 752f55651..2afc3744f 100644 --- a/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHost.cs +++ b/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHost.cs @@ -513,7 +513,7 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun // The UDP method is not working reliably on OSX, and on BSD it hasn't been tested yet var enableHttpStream = _environment.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.OSX || _environment.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.BSD; - //enableHttpStream = true; + enableHttpStream = true; if (enableHttpStream) { mediaSource.Protocol = MediaProtocol.Http; diff --git a/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHttpStream.cs b/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHttpStream.cs index a680f2183..3df9b85a8 100644 --- a/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHttpStream.cs +++ b/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHttpStream.cs @@ -106,10 +106,8 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun FileSystem.CreateDirectory(FileSystem.GetDirectoryName(_tempFilePath)); using (var fileStream = FileSystem.GetFileStream(_tempFilePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, FileOpenOptions.None)) { - ResolveAfterDelay(3000, openTaskCompletionSource); - //await response.Content.CopyToAsync(fileStream, 81920, cancellationToken).ConfigureAwait(false); - StreamHelper.CopyTo(response.Content, fileStream, 81920, cancellationToken); + StreamHelper.CopyTo(response.Content, fileStream, 81920, () => Resolve(openTaskCompletionSource), cancellationToken); //await AsyncStreamCopier.CopyStream(response.Content, fileStream, 81920, 4, cancellationToken).ConfigureAwait(false); } @@ -140,11 +138,10 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun }); } - private void ResolveAfterDelay(int delayMs, TaskCompletionSource openTaskCompletionSource) + private void Resolve(TaskCompletionSource openTaskCompletionSource) { - Task.Run(async () => + Task.Run(() => { - await Task.Delay(delayMs).ConfigureAwait(false); openTaskCompletionSource.TrySetResult(true); }); } diff --git a/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunUdpStream.cs b/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunUdpStream.cs index e98c5285d..4b958c07b 100644 --- a/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunUdpStream.cs +++ b/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunUdpStream.cs @@ -126,8 +126,6 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun FileSystem.CreateDirectory(FileSystem.GetDirectoryName(_tempFilePath)); using (var fileStream = FileSystem.GetFileStream(_tempFilePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, FileOpenOptions.None)) { - ResolveAfterDelay(3000, openTaskCompletionSource); - CopyTo(udpClient, fileStream, openTaskCompletionSource, cancellationToken); } } @@ -170,13 +168,10 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun }); } - public Task CopyToAsync(Stream stream, CancellationToken cancellationToken) + public async Task CopyToAsync(Stream outputStream, CancellationToken cancellationToken) { - return CopyFileTo(_tempFilePath, stream, cancellationToken); - } + var path = _tempFilePath; - protected async Task CopyFileTo(string path, Stream outputStream, CancellationToken cancellationToken) - { long startPosition = -20000; if (startPosition < 0) { @@ -221,15 +216,6 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun } } - private void ResolveAfterDelay(int delayMs, TaskCompletionSource openTaskCompletionSource) - { - Task.Run(async () => - { - await Task.Delay(delayMs).ConfigureAwait(false); - openTaskCompletionSource.TrySetResult(true); - }); - } - private static int RtpHeaderBytes = 12; private void CopyTo(ISocket udpClient, Stream target, TaskCompletionSource openTaskCompletionSource, CancellationToken cancellationToken) { @@ -238,6 +224,8 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun byte[] buffer = new byte[bufferSize]; int read; + var resolved = false; + while ((read = source.Read(buffer, 0, buffer.Length)) != 0) { cancellationToken.ThrowIfCancellationRequested(); @@ -248,6 +236,12 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun { target.Write(buffer, RtpHeaderBytes, read); } + + if (!resolved) + { + resolved = true; + Resolve(openTaskCompletionSource); + } } //var copier = new AsyncStreamCopier(source, target, 0, cancellationToken, false, bufferSize, bufferCount); diff --git a/MediaBrowser.Controller/IO/StreamHelper.cs b/MediaBrowser.Controller/IO/StreamHelper.cs index 168d4b8c6..af97a0233 100644 --- a/MediaBrowser.Controller/IO/StreamHelper.cs +++ b/MediaBrowser.Controller/IO/StreamHelper.cs @@ -1,11 +1,17 @@ using System.IO; using System.Threading; +using System; namespace MediaBrowser.Controller.IO { public static class StreamHelper { public static void CopyTo(Stream source, Stream destination, int bufferSize, CancellationToken cancellationToken) + { + CopyTo(source, destination, bufferSize, null, cancellationToken); + } + + public static void CopyTo(Stream source, Stream destination, int bufferSize, Action onStarted, CancellationToken cancellationToken) { byte[] buffer = new byte[bufferSize]; int read; @@ -14,6 +20,12 @@ namespace MediaBrowser.Controller.IO cancellationToken.ThrowIfCancellationRequested(); destination.Write(buffer, 0, read); + + if (onStarted != null) + { + onStarted(); + onStarted = null; + } } } } diff --git a/SharedVersion.cs b/SharedVersion.cs index fe202b2a3..f3992d52c 100644 --- a/SharedVersion.cs +++ b/SharedVersion.cs @@ -1,3 +1,3 @@ using System.Reflection; -[assembly: AssemblyVersion("3.2.18.2")] +[assembly: AssemblyVersion("3.2.18.3")]