2016-09-25 20:39:13 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
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;
|
|
|
|
|
|
2016-11-04 00:35:19 +01:00
|
|
|
|
namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
|
2016-09-25 20:39:13 +02:00
|
|
|
|
{
|
2016-10-05 09:15:29 +02:00
|
|
|
|
public class HdHomerunLiveStream : LiveStream, IDirectStreamProvider
|
2016-09-25 20:39:13 +02:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
private readonly IServerApplicationPaths _appPaths;
|
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
|
|
|
|
|
|
|
|
|
private readonly CancellationTokenSource _liveStreamCancellationTokenSource = new CancellationTokenSource();
|
2016-09-27 19:51:01 +02:00
|
|
|
|
private readonly TaskCompletionSource<bool> _liveStreamTaskCompletionSource = new TaskCompletionSource<bool>();
|
2016-10-07 17:08:13 +02:00
|
|
|
|
private readonly MulticastStream _multicastStream;
|
2016-09-25 20:39:13 +02:00
|
|
|
|
|
2016-10-07 17:08:13 +02:00
|
|
|
|
public HdHomerunLiveStream(MediaSourceInfo mediaSource, string originalStreamId, IFileSystem fileSystem, IHttpClient httpClient, ILogger logger, IServerApplicationPaths appPaths, IServerApplicationHost appHost)
|
2016-09-25 20:39:13 +02:00
|
|
|
|
: base(mediaSource)
|
|
|
|
|
{
|
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_appPaths = appPaths;
|
|
|
|
|
_appHost = appHost;
|
2016-10-07 17:08:13 +02:00
|
|
|
|
OriginalStreamId = originalStreamId;
|
|
|
|
|
_multicastStream = new MulticastStream(_logger);
|
2016-09-25 20:39:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-29 14:55:49 +02:00
|
|
|
|
protected override async Task OpenInternal(CancellationToken openCancellationToken)
|
2016-09-25 20:39:13 +02:00
|
|
|
|
{
|
|
|
|
|
_liveStreamCancellationTokenSource.Token.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
var mediaSource = OriginalMediaSource;
|
|
|
|
|
|
|
|
|
|
var url = mediaSource.Path;
|
|
|
|
|
|
2016-10-07 17:08:13 +02:00
|
|
|
|
_logger.Info("Opening HDHR Live stream from {0}", url);
|
2016-09-25 20:39:13 +02:00
|
|
|
|
|
|
|
|
|
var taskCompletionSource = new TaskCompletionSource<bool>();
|
|
|
|
|
|
2016-10-07 17:08:13 +02:00
|
|
|
|
StartStreaming(url, 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;
|
2016-10-01 22:29:24 +02:00
|
|
|
|
OpenedMediaSource.SupportsDirectPlay = false;
|
|
|
|
|
OpenedMediaSource.SupportsDirectStream = true;
|
|
|
|
|
OpenedMediaSource.SupportsTranscoding = true;
|
2016-09-30 08:50:06 +02:00
|
|
|
|
|
|
|
|
|
await taskCompletionSource.Task.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
//await Task.Delay(5000).ConfigureAwait(false);
|
2016-09-25 20:39:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Task Close()
|
|
|
|
|
{
|
2016-09-29 14:55:49 +02:00
|
|
|
|
_logger.Info("Closing HDHR live stream");
|
2016-09-25 20:39:13 +02:00
|
|
|
|
_liveStreamCancellationTokenSource.Cancel();
|
|
|
|
|
|
2016-09-27 19:51:01 +02:00
|
|
|
|
return _liveStreamTaskCompletionSource.Task;
|
2016-09-25 20:39:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-07 17:08:13 +02:00
|
|
|
|
private async Task StartStreaming(string url, TaskCompletionSource<bool> openTaskCompletionSource, CancellationToken cancellationToken)
|
2016-09-25 20:39:13 +02:00
|
|
|
|
{
|
|
|
|
|
await Task.Run(async () =>
|
|
|
|
|
{
|
2016-10-07 17:08:13 +02:00
|
|
|
|
var isFirstAttempt = true;
|
2016-09-25 20:39:13 +02:00
|
|
|
|
|
2016-10-07 17:08:13 +02:00
|
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
try
|
2016-09-25 20:39:13 +02:00
|
|
|
|
{
|
2016-10-07 17:08:13 +02:00
|
|
|
|
using (var response = await _httpClient.SendAsync(new HttpRequestOptions
|
2016-09-25 20:39:13 +02:00
|
|
|
|
{
|
2016-10-07 17:08:13 +02:00
|
|
|
|
Url = url,
|
|
|
|
|
CancellationToken = cancellationToken,
|
2016-11-21 00:48:52 +01:00
|
|
|
|
BufferContent = false,
|
|
|
|
|
|
|
|
|
|
// Increase a little bit
|
|
|
|
|
TimeoutMs = 30000
|
2016-10-07 17:08:13 +02:00
|
|
|
|
|
|
|
|
|
}, "GET").ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Opened HDHR stream from {0}", url);
|
2016-09-25 20:39:13 +02:00
|
|
|
|
|
2016-10-07 17:08:13 +02:00
|
|
|
|
if (!cancellationToken.IsCancellationRequested)
|
2016-09-25 20:39:13 +02:00
|
|
|
|
{
|
2016-10-07 17:08:13 +02:00
|
|
|
|
_logger.Info("Beginning multicastStream.CopyUntilCancelled");
|
2016-09-25 20:39:13 +02:00
|
|
|
|
|
2016-10-07 17:08:13 +02:00
|
|
|
|
Action onStarted = null;
|
|
|
|
|
if (isFirstAttempt)
|
2016-09-25 20:39:13 +02:00
|
|
|
|
{
|
2016-10-07 17:08:13 +02:00
|
|
|
|
onStarted = () => openTaskCompletionSource.TrySetResult(true);
|
2016-09-25 20:39:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-07 17:08:13 +02:00
|
|
|
|
await _multicastStream.CopyUntilCancelled(response.Content, onStarted, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
2016-09-25 20:39:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-07 17:08:13 +02:00
|
|
|
|
catch (OperationCanceledException)
|
2016-10-05 09:15:29 +02:00
|
|
|
|
{
|
2016-10-07 17:08:13 +02:00
|
|
|
|
break;
|
2016-10-05 09:15:29 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2016-10-07 17:08:13 +02:00
|
|
|
|
if (isFirstAttempt)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error opening live stream:", ex);
|
|
|
|
|
openTaskCompletionSource.TrySetException(ex);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-10-06 20:55:01 +02:00
|
|
|
|
|
2016-10-07 17:08:13 +02:00
|
|
|
|
_logger.ErrorException("Error copying live stream, will reopen", ex);
|
2016-10-05 09:15:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-07 17:08:13 +02:00
|
|
|
|
isFirstAttempt = false;
|
2016-10-05 09:15:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-07 17:08:13 +02:00
|
|
|
|
_liveStreamTaskCompletionSource.TrySetResult(true);
|
2016-10-03 08:28:45 +02:00
|
|
|
|
|
2016-10-07 17:08:13 +02:00
|
|
|
|
}).ConfigureAwait(false);
|
2016-10-03 08:28:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-07 17:08:13 +02:00
|
|
|
|
public Task CopyToAsync(Stream stream, CancellationToken cancellationToken)
|
2016-09-25 20:39:13 +02:00
|
|
|
|
{
|
2016-10-07 17:08:13 +02:00
|
|
|
|
return _multicastStream.CopyToAsync(stream);
|
2016-09-25 20:39:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|