From 5f71a615c4fbe5500dee2a7e8f3702039fd16375 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 1 Aug 2016 01:08:52 -0400 Subject: [PATCH] reduce file system access by dlna manager --- MediaBrowser.Dlna/DlnaManager.cs | 110 +++++++++--------- MediaBrowser.Dlna/Main/DlnaEntryPoint.cs | 8 +- MediaBrowser.Dlna/Profiles/LgTvProfile.cs | 2 +- MediaBrowser.Dlna/Profiles/Xml/BubbleUPnp.xml | 6 +- MediaBrowser.Dlna/Profiles/Xml/Default.xml | 6 +- MediaBrowser.Dlna/Profiles/Xml/Denon AVR.xml | 6 +- .../Profiles/Xml/DirecTV HD-DVR.xml | 6 +- .../Profiles/Xml/Dish Hopper-Joey.xml | 10 +- MediaBrowser.Dlna/Profiles/Xml/Kodi.xml | 6 +- .../Profiles/Xml/LG Smart TV.xml | 9 +- .../Profiles/Xml/Linksys DMA2100.xml | 6 +- .../Profiles/Xml/MediaMonkey.xml | 6 +- .../Profiles/Xml/Panasonic Viera.xml | 7 +- .../Profiles/Xml/Popcorn Hour.xml | 11 +- .../Profiles/Xml/Samsung Smart TV.xml | 11 +- .../Profiles/Xml/Sony Blu-ray Player 2013.xml | 8 +- .../Profiles/Xml/Sony Blu-ray Player.xml | 9 +- .../Profiles/Xml/Sony Bravia (2010).xml | 12 +- .../Profiles/Xml/Sony Bravia (2011).xml | 12 +- .../Profiles/Xml/Sony Bravia (2012).xml | 9 +- .../Profiles/Xml/Sony Bravia (2013).xml | 8 +- .../Profiles/Xml/Sony Bravia (2014).xml | 8 +- .../Profiles/Xml/Sony PlayStation 3.xml | 10 +- .../Profiles/Xml/Sony PlayStation 4.xml | 10 +- MediaBrowser.Dlna/Profiles/Xml/Vlc.xml | 6 +- MediaBrowser.Dlna/Profiles/Xml/WDTV Live.xml | 8 +- MediaBrowser.Dlna/Profiles/Xml/Xbox 360.xml | 11 +- MediaBrowser.Dlna/Profiles/Xml/Xbox One.xml | 12 +- MediaBrowser.Dlna/Profiles/Xml/foobar2000.xml | 6 +- 29 files changed, 204 insertions(+), 135 deletions(-) diff --git a/MediaBrowser.Dlna/DlnaManager.cs b/MediaBrowser.Dlna/DlnaManager.cs index b4127a91f1..42e976ce8b 100644 --- a/MediaBrowser.Dlna/DlnaManager.cs +++ b/MediaBrowser.Dlna/DlnaManager.cs @@ -29,7 +29,7 @@ namespace MediaBrowser.Dlna private readonly IJsonSerializer _jsonSerializer; private readonly IServerApplicationHost _appHost; - private readonly Dictionary _profiles = new Dictionary(StringComparer.Ordinal); + private readonly Dictionary> _profiles = new Dictionary>(StringComparer.Ordinal); public DlnaManager(IXmlSerializer xmlSerializer, IFileSystem fileSystem, @@ -45,50 +45,40 @@ namespace MediaBrowser.Dlna _appHost = appHost; } - public IEnumerable GetProfiles() + public void InitProfiles() { - ExtractProfilesIfNeeded(); + try + { + ExtractSystemProfiles(); + LoadProfiles(); + } + catch (Exception ex) + { + _logger.ErrorException("Error extracting DLNA profiles.", ex); + } + } + private void LoadProfiles() + { var list = GetProfiles(UserProfilesPath, DeviceProfileType.User) .OrderBy(i => i.Name) .ToList(); list.AddRange(GetProfiles(SystemProfilesPath, DeviceProfileType.System) .OrderBy(i => i.Name)); - - return list; } - private bool _extracted; - private readonly object _syncLock = new object(); - private void ExtractProfilesIfNeeded() + public IEnumerable GetProfiles() { - if (!_extracted) + lock (_profiles) { - lock (_syncLock) - { - if (!_extracted) - { - try - { - ExtractSystemProfiles(); - } - catch (Exception ex) - { - _logger.ErrorException("Error extracting DLNA profiles.", ex); - } - - _extracted = true; - } - - } + var list = _profiles.Values.ToList(); + return list.Select(i => i.Item2).OrderBy(i => i.Name); } } public DeviceProfile GetDefaultProfile() { - ExtractProfilesIfNeeded(); - return new DefaultProfile(); } @@ -304,20 +294,20 @@ namespace MediaBrowser.Dlna { lock (_profiles) { - DeviceProfile profile; - if (_profiles.TryGetValue(path, out profile)) + Tuple profileTuple; + if (_profiles.TryGetValue(path, out profileTuple)) { - return profile; + return profileTuple.Item2; } try { - profile = (DeviceProfile)_xmlSerializer.DeserializeFromFile(typeof(DeviceProfile), path); + var profile = (DeviceProfile)_xmlSerializer.DeserializeFromFile(typeof(DeviceProfile), path); profile.Id = path.ToLower().GetMD5().ToString("N"); profile.ProfileType = type; - _profiles[path] = profile; + _profiles[path] = new Tuple(GetInternalProfileInfo(_fileSystem.GetFileInfo(path), type), profile); return profile; } @@ -344,12 +334,14 @@ namespace MediaBrowser.Dlna private IEnumerable GetProfileInfosInternal() { - ExtractProfilesIfNeeded(); - - return GetProfileInfos(UserProfilesPath, DeviceProfileType.User) - .Concat(GetProfileInfos(SystemProfilesPath, DeviceProfileType.System)) - .OrderBy(i => i.Info.Type == DeviceProfileType.User ? 0 : 1) - .ThenBy(i => i.Info.Name); + lock (_profiles) + { + var list = _profiles.Values.ToList(); + return list + .Select(i => i.Item1) + .OrderBy(i => i.Info.Type == DeviceProfileType.User ? 0 : 1) + .ThenBy(i => i.Info.Name); + } } public IEnumerable GetProfileInfos() @@ -363,17 +355,7 @@ namespace MediaBrowser.Dlna { return _fileSystem.GetFiles(path) .Where(i => string.Equals(i.Extension, ".xml", StringComparison.OrdinalIgnoreCase)) - .Select(i => new InternalProfileInfo - { - Path = i.FullName, - - Info = new DeviceProfileInfo - { - Id = i.FullName.ToLower().GetMD5().ToString("N"), - Name = _fileSystem.GetFileNameWithoutExtension(i), - Type = type - } - }) + .Select(i => GetInternalProfileInfo(i, type)) .ToList(); } catch (DirectoryNotFoundException) @@ -382,6 +364,21 @@ namespace MediaBrowser.Dlna } } + private InternalProfileInfo GetInternalProfileInfo(FileSystemMetadata file, DeviceProfileType type) + { + return new InternalProfileInfo + { + Path = file.FullName, + + Info = new DeviceProfileInfo + { + Id = file.FullName.ToLower().GetMD5().ToString("N"), + Name = _fileSystem.GetFileNameWithoutExtension(file), + Type = type + } + }; + } + private void ExtractSystemProfiles() { var assembly = GetType().Assembly; @@ -427,6 +424,11 @@ namespace MediaBrowser.Dlna } _fileSystem.DeleteFile(info.Path); + + lock (_profiles) + { + _profiles.Remove(info.Path); + } } public void CreateProfile(DeviceProfile profile) @@ -441,7 +443,7 @@ namespace MediaBrowser.Dlna var newFilename = _fileSystem.GetValidFilename(profile.Name) + ".xml"; var path = Path.Combine(UserProfilesPath, newFilename); - SaveProfile(profile, path); + SaveProfile(profile, path, DeviceProfileType.User); } public void UpdateProfile(DeviceProfile profile) @@ -468,14 +470,14 @@ namespace MediaBrowser.Dlna _fileSystem.DeleteFile(current.Path); } - SaveProfile(profile, path); + SaveProfile(profile, path, DeviceProfileType.User); } - private void SaveProfile(DeviceProfile profile, string path) + private void SaveProfile(DeviceProfile profile, string path, DeviceProfileType type) { lock (_profiles) { - _profiles[path] = profile; + _profiles[path] = new Tuple(GetInternalProfileInfo(_fileSystem.GetFileInfo(path), type), profile); } _xmlSerializer.SerializeToFile(profile, path); } diff --git a/MediaBrowser.Dlna/Main/DlnaEntryPoint.cs b/MediaBrowser.Dlna/Main/DlnaEntryPoint.cs index fbd027ccf3..9f2726b315 100644 --- a/MediaBrowser.Dlna/Main/DlnaEntryPoint.cs +++ b/MediaBrowser.Dlna/Main/DlnaEntryPoint.cs @@ -80,6 +80,8 @@ namespace MediaBrowser.Dlna.Main public void Run() { + ((DlnaManager)_dlnaManager).InitProfiles(); + ReloadComponents(); _config.ConfigurationUpdated += _config_ConfigurationUpdated; @@ -240,9 +242,9 @@ namespace MediaBrowser.Dlna.Main var services = new List { - "upnp:rootdevice", - "urn:schemas-upnp-org:device:MediaServer:1", - "urn:schemas-upnp-org:service:ContentDirectory:1", + "upnp:rootdevice", + "urn:schemas-upnp-org:device:MediaServer:1", + "urn:schemas-upnp-org:service:ContentDirectory:1", "urn:schemas-upnp-org:service:ConnectionManager:1", "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1", "uuid:" + udn diff --git a/MediaBrowser.Dlna/Profiles/LgTvProfile.cs b/MediaBrowser.Dlna/Profiles/LgTvProfile.cs index 202ea76fbf..ab9a6a51ff 100644 --- a/MediaBrowser.Dlna/Profiles/LgTvProfile.cs +++ b/MediaBrowser.Dlna/Profiles/LgTvProfile.cs @@ -38,7 +38,7 @@ namespace MediaBrowser.Dlna.Profiles new TranscodingProfile { Container = "ts", - AudioCodec = "ac3", + AudioCodec = "ac3,aac,mp3", VideoCodec = "h264", Type = DlnaProfileType.Video }, diff --git a/MediaBrowser.Dlna/Profiles/Xml/BubbleUPnp.xml b/MediaBrowser.Dlna/Profiles/Xml/BubbleUPnp.xml index 38b7414549..08fec73db2 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/BubbleUPnp.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/BubbleUPnp.xml @@ -40,9 +40,9 @@ - - - + + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Default.xml b/MediaBrowser.Dlna/Profiles/Xml/Default.xml index 9364f464b6..8fae686325 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Default.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Default.xml @@ -33,9 +33,9 @@ - - - + + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Denon AVR.xml b/MediaBrowser.Dlna/Profiles/Xml/Denon AVR.xml index 5b8ff5d684..b15378276b 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Denon AVR.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Denon AVR.xml @@ -37,9 +37,9 @@ - - - + + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/DirecTV HD-DVR.xml b/MediaBrowser.Dlna/Profiles/Xml/DirecTV HD-DVR.xml index 561abe0e5c..27192847d3 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/DirecTV HD-DVR.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/DirecTV HD-DVR.xml @@ -39,8 +39,8 @@ - - + + @@ -51,11 +51,13 @@ + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Dish Hopper-Joey.xml b/MediaBrowser.Dlna/Profiles/Xml/Dish Hopper-Joey.xml index b2d2676575..678c31f80c 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Dish Hopper-Joey.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Dish Hopper-Joey.xml @@ -43,9 +43,9 @@ - - - + + + @@ -57,6 +57,7 @@ + @@ -65,16 +66,19 @@ + + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Kodi.xml b/MediaBrowser.Dlna/Profiles/Xml/Kodi.xml index d0985e1350..79c0dd3033 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Kodi.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Kodi.xml @@ -40,9 +40,9 @@ - - - + + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/LG Smart TV.xml b/MediaBrowser.Dlna/Profiles/Xml/LG Smart TV.xml index 1147aa2993..b74ecd1cec 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/LG Smart TV.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/LG Smart TV.xml @@ -42,9 +42,9 @@ - - - + + + @@ -61,6 +61,7 @@ + @@ -69,11 +70,13 @@ + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Linksys DMA2100.xml b/MediaBrowser.Dlna/Profiles/Xml/Linksys DMA2100.xml index 1e6db99b12..04dc262d89 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Linksys DMA2100.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Linksys DMA2100.xml @@ -37,9 +37,9 @@ - - - + + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/MediaMonkey.xml b/MediaBrowser.Dlna/Profiles/Xml/MediaMonkey.xml index 679aa26bd6..7b3aa03e82 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/MediaMonkey.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/MediaMonkey.xml @@ -43,9 +43,9 @@ - - - + + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Panasonic Viera.xml b/MediaBrowser.Dlna/Profiles/Xml/Panasonic Viera.xml index 256443093f..cbc275eb17 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Panasonic Viera.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Panasonic Viera.xml @@ -50,9 +50,9 @@ - - - + + + @@ -69,6 +69,7 @@ + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Popcorn Hour.xml b/MediaBrowser.Dlna/Profiles/Xml/Popcorn Hour.xml index 3d50b1724c..bfdeb7cb59 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Popcorn Hour.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Popcorn Hour.xml @@ -38,9 +38,9 @@ - - - + + + @@ -51,6 +51,7 @@ + @@ -58,22 +59,26 @@ + + + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Samsung Smart TV.xml b/MediaBrowser.Dlna/Profiles/Xml/Samsung Smart TV.xml index 967538bdf0..c806c1238b 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Samsung Smart TV.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Samsung Smart TV.xml @@ -50,9 +50,9 @@ - - - + + + @@ -70,6 +70,7 @@ + @@ -78,6 +79,7 @@ + @@ -87,6 +89,7 @@ + @@ -95,11 +98,13 @@ + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Sony Blu-ray Player 2013.xml b/MediaBrowser.Dlna/Profiles/Xml/Sony Blu-ray Player 2013.xml index 49f4759b98..e304043625 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Sony Blu-ray Player 2013.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Sony Blu-ray Player 2013.xml @@ -48,9 +48,9 @@ - - - + + + @@ -67,11 +67,13 @@ + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Sony Blu-ray Player.xml b/MediaBrowser.Dlna/Profiles/Xml/Sony Blu-ray Player.xml index 41a996b663..c355057f89 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Sony Blu-ray Player.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Sony Blu-ray Player.xml @@ -47,9 +47,9 @@ - - - + + + @@ -68,16 +68,19 @@ + + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2010).xml b/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2010).xml index ed66118d32..e045d130bf 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2010).xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2010).xml @@ -45,9 +45,9 @@ - - - + + + @@ -66,6 +66,7 @@ + @@ -74,6 +75,7 @@ + @@ -81,22 +83,26 @@ + + + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2011).xml b/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2011).xml index 88ff6047fd..3ccceeaaab 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2011).xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2011).xml @@ -48,9 +48,9 @@ - - - + + + @@ -69,6 +69,7 @@ + @@ -77,6 +78,7 @@ + @@ -84,22 +86,26 @@ + + + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2012).xml b/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2012).xml index fb06ab0acf..a0b992283c 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2012).xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2012).xml @@ -50,9 +50,9 @@ - - - + + + @@ -69,16 +69,19 @@ + + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2013).xml b/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2013).xml index 67526f5f20..52f51c59ed 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2013).xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2013).xml @@ -55,9 +55,9 @@ - - - + + + @@ -74,11 +74,13 @@ + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2014).xml b/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2014).xml index 8502377565..2a6f179df3 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2014).xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Sony Bravia (2014).xml @@ -55,9 +55,9 @@ - - - + + + @@ -74,11 +74,13 @@ + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Sony PlayStation 3.xml b/MediaBrowser.Dlna/Profiles/Xml/Sony PlayStation 3.xml index 11dc332398..9512a61436 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Sony PlayStation 3.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Sony PlayStation 3.xml @@ -45,9 +45,9 @@ - - - + + + @@ -66,22 +66,26 @@ + + + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Sony PlayStation 4.xml b/MediaBrowser.Dlna/Profiles/Xml/Sony PlayStation 4.xml index 5a763006b1..914a6155a6 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Sony PlayStation 4.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Sony PlayStation 4.xml @@ -45,9 +45,9 @@ - - - + + + @@ -66,22 +66,26 @@ + + + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Vlc.xml b/MediaBrowser.Dlna/Profiles/Xml/Vlc.xml index a007a4aa32..9dfacdbc63 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Vlc.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Vlc.xml @@ -40,9 +40,9 @@ - - - + + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/WDTV Live.xml b/MediaBrowser.Dlna/Profiles/Xml/WDTV Live.xml index 6f245202d4..ed0ba41944 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/WDTV Live.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/WDTV Live.xml @@ -51,9 +51,9 @@ - - - + + + @@ -70,11 +70,13 @@ + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Xbox 360.xml b/MediaBrowser.Dlna/Profiles/Xml/Xbox 360.xml index bb937101d3..982a375f65 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Xbox 360.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Xbox 360.xml @@ -46,9 +46,9 @@ - - - + + + @@ -71,6 +71,7 @@ + @@ -79,6 +80,7 @@ + @@ -87,17 +89,20 @@ + + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/Xbox One.xml b/MediaBrowser.Dlna/Profiles/Xml/Xbox One.xml index 3816769442..38ca3b2f7e 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/Xbox One.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/Xbox One.xml @@ -46,9 +46,9 @@ - - - + + + @@ -67,6 +67,7 @@ + @@ -77,6 +78,7 @@ + @@ -87,23 +89,27 @@ + + + + diff --git a/MediaBrowser.Dlna/Profiles/Xml/foobar2000.xml b/MediaBrowser.Dlna/Profiles/Xml/foobar2000.xml index ebc5e83664..d729d6d547 100644 --- a/MediaBrowser.Dlna/Profiles/Xml/foobar2000.xml +++ b/MediaBrowser.Dlna/Profiles/Xml/foobar2000.xml @@ -43,9 +43,9 @@ - - - + + +