jellyfin/Emby.Server.Implementations/LiveTv/TunerHosts/SharedHttpStream.cs

122 lines
5.2 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
using System.Globalization;
using System.IO;
using System.Net.Http;
2016-09-25 11:39:13 -07:00
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
2016-09-25 11:39:13 -07:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
2016-10-05 00:15:29 -07:00
using MediaBrowser.Controller.Library;
2016-09-25 11:39:13 -07:00
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.LiveTv;
using MediaBrowser.Model.MediaInfo;
using Microsoft.Extensions.Logging;
2016-09-25 11:39:13 -07:00
2017-11-14 00:41:21 -07:00
namespace Emby.Server.Implementations.LiveTv.TunerHosts
2016-09-25 11:39:13 -07:00
{
2017-11-14 00:41:21 -07:00
public class SharedHttpStream : LiveStream, IDirectStreamProvider
2016-09-25 11:39:13 -07:00
{
private readonly IHttpClientFactory _httpClientFactory;
2016-09-25 11:39:13 -07:00
private readonly IServerApplicationHost _appHost;
2019-07-07 07:39:35 -07:00
public SharedHttpStream(
MediaSourceInfo mediaSource,
TunerHostInfo tunerHostInfo,
string originalStreamId,
IFileSystem fileSystem,
IHttpClientFactory httpClientFactory,
2019-07-07 07:39:35 -07:00
ILogger logger,
IConfigurationManager configurationManager,
2019-07-07 07:39:35 -07:00
IServerApplicationHost appHost,
IStreamHelper streamHelper)
: base(mediaSource, tunerHostInfo, fileSystem, logger, configurationManager, streamHelper)
2016-09-25 11:39:13 -07:00
{
_httpClientFactory = httpClientFactory;
2016-09-25 11:39:13 -07:00
_appHost = appHost;
2016-10-07 08:08:13 -07:00
OriginalStreamId = originalStreamId;
2016-09-25 11:39:13 -07:00
}
2017-10-23 12:14:11 -07:00
public override async Task Open(CancellationToken openCancellationToken)
2016-09-25 11:39:13 -07:00
{
2017-10-13 23:52:56 -07:00
LiveStreamCancellationTokenSource.Token.ThrowIfCancellationRequested();
2016-09-25 11:39:13 -07:00
var mediaSource = OriginalMediaSource;
var url = mediaSource.Path;
2022-01-07 03:48:59 -07:00
Directory.CreateDirectory(Path.GetDirectoryName(TempFilePath) ?? throw new InvalidOperationException("Path can't be a root directory."));
2017-10-23 12:14:11 -07:00
2017-11-14 00:41:21 -07:00
var typeName = GetType().Name;
Logger.LogInformation("Opening {StreamType} Live stream from {Url}", typeName, url);
2016-09-25 11:39:13 -07:00
2020-10-29 12:58:47 -07:00
// Response stream is disposed manually.
2020-10-29 12:56:29 -07:00
var response = await _httpClientFactory.CreateClient(NamedClient.Default)
.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, CancellationToken.None)
.ConfigureAwait(false);
2017-10-23 12:14:11 -07:00
var taskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
2017-11-21 15:14:56 -07:00
2019-02-13 08:44:58 -07:00
_ = StartStreaming(response, taskCompletionSource, LiveStreamCancellationTokenSource.Token);
2016-09-25 11:39:13 -07:00
MediaSource.Path = _appHost.GetApiUrlForLocalAccess() + "/LiveTv/LiveStreamFiles/" + UniqueId + "/stream.ts";
2018-09-12 10:26:21 -07:00
MediaSource.Protocol = MediaProtocol.Http;
2017-09-09 20:47:33 -07:00
2022-01-07 03:48:59 -07:00
var res = await taskCompletionSource.Task.ConfigureAwait(false);
if (!res)
{
Logger.LogWarning("Zero bytes copied from stream {StreamType} to {FilePath} but no exception raised", GetType().Name, TempFilePath);
2021-05-28 05:33:54 -07:00
throw new EndOfStreamException(string.Format(CultureInfo.InvariantCulture, "Zero bytes copied from stream {0}", GetType().Name));
}
2016-09-25 11:39:13 -07:00
}
private Task StartStreaming(HttpResponseMessage response, TaskCompletionSource<bool> openTaskCompletionSource, CancellationToken cancellationToken)
2016-09-25 11:39:13 -07:00
{
return Task.Run(
async () =>
{
try
{
Logger.LogInformation("Beginning {StreamType} stream to {FilePath}", GetType().Name, TempFilePath);
using (response)
{
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
await using var fileStream = new FileStream(TempFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, IODefaults.FileStreamBufferSize, FileOptions.Asynchronous);
await StreamHelper.CopyToAsync(
stream,
fileStream,
IODefaults.CopyToBufferSize,
() => Resolve(openTaskCompletionSource),
cancellationToken).ConfigureAwait(false);
}
}
catch (OperationCanceledException ex)
{
Logger.LogInformation("Copying of {StreamType} to {FilePath} was canceled", GetType().Name, TempFilePath);
openTaskCompletionSource.TrySetException(ex);
}
catch (Exception ex)
{
Logger.LogError(ex, "Error copying live stream {StreamType} to {FilePath}", GetType().Name, TempFilePath);
openTaskCompletionSource.TrySetException(ex);
}
openTaskCompletionSource.TrySetResult(false);
EnableStreamSharing = false;
await DeleteTempFiles(TempFilePath).ConfigureAwait(false);
},
CancellationToken.None);
2017-05-21 21:54:02 -07:00
}
2016-10-02 23:28:45 -07:00
2017-05-31 22:05:36 -07:00
private void Resolve(TaskCompletionSource<bool> openTaskCompletionSource)
2017-05-21 21:54:02 -07:00
{
2018-09-12 10:26:21 -07:00
DateOpened = DateTime.UtcNow;
2017-11-06 14:32:44 -07:00
openTaskCompletionSource.TrySetResult(true);
2016-10-02 23:28:45 -07:00
}
2016-09-25 11:39:13 -07:00
}
}