jellyfin/Emby.Server.Implementations/LiveTv/TunerHosts/MulticastStream.cs

100 lines
2.8 KiB
C#
Raw Normal View History

2016-10-07 17:08:13 +02:00
using System;
2017-02-01 21:55:56 +01:00
using System.Collections.Concurrent;
2016-10-07 17:08:13 +02:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.Logging;
2017-03-26 18:26:52 +02:00
using MediaBrowser.Model.Net;
2016-10-07 17:08:13 +02:00
2016-11-04 00:35:19 +01:00
namespace Emby.Server.Implementations.LiveTv.TunerHosts
2016-10-07 17:08:13 +02:00
{
public class MulticastStream
{
2017-06-01 07:42:49 +02:00
private readonly ConcurrentDictionary<Guid, QueueStream> _outputStreams = new ConcurrentDictionary<Guid, QueueStream>();
2016-10-07 17:08:13 +02:00
private const int BufferSize = 81920;
private readonly ILogger _logger;
public MulticastStream(ILogger logger)
{
_logger = logger;
}
public async Task CopyUntilCancelled(Stream source, Action onStarted, CancellationToken cancellationToken)
{
2016-11-23 07:54:09 +01:00
byte[] buffer = new byte[BufferSize];
2017-05-02 14:53:21 +02:00
if (source == null)
{
throw new ArgumentNullException("source");
}
2017-06-01 07:42:49 +02:00
while (true)
2016-10-07 17:08:13 +02:00
{
2017-06-01 07:42:49 +02:00
cancellationToken.ThrowIfCancellationRequested();
var bytesRead = source.Read(buffer, 0, buffer.Length);
2016-10-07 17:08:13 +02:00
if (bytesRead > 0)
{
2017-02-01 21:55:56 +01:00
var allStreams = _outputStreams.ToList();
2017-03-27 21:31:24 +02:00
2017-04-05 19:16:44 +02:00
//if (allStreams.Count == 1)
//{
2017-06-01 08:25:07 +02:00
// allStreams[0].Value.Write(buffer, 0, bytesRead);
2017-04-05 19:16:44 +02:00
//}
//else
2017-03-27 21:31:24 +02:00
{
byte[] copy = new byte[bytesRead];
Buffer.BlockCopy(buffer, 0, copy, 0, bytesRead);
foreach (var stream in allStreams)
{
stream.Value.Queue(copy, 0, copy.Length);
}
2017-03-26 18:26:52 +02:00
}
if (onStarted != null)
{
var onStartedCopy = onStarted;
onStarted = null;
Task.Run(onStartedCopy);
}
}
else
{
await Task.Delay(100).ConfigureAwait(false);
}
}
}
2017-05-16 08:42:33 +02:00
public Task CopyToAsync(Stream stream, CancellationToken cancellationToken)
2016-10-07 17:08:13 +02:00
{
var result = new QueueStream(stream, _logger)
{
OnFinished = OnFinished
};
2017-02-01 21:55:56 +01:00
_outputStreams.TryAdd(result.Id, result);
2017-05-16 08:42:33 +02:00
result.Start(cancellationToken);
2016-10-07 17:08:13 +02:00
return result.TaskCompletion.Task;
}
public void RemoveOutputStream(QueueStream stream)
{
2017-02-01 21:55:56 +01:00
QueueStream removed;
_outputStreams.TryRemove(stream.Id, out removed);
2016-10-07 17:08:13 +02:00
}
private void OnFinished(QueueStream queueStream)
{
RemoveOutputStream(queueStream);
}
}
}