2015-03-07 22:43:53 +00:00
|
|
|
|
using MediaBrowser.Controller.Channels;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2015-02-07 21:03:09 +00:00
|
|
|
|
using MediaBrowser.Controller.Persistence;
|
2015-03-07 22:43:53 +00:00
|
|
|
|
using MediaBrowser.Model.Dto;
|
2015-02-07 21:03:09 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2015-03-07 23:39:24 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2015-03-03 07:03:17 +00:00
|
|
|
|
using System;
|
2015-02-07 21:03:09 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2015-03-07 22:43:53 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2015-02-07 21:03:09 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Library
|
|
|
|
|
{
|
|
|
|
|
public class MediaSourceManager : IMediaSourceManager
|
|
|
|
|
{
|
|
|
|
|
private readonly IItemRepository _itemRepo;
|
2015-03-07 22:43:53 +00:00
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly IChannelManager _channelManager;
|
2015-02-07 21:03:09 +00:00
|
|
|
|
|
2015-03-07 22:43:53 +00:00
|
|
|
|
private IMediaSourceProvider[] _providers;
|
2015-03-07 23:39:24 +00:00
|
|
|
|
private readonly ILogger _logger;
|
2015-03-07 22:43:53 +00:00
|
|
|
|
|
2015-03-07 23:39:24 +00:00
|
|
|
|
public MediaSourceManager(IItemRepository itemRepo, IUserManager userManager, ILibraryManager libraryManager, IChannelManager channelManager, ILogger logger)
|
2015-02-07 21:03:09 +00:00
|
|
|
|
{
|
|
|
|
|
_itemRepo = itemRepo;
|
2015-03-07 22:43:53 +00:00
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_channelManager = channelManager;
|
2015-03-07 23:39:24 +00:00
|
|
|
|
_logger = logger;
|
2015-03-07 22:43:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddParts(IEnumerable<IMediaSourceProvider> providers)
|
|
|
|
|
{
|
|
|
|
|
_providers = providers.ToArray();
|
2015-02-07 21:03:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<MediaStream> GetMediaStreams(MediaStreamQuery query)
|
|
|
|
|
{
|
|
|
|
|
var list = _itemRepo.GetMediaStreams(query)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var stream in list)
|
|
|
|
|
{
|
|
|
|
|
stream.SupportsExternalStream = StreamSupportsExternalStream(stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool StreamSupportsExternalStream(MediaStream stream)
|
|
|
|
|
{
|
|
|
|
|
if (stream.IsExternal)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stream.IsTextSubtitleStream)
|
|
|
|
|
{
|
|
|
|
|
return InternalTextStreamSupportsExternalStream(stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool InternalTextStreamSupportsExternalStream(MediaStream stream)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-03-03 07:03:17 +00:00
|
|
|
|
|
|
|
|
|
public IEnumerable<MediaStream> GetMediaStreams(string mediaSourceId)
|
|
|
|
|
{
|
|
|
|
|
var list = GetMediaStreams(new MediaStreamQuery
|
|
|
|
|
{
|
|
|
|
|
ItemId = new Guid(mediaSourceId)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return GetMediaStreamsForItem(list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<MediaStream> GetMediaStreams(Guid itemId)
|
|
|
|
|
{
|
|
|
|
|
var list = GetMediaStreams(new MediaStreamQuery
|
|
|
|
|
{
|
|
|
|
|
ItemId = itemId
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return GetMediaStreamsForItem(list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<MediaStream> GetMediaStreamsForItem(IEnumerable<MediaStream> streams)
|
|
|
|
|
{
|
|
|
|
|
var list = streams.ToList();
|
|
|
|
|
|
|
|
|
|
var subtitleStreams = list
|
|
|
|
|
.Where(i => i.Type == MediaStreamType.Subtitle)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
if (subtitleStreams.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var videoStream = list.FirstOrDefault(i => i.Type == MediaStreamType.Video);
|
|
|
|
|
|
|
|
|
|
// This is abitrary but at some point it becomes too slow to extract subtitles on the fly
|
|
|
|
|
// We need to learn more about when this is the case vs. when it isn't
|
|
|
|
|
const int maxAllowedBitrateForExternalSubtitleStream = 10000000;
|
|
|
|
|
|
|
|
|
|
var videoBitrate = videoStream == null ? maxAllowedBitrateForExternalSubtitleStream : videoStream.BitRate ?? maxAllowedBitrateForExternalSubtitleStream;
|
|
|
|
|
|
|
|
|
|
foreach (var subStream in subtitleStreams)
|
|
|
|
|
{
|
|
|
|
|
var supportsExternalStream = StreamSupportsExternalStream(subStream);
|
|
|
|
|
|
|
|
|
|
if (supportsExternalStream && videoBitrate >= maxAllowedBitrateForExternalSubtitleStream)
|
|
|
|
|
{
|
|
|
|
|
supportsExternalStream = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
subStream.SupportsExternalStream = supportsExternalStream;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
2015-03-07 22:43:53 +00:00
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<MediaSourceInfo>> GetPlayackMediaSources(string id, string userId, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var item = _libraryManager.GetItemById(id);
|
|
|
|
|
IEnumerable<MediaSourceInfo> mediaSources;
|
|
|
|
|
|
2015-03-07 23:39:24 +00:00
|
|
|
|
var hasMediaSources = (IHasMediaSources)item;
|
2015-03-07 22:43:53 +00:00
|
|
|
|
var channelItem = item as IChannelMediaItem;
|
|
|
|
|
|
|
|
|
|
if (channelItem != null)
|
|
|
|
|
{
|
|
|
|
|
mediaSources = await _channelManager.GetChannelItemMediaSources(id, true, cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(userId))
|
|
|
|
|
{
|
|
|
|
|
mediaSources = hasMediaSources.GetMediaSources(true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var user = _userManager.GetUserById(userId);
|
|
|
|
|
mediaSources = GetStaticMediaSources(hasMediaSources, true, user);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-07 23:39:24 +00:00
|
|
|
|
var dynamicMediaSources = await GetDynamicMediaSources(hasMediaSources, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var list = new List<MediaSourceInfo>();
|
|
|
|
|
|
|
|
|
|
list.AddRange(mediaSources);
|
2015-03-08 16:25:46 +00:00
|
|
|
|
|
|
|
|
|
foreach (var source in dynamicMediaSources)
|
|
|
|
|
{
|
|
|
|
|
source.SupportsTranscoding = false;
|
|
|
|
|
list.Add(source);
|
|
|
|
|
}
|
2015-03-07 23:39:24 +00:00
|
|
|
|
|
|
|
|
|
return SortMediaSources(list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<IEnumerable<MediaSourceInfo>> GetDynamicMediaSources(IHasMediaSources item, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var tasks = _providers.Select(i => GetDynamicMediaSources(item, i, cancellationToken));
|
|
|
|
|
var results = await Task.WhenAll(tasks).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return results.SelectMany(i => i.ToList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<IEnumerable<MediaSourceInfo>> GetDynamicMediaSources(IHasMediaSources item, IMediaSourceProvider provider, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return await provider.GetMediaSources(item, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting media sources", ex);
|
|
|
|
|
return new List<MediaSourceInfo>();
|
|
|
|
|
}
|
2015-03-07 22:43:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<IEnumerable<MediaSourceInfo>> GetPlayackMediaSources(string id, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return GetPlayackMediaSources(id, null, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<MediaSourceInfo> GetStaticMediaSources(IHasMediaSources item, bool enablePathSubstitution)
|
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("item");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(item is Video))
|
|
|
|
|
{
|
|
|
|
|
return item.GetMediaSources(enablePathSubstitution);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return item.GetMediaSources(enablePathSubstitution);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<MediaSourceInfo> GetStaticMediaSources(IHasMediaSources item, bool enablePathSubstitution, User user)
|
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("item");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(item is Video))
|
|
|
|
|
{
|
|
|
|
|
return item.GetMediaSources(enablePathSubstitution);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("user");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sources = item.GetMediaSources(enablePathSubstitution).ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var source in sources)
|
|
|
|
|
{
|
|
|
|
|
SetUserProperties(source, user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sources;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetUserProperties(MediaSourceInfo source, User user)
|
|
|
|
|
{
|
|
|
|
|
var preferredAudio = string.IsNullOrEmpty(user.Configuration.AudioLanguagePreference)
|
|
|
|
|
? new string[] { }
|
|
|
|
|
: new[] { user.Configuration.AudioLanguagePreference };
|
|
|
|
|
|
|
|
|
|
var preferredSubs = string.IsNullOrEmpty(user.Configuration.SubtitleLanguagePreference)
|
|
|
|
|
? new List<string> { }
|
|
|
|
|
: new List<string> { user.Configuration.SubtitleLanguagePreference };
|
|
|
|
|
|
|
|
|
|
source.DefaultAudioStreamIndex = MediaStreamSelector.GetDefaultAudioStreamIndex(source.MediaStreams, preferredAudio, user.Configuration.PlayDefaultAudioTrack);
|
|
|
|
|
|
|
|
|
|
var defaultAudioIndex = source.DefaultAudioStreamIndex;
|
|
|
|
|
var audioLangage = defaultAudioIndex == null
|
|
|
|
|
? null
|
|
|
|
|
: source.MediaStreams.Where(i => i.Type == MediaStreamType.Audio && i.Index == defaultAudioIndex).Select(i => i.Language).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
source.DefaultSubtitleStreamIndex = MediaStreamSelector.GetDefaultSubtitleStreamIndex(source.MediaStreams,
|
|
|
|
|
preferredSubs,
|
|
|
|
|
user.Configuration.SubtitleMode,
|
|
|
|
|
audioLangage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<MediaSourceInfo> SortMediaSources(IEnumerable<MediaSourceInfo> sources)
|
|
|
|
|
{
|
|
|
|
|
return sources.OrderBy(i =>
|
|
|
|
|
{
|
|
|
|
|
if (i.VideoType.HasValue && i.VideoType.Value == VideoType.VideoFile)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
}).ThenBy(i => i.Video3DFormat.HasValue ? 1 : 0)
|
|
|
|
|
.ThenByDescending(i =>
|
|
|
|
|
{
|
|
|
|
|
var stream = i.VideoStream;
|
|
|
|
|
|
|
|
|
|
return stream == null || stream.Width == null ? 0 : stream.Width.Value;
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MediaSourceInfo GetStaticMediaSource(IHasMediaSources item, string mediaSourceId, bool enablePathSubstitution)
|
|
|
|
|
{
|
|
|
|
|
return GetStaticMediaSources(item, enablePathSubstitution).FirstOrDefault(i => string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
}
|
2015-02-07 21:03:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|