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