예제 #1
0
        /// <summary>
        /// Installs the iso mounters.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task.</returns>
        private async Task InstallIsoMounters(CancellationToken cancellationToken)
        {
            var list = new List <IIsoMounter>();

            foreach (var isoMounter in GetExports <IIsoMounter>())
            {
                try
                {
                    if (isoMounter.RequiresInstallation && !isoMounter.IsInstalled)
                    {
                        Logger.Info("Installing {0}", isoMounter.Name);

                        await isoMounter.Install(cancellationToken).ConfigureAwait(false);
                    }

                    list.Add(isoMounter);
                }
                catch (Exception ex)
                {
                    Logger.ErrorException("{0} failed to load.", ex, isoMounter.Name);
                }
            }

            IsoManager.AddParts(list);
        }
예제 #2
0
        private async Task AcquireResources(StreamState state, CancellationTokenSource cancellationTokenSource)
        {
            if (state.VideoType == VideoType.Iso && state.IsoType.HasValue && IsoManager.CanMount(state.MediaPath))
            {
                state.IsoMount = await IsoManager.Mount(state.MediaPath, cancellationTokenSource.Token).ConfigureAwait(false);
            }

            if (state.MediaSource.RequiresOpening && string.IsNullOrWhiteSpace(state.Request.LiveStreamId))
            {
                var liveStreamResponse = await MediaSourceManager.OpenLiveStream(new LiveStreamRequest
                {
                    OpenToken = state.MediaSource.OpenToken
                }, cancellationTokenSource.Token).ConfigureAwait(false);

                EncodingHelper.AttachMediaSourceInfo(state, liveStreamResponse.MediaSource, state.RequestedUrl);

                if (state.VideoRequest != null)
                {
                    EncodingHelper.TryStreamCopy(state);
                }
            }

            if (state.MediaSource.BufferMs.HasValue)
            {
                await Task.Delay(state.MediaSource.BufferMs.Value, cancellationTokenSource.Token).ConfigureAwait(false);
            }
        }
예제 #3
0
        /// <summary>
        /// Finds the parts.
        /// </summary>
        protected override void FindParts()
        {
            if (IsFirstRun)
            {
                RegisterServerWithAdministratorAccess();
            }

            base.FindParts();

            HttpServer.Init(GetExports <IRestfulService>(false));

            ServerManager.AddWebSocketListeners(GetExports <IWebSocketListener>(false));

            StartServer(true);

            LibraryManager.AddParts(GetExports <IResolverIgnoreRule>(),
                                    GetExports <IVirtualFolderCreator>(),
                                    GetExports <IItemResolver>(),
                                    GetExports <IIntroProvider>(),
                                    GetExports <IBaseItemComparer>(),
                                    GetExports <ILibraryPrescanTask>(),
                                    GetExports <ILibraryPostScanTask>(),
                                    GetExports <IMetadataSaver>());

            ProviderManager.AddParts(GetExports <BaseMetadataProvider>().ToArray());

            IsoManager.AddParts(GetExports <IIsoMounter>().ToArray());
        }
        /// <summary>
        /// Registers resources that classes will depend on
        /// </summary>
        /// <returns>Task.</returns>
        protected virtual Task RegisterResources(IProgress <double> progress)
        {
            return(Task.Run(() =>
            {
                RegisterSingleInstance(ConfigurationManager);
                RegisterSingleInstance <IApplicationHost>(this);

                RegisterSingleInstance <IApplicationPaths>(ApplicationPaths);

                TaskManager = new TaskManager(ApplicationPaths, JsonSerializer, Logger);

                RegisterSingleInstance(JsonSerializer);
                RegisterSingleInstance(XmlSerializer);

                RegisterSingleInstance(LogManager);
                RegisterSingleInstance(Logger);

                RegisterSingleInstance(TaskManager);

                FileSystemManager = CreateFileSystemManager();
                RegisterSingleInstance(FileSystemManager);

                HttpClient = new HttpClientManager.HttpClientManager(ApplicationPaths, Logger, FileSystemManager, ConfigurationManager);
                RegisterSingleInstance(HttpClient);

                NetworkManager = CreateNetworkManager();
                RegisterSingleInstance(NetworkManager);

                SecurityManager = new PluginSecurityManager(this, HttpClient, JsonSerializer, ApplicationPaths, NetworkManager, LogManager);
                RegisterSingleInstance(SecurityManager);

                InstallationManager = new InstallationManager(Logger, this, ApplicationPaths, HttpClient, JsonSerializer, SecurityManager, NetworkManager, ConfigurationManager);
                RegisterSingleInstance(InstallationManager);

                ZipClient = new ZipClient();
                RegisterSingleInstance(ZipClient);

                IsoManager = new IsoManager();
                RegisterSingleInstance(IsoManager);

                RegisterModules();
            }));
        }
