From bd2ea703e31522d505407a33089b95f997f6b062 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 28 Mar 2015 16:22:27 -0400 Subject: implement modular media sources --- .../LiveTv/LiveTvManager.cs | 9 +-- .../LiveTv/LiveTvMediaSourceProvider.cs | 77 ++++++++++++++++++++++ 2 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 MediaBrowser.Server.Implementations/LiveTv/LiveTvMediaSourceProvider.cs (limited to 'MediaBrowser.Server.Implementations/LiveTv') diff --git a/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs b/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs index 59daa4921a..202a051e3b 100644 --- a/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs +++ b/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs @@ -1,10 +1,8 @@ -using System.Globalization; -using MediaBrowser.Common; +using MediaBrowser.Common; using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Progress; using MediaBrowser.Common.ScheduledTasks; -using MediaBrowser.Controller.Channels; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Drawing; using MediaBrowser.Controller.Dto; @@ -15,7 +13,6 @@ using MediaBrowser.Controller.Localization; using MediaBrowser.Controller.Persistence; using MediaBrowser.Controller.Providers; using MediaBrowser.Controller.Sorting; -using MediaBrowser.Model.Dlna; using MediaBrowser.Model.Dto; using MediaBrowser.Model.Entities; using MediaBrowser.Model.LiveTv; @@ -342,6 +339,8 @@ namespace MediaBrowser.Server.Implementations.LiveTv var service = GetService(channel); _logger.Info("Opening channel stream from {0}, external channel Id: {1}", service.Name, channel.ExternalId); info = await service.GetChannelStream(channel.ExternalId, null, cancellationToken).ConfigureAwait(false); + info.RequiresClosing = true; + info.CloseKey = info.Id; } else { @@ -351,6 +350,8 @@ namespace MediaBrowser.Server.Implementations.LiveTv _logger.Info("Opening recording stream from {0}, external recording Id: {1}", service.Name, recording.RecordingInfo.Id); info = await service.GetRecordingStream(recording.RecordingInfo.Id, null, cancellationToken).ConfigureAwait(false); + info.RequiresClosing = true; + info.CloseKey = info.Id; } _logger.Info("Live stream info: {0}", _jsonSerializer.SerializeToString(info)); diff --git a/MediaBrowser.Server.Implementations/LiveTv/LiveTvMediaSourceProvider.cs b/MediaBrowser.Server.Implementations/LiveTv/LiveTvMediaSourceProvider.cs new file mode 100644 index 0000000000..186bc499db --- /dev/null +++ b/MediaBrowser.Server.Implementations/LiveTv/LiveTvMediaSourceProvider.cs @@ -0,0 +1,77 @@ +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.LiveTv; +using MediaBrowser.Model.Dto; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Server.Implementations.LiveTv +{ + public class LiveTvMediaSourceProvider : IMediaSourceProvider + { + private readonly ILiveTvManager _liveTvManager; + + public LiveTvMediaSourceProvider(ILiveTvManager liveTvManager) + { + _liveTvManager = liveTvManager; + } + + public Task> GetMediaSources(IHasMediaSources item, CancellationToken cancellationToken) + { + var channelItem = item as ILiveTvItem; + + if (channelItem != null) + { + var hasMetadata = (IHasMetadata)channelItem; + + if (string.IsNullOrWhiteSpace(hasMetadata.Path)) + { + return GetMediaSourcesInternal(channelItem, cancellationToken); + } + } + + return Task.FromResult>(new List()); + } + + private async Task> GetMediaSourcesInternal(ILiveTvItem item, CancellationToken cancellationToken) + { + var hasMediaSources = (IHasMediaSources)item; + + var sources = hasMediaSources.GetMediaSources(false) + .ToList(); + + foreach (var source in sources) + { + source.Type = MediaSourceType.Default; + source.RequiresOpening = true; + + var openKeys = new List(); + openKeys.Add(item.GetType().Name); + openKeys.Add(item.Id.ToString("N")); + source.OpenKey = string.Join("|", openKeys.ToArray()); + } + + return sources; + } + + public async Task OpenMediaSource(string openKey, CancellationToken cancellationToken) + { + var keys = openKey.Split(new[] { '|' }, 2); + + if (string.Equals(keys[0], typeof(LiveTvChannel).Name, StringComparison.OrdinalIgnoreCase)) + { + return await _liveTvManager.GetChannelStream(keys[1], cancellationToken).ConfigureAwait(false); + } + + return await _liveTvManager.GetRecordingStream(keys[1], cancellationToken).ConfigureAwait(false); + } + + public Task CloseMediaSource(string closeKey, CancellationToken cancellationToken) + { + return _liveTvManager.CloseLiveStream(closeKey, cancellationToken); + } + } +} -- cgit v1.2.3