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

148 lines
4.8 KiB
C#
Raw Normal View History

2016-10-07 08:08:13 -07:00
using System;
2017-02-01 13:55:56 -07:00
using System.Collections.Concurrent;
2016-10-07 08:08:13 -07: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 09:26:52 -07:00
using MediaBrowser.Model.Net;
2016-10-07 08:08:13 -07:00
2016-11-03 16:35:19 -07:00
namespace Emby.Server.Implementations.LiveTv.TunerHosts
2016-10-07 08:08:13 -07:00
{
public class MulticastStream
{
2017-02-01 13:55:56 -07:00
private readonly ConcurrentDictionary<Guid,QueueStream> _outputStreams = new ConcurrentDictionary<Guid, QueueStream>();
2016-10-07 08:08:13 -07:00
private const int BufferSize = 81920;
private CancellationToken _cancellationToken;
private readonly ILogger _logger;
public MulticastStream(ILogger logger)
{
_logger = logger;
}
public async Task CopyUntilCancelled(Stream source, Action onStarted, CancellationToken cancellationToken)
{
_cancellationToken = cancellationToken;
2016-11-22 23:54:09 -07:00
byte[] buffer = new byte[BufferSize];
2016-10-07 08:08:13 -07:00
while (!cancellationToken.IsCancellationRequested)
{
var bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
if (bytesRead > 0)
{
2017-02-01 13:55:56 -07:00
var allStreams = _outputStreams.ToList();
2017-03-27 12:31:24 -07:00
2017-04-05 10:16:44 -07:00
//if (allStreams.Count == 1)
//{
// await allStreams[0].Value.WriteAsync(buffer, 0, bytesRead).ConfigureAwait(false);
//}
//else
2017-03-27 12:31:24 -07: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 09:26:52 -07:00
}
if (onStarted != null)
{
var onStartedCopy = onStarted;
onStarted = null;
Task.Run(onStartedCopy);
}
}
else
{
await Task.Delay(100).ConfigureAwait(false);
}
}
}
private static int RtpHeaderBytes = 12;
public async Task CopyUntilCancelled(ISocket udpClient, Action onStarted, CancellationToken cancellationToken)
{
_cancellationToken = cancellationToken;
while (!cancellationToken.IsCancellationRequested)
{
var receiveToken = cancellationToken;
// On the first connection attempt, put a timeout to avoid being stuck indefinitely in the event of failure
if (onStarted != null)
{
receiveToken = CancellationTokenSource.CreateLinkedTokenSource(new CancellationTokenSource(5000).Token, cancellationToken).Token;
}
var data = await udpClient.ReceiveAsync(receiveToken).ConfigureAwait(false);
var bytesRead = data.ReceivedBytes - RtpHeaderBytes;
if (bytesRead > 0)
{
var allStreams = _outputStreams.ToList();
2017-03-27 12:31:24 -07:00
if (allStreams.Count == 1)
2017-03-26 09:26:52 -07:00
{
2017-03-27 12:31:24 -07:00
await allStreams[0].Value.WriteAsync(data.Buffer, 0, bytesRead).ConfigureAwait(false);
}
else
{
byte[] copy = new byte[bytesRead];
Buffer.BlockCopy(data.Buffer, RtpHeaderBytes, copy, 0, bytesRead);
foreach (var stream in allStreams)
{
stream.Value.Queue(copy, 0, copy.Length);
}
2016-10-07 08:08:13 -07:00
}
if (onStarted != null)
{
var onStartedCopy = onStarted;
onStarted = null;
Task.Run(onStartedCopy);
}
}
else
{
await Task.Delay(100).ConfigureAwait(false);
}
}
}
public Task CopyToAsync(Stream stream)
{
var result = new QueueStream(stream, _logger)
{
OnFinished = OnFinished
};
2017-02-01 13:55:56 -07:00
_outputStreams.TryAdd(result.Id, result);
2016-10-07 08:08:13 -07:00
result.Start(_cancellationToken);
return result.TaskCompletion.Task;
}
public void RemoveOutputStream(QueueStream stream)
{
2017-02-01 13:55:56 -07:00
QueueStream removed;
_outputStreams.TryRemove(stream.Id, out removed);
2016-10-07 08:08:13 -07:00
}
private void OnFinished(QueueStream queueStream)
{
RemoveOutputStream(queueStream);
}
}
}