2020-02-06 07:20:23 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
using System;
|
2019-01-13 12:22:00 -07:00
|
|
|
using System.Collections.Generic;
|
2017-03-02 14:32:20 -07:00
|
|
|
using System.IO;
|
2020-10-31 11:21:46 -07:00
|
|
|
using System.Linq;
|
2019-01-13 12:22:00 -07:00
|
|
|
using System.Net;
|
2020-10-31 11:21:46 -07:00
|
|
|
using System.Net.NetworkInformation;
|
2019-01-13 12:22:00 -07:00
|
|
|
using System.Net.Sockets;
|
2017-03-02 14:32:20 -07:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-11-08 04:49:00 -07:00
|
|
|
using MediaBrowser.Common.Configuration;
|
2020-03-24 08:12:06 -07:00
|
|
|
using MediaBrowser.Common.Net;
|
2017-03-02 14:32:20 -07:00
|
|
|
using MediaBrowser.Controller;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
using MediaBrowser.Model.IO;
|
2017-11-17 14:54:33 -07:00
|
|
|
using MediaBrowser.Model.LiveTv;
|
2019-01-13 12:22:00 -07:00
|
|
|
using MediaBrowser.Model.MediaInfo;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2017-03-02 14:32:20 -07:00
|
|
|
|
|
|
|
namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
|
|
|
|
{
|
2017-03-02 22:53:21 -07:00
|
|
|
public class HdHomerunUdpStream : LiveStream, IDirectStreamProvider
|
2017-03-02 14:32:20 -07:00
|
|
|
{
|
2019-07-07 07:39:35 -07:00
|
|
|
private const int RtpHeaderBytes = 12;
|
|
|
|
|
2017-03-02 14:32:20 -07:00
|
|
|
private readonly IServerApplicationHost _appHost;
|
2017-03-05 19:32:56 -07:00
|
|
|
private readonly IHdHomerunChannelCommands _channelCommands;
|
2017-03-02 14:32:20 -07:00
|
|
|
private readonly int _numTuners;
|
2017-03-02 21:36:20 -07:00
|
|
|
private readonly INetworkManager _networkManager;
|
2017-03-02 14:32:20 -07:00
|
|
|
|
2019-07-07 12:03:26 -07:00
|
|
|
public HdHomerunUdpStream(
|
|
|
|
MediaSourceInfo mediaSource,
|
|
|
|
TunerHostInfo tunerHostInfo,
|
|
|
|
string originalStreamId,
|
|
|
|
IHdHomerunChannelCommands channelCommands,
|
|
|
|
int numTuners,
|
|
|
|
IFileSystem fileSystem,
|
|
|
|
ILogger logger,
|
2019-11-08 04:49:00 -07:00
|
|
|
IConfigurationManager configurationManager,
|
2019-07-07 12:03:26 -07:00
|
|
|
IServerApplicationHost appHost,
|
2019-07-07 07:39:35 -07:00
|
|
|
INetworkManager networkManager,
|
|
|
|
IStreamHelper streamHelper)
|
2019-11-08 04:49:00 -07:00
|
|
|
: base(mediaSource, tunerHostInfo, fileSystem, logger, configurationManager, streamHelper)
|
2017-03-02 14:32:20 -07:00
|
|
|
{
|
|
|
|
_appHost = appHost;
|
2017-03-02 21:36:20 -07:00
|
|
|
_networkManager = networkManager;
|
2017-03-02 14:32:20 -07:00
|
|
|
OriginalStreamId = originalStreamId;
|
2017-03-05 19:32:56 -07:00
|
|
|
_channelCommands = channelCommands;
|
2017-03-02 14:32:20 -07:00
|
|
|
_numTuners = numTuners;
|
2017-11-14 00:41:21 -07:00
|
|
|
EnableStreamSharing = true;
|
2017-03-02 14:32:20 -07:00
|
|
|
}
|
|
|
|
|
2020-10-31 11:21:46 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Returns an unused UDP port number in the range specified.
|
2020-11-21 06:27:27 -07:00
|
|
|
/// Temporarily placed here until future network PR merged.
|
2020-10-31 11:21:46 -07:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="range">Upper and Lower boundary of ports to select.</param>
|
|
|
|
/// <returns>System.Int32.</returns>
|
|
|
|
private static int GetUdpPortFromRange((int Min, int Max) range)
|
|
|
|
{
|
|
|
|
var properties = IPGlobalProperties.GetIPGlobalProperties();
|
|
|
|
|
|
|
|
// Get active udp listeners.
|
|
|
|
var udpListenerPorts = properties.GetActiveUdpListeners()
|
|
|
|
.Where(n => n.Port >= range.Min && n.Port <= range.Max)
|
|
|
|
.Select(n => n.Port);
|
|
|
|
|
2020-11-21 09:02:54 -07:00
|
|
|
return Enumerable
|
|
|
|
.Range(range.Min, range.Max)
|
|
|
|
.FirstOrDefault(i => !udpListenerPorts.Contains(i));
|
2020-10-31 11:21:46 -07:00
|
|
|
}
|
|
|
|
|
2017-10-23 12:14:11 -07:00
|
|
|
public override async Task Open(CancellationToken openCancellationToken)
|
2017-03-02 14:32:20 -07:00
|
|
|
{
|
2017-10-13 23:52:56 -07:00
|
|
|
LiveStreamCancellationTokenSource.Token.ThrowIfCancellationRequested();
|
2017-03-02 14:32:20 -07:00
|
|
|
|
|
|
|
var mediaSource = OriginalMediaSource;
|
|
|
|
|
2017-03-02 21:36:20 -07:00
|
|
|
var uri = new Uri(mediaSource.Path);
|
2020-11-21 06:27:27 -07:00
|
|
|
// Temporary code to reduce PR size. This will be updated by a future network pr.
|
2020-10-31 11:21:46 -07:00
|
|
|
var localPort = GetUdpPortFromRange((49152, 65535));
|
2017-03-02 14:32:20 -07:00
|
|
|
|
2019-01-26 14:08:04 -07:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(TempFilePath));
|
2017-10-23 12:14:11 -07:00
|
|
|
|
2018-12-20 05:11:26 -07:00
|
|
|
Logger.LogInformation("Opening HDHR UDP Live stream from {host}", uri.Host);
|
2017-03-02 14:32:20 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
var remoteAddress = IPAddress.Parse(uri.Host);
|
|
|
|
IPAddress localAddress = null;
|
2019-07-29 06:26:17 -07:00
|
|
|
using (var tcpClient = new TcpClient())
|
2017-10-23 12:14:11 -07:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-07-29 06:26:17 -07:00
|
|
|
await tcpClient.ConnectAsync(remoteAddress, HdHomerunManager.HdHomeRunPort).ConfigureAwait(false);
|
2020-09-18 22:13:24 -07:00
|
|
|
localAddress = ((IPEndPoint)tcpClient.Client.LocalEndPoint).Address;
|
2019-07-29 06:26:17 -07:00
|
|
|
tcpClient.Close();
|
2017-10-23 12:14:11 -07:00
|
|
|
}
|
2018-12-20 05:11:26 -07:00
|
|
|
catch (Exception ex)
|
2017-10-23 12:14:11 -07:00
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
Logger.LogError(ex, "Unable to determine local ip address for Legacy HDHomerun stream.");
|
2017-10-23 12:14:11 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 22:13:24 -07:00
|
|
|
if (localAddress.IsIPv4MappedToIPv6) {
|
|
|
|
localAddress = localAddress.MapToIPv4();
|
|
|
|
}
|
|
|
|
|
2019-08-16 09:52:16 -07:00
|
|
|
var udpClient = new UdpClient(localPort, AddressFamily.InterNetwork);
|
2019-07-07 07:39:35 -07:00
|
|
|
var hdHomerunManager = new HdHomerunManager();
|
2017-10-23 12:14:11 -07:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// send url to start streaming
|
2019-08-16 12:18:37 -07:00
|
|
|
await hdHomerunManager.StartStreaming(
|
|
|
|
remoteAddress,
|
|
|
|
localAddress,
|
|
|
|
localPort,
|
|
|
|
_channelCommands,
|
|
|
|
_numTuners,
|
|
|
|
openCancellationToken).ConfigureAwait(false);
|
2017-10-23 12:14:11 -07:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
using (udpClient)
|
2018-12-20 05:11:26 -07:00
|
|
|
using (hdHomerunManager)
|
2017-10-23 12:14:11 -07:00
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
if (!(ex is OperationCanceledException))
|
2017-10-23 12:14:11 -07:00
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
Logger.LogError(ex, "Error opening live stream:");
|
2017-10-23 12:14:11 -07:00
|
|
|
}
|
2019-07-29 06:26:17 -07:00
|
|
|
|
2018-12-20 05:11:26 -07:00
|
|
|
throw;
|
2017-10-23 12:14:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 14:32:20 -07:00
|
|
|
var taskCompletionSource = new TaskCompletionSource<bool>();
|
|
|
|
|
2020-11-15 19:44:11 -07:00
|
|
|
_ = StartStreaming(
|
2019-07-07 07:39:35 -07:00
|
|
|
udpClient,
|
|
|
|
hdHomerunManager,
|
|
|
|
remoteAddress,
|
|
|
|
taskCompletionSource,
|
2020-09-18 22:13:24 -07:00
|
|
|
LiveStreamCancellationTokenSource.Token);
|
2017-03-02 14:32:20 -07:00
|
|
|
|
2020-06-14 02:11:11 -07:00
|
|
|
// OpenedMediaSource.Protocol = MediaProtocol.File;
|
|
|
|
// OpenedMediaSource.Path = tempFile;
|
|
|
|
// OpenedMediaSource.ReadAtNativeFramerate = true;
|
2017-03-02 14:32:20 -07:00
|
|
|
|
2020-05-10 11:36:11 -07:00
|
|
|
MediaSource.Path = _appHost.GetLoopbackHttpApiUrl() + "/LiveTv/LiveStreamFiles/" + UniqueId + "/stream.ts";
|
2018-09-12 10:26:21 -07:00
|
|
|
MediaSource.Protocol = MediaProtocol.Http;
|
2020-06-14 02:11:11 -07:00
|
|
|
// OpenedMediaSource.SupportsDirectPlay = false;
|
|
|
|
// OpenedMediaSource.SupportsDirectStream = true;
|
|
|
|
// OpenedMediaSource.SupportsTranscoding = true;
|
2017-03-02 14:32:20 -07:00
|
|
|
|
2020-06-14 02:11:11 -07:00
|
|
|
// await Task.Delay(5000).ConfigureAwait(false);
|
2017-10-23 12:14:11 -07:00
|
|
|
await taskCompletionSource.Task.ConfigureAwait(false);
|
2017-03-02 14:32:20 -07:00
|
|
|
}
|
|
|
|
|
2020-11-03 16:38:47 -07:00
|
|
|
public string GetFilePath()
|
|
|
|
{
|
|
|
|
return TempFilePath;
|
|
|
|
}
|
|
|
|
|
2020-11-15 19:44:11 -07:00
|
|
|
private async Task StartStreaming(UdpClient udpClient, HdHomerunManager hdHomerunManager, IPAddress remoteAddress, TaskCompletionSource<bool> openTaskCompletionSource, CancellationToken cancellationToken)
|
2017-03-02 14:32:20 -07:00
|
|
|
{
|
2020-09-18 22:13:24 -07:00
|
|
|
using (udpClient)
|
|
|
|
using (hdHomerunManager)
|
2017-03-02 22:53:21 -07:00
|
|
|
{
|
2020-09-18 22:13:24 -07:00
|
|
|
try
|
2017-03-02 22:53:21 -07:00
|
|
|
{
|
2020-09-18 22:13:24 -07:00
|
|
|
await CopyTo(udpClient, TempFilePath, openTaskCompletionSource, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException ex)
|
|
|
|
{
|
|
|
|
Logger.LogInformation("HDHR UDP stream cancelled or timed out from {0}", remoteAddress);
|
|
|
|
openTaskCompletionSource.TrySetException(ex);
|
2017-03-02 22:53:21 -07:00
|
|
|
}
|
2020-09-18 22:13:24 -07:00
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Logger.LogError(ex, "Error opening live stream:");
|
|
|
|
openTaskCompletionSource.TrySetException(ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
EnableStreamSharing = false;
|
|
|
|
}
|
2017-03-02 22:53:21 -07:00
|
|
|
|
2020-09-18 22:13:24 -07:00
|
|
|
await DeleteTempFiles(new List<string> { TempFilePath }).ConfigureAwait(false);
|
2017-05-21 21:54:02 -07:00
|
|
|
}
|
|
|
|
|
2019-08-16 09:52:16 -07:00
|
|
|
private async Task CopyTo(UdpClient udpClient, string file, TaskCompletionSource<bool> openTaskCompletionSource, CancellationToken cancellationToken)
|
2017-05-25 06:00:14 -07:00
|
|
|
{
|
2019-08-16 09:52:16 -07:00
|
|
|
var resolved = false;
|
|
|
|
|
|
|
|
using (var fileStream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read))
|
2017-05-31 21:51:43 -07:00
|
|
|
{
|
2019-08-16 09:52:16 -07:00
|
|
|
while (true)
|
2018-12-20 05:11:26 -07:00
|
|
|
{
|
2019-08-16 09:52:16 -07:00
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
using (var timeOutSource = new CancellationTokenSource())
|
|
|
|
using (var linkedSource = CancellationTokenSource.CreateLinkedTokenSource(
|
|
|
|
cancellationToken,
|
|
|
|
timeOutSource.Token))
|
2019-07-07 07:39:35 -07:00
|
|
|
{
|
2019-08-16 09:52:16 -07:00
|
|
|
var resTask = udpClient.ReceiveAsync();
|
|
|
|
if (await Task.WhenAny(resTask, Task.Delay(30000, linkedSource.Token)).ConfigureAwait(false) != resTask)
|
|
|
|
{
|
|
|
|
resTask.Dispose();
|
|
|
|
break;
|
|
|
|
}
|
2017-05-25 06:00:14 -07:00
|
|
|
|
2019-08-16 09:52:16 -07:00
|
|
|
// We don't want all these delay tasks to keep running
|
|
|
|
timeOutSource.Cancel();
|
|
|
|
var res = await resTask.ConfigureAwait(false);
|
|
|
|
var buffer = res.Buffer;
|
2017-05-31 22:05:36 -07:00
|
|
|
|
2019-08-16 09:52:16 -07:00
|
|
|
var read = buffer.Length - RtpHeaderBytes;
|
2017-10-23 12:14:11 -07:00
|
|
|
|
2019-07-07 07:39:35 -07:00
|
|
|
if (read > 0)
|
|
|
|
{
|
2019-08-16 09:52:16 -07:00
|
|
|
fileStream.Write(buffer, RtpHeaderBytes, read);
|
2019-07-07 07:39:35 -07:00
|
|
|
}
|
2017-10-23 12:14:11 -07:00
|
|
|
|
2019-07-07 07:39:35 -07:00
|
|
|
if (!resolved)
|
|
|
|
{
|
|
|
|
resolved = true;
|
|
|
|
DateOpened = DateTime.UtcNow;
|
|
|
|
openTaskCompletionSource.TrySetResult(true);
|
|
|
|
}
|
2017-10-23 12:14:11 -07:00
|
|
|
}
|
2017-05-31 22:05:36 -07:00
|
|
|
}
|
2017-05-25 06:00:14 -07:00
|
|
|
}
|
|
|
|
}
|
2017-03-02 14:32:20 -07:00
|
|
|
}
|
2018-12-13 06:18:25 -07:00
|
|
|
}
|