From 97ae93fe5eb0f010db9835efd72954f31ccdd2cd Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 21 Dec 2014 14:40:37 -0500 Subject: add standalone EncodingOptions --- .../Configuration/EncodingConfigurationFactory.cs | 45 ++++++++++++++++++++++ .../MediaBrowser.MediaEncoding.csproj | 1 + 2 files changed, 46 insertions(+) create mode 100644 MediaBrowser.MediaEncoding/Configuration/EncodingConfigurationFactory.cs (limited to 'MediaBrowser.MediaEncoding') diff --git a/MediaBrowser.MediaEncoding/Configuration/EncodingConfigurationFactory.cs b/MediaBrowser.MediaEncoding/Configuration/EncodingConfigurationFactory.cs new file mode 100644 index 000000000..17470d206 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Configuration/EncodingConfigurationFactory.cs @@ -0,0 +1,45 @@ +using MediaBrowser.Common.Configuration; +using MediaBrowser.Model.Configuration; +using System.Collections.Generic; +using System.IO; + +namespace MediaBrowser.MediaEncoding.Configuration +{ + public class EncodingConfigurationFactory : IConfigurationFactory + { + public IEnumerable GetConfigurations() + { + return new[] + { + new EncodingConfigurationStore() + }; + } + } + + public class EncodingConfigurationStore : ConfigurationStore, IValidatingConfiguration + { + public EncodingConfigurationStore() + { + ConfigurationType = typeof(EncodingOptions); + Key = "encoding"; + } + + public void Validate(object oldConfig, object newConfig) + { + var oldEncodingConfig = (EncodingOptions)oldConfig; + var newEncodingConfig = (EncodingOptions)newConfig; + + var newPath = newEncodingConfig.TranscodingTempPath; + + if (!string.IsNullOrWhiteSpace(newPath) + && !string.Equals(oldEncodingConfig.TranscodingTempPath ?? string.Empty, newPath)) + { + // Validate + if (!Directory.Exists(newPath)) + { + throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath)); + } + } + } + } +} diff --git a/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj b/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj index 6f59b7bec..5c472ebc8 100644 --- a/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj +++ b/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj @@ -56,6 +56,7 @@ Properties\SharedVersion.cs + -- cgit v1.2.3 From 42b14166029d5251e952b72f5c16cd9ae96aa8cb Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 22 Dec 2014 22:58:14 -0500 Subject: begin work on daily episodes --- MediaBrowser.Api/ItemUpdateService.cs | 3 +- .../Configuration/BaseConfigurationManager.cs | 4 + .../Devices/DeviceId.cs | 5 +- .../Security/MBLicenseFile.cs | 4 + MediaBrowser.Common/Extensions/BaseExtensions.cs | 11 +++ MediaBrowser.Common/Plugins/BasePlugin.cs | 4 + MediaBrowser.Controller/Entities/TV/Episode.cs | 11 ++- MediaBrowser.Controller/Entities/TV/Season.cs | 78 ++++++++++++------- MediaBrowser.Controller/Library/IUserManager.cs | 7 ++ MediaBrowser.LocalMetadata/BaseXmlProvider.cs | 6 -- .../Subtitles/SubtitleEncoder.cs | 4 + MediaBrowser.Model/ApiClient/IApiClient.cs | 18 ----- .../Movies/FanartMovieImageProvider.cs | 4 + .../Music/FanArtAlbumProvider.cs | 4 + .../Music/FanArtArtistProvider.cs | 4 + .../People/TvdbPersonImageProvider.cs | 4 + MediaBrowser.Providers/TV/FanArtSeasonProvider.cs | 4 + MediaBrowser.Providers/TV/FanartSeriesProvider.cs | 4 + .../TV/MissingEpisodeProvider.cs | 63 +++++++++------- MediaBrowser.Providers/TV/SeriesPostScanTask.cs | 11 ++- MediaBrowser.Providers/TV/TvdbEpisodeProvider.cs | 15 +++- .../TV/TvdbSeasonImageProvider.cs | 4 + .../TV/TvdbSeriesImageProvider.cs | 4 + .../Connect/ConnectManager.cs | 3 +- .../Drawing/ImageProcessor.cs | 5 ++ .../FileOrganization/EpisodeFileOrganizer.cs | 10 ++- .../Library/LibraryManager.cs | 88 ++++++++++++++++++---- .../Library/Resolvers/TV/EpisodeResolver.cs | 2 - .../Library/UserManager.cs | 68 +++++++++++++---- .../Localization/JavaScript/javascript.json | 4 +- .../Localization/Server/server.json | 9 ++- .../News/NewsService.cs | 8 ++ MediaBrowser.XbmcMetadata/Savers/BaseNfoSaver.cs | 4 + 33 files changed, 349 insertions(+), 128 deletions(-) (limited to 'MediaBrowser.MediaEncoding') diff --git a/MediaBrowser.Api/ItemUpdateService.cs b/MediaBrowser.Api/ItemUpdateService.cs index 020908ddd..272dff3ec 100644 --- a/MediaBrowser.Api/ItemUpdateService.cs +++ b/MediaBrowser.Api/ItemUpdateService.cs @@ -3,6 +3,7 @@ using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.LiveTv; using MediaBrowser.Controller.Localization; using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Providers; @@ -73,7 +74,7 @@ namespace MediaBrowser.Api if (locationType == LocationType.FileSystem || locationType == LocationType.Offline) { - if (!(item is ICollectionFolder) && !(item is UserView) && !(item is AggregateFolder)) + if (!(item is ICollectionFolder) && !(item is UserView) && !(item is AggregateFolder) && !(item is LiveTvChannel) && !(item is IItemByName)) { var collectionType = _libraryManager.GetInheritedContentType(item); if (string.IsNullOrWhiteSpace(collectionType)) diff --git a/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs b/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs index 89f6229ac..c53947e44 100644 --- a/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs +++ b/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs @@ -223,6 +223,10 @@ namespace MediaBrowser.Common.Implementations.Configuration { return Activator.CreateInstance(configurationType); } + catch (DirectoryNotFoundException) + { + return Activator.CreateInstance(configurationType); + } catch (Exception ex) { Logger.ErrorException("Error loading configuration file: {0}", ex, path); diff --git a/MediaBrowser.Common.Implementations/Devices/DeviceId.cs b/MediaBrowser.Common.Implementations/Devices/DeviceId.cs index 5af236026..7c0dc1e1f 100644 --- a/MediaBrowser.Common.Implementations/Devices/DeviceId.cs +++ b/MediaBrowser.Common.Implementations/Devices/DeviceId.cs @@ -38,7 +38,10 @@ namespace MediaBrowser.Common.Implementations.Devices _logger.Error("Invalid value found in device id file"); } } - catch (FileNotFoundException ex) + catch (DirectoryNotFoundException) + { + } + catch (FileNotFoundException) { } catch (Exception ex) diff --git a/MediaBrowser.Common.Implementations/Security/MBLicenseFile.cs b/MediaBrowser.Common.Implementations/Security/MBLicenseFile.cs index 8f3225f4e..63381efcd 100644 --- a/MediaBrowser.Common.Implementations/Security/MBLicenseFile.cs +++ b/MediaBrowser.Common.Implementations/Security/MBLicenseFile.cs @@ -101,6 +101,10 @@ namespace MediaBrowser.Common.Implementations.Security { contents = File.ReadAllLines(licenseFile); } + catch (DirectoryNotFoundException) + { + (File.Create(licenseFile)).Close(); + } catch (FileNotFoundException) { (File.Create(licenseFile)).Close(); diff --git a/MediaBrowser.Common/Extensions/BaseExtensions.cs b/MediaBrowser.Common/Extensions/BaseExtensions.cs index 8e96373f4..4c94f3aa2 100644 --- a/MediaBrowser.Common/Extensions/BaseExtensions.cs +++ b/MediaBrowser.Common/Extensions/BaseExtensions.cs @@ -1,4 +1,6 @@ using System; +using System.Globalization; +using System.Linq; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; @@ -54,6 +56,15 @@ namespace MediaBrowser.Common.Extensions return sb.ToString(); } + public static string RemoveDiacritics(this string text) + { + return String.Concat( + text.Normalize(NormalizationForm.FormD) + .Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) != + UnicodeCategory.NonSpacingMark) + ).Normalize(NormalizationForm.FormC); + } + /// /// Gets the M d5. /// diff --git a/MediaBrowser.Common/Plugins/BasePlugin.cs b/MediaBrowser.Common/Plugins/BasePlugin.cs index 1a536b4ff..ce068463e 100644 --- a/MediaBrowser.Common/Plugins/BasePlugin.cs +++ b/MediaBrowser.Common/Plugins/BasePlugin.cs @@ -204,6 +204,10 @@ namespace MediaBrowser.Common.Plugins { return (TConfigurationType)XmlSerializer.DeserializeFromFile(typeof(TConfigurationType), path); } + catch (DirectoryNotFoundException) + { + return (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType)); + } catch (FileNotFoundException) { return (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType)); diff --git a/MediaBrowser.Controller/Entities/TV/Episode.cs b/MediaBrowser.Controller/Entities/TV/Episode.cs index 6b6f07d4c..6b67cebc8 100644 --- a/MediaBrowser.Controller/Entities/TV/Episode.cs +++ b/MediaBrowser.Controller/Entities/TV/Episode.cs @@ -1,11 +1,11 @@ using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Users; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; -using MediaBrowser.Model.Users; namespace MediaBrowser.Controller.Entities.TV { @@ -178,6 +178,15 @@ namespace MediaBrowser.Controller.Entities.TV } } + [IgnoreDataMember] + public bool IsInSeasonFolder + { + get + { + return FindParent() != null; + } + } + [IgnoreDataMember] public string SeriesName { diff --git a/MediaBrowser.Controller/Entities/TV/Season.cs b/MediaBrowser.Controller/Entities/TV/Season.cs index ceddbbc3b..54db12b6f 100644 --- a/MediaBrowser.Controller/Entities/TV/Season.cs +++ b/MediaBrowser.Controller/Entities/TV/Season.cs @@ -1,13 +1,12 @@ -using MediaBrowser.Controller.Library; -using MediaBrowser.Controller.Localization; +using MediaBrowser.Controller.Localization; using MediaBrowser.Controller.Providers; -using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Querying; +using MediaBrowser.Model.Users; +using MoreLinq; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; -using MediaBrowser.Model.Users; namespace MediaBrowser.Controller.Entities.TV { @@ -156,24 +155,6 @@ namespace MediaBrowser.Controller.Entities.TV return IndexNumber != null ? IndexNumber.Value.ToString("0000") : Name; } - private IEnumerable GetEpisodes() - { - var series = Series; - - if (series != null && series.ContainsEpisodesWithoutSeasonFolders) - { - var seasonNumber = IndexNumber; - - if (seasonNumber.HasValue) - { - return series.RecursiveChildren.OfType() - .Where(i => i.ParentIndexNumber.HasValue && i.ParentIndexNumber.Value == seasonNumber.Value); - } - } - - return Children.OfType(); - } - [IgnoreDataMember] public bool IsMissingSeason { @@ -221,16 +202,32 @@ namespace MediaBrowser.Controller.Entities.TV var episodes = GetRecursiveChildren(user) .OfType(); - if (IndexNumber.HasValue) + var series = Series; + + if (IndexNumber.HasValue && series != null) { - var series = Series; + return series.GetEpisodes(user, IndexNumber.Value, includeMissingEpisodes, includeVirtualUnairedEpisodes, episodes); + } - if (series != null) + if (series != null && series.ContainsEpisodesWithoutSeasonFolders) + { + var seasonNumber = IndexNumber; + var list = episodes.ToList(); + + if (seasonNumber.HasValue) { - return series.GetEpisodes(user, IndexNumber.Value, includeMissingEpisodes, includeVirtualUnairedEpisodes, episodes); + list.AddRange(series.GetRecursiveChildren(user).OfType() + .Where(i => i.ParentIndexNumber.HasValue && i.ParentIndexNumber.Value == seasonNumber.Value)); + } + else + { + list.AddRange(series.GetRecursiveChildren(user).OfType() + .Where(i => !i.ParentIndexNumber.HasValue)); } - } + episodes = list.DistinctBy(i => i.Id); + } + if (!includeMissingEpisodes) { episodes = episodes.Where(i => !i.IsMissingEpisode); @@ -245,6 +242,33 @@ namespace MediaBrowser.Controller.Entities.TV .Cast(); } + private IEnumerable GetEpisodes() + { + var episodes = RecursiveChildren.OfType(); + var series = Series; + + if (series != null && series.ContainsEpisodesWithoutSeasonFolders) + { + var seasonNumber = IndexNumber; + var list = episodes.ToList(); + + if (seasonNumber.HasValue) + { + list.AddRange(series.RecursiveChildren.OfType() + .Where(i => i.ParentIndexNumber.HasValue && i.ParentIndexNumber.Value == seasonNumber.Value)); + } + else + { + list.AddRange(series.RecursiveChildren.OfType() + .Where(i => !i.ParentIndexNumber.HasValue)); + } + + episodes = list.DistinctBy(i => i.Id); + } + + return episodes; + } + public override IEnumerable GetChildren(User user, bool includeLinkedChildren) { return GetEpisodes(user); diff --git a/MediaBrowser.Controller/Library/IUserManager.cs b/MediaBrowser.Controller/Library/IUserManager.cs index f7fbc9c20..f5846973e 100644 --- a/MediaBrowser.Controller/Library/IUserManager.cs +++ b/MediaBrowser.Controller/Library/IUserManager.cs @@ -186,5 +186,12 @@ namespace MediaBrowser.Controller.Library /// The user identifier. /// The user policy. Task UpdateUserPolicy(string userId, UserPolicy userPolicy); + + /// + /// Makes the valid username. + /// + /// The username. + /// System.String. + string MakeValidUsername(string username); } } diff --git a/MediaBrowser.LocalMetadata/BaseXmlProvider.cs b/MediaBrowser.LocalMetadata/BaseXmlProvider.cs index 82e7809e8..74e3b61ca 100644 --- a/MediaBrowser.LocalMetadata/BaseXmlProvider.cs +++ b/MediaBrowser.LocalMetadata/BaseXmlProvider.cs @@ -28,8 +28,6 @@ namespace MediaBrowser.LocalMetadata var path = file.FullName; - //await XmlProviderUtils.XmlParsingResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); - try { result.Item = new T(); @@ -45,10 +43,6 @@ namespace MediaBrowser.LocalMetadata { result.HasMetadata = false; } - finally - { - //XmlProviderUtils.XmlParsingResourcePool.Release(); - } return result; } diff --git a/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs b/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs index 7c512840b..67c9123f5 100644 --- a/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs +++ b/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs @@ -612,6 +612,10 @@ namespace MediaBrowser.MediaEncoding.Subtitles catch (FileNotFoundException) { + } + catch (DirectoryNotFoundException) + { + } catch (IOException ex) { diff --git a/MediaBrowser.Model/ApiClient/IApiClient.cs b/MediaBrowser.Model/ApiClient/IApiClient.cs index dde6ca0b1..0181325fe 100644 --- a/MediaBrowser.Model/ApiClient/IApiClient.cs +++ b/MediaBrowser.Model/ApiClient/IApiClient.cs @@ -505,15 +505,6 @@ namespace MediaBrowser.Model.ApiClient /// The cancellation token. /// Task<PublicSystemInfo>. Task GetPublicSystemInfoAsync(CancellationToken cancellationToken = default(CancellationToken)); - - /// - /// Gets a person - /// - /// The name. - /// The user id. - /// Task{BaseItemDto}. - /// userId - Task GetPersonAsync(string name, string userId); /// /// Gets a list of plugins installed on the server @@ -967,15 +958,6 @@ namespace MediaBrowser.Model.ApiClient /// item string GetPersonImageUrl(BaseItemPerson item, ImageOptions options); - /// - /// Gets an image url that can be used to download an image from the api - /// - /// The name of the person - /// The options. - /// System.String. - /// name - string GetPersonImageUrl(string name, ImageOptions options); - /// /// Gets an image url that can be used to download an image from the api /// diff --git a/MediaBrowser.Providers/Movies/FanartMovieImageProvider.cs b/MediaBrowser.Providers/Movies/FanartMovieImageProvider.cs index a7ccf3f6e..6813f2ff5 100644 --- a/MediaBrowser.Providers/Movies/FanartMovieImageProvider.cs +++ b/MediaBrowser.Providers/Movies/FanartMovieImageProvider.cs @@ -106,6 +106,10 @@ namespace MediaBrowser.Providers.Movies { // No biggie. Don't blow up } + catch (DirectoryNotFoundException) + { + // No biggie. Don't blow up + } } var language = item.GetPreferredMetadataLanguage(); diff --git a/MediaBrowser.Providers/Music/FanArtAlbumProvider.cs b/MediaBrowser.Providers/Music/FanArtAlbumProvider.cs index 94d682f44..123ff9e29 100644 --- a/MediaBrowser.Providers/Music/FanArtAlbumProvider.cs +++ b/MediaBrowser.Providers/Music/FanArtAlbumProvider.cs @@ -82,6 +82,10 @@ namespace MediaBrowser.Providers.Music catch (FileNotFoundException) { + } + catch (DirectoryNotFoundException) + { + } } diff --git a/MediaBrowser.Providers/Music/FanArtArtistProvider.cs b/MediaBrowser.Providers/Music/FanArtArtistProvider.cs index a8df95fd1..6f633cfc8 100644 --- a/MediaBrowser.Providers/Music/FanArtArtistProvider.cs +++ b/MediaBrowser.Providers/Music/FanArtArtistProvider.cs @@ -90,6 +90,10 @@ namespace MediaBrowser.Providers.Music catch (FileNotFoundException) { + } + catch (DirectoryNotFoundException) + { + } } diff --git a/MediaBrowser.Providers/People/TvdbPersonImageProvider.cs b/MediaBrowser.Providers/People/TvdbPersonImageProvider.cs index 63d054664..253acc13f 100644 --- a/MediaBrowser.Providers/People/TvdbPersonImageProvider.cs +++ b/MediaBrowser.Providers/People/TvdbPersonImageProvider.cs @@ -83,6 +83,10 @@ namespace MediaBrowser.Providers.People { return null; } + catch (DirectoryNotFoundException) + { + return null; + } } private RemoteImageInfo GetImageInfo(string xmlFile, string personName, CancellationToken cancellationToken) diff --git a/MediaBrowser.Providers/TV/FanArtSeasonProvider.cs b/MediaBrowser.Providers/TV/FanArtSeasonProvider.cs index 05244af74..9f0cd4ff1 100644 --- a/MediaBrowser.Providers/TV/FanArtSeasonProvider.cs +++ b/MediaBrowser.Providers/TV/FanArtSeasonProvider.cs @@ -98,6 +98,10 @@ namespace MediaBrowser.Providers.TV { // No biggie. Don't blow up } + catch (DirectoryNotFoundException) + { + // No biggie. Don't blow up + } } } diff --git a/MediaBrowser.Providers/TV/FanartSeriesProvider.cs b/MediaBrowser.Providers/TV/FanartSeriesProvider.cs index afc71698b..8ba25e9f1 100644 --- a/MediaBrowser.Providers/TV/FanartSeriesProvider.cs +++ b/MediaBrowser.Providers/TV/FanartSeriesProvider.cs @@ -106,6 +106,10 @@ namespace MediaBrowser.Providers.TV { // No biggie. Don't blow up } + catch (DirectoryNotFoundException) + { + // No biggie. Don't blow up + } } var language = item.GetPreferredMetadataLanguage(); diff --git a/MediaBrowser.Providers/TV/MissingEpisodeProvider.cs b/MediaBrowser.Providers/TV/MissingEpisodeProvider.cs index 21d41ca00..0b52956de 100644 --- a/MediaBrowser.Providers/TV/MissingEpisodeProvider.cs +++ b/MediaBrowser.Providers/TV/MissingEpisodeProvider.cs @@ -2,6 +2,7 @@ using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Localization; using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; @@ -22,14 +23,16 @@ namespace MediaBrowser.Providers.TV private readonly IServerConfigurationManager _config; private readonly ILogger _logger; private readonly ILibraryManager _libraryManager; + private readonly ILocalizationManager _localization; private static readonly CultureInfo UsCulture = new CultureInfo("en-US"); - public MissingEpisodeProvider(ILogger logger, IServerConfigurationManager config, ILibraryManager libraryManager) + public MissingEpisodeProvider(ILogger logger, IServerConfigurationManager config, ILibraryManager libraryManager, ILocalizationManager localization) { _logger = logger; _config = config; _libraryManager = libraryManager; + _localization = localization; } public async Task Run(IEnumerable> series, CancellationToken cancellationToken) @@ -93,16 +96,16 @@ namespace MediaBrowser.Providers.TV var hasBadData = HasInvalidContent(group); - var anySeasonsRemoved = await RemoveObsoleteOrMissingSeasons(group, episodeLookup, false) + var anySeasonsRemoved = await RemoveObsoleteOrMissingSeasons(group, episodeLookup) .ConfigureAwait(false); - var anyEpisodesRemoved = await RemoveObsoleteOrMissingEpisodes(group, episodeLookup, false) + var anyEpisodesRemoved = await RemoveObsoleteOrMissingEpisodes(group, episodeLookup) .ConfigureAwait(false); var hasNewEpisodes = false; var hasNewSeasons = false; - foreach (var series in group.Where(s => s.ContainsEpisodesWithoutSeasonFolders)) + foreach (var series in group) { hasNewSeasons = await AddDummySeasonFolders(series, cancellationToken).ConfigureAwait(false); } @@ -165,14 +168,15 @@ namespace MediaBrowser.Providers.TV /// private async Task AddDummySeasonFolders(Series series, CancellationToken cancellationToken) { - var existingEpisodes = series.RecursiveChildren + var episodesInSeriesFolder = series.RecursiveChildren .OfType() + .Where(i => !i.IsInSeasonFolder) .ToList(); var hasChanges = false; // Loop through the unique season numbers - foreach (var seasonNumber in existingEpisodes.Select(i => i.ParentIndexNumber ?? -1) + foreach (var seasonNumber in episodesInSeriesFolder.Select(i => i.ParentIndexNumber ?? -1) .Where(i => i >= 0) .Distinct() .ToList()) @@ -188,6 +192,20 @@ namespace MediaBrowser.Providers.TV } } + // Unknown season - create a dummy season to put these under + if (episodesInSeriesFolder.Any(i => !i.ParentIndexNumber.HasValue)) + { + var hasSeason = series.Children.OfType() + .Any(i => !i.IndexNumber.HasValue); + + if (!hasSeason) + { + await AddSeason(series, null, cancellationToken).ConfigureAwait(false); + + hasChanges = true; + } + } + return hasChanges; } @@ -292,8 +310,7 @@ namespace MediaBrowser.Providers.TV /// Removes the virtual entry after a corresponding physical version has been added /// private async Task RemoveObsoleteOrMissingEpisodes(IEnumerable series, - IEnumerable> episodeLookup, - bool forceRemoveAll) + IEnumerable> episodeLookup) { var existingEpisodes = (from s in series let seasonOffset = TvdbSeriesProvider.GetSeriesOffset(s.ProviderIds) ?? ((s.AnimeSeriesIndex ?? 1) - 1) @@ -312,11 +329,6 @@ namespace MediaBrowser.Providers.TV var episodesToRemove = virtualEpisodes .Where(i => { - if (forceRemoveAll) - { - return true; - } - if (i.Episode.IndexNumber.HasValue && i.Episode.ParentIndexNumber.HasValue) { var seasonNumber = i.Episode.ParentIndexNumber.Value + i.SeasonOffset; @@ -362,11 +374,9 @@ namespace MediaBrowser.Providers.TV /// /// The series. /// The episode lookup. - /// if set to true [force remove all]. /// Task{System.Boolean}. private async Task RemoveObsoleteOrMissingSeasons(IEnumerable series, - IEnumerable> episodeLookup, - bool forceRemoveAll) + IEnumerable> episodeLookup) { var existingSeasons = (from s in series let seasonOffset = TvdbSeriesProvider.GetSeriesOffset(s.ProviderIds) ?? ((s.AnimeSeriesIndex ?? 1) - 1) @@ -385,11 +395,6 @@ namespace MediaBrowser.Providers.TV var seasonsToRemove = virtualSeasons .Where(i => { - if (forceRemoveAll) - { - return true; - } - if (i.Season.IndexNumber.HasValue) { var seasonNumber = i.Season.IndexNumber.Value + i.SeasonOffset; @@ -409,7 +414,9 @@ namespace MediaBrowser.Providers.TV return false; } - return true; + // Season does not have a number + // Remove if there are no episodes directly in series without a season number + return i.Season.Series.RecursiveChildren.OfType().All(s => s.ParentIndexNumber.HasValue || s.IsInSeasonFolder); }) .ToList(); @@ -472,20 +479,22 @@ namespace MediaBrowser.Providers.TV /// The cancellation token. /// Task{Season}. private async Task AddSeason(Series series, - int seasonNumber, + int? seasonNumber, CancellationToken cancellationToken) { - _logger.Info("Creating Season {0} entry for {1}", seasonNumber, series.Name); + var seasonName = seasonNumber == 0 ? + _config.Configuration.SeasonZeroDisplayName : + (seasonNumber.HasValue ? string.Format(_localization.GetLocalizedString("NameSeasonNumber"), seasonNumber.Value.ToString(UsCulture)) : _localization.GetLocalizedString("NameSeasonUnknown")); - var name = seasonNumber == 0 ? _config.Configuration.SeasonZeroDisplayName : string.Format("Season {0}", seasonNumber.ToString(UsCulture)); + _logger.Info("Creating Season {0} entry for {1}", seasonName, series.Name); var season = new Season { - Name = name, + Name = seasonName, IndexNumber = seasonNumber, Parent = series, DisplayMediaType = typeof(Season).Name, - Id = (series.Id + seasonNumber.ToString(UsCulture) + name).GetMBId(typeof(Season)) + Id = (series.Id + (seasonNumber ?? -1).ToString(UsCulture) + seasonName).GetMBId(typeof(Season)) }; await series.AddChild(season, cancellationToken).ConfigureAwait(false); diff --git a/MediaBrowser.Providers/TV/SeriesPostScanTask.cs b/MediaBrowser.Providers/TV/SeriesPostScanTask.cs index d350d2fe4..e1986a7c1 100644 --- a/MediaBrowser.Providers/TV/SeriesPostScanTask.cs +++ b/MediaBrowser.Providers/TV/SeriesPostScanTask.cs @@ -1,11 +1,12 @@ -using System.Collections.Generic; -using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Localization; using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; using System; +using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -25,12 +26,14 @@ namespace MediaBrowser.Providers.TV private readonly ILibraryManager _libraryManager; private readonly IServerConfigurationManager _config; private readonly ILogger _logger; + private readonly ILocalizationManager _localization; - public SeriesPostScanTask(ILibraryManager libraryManager, ILogger logger, IServerConfigurationManager config) + public SeriesPostScanTask(ILibraryManager libraryManager, ILogger logger, IServerConfigurationManager config, ILocalizationManager localization) { _libraryManager = libraryManager; _logger = logger; _config = config; + _localization = localization; } public Task Run(IProgress progress, CancellationToken cancellationToken) @@ -47,7 +50,7 @@ namespace MediaBrowser.Providers.TV var seriesGroups = FindSeriesGroups(seriesList).Where(g => !string.IsNullOrEmpty(g.Key)).ToList(); - await new MissingEpisodeProvider(_logger, _config, _libraryManager).Run(seriesGroups, cancellationToken).ConfigureAwait(false); + await new MissingEpisodeProvider(_logger, _config, _libraryManager, _localization).Run(seriesGroups, cancellationToken).ConfigureAwait(false); var numComplete = 0; diff --git a/MediaBrowser.Providers/TV/TvdbEpisodeProvider.cs b/MediaBrowser.Providers/TV/TvdbEpisodeProvider.cs index ef9f5427c..52c1ab7dd 100644 --- a/MediaBrowser.Providers/TV/TvdbEpisodeProvider.cs +++ b/MediaBrowser.Providers/TV/TvdbEpisodeProvider.cs @@ -72,6 +72,10 @@ namespace MediaBrowser.Providers.TV { // Don't fail the provider because this will just keep on going and going. } + catch (DirectoryNotFoundException) + { + // Don't fail the provider because this will just keep on going and going. + } } return list; @@ -101,6 +105,10 @@ namespace MediaBrowser.Providers.TV { // Don't fail the provider because this will just keep on going and going. } + catch (DirectoryNotFoundException) + { + // Don't fail the provider because this will just keep on going and going. + } } return result; @@ -208,8 +216,9 @@ namespace MediaBrowser.Providers.TV /// Fetches the episode data. /// /// The identifier. + /// The identity. /// The series data path. - /// + /// The series provider ids. /// The cancellation token. /// Task{System.Boolean}. private Episode FetchEpisodeData(EpisodeInfo id, EpisodeIdentity identity, string seriesDataPath, Dictionary seriesProviderIds, CancellationToken cancellationToken) @@ -279,6 +288,10 @@ namespace MediaBrowser.Providers.TV { break; } + catch (DirectoryNotFoundException) + { + break; + } episodeNumber++; } diff --git a/MediaBrowser.Providers/TV/TvdbSeasonImageProvider.cs b/MediaBrowser.Providers/TV/TvdbSeasonImageProvider.cs index efafeae96..1ebd7bed5 100644 --- a/MediaBrowser.Providers/TV/TvdbSeasonImageProvider.cs +++ b/MediaBrowser.Providers/TV/TvdbSeasonImageProvider.cs @@ -94,6 +94,10 @@ namespace MediaBrowser.Providers.TV { // No tvdb data yet. Don't blow up } + catch (DirectoryNotFoundException) + { + // No tvdb data yet. Don't blow up + } } return new RemoteImageInfo[] { }; diff --git a/MediaBrowser.Providers/TV/TvdbSeriesImageProvider.cs b/MediaBrowser.Providers/TV/TvdbSeriesImageProvider.cs index 9cc09c40c..08913d3b4 100644 --- a/MediaBrowser.Providers/TV/TvdbSeriesImageProvider.cs +++ b/MediaBrowser.Providers/TV/TvdbSeriesImageProvider.cs @@ -87,6 +87,10 @@ namespace MediaBrowser.Providers.TV { // No tvdb data yet. Don't blow up } + catch (DirectoryNotFoundException) + { + // No tvdb data yet. Don't blow up + } } return new RemoteImageInfo[] { }; diff --git a/MediaBrowser.Server.Implementations/Connect/ConnectManager.cs b/MediaBrowser.Server.Implementations/Connect/ConnectManager.cs index 376dc3548..67d844543 100644 --- a/MediaBrowser.Server.Implementations/Connect/ConnectManager.cs +++ b/MediaBrowser.Server.Implementations/Connect/ConnectManager.cs @@ -1,4 +1,5 @@ using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Net; using MediaBrowser.Controller; using MediaBrowser.Controller.Configuration; @@ -789,7 +790,7 @@ namespace MediaBrowser.Server.Implementations.Connect if (user == null) { // Add user - user = await _userManager.CreateUser(connectEntry.UserName).ConfigureAwait(false); + user = await _userManager.CreateUser(_userManager.MakeValidUsername(connectEntry.UserName)).ConfigureAwait(false); user.ConnectUserName = connectEntry.UserName; user.ConnectUserId = connectEntry.UserId; diff --git a/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs b/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs index b141fea1e..967c78c50 100644 --- a/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs +++ b/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs @@ -78,6 +78,11 @@ namespace MediaBrowser.Server.Implementations.Drawing // No biggie sizeDictionary = new Dictionary(); } + catch (DirectoryNotFoundException) + { + // No biggie + sizeDictionary = new Dictionary(); + } catch (Exception ex) { logger.ErrorException("Error parsing image size cache file", ex); diff --git a/MediaBrowser.Server.Implementations/FileOrganization/EpisodeFileOrganizer.cs b/MediaBrowser.Server.Implementations/FileOrganization/EpisodeFileOrganizer.cs index 3b5e34520..432ea1f69 100644 --- a/MediaBrowser.Server.Implementations/FileOrganization/EpisodeFileOrganizer.cs +++ b/MediaBrowser.Server.Implementations/FileOrganization/EpisodeFileOrganizer.cs @@ -459,11 +459,11 @@ namespace MediaBrowser.Server.Implementations.FileOrganization private bool IsSameEpisode(string sourcePath, string newPath) { - var sourceFileInfo = new FileInfo(sourcePath); - var destinationFileInfo = new FileInfo(newPath); - try { + var sourceFileInfo = new FileInfo(sourcePath); + var destinationFileInfo = new FileInfo(newPath); + if (sourceFileInfo.Length == destinationFileInfo.Length) { return true; @@ -473,6 +473,10 @@ namespace MediaBrowser.Server.Implementations.FileOrganization { return false; } + catch (DirectoryNotFoundException) + { + return false; + } return false; } diff --git a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs index d52288f87..66125784c 100644 --- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs +++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs @@ -1755,9 +1755,12 @@ namespace MediaBrowser.Server.Implementations.Library var resolver = new EpisodeResolver(new ExtendedNamingOptions(), new Naming.Logging.NullLogger()); + var fileType = episode.VideoType == VideoType.BluRay || episode.VideoType == VideoType.Dvd || episode.VideoType == VideoType.HdDvd ? + FileInfoType.Directory : + FileInfoType.File; + var locationType = episode.LocationType; - var fileType = /*args.IsDirectory ? FileInfoType.Directory :*/ FileInfoType.File; var episodeInfo = locationType == LocationType.FileSystem || locationType == LocationType.Offline ? resolver.Resolve(episode.Path, fileType) : new Naming.TV.EpisodeInfo(); @@ -1769,29 +1772,42 @@ namespace MediaBrowser.Server.Implementations.Library var changed = false; - if (!episode.IndexNumber.HasValue) + if (episodeInfo.IsByDate) { - episode.IndexNumber = episodeInfo.EpisodeNumber; - if (episode.IndexNumber.HasValue) { + episode.IndexNumber = null; changed = true; } - } - - if (!episode.IndexNumberEnd.HasValue) - { - episode.IndexNumberEnd = episodeInfo.EndingEpsiodeNumber; if (episode.IndexNumberEnd.HasValue) { + episode.IndexNumberEnd = null; changed = true; } - } - if (!episode.ParentIndexNumber.HasValue) - { - episode.ParentIndexNumber = episodeInfo.SeasonNumber; + if (!episode.PremiereDate.HasValue) + { + if (episodeInfo.Year.HasValue && episodeInfo.Month.HasValue && episodeInfo.Day.HasValue) + { + episode.PremiereDate = new DateTime(episodeInfo.Year.Value, episodeInfo.Month.Value, episodeInfo.Day.Value).ToUniversalTime(); + } + + if (episode.PremiereDate.HasValue) + { + changed = true; + } + } + + if (!episode.ProductionYear.HasValue) + { + episode.ProductionYear = episodeInfo.Year; + + if (episode.ProductionYear.HasValue) + { + changed = true; + } + } if (!episode.ParentIndexNumber.HasValue) { @@ -1801,11 +1817,53 @@ namespace MediaBrowser.Server.Implementations.Library { episode.ParentIndexNumber = season.IndexNumber; } + + if (episode.ParentIndexNumber.HasValue) + { + changed = true; + } + } + } + else + { + if (!episode.IndexNumber.HasValue) + { + episode.IndexNumber = episodeInfo.EpisodeNumber; + + if (episode.IndexNumber.HasValue) + { + changed = true; + } } - if (episode.ParentIndexNumber.HasValue) + if (!episode.IndexNumberEnd.HasValue) { - changed = true; + episode.IndexNumberEnd = episodeInfo.EndingEpsiodeNumber; + + if (episode.IndexNumberEnd.HasValue) + { + changed = true; + } + } + + if (!episode.ParentIndexNumber.HasValue) + { + episode.ParentIndexNumber = episodeInfo.SeasonNumber; + + if (!episode.ParentIndexNumber.HasValue) + { + var season = episode.Season; + + if (season != null) + { + episode.ParentIndexNumber = season.IndexNumber; + } + } + + if (episode.ParentIndexNumber.HasValue) + { + changed = true; + } } } diff --git a/MediaBrowser.Server.Implementations/Library/Resolvers/TV/EpisodeResolver.cs b/MediaBrowser.Server.Implementations/Library/Resolvers/TV/EpisodeResolver.cs index 39b0a93cc..1a873f01e 100644 --- a/MediaBrowser.Server.Implementations/Library/Resolvers/TV/EpisodeResolver.cs +++ b/MediaBrowser.Server.Implementations/Library/Resolvers/TV/EpisodeResolver.cs @@ -1,7 +1,5 @@ using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Library; -using MediaBrowser.Naming.Common; -using MediaBrowser.Naming.IO; using System.Linq; namespace MediaBrowser.Server.Implementations.Library.Resolvers.TV diff --git a/MediaBrowser.Server.Implementations/Library/UserManager.cs b/MediaBrowser.Server.Implementations/Library/UserManager.cs index 1d58ad074..02d7c1be1 100644 --- a/MediaBrowser.Server.Implementations/Library/UserManager.cs +++ b/MediaBrowser.Server.Implementations/Library/UserManager.cs @@ -171,6 +171,38 @@ namespace MediaBrowser.Server.Implementations.Library return AuthenticateUser(username, passwordSha1, null, remoteEndPoint); } + public bool IsValidUsername(string username) + { + // Usernames can contain letters (a-z), numbers (0-9), dashes (-), underscores (_), apostrophes ('), and periods (.) + return username.All(IsValidCharacter); + } + + private bool IsValidCharacter(char i) + { + return char.IsLetterOrDigit(i) || char.Equals(i, '-') || char.Equals(i, '_') || char.Equals(i, '\'') || + char.Equals(i, '.'); + } + + public string MakeValidUsername(string username) + { + if (IsValidUsername(username)) + { + return username; + } + + // Usernames can contain letters (a-z), numbers (0-9), dashes (-), underscores (_), apostrophes ('), and periods (.) + var builder = new StringBuilder(); + + foreach (var c in username) + { + if (IsValidCharacter(c)) + { + builder.Append(c); + } + } + return builder.ToString(); + } + public async Task AuthenticateUser(string username, string passwordSha1, string passwordMd5, string remoteEndPoint) { if (string.IsNullOrWhiteSpace(username)) @@ -178,7 +210,8 @@ namespace MediaBrowser.Server.Implementations.Library throw new ArgumentNullException("username"); } - var user = Users.FirstOrDefault(i => string.Equals(username, i.Name, StringComparison.OrdinalIgnoreCase)); + var user = Users + .FirstOrDefault(i => string.Equals(username, i.Name, StringComparison.OrdinalIgnoreCase)); if (user == null) { @@ -203,20 +236,6 @@ namespace MediaBrowser.Server.Implementations.Library } } - // Maybe user accidently entered connect credentials. let's be flexible - if (!success && user.ConnectLinkType.HasValue && !string.IsNullOrWhiteSpace(passwordMd5)) - { - try - { - await _connectFactory().Authenticate(user.ConnectUserName, passwordMd5).ConfigureAwait(false); - success = true; - } - catch - { - - } - } - // Update LastActivityDate and LastLoginDate, then save if (success) { @@ -273,7 +292,7 @@ namespace MediaBrowser.Server.Implementations.Library // There always has to be at least one user. if (users.Count == 0) { - var name = Environment.UserName; + var name = MakeValidUsername(Environment.UserName); var user = InstantiateNewUser(name, false); @@ -477,6 +496,11 @@ namespace MediaBrowser.Server.Implementations.Library throw new ArgumentNullException("name"); } + if (!IsValidUsername(name)) + { + throw new ArgumentException("Only alphanumeric characters are allowed."); + } + if (Users.Any(u => u.Name.Equals(name, StringComparison.OrdinalIgnoreCase))) { throw new ArgumentException(string.Format("A user with the name '{0}' already exists.", name)); @@ -803,6 +827,10 @@ namespace MediaBrowser.Server.Implementations.Library return (UserPolicy)_xmlSerializer.DeserializeFromFile(typeof(UserPolicy), path); } } + catch (DirectoryNotFoundException) + { + return GetDefaultPolicy(user); + } catch (FileNotFoundException) { return GetDefaultPolicy(user); @@ -840,6 +868,8 @@ namespace MediaBrowser.Server.Implementations.Library var path = GetPolifyFilePath(user); + Directory.CreateDirectory(Path.GetDirectoryName(path)); + lock (_policySyncLock) { _xmlSerializer.SerializeToFile(userPolicy, path); @@ -900,6 +930,10 @@ namespace MediaBrowser.Server.Implementations.Library return (UserConfiguration)_xmlSerializer.DeserializeFromFile(typeof(UserConfiguration), path); } } + catch (DirectoryNotFoundException) + { + return new UserConfiguration(); + } catch (FileNotFoundException) { return new UserConfiguration(); @@ -930,6 +964,8 @@ namespace MediaBrowser.Server.Implementations.Library config = _jsonSerializer.DeserializeFromString(json); } + Directory.CreateDirectory(Path.GetDirectoryName(path)); + lock (_configSyncLock) { _xmlSerializer.SerializeToFile(config, path); diff --git a/MediaBrowser.Server.Implementations/Localization/JavaScript/javascript.json b/MediaBrowser.Server.Implementations/Localization/JavaScript/javascript.json index 695200239..827a2388d 100644 --- a/MediaBrowser.Server.Implementations/Localization/JavaScript/javascript.json +++ b/MediaBrowser.Server.Implementations/Localization/JavaScript/javascript.json @@ -419,7 +419,7 @@ "HeaderMediaLocations": "Media Locations", "LabelFolderTypeValue": "Folder type: {0}", "LabelPathSubstitutionHelp": "Optional: Path substitution can map server paths to network shares that clients can access for direct playback.", - "FolderTypeMixed": "Mixed videos", + "FolderTypeMixed": "Mixed content", "FolderTypeMovies": "Movies", "FolderTypeMusic": "Music", "FolderTypeAdultVideos": "Adult videos", @@ -658,5 +658,5 @@ "LabelItemLimitHelp": "Optional. Set a limit to the number of items that will be synced.", "MessageBookPluginRequired": "Requires installation of the Bookshelf plugin", "MessageGamePluginRequired": "Requires installation of the GameBrowser plugin", - "MessageMixedContentHelp": "Content will be displayed with as a plain folder structure" + "MessageMixedContentHelp": "Content will be displayed as a plain folder structure" } diff --git a/MediaBrowser.Server.Implementations/Localization/Server/server.json b/MediaBrowser.Server.Implementations/Localization/Server/server.json index 9a515d492..c0af13950 100644 --- a/MediaBrowser.Server.Implementations/Localization/Server/server.json +++ b/MediaBrowser.Server.Implementations/Localization/Server/server.json @@ -37,7 +37,7 @@ "ButtonOk": "Ok", "ButtonCancel": "Cancel", "ButtonNew": "New", - "FolderTypeMixed": "Mixed videos", + "FolderTypeMixed": "Mixed content", "FolderTypeMovies": "Movies", "FolderTypeMusic": "Music", "FolderTypeAdultVideos": "Adult videos", @@ -48,7 +48,7 @@ "FolderTypeBooks": "Books", "FolderTypeTvShows": "TV", "FolderTypeInherit": "Inherit", - "LabelContentType": "Content type:", + "LabelContentType": "Content type:", "HeaderSetupLibrary": "Setup your media library", "ButtonAddMediaFolder": "Add media folder", "LabelFolderType": "Folder type:", @@ -1307,5 +1307,8 @@ "LabelEnableSingleImageInDidlLimitHelp": "Some devices will not render properly if multiple images are embedded within Didl.", "TabActivity": "Activity", "TitleSync": "Sync", - "OptionAllowSyncContent": "Allow syncing media to devices" + "OptionAllowSyncContent": "Allow syncing media to devices", + "NameSeasonUnknown": "Season Unknown", + "NameSeasonNumber": "Season {0}", + "LabelNewUserNameHelp": "Usernames can contain letters (a-z), numbers (0-9), dashes (-), underscores (_), apostrophes ('), and periods (.)" } diff --git a/MediaBrowser.Server.Implementations/News/NewsService.cs b/MediaBrowser.Server.Implementations/News/NewsService.cs index 9eeadfab7..684363d01 100644 --- a/MediaBrowser.Server.Implementations/News/NewsService.cs +++ b/MediaBrowser.Server.Implementations/News/NewsService.cs @@ -26,6 +26,14 @@ namespace MediaBrowser.Server.Implementations.News { return GetProductNewsInternal(query); } + catch (DirectoryNotFoundException) + { + // No biggie + return new QueryResult + { + Items = new NewsItem[] { } + }; + } catch (FileNotFoundException) { // No biggie diff --git a/MediaBrowser.XbmcMetadata/Savers/BaseNfoSaver.cs b/MediaBrowser.XbmcMetadata/Savers/BaseNfoSaver.cs index 86e92530f..0f1d53ea6 100644 --- a/MediaBrowser.XbmcMetadata/Savers/BaseNfoSaver.cs +++ b/MediaBrowser.XbmcMetadata/Savers/BaseNfoSaver.cs @@ -256,6 +256,10 @@ namespace MediaBrowser.XbmcMetadata.Savers catch (FileNotFoundException) { + } + catch (DirectoryNotFoundException) + { + } writer.WriteEndElement(); -- cgit v1.2.3 From c93740461e5cef99deb378e587b75cf74950b94e Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 2 Jan 2015 00:36:27 -0500 Subject: support audio sync transcoding --- MediaBrowser.Api/ApiEntryPoint.cs | 5 ++- MediaBrowser.Api/Playback/BaseStreamingService.cs | 4 +- MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs | 5 ++- MediaBrowser.Api/Playback/Hls/VideoHlsService.cs | 3 +- .../MediaBrowser.Controller.csproj | 1 + .../MediaEncoding/IMediaEncoder.cs | 11 ++++++ .../Savers/XmlSaverHelpers.cs | 3 +- MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs | 45 +++++++++++++++++++++- .../MediaBrowser.MediaEncoding.csproj | 5 +++ MediaBrowser.Model/Dlna/StreamBuilder.cs | 6 ++- MediaBrowser.Model/Dlna/StreamInfo.cs | 1 + MediaBrowser.Model/Sync/LocalItem.cs | 2 +- .../Devices/DeviceManager.cs | 2 +- .../EntryPoints/UsageEntryPoint.cs | 6 ++- .../EntryPoints/UsageReporter.cs | 14 ++++++- .../Library/LibraryManager.cs | 14 +++---- .../Localization/Server/server.json | 1 + .../Sync/AppSyncProvider.cs | 4 +- .../Sync/SyncJobProcessor.cs | 31 +++++++++------ .../Sync/SyncManager.cs | 11 ++++-- .../Sync/SyncScheduledTask.cs | 7 +++- .../ApplicationHost.cs | 21 +++++++--- 22 files changed, 156 insertions(+), 46 deletions(-) (limited to 'MediaBrowser.MediaEncoding') diff --git a/MediaBrowser.Api/ApiEntryPoint.cs b/MediaBrowser.Api/ApiEntryPoint.cs index a05d7d1b2..0eb92d5a7 100644 --- a/MediaBrowser.Api/ApiEntryPoint.cs +++ b/MediaBrowser.Api/ApiEntryPoint.cs @@ -4,6 +4,7 @@ using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Plugins; using MediaBrowser.Controller.Session; using MediaBrowser.Model.Configuration; +using MediaBrowser.Model.Dlna; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Session; using System; @@ -85,7 +86,9 @@ namespace MediaBrowser.Api /// private void DeleteEncodedMediaCache() { - foreach (var file in Directory.EnumerateFiles(_config.ApplicationPaths.TranscodingTempPath, "*", SearchOption.AllDirectories) + var path = Path.Combine(_config.ApplicationPaths.TranscodingTempPath, EncodingContext.Streaming.ToString().ToLower()); + + foreach (var file in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories) .ToList()) { File.Delete(file); diff --git a/MediaBrowser.Api/Playback/BaseStreamingService.cs b/MediaBrowser.Api/Playback/BaseStreamingService.cs index 03106b6e7..0858a0347 100644 --- a/MediaBrowser.Api/Playback/BaseStreamingService.cs +++ b/MediaBrowser.Api/Playback/BaseStreamingService.cs @@ -119,8 +119,8 @@ namespace MediaBrowser.Api.Playback /// System.String. private string GetOutputFilePath(StreamState state) { - var folder = ServerConfigurationManager.ApplicationPaths.TranscodingTempPath; - + var folder = Path.Combine(ServerConfigurationManager.ApplicationPaths.TranscodingTempPath, EncodingContext.Streaming.ToString().ToLower()); + var outputFileExtension = GetOutputFileExtension(state); var data = GetCommandLineArguments("dummy\\dummy", "dummyTranscodingId", state, false); diff --git a/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs b/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs index 2263a2b37..14045b3a5 100644 --- a/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs +++ b/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs @@ -1,4 +1,5 @@ using MediaBrowser.Controller; +using MediaBrowser.Model.Dlna; using ServiceStack; using System; using System.IO; @@ -65,7 +66,7 @@ namespace MediaBrowser.Api.Playback.Hls { var file = request.PlaylistId + Path.GetExtension(Request.PathInfo); - file = Path.Combine(_appPaths.TranscodingTempPath, file); + file = Path.Combine(_appPaths.TranscodingTempPath, EncodingContext.Streaming.ToString().ToLower(), file); return ResultFactory.GetStaticFileResult(Request, file, FileShare.ReadWrite); } @@ -84,7 +85,7 @@ namespace MediaBrowser.Api.Playback.Hls { var file = request.SegmentId + Path.GetExtension(Request.PathInfo); - file = Path.Combine(_appPaths.TranscodingTempPath, file); + file = Path.Combine(_appPaths.TranscodingTempPath, EncodingContext.Streaming.ToString().ToLower(), file); return ResultFactory.GetStaticFileResult(Request, file, FileShare.ReadWrite); } diff --git a/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs b/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs index de845c88d..d786b51b3 100644 --- a/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs @@ -5,6 +5,7 @@ using MediaBrowser.Controller.Dlna; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.LiveTv; using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Model.Dlna; using MediaBrowser.Model.IO; using ServiceStack; using System; @@ -70,7 +71,7 @@ namespace MediaBrowser.Api.Playback.Hls { var file = request.SegmentId + Path.GetExtension(Request.PathInfo); - file = Path.Combine(ServerConfigurationManager.ApplicationPaths.TranscodingTempPath, file); + file = Path.Combine(ServerConfigurationManager.ApplicationPaths.TranscodingTempPath, EncodingContext.Streaming.ToString().ToLower(), file); return ResultFactory.GetStaticFileResult(Request, file); } diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index 3ed87ced8..1d16db62c 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -199,6 +199,7 @@ + diff --git a/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs b/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs index 38c2c83c4..8f56bfda5 100644 --- a/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs +++ b/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs @@ -96,5 +96,16 @@ namespace MediaBrowser.Controller.MediaEncoding /// The ticks. /// System.String. string GetTimeParameter(long ticks); + + /// + /// Encodes the audio. + /// + /// The options. + /// The progress. + /// The cancellation token. + /// Task. + Task EncodeAudio(EncodingJobOptions options, + IProgress progress, + CancellationToken cancellationToken); } } diff --git a/MediaBrowser.LocalMetadata/Savers/XmlSaverHelpers.cs b/MediaBrowser.LocalMetadata/Savers/XmlSaverHelpers.cs index 4388ec964..b5b9278cc 100644 --- a/MediaBrowser.LocalMetadata/Savers/XmlSaverHelpers.cs +++ b/MediaBrowser.LocalMetadata/Savers/XmlSaverHelpers.cs @@ -112,7 +112,8 @@ namespace MediaBrowser.LocalMetadata.Savers "Website", "Zap2ItId", "CollectionItems", - "PlaylistItems" + "PlaylistItems", + "Shares" }.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase); diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs index 7fb27e25f..a7d8b6f1d 100644 --- a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs +++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs @@ -1,10 +1,16 @@ +using MediaBrowser.Common.IO; +using MediaBrowser.Controller.Channels; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.LiveTv; using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Controller.Session; using MediaBrowser.Model.Entities; +using MediaBrowser.Model.IO; using MediaBrowser.Model.Logging; using MediaBrowser.Model.MediaInfo; using MediaBrowser.Model.Serialization; using System; -using System.Collections.Concurrent; using System.Diagnostics; using System.Globalization; using System.IO; @@ -51,11 +57,26 @@ namespace MediaBrowser.MediaEncoding.Encoder public string Version { get; private set; } - public MediaEncoder(ILogger logger, IJsonSerializer jsonSerializer, string ffMpegPath, string ffProbePath, string version) + protected readonly IServerConfigurationManager ConfigurationManager; + protected readonly IFileSystem FileSystem; + protected readonly ILiveTvManager LiveTvManager; + protected readonly IIsoManager IsoManager; + protected readonly ILibraryManager LibraryManager; + protected readonly IChannelManager ChannelManager; + protected readonly ISessionManager SessionManager; + + public MediaEncoder(ILogger logger, IJsonSerializer jsonSerializer, string ffMpegPath, string ffProbePath, string version, IServerConfigurationManager configurationManager, IFileSystem fileSystem, ILiveTvManager liveTvManager, IIsoManager isoManager, ILibraryManager libraryManager, IChannelManager channelManager, ISessionManager sessionManager) { _logger = logger; _jsonSerializer = jsonSerializer; Version = version; + ConfigurationManager = configurationManager; + FileSystem = fileSystem; + LiveTvManager = liveTvManager; + IsoManager = isoManager; + LibraryManager = libraryManager; + ChannelManager = channelManager; + SessionManager = sessionManager; FFProbePath = ffProbePath; FFMpegPath = ffMpegPath; } @@ -511,5 +532,25 @@ namespace MediaBrowser.MediaEncoding.Encoder throw new ApplicationException(msg); } } + + public async Task EncodeAudio(EncodingJobOptions options, + IProgress progress, + CancellationToken cancellationToken) + { + var job = await new AudioEncoder(this, + _logger, + ConfigurationManager, + FileSystem, + LiveTvManager, + IsoManager, + LibraryManager, + ChannelManager, + SessionManager) + .Start(options, progress, cancellationToken).ConfigureAwait(false); + + await job.TaskCompletionSource.Task.ConfigureAwait(false); + + return job.OutputFilePath; + } } } diff --git a/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj b/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj index 5c472ebc8..9daa3319f 100644 --- a/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj +++ b/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj @@ -57,7 +57,12 @@ + + + + + diff --git a/MediaBrowser.Model/Dlna/StreamBuilder.cs b/MediaBrowser.Model/Dlna/StreamBuilder.cs index 208ea1420..7c47b0d44 100644 --- a/MediaBrowser.Model/Dlna/StreamBuilder.cs +++ b/MediaBrowser.Model/Dlna/StreamBuilder.cs @@ -109,7 +109,8 @@ namespace MediaBrowser.Model.Dlna ItemId = options.ItemId, MediaType = DlnaProfileType.Audio, MediaSource = item, - RunTimeTicks = item.RunTimeTicks + RunTimeTicks = item.RunTimeTicks, + Context = options.Context }; int? maxBitrateSetting = options.GetMaxBitrate(); @@ -240,7 +241,8 @@ namespace MediaBrowser.Model.Dlna ItemId = options.ItemId, MediaType = DlnaProfileType.Video, MediaSource = item, - RunTimeTicks = item.RunTimeTicks + RunTimeTicks = item.RunTimeTicks, + Context = options.Context }; int? audioStreamIndex = options.AudioStreamIndex ?? item.DefaultAudioStreamIndex; diff --git a/MediaBrowser.Model/Dlna/StreamInfo.cs b/MediaBrowser.Model/Dlna/StreamInfo.cs index 703c73566..22eb0cf6c 100644 --- a/MediaBrowser.Model/Dlna/StreamInfo.cs +++ b/MediaBrowser.Model/Dlna/StreamInfo.cs @@ -18,6 +18,7 @@ namespace MediaBrowser.Model.Dlna public string ItemId { get; set; } public PlayMethod PlayMethod { get; set; } + public EncodingContext Context { get; set; } public DlnaProfileType MediaType { get; set; } diff --git a/MediaBrowser.Model/Sync/LocalItem.cs b/MediaBrowser.Model/Sync/LocalItem.cs index 51faaff90..ec4544524 100644 --- a/MediaBrowser.Model/Sync/LocalItem.cs +++ b/MediaBrowser.Model/Sync/LocalItem.cs @@ -23,7 +23,7 @@ namespace MediaBrowser.Model.Sync /// Gets or sets the unique identifier. /// /// The unique identifier. - public string UniqueId { get; set; } + public string Id { get; set; } /// /// Gets or sets the item identifier. /// diff --git a/MediaBrowser.Server.Implementations/Devices/DeviceManager.cs b/MediaBrowser.Server.Implementations/Devices/DeviceManager.cs index 99fa40789..ddd5ef58d 100644 --- a/MediaBrowser.Server.Implementations/Devices/DeviceManager.cs +++ b/MediaBrowser.Server.Implementations/Devices/DeviceManager.cs @@ -9,12 +9,12 @@ using MediaBrowser.Model.Extensions; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Querying; using MediaBrowser.Model.Session; +using MediaBrowser.Model.Users; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; -using MediaBrowser.Model.Users; namespace MediaBrowser.Server.Implementations.Devices { diff --git a/MediaBrowser.Server.Implementations/EntryPoints/UsageEntryPoint.cs b/MediaBrowser.Server.Implementations/EntryPoints/UsageEntryPoint.cs index fcc664011..0e99ee673 100644 --- a/MediaBrowser.Server.Implementations/EntryPoints/UsageEntryPoint.cs +++ b/MediaBrowser.Server.Implementations/EntryPoints/UsageEntryPoint.cs @@ -1,6 +1,7 @@ using MediaBrowser.Common; using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Plugins; using MediaBrowser.Controller.Session; using MediaBrowser.Model.Logging; @@ -21,6 +22,7 @@ namespace MediaBrowser.Server.Implementations.EntryPoints private readonly IHttpClient _httpClient; private readonly ILogger _logger; private readonly ISessionManager _sessionManager; + private readonly IUserManager _userManager; private Timer _timer; private readonly TimeSpan _frequency = TimeSpan.FromHours(24); @@ -65,7 +67,7 @@ namespace MediaBrowser.Server.Implementations.EntryPoints { try { - await new UsageReporter(_applicationHost, _networkManager, _httpClient) + await new UsageReporter(_applicationHost, _networkManager, _httpClient, _userManager) .ReportAppUsage(client, CancellationToken.None) .ConfigureAwait(false); } @@ -107,7 +109,7 @@ namespace MediaBrowser.Server.Implementations.EntryPoints { try { - await new UsageReporter(_applicationHost, _networkManager, _httpClient) + await new UsageReporter(_applicationHost, _networkManager, _httpClient, _userManager) .ReportServerUsage(CancellationToken.None) .ConfigureAwait(false); } diff --git a/MediaBrowser.Server.Implementations/EntryPoints/UsageReporter.cs b/MediaBrowser.Server.Implementations/EntryPoints/UsageReporter.cs index 36ba55828..5be267313 100644 --- a/MediaBrowser.Server.Implementations/EntryPoints/UsageReporter.cs +++ b/MediaBrowser.Server.Implementations/EntryPoints/UsageReporter.cs @@ -1,7 +1,11 @@ using MediaBrowser.Common; using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.Connect; using System; using System.Collections.Generic; +using System.Globalization; +using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -12,13 +16,15 @@ namespace MediaBrowser.Server.Implementations.EntryPoints private readonly IApplicationHost _applicationHost; private readonly INetworkManager _networkManager; private readonly IHttpClient _httpClient; + private readonly IUserManager _userManager; private const string MbAdminUrl = "http://www.mb3admin.com/admin/"; - public UsageReporter(IApplicationHost applicationHost, INetworkManager networkManager, IHttpClient httpClient) + public UsageReporter(IApplicationHost applicationHost, INetworkManager networkManager, IHttpClient httpClient, IUserManager userManager) { _applicationHost = applicationHost; _networkManager = networkManager; _httpClient = httpClient; + _userManager = userManager; } public Task ReportServerUsage(CancellationToken cancellationToken) @@ -38,6 +44,12 @@ namespace MediaBrowser.Server.Implementations.EntryPoints { "isservice", _applicationHost.IsRunningAsService.ToString().ToLower()} }; + var users = _userManager.Users.ToList(); + + data["localusers"] = users.Count(i => !i.ConnectLinkType.HasValue).ToString(CultureInfo.InvariantCulture); + data["guests"] = users.Count(i => i.ConnectLinkType.HasValue && i.ConnectLinkType.Value == UserLinkType.Guest).ToString(CultureInfo.InvariantCulture); + data["linkedusers"] = users.Count(i => i.ConnectLinkType.HasValue && i.ConnectLinkType.Value == UserLinkType.LinkedUser).ToString(CultureInfo.InvariantCulture); + return _httpClient.Post(MbAdminUrl + "service/registration/ping", data, cancellationToken); } diff --git a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs index b4a4c7e9a..37a68b52b 100644 --- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs +++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs @@ -1556,25 +1556,25 @@ namespace MediaBrowser.Server.Implementations.Library { // Types cannot be overridden, so go from the top down until we find a configured content type - var type = GetTopFolderContentType(item); + var type = GetInheritedContentType(item); if (!string.IsNullOrWhiteSpace(type)) { return type; } - type = GetInheritedContentType(item); + return GetConfiguredContentType(item); + } + + public string GetInheritedContentType(BaseItem item) + { + var type = GetTopFolderContentType(item); if (!string.IsNullOrWhiteSpace(type)) { return type; } - return GetConfiguredContentType(item); - } - - public string GetInheritedContentType(BaseItem item) - { return item.Parents .Select(GetConfiguredContentType) .LastOrDefault(i => !string.IsNullOrWhiteSpace(i)); diff --git a/MediaBrowser.Server.Implementations/Localization/Server/server.json b/MediaBrowser.Server.Implementations/Localization/Server/server.json index cf4f0c4bf..e07c9adb7 100644 --- a/MediaBrowser.Server.Implementations/Localization/Server/server.json +++ b/MediaBrowser.Server.Implementations/Localization/Server/server.json @@ -1317,5 +1317,6 @@ "NameSeasonUnknown": "Season Unknown", "NameSeasonNumber": "Season {0}", "LabelNewUserNameHelp": "Usernames can contain letters (a-z), numbers (0-9), dashes (-), underscores (_), apostrophes ('), and periods (.)", + "TabJobs": "Jobs", "TabSyncJobs": "Sync Jobs" } diff --git a/MediaBrowser.Server.Implementations/Sync/AppSyncProvider.cs b/MediaBrowser.Server.Implementations/Sync/AppSyncProvider.cs index 8f6129dca..6cc5be955 100644 --- a/MediaBrowser.Server.Implementations/Sync/AppSyncProvider.cs +++ b/MediaBrowser.Server.Implementations/Sync/AppSyncProvider.cs @@ -46,7 +46,9 @@ namespace MediaBrowser.Server.Implementations.Sync public DeviceProfile GetDeviceProfile(SyncTarget target) { - return new DeviceProfile(); + var caps = _deviceManager.GetCapabilities(target.Id); + + return caps == null || caps.DeviceProfile == null ? new DeviceProfile() : caps.DeviceProfile; } public string Name diff --git a/MediaBrowser.Server.Implementations/Sync/SyncJobProcessor.cs b/MediaBrowser.Server.Implementations/Sync/SyncJobProcessor.cs index 97c6a6dc8..8e5b765a6 100644 --- a/MediaBrowser.Server.Implementations/Sync/SyncJobProcessor.cs +++ b/MediaBrowser.Server.Implementations/Sync/SyncJobProcessor.cs @@ -1,6 +1,7 @@ using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Controller.Sync; using MediaBrowser.Controller.TV; using MediaBrowser.Model.Dlna; @@ -28,8 +29,9 @@ namespace MediaBrowser.Server.Implementations.Sync private readonly ILogger _logger; private readonly IUserManager _userManager; private readonly ITVSeriesManager _tvSeriesManager; + private readonly IMediaEncoder MediaEncoder; - public SyncJobProcessor(ILibraryManager libraryManager, ISyncRepository syncRepo, ISyncManager syncManager, ILogger logger, IUserManager userManager, ITVSeriesManager tvSeriesManager) + public SyncJobProcessor(ILibraryManager libraryManager, ISyncRepository syncRepo, ISyncManager syncManager, ILogger logger, IUserManager userManager, ITVSeriesManager tvSeriesManager, IMediaEncoder mediaEncoder) { _libraryManager = libraryManager; _syncRepo = syncRepo; @@ -37,6 +39,7 @@ namespace MediaBrowser.Server.Implementations.Sync _logger = logger; _userManager = userManager; _tvSeriesManager = tvSeriesManager; + MediaEncoder = mediaEncoder; } public async Task EnsureJobItems(SyncJob job) @@ -392,7 +395,7 @@ namespace MediaBrowser.Server.Implementations.Sync { var options = new VideoOptions { - Context = EncodingContext.Streaming, + Context = EncodingContext.Static, ItemId = item.Id.ToString("N"), DeviceId = jobItem.TargetId, Profile = profile, @@ -406,7 +409,10 @@ namespace MediaBrowser.Server.Implementations.Sync if (streamInfo.PlayMethod == PlayMethod.Transcode) { + jobItem.Status = SyncJobItemStatus.Converting; await _syncRepo.Update(jobItem).ConfigureAwait(false); + + //jobItem.OutputPath = await MediaEncoder.EncodeAudio(new EncodingJobOptions(streamInfo, profile), new Progress(), cancellationToken); } else { @@ -418,12 +424,12 @@ namespace MediaBrowser.Server.Implementations.Sync { jobItem.OutputPath = await DownloadFile(jobItem, mediaSource, cancellationToken).ConfigureAwait(false); } - throw new InvalidOperationException(string.Format("Cannot direct stream {0} protocol", mediaSource.Protocol)); + else + { + throw new InvalidOperationException(string.Format("Cannot direct stream {0} protocol", mediaSource.Protocol)); + } } - // TODO: Transcode - jobItem.OutputPath = mediaSource.Path; - jobItem.Progress = 50; jobItem.Status = SyncJobItemStatus.Transferring; await _syncRepo.Update(jobItem).ConfigureAwait(false); @@ -433,7 +439,7 @@ namespace MediaBrowser.Server.Implementations.Sync { var options = new AudioOptions { - Context = EncodingContext.Streaming, + Context = EncodingContext.Static, ItemId = item.Id.ToString("N"), DeviceId = jobItem.TargetId, Profile = profile, @@ -447,7 +453,10 @@ namespace MediaBrowser.Server.Implementations.Sync if (streamInfo.PlayMethod == PlayMethod.Transcode) { + jobItem.Status = SyncJobItemStatus.Converting; await _syncRepo.Update(jobItem).ConfigureAwait(false); + + jobItem.OutputPath = await MediaEncoder.EncodeAudio(new EncodingJobOptions(streamInfo, profile), new Progress(), cancellationToken); } else { @@ -459,12 +468,12 @@ namespace MediaBrowser.Server.Implementations.Sync { jobItem.OutputPath = await DownloadFile(jobItem, mediaSource, cancellationToken).ConfigureAwait(false); } - throw new InvalidOperationException(string.Format("Cannot direct stream {0} protocol", mediaSource.Protocol)); + else + { + throw new InvalidOperationException(string.Format("Cannot direct stream {0} protocol", mediaSource.Protocol)); + } } - // TODO: Transcode - jobItem.OutputPath = mediaSource.Path; - jobItem.Progress = 50; jobItem.Status = SyncJobItemStatus.Transferring; await _syncRepo.Update(jobItem).ConfigureAwait(false); diff --git a/MediaBrowser.Server.Implementations/Sync/SyncManager.cs b/MediaBrowser.Server.Implementations/Sync/SyncManager.cs index 7e8db3e6a..68eaa38d3 100644 --- a/MediaBrowser.Server.Implementations/Sync/SyncManager.cs +++ b/MediaBrowser.Server.Implementations/Sync/SyncManager.cs @@ -8,6 +8,7 @@ using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.LiveTv; +using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Controller.Sync; using MediaBrowser.Controller.TV; using MediaBrowser.Model.Dlna; @@ -36,10 +37,11 @@ namespace MediaBrowser.Server.Implementations.Sync private readonly Func _dtoService; private readonly IApplicationHost _appHost; private readonly ITVSeriesManager _tvSeriesManager; + private readonly Func MediaEncoder; private ISyncProvider[] _providers = { }; - public SyncManager(ILibraryManager libraryManager, ISyncRepository repo, IImageProcessor imageProcessor, ILogger logger, IUserManager userManager, Func dtoService, IApplicationHost appHost, ITVSeriesManager tvSeriesManager) + public SyncManager(ILibraryManager libraryManager, ISyncRepository repo, IImageProcessor imageProcessor, ILogger logger, IUserManager userManager, Func dtoService, IApplicationHost appHost, ITVSeriesManager tvSeriesManager, Func mediaEncoder) { _libraryManager = libraryManager; _repo = repo; @@ -49,6 +51,7 @@ namespace MediaBrowser.Server.Implementations.Sync _dtoService = dtoService; _appHost = appHost; _tvSeriesManager = tvSeriesManager; + MediaEncoder = mediaEncoder; } public void AddParts(IEnumerable providers) @@ -58,7 +61,7 @@ namespace MediaBrowser.Server.Implementations.Sync public async Task CreateJob(SyncJobRequest request) { - var processor = new SyncJobProcessor(_libraryManager, _repo, this, _logger, _userManager, _tvSeriesManager); + var processor = new SyncJobProcessor(_libraryManager, _repo, this, _logger, _userManager, _tvSeriesManager, MediaEncoder()); var user = _userManager.GetUserById(request.UserId); @@ -162,7 +165,7 @@ namespace MediaBrowser.Server.Implementations.Sync if (item == null) { - var processor = new SyncJobProcessor(_libraryManager, _repo, this, _logger, _userManager, _tvSeriesManager); + var processor = new SyncJobProcessor(_libraryManager, _repo, this, _logger, _userManager, _tvSeriesManager, MediaEncoder()); var user = _userManager.GetUserById(job.UserId); @@ -392,7 +395,7 @@ namespace MediaBrowser.Server.Implementations.Sync await _repo.Update(jobItem).ConfigureAwait(false); - var processor = new SyncJobProcessor(_libraryManager, _repo, this, _logger, _userManager, _tvSeriesManager); + var processor = new SyncJobProcessor(_libraryManager, _repo, this, _logger, _userManager, _tvSeriesManager, MediaEncoder()); await processor.UpdateJobStatus(jobItem.JobId).ConfigureAwait(false); } diff --git a/MediaBrowser.Server.Implementations/Sync/SyncScheduledTask.cs b/MediaBrowser.Server.Implementations/Sync/SyncScheduledTask.cs index e7e30b857..797184298 100644 --- a/MediaBrowser.Server.Implementations/Sync/SyncScheduledTask.cs +++ b/MediaBrowser.Server.Implementations/Sync/SyncScheduledTask.cs @@ -1,5 +1,6 @@ using MediaBrowser.Common.ScheduledTasks; using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Controller.Sync; using MediaBrowser.Controller.TV; using MediaBrowser.Model.Logging; @@ -18,8 +19,9 @@ namespace MediaBrowser.Server.Implementations.Sync private readonly ILogger _logger; private readonly IUserManager _userManager; private readonly ITVSeriesManager _tvSeriesManager; + private readonly IMediaEncoder MediaEncoder; - public SyncScheduledTask(ILibraryManager libraryManager, ISyncRepository syncRepo, ISyncManager syncManager, ILogger logger, IUserManager userManager, ITVSeriesManager tvSeriesManager) + public SyncScheduledTask(ILibraryManager libraryManager, ISyncRepository syncRepo, ISyncManager syncManager, ILogger logger, IUserManager userManager, ITVSeriesManager tvSeriesManager, IMediaEncoder mediaEncoder) { _libraryManager = libraryManager; _syncRepo = syncRepo; @@ -27,6 +29,7 @@ namespace MediaBrowser.Server.Implementations.Sync _logger = logger; _userManager = userManager; _tvSeriesManager = tvSeriesManager; + MediaEncoder = mediaEncoder; } public string Name @@ -49,7 +52,7 @@ namespace MediaBrowser.Server.Implementations.Sync public Task Execute(CancellationToken cancellationToken, IProgress progress) { - return new SyncJobProcessor(_libraryManager, _syncRepo, _syncManager, _logger, _userManager, _tvSeriesManager).Sync(progress, + return new SyncJobProcessor(_libraryManager, _syncRepo, _syncManager, _logger, _userManager, _tvSeriesManager, MediaEncoder).Sync(progress, cancellationToken); } diff --git a/MediaBrowser.Server.Startup.Common/ApplicationHost.cs b/MediaBrowser.Server.Startup.Common/ApplicationHost.cs index ed5441336..8a1721b7e 100644 --- a/MediaBrowser.Server.Startup.Common/ApplicationHost.cs +++ b/MediaBrowser.Server.Startup.Common/ApplicationHost.cs @@ -476,16 +476,13 @@ namespace MediaBrowser.Server.Startup.Common var innerProgress = new ActionableProgress(); innerProgress.RegisterAction(p => progress.Report((.75 * p) + 15)); - await RegisterMediaEncoder(innerProgress).ConfigureAwait(false); - progress.Report(90); - ImageProcessor = new ImageProcessor(LogManager.GetLogger("ImageProcessor"), ServerConfigurationManager.ApplicationPaths, FileSystemManager, JsonSerializer, MediaEncoder); RegisterSingleInstance(ImageProcessor); TVSeriesManager = new TVSeriesManager(UserManager, UserDataManager, LibraryManager); RegisterSingleInstance(TVSeriesManager); - SyncManager = new SyncManager(LibraryManager, SyncRepository, ImageProcessor, LogManager.GetLogger("SyncManager"), UserManager, () => DtoService, this, TVSeriesManager); + SyncManager = new SyncManager(LibraryManager, SyncRepository, ImageProcessor, LogManager.GetLogger("SyncManager"), UserManager, () => DtoService, this, TVSeriesManager, () => MediaEncoder); RegisterSingleInstance(SyncManager); DtoService = new DtoService(Logger, LibraryManager, UserDataManager, ItemRepository, ImageProcessor, ServerConfigurationManager, FileSystemManager, ProviderManager, () => ChannelManager, SyncManager, this); @@ -547,6 +544,9 @@ namespace MediaBrowser.Server.Startup.Common ChapterManager = new ChapterManager(LibraryManager, LogManager.GetLogger("ChapterManager"), ServerConfigurationManager, ItemRepository); RegisterSingleInstance(ChapterManager); + await RegisterMediaEncoder(innerProgress).ConfigureAwait(false); + progress.Report(90); + EncodingManager = new EncodingManager(FileSystemManager, Logger, MediaEncoder, ChapterManager); RegisterSingleInstance(EncodingManager); @@ -591,7 +591,18 @@ namespace MediaBrowser.Server.Startup.Common new FFmpegValidator(Logger, ApplicationPaths).Validate(info); - MediaEncoder = new MediaEncoder(LogManager.GetLogger("MediaEncoder"), JsonSerializer, info.EncoderPath, info.ProbePath, info.Version); + MediaEncoder = new MediaEncoder(LogManager.GetLogger("MediaEncoder"), + JsonSerializer, + info.EncoderPath, + info.ProbePath, + info.Version, + ServerConfigurationManager, + FileSystemManager, + LiveTvManager, + IsoManager, + LibraryManager, + ChannelManager, + SessionManager); RegisterSingleInstance(MediaEncoder); } -- cgit v1.2.3 From c63c39ce57dc6a22058da227a11ef1d62f28f627 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 2 Jan 2015 01:12:58 -0500 Subject: sync video transcoding --- .../MediaEncoding/EncodingJobOptions.cs | 91 ++ .../MediaEncoding/IMediaEncoder.cs | 11 + MediaBrowser.MediaEncoding/Encoder/AudioEncoder.cs | 86 ++ MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs | 1049 ++++++++++++++++++++ MediaBrowser.MediaEncoding/Encoder/EncodingJob.cs | 434 ++++++++ .../Encoder/EncodingJobFactory.cs | 830 ++++++++++++++++ MediaBrowser.MediaEncoding/Encoder/JobLogger.cs | 122 +++ MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs | 46 +- MediaBrowser.MediaEncoding/Encoder/VideoEncoder.cs | 177 ++++ .../MediaBrowser.MediaEncoding.csproj | 1 + .../Sync/SyncJobProcessor.cs | 6 +- .../ApplicationHost.cs | 7 +- 12 files changed, 2844 insertions(+), 16 deletions(-) create mode 100644 MediaBrowser.Controller/MediaEncoding/EncodingJobOptions.cs create mode 100644 MediaBrowser.MediaEncoding/Encoder/AudioEncoder.cs create mode 100644 MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs create mode 100644 MediaBrowser.MediaEncoding/Encoder/EncodingJob.cs create mode 100644 MediaBrowser.MediaEncoding/Encoder/EncodingJobFactory.cs create mode 100644 MediaBrowser.MediaEncoding/Encoder/JobLogger.cs create mode 100644 MediaBrowser.MediaEncoding/Encoder/VideoEncoder.cs (limited to 'MediaBrowser.MediaEncoding') diff --git a/MediaBrowser.Controller/MediaEncoding/EncodingJobOptions.cs b/MediaBrowser.Controller/MediaEncoding/EncodingJobOptions.cs new file mode 100644 index 000000000..a988c2f97 --- /dev/null +++ b/MediaBrowser.Controller/MediaEncoding/EncodingJobOptions.cs @@ -0,0 +1,91 @@ +using MediaBrowser.Model.Dlna; + +namespace MediaBrowser.Controller.MediaEncoding +{ + public class EncodingJobOptions + { + public string OutputContainer { get; set; } + + public long? StartTimeTicks { get; set; } + public int? Width { get; set; } + public int? Height { get; set; } + public int? MaxWidth { get; set; } + public int? MaxHeight { get; set; } + public bool Static = false; + public float? Framerate { get; set; } + public float? MaxFramerate { get; set; } + public string Profile { get; set; } + public int? Level { get; set; } + + public string DeviceId { get; set; } + public string ItemId { get; set; } + public string MediaSourceId { get; set; } + public string AudioCodec { get; set; } + + public bool EnableAutoStreamCopy { get; set; } + + public int? MaxAudioChannels { get; set; } + public int? AudioChannels { get; set; } + public int? AudioBitRate { get; set; } + public int? AudioSampleRate { get; set; } + + public DeviceProfile DeviceProfile { get; set; } + public EncodingContext Context { get; set; } + + public string VideoCodec { get; set; } + + public int? VideoBitRate { get; set; } + public int? AudioStreamIndex { get; set; } + public int? VideoStreamIndex { get; set; } + public int? SubtitleStreamIndex { get; set; } + public int? MaxRefFrames { get; set; } + public int? MaxVideoBitDepth { get; set; } + public SubtitleDeliveryMethod SubtitleMethod { get; set; } + + /// + /// Gets a value indicating whether this instance has fixed resolution. + /// + /// true if this instance has fixed resolution; otherwise, false. + public bool HasFixedResolution + { + get + { + return Width.HasValue || Height.HasValue; + } + } + + public bool? Cabac { get; set; } + + public EncodingJobOptions() + { + + } + + public EncodingJobOptions(StreamInfo info, DeviceProfile deviceProfile) + { + OutputContainer = info.Container; + StartTimeTicks = info.StartPositionTicks; + MaxWidth = info.MaxWidth; + MaxHeight = info.MaxHeight; + MaxFramerate = info.MaxFramerate; + Profile = info.VideoProfile; + Level = info.VideoLevel; + ItemId = info.ItemId; + MediaSourceId = info.MediaSourceId; + AudioCodec = info.AudioCodec; + MaxAudioChannels = info.MaxAudioChannels; + AudioBitRate = info.AudioBitrate; + AudioSampleRate = info.TargetAudioSampleRate; + DeviceProfile = deviceProfile; + VideoCodec = info.VideoCodec; + VideoBitRate = info.VideoBitrate; + AudioStreamIndex = info.AudioStreamIndex; + SubtitleStreamIndex = info.SubtitleStreamIndex; + MaxRefFrames = info.MaxRefFrames; + MaxVideoBitDepth = info.MaxVideoBitDepth; + SubtitleMethod = info.SubtitleDeliveryMethod; + Cabac = info.Cabac; + Context = info.Context; + } + } +} diff --git a/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs b/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs index 8f56bfda5..47544f972 100644 --- a/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs +++ b/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs @@ -107,5 +107,16 @@ namespace MediaBrowser.Controller.MediaEncoding Task EncodeAudio(EncodingJobOptions options, IProgress progress, CancellationToken cancellationToken); + + /// + /// Encodes the video. + /// + /// The options. + /// The progress. + /// The cancellation token. + /// Task<System.String>. + Task EncodeVideo(EncodingJobOptions options, + IProgress progress, + CancellationToken cancellationToken); } } diff --git a/MediaBrowser.MediaEncoding/Encoder/AudioEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/AudioEncoder.cs new file mode 100644 index 000000000..7054accfa --- /dev/null +++ b/MediaBrowser.MediaEncoding/Encoder/AudioEncoder.cs @@ -0,0 +1,86 @@ +using MediaBrowser.Common.IO; +using MediaBrowser.Controller.Channels; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.LiveTv; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Controller.Session; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Logging; +using System; +using System.Collections.Generic; + +namespace MediaBrowser.MediaEncoding.Encoder +{ + public class AudioEncoder : BaseEncoder + { + public AudioEncoder(MediaEncoder mediaEncoder, ILogger logger, IServerConfigurationManager configurationManager, IFileSystem fileSystem, ILiveTvManager liveTvManager, IIsoManager isoManager, ILibraryManager libraryManager, IChannelManager channelManager, ISessionManager sessionManager, ISubtitleEncoder subtitleEncoder) : base(mediaEncoder, logger, configurationManager, fileSystem, liveTvManager, isoManager, libraryManager, channelManager, sessionManager, subtitleEncoder) + { + } + + protected override string GetCommandLineArguments(EncodingJob job) + { + var audioTranscodeParams = new List(); + + var bitrate = job.OutputAudioBitrate; + + if (bitrate.HasValue) + { + audioTranscodeParams.Add("-ab " + bitrate.Value.ToString(UsCulture)); + } + + if (job.OutputAudioChannels.HasValue) + { + audioTranscodeParams.Add("-ac " + job.OutputAudioChannels.Value.ToString(UsCulture)); + } + + if (job.OutputAudioSampleRate.HasValue) + { + audioTranscodeParams.Add("-ar " + job.OutputAudioSampleRate.Value.ToString(UsCulture)); + } + + var threads = GetNumberOfThreads(job, false); + + var inputModifier = GetInputModifier(job); + + return string.Format("{0} {1} -threads {2}{3} {4} -id3v2_version 3 -write_id3v1 1 -y \"{5}\"", + inputModifier, + GetInputArgument(job), + threads, + " -vn", + string.Join(" ", audioTranscodeParams.ToArray()), + job.OutputFilePath).Trim(); + } + + protected override string GetOutputFileExtension(EncodingJob state) + { + var ext = base.GetOutputFileExtension(state); + + if (!string.IsNullOrEmpty(ext)) + { + return ext; + } + + var audioCodec = state.Options.AudioCodec; + + if (string.Equals("aac", audioCodec, StringComparison.OrdinalIgnoreCase)) + { + return ".aac"; + } + if (string.Equals("mp3", audioCodec, StringComparison.OrdinalIgnoreCase)) + { + return ".mp3"; + } + if (string.Equals("vorbis", audioCodec, StringComparison.OrdinalIgnoreCase)) + { + return ".ogg"; + } + if (string.Equals("wma", audioCodec, StringComparison.OrdinalIgnoreCase)) + { + return ".wma"; + } + + return null; + } + } +} diff --git a/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs new file mode 100644 index 000000000..a350d0577 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs @@ -0,0 +1,1049 @@ +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.IO; +using MediaBrowser.Controller.Channels; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.LiveTv; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Controller.Session; +using MediaBrowser.MediaEncoding.Subtitles; +using MediaBrowser.Model.Configuration; +using MediaBrowser.Model.Dlna; +using MediaBrowser.Model.Drawing; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Logging; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Globalization; +using System.IO; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.MediaEncoding.Encoder +{ + public abstract class BaseEncoder + { + protected readonly MediaEncoder MediaEncoder; + protected readonly ILogger Logger; + protected readonly IServerConfigurationManager ConfigurationManager; + protected readonly IFileSystem FileSystem; + protected readonly ILiveTvManager LiveTvManager; + protected readonly IIsoManager IsoManager; + protected readonly ILibraryManager LibraryManager; + protected readonly IChannelManager ChannelManager; + protected readonly ISessionManager SessionManager; + protected readonly ISubtitleEncoder SubtitleEncoder; + + protected readonly CultureInfo UsCulture = new CultureInfo("en-US"); + + public BaseEncoder(MediaEncoder mediaEncoder, + ILogger logger, + IServerConfigurationManager configurationManager, + IFileSystem fileSystem, + ILiveTvManager liveTvManager, + IIsoManager isoManager, + ILibraryManager libraryManager, + IChannelManager channelManager, + ISessionManager sessionManager, ISubtitleEncoder subtitleEncoder) + { + MediaEncoder = mediaEncoder; + Logger = logger; + ConfigurationManager = configurationManager; + FileSystem = fileSystem; + LiveTvManager = liveTvManager; + IsoManager = isoManager; + LibraryManager = libraryManager; + ChannelManager = channelManager; + SessionManager = sessionManager; + SubtitleEncoder = subtitleEncoder; + } + + public async Task Start(EncodingJobOptions options, + IProgress progress, + CancellationToken cancellationToken) + { + var encodingJob = await new EncodingJobFactory(Logger, LiveTvManager, LibraryManager, ChannelManager) + .CreateJob(options, IsVideoEncoder, progress, cancellationToken).ConfigureAwait(false); + + encodingJob.OutputFilePath = GetOutputFilePath(encodingJob); + Directory.CreateDirectory(Path.GetDirectoryName(encodingJob.OutputFilePath)); + + if (options.Context == EncodingContext.Static && encodingJob.IsInputVideo) + { + encodingJob.ReadInputAtNativeFramerate = true; + } + + await AcquireResources(encodingJob, cancellationToken).ConfigureAwait(false); + + var commandLineArgs = GetCommandLineArguments(encodingJob); + + if (GetEncodingOptions().EnableDebugLogging) + { + commandLineArgs = "-loglevel debug " + commandLineArgs; + } + + var process = new Process + { + StartInfo = new ProcessStartInfo + { + CreateNoWindow = true, + UseShellExecute = false, + + // Must consume both stdout and stderr or deadlocks may occur + RedirectStandardOutput = true, + RedirectStandardError = true, + RedirectStandardInput = true, + + FileName = MediaEncoder.EncoderPath, + Arguments = commandLineArgs, + + WindowStyle = ProcessWindowStyle.Hidden, + ErrorDialog = false + }, + + EnableRaisingEvents = true + }; + + var workingDirectory = GetWorkingDirectory(options); + if (!string.IsNullOrWhiteSpace(workingDirectory)) + { + process.StartInfo.WorkingDirectory = workingDirectory; + } + + OnTranscodeBeginning(encodingJob); + + var commandLineLogMessage = process.StartInfo.FileName + " " + process.StartInfo.Arguments; + Logger.Info(commandLineLogMessage); + + var logFilePath = Path.Combine(ConfigurationManager.CommonApplicationPaths.LogDirectoryPath, "transcode-" + Guid.NewGuid() + ".txt"); + Directory.CreateDirectory(Path.GetDirectoryName(logFilePath)); + + // FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory. + encodingJob.LogFileStream = FileSystem.GetFileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, true); + + var commandLineLogMessageBytes = Encoding.UTF8.GetBytes(commandLineLogMessage + Environment.NewLine + Environment.NewLine); + await encodingJob.LogFileStream.WriteAsync(commandLineLogMessageBytes, 0, commandLineLogMessageBytes.Length, cancellationToken).ConfigureAwait(false); + + process.Exited += (sender, args) => OnFfMpegProcessExited(process, encodingJob); + + try + { + process.Start(); + } + catch (Exception ex) + { + Logger.ErrorException("Error starting ffmpeg", ex); + + OnTranscodeFailedToStart(encodingJob.OutputFilePath, encodingJob); + + throw; + } + + // MUST read both stdout and stderr asynchronously or a deadlock may occurr + process.BeginOutputReadLine(); + + // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback + new JobLogger(Logger).StartStreamingLog(encodingJob, process.StandardError.BaseStream, encodingJob.LogFileStream); + + // Wait for the file to exist before proceeeding + while (!File.Exists(encodingJob.OutputFilePath) && !encodingJob.HasExited) + { + await Task.Delay(100, cancellationToken).ConfigureAwait(false); + } + + return encodingJob; + } + + /// + /// Processes the exited. + /// + /// The process. + /// The job. + private void OnFfMpegProcessExited(Process process, EncodingJob job) + { + job.HasExited = true; + + Logger.Debug("Disposing stream resources"); + job.Dispose(); + + try + { + Logger.Info("FFMpeg exited with code {0}", process.ExitCode); + + try + { + job.TaskCompletionSource.TrySetResult(true); + } + catch + { + } + } + catch + { + Logger.Error("FFMpeg exited with an error."); + + try + { + job.TaskCompletionSource.TrySetException(new ApplicationException()); + } + catch + { + } + } + + // This causes on exited to be called twice: + //try + //{ + // // Dispose the process + // process.Dispose(); + //} + //catch (Exception ex) + //{ + // Logger.ErrorException("Error disposing ffmpeg.", ex); + //} + } + + private void OnTranscodeBeginning(EncodingJob job) + { + job.ReportTranscodingProgress(null, null, null, null); + } + + private void OnTranscodeFailedToStart(string path, EncodingJob job) + { + if (!string.IsNullOrWhiteSpace(job.Options.DeviceId)) + { + SessionManager.ClearTranscodingInfo(job.Options.DeviceId); + } + } + + protected virtual bool IsVideoEncoder + { + get { return false; } + } + + protected virtual string GetWorkingDirectory(EncodingJobOptions options) + { + return null; + } + + protected EncodingOptions GetEncodingOptions() + { + return ConfigurationManager.GetConfiguration("encoding"); + } + + protected abstract string GetCommandLineArguments(EncodingJob job); + + private string GetOutputFilePath(EncodingJob state) + { + var folder = ConfigurationManager.ApplicationPaths.TranscodingTempPath; + + var outputFileExtension = GetOutputFileExtension(state); + var context = state.Options.Context; + + var filename = state.Id + (outputFileExtension ?? string.Empty).ToLower(); + return Path.Combine(folder, context.ToString().ToLower(), filename); + } + + protected virtual string GetOutputFileExtension(EncodingJob state) + { + if (!string.IsNullOrWhiteSpace(state.Options.OutputContainer)) + { + return "." + state.Options.OutputContainer; + } + + return null; + } + + /// + /// Gets the number of threads. + /// + /// System.Int32. + protected int GetNumberOfThreads(EncodingJob job, bool isWebm) + { + if (isWebm) + { + // Recommended per docs + return Math.Max(Environment.ProcessorCount - 1, 2); + } + + // Use more when this is true. -re will keep cpu usage under control + if (job.ReadInputAtNativeFramerate) + { + if (isWebm) + { + return Math.Max(Environment.ProcessorCount - 1, 2); + } + + return 0; + } + + // Webm: http://www.webmproject.org/docs/encoder-parameters/ + // The decoder will usually automatically use an appropriate number of threads according to how many cores are available but it can only use multiple threads + // for the coefficient data if the encoder selected --token-parts > 0 at encode time. + + switch (GetQualitySetting()) + { + case EncodingQuality.HighSpeed: + return 2; + case EncodingQuality.HighQuality: + return 2; + case EncodingQuality.MaxQuality: + return isWebm ? Math.Max(Environment.ProcessorCount - 1, 2) : 0; + default: + throw new Exception("Unrecognized MediaEncodingQuality value."); + } + } + + protected EncodingQuality GetQualitySetting() + { + var quality = GetEncodingOptions().EncodingQuality; + + if (quality == EncodingQuality.Auto) + { + var cpuCount = Environment.ProcessorCount; + + if (cpuCount >= 4) + { + //return EncodingQuality.HighQuality; + } + + return EncodingQuality.HighSpeed; + } + + return quality; + } + + protected string GetInputModifier(EncodingJob job, bool genPts = true) + { + var inputModifier = string.Empty; + + var probeSize = GetProbeSizeArgument(job); + inputModifier += " " + probeSize; + inputModifier = inputModifier.Trim(); + + var userAgentParam = GetUserAgentParam(job); + + if (!string.IsNullOrWhiteSpace(userAgentParam)) + { + inputModifier += " " + userAgentParam; + } + + inputModifier = inputModifier.Trim(); + + inputModifier += " " + GetFastSeekCommandLineParameter(job.Options); + inputModifier = inputModifier.Trim(); + + if (job.IsVideoRequest && genPts) + { + inputModifier += " -fflags +genpts"; + } + + if (!string.IsNullOrEmpty(job.InputAudioSync)) + { + inputModifier += " -async " + job.InputAudioSync; + } + + if (!string.IsNullOrEmpty(job.InputVideoSync)) + { + inputModifier += " -vsync " + job.InputVideoSync; + } + + if (job.ReadInputAtNativeFramerate) + { + inputModifier += " -re"; + } + + return inputModifier; + } + + private string GetUserAgentParam(EncodingJob job) + { + string useragent = null; + + job.RemoteHttpHeaders.TryGetValue("User-Agent", out useragent); + + if (!string.IsNullOrWhiteSpace(useragent)) + { + return "-user-agent \"" + useragent + "\""; + } + + return string.Empty; + } + + /// + /// Gets the probe size argument. + /// + /// The job. + /// System.String. + private string GetProbeSizeArgument(EncodingJob job) + { + if (job.PlayableStreamFileNames.Count > 0) + { + return MediaEncoder.GetProbeSizeArgument(job.PlayableStreamFileNames.ToArray(), job.InputProtocol); + } + + return MediaEncoder.GetProbeSizeArgument(new[] { job.MediaPath }, job.InputProtocol); + } + + /// + /// Gets the fast seek command line parameter. + /// + /// The options. + /// System.String. + /// The fast seek command line parameter. + protected string GetFastSeekCommandLineParameter(EncodingJobOptions options) + { + var time = options.StartTimeTicks; + + if (time.HasValue && time.Value > 0) + { + return string.Format("-ss {0}", MediaEncoder.GetTimeParameter(time.Value)); + } + + return string.Empty; + } + + /// + /// Gets the input argument. + /// + /// The job. + /// System.String. + protected string GetInputArgument(EncodingJob job) + { + var arg = "-i " + GetInputPathArgument(job); + + if (job.SubtitleStream != null) + { + if (job.SubtitleStream.IsExternal && !job.SubtitleStream.IsTextSubtitleStream) + { + arg += " -i " + job.SubtitleStream.Path; + } + } + + return arg; + } + + private string GetInputPathArgument(EncodingJob job) + { + //if (job.InputProtocol == MediaProtocol.File && + // job.RunTimeTicks.HasValue && + // job.VideoType == VideoType.VideoFile && + // !string.Equals(job.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) + //{ + // if (job.RunTimeTicks.Value >= TimeSpan.FromMinutes(5).Ticks && job.IsInputVideo) + // { + // if (SupportsThrottleWithStream) + // { + // var url = "http://localhost:" + ServerConfigurationManager.Configuration.HttpServerPortNumber.ToString(UsCulture) + "/mediabrowser/videos/" + job.Request.Id + "/stream?static=true&Throttle=true&mediaSourceId=" + job.Request.MediaSourceId; + + // url += "&transcodingJobId=" + transcodingJobId; + + // return string.Format("\"{0}\"", url); + // } + // } + //} + + var protocol = job.InputProtocol; + + var inputPath = new[] { job.MediaPath }; + + if (job.IsInputVideo) + { + if (!(job.VideoType == VideoType.Iso && job.IsoMount == null)) + { + inputPath = MediaEncoderHelpers.GetInputArgument(job.MediaPath, job.InputProtocol, job.IsoMount, job.PlayableStreamFileNames); + } + } + + return MediaEncoder.GetInputArgument(inputPath, protocol); + } + + private async Task AcquireResources(EncodingJob state, CancellationToken cancellationToken) + { + if (state.VideoType == VideoType.Iso && state.IsoType.HasValue && IsoManager.CanMount(state.MediaPath)) + { + state.IsoMount = await IsoManager.Mount(state.MediaPath, cancellationToken).ConfigureAwait(false); + } + + if (string.IsNullOrEmpty(state.MediaPath)) + { + var checkCodecs = false; + + if (string.Equals(state.ItemType, typeof(LiveTvChannel).Name)) + { + var streamInfo = await LiveTvManager.GetChannelStream(state.Options.ItemId, cancellationToken).ConfigureAwait(false); + + state.LiveTvStreamId = streamInfo.Id; + + state.MediaPath = streamInfo.Path; + state.InputProtocol = streamInfo.Protocol; + + await Task.Delay(1500, cancellationToken).ConfigureAwait(false); + + AttachMediaStreamInfo(state, streamInfo, state.Options); + checkCodecs = true; + } + + else if (string.Equals(state.ItemType, typeof(LiveTvVideoRecording).Name) || + string.Equals(state.ItemType, typeof(LiveTvAudioRecording).Name)) + { + var streamInfo = await LiveTvManager.GetRecordingStream(state.Options.ItemId, cancellationToken).ConfigureAwait(false); + + state.LiveTvStreamId = streamInfo.Id; + + state.MediaPath = streamInfo.Path; + state.InputProtocol = streamInfo.Protocol; + + await Task.Delay(1500, cancellationToken).ConfigureAwait(false); + + AttachMediaStreamInfo(state, streamInfo, state.Options); + checkCodecs = true; + } + + if (state.IsVideoRequest && checkCodecs) + { + if (state.VideoStream != null && EncodingJobFactory.CanStreamCopyVideo(state.Options, state.VideoStream)) + { + state.OutputVideoCodec = "copy"; + } + + if (state.AudioStream != null && EncodingJobFactory.CanStreamCopyAudio(state.Options, state.AudioStream, state.SupportedAudioCodecs)) + { + state.OutputAudioCodec = "copy"; + } + } + } + } + + private void AttachMediaStreamInfo(EncodingJob state, + ChannelMediaInfo mediaInfo, + EncodingJobOptions videoRequest) + { + var mediaSource = mediaInfo.ToMediaSource(); + + state.InputProtocol = mediaSource.Protocol; + state.MediaPath = mediaSource.Path; + state.RunTimeTicks = mediaSource.RunTimeTicks; + state.RemoteHttpHeaders = mediaSource.RequiredHttpHeaders; + state.InputBitrate = mediaSource.Bitrate; + state.InputFileSize = mediaSource.Size; + state.ReadInputAtNativeFramerate = mediaSource.ReadAtNativeFramerate; + + if (state.ReadInputAtNativeFramerate) + { + state.OutputAudioSync = "1000"; + state.InputVideoSync = "-1"; + state.InputAudioSync = "1"; + } + + EncodingJobFactory.AttachMediaStreamInfo(state, mediaSource.MediaStreams, videoRequest); + } + + /// + /// Gets the internal graphical subtitle param. + /// + /// The state. + /// The output video codec. + /// System.String. + protected string GetGraphicalSubtitleParam(EncodingJob state, string outputVideoCodec) + { + var outputSizeParam = string.Empty; + + var request = state.Options; + + // Add resolution params, if specified + if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue) + { + outputSizeParam = GetOutputSizeParam(state, outputVideoCodec).TrimEnd('"'); + outputSizeParam = "," + outputSizeParam.Substring(outputSizeParam.IndexOf("scale", StringComparison.OrdinalIgnoreCase)); + } + + var videoSizeParam = string.Empty; + + if (state.VideoStream != null && state.VideoStream.Width.HasValue && state.VideoStream.Height.HasValue) + { + videoSizeParam = string.Format(",scale={0}:{1}", state.VideoStream.Width.Value.ToString(UsCulture), state.VideoStream.Height.Value.ToString(UsCulture)); + } + + var mapPrefix = state.SubtitleStream.IsExternal ? + 1 : + 0; + + var subtitleStreamIndex = state.SubtitleStream.IsExternal + ? 0 + : state.SubtitleStream.Index; + + return string.Format(" -filter_complex \"[{0}:{1}]format=yuva444p{4},lut=u=128:v=128:y=gammaval(.3)[sub] ; [0:{2}] [sub] overlay{3}\"", + mapPrefix.ToString(UsCulture), + subtitleStreamIndex.ToString(UsCulture), + state.VideoStream.Index.ToString(UsCulture), + outputSizeParam, + videoSizeParam); + } + + /// + /// Gets the video bitrate to specify on the command line + /// + /// The state. + /// The video codec. + /// if set to true [is HLS]. + /// System.String. + protected string GetVideoQualityParam(EncodingJob state, string videoCodec, bool isHls) + { + var param = string.Empty; + + var isVc1 = state.VideoStream != null && + string.Equals(state.VideoStream.Codec, "vc1", StringComparison.OrdinalIgnoreCase); + + var qualitySetting = GetQualitySetting(); + + if (string.Equals(videoCodec, "libx264", StringComparison.OrdinalIgnoreCase)) + { + switch (qualitySetting) + { + case EncodingQuality.HighSpeed: + param = "-preset superfast"; + break; + case EncodingQuality.HighQuality: + param = "-preset superfast"; + break; + case EncodingQuality.MaxQuality: + param = "-preset superfast"; + break; + } + + switch (qualitySetting) + { + case EncodingQuality.HighSpeed: + param += " -crf 23"; + break; + case EncodingQuality.HighQuality: + param += " -crf 20"; + break; + case EncodingQuality.MaxQuality: + param += " -crf 18"; + break; + } + } + + // webm + else if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase)) + { + // Values 0-3, 0 being highest quality but slower + var profileScore = 0; + + string crf; + var qmin = "0"; + var qmax = "50"; + + switch (qualitySetting) + { + case EncodingQuality.HighSpeed: + crf = "10"; + break; + case EncodingQuality.HighQuality: + crf = "6"; + break; + case EncodingQuality.MaxQuality: + crf = "4"; + break; + default: + throw new ArgumentException("Unrecognized quality setting"); + } + + if (isVc1) + { + profileScore++; + } + + // Max of 2 + profileScore = Math.Min(profileScore, 2); + + // http://www.webmproject.org/docs/encoder-parameters/ + param = string.Format("-speed 16 -quality good -profile:v {0} -slices 8 -crf {1} -qmin {2} -qmax {3}", + profileScore.ToString(UsCulture), + crf, + qmin, + qmax); + } + + else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase)) + { + param = "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2"; + } + + // asf/wmv + else if (string.Equals(videoCodec, "wmv2", StringComparison.OrdinalIgnoreCase)) + { + param = "-qmin 2"; + } + + else if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase)) + { + param = "-mbd 2"; + } + + param += GetVideoBitrateParam(state, videoCodec, isHls); + + var framerate = GetFramerateParam(state); + if (framerate.HasValue) + { + param += string.Format(" -r {0}", framerate.Value.ToString(UsCulture)); + } + + if (!string.IsNullOrEmpty(state.OutputVideoSync)) + { + param += " -vsync " + state.OutputVideoSync; + } + + if (!string.IsNullOrEmpty(state.Options.Profile)) + { + param += " -profile:v " + state.Options.Profile; + } + + if (state.Options.Level.HasValue) + { + param += " -level " + state.Options.Level.Value.ToString(UsCulture); + } + + return param; + } + + protected string GetVideoBitrateParam(EncodingJob state, string videoCodec, bool isHls) + { + var bitrate = state.OutputVideoBitrate; + + if (bitrate.HasValue) + { + var hasFixedResolution = state.Options.HasFixedResolution; + + if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase)) + { + if (hasFixedResolution) + { + return string.Format(" -minrate:v ({0}*.90) -maxrate:v ({0}*1.10) -bufsize:v {0} -b:v {0}", bitrate.Value.ToString(UsCulture)); + } + + // With vpx when crf is used, b:v becomes a max rate + // https://trac.ffmpeg.org/wiki/vpxEncodingGuide. But higher bitrate source files -b:v causes judder so limite the bitrate but dont allow it to "saturate" the bitrate. So dont contrain it down just up. + return string.Format(" -maxrate:v {0} -bufsize:v ({0}*2) -b:v {0}", bitrate.Value.ToString(UsCulture)); + } + + if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase)) + { + return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture)); + } + + // H264 + if (hasFixedResolution) + { + if (isHls) + { + return string.Format(" -b:v {0} -maxrate ({0}*.80) -bufsize {0}", bitrate.Value.ToString(UsCulture)); + } + + return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture)); + } + + return string.Format(" -maxrate {0} -bufsize {1}", + bitrate.Value.ToString(UsCulture), + (bitrate.Value * 2).ToString(UsCulture)); + } + + return string.Empty; + } + + protected double? GetFramerateParam(EncodingJob state) + { + if (state.Options.Framerate.HasValue) + { + return state.Options.Framerate.Value; + } + + var maxrate = state.Options.MaxFramerate; + + if (maxrate.HasValue && state.VideoStream != null) + { + var contentRate = state.VideoStream.AverageFrameRate ?? state.VideoStream.RealFrameRate; + + if (contentRate.HasValue && contentRate.Value > maxrate.Value) + { + return maxrate; + } + } + + return null; + } + + /// + /// Gets the map args. + /// + /// The state. + /// System.String. + protected virtual string GetMapArgs(EncodingJob state) + { + // If we don't have known media info + // If input is video, use -sn to drop subtitles + // Otherwise just return empty + if (state.VideoStream == null && state.AudioStream == null) + { + return state.IsInputVideo ? "-sn" : string.Empty; + } + + // We have media info, but we don't know the stream indexes + if (state.VideoStream != null && state.VideoStream.Index == -1) + { + return "-sn"; + } + + // We have media info, but we don't know the stream indexes + if (state.AudioStream != null && state.AudioStream.Index == -1) + { + return state.IsInputVideo ? "-sn" : string.Empty; + } + + var args = string.Empty; + + if (state.VideoStream != null) + { + args += string.Format("-map 0:{0}", state.VideoStream.Index); + } + else + { + args += "-map -0:v"; + } + + if (state.AudioStream != null) + { + args += string.Format(" -map 0:{0}", state.AudioStream.Index); + } + + else + { + args += " -map -0:a"; + } + + if (state.SubtitleStream == null) + { + args += " -map -0:s"; + } + else if (state.SubtitleStream.IsExternal && !state.SubtitleStream.IsTextSubtitleStream) + { + args += " -map 1:0 -sn"; + } + + return args; + } + + /// + /// Determines whether the specified stream is H264. + /// + /// The stream. + /// true if the specified stream is H264; otherwise, false. + protected bool IsH264(MediaStream stream) + { + var codec = stream.Codec ?? string.Empty; + + return codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 || + codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1; + } + + /// + /// If we're going to put a fixed size on the command line, this will calculate it + /// + /// The state. + /// The output video codec. + /// if set to true [allow time stamp copy]. + /// System.String. + protected string GetOutputSizeParam(EncodingJob state, + string outputVideoCodec, + bool allowTimeStampCopy = true) + { + // http://sonnati.wordpress.com/2012/10/19/ffmpeg-the-swiss-army-knife-of-internet-streaming-part-vi/ + + var request = state.Options; + + var filters = new List(); + + if (state.DeInterlace) + { + filters.Add("yadif=0:-1:0"); + } + + // If fixed dimensions were supplied + if (request.Width.HasValue && request.Height.HasValue) + { + var widthParam = request.Width.Value.ToString(UsCulture); + var heightParam = request.Height.Value.ToString(UsCulture); + + filters.Add(string.Format("scale=trunc({0}/2)*2:trunc({1}/2)*2", widthParam, heightParam)); + } + + // If Max dimensions were supplied, for width selects lowest even number between input width and width req size and selects lowest even number from in width*display aspect and requested size + else if (request.MaxWidth.HasValue && request.MaxHeight.HasValue) + { + var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture); + var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture); + + filters.Add(string.Format("scale=trunc(min(iw\\,{0})/2)*2:trunc(min((iw/dar)\\,{1})/2)*2", maxWidthParam, maxHeightParam)); + } + + // If a fixed width was requested + else if (request.Width.HasValue) + { + var widthParam = request.Width.Value.ToString(UsCulture); + + filters.Add(string.Format("scale={0}:trunc(ow/a/2)*2", widthParam)); + } + + // If a fixed height was requested + else if (request.Height.HasValue) + { + var heightParam = request.Height.Value.ToString(UsCulture); + + filters.Add(string.Format("scale=trunc(oh*a*2)/2:{0}", heightParam)); + } + + // If a max width was requested + else if (request.MaxWidth.HasValue && (!request.MaxHeight.HasValue || state.VideoStream == null)) + { + var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture); + + filters.Add(string.Format("scale=min(iw\\,{0}):trunc(ow/dar/2)*2", maxWidthParam)); + } + + // If a max height was requested + else if (request.MaxHeight.HasValue && (!request.MaxWidth.HasValue || state.VideoStream == null)) + { + var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture); + + filters.Add(string.Format("scale=trunc(oh*a*2)/2:min(ih\\,{0})", maxHeightParam)); + } + + else if (request.MaxWidth.HasValue || + request.MaxHeight.HasValue || + request.Width.HasValue || + request.Height.HasValue) + { + if (state.VideoStream != null) + { + // Need to perform calculations manually + + // Try to account for bad media info + var currentHeight = state.VideoStream.Height ?? request.MaxHeight ?? request.Height ?? 0; + var currentWidth = state.VideoStream.Width ?? request.MaxWidth ?? request.Width ?? 0; + + var outputSize = DrawingUtils.Resize(currentWidth, currentHeight, request.Width, request.Height, request.MaxWidth, request.MaxHeight); + + var manualWidthParam = outputSize.Width.ToString(UsCulture); + var manualHeightParam = outputSize.Height.ToString(UsCulture); + + filters.Add(string.Format("scale=trunc({0}/2)*2:trunc({1}/2)*2", manualWidthParam, manualHeightParam)); + } + } + + var output = string.Empty; + + if (state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream) + { + var subParam = GetTextSubtitleParam(state); + + filters.Add(subParam); + + if (allowTimeStampCopy) + { + output += " -copyts"; + } + } + + if (filters.Count > 0) + { + output += string.Format(" -vf \"{0}\"", string.Join(",", filters.ToArray())); + } + + return output; + } + + /// + /// Gets the text subtitle param. + /// + /// The state. + /// System.String. + protected string GetTextSubtitleParam(EncodingJob state) + { + var seconds = Math.Round(TimeSpan.FromTicks(state.Options.StartTimeTicks ?? 0).TotalSeconds); + + if (state.SubtitleStream.IsExternal) + { + var subtitlePath = state.SubtitleStream.Path; + + var charsetParam = string.Empty; + + if (!string.IsNullOrEmpty(state.SubtitleStream.Language)) + { + var charenc = SubtitleEncoder.GetSubtitleFileCharacterSet(subtitlePath, state.SubtitleStream.Language); + + if (!string.IsNullOrEmpty(charenc)) + { + charsetParam = ":charenc=" + charenc; + } + } + + // TODO: Perhaps also use original_size=1920x800 ?? + return string.Format("subtitles=filename='{0}'{1},setpts=PTS -{2}/TB", + subtitlePath.Replace('\\', '/').Replace(":/", "\\:/"), + charsetParam, + seconds.ToString(UsCulture)); + } + + return string.Format("subtitles='{0}:si={1}',setpts=PTS -{2}/TB", + state.MediaPath.Replace('\\', '/').Replace(":/", "\\:/"), + state.InternalSubtitleStreamOffset.ToString(UsCulture), + seconds.ToString(UsCulture)); + } + + protected string GetAudioFilterParam(EncodingJob state, bool isHls) + { + var volParam = string.Empty; + var audioSampleRate = string.Empty; + + var channels = state.OutputAudioChannels; + + // Boost volume to 200% when downsampling from 6ch to 2ch + if (channels.HasValue && channels.Value <= 2) + { + if (state.AudioStream != null && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5) + { + volParam = ",volume=" + GetEncodingOptions().DownMixAudioBoost.ToString(UsCulture); + } + } + + if (state.OutputAudioSampleRate.HasValue) + { + audioSampleRate = state.OutputAudioSampleRate.Value + ":"; + } + + var adelay = isHls ? "adelay=1," : string.Empty; + + var pts = string.Empty; + + if (state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream) + { + var seconds = TimeSpan.FromTicks(state.Options.StartTimeTicks ?? 0).TotalSeconds; + + pts = string.Format(",asetpts=PTS-{0}/TB", Math.Round(seconds).ToString(UsCulture)); + } + + return string.Format("-af \"{0}aresample={1}async={4}{2}{3}\"", + + adelay, + audioSampleRate, + volParam, + pts, + state.OutputAudioSync); + } + } +} diff --git a/MediaBrowser.MediaEncoding/Encoder/EncodingJob.cs b/MediaBrowser.MediaEncoding/Encoder/EncodingJob.cs new file mode 100644 index 000000000..40ca08c40 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Encoder/EncodingJob.cs @@ -0,0 +1,434 @@ +using MediaBrowser.Controller.LiveTv; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Model.Dlna; +using MediaBrowser.Model.Drawing; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.MediaInfo; +using MediaBrowser.Model.Net; +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.MediaEncoding.Encoder +{ + public class EncodingJob : IDisposable + { + public bool HasExited { get; internal set; } + + public Stream LogFileStream { get; set; } + public IProgress Progress { get; set; } + public TaskCompletionSource TaskCompletionSource; + + public EncodingJobOptions Options { get; set; } + public string InputContainer { get; set; } + public List AllMediaStreams { get; set; } + public MediaStream AudioStream { get; set; } + public MediaStream VideoStream { get; set; } + public MediaStream SubtitleStream { get; set; } + public IIsoMount IsoMount { get; set; } + + public bool ReadInputAtNativeFramerate { get; set; } + public bool IsVideoRequest { get; set; } + public string InputAudioSync { get; set; } + public string InputVideoSync { get; set; } + public string Id { get; set; } + + public string MediaPath { get; set; } + public MediaProtocol InputProtocol { get; set; } + public bool IsInputVideo { get; set; } + public VideoType VideoType { get; set; } + public IsoType? IsoType { get; set; } + public List PlayableStreamFileNames { get; set; } + + public List SupportedAudioCodecs { get; set; } + public Dictionary RemoteHttpHeaders { get; set; } + public TransportStreamTimestamp InputTimestamp { get; set; } + + public bool DeInterlace { get; set; } + public string MimeType { get; set; } + public bool EstimateContentLength { get; set; } + public bool EnableMpegtsM2TsMode { get; set; } + public TranscodeSeekInfo TranscodeSeekInfo { get; set; } + public long? EncodingDurationTicks { get; set; } + public string LiveTvStreamId { get; set; } + public long? RunTimeTicks; + + public string ItemType { get; set; } + + public long? InputBitrate { get; set; } + public long? InputFileSize { get; set; } + public string OutputAudioSync = "1"; + public string OutputVideoSync = "vfr"; + + public string GetMimeType(string outputPath) + { + if (!string.IsNullOrEmpty(MimeType)) + { + return MimeType; + } + + return MimeTypes.GetMimeType(outputPath); + } + + private readonly ILogger _logger; + private readonly ILiveTvManager _liveTvManager; + + public EncodingJob(ILogger logger, ILiveTvManager liveTvManager) + { + _logger = logger; + _liveTvManager = liveTvManager; + Id = Guid.NewGuid().ToString("N"); + + RemoteHttpHeaders = new Dictionary(StringComparer.OrdinalIgnoreCase); + _logger = logger; + SupportedAudioCodecs = new List(); + PlayableStreamFileNames = new List(); + RemoteHttpHeaders = new Dictionary(StringComparer.OrdinalIgnoreCase); + AllMediaStreams = new List(); + TaskCompletionSource = new TaskCompletionSource(); + } + + public void Dispose() + { + DisposeLiveStream(); + DisposeLogStream(); + DisposeIsoMount(); + } + + private void DisposeLogStream() + { + if (LogFileStream != null) + { + try + { + LogFileStream.Dispose(); + } + catch (Exception ex) + { + _logger.ErrorException("Error disposing log stream", ex); + } + + LogFileStream = null; + } + } + + private void DisposeIsoMount() + { + if (IsoMount != null) + { + try + { + IsoMount.Dispose(); + } + catch (Exception ex) + { + _logger.ErrorException("Error disposing iso mount", ex); + } + + IsoMount = null; + } + } + + private async void DisposeLiveStream() + { + if (!string.IsNullOrEmpty(LiveTvStreamId)) + { + try + { + await _liveTvManager.CloseLiveStream(LiveTvStreamId, CancellationToken.None).ConfigureAwait(false); + } + catch (Exception ex) + { + _logger.ErrorException("Error closing live tv stream", ex); + } + } + } + + public int InternalSubtitleStreamOffset { get; set; } + + public string OutputFilePath { get; set; } + public string OutputVideoCodec { get; set; } + public string OutputAudioCodec { get; set; } + public int? OutputAudioChannels; + public int? OutputAudioSampleRate; + public int? OutputAudioBitrate; + public int? OutputVideoBitrate; + + public string ActualOutputVideoCodec + { + get + { + var codec = OutputVideoCodec; + + if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase)) + { + var stream = VideoStream; + + if (stream != null) + { + return stream.Codec; + } + + return null; + } + + return codec; + } + } + + public string ActualOutputAudioCodec + { + get + { + var codec = OutputAudioCodec; + + if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase)) + { + var stream = AudioStream; + + if (stream != null) + { + return stream.Codec; + } + + return null; + } + + return codec; + } + } + + public int? TotalOutputBitrate + { + get + { + return (OutputAudioBitrate ?? 0) + (OutputVideoBitrate ?? 0); + } + } + + public int? OutputWidth + { + get + { + if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue) + { + var size = new ImageSize + { + Width = VideoStream.Width.Value, + Height = VideoStream.Height.Value + }; + + var newSize = DrawingUtils.Resize(size, + Options.Width, + Options.Height, + Options.MaxWidth, + Options.MaxHeight); + + return Convert.ToInt32(newSize.Width); + } + + if (!IsVideoRequest) + { + return null; + } + + return Options.MaxWidth ?? Options.Width; + } + } + + public int? OutputHeight + { + get + { + if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue) + { + var size = new ImageSize + { + Width = VideoStream.Width.Value, + Height = VideoStream.Height.Value + }; + + var newSize = DrawingUtils.Resize(size, + Options.Width, + Options.Height, + Options.MaxWidth, + Options.MaxHeight); + + return Convert.ToInt32(newSize.Height); + } + + if (!IsVideoRequest) + { + return null; + } + + return Options.MaxHeight ?? Options.Height; + } + } + + /// + /// Predicts the audio sample rate that will be in the output stream + /// + public int? TargetVideoBitDepth + { + get + { + var stream = VideoStream; + return stream == null || !Options.Static ? null : stream.BitDepth; + } + } + + /// + /// Gets the target reference frames. + /// + /// The target reference frames. + public int? TargetRefFrames + { + get + { + var stream = VideoStream; + return stream == null || !Options.Static ? null : stream.RefFrames; + } + } + + /// + /// Predicts the audio sample rate that will be in the output stream + /// + public float? TargetFramerate + { + get + { + var stream = VideoStream; + var requestedFramerate = Options.MaxFramerate ?? Options.Framerate; + + return requestedFramerate.HasValue && !Options.Static + ? requestedFramerate + : stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate; + } + } + + /// + /// Predicts the audio sample rate that will be in the output stream + /// + public double? TargetVideoLevel + { + get + { + var stream = VideoStream; + return Options.Level.HasValue && !Options.Static + ? Options.Level.Value + : stream == null ? null : stream.Level; + } + } + + public TransportStreamTimestamp TargetTimestamp + { + get + { + var defaultValue = string.Equals(Options.OutputContainer, "m2ts", StringComparison.OrdinalIgnoreCase) ? + TransportStreamTimestamp.Valid : + TransportStreamTimestamp.None; + + return !Options.Static + ? defaultValue + : InputTimestamp; + } + } + + /// + /// Predicts the audio sample rate that will be in the output stream + /// + public int? TargetPacketLength + { + get + { + var stream = VideoStream; + return !Options.Static + ? null + : stream == null ? null : stream.PacketLength; + } + } + + /// + /// Predicts the audio sample rate that will be in the output stream + /// + public string TargetVideoProfile + { + get + { + var stream = VideoStream; + return !string.IsNullOrEmpty(Options.Profile) && !Options.Static + ? Options.Profile + : stream == null ? null : stream.Profile; + } + } + + public bool? IsTargetAnamorphic + { + get + { + if (Options.Static) + { + return VideoStream == null ? null : VideoStream.IsAnamorphic; + } + + return false; + } + } + + public bool? IsTargetCabac + { + get + { + if (Options.Static) + { + return VideoStream == null ? null : VideoStream.IsCabac; + } + + return true; + } + } + + public void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded) + { + var ticks = transcodingPosition.HasValue ? transcodingPosition.Value.Ticks : (long?)null; + + // job.Framerate = framerate; + + if (percentComplete.HasValue) + { + Progress.Report(percentComplete.Value); + } + + // job.TranscodingPositionTicks = ticks; + // job.BytesTranscoded = bytesTranscoded; + + var deviceId = Options.DeviceId; + + if (!string.IsNullOrWhiteSpace(deviceId)) + { + var audioCodec = ActualOutputVideoCodec; + var videoCodec = ActualOutputVideoCodec; + + // SessionManager.ReportTranscodingInfo(deviceId, new TranscodingInfo + // { + // Bitrate = job.TotalOutputBitrate, + // AudioCodec = audioCodec, + // VideoCodec = videoCodec, + // Container = job.Options.OutputContainer, + // Framerate = framerate, + // CompletionPercentage = percentComplete, + // Width = job.OutputWidth, + // Height = job.OutputHeight, + // AudioChannels = job.OutputAudioChannels, + // IsAudioDirect = string.Equals(job.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase), + // IsVideoDirect = string.Equals(job.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase) + // }); + } + } + } +} diff --git a/MediaBrowser.MediaEncoding/Encoder/EncodingJobFactory.cs b/MediaBrowser.MediaEncoding/Encoder/EncodingJobFactory.cs new file mode 100644 index 000000000..00c7b61e3 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Encoder/EncodingJobFactory.cs @@ -0,0 +1,830 @@ +using MediaBrowser.Controller.Channels; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.LiveTv; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Model.Dlna; +using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.MediaInfo; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.MediaEncoding.Encoder +{ + public class EncodingJobFactory + { + private readonly ILogger _logger; + private readonly ILiveTvManager _liveTvManager; + private readonly ILibraryManager _libraryManager; + private readonly IChannelManager _channelManager; + + protected static readonly CultureInfo UsCulture = new CultureInfo("en-US"); + + public EncodingJobFactory(ILogger logger, ILiveTvManager liveTvManager, ILibraryManager libraryManager, IChannelManager channelManager) + { + _logger = logger; + _liveTvManager = liveTvManager; + _libraryManager = libraryManager; + _channelManager = channelManager; + } + + public async Task CreateJob(EncodingJobOptions options, bool isVideoRequest, IProgress progress, CancellationToken cancellationToken) + { + var request = options; + + if (string.IsNullOrEmpty(request.AudioCodec)) + { + request.AudioCodec = InferAudioCodec(request.OutputContainer); + } + + var state = new EncodingJob(_logger, _liveTvManager) + { + Options = options, + IsVideoRequest = isVideoRequest, + Progress = progress + }; + + if (!string.IsNullOrWhiteSpace(request.AudioCodec)) + { + state.SupportedAudioCodecs = request.AudioCodec.Split(',').Where(i => !string.IsNullOrWhiteSpace(i)).ToList(); + request.AudioCodec = state.SupportedAudioCodecs.FirstOrDefault(); + } + + var item = _libraryManager.GetItemById(request.ItemId); + + List mediaStreams = null; + + state.ItemType = item.GetType().Name; + + if (item is ILiveTvRecording) + { + var recording = await _liveTvManager.GetInternalRecording(request.ItemId, cancellationToken).ConfigureAwait(false); + + state.VideoType = VideoType.VideoFile; + state.IsInputVideo = string.Equals(recording.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase); + + var path = recording.RecordingInfo.Path; + var mediaUrl = recording.RecordingInfo.Url; + + var source = string.IsNullOrEmpty(request.MediaSourceId) + ? recording.GetMediaSources(false).First() + : recording.GetMediaSources(false).First(i => string.Equals(i.Id, request.MediaSourceId)); + + mediaStreams = source.MediaStreams; + + // Just to prevent this from being null and causing other methods to fail + state.MediaPath = string.Empty; + + if (!string.IsNullOrEmpty(path)) + { + state.MediaPath = path; + state.InputProtocol = MediaProtocol.File; + } + else if (!string.IsNullOrEmpty(mediaUrl)) + { + state.MediaPath = mediaUrl; + state.InputProtocol = MediaProtocol.Http; + } + + state.RunTimeTicks = recording.RunTimeTicks; + state.DeInterlace = true; + state.OutputAudioSync = "1000"; + state.InputVideoSync = "-1"; + state.InputAudioSync = "1"; + state.InputContainer = recording.Container; + state.ReadInputAtNativeFramerate = source.ReadAtNativeFramerate; + } + else if (item is LiveTvChannel) + { + var channel = _liveTvManager.GetInternalChannel(request.ItemId); + + state.VideoType = VideoType.VideoFile; + state.IsInputVideo = string.Equals(channel.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase); + mediaStreams = new List(); + + state.DeInterlace = true; + + // Just to prevent this from being null and causing other methods to fail + state.MediaPath = string.Empty; + } + else if (item is IChannelMediaItem) + { + var mediaSource = await GetChannelMediaInfo(request.ItemId, request.MediaSourceId, cancellationToken).ConfigureAwait(false); + state.IsInputVideo = string.Equals(item.MediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase); + state.InputProtocol = mediaSource.Protocol; + state.MediaPath = mediaSource.Path; + state.RunTimeTicks = item.RunTimeTicks; + state.RemoteHttpHeaders = mediaSource.RequiredHttpHeaders; + state.InputBitrate = mediaSource.Bitrate; + state.InputFileSize = mediaSource.Size; + state.ReadInputAtNativeFramerate = mediaSource.ReadAtNativeFramerate; + mediaStreams = mediaSource.MediaStreams; + } + else + { + var hasMediaSources = (IHasMediaSources)item; + var mediaSource = string.IsNullOrEmpty(request.MediaSourceId) + ? hasMediaSources.GetMediaSources(false).First() + : hasMediaSources.GetMediaSources(false).First(i => string.Equals(i.Id, request.MediaSourceId)); + + mediaStreams = mediaSource.MediaStreams; + + state.MediaPath = mediaSource.Path; + state.InputProtocol = mediaSource.Protocol; + state.InputContainer = mediaSource.Container; + state.InputFileSize = mediaSource.Size; + state.InputBitrate = mediaSource.Bitrate; + state.ReadInputAtNativeFramerate = mediaSource.ReadAtNativeFramerate; + + var video = item as Video; + + if (video != null) + { + state.IsInputVideo = true; + + if (mediaSource.VideoType.HasValue) + { + state.VideoType = mediaSource.VideoType.Value; + } + + state.IsoType = mediaSource.IsoType; + + state.PlayableStreamFileNames = mediaSource.PlayableStreamFileNames.ToList(); + + if (mediaSource.Timestamp.HasValue) + { + state.InputTimestamp = mediaSource.Timestamp.Value; + } + } + + state.RunTimeTicks = mediaSource.RunTimeTicks; + } + + AttachMediaStreamInfo(state, mediaStreams, request); + + state.OutputAudioBitrate = GetAudioBitrateParam(request, state.AudioStream); + state.OutputAudioSampleRate = request.AudioSampleRate; + + state.OutputAudioCodec = GetAudioCodec(request); + + state.OutputAudioChannels = GetNumAudioChannelsParam(request, state.AudioStream, state.OutputAudioCodec); + + if (isVideoRequest) + { + state.OutputVideoCodec = GetVideoCodec(request); + state.OutputVideoBitrate = GetVideoBitrateParamValue(request, state.VideoStream); + + if (state.OutputVideoBitrate.HasValue) + { + var resolution = ResolutionNormalizer.Normalize(state.OutputVideoBitrate.Value, + state.OutputVideoCodec, + request.MaxWidth, + request.MaxHeight); + + request.MaxWidth = resolution.MaxWidth; + request.MaxHeight = resolution.MaxHeight; + } + } + + ApplyDeviceProfileSettings(state); + + if (isVideoRequest) + { + if (state.VideoStream != null && CanStreamCopyVideo(request, state.VideoStream)) + { + state.OutputVideoCodec = "copy"; + } + + if (state.AudioStream != null && CanStreamCopyAudio(request, state.AudioStream, state.SupportedAudioCodecs)) + { + state.OutputAudioCodec = "copy"; + } + } + + return state; + } + + internal static void AttachMediaStreamInfo(EncodingJob state, + List mediaStreams, + EncodingJobOptions videoRequest) + { + if (videoRequest != null) + { + if (string.IsNullOrEmpty(videoRequest.VideoCodec)) + { + videoRequest.VideoCodec = InferVideoCodec(videoRequest.OutputContainer); + } + + state.VideoStream = GetMediaStream(mediaStreams, videoRequest.VideoStreamIndex, MediaStreamType.Video); + state.SubtitleStream = GetMediaStream(mediaStreams, videoRequest.SubtitleStreamIndex, MediaStreamType.Subtitle, false); + state.AudioStream = GetMediaStream(mediaStreams, videoRequest.AudioStreamIndex, MediaStreamType.Audio); + + if (state.SubtitleStream != null && !state.SubtitleStream.IsExternal) + { + state.InternalSubtitleStreamOffset = mediaStreams.Where(i => i.Type == MediaStreamType.Subtitle && !i.IsExternal).ToList().IndexOf(state.SubtitleStream); + } + + if (state.VideoStream != null && state.VideoStream.IsInterlaced) + { + state.DeInterlace = true; + } + + EnforceResolutionLimit(state, videoRequest); + } + else + { + state.AudioStream = GetMediaStream(mediaStreams, null, MediaStreamType.Audio, true); + } + + state.AllMediaStreams = mediaStreams; + } + + /// + /// Infers the video codec. + /// + /// The container. + /// System.Nullable{VideoCodecs}. + private static string InferVideoCodec(string container) + { + if (string.Equals(container, "asf", StringComparison.OrdinalIgnoreCase)) + { + return "wmv"; + } + if (string.Equals(container, "webm", StringComparison.OrdinalIgnoreCase)) + { + return "vpx"; + } + if (string.Equals(container, "ogg", StringComparison.OrdinalIgnoreCase) || string.Equals(container, "ogv", StringComparison.OrdinalIgnoreCase)) + { + return "theora"; + } + if (string.Equals(container, "m3u8", StringComparison.OrdinalIgnoreCase) || string.Equals(container, "ts", StringComparison.OrdinalIgnoreCase)) + { + return "h264"; + } + + return "copy"; + } + + private string InferAudioCodec(string container) + { + if (string.Equals(container, "mp3", StringComparison.OrdinalIgnoreCase)) + { + return "mp3"; + } + if (string.Equals(container, "aac", StringComparison.OrdinalIgnoreCase)) + { + return "aac"; + } + if (string.Equals(container, "wma", StringComparison.OrdinalIgnoreCase)) + { + return "wma"; + } + if (string.Equals(container, "ogg", StringComparison.OrdinalIgnoreCase)) + { + return "vorbis"; + } + if (string.Equals(container, "oga", StringComparison.OrdinalIgnoreCase)) + { + return "vorbis"; + } + if (string.Equals(container, "ogv", StringComparison.OrdinalIgnoreCase)) + { + return "vorbis"; + } + if (string.Equals(container, "webm", StringComparison.OrdinalIgnoreCase)) + { + return "vorbis"; + } + if (string.Equals(container, "webma", StringComparison.OrdinalIgnoreCase)) + { + return "vorbis"; + } + + return "copy"; + } + + /// + /// Determines which stream will be used for playback + /// + /// All stream. + /// Index of the desired. + /// The type. + /// if set to true [return first if no index]. + /// MediaStream. + private static MediaStream GetMediaStream(IEnumerable allStream, int? desiredIndex, MediaStreamType type, bool returnFirstIfNoIndex = true) + { + var streams = allStream.Where(s => s.Type == type).OrderBy(i => i.Index).ToList(); + + if (desiredIndex.HasValue) + { + var stream = streams.FirstOrDefault(s => s.Index == desiredIndex.Value); + + if (stream != null) + { + return stream; + } + } + + if (type == MediaStreamType.Video) + { + streams = streams.Where(i => !string.Equals(i.Codec, "mjpeg", StringComparison.OrdinalIgnoreCase)).ToList(); + } + + if (returnFirstIfNoIndex && type == MediaStreamType.Audio) + { + return streams.FirstOrDefault(i => i.Channels.HasValue && i.Channels.Value > 0) ?? + streams.FirstOrDefault(); + } + + // Just return the first one + return returnFirstIfNoIndex ? streams.FirstOrDefault() : null; + } + + /// + /// Enforces the resolution limit. + /// + /// The state. + /// The video request. + private static void EnforceResolutionLimit(EncodingJob state, EncodingJobOptions videoRequest) + { + // Switch the incoming params to be ceilings rather than fixed values + videoRequest.MaxWidth = videoRequest.MaxWidth ?? videoRequest.Width; + videoRequest.MaxHeight = videoRequest.MaxHeight ?? videoRequest.Height; + + videoRequest.Width = null; + videoRequest.Height = null; + } + + /// + /// Gets the number of audio channels to specify on the command line + /// + /// The request. + /// The audio stream. + /// The output audio codec. + /// System.Nullable{System.Int32}. + private int? GetNumAudioChannelsParam(EncodingJobOptions request, MediaStream audioStream, string outputAudioCodec) + { + if (audioStream != null) + { + var codec = outputAudioCodec ?? string.Empty; + + if (audioStream.Channels > 2 && codec.IndexOf("wma", StringComparison.OrdinalIgnoreCase) != -1) + { + // wmav2 currently only supports two channel output + return 2; + } + } + + if (request.MaxAudioChannels.HasValue) + { + if (audioStream != null && audioStream.Channels.HasValue) + { + return Math.Min(request.MaxAudioChannels.Value, audioStream.Channels.Value); + } + + // If we don't have any media info then limit it to 5 to prevent encoding errors due to asking for too many channels + return Math.Min(request.MaxAudioChannels.Value, 5); + } + + return request.AudioChannels; + } + + private int? GetVideoBitrateParamValue(EncodingJobOptions request, MediaStream videoStream) + { + var bitrate = request.VideoBitRate; + + if (videoStream != null) + { + var isUpscaling = request.Height.HasValue && videoStream.Height.HasValue && + request.Height.Value > videoStream.Height.Value; + + if (request.Width.HasValue && videoStream.Width.HasValue && + request.Width.Value > videoStream.Width.Value) + { + isUpscaling = true; + } + + // Don't allow bitrate increases unless upscaling + if (!isUpscaling) + { + if (bitrate.HasValue && videoStream.BitRate.HasValue) + { + bitrate = Math.Min(bitrate.Value, videoStream.BitRate.Value); + } + } + } + + return bitrate; + } + + private async Task GetChannelMediaInfo(string id, + string mediaSourceId, + CancellationToken cancellationToken) + { + var channelMediaSources = await _channelManager.GetChannelItemMediaSources(id, true, cancellationToken) + .ConfigureAwait(false); + + var list = channelMediaSources.ToList(); + + if (!string.IsNullOrWhiteSpace(mediaSourceId)) + { + var source = list + .FirstOrDefault(i => string.Equals(mediaSourceId, i.Id)); + + if (source != null) + { + return source; + } + } + + return list.First(); + } + + protected string GetVideoBitrateParam(EncodingJob state, string videoCodec, bool isHls) + { + var bitrate = state.OutputVideoBitrate; + + if (bitrate.HasValue) + { + var hasFixedResolution = state.Options.HasFixedResolution; + + if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase)) + { + if (hasFixedResolution) + { + return string.Format(" -minrate:v ({0}*.90) -maxrate:v ({0}*1.10) -bufsize:v {0} -b:v {0}", bitrate.Value.ToString(UsCulture)); + } + + // With vpx when crf is used, b:v becomes a max rate + // https://trac.ffmpeg.org/wiki/vpxEncodingGuide. But higher bitrate source files -b:v causes judder so limite the bitrate but dont allow it to "saturate" the bitrate. So dont contrain it down just up. + return string.Format(" -maxrate:v {0} -bufsize:v ({0}*2) -b:v {0}", bitrate.Value.ToString(UsCulture)); + } + + if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase)) + { + return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture)); + } + + // H264 + if (hasFixedResolution) + { + if (isHls) + { + return string.Format(" -b:v {0} -maxrate ({0}*.80) -bufsize {0}", bitrate.Value.ToString(UsCulture)); + } + + return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture)); + } + + return string.Format(" -maxrate {0} -bufsize {1}", + bitrate.Value.ToString(UsCulture), + (bitrate.Value * 2).ToString(UsCulture)); + } + + return string.Empty; + } + + private int? GetAudioBitrateParam(EncodingJobOptions request, MediaStream audioStream) + { + if (request.AudioBitRate.HasValue) + { + // Make sure we don't request a bitrate higher than the source + var currentBitrate = audioStream == null ? request.AudioBitRate.Value : audioStream.BitRate ?? request.AudioBitRate.Value; + + return request.AudioBitRate.Value; + //return Math.Min(currentBitrate, request.AudioBitRate.Value); + } + + return null; + } + + /// + /// Determines whether the specified stream is H264. + /// + /// The stream. + /// true if the specified stream is H264; otherwise, false. + protected bool IsH264(MediaStream stream) + { + var codec = stream.Codec ?? string.Empty; + + return codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 || + codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1; + } + + /// + /// Gets the name of the output audio codec + /// + /// The request. + /// System.String. + private string GetAudioCodec(EncodingJobOptions request) + { + var codec = request.AudioCodec; + + if (string.Equals(codec, "aac", StringComparison.OrdinalIgnoreCase)) + { + return "aac -strict experimental"; + } + if (string.Equals(codec, "mp3", StringComparison.OrdinalIgnoreCase)) + { + return "libmp3lame"; + } + if (string.Equals(codec, "vorbis", StringComparison.OrdinalIgnoreCase)) + { + return "libvorbis"; + } + if (string.Equals(codec, "wma", StringComparison.OrdinalIgnoreCase)) + { + return "wmav2"; + } + + return (codec ?? string.Empty).ToLower(); + } + + /// + /// Gets the name of the output video codec + /// + /// The request. + /// System.String. + private string GetVideoCodec(EncodingJobOptions request) + { + var codec = request.VideoCodec; + + if (!string.IsNullOrEmpty(codec)) + { + if (string.Equals(codec, "h264", StringComparison.OrdinalIgnoreCase)) + { + return "libx264"; + } + if (string.Equals(codec, "h265", StringComparison.OrdinalIgnoreCase)) + { + return "libx265"; + } + if (string.Equals(codec, "vpx", StringComparison.OrdinalIgnoreCase)) + { + return "libvpx"; + } + if (string.Equals(codec, "wmv", StringComparison.OrdinalIgnoreCase)) + { + return "wmv2"; + } + if (string.Equals(codec, "theora", StringComparison.OrdinalIgnoreCase)) + { + return "libtheora"; + } + + return codec.ToLower(); + } + + return "copy"; + } + + internal static bool CanStreamCopyVideo(EncodingJobOptions request, MediaStream videoStream) + { + if (videoStream.IsInterlaced) + { + return false; + } + + // Can't stream copy if we're burning in subtitles + if (request.SubtitleStreamIndex.HasValue) + { + if (request.SubtitleMethod == SubtitleDeliveryMethod.Encode) + { + return false; + } + } + + // Source and target codecs must match + if (!string.Equals(request.VideoCodec, videoStream.Codec, StringComparison.OrdinalIgnoreCase)) + { + return false; + } + + // If client is requesting a specific video profile, it must match the source + if (!string.IsNullOrEmpty(request.Profile)) + { + if (string.IsNullOrEmpty(videoStream.Profile)) + { + return false; + } + + if (!string.Equals(request.Profile, videoStream.Profile, StringComparison.OrdinalIgnoreCase)) + { + var currentScore = GetVideoProfileScore(videoStream.Profile); + var requestedScore = GetVideoProfileScore(request.Profile); + + if (currentScore == -1 || currentScore > requestedScore) + { + return false; + } + } + } + + // Video width must fall within requested value + if (request.MaxWidth.HasValue) + { + if (!videoStream.Width.HasValue || videoStream.Width.Value > request.MaxWidth.Value) + { + return false; + } + } + + // Video height must fall within requested value + if (request.MaxHeight.HasValue) + { + if (!videoStream.Height.HasValue || videoStream.Height.Value > request.MaxHeight.Value) + { + return false; + } + } + + // Video framerate must fall within requested value + var requestedFramerate = request.MaxFramerate ?? request.Framerate; + if (requestedFramerate.HasValue) + { + var videoFrameRate = videoStream.AverageFrameRate ?? videoStream.RealFrameRate; + + if (!videoFrameRate.HasValue || videoFrameRate.Value > requestedFramerate.Value) + { + return false; + } + } + + // Video bitrate must fall within requested value + if (request.VideoBitRate.HasValue) + { + if (!videoStream.BitRate.HasValue || videoStream.BitRate.Value > request.VideoBitRate.Value) + { + return false; + } + } + + if (request.MaxVideoBitDepth.HasValue) + { + if (videoStream.BitDepth.HasValue && videoStream.BitDepth.Value > request.MaxVideoBitDepth.Value) + { + return false; + } + } + + if (request.MaxRefFrames.HasValue) + { + if (videoStream.RefFrames.HasValue && videoStream.RefFrames.Value > request.MaxRefFrames.Value) + { + return false; + } + } + + // If a specific level was requested, the source must match or be less than + if (request.Level.HasValue) + { + if (!videoStream.Level.HasValue) + { + return false; + } + + if (videoStream.Level.Value > request.Level.Value) + { + return false; + } + } + + if (request.Cabac.HasValue && request.Cabac.Value) + { + if (videoStream.IsCabac.HasValue && !videoStream.IsCabac.Value) + { + return false; + } + } + + return request.EnableAutoStreamCopy; + } + + private static int GetVideoProfileScore(string profile) + { + var list = new List + { + "Constrained Baseline", + "Baseline", + "Extended", + "Main", + "High", + "Progressive High", + "Constrained High" + }; + + return Array.FindIndex(list.ToArray(), t => string.Equals(t, profile, StringComparison.OrdinalIgnoreCase)); + } + + internal static bool CanStreamCopyAudio(EncodingJobOptions request, MediaStream audioStream, List supportedAudioCodecs) + { + // Source and target codecs must match + if (string.IsNullOrEmpty(audioStream.Codec) || !supportedAudioCodecs.Contains(audioStream.Codec, StringComparer.OrdinalIgnoreCase)) + { + return false; + } + + // Video bitrate must fall within requested value + if (request.AudioBitRate.HasValue) + { + if (!audioStream.BitRate.HasValue || audioStream.BitRate.Value <= 0) + { + return false; + } + if (audioStream.BitRate.Value > request.AudioBitRate.Value) + { + return false; + } + } + + // Channels must fall within requested value + var channels = request.AudioChannels ?? request.MaxAudioChannels; + if (channels.HasValue) + { + if (!audioStream.Channels.HasValue || audioStream.Channels.Value <= 0) + { + return false; + } + if (audioStream.Channels.Value > channels.Value) + { + return false; + } + } + + // Sample rate must fall within requested value + if (request.AudioSampleRate.HasValue) + { + if (!audioStream.SampleRate.HasValue || audioStream.SampleRate.Value <= 0) + { + return false; + } + if (audioStream.SampleRate.Value > request.AudioSampleRate.Value) + { + return false; + } + } + + return request.EnableAutoStreamCopy; + } + + private void ApplyDeviceProfileSettings(EncodingJob state) + { + var profile = state.Options.DeviceProfile; + + if (profile == null) + { + // Don't use settings from the default profile. + // Only use a specific profile if it was requested. + return; + } + + var audioCodec = state.ActualOutputAudioCodec; + + var videoCodec = state.ActualOutputVideoCodec; + var outputContainer = state.Options.OutputContainer; + + var mediaProfile = state.IsVideoRequest ? + profile.GetAudioMediaProfile(outputContainer, audioCodec, state.OutputAudioChannels, state.OutputAudioBitrate) : + profile.GetVideoMediaProfile(outputContainer, + audioCodec, + videoCodec, + state.OutputAudioBitrate, + state.OutputAudioChannels, + state.OutputWidth, + state.OutputHeight, + state.TargetVideoBitDepth, + state.OutputVideoBitrate, + state.TargetVideoProfile, + state.TargetVideoLevel, + state.TargetFramerate, + state.TargetPacketLength, + state.TargetTimestamp, + state.IsTargetAnamorphic, + state.IsTargetCabac, + state.TargetRefFrames); + + if (mediaProfile != null) + { + state.MimeType = mediaProfile.MimeType; + } + + var transcodingProfile = state.IsVideoRequest ? + profile.GetAudioTranscodingProfile(outputContainer, audioCodec) : + profile.GetVideoTranscodingProfile(outputContainer, audioCodec, videoCodec); + + if (transcodingProfile != null) + { + state.EstimateContentLength = transcodingProfile.EstimateContentLength; + state.EnableMpegtsM2TsMode = transcodingProfile.EnableMpegtsM2TsMode; + state.TranscodeSeekInfo = transcodingProfile.TranscodeSeekInfo; + } + } + } +} diff --git a/MediaBrowser.MediaEncoding/Encoder/JobLogger.cs b/MediaBrowser.MediaEncoding/Encoder/JobLogger.cs new file mode 100644 index 000000000..6be870519 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Encoder/JobLogger.cs @@ -0,0 +1,122 @@ +using MediaBrowser.Common.Extensions; +using MediaBrowser.Model.Logging; +using System; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; + +namespace MediaBrowser.MediaEncoding.Encoder +{ + public class JobLogger + { + private readonly CultureInfo _usCulture = new CultureInfo("en-US"); + private readonly ILogger _logger; + + public JobLogger(ILogger logger) + { + _logger = logger; + } + + public async void StartStreamingLog(EncodingJob transcodingJob, Stream source, Stream target) + { + try + { + using (var reader = new StreamReader(source)) + { + while (!reader.EndOfStream) + { + var line = await reader.ReadLineAsync().ConfigureAwait(false); + + ParseLogLine(line, transcodingJob); + + var bytes = Encoding.UTF8.GetBytes(Environment.NewLine + line); + + await target.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false); + } + } + } + catch (Exception ex) + { + _logger.ErrorException("Error reading ffmpeg log", ex); + } + } + + private void ParseLogLine(string line, EncodingJob transcodingJob) + { + float? framerate = null; + double? percent = null; + TimeSpan? transcodingPosition = null; + long? bytesTranscoded = null; + + var parts = line.Split(' '); + + var totalMs = transcodingJob.RunTimeTicks.HasValue + ? TimeSpan.FromTicks(transcodingJob.RunTimeTicks.Value).TotalMilliseconds + : 0; + + var startMs = transcodingJob.Options.StartTimeTicks.HasValue + ? TimeSpan.FromTicks(transcodingJob.Options.StartTimeTicks.Value).TotalMilliseconds + : 0; + + for (var i = 0; i < parts.Length; i++) + { + var part = parts[i]; + + if (string.Equals(part, "fps=", StringComparison.OrdinalIgnoreCase) && + (i + 1 < parts.Length)) + { + var rate = parts[i + 1]; + float val; + + if (float.TryParse(rate, NumberStyles.Any, _usCulture, out val)) + { + framerate = val; + } + } + else if (transcodingJob.RunTimeTicks.HasValue && + part.StartsWith("time=", StringComparison.OrdinalIgnoreCase)) + { + var time = part.Split(new[] { '=' }, 2).Last(); + TimeSpan val; + + if (TimeSpan.TryParse(time, _usCulture, out val)) + { + var currentMs = startMs + val.TotalMilliseconds; + + var percentVal = currentMs / totalMs; + percent = 100 * percentVal; + + transcodingPosition = val; + } + } + else if (part.StartsWith("size=", StringComparison.OrdinalIgnoreCase)) + { + var size = part.Split(new[] { '=' }, 2).Last(); + + int? scale = null; + if (size.IndexOf("kb", StringComparison.OrdinalIgnoreCase) != -1) + { + scale = 1024; + size = size.Replace("kb", string.Empty, StringComparison.OrdinalIgnoreCase); + } + + if (scale.HasValue) + { + long val; + + if (long.TryParse(size, NumberStyles.Any, _usCulture, out val)) + { + bytesTranscoded = val * scale.Value; + } + } + } + } + + if (framerate.HasValue || percent.HasValue) + { + transcodingJob.ReportTranscodingProgress(transcodingPosition, framerate, percent, bytesTranscoded); + } + } + } +} diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs index a7d8b6f1d..e800b4254 100644 --- a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs +++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs @@ -64,8 +64,9 @@ namespace MediaBrowser.MediaEncoding.Encoder protected readonly ILibraryManager LibraryManager; protected readonly IChannelManager ChannelManager; protected readonly ISessionManager SessionManager; - - public MediaEncoder(ILogger logger, IJsonSerializer jsonSerializer, string ffMpegPath, string ffProbePath, string version, IServerConfigurationManager configurationManager, IFileSystem fileSystem, ILiveTvManager liveTvManager, IIsoManager isoManager, ILibraryManager libraryManager, IChannelManager channelManager, ISessionManager sessionManager) + protected readonly Func SubtitleEncoder; + + public MediaEncoder(ILogger logger, IJsonSerializer jsonSerializer, string ffMpegPath, string ffProbePath, string version, IServerConfigurationManager configurationManager, IFileSystem fileSystem, ILiveTvManager liveTvManager, IIsoManager isoManager, ILibraryManager libraryManager, IChannelManager channelManager, ISessionManager sessionManager, Func subtitleEncoder) { _logger = logger; _jsonSerializer = jsonSerializer; @@ -77,6 +78,7 @@ namespace MediaBrowser.MediaEncoding.Encoder LibraryManager = libraryManager; ChannelManager = channelManager; SessionManager = sessionManager; + SubtitleEncoder = subtitleEncoder; FFProbePath = ffProbePath; FFMpegPath = ffMpegPath; } @@ -494,7 +496,7 @@ namespace MediaBrowser.MediaEncoding.Encoder }; _logger.Info(process.StartInfo.FileName + " " + process.StartInfo.Arguments); - + await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); process.Start(); @@ -537,15 +539,37 @@ namespace MediaBrowser.MediaEncoding.Encoder IProgress progress, CancellationToken cancellationToken) { - var job = await new AudioEncoder(this, - _logger, - ConfigurationManager, - FileSystem, + var job = await new AudioEncoder(this, + _logger, + ConfigurationManager, + FileSystem, + LiveTvManager, + IsoManager, + LibraryManager, + ChannelManager, + SessionManager, + SubtitleEncoder()) + .Start(options, progress, cancellationToken).ConfigureAwait(false); + + await job.TaskCompletionSource.Task.ConfigureAwait(false); + + return job.OutputFilePath; + } + + public async Task EncodeVideo(EncodingJobOptions options, + IProgress progress, + CancellationToken cancellationToken) + { + var job = await new VideoEncoder(this, + _logger, + ConfigurationManager, + FileSystem, LiveTvManager, - IsoManager, - LibraryManager, - ChannelManager, - SessionManager) + IsoManager, + LibraryManager, + ChannelManager, + SessionManager, + SubtitleEncoder()) .Start(options, progress, cancellationToken).ConfigureAwait(false); await job.TaskCompletionSource.Task.ConfigureAwait(false); diff --git a/MediaBrowser.MediaEncoding/Encoder/VideoEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/VideoEncoder.cs new file mode 100644 index 000000000..36406e3a3 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Encoder/VideoEncoder.cs @@ -0,0 +1,177 @@ +using MediaBrowser.Common.IO; +using MediaBrowser.Controller.Channels; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.LiveTv; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Controller.Session; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Logging; +using System; +using System.IO; + +namespace MediaBrowser.MediaEncoding.Encoder +{ + public class VideoEncoder : BaseEncoder + { + public VideoEncoder(MediaEncoder mediaEncoder, ILogger logger, IServerConfigurationManager configurationManager, IFileSystem fileSystem, ILiveTvManager liveTvManager, IIsoManager isoManager, ILibraryManager libraryManager, IChannelManager channelManager, ISessionManager sessionManager, ISubtitleEncoder subtitleEncoder) : base(mediaEncoder, logger, configurationManager, fileSystem, liveTvManager, isoManager, libraryManager, channelManager, sessionManager, subtitleEncoder) + { + } + + protected override string GetCommandLineArguments(EncodingJob state) + { + // Get the output codec name + var videoCodec = state.OutputVideoCodec; + + var format = string.Empty; + var keyFrame = string.Empty; + + if (string.Equals(Path.GetExtension(state.OutputFilePath), ".mp4", StringComparison.OrdinalIgnoreCase)) + { + format = " -f mp4 -movflags frag_keyframe+empty_moov"; + } + + var threads = GetNumberOfThreads(state, string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase)); + + var inputModifier = GetInputModifier(state); + + return string.Format("{0} {1}{2} {3} {4} -map_metadata -1 -threads {5} {6}{7} -y \"{8}\"", + inputModifier, + GetInputArgument(state), + keyFrame, + GetMapArgs(state), + GetVideoArguments(state, videoCodec), + threads, + GetAudioArguments(state), + format, + state.OutputFilePath + ).Trim(); + } + + /// + /// Gets video arguments to pass to ffmpeg + /// + /// The state. + /// The video codec. + /// System.String. + private string GetVideoArguments(EncodingJob state, string codec) + { + var args = "-codec:v:0 " + codec; + + if (state.EnableMpegtsM2TsMode) + { + args += " -mpegts_m2ts_mode 1"; + } + + // See if we can save come cpu cycles by avoiding encoding + if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase)) + { + return state.VideoStream != null && IsH264(state.VideoStream) && string.Equals(state.Options.OutputContainer, "ts", StringComparison.OrdinalIgnoreCase) ? + args + " -bsf:v h264_mp4toannexb" : + args; + } + + var keyFrameArg = string.Format(" -force_key_frames expr:gte(t,n_forced*{0})", + 5.ToString(UsCulture)); + + args += keyFrameArg; + + var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream; + + // Add resolution params, if specified + if (!hasGraphicalSubs) + { + args += GetOutputSizeParam(state, codec); + } + + var qualityParam = GetVideoQualityParam(state, codec, false); + + if (!string.IsNullOrEmpty(qualityParam)) + { + args += " " + qualityParam.Trim(); + } + + // This is for internal graphical subs + if (hasGraphicalSubs) + { + args += GetGraphicalSubtitleParam(state, codec); + } + + return args; + } + + /// + /// Gets audio arguments to pass to ffmpeg + /// + /// The state. + /// System.String. + private string GetAudioArguments(EncodingJob state) + { + // If the video doesn't have an audio stream, return a default. + if (state.AudioStream == null && state.VideoStream != null) + { + return string.Empty; + } + + // Get the output codec name + var codec = state.OutputAudioCodec; + + var args = "-codec:a:0 " + codec; + + if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase)) + { + return args; + } + + // Add the number of audio channels + var channels = state.OutputAudioChannels; + + if (channels.HasValue) + { + args += " -ac " + channels.Value; + } + + var bitrate = state.OutputAudioBitrate; + + if (bitrate.HasValue) + { + args += " -ab " + bitrate.Value.ToString(UsCulture); + } + + args += " " + GetAudioFilterParam(state, false); + + return args; + } + + protected override string GetOutputFileExtension(EncodingJob state) + { + var ext = base.GetOutputFileExtension(state); + + if (!string.IsNullOrEmpty(ext)) + { + return ext; + } + + var videoCodec = state.Options.VideoCodec; + + if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase)) + { + return ".ts"; + } + if (string.Equals(videoCodec, "theora", StringComparison.OrdinalIgnoreCase)) + { + return ".ogv"; + } + if (string.Equals(videoCodec, "vpx", StringComparison.OrdinalIgnoreCase)) + { + return ".webm"; + } + if (string.Equals(videoCodec, "wmv", StringComparison.OrdinalIgnoreCase)) + { + return ".asf"; + } + + return null; + } + } +} diff --git a/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj b/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj index 9daa3319f..38d8fa138 100644 --- a/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj +++ b/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj @@ -64,6 +64,7 @@ + diff --git a/MediaBrowser.Server.Implementations/Sync/SyncJobProcessor.cs b/MediaBrowser.Server.Implementations/Sync/SyncJobProcessor.cs index 8e5b765a6..896e49cb2 100644 --- a/MediaBrowser.Server.Implementations/Sync/SyncJobProcessor.cs +++ b/MediaBrowser.Server.Implementations/Sync/SyncJobProcessor.cs @@ -412,7 +412,7 @@ namespace MediaBrowser.Server.Implementations.Sync jobItem.Status = SyncJobItemStatus.Converting; await _syncRepo.Update(jobItem).ConfigureAwait(false); - //jobItem.OutputPath = await MediaEncoder.EncodeAudio(new EncodingJobOptions(streamInfo, profile), new Progress(), cancellationToken); + jobItem.OutputPath = await MediaEncoder.EncodeVideo(new EncodingJobOptions(streamInfo, profile), new Progress(), cancellationToken); } else { @@ -420,7 +420,7 @@ namespace MediaBrowser.Server.Implementations.Sync { jobItem.OutputPath = mediaSource.Path; } - if (mediaSource.Protocol == MediaProtocol.Http) + else if (mediaSource.Protocol == MediaProtocol.Http) { jobItem.OutputPath = await DownloadFile(jobItem, mediaSource, cancellationToken).ConfigureAwait(false); } @@ -464,7 +464,7 @@ namespace MediaBrowser.Server.Implementations.Sync { jobItem.OutputPath = mediaSource.Path; } - if (mediaSource.Protocol == MediaProtocol.Http) + else if (mediaSource.Protocol == MediaProtocol.Http) { jobItem.OutputPath = await DownloadFile(jobItem, mediaSource, cancellationToken).ConfigureAwait(false); } diff --git a/MediaBrowser.Server.Startup.Common/ApplicationHost.cs b/MediaBrowser.Server.Startup.Common/ApplicationHost.cs index 8a1721b7e..41f0a2806 100644 --- a/MediaBrowser.Server.Startup.Common/ApplicationHost.cs +++ b/MediaBrowser.Server.Startup.Common/ApplicationHost.cs @@ -185,6 +185,7 @@ namespace MediaBrowser.Server.Startup.Common /// /// The media encoder. private IMediaEncoder MediaEncoder { get; set; } + private ISubtitleEncoder SubtitleEncoder { get; set; } private IConnectManager ConnectManager { get; set; } private ISessionManager SessionManager { get; set; } @@ -560,7 +561,8 @@ namespace MediaBrowser.Server.Startup.Common RegisterSingleInstance(new SessionContext(UserManager, authContext, SessionManager)); RegisterSingleInstance(new AuthService(UserManager, authContext, ServerConfigurationManager, ConnectManager, SessionManager, DeviceManager)); - RegisterSingleInstance(new SubtitleEncoder(LibraryManager, LogManager.GetLogger("SubtitleEncoder"), ApplicationPaths, FileSystemManager, MediaEncoder, JsonSerializer)); + SubtitleEncoder = new SubtitleEncoder(LibraryManager, LogManager.GetLogger("SubtitleEncoder"), ApplicationPaths, FileSystemManager, MediaEncoder, JsonSerializer); + RegisterSingleInstance(SubtitleEncoder); await ConfigureDisplayPreferencesRepositories().ConfigureAwait(false); await ConfigureItemRepositories().ConfigureAwait(false); @@ -602,7 +604,8 @@ namespace MediaBrowser.Server.Startup.Common IsoManager, LibraryManager, ChannelManager, - SessionManager); + SessionManager, + () => SubtitleEncoder); RegisterSingleInstance(MediaEncoder); } -- cgit v1.2.3 From 36295aa833bf5d152613500a4546aa76314150a8 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 3 Jan 2015 14:38:22 -0500 Subject: use SocketHttpListener project --- MediaBrowser.Api/Dlna/DlnaServerService.cs | 8 +--- MediaBrowser.Api/IHasDtoOptions.cs | 2 +- MediaBrowser.Api/Playback/BaseStreamingService.cs | 2 +- MediaBrowser.Controller/Dto/DtoOptions.cs | 47 ++++++++++++++++++++++ .../MediaBrowser.Controller.csproj | 1 + .../Encoder/EncodingUtils.cs | 16 +++++++- .../MediaBrowser.Model.Portable.csproj | 3 -- .../MediaBrowser.Model.net35.csproj | 3 -- MediaBrowser.Model/Dto/DtoOptions.cs | 47 ---------------------- MediaBrowser.Model/MediaBrowser.Model.csproj | 1 - .../Drawing/ImageProcessor.cs | 2 +- .../FileOrganization/EpisodeFileOrganizer.cs | 1 + .../HttpServer/SocketSharp/Extensions.cs | 6 +-- .../HttpServer/SocketSharp/SharpWebSocket.cs | 10 ++--- .../HttpServer/SocketSharp/SocketSharpLogger.cs | 2 +- .../SocketSharp/WebSocketSharpListener.cs | 2 +- .../SocketSharp/WebSocketSharpRequest.cs | 2 +- .../SocketSharp/WebSocketSharpResponse.cs | 2 +- .../MediaBrowser.Server.Implementations.csproj | 7 ++-- .../Sync/SyncManager.cs | 26 +++++++++--- .../MediaBrowser.WebDashboard.csproj | 24 ----------- 21 files changed, 105 insertions(+), 109 deletions(-) create mode 100644 MediaBrowser.Controller/Dto/DtoOptions.cs delete mode 100644 MediaBrowser.Model/Dto/DtoOptions.cs (limited to 'MediaBrowser.MediaEncoding') diff --git a/MediaBrowser.Api/Dlna/DlnaServerService.cs b/MediaBrowser.Api/Dlna/DlnaServerService.cs index 9981d506e..94d6e25b0 100644 --- a/MediaBrowser.Api/Dlna/DlnaServerService.cs +++ b/MediaBrowser.Api/Dlna/DlnaServerService.cs @@ -1,5 +1,4 @@ -using MediaBrowser.Common.Configuration; -using MediaBrowser.Controller.Dlna; +using MediaBrowser.Controller.Dlna; using ServiceStack; using ServiceStack.Text.Controller; using ServiceStack.Web; @@ -77,14 +76,11 @@ namespace MediaBrowser.Api.Dlna private readonly IContentDirectory _contentDirectory; private readonly IConnectionManager _connectionManager; - private readonly IConfigurationManager _config; - - public DlnaServerService(IDlnaManager dlnaManager, IContentDirectory contentDirectory, IConnectionManager connectionManager, IConfigurationManager config) + public DlnaServerService(IDlnaManager dlnaManager, IContentDirectory contentDirectory, IConnectionManager connectionManager) { _dlnaManager = dlnaManager; _contentDirectory = contentDirectory; _connectionManager = connectionManager; - _config = config; } public object Get(GetDescriptionXml request) diff --git a/MediaBrowser.Api/IHasDtoOptions.cs b/MediaBrowser.Api/IHasDtoOptions.cs index 2e677e816..7fe47c4a1 100644 --- a/MediaBrowser.Api/IHasDtoOptions.cs +++ b/MediaBrowser.Api/IHasDtoOptions.cs @@ -1,4 +1,4 @@ -using MediaBrowser.Model.Dto; +using MediaBrowser.Controller.Dto; using MediaBrowser.Model.Entities; using System; using System.Linq; diff --git a/MediaBrowser.Api/Playback/BaseStreamingService.cs b/MediaBrowser.Api/Playback/BaseStreamingService.cs index 0858a0347..8662e64b4 100644 --- a/MediaBrowser.Api/Playback/BaseStreamingService.cs +++ b/MediaBrowser.Api/Playback/BaseStreamingService.cs @@ -834,7 +834,7 @@ namespace MediaBrowser.Api.Playback { if (state.SubtitleStream.IsExternal && !state.SubtitleStream.IsTextSubtitleStream) { - arg += " -i " + state.SubtitleStream.Path; + arg += " -i \"" + state.SubtitleStream.Path + "\""; } } diff --git a/MediaBrowser.Controller/Dto/DtoOptions.cs b/MediaBrowser.Controller/Dto/DtoOptions.cs new file mode 100644 index 000000000..eeb4fc114 --- /dev/null +++ b/MediaBrowser.Controller/Dto/DtoOptions.cs @@ -0,0 +1,47 @@ +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Querying; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace MediaBrowser.Controller.Dto +{ + public class DtoOptions + { + private static readonly List DefaultExcludedFields = new List + { + ItemFields.SeasonUserData + }; + + public List Fields { get; set; } + public List ImageTypes { get; set; } + public int ImageTypeLimit { get; set; } + public bool EnableImages { get; set; } + + public DtoOptions() + { + Fields = new List(); + ImageTypeLimit = int.MaxValue; + EnableImages = true; + + Fields = Enum.GetNames(typeof (ItemFields)) + .Select(i => (ItemFields) Enum.Parse(typeof (ItemFields), i, true)) + .Except(DefaultExcludedFields) + .ToList(); + + ImageTypes = Enum.GetNames(typeof(ImageType)) + .Select(i => (ImageType)Enum.Parse(typeof(ImageType), i, true)) + .ToList(); + } + + public int GetImageLimit(ImageType type) + { + if (EnableImages && ImageTypes.Contains(type)) + { + return ImageTypeLimit; + } + + return 0; + } + } +} diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index 1d16db62c..0667730fd 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -115,6 +115,7 @@ + diff --git a/MediaBrowser.MediaEncoding/Encoder/EncodingUtils.cs b/MediaBrowser.MediaEncoding/Encoder/EncodingUtils.cs index c76f78009..b34a4cd38 100644 --- a/MediaBrowser.MediaEncoding/Encoder/EncodingUtils.cs +++ b/MediaBrowser.MediaEncoding/Encoder/EncodingUtils.cs @@ -41,7 +41,7 @@ namespace MediaBrowser.MediaEncoding.Encoder // If there's more than one we'll need to use the concat command if (inputFiles.Count > 1) { - var files = string.Join("|", inputFiles); + var files = string.Join("|", inputFiles.Select(NormalizePath).ToArray()); return string.Format("concat:\"{0}\"", files); } @@ -57,9 +57,23 @@ namespace MediaBrowser.MediaEncoding.Encoder /// System.String. private static string GetFileInputArgument(string path) { + // Quotes are valid path characters in linux and they need to be escaped here with a leading \ + path = NormalizePath(path); + return string.Format("file:\"{0}\"", path); } + /// + /// Normalizes the path. + /// + /// The path. + /// System.String. + private static string NormalizePath(string path) + { + // Quotes are valid path characters in linux and they need to be escaped here with a leading \ + return path.Replace("\"", "\\\""); + } + public static string GetProbeSizeArgument(bool isDvd) { return isDvd ? "-probesize 1G -analyzeduration 200M" : string.Empty; diff --git a/MediaBrowser.Model.Portable/MediaBrowser.Model.Portable.csproj b/MediaBrowser.Model.Portable/MediaBrowser.Model.Portable.csproj index e4cfab571..c49e3e303 100644 --- a/MediaBrowser.Model.Portable/MediaBrowser.Model.Portable.csproj +++ b/MediaBrowser.Model.Portable/MediaBrowser.Model.Portable.csproj @@ -428,9 +428,6 @@ Dto\ChapterInfoDto.cs - - Dto\DtoOptions.cs - Dto\GameSystemSummary.cs diff --git a/MediaBrowser.Model.net35/MediaBrowser.Model.net35.csproj b/MediaBrowser.Model.net35/MediaBrowser.Model.net35.csproj index 3fc5201a7..d8a29e8da 100644 --- a/MediaBrowser.Model.net35/MediaBrowser.Model.net35.csproj +++ b/MediaBrowser.Model.net35/MediaBrowser.Model.net35.csproj @@ -393,9 +393,6 @@ Dto\ChapterInfoDto.cs - - Dto\DtoOptions.cs - Dto\GameSystemSummary.cs diff --git a/MediaBrowser.Model/Dto/DtoOptions.cs b/MediaBrowser.Model/Dto/DtoOptions.cs deleted file mode 100644 index 60eb6b1fe..000000000 --- a/MediaBrowser.Model/Dto/DtoOptions.cs +++ /dev/null @@ -1,47 +0,0 @@ -using MediaBrowser.Model.Entities; -using MediaBrowser.Model.Querying; -using System; -using System.Collections.Generic; -using System.Linq; - -namespace MediaBrowser.Model.Dto -{ - public class DtoOptions - { - private static readonly List DefaultExcludedFields = new List - { - ItemFields.SeasonUserData - }; - - public List Fields { get; set; } - public List ImageTypes { get; set; } - public int ImageTypeLimit { get; set; } - public bool EnableImages { get; set; } - - public DtoOptions() - { - Fields = new List(); - ImageTypeLimit = int.MaxValue; - EnableImages = true; - - Fields = Enum.GetNames(typeof (ItemFields)) - .Select(i => (ItemFields) Enum.Parse(typeof (ItemFields), i, true)) - .Except(DefaultExcludedFields) - .ToList(); - - ImageTypes = Enum.GetNames(typeof(ImageType)) - .Select(i => (ImageType)Enum.Parse(typeof(ImageType), i, true)) - .ToList(); - } - - public int GetImageLimit(ImageType type) - { - if (EnableImages && ImageTypes.Contains(type)) - { - return ImageTypeLimit; - } - - return 0; - } - } -} diff --git a/MediaBrowser.Model/MediaBrowser.Model.csproj b/MediaBrowser.Model/MediaBrowser.Model.csproj index d5280f4b1..2a1b0b659 100644 --- a/MediaBrowser.Model/MediaBrowser.Model.csproj +++ b/MediaBrowser.Model/MediaBrowser.Model.csproj @@ -127,7 +127,6 @@ - diff --git a/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs b/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs index 967c78c50..5055d2750 100644 --- a/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs +++ b/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs @@ -367,7 +367,7 @@ namespace MediaBrowser.Server.Implementations.Drawing new UnplayedCountIndicator().DrawUnplayedCountIndicator(graphics, currentImageSize, options.UnplayedCount.Value); } - if (options.PercentPlayed >= 0) + if (options.PercentPlayed > 0) { var currentImageSize = new Size(imageWidth, imageHeight); diff --git a/MediaBrowser.Server.Implementations/FileOrganization/EpisodeFileOrganizer.cs b/MediaBrowser.Server.Implementations/FileOrganization/EpisodeFileOrganizer.cs index 432ea1f69..a4ac22eea 100644 --- a/MediaBrowser.Server.Implementations/FileOrganization/EpisodeFileOrganizer.cs +++ b/MediaBrowser.Server.Implementations/FileOrganization/EpisodeFileOrganizer.cs @@ -373,6 +373,7 @@ namespace MediaBrowser.Server.Implementations.FileOrganization if (episode == null) { + _logger.Warn("No provider metadata found for {0} season {1} episode {2}", series.Name, seasonNumber, episodeNumber); return null; } diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/Extensions.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/Extensions.cs index 63d57b6be..154313fb9 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/Extensions.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/Extensions.cs @@ -1,6 +1,6 @@ -using System; -using MediaBrowser.Model.Logging; -using WebSocketSharp.Net; +using MediaBrowser.Model.Logging; +using SocketHttpListener.Net; +using System; namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp { diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SharpWebSocket.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SharpWebSocket.cs index 401d49325..9dcb679f4 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SharpWebSocket.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SharpWebSocket.cs @@ -21,7 +21,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp /// Gets or sets the web socket. /// /// The web socket. - private WebSocketSharp.WebSocket WebSocket { get; set; } + private SocketHttpListener.WebSocket WebSocket { get; set; } private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); @@ -31,7 +31,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp /// The socket. /// The logger. /// socket - public SharpWebSocket(WebSocketSharp.WebSocket socket, ILogger logger) + public SharpWebSocket(SocketHttpListener.WebSocket socket, ILogger logger) { if (socket == null) { @@ -53,17 +53,17 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp WebSocket.ConnectAsServer(); } - void socket_OnError(object sender, WebSocketSharp.ErrorEventArgs e) + void socket_OnError(object sender, SocketHttpListener.ErrorEventArgs e) { EventHelper.FireEventIfNotNull(Closed, this, EventArgs.Empty, _logger); } - void socket_OnClose(object sender, WebSocketSharp.CloseEventArgs e) + void socket_OnClose(object sender, SocketHttpListener.CloseEventArgs e) { EventHelper.FireEventIfNotNull(Closed, this, EventArgs.Empty, _logger); } - void socket_OnMessage(object sender, WebSocketSharp.MessageEventArgs e) + void socket_OnMessage(object sender, SocketHttpListener.MessageEventArgs e) { if (OnReceive != null) { diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SocketSharpLogger.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SocketSharpLogger.cs index ffc99793e..427671b30 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SocketSharpLogger.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SocketSharpLogger.cs @@ -3,7 +3,7 @@ using System; namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp { - public class SocketSharpLogger : WebSocketSharp.Logging.ILogger + public class SocketSharpLogger : SocketHttpListener.Logging.ILogger { private readonly ILogger _logger; diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs index 77d968409..419d145bb 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs @@ -7,7 +7,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using WebSocketSharp.Net; +using SocketHttpListener.Net; namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp { diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs index 7a5f6fbdc..54c27cf0a 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs @@ -7,7 +7,7 @@ using MediaBrowser.Model.Logging; using ServiceStack; using ServiceStack.Host; using ServiceStack.Web; -using WebSocketSharp.Net; +using SocketHttpListener.Net; namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp { diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs index b725af610..171dacb22 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs @@ -5,7 +5,7 @@ using MediaBrowser.Model.Logging; using ServiceStack; using ServiceStack.Host; using ServiceStack.Web; -using HttpListenerResponse = WebSocketSharp.Net.HttpListenerResponse; +using HttpListenerResponse = SocketHttpListener.Net.HttpListenerResponse; namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp { diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj index 4e698b12f..ccc0737e9 100644 --- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj +++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj @@ -64,6 +64,9 @@ ..\ThirdParty\ServiceStack\ServiceStack.Api.Swagger.dll + + ..\ThirdParty\SocketHttpListener\SocketHttpListener.dll + @@ -91,10 +94,6 @@ ..\ThirdParty\ServiceStack.Text\ServiceStack.Text.dll - - False - ..\ThirdParty\WebsocketSharp\websocket-sharp.dll - diff --git a/MediaBrowser.Server.Implementations/Sync/SyncManager.cs b/MediaBrowser.Server.Implementations/Sync/SyncManager.cs index 68eaa38d3..3b2d70f84 100644 --- a/MediaBrowser.Server.Implementations/Sync/SyncManager.cs +++ b/MediaBrowser.Server.Implementations/Sync/SyncManager.cs @@ -37,7 +37,7 @@ namespace MediaBrowser.Server.Implementations.Sync private readonly Func _dtoService; private readonly IApplicationHost _appHost; private readonly ITVSeriesManager _tvSeriesManager; - private readonly Func MediaEncoder; + private readonly Func _mediaEncoder; private ISyncProvider[] _providers = { }; @@ -51,7 +51,7 @@ namespace MediaBrowser.Server.Implementations.Sync _dtoService = dtoService; _appHost = appHost; _tvSeriesManager = tvSeriesManager; - MediaEncoder = mediaEncoder; + _mediaEncoder = mediaEncoder; } public void AddParts(IEnumerable providers) @@ -61,7 +61,7 @@ namespace MediaBrowser.Server.Implementations.Sync public async Task CreateJob(SyncJobRequest request) { - var processor = new SyncJobProcessor(_libraryManager, _repo, this, _logger, _userManager, _tvSeriesManager, MediaEncoder()); + var processor = new SyncJobProcessor(_libraryManager, _repo, this, _logger, _userManager, _tvSeriesManager, _mediaEncoder()); var user = _userManager.GetUserById(request.UserId); @@ -165,7 +165,7 @@ namespace MediaBrowser.Server.Implementations.Sync if (item == null) { - var processor = new SyncJobProcessor(_libraryManager, _repo, this, _logger, _userManager, _tvSeriesManager, MediaEncoder()); + var processor = new SyncJobProcessor(_libraryManager, _repo, this, _logger, _userManager, _tvSeriesManager, _mediaEncoder()); var user = _userManager.GetUserById(job.UserId); @@ -395,7 +395,7 @@ namespace MediaBrowser.Server.Implementations.Sync await _repo.Update(jobItem).ConfigureAwait(false); - var processor = new SyncJobProcessor(_libraryManager, _repo, this, _logger, _userManager, _tvSeriesManager, MediaEncoder()); + var processor = new SyncJobProcessor(_libraryManager, _repo, this, _logger, _userManager, _tvSeriesManager, _mediaEncoder()); await processor.UpdateJobStatus(jobItem.JobId).ConfigureAwait(false); } @@ -532,6 +532,22 @@ namespace MediaBrowser.Server.Implementations.Sync } } + // Now check each item that's on the device + foreach (var itemId in request.LocalItemIds) + { + // See if it's already marked for removal + if (response.ItemIdsToRemove.Contains(itemId, StringComparer.OrdinalIgnoreCase)) + { + continue; + } + + // If there isn't a sync job for this item, mark it for removal + if (!jobItemResult.Items.Any(i => string.Equals(itemId, i.ItemId, StringComparison.OrdinalIgnoreCase))) + { + response.ItemIdsToRemove.Add(itemId); + } + } + response.ItemIdsToRemove = response.ItemIdsToRemove.Distinct(StringComparer.OrdinalIgnoreCase).ToList(); return response; diff --git a/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj b/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj index df8f4072a..1343b1e69 100644 --- a/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj +++ b/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj @@ -624,33 +624,9 @@ PreserveNewest - - PreserveNewest - - - PreserveNewest - PreserveNewest - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - PreserveNewest -- cgit v1.2.3