| | 1 | | #pragma warning disable CA1711 |
| | 2 | | #pragma warning disable CS1591 |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Globalization; |
| | 6 | | using System.IO; |
| | 7 | | using System.Net.Http; |
| | 8 | | using System.Threading; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | | using MediaBrowser.Common.Configuration; |
| | 11 | | using MediaBrowser.Common.Net; |
| | 12 | | using MediaBrowser.Controller; |
| | 13 | | using MediaBrowser.Controller.Library; |
| | 14 | | using MediaBrowser.Model.Dto; |
| | 15 | | using MediaBrowser.Model.IO; |
| | 16 | | using MediaBrowser.Model.LiveTv; |
| | 17 | | using MediaBrowser.Model.MediaInfo; |
| | 18 | | using Microsoft.Extensions.Logging; |
| | 19 | |
|
| | 20 | | namespace Jellyfin.LiveTv.TunerHosts |
| | 21 | | { |
| | 22 | | public class SharedHttpStream : LiveStream, IDirectStreamProvider |
| | 23 | | { |
| | 24 | | private readonly IHttpClientFactory _httpClientFactory; |
| | 25 | | private readonly IServerApplicationHost _appHost; |
| | 26 | |
|
| | 27 | | public SharedHttpStream( |
| | 28 | | MediaSourceInfo mediaSource, |
| | 29 | | TunerHostInfo tunerHostInfo, |
| | 30 | | string originalStreamId, |
| | 31 | | IFileSystem fileSystem, |
| | 32 | | IHttpClientFactory httpClientFactory, |
| | 33 | | ILogger logger, |
| | 34 | | IConfigurationManager configurationManager, |
| | 35 | | IServerApplicationHost appHost, |
| | 36 | | IStreamHelper streamHelper) |
| 0 | 37 | | : base(mediaSource, tunerHostInfo, fileSystem, logger, configurationManager, streamHelper) |
| | 38 | | { |
| 0 | 39 | | _httpClientFactory = httpClientFactory; |
| 0 | 40 | | _appHost = appHost; |
| 0 | 41 | | OriginalStreamId = originalStreamId; |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | public override async Task Open(CancellationToken openCancellationToken) |
| | 45 | | { |
| | 46 | | LiveStreamCancellationTokenSource.Token.ThrowIfCancellationRequested(); |
| | 47 | |
|
| | 48 | | var mediaSource = OriginalMediaSource; |
| | 49 | |
|
| | 50 | | var url = mediaSource.Path; |
| | 51 | |
|
| | 52 | | Directory.CreateDirectory(Path.GetDirectoryName(TempFilePath) ?? throw new InvalidOperationException("Path c |
| | 53 | |
|
| | 54 | | var typeName = GetType().Name; |
| | 55 | | Logger.LogInformation("Opening {StreamType} Live stream from {Url}", typeName, url); |
| | 56 | |
|
| | 57 | | // Response stream is disposed manually. |
| | 58 | | var response = await _httpClientFactory.CreateClient(NamedClient.Default) |
| | 59 | | .GetAsync(url, HttpCompletionOption.ResponseHeadersRead, CancellationToken.None) |
| | 60 | | .ConfigureAwait(false); |
| | 61 | |
|
| | 62 | | var taskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously |
| | 63 | |
|
| | 64 | | _ = StartStreaming(response, taskCompletionSource, LiveStreamCancellationTokenSource.Token); |
| | 65 | |
|
| | 66 | | MediaSource.Path = _appHost.GetApiUrlForLocalAccess() + "/LiveTv/LiveStreamFiles/" + UniqueId + "/stream.ts" |
| | 67 | | MediaSource.Protocol = MediaProtocol.Http; |
| | 68 | |
|
| | 69 | | var res = await taskCompletionSource.Task.ConfigureAwait(false); |
| | 70 | | if (!res) |
| | 71 | | { |
| | 72 | | Logger.LogWarning("Zero bytes copied from stream {StreamType} to {FilePath} but no exception raised", Ge |
| | 73 | | throw new EndOfStreamException(string.Format(CultureInfo.InvariantCulture, "Zero bytes copied from strea |
| | 74 | | } |
| | 75 | | } |
| | 76 | |
|
| | 77 | | private Task StartStreaming(HttpResponseMessage response, TaskCompletionSource<bool> openTaskCompletionSource, C |
| | 78 | | { |
| 0 | 79 | | return Task.Run( |
| 0 | 80 | | async () => |
| 0 | 81 | | { |
| 0 | 82 | | try |
| 0 | 83 | | { |
| 0 | 84 | | Logger.LogInformation("Beginning {StreamType} stream to {FilePath}", GetType().Name, TempFilePat |
| 0 | 85 | | using (response) |
| 0 | 86 | | { |
| 0 | 87 | | var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(fals |
| 0 | 88 | | await using (stream.ConfigureAwait(false)) |
| 0 | 89 | | { |
| 0 | 90 | | var fileStream = new FileStream( |
| 0 | 91 | | TempFilePath, |
| 0 | 92 | | FileMode.Create, |
| 0 | 93 | | FileAccess.Write, |
| 0 | 94 | | FileShare.Read, |
| 0 | 95 | | IODefaults.FileStreamBufferSize, |
| 0 | 96 | | FileOptions.Asynchronous); |
| 0 | 97 | |
|
| 0 | 98 | | await using (fileStream.ConfigureAwait(false)) |
| 0 | 99 | | { |
| 0 | 100 | | await StreamHelper.CopyToAsync( |
| 0 | 101 | | stream, |
| 0 | 102 | | fileStream, |
| 0 | 103 | | IODefaults.CopyToBufferSize, |
| 0 | 104 | | () => Resolve(openTaskCompletionSource), |
| 0 | 105 | | cancellationToken).ConfigureAwait(false); |
| 0 | 106 | | } |
| 0 | 107 | | } |
| 0 | 108 | | } |
| 0 | 109 | | } |
| 0 | 110 | | catch (OperationCanceledException ex) |
| 0 | 111 | | { |
| 0 | 112 | | Logger.LogInformation("Copying of {StreamType} to {FilePath} was canceled", GetType().Name, Temp |
| 0 | 113 | | openTaskCompletionSource.TrySetException(ex); |
| 0 | 114 | | } |
| 0 | 115 | | catch (Exception ex) |
| 0 | 116 | | { |
| 0 | 117 | | Logger.LogError(ex, "Error copying live stream {StreamType} to {FilePath}", GetType().Name, Temp |
| 0 | 118 | | openTaskCompletionSource.TrySetException(ex); |
| 0 | 119 | | } |
| 0 | 120 | |
|
| 0 | 121 | | openTaskCompletionSource.TrySetResult(false); |
| 0 | 122 | |
|
| 0 | 123 | | EnableStreamSharing = false; |
| 0 | 124 | | await DeleteTempFiles(TempFilePath).ConfigureAwait(false); |
| 0 | 125 | | }, |
| 0 | 126 | | CancellationToken.None); |
| | 127 | | } |
| | 128 | |
|
| | 129 | | private void Resolve(TaskCompletionSource<bool> openTaskCompletionSource) |
| | 130 | | { |
| 0 | 131 | | DateOpened = DateTime.UtcNow; |
| 0 | 132 | | openTaskCompletionSource.TrySetResult(true); |
| 0 | 133 | | } |
| | 134 | | } |
| | 135 | | } |