2016-10-07 15:08:13 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
|
2016-11-03 23:35:19 +00:00
|
|
|
|
namespace Emby.Server.Implementations.LiveTv.TunerHosts
|
2016-10-07 15:08:13 +00:00
|
|
|
|
{
|
|
|
|
|
public class QueueStream
|
|
|
|
|
{
|
|
|
|
|
private readonly Stream _outputStream;
|
2017-08-19 19:43:35 +00:00
|
|
|
|
private readonly BlockingCollection<Tuple<byte[], int, int>> _queue = new BlockingCollection<Tuple<byte[], int, int>>();
|
2016-10-07 15:08:13 +00:00
|
|
|
|
public TaskCompletionSource<bool> TaskCompletion { get; private set; }
|
|
|
|
|
|
|
|
|
|
public Action<QueueStream> OnFinished { get; set; }
|
|
|
|
|
private readonly ILogger _logger;
|
2017-02-01 20:55:56 +00:00
|
|
|
|
public Guid Id = Guid.NewGuid();
|
2016-10-07 15:08:13 +00:00
|
|
|
|
|
|
|
|
|
public QueueStream(Stream outputStream, ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
_outputStream = outputStream;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
TaskCompletion = new TaskCompletionSource<bool>();
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-26 16:26:52 +00:00
|
|
|
|
public void Queue(byte[] bytes, int offset, int count)
|
2016-10-07 15:08:13 +00:00
|
|
|
|
{
|
2017-08-19 19:43:35 +00:00
|
|
|
|
_queue.Add(new Tuple<byte[], int, int>(bytes, offset, count));
|
2016-10-07 15:08:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start(CancellationToken cancellationToken)
|
|
|
|
|
{
|
2017-06-01 05:42:49 +00:00
|
|
|
|
Task.Run(() => StartInternal(cancellationToken));
|
2016-10-07 15:08:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-27 19:31:24 +00:00
|
|
|
|
private void OnClosed()
|
|
|
|
|
{
|
|
|
|
|
GC.Collect();
|
|
|
|
|
if (OnFinished != null)
|
|
|
|
|
{
|
|
|
|
|
OnFinished(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 06:25:07 +00:00
|
|
|
|
public void Write(byte[] bytes, int offset, int count)
|
2017-03-27 19:31:24 +00:00
|
|
|
|
{
|
|
|
|
|
//return _outputStream.WriteAsync(bytes, offset, count, cancellationToken);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-06-01 06:25:07 +00:00
|
|
|
|
_outputStream.Write(bytes, offset, count);
|
2017-03-27 19:31:24 +00:00
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("QueueStream cancelled");
|
|
|
|
|
TaskCompletion.TrySetCanceled();
|
|
|
|
|
OnClosed();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error in QueueStream", ex);
|
|
|
|
|
TaskCompletion.TrySetException(ex);
|
|
|
|
|
OnClosed();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-19 19:43:35 +00:00
|
|
|
|
private void StartInternal(CancellationToken cancellationToken)
|
2016-10-07 15:08:13 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-02-01 20:55:56 +00:00
|
|
|
|
while (true)
|
2016-10-07 15:08:13 +00:00
|
|
|
|
{
|
2017-06-01 05:42:49 +00:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2017-08-19 19:43:35 +00:00
|
|
|
|
foreach (var result in _queue.GetConsumingEnumerable())
|
2016-10-07 15:08:13 +00:00
|
|
|
|
{
|
2017-06-01 05:42:49 +00:00
|
|
|
|
_outputStream.Write(result.Item1, result.Item2, result.Item3);
|
2016-10-07 15:08:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("QueueStream cancelled");
|
|
|
|
|
TaskCompletion.TrySetCanceled();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error in QueueStream", ex);
|
|
|
|
|
TaskCompletion.TrySetException(ex);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2017-03-27 19:31:24 +00:00
|
|
|
|
OnClosed();
|
2016-10-07 15:08:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|