update stream buffering

pull/702/head
Luke Pulverenti 9 years ago
parent 25395c5d82
commit a7b25c065c

@ -210,8 +210,6 @@ namespace MediaBrowser.Api.Playback.Hls
{ {
ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, p => false); ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, p => false);
await ReadSegmentLengths(playlistPath).ConfigureAwait(false);
if (currentTranscodingIndex.HasValue) if (currentTranscodingIndex.HasValue)
{ {
DeleteLastFile(playlistPath, segmentExtension, 0); DeleteLastFile(playlistPath, segmentExtension, 0);
@ -255,55 +253,8 @@ namespace MediaBrowser.Api.Playback.Hls
return await GetSegmentResult(playlistPath, segmentPath, requestedIndex, segmentLength, job, cancellationToken).ConfigureAwait(false); return await GetSegmentResult(playlistPath, segmentPath, requestedIndex, segmentLength, job, cancellationToken).ConfigureAwait(false);
} }
private static readonly ConcurrentDictionary<string, double> SegmentLengths = new ConcurrentDictionary<string, double>(StringComparer.OrdinalIgnoreCase); // 256k
private async Task ReadSegmentLengths(string playlist) private const int BufferSize = 262144;
{
try
{
using (var fileStream = GetPlaylistFileStream(playlist))
{
using (var reader = new StreamReader(fileStream))
{
double duration = -1;
while (!reader.EndOfStream)
{
var text = await reader.ReadLineAsync().ConfigureAwait(false);
if (text.StartsWith("#EXTINF", StringComparison.OrdinalIgnoreCase))
{
var parts = text.Split(new[] { ':' }, 2);
if (parts.Length == 2)
{
var time = parts[1].Trim(new[] { ',' }).Trim();
double timeValue;
if (double.TryParse(time, NumberStyles.Any, CultureInfo.InvariantCulture, out timeValue))
{
duration = timeValue;
continue;
}
}
}
else if (duration != -1)
{
SegmentLengths.AddOrUpdate(text, duration, (k, v) => duration);
Logger.Debug("Added segment length of {0} for {1}", duration, text);
}
duration = -1;
}
}
}
}
catch (DirectoryNotFoundException)
{
}
catch (FileNotFoundException)
{
}
}
private long GetSeekPositionTicks(StreamState state, string playlist, int requestedIndex) private long GetSeekPositionTicks(StreamState state, string playlist, int requestedIndex)
{ {
@ -455,11 +406,9 @@ namespace MediaBrowser.Api.Playback.Hls
{ {
using (var fileStream = GetPlaylistFileStream(playlistPath)) using (var fileStream = GetPlaylistFileStream(playlistPath))
{ {
using (var reader = new StreamReader(fileStream)) using (var reader = new StreamReader(fileStream, Encoding.UTF8, true, BufferSize))
{
while (!reader.EndOfStream)
{ {
var text = await reader.ReadLineAsync().ConfigureAwait(false); var text = await reader.ReadToEndAsync().ConfigureAwait(false);
// If it appears in the playlist, it's done // If it appears in the playlist, it's done
if (text.IndexOf(segmentFilename, StringComparison.OrdinalIgnoreCase) != -1) if (text.IndexOf(segmentFilename, StringComparison.OrdinalIgnoreCase) != -1)
@ -468,8 +417,7 @@ namespace MediaBrowser.Api.Playback.Hls
{ {
return GetSegmentResult(segmentPath, segmentIndex, segmentLength, transcodingJob); return GetSegmentResult(segmentPath, segmentIndex, segmentLength, transcodingJob);
} }
break; //break;
}
} }
} }
} }

@ -91,6 +91,9 @@ namespace MediaBrowser.Api.Playback.Progressive
private readonly IFileSystem _fileSystem; private readonly IFileSystem _fileSystem;
private readonly TranscodingJob _job; private readonly TranscodingJob _job;
// 256k
private const int BufferSize = 262144;
private long _bytesWritten = 0; private long _bytesWritten = 0;
public ProgressiveFileCopier(IFileSystem fileSystem, TranscodingJob job) public ProgressiveFileCopier(IFileSystem fileSystem, TranscodingJob job)
@ -108,7 +111,7 @@ namespace MediaBrowser.Api.Playback.Progressive
{ {
while (eofCount < 15) while (eofCount < 15)
{ {
CopyToInternal(fs, outputStream, 81920); CopyToInternal(fs, outputStream, BufferSize);
var fsPosition = fs.Position; var fsPosition = fs.Position;

@ -150,7 +150,7 @@ namespace MediaBrowser.Controller.Entities.TV
{ {
var series = Series; var series = Series;
if (ParentIndexNumber.HasValue) if (series != null && ParentIndexNumber.HasValue)
{ {
var findNumber = ParentIndexNumber.Value; var findNumber = ParentIndexNumber.Value;

@ -28,6 +28,9 @@ namespace MediaBrowser.Server.Implementations.HttpServer
public Action OnComplete { get; set; } public Action OnComplete { get; set; }
private readonly ILogger _logger; private readonly ILogger _logger;
// 256k
private const int BufferSize = 262144;
/// <summary> /// <summary>
/// The _options /// The _options
/// </summary> /// </summary>
@ -187,7 +190,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
// If the requested range is "0-", we can optimize by just doing a stream copy // If the requested range is "0-", we can optimize by just doing a stream copy
if (RangeEnd >= TotalContentLength - 1) if (RangeEnd >= TotalContentLength - 1)
{ {
source.CopyTo(responseStream); source.CopyTo(responseStream, BufferSize);
} }
else else
{ {
@ -211,8 +214,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
private void CopyToInternal(Stream source, Stream destination, long copyLength) private void CopyToInternal(Stream source, Stream destination, long copyLength)
{ {
const int bufferSize = 81920; var array = new byte[BufferSize];
var array = new byte[bufferSize];
int count; int count;
while ((count = source.Read(array, 0, array.Length)) != 0) while ((count = source.Read(array, 0, array.Length)) != 0)
{ {
@ -249,7 +251,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
// If the requested range is "0-", we can optimize by just doing a stream copy // If the requested range is "0-", we can optimize by just doing a stream copy
if (RangeEnd >= TotalContentLength - 1) if (RangeEnd >= TotalContentLength - 1)
{ {
await source.CopyToAsync(responseStream).ConfigureAwait(false); await source.CopyToAsync(responseStream, BufferSize).ConfigureAwait(false);
} }
else else
{ {
@ -268,8 +270,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
private async Task CopyToAsyncInternal(Stream source, Stream destination, int copyLength, CancellationToken cancellationToken) private async Task CopyToAsyncInternal(Stream source, Stream destination, int copyLength, CancellationToken cancellationToken)
{ {
const int bufferSize = 81920; var array = new byte[BufferSize];
var array = new byte[bufferSize];
int count; int count;
while ((count = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false)) != 0) while ((count = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false)) != 0)
{ {

@ -81,6 +81,9 @@ namespace MediaBrowser.Server.Implementations.HttpServer
WriteToInternal(responseStream); WriteToInternal(responseStream);
} }
// 256k
private const int BufferSize = 262144;
/// <summary> /// <summary>
/// Writes to async. /// Writes to async.
/// </summary> /// </summary>
@ -92,7 +95,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
{ {
using (var src = SourceStream) using (var src = SourceStream)
{ {
src.CopyTo(responseStream); src.CopyTo(responseStream, BufferSize);
} }
} }
catch (Exception ex) catch (Exception ex)

@ -525,7 +525,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV
{ {
using (var output = File.Open(recordPath, FileMode.Create, FileAccess.Write, FileShare.Read)) using (var output = File.Open(recordPath, FileMode.Create, FileAccess.Write, FileShare.Read))
{ {
await response.Content.CopyToAsync(output, 4096, linkedToken); await response.Content.CopyToAsync(output, 131072, linkedToken);
} }
} }

Loading…
Cancel
Save