update live stream buffers

This commit is contained in:
Luke Pulverenti 2017-06-01 01:05:36 -04:00
parent 804c98c864
commit 386ed8d34a
5 changed files with 27 additions and 24 deletions

View File

@ -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;

View File

@ -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<bool> openTaskCompletionSource)
private void Resolve(TaskCompletionSource<bool> openTaskCompletionSource)
{
Task.Run(async () =>
Task.Run(() =>
{
await Task.Delay(delayMs).ConfigureAwait(false);
openTaskCompletionSource.TrySetResult(true);
});
}

View File

@ -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<bool> 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<bool> 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);

View File

@ -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;
}
}
}
}

View File

@ -1,3 +1,3 @@
using System.Reflection;
[assembly: AssemblyVersion("3.2.18.2")]
[assembly: AssemblyVersion("3.2.18.3")]