2020-02-06 07:20:23 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 12:54:44 -07:00
|
|
|
using System;
|
2019-01-13 12:22:00 -07:00
|
|
|
using System.Collections.Generic;
|
2020-05-08 13:11:43 -07:00
|
|
|
using System.Globalization;
|
2019-01-26 13:47:11 -07:00
|
|
|
using System.IO;
|
2019-12-13 12:11:37 -07:00
|
|
|
using System.Net.Http;
|
2016-09-25 11:39:13 -07:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-11-08 04:49:00 -07:00
|
|
|
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;
|
2019-01-13 12:22:00 -07:00
|
|
|
using MediaBrowser.Model.IO;
|
2017-11-17 14:54:33 -07:00
|
|
|
using MediaBrowser.Model.LiveTv;
|
2019-01-13 12:22:00 -07:00
|
|
|
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 IHttpClient _httpClient;
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
|
|
|
|
2019-07-07 07:39:35 -07:00
|
|
|
public SharedHttpStream(
|
|
|
|
MediaSourceInfo mediaSource,
|
|
|
|
TunerHostInfo tunerHostInfo,
|
|
|
|
string originalStreamId,
|
|
|
|
IFileSystem fileSystem,
|
|
|
|
IHttpClient httpClient,
|
|
|
|
ILogger logger,
|
2019-11-08 04:49:00 -07:00
|
|
|
IConfigurationManager configurationManager,
|
2019-07-07 07:39:35 -07:00
|
|
|
IServerApplicationHost appHost,
|
|
|
|
IStreamHelper streamHelper)
|
2019-11-08 04:49:00 -07:00
|
|
|
: base(mediaSource, tunerHostInfo, fileSystem, logger, configurationManager, streamHelper)
|
2016-09-25 11:39:13 -07:00
|
|
|
{
|
|
|
|
_httpClient = httpClient;
|
|
|
|
_appHost = appHost;
|
2016-10-07 08:08:13 -07:00
|
|
|
OriginalStreamId = originalStreamId;
|
2017-11-14 00:41:21 -07:00
|
|
|
EnableStreamSharing = true;
|
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;
|
|
|
|
|
2019-01-26 14:08:04 -07:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(TempFilePath));
|
2017-10-23 12:14:11 -07:00
|
|
|
|
2017-11-14 00:41:21 -07:00
|
|
|
var typeName = GetType().Name;
|
2018-12-13 06:18:25 -07:00
|
|
|
Logger.LogInformation("Opening " + typeName + " Live stream from {0}", url);
|
2016-09-25 11:39:13 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
var httpRequestOptions = new HttpRequestOptions
|
2017-10-23 12:14:11 -07:00
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
CancellationToken = CancellationToken.None,
|
|
|
|
BufferContent = false,
|
2020-04-02 08:00:28 -07:00
|
|
|
DecompressionMethod = CompressionMethods.None
|
2018-09-12 10:26:21 -07:00
|
|
|
};
|
2017-10-23 12:14:11 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
foreach (var header in mediaSource.RequiredHttpHeaders)
|
|
|
|
{
|
|
|
|
httpRequestOptions.RequestHeaders[header.Key] = header.Value;
|
|
|
|
}
|
|
|
|
|
2019-12-13 12:11:37 -07:00
|
|
|
var response = await _httpClient.SendAsync(httpRequestOptions, HttpMethod.Get).ConfigureAwait(false);
|
2017-10-23 12:14:11 -07:00
|
|
|
|
2017-11-14 00:41:21 -07:00
|
|
|
var extension = "ts";
|
|
|
|
var requiresRemux = false;
|
|
|
|
|
|
|
|
var contentType = response.ContentType ?? string.Empty;
|
2017-11-18 21:59:34 -07:00
|
|
|
if (contentType.IndexOf("matroska", StringComparison.OrdinalIgnoreCase) != -1)
|
|
|
|
{
|
2017-11-19 17:20:12 -07:00
|
|
|
requiresRemux = true;
|
2017-11-18 21:59:34 -07:00
|
|
|
}
|
|
|
|
else if (contentType.IndexOf("mp4", StringComparison.OrdinalIgnoreCase) != -1 ||
|
|
|
|
contentType.IndexOf("dash", StringComparison.OrdinalIgnoreCase) != -1 ||
|
2017-11-29 13:50:18 -07:00
|
|
|
contentType.IndexOf("mpegURL", StringComparison.OrdinalIgnoreCase) != -1 ||
|
|
|
|
contentType.IndexOf("text/", StringComparison.OrdinalIgnoreCase) != -1)
|
2017-11-14 00:41:21 -07:00
|
|
|
{
|
|
|
|
requiresRemux = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the stream without any sharing features
|
|
|
|
if (requiresRemux)
|
|
|
|
{
|
|
|
|
using (response)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetTempFilePath(extension);
|
2017-10-23 12:14:11 -07:00
|
|
|
|
2017-11-06 14:32:44 -07:00
|
|
|
var taskCompletionSource = new TaskCompletionSource<bool>();
|
2017-11-21 15:14:56 -07:00
|
|
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
2019-02-13 08:44:58 -07:00
|
|
|
_ = StartStreaming(response, taskCompletionSource, LiveStreamCancellationTokenSource.Token);
|
2016-09-25 11:39:13 -07:00
|
|
|
|
2016-09-29 23:50:06 -07:00
|
|
|
//OpenedMediaSource.Protocol = MediaProtocol.File;
|
|
|
|
//OpenedMediaSource.Path = tempFile;
|
|
|
|
//OpenedMediaSource.ReadAtNativeFramerate = true;
|
2016-09-25 11:39:13 -07:00
|
|
|
|
2020-04-07 08:41:15 -07:00
|
|
|
MediaSource.Path = _appHost.GetLocalApiUrl("127.0.0.1", true) + "/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
|
|
|
|
2017-11-19 17:20:12 -07:00
|
|
|
//OpenedMediaSource.Path = TempFilePath;
|
|
|
|
//OpenedMediaSource.Protocol = MediaProtocol.File;
|
|
|
|
|
2017-09-09 20:47:33 -07:00
|
|
|
//OpenedMediaSource.Path = _tempFilePath;
|
|
|
|
//OpenedMediaSource.Protocol = MediaProtocol.File;
|
2017-05-19 09:39:40 -07:00
|
|
|
//OpenedMediaSource.SupportsDirectPlay = false;
|
|
|
|
//OpenedMediaSource.SupportsDirectStream = true;
|
|
|
|
//OpenedMediaSource.SupportsTranscoding = true;
|
2017-11-06 14:32:44 -07:00
|
|
|
await taskCompletionSource.Task.ConfigureAwait(false);
|
2020-05-08 04:32:41 -07:00
|
|
|
if (taskCompletionSource.Task.Exception != null)
|
|
|
|
{
|
|
|
|
// Error happened during opening the stream, re-raise the exception to inform the caller
|
|
|
|
throw taskCompletionSource.Task.Exception;
|
|
|
|
}
|
2020-05-08 13:11:43 -07:00
|
|
|
if (!taskCompletionSource.Task.Result)
|
|
|
|
{
|
|
|
|
Logger.LogWarning("Zero bytes copied from stream {0} to {1} but no exception raised", GetType().Name, TempFilePath);
|
|
|
|
throw new EndOfStreamException(String.Format(CultureInfo.InvariantCulture,
|
|
|
|
"Zero bytes copied from stream {0}",
|
|
|
|
GetType().Name));
|
|
|
|
}
|
2016-09-25 11:39:13 -07:00
|
|
|
}
|
|
|
|
|
2017-11-06 14:32:44 -07:00
|
|
|
private Task StartStreaming(HttpResponseInfo response, TaskCompletionSource<bool> openTaskCompletionSource, CancellationToken cancellationToken)
|
2016-09-25 11:39:13 -07:00
|
|
|
{
|
2017-05-21 21:54:02 -07:00
|
|
|
return Task.Run(async () =>
|
2016-09-25 11:39:13 -07:00
|
|
|
{
|
2017-10-10 23:24:22 -07:00
|
|
|
try
|
2016-10-07 08:08:13 -07:00
|
|
|
{
|
2018-12-13 06:18:25 -07:00
|
|
|
Logger.LogInformation("Beginning {0} stream to {1}", GetType().Name, TempFilePath);
|
2017-10-23 12:14:11 -07:00
|
|
|
using (response)
|
2018-12-13 06:18:25 -07:00
|
|
|
using (var stream = response.Content)
|
2020-01-08 09:52:50 -07:00
|
|
|
using (var fileStream = new FileStream(TempFilePath, FileMode.Create, FileAccess.Write, FileShare.Read))
|
2016-10-05 00:15:29 -07:00
|
|
|
{
|
2019-07-07 07:39:35 -07:00
|
|
|
await StreamHelper.CopyToAsync(
|
|
|
|
stream,
|
|
|
|
fileStream,
|
2020-01-08 09:52:50 -07:00
|
|
|
IODefaults.CopyToBufferSize,
|
2019-07-07 07:39:35 -07:00
|
|
|
() => Resolve(openTaskCompletionSource),
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
2016-10-05 00:15:29 -07:00
|
|
|
}
|
2017-10-10 23:24:22 -07:00
|
|
|
}
|
2020-05-08 04:32:41 -07:00
|
|
|
catch (OperationCanceledException ex)
|
2017-10-10 23:24:22 -07:00
|
|
|
{
|
2020-05-08 13:11:43 -07:00
|
|
|
Logger.LogInformation("Copying of {0} to {1} was canceled", GetType().Name, TempFilePath);
|
2020-05-08 04:32:41 -07:00
|
|
|
openTaskCompletionSource.TrySetException(ex);
|
2017-10-10 23:24:22 -07:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2020-05-08 04:32:41 -07:00
|
|
|
Logger.LogError(ex, "Error copying live stream {0} to {1}.", GetType().Name, TempFilePath);
|
|
|
|
openTaskCompletionSource.TrySetException(ex);
|
2016-10-05 00:15:29 -07:00
|
|
|
}
|
2020-05-08 13:11:43 -07:00
|
|
|
openTaskCompletionSource.TrySetResult(false);
|
2019-07-07 07:39:35 -07:00
|
|
|
|
2017-11-05 14:51:23 -07:00
|
|
|
EnableStreamSharing = false;
|
2018-09-12 10:26:21 -07:00
|
|
|
await DeleteTempFiles(new List<string> { TempFilePath }).ConfigureAwait(false);
|
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
|
|
|
}
|
|
|
|
}
|