2016-09-25 11:39:13 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-05-24 21:25:51 -07:00
|
|
|
|
using Emby.Server.Implementations.IO;
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2016-09-25 11:39:13 -07:00
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Controller;
|
|
|
|
|
using MediaBrowser.Controller.LiveTv;
|
2016-10-05 00:15:29 -07:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2016-09-25 11:39:13 -07:00
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2017-05-21 21:54:02 -07:00
|
|
|
|
using MediaBrowser.Model.System;
|
2017-05-31 21:51:43 -07:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
using MediaBrowser.Controller.IO;
|
2016-09-25 11:39:13 -07:00
|
|
|
|
|
2016-11-03 16:35:19 -07:00
|
|
|
|
namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
|
2016-09-25 11:39:13 -07:00
|
|
|
|
{
|
2017-03-02 22:53:21 -07:00
|
|
|
|
public class HdHomerunHttpStream : LiveStream, IDirectStreamProvider
|
2016-09-25 11:39:13 -07:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
|
|
|
|
|
|
|
|
|
private readonly CancellationTokenSource _liveStreamCancellationTokenSource = new CancellationTokenSource();
|
2016-09-27 10:51:01 -07:00
|
|
|
|
private readonly TaskCompletionSource<bool> _liveStreamTaskCompletionSource = new TaskCompletionSource<bool>();
|
2016-09-25 11:39:13 -07:00
|
|
|
|
|
2017-05-31 23:25:07 -07:00
|
|
|
|
private readonly MulticastStream _multicastStream;
|
|
|
|
|
|
2017-05-21 21:54:02 -07:00
|
|
|
|
private readonly string _tempFilePath;
|
2017-05-31 23:25:07 -07:00
|
|
|
|
private bool _enableFileBuffer = false;
|
2017-05-21 21:54:02 -07:00
|
|
|
|
|
|
|
|
|
public HdHomerunHttpStream(MediaSourceInfo mediaSource, string originalStreamId, IFileSystem fileSystem, IHttpClient httpClient, ILogger logger, IServerApplicationPaths appPaths, IServerApplicationHost appHost, IEnvironmentInfo environment)
|
|
|
|
|
: base(mediaSource, environment, fileSystem)
|
2016-09-25 11:39:13 -07:00
|
|
|
|
{
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_appHost = appHost;
|
2016-10-07 08:08:13 -07:00
|
|
|
|
OriginalStreamId = originalStreamId;
|
2017-05-21 21:54:02 -07:00
|
|
|
|
|
2017-05-31 23:25:07 -07:00
|
|
|
|
_multicastStream = new MulticastStream(_logger);
|
2017-05-21 21:54:02 -07:00
|
|
|
|
_tempFilePath = Path.Combine(appPaths.TranscodingTempPath, UniqueId + ".ts");
|
2016-09-25 11:39:13 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-29 05:55:49 -07:00
|
|
|
|
protected override async Task OpenInternal(CancellationToken openCancellationToken)
|
2016-09-25 11:39:13 -07:00
|
|
|
|
{
|
|
|
|
|
_liveStreamCancellationTokenSource.Token.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
var mediaSource = OriginalMediaSource;
|
|
|
|
|
|
|
|
|
|
var url = mediaSource.Path;
|
|
|
|
|
|
2016-10-07 08:08:13 -07:00
|
|
|
|
_logger.Info("Opening HDHR Live stream from {0}", url);
|
2016-09-25 11:39:13 -07:00
|
|
|
|
|
|
|
|
|
var taskCompletionSource = new TaskCompletionSource<bool>();
|
|
|
|
|
|
2016-10-07 08:08:13 -07:00
|
|
|
|
StartStreaming(url, 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
|
|
|
|
|
2016-10-19 13:31:46 -07:00
|
|
|
|
OpenedMediaSource.Path = _appHost.GetLocalApiUrl("127.0.0.1") + "/LiveTv/LiveStreamFiles/" + UniqueId + "/stream.ts";
|
2016-09-29 05:55:49 -07:00
|
|
|
|
OpenedMediaSource.Protocol = MediaProtocol.Http;
|
2017-05-19 09:39:40 -07:00
|
|
|
|
//OpenedMediaSource.SupportsDirectPlay = false;
|
|
|
|
|
//OpenedMediaSource.SupportsDirectStream = true;
|
|
|
|
|
//OpenedMediaSource.SupportsTranscoding = true;
|
2016-09-29 23:50:06 -07:00
|
|
|
|
|
|
|
|
|
await taskCompletionSource.Task.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
//await Task.Delay(5000).ConfigureAwait(false);
|
2016-09-25 11:39:13 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Task Close()
|
|
|
|
|
{
|
2016-09-29 05:55:49 -07:00
|
|
|
|
_logger.Info("Closing HDHR live stream");
|
2016-09-25 11:39:13 -07:00
|
|
|
|
_liveStreamCancellationTokenSource.Cancel();
|
|
|
|
|
|
2016-09-27 10:51:01 -07:00
|
|
|
|
return _liveStreamTaskCompletionSource.Task;
|
2016-09-25 11:39:13 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-21 21:54:02 -07:00
|
|
|
|
private Task StartStreaming(string url, 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
|
|
|
|
{
|
2016-10-07 08:08:13 -07:00
|
|
|
|
var isFirstAttempt = true;
|
2016-09-25 11:39:13 -07:00
|
|
|
|
|
2016-10-07 08:08:13 -07:00
|
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
try
|
2016-09-25 11:39:13 -07:00
|
|
|
|
{
|
2016-10-07 08:08:13 -07:00
|
|
|
|
using (var response = await _httpClient.SendAsync(new HttpRequestOptions
|
2016-09-25 11:39:13 -07:00
|
|
|
|
{
|
2016-10-07 08:08:13 -07:00
|
|
|
|
Url = url,
|
|
|
|
|
CancellationToken = cancellationToken,
|
2016-11-20 16:48:52 -07:00
|
|
|
|
BufferContent = false,
|
|
|
|
|
|
|
|
|
|
// Increase a little bit
|
|
|
|
|
TimeoutMs = 30000
|
2016-10-07 08:08:13 -07:00
|
|
|
|
|
|
|
|
|
}, "GET").ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Opened HDHR stream from {0}", url);
|
2016-09-25 11:39:13 -07:00
|
|
|
|
|
2016-10-07 08:08:13 -07:00
|
|
|
|
if (!cancellationToken.IsCancellationRequested)
|
2016-09-25 11:39:13 -07:00
|
|
|
|
{
|
2016-10-07 08:08:13 -07:00
|
|
|
|
_logger.Info("Beginning multicastStream.CopyUntilCancelled");
|
2016-09-25 11:39:13 -07:00
|
|
|
|
|
2017-05-31 23:25:07 -07:00
|
|
|
|
if (_enableFileBuffer)
|
2016-09-25 11:39:13 -07:00
|
|
|
|
{
|
2017-05-31 23:25:07 -07:00
|
|
|
|
FileSystem.CreateDirectory(FileSystem.GetDirectoryName(_tempFilePath));
|
|
|
|
|
using (var fileStream = FileSystem.GetFileStream(_tempFilePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, FileOpenOptions.None))
|
|
|
|
|
{
|
|
|
|
|
StreamHelper.CopyTo(response.Content, fileStream, 81920, () => Resolve(openTaskCompletionSource), cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-06-02 22:43:33 -07:00
|
|
|
|
Resolve(openTaskCompletionSource);
|
|
|
|
|
|
|
|
|
|
await _multicastStream.CopyUntilCancelled(response.Content, null, cancellationToken).ConfigureAwait(false);
|
2017-05-21 21:54:02 -07:00
|
|
|
|
}
|
2016-10-07 08:08:13 -07:00
|
|
|
|
}
|
2016-09-25 11:39:13 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-07 08:08:13 -07:00
|
|
|
|
catch (OperationCanceledException)
|
2016-10-05 00:15:29 -07:00
|
|
|
|
{
|
2016-10-07 08:08:13 -07:00
|
|
|
|
break;
|
2016-10-05 00:15:29 -07:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2016-10-07 08:08:13 -07:00
|
|
|
|
if (isFirstAttempt)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error opening live stream:", ex);
|
|
|
|
|
openTaskCompletionSource.TrySetException(ex);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-10-06 11:55:01 -07:00
|
|
|
|
|
2016-10-07 08:08:13 -07:00
|
|
|
|
_logger.ErrorException("Error copying live stream, will reopen", ex);
|
2016-10-05 00:15:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-07 08:08:13 -07:00
|
|
|
|
isFirstAttempt = false;
|
2016-10-05 00:15:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-07 08:08:13 -07:00
|
|
|
|
_liveStreamTaskCompletionSource.TrySetResult(true);
|
2017-05-31 23:25:07 -07:00
|
|
|
|
//await DeleteTempFile(_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
|
|
|
|
{
|
2017-05-31 22:05:36 -07:00
|
|
|
|
Task.Run(() =>
|
2017-05-21 21:54:02 -07:00
|
|
|
|
{
|
|
|
|
|
openTaskCompletionSource.TrySetResult(true);
|
|
|
|
|
});
|
2016-10-02 23:28:45 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-07 08:08:13 -07:00
|
|
|
|
public Task CopyToAsync(Stream stream, CancellationToken cancellationToken)
|
2016-09-25 11:39:13 -07:00
|
|
|
|
{
|
2017-05-31 23:25:07 -07:00
|
|
|
|
return _multicastStream.CopyToAsync(stream, cancellationToken);
|
2017-05-25 06:00:14 -07:00
|
|
|
|
}
|
2016-09-25 11:39:13 -07:00
|
|
|
|
}
|
|
|
|
|
}
|