implement modular media sources

This commit is contained in:
Luke Pulverenti
2015-03-28 16:22:27 -04:00
parent 3add1872c8
commit bd2ea703e3
42 changed files with 702 additions and 371 deletions

View File

@@ -169,7 +169,7 @@ namespace MediaBrowser.Server.Implementations.Channels
foreach (var item in result.Items)
{
var channelItem = (IChannelItem)item;
var channelItem = (IChannelMediaItem)item;
var channelFeatures = _manager.GetChannelFeatures(channelItem.ChannelId);
@@ -179,7 +179,7 @@ namespace MediaBrowser.Server.Implementations.Channels
{
try
{
await DownloadChannelItem(item, options, cancellationToken, path);
await DownloadChannelItem(channelItem, options, cancellationToken, path);
}
catch (OperationCanceledException)
{
@@ -210,13 +210,13 @@ namespace MediaBrowser.Server.Implementations.Channels
return channelOptions.DownloadSizeLimit;
}
private async Task DownloadChannelItem(BaseItem item,
private async Task DownloadChannelItem(IChannelMediaItem item,
ChannelOptions channelOptions,
CancellationToken cancellationToken,
string path)
{
var itemId = item.Id.ToString("N");
var sources = await _manager.GetChannelItemMediaSources(itemId, false, cancellationToken)
var sources = await _manager.GetStaticMediaSources(item, true, cancellationToken)
.ConfigureAwait(false);
var cachedVersions = sources.Where(i => i.Protocol == MediaProtocol.File).ToList();
@@ -237,11 +237,9 @@ namespace MediaBrowser.Server.Implementations.Channels
}
}
var channelItem = (IChannelMediaItem)item;
var destination = Path.Combine(path, item.ChannelId, itemId);
var destination = Path.Combine(path, channelItem.ChannelId, itemId);
await _manager.DownloadChannelItem(channelItem, destination, new Progress<double>(), cancellationToken)
await _manager.DownloadChannelItem(item, destination, new Progress<double>(), cancellationToken)
.ConfigureAwait(false);
await RefreshMediaSourceItem(destination, cancellationToken).ConfigureAwait(false);

View File

@@ -0,0 +1,43 @@
using MediaBrowser.Controller.Channels;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Dto;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Channels
{
public class ChannelDynamicMediaSourceProvider : IMediaSourceProvider
{
private readonly ChannelManager _channelManager;
public ChannelDynamicMediaSourceProvider(IChannelManager channelManager)
{
_channelManager = (ChannelManager)channelManager;
}
public Task<IEnumerable<MediaSourceInfo>> GetMediaSources(IHasMediaSources item, CancellationToken cancellationToken)
{
var channelItem = item as IChannelMediaItem;
if (channelItem != null)
{
return _channelManager.GetDynamicMediaSources(channelItem, cancellationToken);
}
return Task.FromResult<IEnumerable<MediaSourceInfo>>(new List<MediaSourceInfo>());
}
public Task<MediaSourceInfo> OpenMediaSource(string openKey, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task CloseMediaSource(string closeKey, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
}

View File

@@ -241,10 +241,25 @@ namespace MediaBrowser.Server.Implementations.Channels
return item;
}
public async Task<IEnumerable<MediaSourceInfo>> GetChannelItemMediaSources(string id, bool includeDynamicSources, CancellationToken cancellationToken)
public async Task<IEnumerable<MediaSourceInfo>> GetStaticMediaSources(IChannelMediaItem item, bool includeCachedVersions, CancellationToken cancellationToken)
{
var item = (IChannelMediaItem)_libraryManager.GetItemById(id);
IEnumerable<ChannelMediaInfo> results = item.ChannelMediaSources;
var sources = SortMediaInfoResults(results)
.Select(i => GetMediaSource(item, i))
.ToList();
if (includeCachedVersions)
{
var cachedVersions = GetCachedChannelItemMediaSources(item);
sources.InsertRange(0, cachedVersions);
}
return sources.Where(IsValidMediaSource);
}
public async Task<IEnumerable<MediaSourceInfo>> GetDynamicMediaSources(IChannelMediaItem item, CancellationToken cancellationToken)
{
var channel = GetChannel(item.ChannelId);
var channelPlugin = GetChannelProvider(channel);
@@ -252,24 +267,25 @@ namespace MediaBrowser.Server.Implementations.Channels
IEnumerable<ChannelMediaInfo> results;
if (requiresCallback != null && includeDynamicSources)
if (requiresCallback != null)
{
results = await GetChannelItemMediaSourcesInternal(requiresCallback, item.ExternalId, cancellationToken)
.ConfigureAwait(false);
.ConfigureAwait(false);
}
else
{
results = item.ChannelMediaSources;
results = new List<ChannelMediaInfo>();
}
var sources = SortMediaInfoResults(results).Select(i => GetMediaSource(item, i))
var list = SortMediaInfoResults(results)
.Select(i => GetMediaSource(item, i))
.Where(IsValidMediaSource)
.ToList();
var cachedVersions = GetCachedChannelItemMediaSources(item);
list.InsertRange(0, cachedVersions);
sources.InsertRange(0, cachedVersions);
return sources.Where(IsValidMediaSource);
return list;
}
private readonly ConcurrentDictionary<string, Tuple<DateTime, List<ChannelMediaInfo>>> _channelItemMediaInfo =
@@ -297,14 +313,7 @@ namespace MediaBrowser.Server.Implementations.Channels
return list;
}
public IEnumerable<MediaSourceInfo> GetCachedChannelItemMediaSources(string id)
{
var item = (IChannelMediaItem)_libraryManager.GetItemById(id);
return GetCachedChannelItemMediaSources(item);
}
public IEnumerable<MediaSourceInfo> GetCachedChannelItemMediaSources(IChannelMediaItem item)
private IEnumerable<MediaSourceInfo> GetCachedChannelItemMediaSources(IChannelMediaItem item)
{
var filenamePrefix = item.Id.ToString("N");
var parentPath = Path.Combine(ChannelDownloadPath, item.ChannelId);
@@ -339,7 +348,6 @@ namespace MediaBrowser.Server.Implementations.Channels
if (source != null)
{
source.Type = MediaSourceType.Cache;
return new[] { source };
}
}
@@ -1408,8 +1416,7 @@ namespace MediaBrowser.Server.Implementations.Channels
public async Task DownloadChannelItem(IChannelMediaItem item, string destination,
IProgress<double> progress, CancellationToken cancellationToken)
{
var itemId = item.Id.ToString("N");
var sources = await GetChannelItemMediaSources(itemId, true, cancellationToken)
var sources = await GetDynamicMediaSources(item, cancellationToken)
.ConfigureAwait(false);
var list = sources.Where(i => i.Protocol == MediaProtocol.Http).ToList();