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

170 lines
6.2 KiB
C#
Raw Normal View History

2016-09-25 20:39:13 +02:00
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
2017-05-25 06:25:51 +02:00
using Emby.Server.Implementations.IO;
2016-10-25 21:02:04 +02:00
using MediaBrowser.Model.IO;
2016-09-25 20:39:13 +02:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.LiveTv;
2016-10-05 09:15:29 +02:00
using MediaBrowser.Controller.Library;
2016-09-25 20:39:13 +02:00
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.MediaInfo;
2017-05-22 06:54:02 +02:00
using MediaBrowser.Model.System;
2017-06-01 06:51:43 +02:00
using System.Globalization;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.LiveTv;
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
{
private readonly IHttpClient _httpClient;
private readonly IServerApplicationHost _appHost;
public SharedHttpStream(MediaSourceInfo mediaSource, TunerHostInfo tunerHostInfo, string originalStreamId, IFileSystem fileSystem, IHttpClient httpClient, ILogger logger, IServerApplicationPaths appPaths, IServerApplicationHost appHost, IEnvironmentInfo environment)
: base(mediaSource, tunerHostInfo, environment, fileSystem, logger, appPaths)
2016-09-25 20:39:13 +02:00
{
_httpClient = httpClient;
_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;
2017-10-23 21:14:11 +02:00
FileSystem.CreateDirectory(FileSystem.GetDirectoryName(TempFilePath));
2017-11-14 08:41:21 +01:00
var typeName = GetType().Name;
Logger.Info("Opening " + typeName + " Live stream from {0}", url);
2016-09-25 20:39:13 +02:00
2017-10-23 21:14:11 +02:00
var response = await _httpClient.SendAsync(new HttpRequestOptions
{
Url = url,
CancellationToken = CancellationToken.None,
BufferContent = false,
// Increase a little bit
TimeoutMs = 30000,
2016-09-25 20:39:13 +02:00
2017-11-14 08:41:21 +01:00
EnableHttpCompression = false,
LogResponse = true,
LogResponseHeaders = true
2017-10-23 21:14:11 +02:00
}, "GET").ConfigureAwait(false);
2017-11-14 08:41:21 +01:00
var extension = "ts";
var requiresRemux = false;
var contentType = response.ContentType ?? 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 ||
contentType.IndexOf("mpegURL", 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
var now = DateTime.UtcNow;
2017-11-06 22:32:44 +01:00
StartStreaming(response, taskCompletionSource, LiveStreamCancellationTokenSource.Token);
2016-09-25 20:39:13 +02:00
2016-09-30 08:50:06 +02:00
//OpenedMediaSource.Protocol = MediaProtocol.File;
//OpenedMediaSource.Path = tempFile;
//OpenedMediaSource.ReadAtNativeFramerate = true;
2016-09-25 20:39:13 +02:00
2016-10-19 22:31:46 +02:00
OpenedMediaSource.Path = _appHost.GetLocalApiUrl("127.0.0.1") + "/LiveTv/LiveStreamFiles/" + UniqueId + "/stream.ts";
2016-09-29 14:55:49 +02:00
OpenedMediaSource.Protocol = MediaProtocol.Http;
2017-09-10 05:47:33 +02:00
2017-11-20 01:20:12 +01:00
//OpenedMediaSource.Path = TempFilePath;
//OpenedMediaSource.Protocol = MediaProtocol.File;
2017-09-10 05:47:33 +02:00
//OpenedMediaSource.Path = _tempFilePath;
//OpenedMediaSource.Protocol = MediaProtocol.File;
2017-05-19 18:39:40 +02:00
//OpenedMediaSource.SupportsDirectPlay = false;
//OpenedMediaSource.SupportsDirectStream = true;
//OpenedMediaSource.SupportsTranscoding = true;
2017-11-06 22:32:44 +01:00
await taskCompletionSource.Task.ConfigureAwait(false);
2017-11-21 23:14:56 +01:00
if (OpenedMediaSource.SupportsProbing)
{
var elapsed = (DateTime.UtcNow - now).TotalMilliseconds;
var delay = Convert.ToInt32(3000 - elapsed);
if (delay > 0)
{
Logger.Info("Delaying shared stream by {0}ms to allow the buffer to build.", delay);
await Task.Delay(delay).ConfigureAwait(false);
}
}
2016-09-25 20:39:13 +02:00
}
protected override void CloseInternal()
2016-09-25 20:39:13 +02:00
{
2017-10-14 08:52:56 +02:00
LiveStreamCancellationTokenSource.Cancel();
2016-09-25 20:39:13 +02:00
}
2017-11-06 22:32:44 +01:00
private Task StartStreaming(HttpResponseInfo response, TaskCompletionSource<bool> openTaskCompletionSource, CancellationToken cancellationToken)
2016-09-25 20:39:13 +02:00
{
2017-05-22 06:54:02 +02:00
return Task.Run(async () =>
2016-09-25 20:39:13 +02:00
{
try
2016-10-07 17:08:13 +02:00
{
2017-10-23 21:14:11 +02:00
using (response)
2016-10-05 09:15:29 +02:00
{
2017-10-20 18:16:56 +02:00
using (var stream = response.Content)
{
2017-11-14 08:41:21 +01:00
Logger.Info("Beginning {0} stream to {1}", GetType().Name, TempFilePath);
2017-10-20 18:16:56 +02:00
using (var fileStream = FileSystem.GetFileStream(TempFilePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, FileOpenOptions.None))
{
2017-11-06 22:32:44 +01:00
StreamHelper.CopyTo(stream, fileStream, 81920, () => Resolve(openTaskCompletionSource), cancellationToken);
2017-10-20 18:16:56 +02:00
}
2016-10-07 17:08:13 +02:00
}
2016-10-05 09:15:29 +02:00
}
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
Logger.ErrorException("Error copying live stream.", ex);
2016-10-05 09:15:29 +02:00
}
EnableStreamSharing = false;
2017-10-23 21:14:11 +02:00
await DeleteTempFile(TempFilePath).ConfigureAwait(false);
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
{
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
}
}