CoreAppHost.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using Emby.Drawing;
  6. using Emby.Server.Implementations;
  7. using Emby.Server.Implementations.Session;
  8. using Jellyfin.Api.WebSocketListeners;
  9. using Jellyfin.Drawing.Skia;
  10. using Jellyfin.Server.Implementations;
  11. using Jellyfin.Server.Implementations.Activity;
  12. using Jellyfin.Server.Implementations.Devices;
  13. using Jellyfin.Server.Implementations.Events;
  14. using Jellyfin.Server.Implementations.Users;
  15. using MediaBrowser.Controller;
  16. using MediaBrowser.Controller.BaseItemManager;
  17. using MediaBrowser.Controller.Devices;
  18. using MediaBrowser.Controller.Drawing;
  19. using MediaBrowser.Controller.Events;
  20. using MediaBrowser.Controller.Library;
  21. using MediaBrowser.Controller.Net;
  22. using MediaBrowser.Model.Activity;
  23. using MediaBrowser.Model.IO;
  24. using Microsoft.EntityFrameworkCore;
  25. using Microsoft.Extensions.Configuration;
  26. using Microsoft.Extensions.DependencyInjection;
  27. using Microsoft.Extensions.Logging;
  28. namespace Jellyfin.Server
  29. {
  30. /// <summary>
  31. /// Implementation of the abstract <see cref="ApplicationHost" /> class.
  32. /// </summary>
  33. public class CoreAppHost : ApplicationHost
  34. {
  35. /// <summary>
  36. /// Initializes a new instance of the <see cref="CoreAppHost" /> class.
  37. /// </summary>
  38. /// <param name="applicationPaths">The <see cref="ServerApplicationPaths" /> to be used by the <see cref="CoreAppHost" />.</param>
  39. /// <param name="loggerFactory">The <see cref="ILoggerFactory" /> to be used by the <see cref="CoreAppHost" />.</param>
  40. /// <param name="options">The <see cref="StartupOptions" /> to be used by the <see cref="CoreAppHost" />.</param>
  41. /// <param name="startupConfig">The <see cref="IConfiguration" /> to be used by the <see cref="CoreAppHost" />.</param>
  42. /// <param name="fileSystem">The <see cref="IFileSystem" /> to be used by the <see cref="CoreAppHost" />.</param>
  43. /// <param name="collection">The <see cref="IServiceCollection"/> 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. IFileSystem fileSystem,
  50. IServiceCollection collection)
  51. : base(
  52. applicationPaths,
  53. loggerFactory,
  54. options,
  55. startupConfig,
  56. fileSystem,
  57. collection)
  58. {
  59. }
  60. /// <inheritdoc/>
  61. protected override void RegisterServices()
  62. {
  63. // Register an image encoder
  64. bool useSkiaEncoder = SkiaEncoder.IsNativeLibAvailable();
  65. Type imageEncoderType = useSkiaEncoder
  66. ? typeof(SkiaEncoder)
  67. : typeof(NullImageEncoder);
  68. ServiceCollection.AddSingleton(typeof(IImageEncoder), imageEncoderType);
  69. // Log a warning if the Skia encoder could not be used
  70. if (!useSkiaEncoder)
  71. {
  72. Logger.LogWarning($"Skia not available. Will fallback to {nameof(NullImageEncoder)}.");
  73. }
  74. ServiceCollection.AddDbContextPool<JellyfinDb>(
  75. options => options.UseSqlite($"Filename={Path.Combine(ApplicationPaths.DataPath, "jellyfin.db")}"));
  76. ServiceCollection.AddEventServices();
  77. ServiceCollection.AddSingleton<IBaseItemManager, BaseItemManager>();
  78. ServiceCollection.AddSingleton<IEventManager, EventManager>();
  79. ServiceCollection.AddSingleton<JellyfinDbProvider>();
  80. ServiceCollection.AddSingleton<IActivityManager, ActivityManager>();
  81. ServiceCollection.AddSingleton<IUserManager, UserManager>();
  82. ServiceCollection.AddSingleton<IDisplayPreferencesManager, DisplayPreferencesManager>();
  83. ServiceCollection.AddSingleton<IDeviceManager, DeviceManager>();
  84. // TODO search the assemblies instead of adding them manually?
  85. ServiceCollection.AddSingleton<IWebSocketListener, SessionWebSocketListener>();
  86. ServiceCollection.AddSingleton<IWebSocketListener, ActivityLogWebSocketListener>();
  87. ServiceCollection.AddSingleton<IWebSocketListener, ScheduledTasksWebSocketListener>();
  88. ServiceCollection.AddSingleton<IWebSocketListener, SessionInfoWebSocketListener>();
  89. base.RegisterServices();
  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(JellyfinDb).Assembly;
  100. }
  101. /// <inheritdoc />
  102. protected override void ShutdownInternal() => Program.Shutdown();
  103. }
  104. }