using Emby.Drawing;
using Emby.Drawing.GDI;
using Emby.Drawing.ImageMagick;
using MediaBrowser.Api;
using MediaBrowser.Common;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Events;
using MediaBrowser.Common.Extensions;
using Emby.Common.Implementations.ScheduledTasks;
using MediaBrowser.Common.Net;
using MediaBrowser.Common.Progress;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Channels;
using MediaBrowser.Controller.Chapters;
using MediaBrowser.Controller.Collections;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Connect;
using MediaBrowser.Controller.Devices;
using MediaBrowser.Controller.Dlna;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.FileOrganization;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Notifications;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Playlists;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Controller.Security;
using MediaBrowser.Controller.Session;
using MediaBrowser.Controller.Sorting;
using MediaBrowser.Controller.Subtitles;
using MediaBrowser.Controller.Sync;
using MediaBrowser.Controller.TV;
using MediaBrowser.LocalMetadata.Savers;
using MediaBrowser.MediaEncoding.BdInfo;
using MediaBrowser.MediaEncoding.Encoder;
using MediaBrowser.MediaEncoding.Subtitles;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.MediaInfo;
using MediaBrowser.Model.System;
using MediaBrowser.Model.Updates;
using MediaBrowser.Providers.Chapters;
using MediaBrowser.Providers.Manager;
using MediaBrowser.Providers.Subtitles;
using MediaBrowser.Server.Implementations;
using MediaBrowser.Server.Implementations.Activity;
using MediaBrowser.Server.Implementations.Channels;
using MediaBrowser.Server.Implementations.Collections;
using MediaBrowser.Server.Implementations.Configuration;
using MediaBrowser.Server.Implementations.Connect;
using MediaBrowser.Server.Implementations.Devices;
using MediaBrowser.Server.Implementations.Dto;
using MediaBrowser.Server.Implementations.EntryPoints;
using MediaBrowser.Server.Implementations.FileOrganization;
using MediaBrowser.Server.Implementations.HttpServer;
using MediaBrowser.Server.Implementations.HttpServer.Security;
using MediaBrowser.Server.Implementations.IO;
using MediaBrowser.Server.Implementations.Library;
using MediaBrowser.Server.Implementations.LiveTv;
using MediaBrowser.Server.Implementations.Localization;
using MediaBrowser.Server.Implementations.MediaEncoder;
using MediaBrowser.Server.Implementations.Notifications;
using MediaBrowser.Server.Implementations.Persistence;
using MediaBrowser.Server.Implementations.Playlists;
using MediaBrowser.Server.Implementations.Security;
using MediaBrowser.Server.Implementations.ServerManager;
using MediaBrowser.Server.Implementations.Session;
using MediaBrowser.Server.Implementations.Social;
using MediaBrowser.Server.Implementations.Sync;
using MediaBrowser.Server.Implementations.TV;
using MediaBrowser.Server.Startup.Common.FFMpeg;
using MediaBrowser.Server.Startup.Common.Migrations;
using MediaBrowser.WebDashboard.Api;
using MediaBrowser.XbmcMetadata.Providers;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Emby.Common.Implementations;
using Emby.Common.Implementations.Networking;
using Emby.Common.Implementations.Updates;
using Emby.Photos;
using MediaBrowser.Model.IO;
using MediaBrowser.Api.Playback;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Common.Security;
using MediaBrowser.Common.Updates;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.IO;
using Emby.Dlna;
using Emby.Dlna.ConnectionManager;
using Emby.Dlna.ContentDirectory;
using Emby.Dlna.Main;
using Emby.Dlna.MediaReceiverRegistrar;
using Emby.Dlna.Ssdp;
using MediaBrowser.Model.Activity;
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.News;
using MediaBrowser.Model.Reflection;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Services;
using MediaBrowser.Model.Social;
using MediaBrowser.Model.TextEncoding;
using MediaBrowser.Model.Xml;
using MediaBrowser.Server.Implementations.Archiving;
using MediaBrowser.Server.Implementations.Reflection;
using MediaBrowser.Server.Implementations.Serialization;
using MediaBrowser.Server.Implementations.TextEncoding;
using MediaBrowser.Server.Implementations.Updates;
using MediaBrowser.Server.Implementations.Xml;
using OpenSubtitlesHandler;
using ServiceStack;
using StringExtensions = MediaBrowser.Controller.Extensions.StringExtensions;
namespace MediaBrowser.Server.Startup.Common
{
///
/// Class CompositionRoot
///
public class ApplicationHost : BaseApplicationHost, IServerApplicationHost, IDependencyContainer
{
///
/// Gets the server configuration manager.
///
/// The server configuration manager.
public IServerConfigurationManager ServerConfigurationManager
{
get { return (IServerConfigurationManager)ConfigurationManager; }
}
///
/// Gets the configuration manager.
///
/// IConfigurationManager.
protected override IConfigurationManager GetConfigurationManager()
{
return new ServerConfigurationManager(ApplicationPaths, LogManager, XmlSerializer, FileSystemManager);
}
///
/// Gets or sets the server manager.
///
/// The server manager.
private IServerManager ServerManager { get; set; }
///
/// Gets or sets the user manager.
///
/// The user manager.
public IUserManager UserManager { get; set; }
///
/// Gets or sets the library manager.
///
/// The library manager.
internal ILibraryManager LibraryManager { get; set; }
///
/// Gets or sets the directory watchers.
///
/// The directory watchers.
private ILibraryMonitor LibraryMonitor { get; set; }
///
/// Gets or sets the provider manager.
///
/// The provider manager.
private IProviderManager ProviderManager { get; set; }
///
/// Gets or sets the HTTP server.
///
/// The HTTP server.
private IHttpServer HttpServer { get; set; }
private IDtoService DtoService { get; set; }
private IImageProcessor ImageProcessor { get; set; }
///
/// Gets or sets the media encoder.
///
/// The media encoder.
private IMediaEncoder MediaEncoder { get; set; }
private ISubtitleEncoder SubtitleEncoder { get; set; }
private IConnectManager ConnectManager { get; set; }
private ISessionManager SessionManager { get; set; }
private ILiveTvManager LiveTvManager { get; set; }
public ILocalizationManager LocalizationManager { get; set; }
private IEncodingManager EncodingManager { get; set; }
private IChannelManager ChannelManager { get; set; }
private ISyncManager SyncManager { get; set; }
///
/// Gets or sets the user data repository.
///
/// The user data repository.
private IUserDataManager UserDataManager { get; set; }
private IUserRepository UserRepository { get; set; }
internal IDisplayPreferencesRepository DisplayPreferencesRepository { get; set; }
internal IItemRepository ItemRepository { get; set; }
private INotificationsRepository NotificationsRepository { get; set; }
private IFileOrganizationRepository FileOrganizationRepository { get; set; }
private INotificationManager NotificationManager { get; set; }
private ISubtitleManager SubtitleManager { get; set; }
private IChapterManager ChapterManager { get; set; }
private IDeviceManager DeviceManager { get; set; }
internal IUserViewManager UserViewManager { get; set; }
private IAuthenticationRepository AuthenticationRepository { get; set; }
private ISyncRepository SyncRepository { get; set; }
private ITVSeriesManager TVSeriesManager { get; set; }
private ICollectionManager CollectionManager { get; set; }
private IMediaSourceManager MediaSourceManager { get; set; }
private IPlaylistManager PlaylistManager { get; set; }
///
/// Gets or sets the installation manager.
///
/// The installation manager.
protected IInstallationManager InstallationManager { get; private set; }
///
/// Gets the security manager.
///
/// The security manager.
protected ISecurityManager SecurityManager { get; private set; }
///
/// Gets or sets the zip client.
///
/// The zip client.
protected IZipClient ZipClient { get; private set; }
private readonly StartupOptions _startupOptions;
private readonly string _releaseAssetFilename;
internal INativeApp NativeApp { get; set; }
///
/// Initializes a new instance of the class.
///
/// The application paths.
/// The log manager.
/// The options.
/// The file system.
/// The release asset filename.
/// The native application.
public ApplicationHost(ServerApplicationPaths applicationPaths,
ILogManager logManager,
StartupOptions options,
IFileSystem fileSystem,
string releaseAssetFilename,
INativeApp nativeApp)
: base(applicationPaths, logManager, fileSystem)
{
_startupOptions = options;
_releaseAssetFilename = releaseAssetFilename;
NativeApp = nativeApp;
SetBaseExceptionMessage();
}
private Version _version;
///
/// Gets the current application version
///
/// The application version.
public override Version ApplicationVersion
{
get
{
return _version ?? (_version = NativeApp.GetType().Assembly.GetName().Version);
}
}
public override string OperatingSystemDisplayName
{
get { return NativeApp.Environment.OperatingSystemVersionString; }
}
public override bool IsRunningAsService
{
get { return NativeApp.IsRunningAsService; }
}
public bool SupportsRunningAsService
{
get { return NativeApp.SupportsRunningAsService; }
}
public bool SupportsLibraryMonitor
{
get { return NativeApp.SupportsLibraryMonitor; }
}
///
/// Gets the name.
///
/// The name.
public override string Name
{
get
{
return "Emby Server";
}
}
///
/// Gets a value indicating whether this instance can self restart.
///
/// true if this instance can self restart; otherwise, false.
public override bool CanSelfRestart
{
get { return NativeApp.CanSelfRestart; }
}
public bool SupportsAutoRunAtStartup
{
get { return NativeApp.SupportsAutoRunAtStartup; }
}
private void SetBaseExceptionMessage()
{
var builder = GetBaseExceptionMessage(ApplicationPaths);
// Skip if plugins haven't been loaded yet
//if (Plugins != null)
//{
// var pluginString = string.Join("|", Plugins.Select(i => i.Name + "-" + i.Version.ToString()).ToArray());
// builder.Insert(0, string.Format("Plugins: {0}{1}", pluginString, Environment.NewLine));
//}
builder.Insert(0, string.Format("Version: {0}{1}", ApplicationVersion, Environment.NewLine));
builder.Insert(0, "*** Error Report ***" + Environment.NewLine);
LogManager.ExceptionMessagePrefix = builder.ToString();
}
///
/// Runs the startup tasks.
///
public override async Task RunStartupTasks()
{
await PerformPreInitMigrations().ConfigureAwait(false);
if (ServerConfigurationManager.Configuration.MigrationVersion < CleanDatabaseScheduledTask.MigrationVersion &&
ServerConfigurationManager.Configuration.IsStartupWizardCompleted)
{
TaskManager.SuspendTriggers = true;
}
await base.RunStartupTasks().ConfigureAwait(false);
await MediaEncoder.Init().ConfigureAwait(false);
if (string.IsNullOrWhiteSpace(MediaEncoder.EncoderPath))
{
if (ServerConfigurationManager.Configuration.IsStartupWizardCompleted)
{
ServerConfigurationManager.Configuration.IsStartupWizardCompleted = false;
ServerConfigurationManager.SaveConfiguration();
}
}
Logger.Info("ServerId: {0}", SystemId);
Logger.Info("Core startup complete");
HttpServer.GlobalResponse = null;
PerformPostInitMigrations();
Logger.Info("Post-init migrations complete");
foreach (var entryPoint in GetExports().ToList())
{
var name = entryPoint.GetType().FullName;
Logger.Info("Starting entry point {0}", name);
var now = DateTime.UtcNow;
try
{
entryPoint.Run();
}
catch (Exception ex)
{
Logger.ErrorException("Error in {0}", ex, name);
}
Logger.Info("Entry point completed: {0}. Duration: {1} seconds", name, (DateTime.UtcNow - now).TotalSeconds.ToString(CultureInfo.InvariantCulture));
}
Logger.Info("All entry points have started");
LogManager.RemoveConsoleOutput();
}
protected override IMemoryStreamProvider CreateMemoryStreamProvider()
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
return new RecyclableMemoryStreamProvider();
}
return new MemoryStreamProvider();
}
protected override ISystemEvents CreateSystemEvents()
{
return new SystemEvents(LogManager.GetLogger("SystemEvents"));
}
protected override IJsonSerializer CreateJsonSerializer()
{
try
{
// https://github.com/ServiceStack/ServiceStack/blob/master/tests/ServiceStack.WebHost.IntegrationTests/Web.config#L4
Licensing.RegisterLicense("1001-e1JlZjoxMDAxLE5hbWU6VGVzdCBCdXNpbmVzcyxUeXBlOkJ1c2luZXNzLEhhc2g6UHVNTVRPclhvT2ZIbjQ5MG5LZE1mUTd5RUMzQnBucTFEbTE3TDczVEF4QUNMT1FhNXJMOWkzVjFGL2ZkVTE3Q2pDNENqTkQyUktRWmhvUVBhYTBiekJGUUZ3ZE5aZHFDYm9hL3lydGlwUHI5K1JsaTBYbzNsUC85cjVJNHE5QVhldDN6QkE4aTlvdldrdTgyTk1relY2eis2dFFqTThYN2lmc0JveHgycFdjPSxFeHBpcnk6MjAxMy0wMS0wMX0=");
}
catch
{
// Failing under mono
}
var result = new JsonSerializer(FileSystemManager, LogManager.GetLogger("JsonSerializer"));
ServiceStack.Text.JsConfig.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "ShortOverview", "Taglines", "Keywords", "ExtraType" };
ServiceStack.Text.JsConfig.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "ShortOverview", "Taglines", "Keywords", "ExtraType" };
ServiceStack.Text.JsConfig.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "ShortOverview", "Taglines", "Keywords", "ExtraType" };
ServiceStack.Text.JsConfig.ExcludePropertyNames = new[] { "Artists", "AlbumArtists", "ChannelMediaSources", "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "ShortOverview", "Taglines", "Keywords", "ExtraType" };
ServiceStack.Text.JsConfig.ExcludePropertyNames = new[] { "ProviderIds", "ImageInfos", "ProductionLocations", "ThemeSongIds", "ThemeVideoIds", "TotalBitrate", "ShortOverview", "Taglines", "Keywords", "ExtraType" };
ServiceStack.Text.JsConfig