| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | #pragma warning disable CA1711 |
| | | 4 | | #pragma warning disable CS1591 |
| | | 5 | | |
| | | 6 | | using System; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Linq; |
| | | 9 | | using System.Net; |
| | | 10 | | using System.Net.NetworkInformation; |
| | | 11 | | using System.Net.Sockets; |
| | | 12 | | using System.Threading; |
| | | 13 | | using System.Threading.Tasks; |
| | | 14 | | using MediaBrowser.Common.Configuration; |
| | | 15 | | using MediaBrowser.Controller; |
| | | 16 | | using MediaBrowser.Controller.Library; |
| | | 17 | | using MediaBrowser.Model.Dto; |
| | | 18 | | using MediaBrowser.Model.IO; |
| | | 19 | | using MediaBrowser.Model.LiveTv; |
| | | 20 | | using MediaBrowser.Model.MediaInfo; |
| | | 21 | | using Microsoft.Extensions.Logging; |
| | | 22 | | |
| | | 23 | | namespace Jellyfin.LiveTv.TunerHosts.HdHomerun |
| | | 24 | | { |
| | | 25 | | public class HdHomerunUdpStream : LiveStream, IDirectStreamProvider |
| | | 26 | | { |
| | | 27 | | private const int RtpHeaderBytes = 12; |
| | | 28 | | |
| | | 29 | | private readonly IServerApplicationHost _appHost; |
| | | 30 | | private readonly IHdHomerunChannelCommands _channelCommands; |
| | | 31 | | private readonly int _numTuners; |
| | | 32 | | |
| | | 33 | | public HdHomerunUdpStream( |
| | | 34 | | MediaSourceInfo mediaSource, |
| | | 35 | | TunerHostInfo tunerHostInfo, |
| | | 36 | | string originalStreamId, |
| | | 37 | | IHdHomerunChannelCommands channelCommands, |
| | | 38 | | int numTuners, |
| | | 39 | | IFileSystem fileSystem, |
| | | 40 | | ILogger logger, |
| | | 41 | | IConfigurationManager configurationManager, |
| | | 42 | | IServerApplicationHost appHost, |
| | | 43 | | IStreamHelper streamHelper) |
| | 0 | 44 | | : base(mediaSource, tunerHostInfo, fileSystem, logger, configurationManager, streamHelper) |
| | | 45 | | { |
| | 0 | 46 | | _appHost = appHost; |
| | 0 | 47 | | OriginalStreamId = originalStreamId; |
| | 0 | 48 | | _channelCommands = channelCommands; |
| | 0 | 49 | | _numTuners = numTuners; |
| | 0 | 50 | | EnableStreamSharing = true; |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Returns an unused UDP port number in the range specified. |
| | | 55 | | /// Temporarily placed here until future network PR merged. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="range">Upper and Lower boundary of ports to select.</param> |
| | | 58 | | /// <returns>System.Int32.</returns> |
| | | 59 | | private static int GetUdpPortFromRange((int Min, int Max) range) |
| | | 60 | | { |
| | 0 | 61 | | var properties = IPGlobalProperties.GetIPGlobalProperties(); |
| | | 62 | | |
| | | 63 | | // Get active udp listeners. |
| | 0 | 64 | | var udpListenerPorts = properties.GetActiveUdpListeners() |
| | 0 | 65 | | .Where(n => n.Port >= range.Min && n.Port <= range.Max) |
| | 0 | 66 | | .Select(n => n.Port); |
| | | 67 | | |
| | 0 | 68 | | return Enumerable |
| | 0 | 69 | | .Range(range.Min, range.Max) |
| | 0 | 70 | | .FirstOrDefault(i => !udpListenerPorts.Contains(i)); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | public override async Task Open(CancellationToken openCancellationToken) |
| | | 74 | | { |
| | | 75 | | LiveStreamCancellationTokenSource.Token.ThrowIfCancellationRequested(); |
| | | 76 | | |
| | | 77 | | var mediaSource = OriginalMediaSource; |
| | | 78 | | |
| | | 79 | | var uri = new Uri(mediaSource.Path); |
| | | 80 | | // Temporary code to reduce PR size. This will be updated by a future network pr. |
| | | 81 | | var localPort = GetUdpPortFromRange((49152, 65535)); |
| | | 82 | | |
| | | 83 | | Directory.CreateDirectory(Path.GetDirectoryName(TempFilePath)); |
| | | 84 | | |
| | | 85 | | Logger.LogInformation("Opening HDHR UDP Live stream from {Host}", uri.Host); |
| | | 86 | | |
| | | 87 | | var remoteAddress = IPAddress.Parse(uri.Host); |
| | | 88 | | IPAddress localAddress; |
| | | 89 | | using (var tcpClient = new TcpClient()) |
| | | 90 | | { |
| | | 91 | | try |
| | | 92 | | { |
| | | 93 | | await tcpClient.ConnectAsync(remoteAddress, HdHomerunManager.HdHomeRunPort, openCancellationToken).C |
| | | 94 | | localAddress = ((IPEndPoint)tcpClient.Client.LocalEndPoint).Address; |
| | | 95 | | tcpClient.Close(); |
| | | 96 | | } |
| | | 97 | | catch (Exception ex) |
| | | 98 | | { |
| | | 99 | | Logger.LogError(ex, "Unable to determine local ip address for Legacy HDHomerun stream."); |
| | | 100 | | return; |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | if (localAddress.IsIPv4MappedToIPv6) |
| | | 105 | | { |
| | | 106 | | localAddress = localAddress.MapToIPv4(); |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | var udpClient = new UdpClient(localPort, AddressFamily.InterNetwork); |
| | | 110 | | var hdHomerunManager = new HdHomerunManager(); |
| | | 111 | | |
| | | 112 | | try |
| | | 113 | | { |
| | | 114 | | // send url to start streaming |
| | | 115 | | await hdHomerunManager.StartStreaming( |
| | | 116 | | remoteAddress, |
| | | 117 | | localAddress, |
| | | 118 | | localPort, |
| | | 119 | | _channelCommands, |
| | | 120 | | _numTuners, |
| | | 121 | | openCancellationToken).ConfigureAwait(false); |
| | | 122 | | } |
| | | 123 | | catch (Exception ex) |
| | | 124 | | { |
| | | 125 | | using (udpClient) |
| | | 126 | | using (hdHomerunManager) |
| | | 127 | | { |
| | | 128 | | if (ex is not OperationCanceledException) |
| | | 129 | | { |
| | | 130 | | Logger.LogError(ex, "Error opening live stream:"); |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | throw; |
| | | 134 | | } |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | var taskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously |
| | | 138 | | |
| | | 139 | | _ = StartStreaming( |
| | | 140 | | udpClient, |
| | | 141 | | hdHomerunManager, |
| | | 142 | | remoteAddress, |
| | | 143 | | taskCompletionSource, |
| | | 144 | | LiveStreamCancellationTokenSource.Token); |
| | | 145 | | |
| | | 146 | | // OpenedMediaSource.Protocol = MediaProtocol.File; |
| | | 147 | | // OpenedMediaSource.Path = tempFile; |
| | | 148 | | // OpenedMediaSource.ReadAtNativeFramerate = true; |
| | | 149 | | |
| | | 150 | | MediaSource.Path = _appHost.GetApiUrlForLocalAccess() + "/LiveTv/LiveStreamFiles/" + UniqueId + "/stream.ts" |
| | | 151 | | MediaSource.Protocol = MediaProtocol.Http; |
| | | 152 | | // OpenedMediaSource.SupportsDirectPlay = false; |
| | | 153 | | // OpenedMediaSource.SupportsDirectStream = true; |
| | | 154 | | // OpenedMediaSource.SupportsTranscoding = true; |
| | | 155 | | |
| | | 156 | | // await Task.Delay(5000).ConfigureAwait(false); |
| | | 157 | | await taskCompletionSource.Task.ConfigureAwait(false); |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | private async Task StartStreaming(UdpClient udpClient, HdHomerunManager hdHomerunManager, IPAddress remoteAddres |
| | | 161 | | { |
| | | 162 | | using (udpClient) |
| | | 163 | | using (hdHomerunManager) |
| | | 164 | | { |
| | | 165 | | try |
| | | 166 | | { |
| | | 167 | | await CopyTo(udpClient, TempFilePath, openTaskCompletionSource, cancellationToken).ConfigureAwait(fa |
| | | 168 | | } |
| | | 169 | | catch (Exception ex) when (ex is OperationCanceledException || ex is TimeoutException) |
| | | 170 | | { |
| | | 171 | | Logger.LogInformation("HDHR UDP stream cancelled or timed out from {0}", remoteAddress); |
| | | 172 | | openTaskCompletionSource.TrySetException(ex); |
| | | 173 | | } |
| | | 174 | | catch (Exception ex) |
| | | 175 | | { |
| | | 176 | | Logger.LogError(ex, "Error opening live stream:"); |
| | | 177 | | openTaskCompletionSource.TrySetException(ex); |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | EnableStreamSharing = false; |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | await DeleteTempFiles(TempFilePath).ConfigureAwait(false); |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | private async Task CopyTo(UdpClient udpClient, string file, TaskCompletionSource<bool> openTaskCompletionSource, |
| | | 187 | | { |
| | | 188 | | var resolved = false; |
| | | 189 | | |
| | | 190 | | var fileStream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read); |
| | | 191 | | await using (fileStream.ConfigureAwait(false)) |
| | | 192 | | { |
| | | 193 | | while (true) |
| | | 194 | | { |
| | | 195 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 196 | | var res = await udpClient.ReceiveAsync(cancellationToken) |
| | | 197 | | .AsTask() |
| | | 198 | | .WaitAsync(TimeSpan.FromMilliseconds(30000), CancellationToken.None) |
| | | 199 | | .ConfigureAwait(false); |
| | | 200 | | var buffer = res.Buffer; |
| | | 201 | | |
| | | 202 | | var read = buffer.Length - RtpHeaderBytes; |
| | | 203 | | |
| | | 204 | | if (read > 0) |
| | | 205 | | { |
| | | 206 | | await fileStream.WriteAsync(buffer.AsMemory(RtpHeaderBytes, read), cancellationToken).ConfigureA |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | if (!resolved) |
| | | 210 | | { |
| | | 211 | | resolved = true; |
| | | 212 | | DateOpened = DateTime.UtcNow; |
| | | 213 | | openTaskCompletionSource.TrySetResult(true); |
| | | 214 | | } |
| | | 215 | | } |
| | | 216 | | } |
| | | 217 | | } |
| | | 218 | | } |
| | | 219 | | } |