diff options
| author | Luke <luke.pulverenti@gmail.com> | 2017-10-01 23:35:45 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-10-01 23:35:45 -0400 |
| commit | a73da532d52ae68ad01965dbf368bacc7d56a218 (patch) | |
| tree | a6f05e95515f822dfc129c36554f74f1da4d4c23 /Emby.Server.Implementations/LiveTv/TunerHosts | |
| parent | f651fd6ffba80a6f8e54fca7d014622692cc08f7 (diff) | |
| parent | 796f374359d75e22d2095b3f3a8f9b220cacde27 (diff) | |
Merge pull request #2931 from MediaBrowser/beta
Beta
Diffstat (limited to 'Emby.Server.Implementations/LiveTv/TunerHosts')
6 files changed, 170 insertions, 131 deletions
diff --git a/Emby.Server.Implementations/LiveTv/TunerHosts/BaseTunerHost.cs b/Emby.Server.Implementations/LiveTv/TunerHosts/BaseTunerHost.cs index 4b4f61d535..e0fd32aeef 100644 --- a/Emby.Server.Implementations/LiveTv/TunerHosts/BaseTunerHost.cs +++ b/Emby.Server.Implementations/LiveTv/TunerHosts/BaseTunerHost.cs @@ -193,9 +193,9 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts return new List<MediaSourceInfo>(); } - protected abstract Task<LiveStream> GetChannelStream(TunerHostInfo tuner, string channelId, string streamId, CancellationToken cancellationToken); + protected abstract Task<ILiveStream> GetChannelStream(TunerHostInfo tuner, string channelId, string streamId, CancellationToken cancellationToken); - public async Task<LiveStream> GetChannelStream(string channelId, string streamId, CancellationToken cancellationToken) + public async Task<ILiveStream> GetChannelStream(string channelId, string streamId, CancellationToken cancellationToken) { if (string.IsNullOrWhiteSpace(channelId)) { @@ -247,7 +247,10 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts try { var liveStream = await GetChannelStream(host, channelId, streamId, cancellationToken).ConfigureAwait(false); + var startTime = DateTime.UtcNow; await liveStream.Open(cancellationToken).ConfigureAwait(false); + var endTime = DateTime.UtcNow; + Logger.Info("Live stream opened after {0}ms", (endTime - startTime).TotalMilliseconds); return liveStream; } catch (Exception ex) diff --git a/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHost.cs b/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHost.cs index f974b5c2c7..bb11dac5f1 100644 --- a/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHost.cs +++ b/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHost.cs @@ -347,6 +347,15 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun videoCodec = "h264"; videoBitrate = 1000000; } + else + { + // This is for android tv's 1200 condition. Remove once not needed anymore so that we can avoid possible side effects of dummying up this data + if ((channelInfo.IsHD ?? true)) + { + width = 1920; + height = 1080; + } + } if (channelInfo != null) { @@ -491,7 +500,7 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun return list; } - protected override async Task<LiveStream> GetChannelStream(TunerHostInfo info, string channelId, string streamId, CancellationToken cancellationToken) + protected override async Task<ILiveStream> GetChannelStream(TunerHostInfo info, string channelId, string streamId, CancellationToken cancellationToken) { var profile = streamId.Split('_')[0]; diff --git a/Emby.Server.Implementations/LiveTv/TunerHosts/LiveStream.cs b/Emby.Server.Implementations/LiveTv/TunerHosts/LiveStream.cs new file mode 100644 index 0000000000..4641a1c91f --- /dev/null +++ b/Emby.Server.Implementations/LiveTv/TunerHosts/LiveStream.cs @@ -0,0 +1,153 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Controller; +using MediaBrowser.Controller.IO; +using MediaBrowser.Controller.LiveTv; +using MediaBrowser.Model.Dto; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.System; + +namespace Emby.Server.Implementations.LiveTv.TunerHosts +{ + public class LiveStream : ILiveStream + { + public MediaSourceInfo OriginalMediaSource { get; set; } + public MediaSourceInfo OpenedMediaSource { get; set; } + public int ConsumerCount + { + get { return SharedStreamIds.Count; } + } + public ITunerHost TunerHost { get; set; } + public string OriginalStreamId { get; set; } + public bool EnableStreamSharing { get; set; } + public string UniqueId { get; private set; } + + public List<string> SharedStreamIds { get; private set; } + protected readonly IEnvironmentInfo Environment; + protected readonly IFileSystem FileSystem; + + protected readonly string TempFilePath; + protected readonly ILogger Logger; + + public LiveStream(MediaSourceInfo mediaSource, IEnvironmentInfo environment, IFileSystem fileSystem, ILogger logger, IServerApplicationPaths appPaths) + { + OriginalMediaSource = mediaSource; + Environment = environment; + FileSystem = fileSystem; + OpenedMediaSource = mediaSource; + Logger = logger; + EnableStreamSharing = true; + SharedStreamIds = new List<string>(); + UniqueId = Guid.NewGuid().ToString("N"); + TempFilePath = Path.Combine(appPaths.TranscodingTempPath, UniqueId + ".ts"); + } + + public Task Open(CancellationToken cancellationToken) + { + return OpenInternal(cancellationToken); + } + + protected virtual Task OpenInternal(CancellationToken cancellationToken) + { + return Task.FromResult(true); + } + + public virtual Task Close() + { + return Task.FromResult(true); + } + + protected Stream GetInputStream(string path, bool allowAsyncFileRead) + { + var fileOpenOptions = FileOpenOptions.SequentialScan; + + if (allowAsyncFileRead) + { + fileOpenOptions |= FileOpenOptions.Asynchronous; + } + + return FileSystem.GetFileStream(path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite, fileOpenOptions); + } + + protected async Task DeleteTempFile(string path, int retryCount = 0) + { + try + { + FileSystem.DeleteFile(path); + return; + } + catch + { + + } + + if (retryCount > 20) + { + return; + } + + await Task.Delay(500).ConfigureAwait(false); + await DeleteTempFile(path, retryCount + 1).ConfigureAwait(false); + } + + public async Task CopyToAsync(Stream stream, CancellationToken cancellationToken) + { + var allowAsync = false;//Environment.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows; + // use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039 + + using (var inputStream = (FileStream)GetInputStream(TempFilePath, allowAsync)) + { + TrySeek(inputStream, -20000); + + await CopyTo(inputStream, stream, 81920, null, cancellationToken).ConfigureAwait(false); + } + } + + private static async Task CopyTo(Stream source, Stream destination, int bufferSize, Action onStarted, CancellationToken cancellationToken) + { + byte[] buffer = new byte[bufferSize]; + while (true) + { + cancellationToken.ThrowIfCancellationRequested(); + + var read = source.Read(buffer, 0, buffer.Length); + + if (read > 0) + { + //await destination.WriteAsync(buffer, 0, read).ConfigureAwait(false); + destination.Write(buffer, 0, read); + + if (onStarted != null) + { + onStarted(); + onStarted = null; + } + } + else + { + await Task.Delay(10).ConfigureAwait(false); + } + } + } + + private void TrySeek(FileStream stream, long offset) + { + try + { + stream.Seek(offset, SeekOrigin.End); + } + catch (ArgumentException) + { + + } + catch (Exception ex) + { + Logger.ErrorException("Error seeking stream", ex); + } + } + } +} diff --git a/Emby.Server.Implementations/LiveTv/TunerHosts/M3UTunerHost.cs b/Emby.Server.Implementations/LiveTv/TunerHosts/M3UTunerHost.cs index eac8455791..8d1854f4b2 100644 --- a/Emby.Server.Implementations/LiveTv/TunerHosts/M3UTunerHost.cs +++ b/Emby.Server.Implementations/LiveTv/TunerHosts/M3UTunerHost.cs @@ -75,11 +75,11 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts return Task.FromResult(list); } - protected override async Task<LiveStream> GetChannelStream(TunerHostInfo info, string channelId, string streamId, CancellationToken cancellationToken) + protected override async Task<ILiveStream> GetChannelStream(TunerHostInfo info, string channelId, string streamId, CancellationToken cancellationToken) { var sources = await GetChannelStreamMediaSources(info, channelId, cancellationToken).ConfigureAwait(false); - var liveStream = new LiveStream(sources.First(), _environment, FileSystem); + var liveStream = new LiveStream(sources.First(), _environment, FileSystem, Logger, Config.ApplicationPaths); return liveStream; } diff --git a/Emby.Server.Implementations/LiveTv/TunerHosts/MulticastStream.cs b/Emby.Server.Implementations/LiveTv/TunerHosts/MulticastStream.cs deleted file mode 100644 index 45a0c348e1..0000000000 --- a/Emby.Server.Implementations/LiveTv/TunerHosts/MulticastStream.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using MediaBrowser.Model.Logging; -using MediaBrowser.Model.Net; - -namespace Emby.Server.Implementations.LiveTv.TunerHosts -{ - public class MulticastStream - { - private readonly ConcurrentDictionary<Guid, QueueStream> _outputStreams = new ConcurrentDictionary<Guid, QueueStream>(); - private const int BufferSize = 81920; - private readonly ILogger _logger; - - public MulticastStream(ILogger logger) - { - _logger = logger; - } - - public async Task CopyUntilCancelled(Stream source, Action onStarted, CancellationToken cancellationToken) - { - if (source == null) - { - throw new ArgumentNullException("source"); - } - - while (true) - { - cancellationToken.ThrowIfCancellationRequested(); - - byte[] buffer = new byte[BufferSize]; - - var bytesRead = source.Read(buffer, 0, buffer.Length); - - if (bytesRead > 0) - { - foreach (var stream in _outputStreams) - { - stream.Value.Queue(buffer, 0, bytesRead); - } - - if (onStarted != null) - { - var onStartedCopy = onStarted; - onStarted = null; - Task.Run(onStartedCopy); - } - } - - else - { - await Task.Delay(100).ConfigureAwait(false); - } - } - } - - public Task CopyToAsync(Stream stream, CancellationToken cancellationToken) - { - var queueStream = new QueueStream(stream, _logger); - - _outputStreams.TryAdd(queueStream.Id, queueStream); - - try - { - queueStream.Start(cancellationToken); - } - finally - { - _outputStreams.TryRemove(queueStream.Id, out queueStream); - GC.Collect(); - } - - return Task.FromResult(true); - } - } -} diff --git a/Emby.Server.Implementations/LiveTv/TunerHosts/QueueStream.cs b/Emby.Server.Implementations/LiveTv/TunerHosts/QueueStream.cs deleted file mode 100644 index 07a4daa87d..0000000000 --- a/Emby.Server.Implementations/LiveTv/TunerHosts/QueueStream.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using MediaBrowser.Model.Logging; - -namespace Emby.Server.Implementations.LiveTv.TunerHosts -{ - public class QueueStream - { - private readonly Stream _outputStream; - private readonly BlockingCollection<Tuple<byte[], int, int>> _queue = new BlockingCollection<Tuple<byte[], int, int>>(); - - private readonly ILogger _logger; - public Guid Id = Guid.NewGuid(); - - public QueueStream(Stream outputStream, ILogger logger) - { - _outputStream = outputStream; - _logger = logger; - } - - public void Queue(byte[] bytes, int offset, int count) - { - _queue.Add(new Tuple<byte[], int, int>(bytes, offset, count)); - } - - public void Start(CancellationToken cancellationToken) - { - while (true) - { - foreach (var result in _queue.GetConsumingEnumerable()) - { - cancellationToken.ThrowIfCancellationRequested(); - - _outputStream.Write(result.Item1, result.Item2, result.Item3); - } - } - } - } -} |
