| | 1 | | #nullable disable |
| | 2 | |
|
| | 3 | | #pragma warning disable CA1711 |
| | 4 | | #pragma warning disable CS1591 |
| | 5 | |
|
| | 6 | | using System; |
| | 7 | | using System.Globalization; |
| | 8 | | using System.IO; |
| | 9 | | using System.Threading; |
| | 10 | | using System.Threading.Tasks; |
| | 11 | | using MediaBrowser.Common.Configuration; |
| | 12 | | using MediaBrowser.Controller.Library; |
| | 13 | | using MediaBrowser.Model.Dto; |
| | 14 | | using MediaBrowser.Model.IO; |
| | 15 | | using MediaBrowser.Model.LiveTv; |
| | 16 | | using Microsoft.Extensions.Logging; |
| | 17 | |
|
| | 18 | | namespace Jellyfin.LiveTv.TunerHosts |
| | 19 | | { |
| | 20 | | public class LiveStream : ILiveStream |
| | 21 | | { |
| | 22 | | private readonly IConfigurationManager _configurationManager; |
| | 23 | |
|
| | 24 | | public LiveStream( |
| | 25 | | MediaSourceInfo mediaSource, |
| | 26 | | TunerHostInfo tuner, |
| | 27 | | IFileSystem fileSystem, |
| | 28 | | ILogger logger, |
| | 29 | | IConfigurationManager configurationManager, |
| | 30 | | IStreamHelper streamHelper) |
| | 31 | | { |
| 0 | 32 | | OriginalMediaSource = mediaSource; |
| | 33 | | FileSystem = fileSystem; |
| 0 | 34 | | MediaSource = mediaSource; |
| | 35 | | Logger = logger; |
| 0 | 36 | | EnableStreamSharing = true; |
| 0 | 37 | | UniqueId = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture); |
| | 38 | |
|
| 0 | 39 | | if (tuner is not null) |
| | 40 | | { |
| 0 | 41 | | TunerHostId = tuner.Id; |
| | 42 | | } |
| | 43 | |
|
| 0 | 44 | | _configurationManager = configurationManager; |
| | 45 | | StreamHelper = streamHelper; |
| | 46 | |
|
| 0 | 47 | | ConsumerCount = 1; |
| 0 | 48 | | SetTempFilePath("ts"); |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | protected IFileSystem FileSystem { get; } |
| | 52 | |
|
| | 53 | | protected IStreamHelper StreamHelper { get; } |
| | 54 | |
|
| | 55 | | protected ILogger Logger { get; } |
| | 56 | |
|
| | 57 | | protected CancellationTokenSource LiveStreamCancellationTokenSource { get; } = new CancellationTokenSource(); |
| | 58 | |
|
| | 59 | | protected string TempFilePath { get; set; } |
| | 60 | |
|
| | 61 | | public MediaSourceInfo OriginalMediaSource { get; set; } |
| | 62 | |
|
| | 63 | | public MediaSourceInfo MediaSource { get; set; } |
| | 64 | |
|
| | 65 | | public int ConsumerCount { get; set; } |
| | 66 | |
|
| | 67 | | public string OriginalStreamId { get; set; } |
| | 68 | |
|
| | 69 | | public bool EnableStreamSharing { get; set; } |
| | 70 | |
|
| | 71 | | public string UniqueId { get; } |
| | 72 | |
|
| | 73 | | public string TunerHostId { get; } |
| | 74 | |
|
| | 75 | | public DateTime DateOpened { get; protected set; } |
| | 76 | |
|
| | 77 | | protected void SetTempFilePath(string extension) |
| | 78 | | { |
| 0 | 79 | | TempFilePath = Path.Combine(_configurationManager.GetTranscodePath(), UniqueId + "." + extension); |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | public virtual Task Open(CancellationToken openCancellationToken) |
| | 83 | | { |
| 0 | 84 | | DateOpened = DateTime.UtcNow; |
| 0 | 85 | | return Task.CompletedTask; |
| | 86 | | } |
| | 87 | |
|
| | 88 | | public async Task Close() |
| | 89 | | { |
| | 90 | | EnableStreamSharing = false; |
| | 91 | |
|
| | 92 | | Logger.LogInformation("Closing {Type}", GetType().Name); |
| | 93 | |
|
| | 94 | | await LiveStreamCancellationTokenSource.CancelAsync().ConfigureAwait(false); |
| | 95 | | } |
| | 96 | |
|
| | 97 | | public Stream GetStream() |
| | 98 | | { |
| 0 | 99 | | var stream = new FileStream( |
| 0 | 100 | | TempFilePath, |
| 0 | 101 | | FileMode.Open, |
| 0 | 102 | | FileAccess.Read, |
| 0 | 103 | | FileShare.ReadWrite, |
| 0 | 104 | | IODefaults.FileStreamBufferSize, |
| 0 | 105 | | FileOptions.SequentialScan | FileOptions.Asynchronous); |
| | 106 | |
|
| 0 | 107 | | bool seekFile = (DateTime.UtcNow - DateOpened).TotalSeconds > 10; |
| 0 | 108 | | if (seekFile) |
| | 109 | | { |
| 0 | 110 | | TrySeek(stream, -20000); |
| | 111 | | } |
| | 112 | |
|
| 0 | 113 | | return stream; |
| | 114 | | } |
| | 115 | |
|
| | 116 | | /// <inheritdoc /> |
| | 117 | | public void Dispose() |
| | 118 | | { |
| 0 | 119 | | Dispose(true); |
| 0 | 120 | | GC.SuppressFinalize(this); |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | protected virtual void Dispose(bool dispose) |
| | 124 | | { |
| 0 | 125 | | if (dispose) |
| | 126 | | { |
| 0 | 127 | | LiveStreamCancellationTokenSource?.Dispose(); |
| | 128 | | } |
| 0 | 129 | | } |
| | 130 | |
|
| | 131 | | protected async Task DeleteTempFiles(string path, int retryCount = 0) |
| | 132 | | { |
| | 133 | | if (retryCount == 0) |
| | 134 | | { |
| | 135 | | Logger.LogInformation("Deleting temp file {FilePath}", path); |
| | 136 | | } |
| | 137 | |
|
| | 138 | | try |
| | 139 | | { |
| | 140 | | FileSystem.DeleteFile(path); |
| | 141 | | } |
| | 142 | | catch (Exception ex) |
| | 143 | | { |
| | 144 | | Logger.LogError(ex, "Error deleting file {FilePath}", path); |
| | 145 | | if (retryCount <= 40) |
| | 146 | | { |
| | 147 | | await Task.Delay(500).ConfigureAwait(false); |
| | 148 | | await DeleteTempFiles(path, retryCount + 1).ConfigureAwait(false); |
| | 149 | | } |
| | 150 | | } |
| | 151 | | } |
| | 152 | |
|
| | 153 | | private void TrySeek(FileStream stream, long offset) |
| | 154 | | { |
| 0 | 155 | | if (!stream.CanSeek) |
| | 156 | | { |
| 0 | 157 | | return; |
| | 158 | | } |
| | 159 | |
|
| | 160 | | try |
| | 161 | | { |
| 0 | 162 | | stream.Seek(offset, SeekOrigin.End); |
| 0 | 163 | | } |
| 0 | 164 | | catch (IOException) |
| | 165 | | { |
| 0 | 166 | | } |
| 0 | 167 | | catch (ArgumentException) |
| | 168 | | { |
| 0 | 169 | | } |
| 0 | 170 | | catch (Exception ex) |
| | 171 | | { |
| 0 | 172 | | Logger.LogError(ex, "Error seeking stream"); |
| 0 | 173 | | } |
| 0 | 174 | | } |
| | 175 | | } |
| | 176 | | } |