2015-03-17 03:51:35 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using MediaBrowser.Controller.Channels;
|
2015-03-07 22:43:53 +00:00
|
|
|
|
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-03-17 03:51:35 +00:00
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
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
|
|
|
|
|
2015-03-12 03:37:25 +00:00
|
|
|
|
public async Task<IEnumerable<MediaSourceInfo>> GetPlayackMediaSources(string id, string userId, bool enablePathSubstitution, CancellationToken cancellationToken)
|
2015-03-07 22:43:53 +00:00
|
|
|
|
{
|
|
|
|
|
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))
|
|
|
|
|
{
|
2015-03-12 03:37:25 +00:00
|
|
|
|
mediaSources = hasMediaSources.GetMediaSources(enablePathSubstitution);
|
2015-03-07 22:43:53 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var user = _userManager.GetUserById(userId);
|
2015-03-12 03:37:25 +00:00
|
|
|
|
mediaSources = GetStaticMediaSources(hasMediaSources, enablePathSubstitution, user);
|
2015-03-07 22:43:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2015-03-17 03:51:35 +00:00
|
|
|
|
|
|
|
|
|
if (source.Protocol == MediaProtocol.File)
|
|
|
|
|
{
|
|
|
|
|
source.SupportsDirectStream = File.Exists(source.Path);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-08 16:25:46 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2015-03-12 03:37:25 +00:00
|
|
|
|
public Task<IEnumerable<MediaSourceInfo>> GetPlayackMediaSources(string id, bool enablePathSubstitution, CancellationToken cancellationToken)
|
2015-03-07 22:43:53 +00:00
|
|
|
|
{
|
2015-03-12 03:37:25 +00:00
|
|
|
|
return GetPlayackMediaSources(id, null, enablePathSubstitution, cancellationToken);
|
2015-03-07 22:43:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|