< Summary - Jellyfin

Information
Class: Jellyfin.LiveTv.TunerHosts.SharedHttpStream
Assembly: Jellyfin.LiveTv
File(s): /srv/git/jellyfin/src/Jellyfin.LiveTv/TunerHosts/SharedHttpStream.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 56
Coverable lines: 56
Total lines: 135
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
StartStreaming(...)100%210%
Resolve(...)100%210%

File(s)

/srv/git/jellyfin/src/Jellyfin.LiveTv/TunerHosts/SharedHttpStream.cs

#LineLine coverage
 1#pragma warning disable CA1711
 2#pragma warning disable CS1591
 3
 4using System;
 5using System.Globalization;
 6using System.IO;
 7using System.Net.Http;
 8using System.Threading;
 9using System.Threading.Tasks;
 10using MediaBrowser.Common.Configuration;
 11using MediaBrowser.Common.Net;
 12using MediaBrowser.Controller;
 13using MediaBrowser.Controller.Library;
 14using MediaBrowser.Model.Dto;
 15using MediaBrowser.Model.IO;
 16using MediaBrowser.Model.LiveTv;
 17using MediaBrowser.Model.MediaInfo;
 18using Microsoft.Extensions.Logging;
 19
 20namespace 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)
 037            : base(mediaSource, tunerHostInfo, fileSystem, logger, configurationManager, streamHelper)
 38        {
 039            _httpClientFactory = httpClientFactory;
 040            _appHost = appHost;
 041            OriginalStreamId = originalStreamId;
 042        }
 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        {
 079            return Task.Run(
 080                async () =>
 081                {
 082                    try
 083                    {
 084                        Logger.LogInformation("Beginning {StreamType} stream to {FilePath}", GetType().Name, TempFilePat
 085                        using (response)
 086                        {
 087                            var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(fals
 088                            await using (stream.ConfigureAwait(false))
 089                            {
 090                                var fileStream = new FileStream(
 091                                    TempFilePath,
 092                                    FileMode.Create,
 093                                    FileAccess.Write,
 094                                    FileShare.Read,
 095                                    IODefaults.FileStreamBufferSize,
 096                                    FileOptions.Asynchronous);
 097
 098                                await using (fileStream.ConfigureAwait(false))
 099                                {
 0100                                    await StreamHelper.CopyToAsync(
 0101                                        stream,
 0102                                        fileStream,
 0103                                        IODefaults.CopyToBufferSize,
 0104                                        () => Resolve(openTaskCompletionSource),
 0105                                        cancellationToken).ConfigureAwait(false);
 0106                                }
 0107                            }
 0108                        }
 0109                    }
 0110                    catch (OperationCanceledException ex)
 0111                    {
 0112                        Logger.LogInformation("Copying of {StreamType} to {FilePath} was canceled", GetType().Name, Temp
 0113                        openTaskCompletionSource.TrySetException(ex);
 0114                    }
 0115                    catch (Exception ex)
 0116                    {
 0117                        Logger.LogError(ex, "Error copying live stream {StreamType} to {FilePath}", GetType().Name, Temp
 0118                        openTaskCompletionSource.TrySetException(ex);
 0119                    }
 0120
 0121                    openTaskCompletionSource.TrySetResult(false);
 0122
 0123                    EnableStreamSharing = false;
 0124                    await DeleteTempFiles(TempFilePath).ConfigureAwait(false);
 0125                },
 0126                CancellationToken.None);
 127        }
 128
 129        private void Resolve(TaskCompletionSource<bool> openTaskCompletionSource)
 130        {
 0131            DateOpened = DateTime.UtcNow;
 0132            openTaskCompletionSource.TrySetResult(true);
 0133        }
 134    }
 135}