2021-05-20 21:28:18 +02:00
|
|
|
#nullable disable
|
|
|
|
|
2020-02-06 15:20:23 +01:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 20:54:44 +01:00
|
|
|
using System;
|
2019-01-13 20:22:00 +01:00
|
|
|
using System.Collections.Generic;
|
2020-05-08 22:11:43 +02:00
|
|
|
using System.Globalization;
|
2019-01-26 21:47:11 +01:00
|
|
|
using System.IO;
|
2019-12-13 20:11:37 +01:00
|
|
|
using System.Net.Http;
|
2016-09-25 20:39:13 +02:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-11-08 12:49:00 +01:00
|
|
|
using MediaBrowser.Common.Configuration;
|
2016-09-25 20:39:13 +02:00
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using MediaBrowser.Controller;
|
2016-10-05 09:15:29 +02:00
|
|
|
using MediaBrowser.Controller.Library;
|
2016-09-25 20:39:13 +02:00
|
|
|
using MediaBrowser.Model.Dto;
|
2019-01-13 20:22:00 +01:00
|
|
|
using MediaBrowser.Model.IO;
|
2017-11-17 22:54:33 +01:00
|
|
|
using MediaBrowser.Model.LiveTv;
|
2019-01-13 20:22:00 +01:00
|
|
|
using MediaBrowser.Model.MediaInfo;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2016-09-25 20:39:13 +02:00
|
|
|
|
2017-11-14 08:41:21 +01:00
|
|
|
namespace Emby.Server.Implementations.LiveTv.TunerHosts
|
2016-09-25 20:39:13 +02:00
|
|
|
{
|
2017-11-14 08:41:21 +01:00
|
|
|
public class SharedHttpStream : LiveStream, IDirectStreamProvider
|
2016-09-25 20:39:13 +02:00
|
|
|
{
|
2020-08-31 20:06:42 +02:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2016-09-25 20:39:13 +02:00
|
|
|
private readonly IServerApplicationHost _appHost;
|
|
|
|
|
2019-07-07 16:39:35 +02:00
|
|
|
public SharedHttpStream(
|
|
|
|
MediaSourceInfo mediaSource,
|
|
|
|
TunerHostInfo tunerHostInfo,
|
|
|
|
string originalStreamId,
|
|
|
|
IFileSystem fileSystem,
|
2020-08-31 20:06:42 +02:00
|
|
|
IHttpClientFactory httpClientFactory,
|
2019-07-07 16:39:35 +02:00
|
|
|
ILogger logger,
|
2019-11-08 12:49:00 +01:00
|
|
|
IConfigurationManager configurationManager,
|
2019-07-07 16:39:35 +02:00
|
|
|
IServerApplicationHost appHost,
|
|
|
|
IStreamHelper streamHelper)
|
2019-11-08 12:49:00 +01:00
|
|
|
: base(mediaSource, tunerHostInfo, fileSystem, logger, configurationManager, streamHelper)
|
2016-09-25 20:39:13 +02:00
|
|
|
{
|
2020-08-31 20:06:42 +02:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2016-09-25 20:39:13 +02:00
|
|
|
_appHost = appHost;
|
2016-10-07 17:08:13 +02:00
|
|
|
OriginalStreamId = originalStreamId;
|
2017-11-14 08:41:21 +01:00
|
|
|
EnableStreamSharing = true;
|
2016-09-25 20:39:13 +02:00
|
|
|
}
|
|
|
|
|
2017-10-23 21:14:11 +02:00
|
|
|
public override async Task Open(CancellationToken openCancellationToken)
|
2016-09-25 20:39:13 +02:00
|
|
|
{
|
2017-10-14 08:52:56 +02:00
|
|
|
LiveStreamCancellationTokenSource.Token.ThrowIfCancellationRequested();
|
2016-09-25 20:39:13 +02:00
|
|
|
|
|
|
|
var mediaSource = OriginalMediaSource;
|
|
|
|
|
|
|
|
var url = mediaSource.Path;
|
|
|
|
|
2019-01-26 22:08:04 +01:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(TempFilePath));
|
2017-10-23 21:14:11 +02:00
|
|
|
|
2017-11-14 08:41:21 +01:00
|
|
|
var typeName = GetType().Name;
|
2018-12-13 14:18:25 +01:00
|
|
|
Logger.LogInformation("Opening " + typeName + " Live stream from {0}", url);
|
2016-09-25 20:39:13 +02:00
|
|
|
|
2020-10-29 20:58:47 +01:00
|
|
|
// Response stream is disposed manually.
|
2020-10-29 20:56:29 +01:00
|
|
|
var response = await _httpClientFactory.CreateClient(NamedClient.Default)
|
2020-09-01 15:51:06 +02:00
|
|
|
.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, CancellationToken.None)
|
2020-08-31 20:06:42 +02:00
|
|
|
.ConfigureAwait(false);
|
2017-10-23 21:14:11 +02:00
|
|
|
|
2017-11-14 08:41:21 +01:00
|
|
|
var extension = "ts";
|
|
|
|
var requiresRemux = false;
|
|
|
|
|
2020-11-25 00:42:27 +01:00
|
|
|
var contentType = response.Content.Headers.ContentType?.ToString() ?? string.Empty;
|
2017-11-19 05:59:34 +01:00
|
|
|
if (contentType.IndexOf("matroska", StringComparison.OrdinalIgnoreCase) != -1)
|
|
|
|
{
|
2017-11-20 01:20:12 +01:00
|
|
|
requiresRemux = true;
|
2017-11-19 05:59:34 +01:00
|
|
|
}
|
|
|
|
else if (contentType.IndexOf("mp4", StringComparison.OrdinalIgnoreCase) != -1 ||
|
|
|
|
contentType.IndexOf("dash", StringComparison.OrdinalIgnoreCase) != -1 ||
|
2017-11-29 21:50:18 +01:00
|
|
|
contentType.IndexOf("mpegURL", StringComparison.OrdinalIgnoreCase) != -1 ||
|
|
|
|
contentType.IndexOf("text/", StringComparison.OrdinalIgnoreCase) != -1)
|
2017-11-14 08:41:21 +01:00
|
|
|
{
|
|
|
|
requiresRemux = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the stream without any sharing features
|
|
|
|
if (requiresRemux)
|
|
|
|
{
|
|
|
|
using (response)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetTempFilePath(extension);
|
2017-10-23 21:14:11 +02:00
|
|
|
|
2017-11-06 22:32:44 +01:00
|
|
|
var taskCompletionSource = new TaskCompletionSource<bool>();
|
2017-11-21 23:14:56 +01:00
|
|
|
|
2019-02-13 16:44:58 +01:00
|
|
|
_ = StartStreaming(response, taskCompletionSource, LiveStreamCancellationTokenSource.Token);
|
2016-09-25 20:39:13 +02:00
|
|
|
|
2020-06-14 11:11:11 +02:00
|
|
|
// OpenedMediaSource.Protocol = MediaProtocol.File;
|
|
|
|
// OpenedMediaSource.Path = tempFile;
|
|
|
|
// OpenedMediaSource.ReadAtNativeFramerate = true;
|
2016-09-25 20:39:13 +02:00
|
|
|
|
2020-05-10 20:36:11 +02:00
|
|
|
MediaSource.Path = _appHost.GetLoopbackHttpApiUrl() + "/LiveTv/LiveStreamFiles/" + UniqueId + "/stream.ts";
|
2018-09-12 19:26:21 +02:00
|
|
|
MediaSource.Protocol = MediaProtocol.Http;
|
2017-09-10 05:47:33 +02:00
|
|
|
|
2020-06-14 11:11:11 +02:00
|
|
|
// OpenedMediaSource.Path = TempFilePath;
|
|
|
|
// OpenedMediaSource.Protocol = MediaProtocol.File;
|
2017-11-20 01:20:12 +01:00
|
|
|
|
2020-06-14 11:11:11 +02:00
|
|
|
// OpenedMediaSource.Path = _tempFilePath;
|
|
|
|
// OpenedMediaSource.Protocol = MediaProtocol.File;
|
|
|
|
// OpenedMediaSource.SupportsDirectPlay = false;
|
|
|
|
// OpenedMediaSource.SupportsDirectStream = true;
|
|
|
|
// OpenedMediaSource.SupportsTranscoding = true;
|
2017-11-06 22:32:44 +01:00
|
|
|
await taskCompletionSource.Task.ConfigureAwait(false);
|
2020-05-08 13:32:41 +02:00
|
|
|
if (taskCompletionSource.Task.Exception != null)
|
|
|
|
{
|
2020-05-18 16:01:29 +02:00
|
|
|
// Error happened while opening the stream so raise the exception again to inform the caller
|
2020-05-08 13:32:41 +02:00
|
|
|
throw taskCompletionSource.Task.Exception;
|
|
|
|
}
|
2020-05-18 16:01:29 +02:00
|
|
|
|
2020-05-08 22:11:43 +02:00
|
|
|
if (!taskCompletionSource.Task.Result)
|
|
|
|
{
|
|
|
|
Logger.LogWarning("Zero bytes copied from stream {0} to {1} but no exception raised", GetType().Name, TempFilePath);
|
2021-05-28 14:33:54 +02:00
|
|
|
throw new EndOfStreamException(string.Format(CultureInfo.InvariantCulture, "Zero bytes copied from stream {0}", GetType().Name));
|
2020-05-08 22:11:43 +02:00
|
|
|
}
|
2016-09-25 20:39:13 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 00:38:47 +01:00
|
|
|
public string GetFilePath()
|
|
|
|
{
|
|
|
|
return TempFilePath;
|
|
|
|
}
|
|
|
|
|
2020-08-31 20:06:42 +02:00
|
|
|
private Task StartStreaming(HttpResponseMessage response, TaskCompletionSource<bool> openTaskCompletionSource, CancellationToken cancellationToken)
|
2016-09-25 20:39:13 +02:00
|
|
|
{
|
2021-06-12 22:20:35 +02:00
|
|
|
return Task.Run(
|
|
|
|
async () =>
|
2017-10-11 08:24:22 +02:00
|
|
|
{
|
2021-06-12 22:20:35 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Logger.LogInformation("Beginning {0} stream to {1}", GetType().Name, TempFilePath);
|
|
|
|
using var message = 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, AsyncFile.UseAsyncIO);
|
|
|
|
await StreamHelper.CopyToAsync(
|
|
|
|
stream,
|
|
|
|
fileStream,
|
|
|
|
IODefaults.CopyToBufferSize,
|
|
|
|
() => Resolve(openTaskCompletionSource),
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException ex)
|
|
|
|
{
|
|
|
|
Logger.LogInformation("Copying of {0} to {1} was canceled", GetType().Name, TempFilePath);
|
|
|
|
openTaskCompletionSource.TrySetException(ex);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Logger.LogError(ex, "Error copying live stream {0} to {1}.", GetType().Name, TempFilePath);
|
|
|
|
openTaskCompletionSource.TrySetException(ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
openTaskCompletionSource.TrySetResult(false);
|
|
|
|
|
|
|
|
EnableStreamSharing = false;
|
|
|
|
await DeleteTempFiles(new List<string> { TempFilePath }).ConfigureAwait(false);
|
|
|
|
},
|
|
|
|
CancellationToken.None);
|
2017-05-22 06:54:02 +02:00
|
|
|
}
|
2016-10-03 08:28:45 +02:00
|
|
|
|
2017-06-01 07:05:36 +02:00
|
|
|
private void Resolve(TaskCompletionSource<bool> openTaskCompletionSource)
|
2017-05-22 06:54:02 +02:00
|
|
|
{
|
2018-09-12 19:26:21 +02:00
|
|
|
DateOpened = DateTime.UtcNow;
|
2017-11-06 22:32:44 +01:00
|
|
|
openTaskCompletionSource.TrySetResult(true);
|
2016-10-03 08:28:45 +02:00
|
|
|
}
|
2016-09-25 20:39:13 +02:00
|
|
|
}
|
|
|
|
}
|