CoreAppHost.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Emby.Server.Implementations;
  5. using Emby.Server.Implementations.Session;
  6. using Jellyfin.Api.WebSocketListeners;
  7. using Jellyfin.Drawing;
  8. using Jellyfin.Drawing.Skia;
  9. using Jellyfin.Server.Implementations;
  10. using Jellyfin.Server.Implementations.Activity;
  11. using Jellyfin.Server.Implementations.Devices;
  12. using Jellyfin.Server.Implementations.Events;
  13. using Jellyfin.Server.Implementations.Security;
  14. using Jellyfin.Server.Implementations.Trickplay;
  15. using Jellyfin.Server.Implementations.Users;
  16. using MediaBrowser.Controller;
  17. using MediaBrowser.Controller.BaseItemManager;
  18. using MediaBrowser.Controller.Devices;
  19. using MediaBrowser.Controller.Drawing;
  20. using MediaBrowser.Controller.Events;
  21. using MediaBrowser.Controller.Library;
  22. using MediaBrowser.Controller.Lyrics;
  23. using MediaBrowser.Controller.Net;
  24. using MediaBrowser.Controller.Security;
  25. using MediaBrowser.Controller.Trickplay;
  26. using MediaBrowser.Model.Activity;
  27. using Microsoft.Extensions.Configuration;
  28. using Microsoft.Extensions.DependencyInjection;
  29. using Microsoft.Extensions.Logging;
  30. namespace Jellyfin.Server
  31. {
  32. /// <summary>
  33. /// Implementation of the abstract <see cref="ApplicationHost" /> class.
  34. /// </summary>
  35. public class CoreAppHost : ApplicationHost
  36. {
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="CoreAppHost" /> class.
  39. /// </summary>
  40. /// <param name="applicationPaths">The <see cref="ServerApplicationPaths" /> to be used by the <see cref="CoreAppHost" />.</param>
  41. /// <param name="loggerFactory">The <see cref="ILoggerFactory" /> to be used by the <see cref="CoreAppHost" />.</param>
  42. /// <param name="options">The <see cref="StartupOptions" /> to be used by the <see cref="CoreAppHost" />.</param>
  43. /// <param name="startupConfig">The <see cref="IConfiguration" /> to be used by the <see cref="CoreAppHost" />.</param>
  44. public CoreAppHost(
  45. IServerApplicationPaths applicationPaths,
  46. ILoggerFactory loggerFactory,
  47. IStartupOptions options,
  48. IConfiguration startupConfig)
  49. : base(
  50. applicationPaths,
  51. loggerFactory,
  52. options,
  53. startupConfig)
  54. {
  55. }
  56. /// <inheritdoc/>
  57. protected override void RegisterServices(IServiceCollection serviceCollection)
  58. {
  59. // Register an image encoder
  60. bool useSkiaEncoder = SkiaEncoder.IsNativeLibAvailable();
  61. Type imageEncoderType = useSkiaEncoder
  62. ? typeof(SkiaEncoder)
  63. : typeof(NullImageEncoder);
  64. serviceCollection.AddSingleton(typeof(IImageEncoder), imageEncoderType);
  65. // Log a warning if the Skia encoder could not be used
  66. if (!useSkiaEncoder)
  67. {
  68. Logger.LogWarning("Skia not available. Will fallback to {ImageEncoder}.", nameof(NullImageEncoder));
  69. }
  70. serviceCollection.AddEventServices();
  71. serviceCollection.AddSingleton<IBaseItemManager, BaseItemManager>();
  72. serviceCollection.AddSingleton<IEventManager, EventManager>();
  73. serviceCollection.AddSingleton<IActivityManager, ActivityManager>();
  74. serviceCollection.AddSingleton<IUserManager, UserManager>();
  75. serviceCollection.AddScoped<IDisplayPreferencesManager, DisplayPreferencesManager>();
  76. serviceCollection.AddSingleton<IDeviceManager, DeviceManager>();
  77. serviceCollection.AddSingleton<ITrickplayManager, TrickplayManager>();
  78. // TODO search the assemblies instead of adding them manually?
  79. serviceCollection.AddSingleton<IWebSocketListener, SessionWebSocketListener>();
  80. serviceCollection.AddSingleton<IWebSocketListener, ActivityLogWebSocketListener>();
  81. serviceCollection.AddSingleton<IWebSocketListener, ScheduledTasksWebSocketListener>();
  82. serviceCollection.AddSingleton<IWebSocketListener, SessionInfoWebSocketListener>();
  83. serviceCollection.AddSingleton<IAuthorizationContext, AuthorizationContext>();
  84. serviceCollection.AddScoped<IAuthenticationManager, AuthenticationManager>();
  85. foreach (var type in GetExportTypes<ILyricProvider>())
  86. {
  87. serviceCollection.AddSingleton(typeof(ILyricProvider), type);
  88. }
  89. base.RegisterServices(serviceCollection);
  90. }
  91. /// <inheritdoc />
  92. protected override void RestartInternal() => Program.Restart();
  93. /// <inheritdoc />
  94. protected override IEnumerable<Assembly> GetAssembliesWithPartsInternal()
  95. {
  96. // Jellyfin.Server
  97. yield return typeof(CoreAppHost).Assembly;
  98. // Jellyfin.Server.Implementations
  99. yield return typeof(JellyfinDbContext).Assembly;
  100. }
  101. /// <inheritdoc />
  102. protected override void ShutdownInternal() => Program.Shutdown();
  103. }
  104. }