2019-01-13 19:54:44 +00:00
|
|
|
using System;
|
2019-01-13 19:22:00 +00:00
|
|
|
using System.Collections.Generic;
|
2019-01-26 20:47:11 +00:00
|
|
|
using System.IO;
|
2016-09-25 18:39:13 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using MediaBrowser.Controller;
|
2016-10-05 07:15:29 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
2016-09-25 18:39:13 +00:00
|
|
|
using MediaBrowser.Model.Dto;
|
2019-01-13 19:22:00 +00:00
|
|
|
using MediaBrowser.Model.IO;
|
2017-11-17 21:54:33 +00:00
|
|
|
using MediaBrowser.Model.LiveTv;
|
2019-01-13 19:22:00 +00:00
|
|
|
using MediaBrowser.Model.MediaInfo;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2016-09-25 18:39:13 +00:00
|
|
|
|
2017-11-14 07:41:21 +00:00
|
|
|
namespace Emby.Server.Implementations.LiveTv.TunerHosts
|
2016-09-25 18:39:13 +00:00
|
|
|
{
|
2017-11-14 07:41:21 +00:00
|
|
|
public class SharedHttpStream : LiveStream, IDirectStreamProvider
|
2016-09-25 18:39:13 +00:00
|
|
|
{
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public SharedHttpStream(MediaSourceInfo mediaSource, TunerHostInfo tunerHostInfo, string originalStreamId, IFileSystem fileSystem, IHttpClient httpClient, ILogger logger, IServerApplicationPaths appPaths, IServerApplicationHost appHost)
|
|
|
|
: base(mediaSource, tunerHostInfo, 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;
|
2017-11-14 07:41:21 +00:00
|
|
|
EnableStreamSharing = true;
|
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;
|
|
|
|
|
2019-01-26 21:08:04 +00:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(TempFilePath));
|
2017-10-23 19:14:11 +00:00
|
|
|
|
2017-11-14 07:41:21 +00:00
|
|
|
var typeName = GetType().Name;
|
2018-12-13 13:18:25 +00:00
|
|
|
Logger.LogInformation("Opening " + typeName + " Live stream from {0}", url);
|
2016-09-25 18:39:13 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var httpRequestOptions = new HttpRequestOptions
|
2017-10-23 19:14:11 +00:00
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
CancellationToken = CancellationToken.None,
|
|
|
|
BufferContent = false,
|
|
|
|
|
|
|
|
// Increase a little bit
|
|
|
|
TimeoutMs = 30000,
|
2016-09-25 18:39:13 +00:00
|
|
|
|
2017-11-14 07:41:21 +00:00
|
|
|
EnableHttpCompression = false,
|
|
|
|
|
|
|
|
LogResponse = true,
|
|
|
|
LogResponseHeaders = true
|
2018-09-12 17:26:21 +00:00
|
|
|
};
|
2017-10-23 19:14:11 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
foreach (var header in mediaSource.RequiredHttpHeaders)
|
|
|
|
{
|
|
|
|
httpRequestOptions.RequestHeaders[header.Key] = header.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
var response = await _httpClient.SendAsync(httpRequestOptions, "GET").ConfigureAwait(false);
|
2017-10-23 19:14:11 +00:00
|
|
|
|
2017-11-14 07:41:21 +00:00
|
|
|
var extension = "ts";
|
|
|
|
var requiresRemux = false;
|
|
|
|
|
|
|
|
var contentType = response.ContentType ?? string.Empty;
|
2017-11-19 04:59:34 +00:00
|
|
|
if (contentType.IndexOf("matroska", StringComparison.OrdinalIgnoreCase) != -1)
|
|
|
|
{
|
2017-11-20 00:20:12 +00:00
|
|
|
requiresRemux = true;
|
2017-11-19 04:59:34 +00:00
|
|
|
}
|
|
|
|
else if (contentType.IndexOf("mp4", StringComparison.OrdinalIgnoreCase) != -1 ||
|
|
|
|
contentType.IndexOf("dash", StringComparison.OrdinalIgnoreCase) != -1 ||
|
2017-11-29 20:50:18 +00:00
|
|
|
contentType.IndexOf("mpegURL", StringComparison.OrdinalIgnoreCase) != -1 ||
|
|
|
|
contentType.IndexOf("text/", StringComparison.OrdinalIgnoreCase) != -1)
|
2017-11-14 07:41:21 +00:00
|
|
|
{
|
|
|
|
requiresRemux = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the stream without any sharing features
|
|
|
|
if (requiresRemux)
|
|
|
|
{
|
|
|
|
using (response)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetTempFilePath(extension);
|
2017-10-23 19:14:11 +00:00
|
|
|
|
2017-11-06 21:32:44 +00:00
|
|
|
var taskCompletionSource = new TaskCompletionSource<bool>();
|
2017-11-21 22:14:56 +00:00
|
|
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
2019-02-01 20:56:50 +00:00
|
|
|
var _ = 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
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
MediaSource.Path = _appHost.GetLocalApiUrl("127.0.0.1") + "/LiveTv/LiveStreamFiles/" + UniqueId + "/stream.ts";
|
|
|
|
MediaSource.Protocol = MediaProtocol.Http;
|
2017-09-10 03:47:33 +00:00
|
|
|
|
2017-11-20 00:20:12 +00:00
|
|
|
//OpenedMediaSource.Path = TempFilePath;
|
|
|
|
//OpenedMediaSource.Protocol = MediaProtocol.File;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-10-11 06:24:22 +00:00
|
|
|
try
|
2016-10-07 15:08:13 +00:00
|
|
|
{
|
2018-12-13 13:18:25 +00:00
|
|
|
Logger.LogInformation("Beginning {0} stream to {1}", GetType().Name, TempFilePath);
|
2017-10-23 19:14:11 +00:00
|
|
|
using (response)
|
2018-12-13 13:18:25 +00:00
|
|
|
using (var stream = response.Content)
|
|
|
|
using (var fileStream = FileSystem.GetFileStream(TempFilePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, FileOpenOptions.None))
|
2016-10-05 07:15:29 +00:00
|
|
|
{
|
2018-12-13 13:18:25 +00:00
|
|
|
await ApplicationHost.StreamHelper.CopyToAsync(stream, fileStream, 81920, () => Resolve(openTaskCompletionSource), cancellationToken).ConfigureAwait(false);
|
2016-10-05 07:15:29 +00:00
|
|
|
}
|
2017-10-11 06:24:22 +00:00
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
Logger.LogError(ex, "Error copying live stream.");
|
2016-10-05 07:15:29 +00:00
|
|
|
}
|
2017-11-05 21:51:23 +00:00
|
|
|
EnableStreamSharing = false;
|
2018-09-12 17:26:21 +00:00
|
|
|
await DeleteTempFiles(new List<string> { 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
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
DateOpened = DateTime.UtcNow;
|
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
|
|
|
}
|
|
|
|
}
|