예제 #5
0
        /// <summary>
        /// Registers resources that classes will depend on
        /// </summary>
        /// <returns>Task.</returns>
        protected virtual Task RegisterResources(IProgress <double> progress)
        {
            RegisterSingleInstance(ConfigurationManager);
            RegisterSingleInstance <IApplicationHost>(this);

            RegisterSingleInstance <IApplicationPaths>(ApplicationPaths);

            TaskManager = new TaskManager(ApplicationPaths, JsonSerializer, LogManager.GetLogger("TaskManager"), FileSystemManager, SystemEvents);

            RegisterSingleInstance(JsonSerializer);
            RegisterSingleInstance(XmlSerializer);
            RegisterSingleInstance(MemoryStreamFactory);
            RegisterSingleInstance(SystemEvents);

            RegisterSingleInstance(LogManager);
            RegisterSingleInstance(Logger);

            RegisterSingleInstance(TaskManager);
            RegisterSingleInstance(EnvironmentInfo);

            RegisterSingleInstance(FileSystemManager);

            HttpClient = new HttpClientManager.HttpClientManager(ApplicationPaths, LogManager.GetLogger("HttpClient"), FileSystemManager, MemoryStreamFactory, GetDefaultUserAgent);
            RegisterSingleInstance(HttpClient);

            RegisterSingleInstance(NetworkManager);

            IsoManager = new IsoManager();
            RegisterSingleInstance(IsoManager);

            ProcessFactory = new ProcessFactory();
            RegisterSingleInstance(ProcessFactory);

            TimerFactory = new TimerFactory();
            RegisterSingleInstance(TimerFactory);

            SocketFactory = new SocketFactory(LogManager.GetLogger("SocketFactory"));
            RegisterSingleInstance(SocketFactory);

            RegisterSingleInstance(CryptographyProvider);

            return(Task.FromResult(true));
        }
        /// <summary>
        /// Registers resources that classes will depend on
        /// </summary>
        /// <returns>Task.</returns>
        protected virtual Task RegisterResources(IProgress <double> progress)
        {
            RegisterSingleInstance(ConfigurationManager);
            RegisterSingleInstance <IApplicationHost>(this);

            RegisterSingleInstance <IApplicationPaths>(ApplicationPaths);

            TaskManager = new TaskManager(ApplicationPaths, JsonSerializer, LogManager.GetLogger("TaskManager"), FileSystemManager);

            RegisterSingleInstance(JsonSerializer);
            RegisterSingleInstance(XmlSerializer);
            RegisterSingleInstance(MemoryStreamProvider);

            RegisterSingleInstance(LogManager);
            RegisterSingleInstance(Logger);

            RegisterSingleInstance(TaskManager);

            RegisterSingleInstance(FileSystemManager);

            HttpClient = new HttpClientManager.HttpClientManager(ApplicationPaths, LogManager.GetLogger("HttpClient"), FileSystemManager, MemoryStreamProvider);
            RegisterSingleInstance(HttpClient);

            NetworkManager = CreateNetworkManager(LogManager.GetLogger("NetworkManager"));
            RegisterSingleInstance(NetworkManager);

            SecurityManager = new PluginSecurityManager(this, HttpClient, JsonSerializer, ApplicationPaths, LogManager);
            RegisterSingleInstance(SecurityManager);

            InstallationManager = new InstallationManager(LogManager.GetLogger("InstallationManager"), this, ApplicationPaths, HttpClient, JsonSerializer, SecurityManager, ConfigurationManager, FileSystemManager);
            RegisterSingleInstance(InstallationManager);

            ZipClient = new ZipClient(FileSystemManager);
            RegisterSingleInstance(ZipClient);

            IsoManager = new IsoManager();
            RegisterSingleInstance(IsoManager);

            return(Task.FromResult(true));
        }
예제 #7
0
        /// <summary>
        /// Starts the FFMPEG.
        /// </summary>
        /// <param name="state">The state.</param>
        /// <param name="outputPath">The output path.</param>
        /// <returns>Task.</returns>
        protected async Task StartFfMpeg(StreamState state, string outputPath)
        {
            var parentPath = Path.GetDirectoryName(outputPath);

            Directory.CreateDirectory(parentPath);

            var video = state.Item as Video;

            if (video != null && video.VideoType == VideoType.Iso && video.IsoType.HasValue && IsoManager.CanMount(video.Path))
            {
                state.IsoMount = await IsoManager.Mount(video.Path, CancellationToken.None).ConfigureAwait(false);
            }

            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,

                    FileName         = MediaEncoder.EncoderPath,
                    WorkingDirectory = Path.GetDirectoryName(MediaEncoder.EncoderPath),
                    Arguments        = GetCommandLineArguments(outputPath, state, true),

                    WindowStyle = ProcessWindowStyle.Hidden,
                    ErrorDialog = false
                },

                EnableRaisingEvents = true
            };

            ApiEntryPoint.Instance.OnTranscodeBeginning(outputPath, TranscodingJobType, process, video != null, state.Request.StartTimeTicks, state.Item.Path, state.Request.DeviceId);

            Logger.Info(process.StartInfo.FileName + " " + process.StartInfo.Arguments);

            var logFilePath = Path.Combine(ApplicationPaths.LogDirectoryPath, "ffmpeg-" + Guid.NewGuid() + ".txt");

            // FFMpeg writes debug/error info to stderr. This is useful when debugging so let's put it in the log directory.
            state.LogFileStream = FileSystem.GetFileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, true);

            process.Exited += (sender, args) => OnFfMpegProcessExited(process, state);

            try
            {
                process.Start();
            }
            catch (Exception ex)
            {
                Logger.ErrorException("Error starting ffmpeg", ex);

                ApiEntryPoint.Instance.OnTranscodeFailedToStart(outputPath, TranscodingJobType);

                state.LogFileStream.Dispose();

                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
            process.StandardError.BaseStream.CopyToAsync(state.LogFileStream);

            // Wait for the file to exist before proceeeding
            while (!File.Exists(outputPath))
            {
                await Task.Delay(100).ConfigureAwait(false);
            }

            // Allow a small amount of time to buffer a little
            if (state.Item is Video)
            {
                await Task.Delay(500).ConfigureAwait(false);
            }

            // This is arbitrary, but add a little buffer time when internet streaming
            if (state.Item.LocationType == LocationType.Remote)
            {
                await Task.Delay(2000).ConfigureAwait(false);
            }
        }
