jellyfin/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHttpStream.cs

120 lines
4.5 KiB
C#
Raw Normal View History

2016-09-25 18:39:13 +00:00
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
2017-05-25 04:25:51 +00:00
using Emby.Server.Implementations.IO;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.IO;
2016-09-25 18:39:13 +00:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.LiveTv;
2016-10-05 07:15:29 +00:00
using MediaBrowser.Controller.Library;
2016-09-25 18:39:13 +00:00
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.MediaInfo;
2017-05-22 04:54:02 +00:00
using MediaBrowser.Model.System;
2017-06-01 04:51:43 +00:00
using System.Globalization;
using MediaBrowser.Controller.IO;
2016-09-25 18:39:13 +00:00
2016-11-03 23:35:19 +00:00
namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
2016-09-25 18:39:13 +00:00
{
2017-03-03 05:53:21 +00:00
public class HdHomerunHttpStream : LiveStream, IDirectStreamProvider
2016-09-25 18:39:13 +00:00
{
private readonly IHttpClient _httpClient;
private readonly IServerApplicationHost _appHost;
2017-05-22 04:54:02 +00:00
public HdHomerunHttpStream(MediaSourceInfo mediaSource, string originalStreamId, IFileSystem fileSystem, IHttpClient httpClient, ILogger logger, IServerApplicationPaths appPaths, IServerApplicationHost appHost, IEnvironmentInfo environment)
2017-09-28 17:02:49 +00:00
: base(mediaSource, environment, fileSystem, logger, appPaths)
2016-09-25 18:39:13 +00:00
{
_httpClient = httpClient;
_appHost = appHost;
2016-10-07 15:08:13 +00:00
OriginalStreamId = originalStreamId;
2016-09-25 18:39:13 +00:00
}
2017-10-23 19:14:11 +00:00
public override async Task Open(CancellationToken openCancellationToken)
2016-09-25 18:39:13 +00:00
{
2017-10-14 06:52:56 +00:00
LiveStreamCancellationTokenSource.Token.ThrowIfCancellationRequested();
2016-09-25 18:39:13 +00:00
var mediaSource = OriginalMediaSource;
var url = mediaSource.Path;
2017-10-23 19:14:11 +00:00
FileSystem.CreateDirectory(FileSystem.GetDirectoryName(TempFilePath));
2017-09-28 17:02:49 +00:00
Logger.Info("Opening HDHR Live stream from {0}", url);
2016-09-25 18:39:13 +00:00
2017-10-23 19:14:11 +00:00
var response = await _httpClient.SendAsync(new HttpRequestOptions
{
Url = url,
CancellationToken = CancellationToken.None,
BufferContent = false,
// Increase a little bit
TimeoutMs = 30000,
2016-09-25 18:39:13 +00:00
2017-10-23 19:14:11 +00:00
EnableHttpCompression = false
}, "GET").ConfigureAwait(false);
Logger.Info("Opened HDHR stream from {0}", url);
2017-11-06 21:32:44 +00:00
var taskCompletionSource = new TaskCompletionSource<bool>();
StartStreaming(response, taskCompletionSource, LiveStreamCancellationTokenSource.Token);
2016-09-25 18:39:13 +00:00
2016-09-30 06:50:06 +00:00
//OpenedMediaSource.Protocol = MediaProtocol.File;
//OpenedMediaSource.Path = tempFile;
//OpenedMediaSource.ReadAtNativeFramerate = true;
2016-09-25 18:39:13 +00:00
2016-10-19 20:31:46 +00:00
OpenedMediaSource.Path = _appHost.GetLocalApiUrl("127.0.0.1") + "/LiveTv/LiveStreamFiles/" + UniqueId + "/stream.ts";
2016-09-29 12:55:49 +00:00
OpenedMediaSource.Protocol = MediaProtocol.Http;
2017-09-10 03:47:33 +00:00
//OpenedMediaSource.Path = _tempFilePath;
//OpenedMediaSource.Protocol = MediaProtocol.File;
2017-05-19 16:39:40 +00:00
//OpenedMediaSource.SupportsDirectPlay = false;
//OpenedMediaSource.SupportsDirectStream = true;
//OpenedMediaSource.SupportsTranscoding = true;
2017-11-06 21:32:44 +00:00
await taskCompletionSource.Task.ConfigureAwait(false);
2016-09-25 18:39:13 +00:00
}
protected override void CloseInternal()
2016-09-25 18:39:13 +00:00
{
2017-10-14 06:52:56 +00:00
LiveStreamCancellationTokenSource.Cancel();
2016-09-25 18:39:13 +00:00
}
2017-11-06 21:32:44 +00:00
private Task StartStreaming(HttpResponseInfo response, TaskCompletionSource<bool> openTaskCompletionSource, CancellationToken cancellationToken)
2016-09-25 18:39:13 +00:00
{
2017-05-22 04:54:02 +00:00
return Task.Run(async () =>
2016-09-25 18:39:13 +00:00
{
try
2016-10-07 15:08:13 +00:00
{
2017-10-23 19:14:11 +00:00
using (response)
2016-10-05 07:15:29 +00:00
{
2017-10-20 16:16:56 +00:00
using (var stream = response.Content)
{
2017-10-23 19:14:11 +00:00
Logger.Info("Beginning HdHomerunHttpStream stream to file");
2017-10-20 16:16:56 +00:00
using (var fileStream = FileSystem.GetFileStream(TempFilePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, FileOpenOptions.None))
{
2017-11-06 21:32:44 +00:00
StreamHelper.CopyTo(stream, fileStream, 81920, () => Resolve(openTaskCompletionSource), cancellationToken);
2017-10-20 16:16:56 +00:00
}
2016-10-07 15:08:13 +00:00
}
2016-10-05 07:15:29 +00:00
}
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
Logger.ErrorException("Error copying live stream.", ex);
2016-10-05 07:15:29 +00:00
}
EnableStreamSharing = false;
2017-10-23 19:14:11 +00:00
await DeleteTempFile(TempFilePath).ConfigureAwait(false);
2017-05-22 04:54:02 +00:00
});
}
2016-10-03 06:28:45 +00:00
2017-06-01 05:05:36 +00:00
private void Resolve(TaskCompletionSource<bool> openTaskCompletionSource)
2017-05-22 04:54:02 +00:00
{
2017-11-06 21:32:44 +00:00
openTaskCompletionSource.TrySetResult(true);
2016-10-03 06:28:45 +00:00
}
2016-09-25 18:39:13 +00:00
}
}