예제 #8
0
        /// <summary>
        /// Registers resources that classes will depend on
        /// </summary>
        /// <returns>Task.</returns>
        protected override async Task RegisterResources()
        {
            ServerKernel = new Kernel();

            await base.RegisterResources().ConfigureAwait(false);

            RegisterSingleInstance <IHttpResultFactory>(new HttpResultFactory(LogManager));

            RegisterSingleInstance <IServerApplicationHost>(this);
            RegisterSingleInstance <IServerApplicationPaths>(ApplicationPaths);

            RegisterSingleInstance(ServerKernel);
            RegisterSingleInstance(ServerConfigurationManager);

            RegisterSingleInstance <IWebSocketServer>(() => new AlchemyServer(Logger));

            IsoManager = new IsoManager();
            RegisterSingleInstance(IsoManager);

            RegisterSingleInstance <IBlurayExaminer>(() => new BdInfoExaminer());

            ZipClient = new DotNetZipClient();
            RegisterSingleInstance(ZipClient);

            UserDataRepository = new SqliteUserDataRepository(ApplicationPaths, JsonSerializer, LogManager);
            RegisterSingleInstance(UserDataRepository);

            UserRepository = await GetUserRepository().ConfigureAwait(false);

            RegisterSingleInstance(UserRepository);

            DisplayPreferencesRepository = new SqliteDisplayPreferencesRepository(ApplicationPaths, JsonSerializer, LogManager);
            RegisterSingleInstance(DisplayPreferencesRepository);

            ItemRepository = new SqliteItemRepository(ApplicationPaths, JsonSerializer, LogManager);
            RegisterSingleInstance(ItemRepository);

            UserManager = new UserManager(Logger, ServerConfigurationManager, UserRepository);
            RegisterSingleInstance(UserManager);

            LibraryManager = new LibraryManager(Logger, TaskManager, UserManager, ServerConfigurationManager, UserDataRepository, () => DirectoryWatchers);
            RegisterSingleInstance(LibraryManager);

            DirectoryWatchers = new DirectoryWatchers(LogManager, TaskManager, LibraryManager, ServerConfigurationManager);
            RegisterSingleInstance(DirectoryWatchers);

            ProviderManager = new ProviderManager(HttpClient, ServerConfigurationManager, DirectoryWatchers, LogManager, LibraryManager);
            RegisterSingleInstance(ProviderManager);

            RegisterSingleInstance <ILibrarySearchEngine>(() => new LuceneSearchEngine(ApplicationPaths, LogManager, LibraryManager));

            MediaEncoder = new MediaEncoder(LogManager.GetLogger("MediaEncoder"), ZipClient, ApplicationPaths, JsonSerializer, HttpClient);
            RegisterSingleInstance(MediaEncoder);

            var clientConnectionManager = new SessionManager(UserDataRepository, ServerConfigurationManager, Logger, UserRepository);

            RegisterSingleInstance <ISessionManager>(clientConnectionManager);

            HttpServer = await _httpServerCreationTask.ConfigureAwait(false);

            RegisterSingleInstance(HttpServer, false);

            ServerManager = new ServerManager(this, JsonSerializer, Logger, ServerConfigurationManager);
            RegisterSingleInstance(ServerManager);

            LocalizationManager = new LocalizationManager(ServerConfigurationManager);
            RegisterSingleInstance(LocalizationManager);

            DtoService = new DtoService(Logger, LibraryManager, UserManager, UserDataRepository, ItemRepository);
            RegisterSingleInstance(DtoService);

            var displayPreferencesTask = Task.Run(async() => await ConfigureDisplayPreferencesRepositories().ConfigureAwait(false));
            var itemsTask    = Task.Run(async() => await ConfigureItemRepositories().ConfigureAwait(false));
            var userdataTask = Task.Run(async() => await ConfigureUserDataRepositories().ConfigureAwait(false));

            await ConfigureNotificationsRepository().ConfigureAwait(false);

            await Task.WhenAll(itemsTask, displayPreferencesTask, userdataTask).ConfigureAwait(false);

            SetKernelProperties();
        }