From ab0e851db987815ad3ab710fb76908c79ee93d13 Mon Sep 17 00:00:00 2001 From: Bond-009 Date: Sat, 2 Feb 2019 23:55:47 +0100 Subject: [PATCH 1/3] Cleanup streaming services --- MediaBrowser.Api/ApiEntryPoint.cs | 81 ++++++++++++----- MediaBrowser.Api/BaseApiService.cs | 22 +++-- .../Playback/BaseStreamingService.cs | 91 +++++++++++-------- .../Playback/Hls/BaseHlsService.cs | 39 +++++--- .../Playback/Hls/DynamicHlsService.cs | 27 +++++- .../Playback/Hls/VideoHlsService.cs | 31 ++++++- .../Playback/Progressive/AudioService.cs | 28 +++++- .../BaseProgressiveStreamingService.cs | 29 +++++- .../Playback/Progressive/VideoService.cs | 28 +++++- .../Playback/TranscodingThrottler.cs | 19 ++-- .../Playback/UniversalAudioService.cs | 3 - 11 files changed, 297 insertions(+), 101 deletions(-) diff --git a/MediaBrowser.Api/ApiEntryPoint.cs b/MediaBrowser.Api/ApiEntryPoint.cs index a212d3f3a7..9e5e7f16b4 100644 --- a/MediaBrowser.Api/ApiEntryPoint.cs +++ b/MediaBrowser.Api/ApiEntryPoint.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Threading; @@ -58,6 +59,8 @@ namespace MediaBrowser.Api private readonly Dictionary _transcodingLocks = new Dictionary(); + private bool _disposed = false; + /// /// Initializes a new instance of the class. /// @@ -66,7 +69,15 @@ namespace MediaBrowser.Api /// The configuration. /// The file system. /// The media source manager. - public ApiEntryPoint(ILogger logger, ISessionManager sessionManager, IServerConfigurationManager config, IFileSystem fileSystem, IMediaSourceManager mediaSourceManager, ITimerFactory timerFactory, IProcessFactory processFactory, IHttpResultFactory resultFactory) + public ApiEntryPoint( + ILogger logger, + ISessionManager sessionManager, + IServerConfigurationManager config, + IFileSystem fileSystem, + IMediaSourceManager mediaSourceManager, + ITimerFactory timerFactory, + IProcessFactory processFactory, + IHttpResultFactory resultFactory) { Logger = logger; _sessionManager = sessionManager; @@ -77,9 +88,10 @@ namespace MediaBrowser.Api ProcessFactory = processFactory; ResultFactory = resultFactory; - Instance = this; _sessionManager.PlaybackProgress += _sessionManager_PlaybackProgress; _sessionManager.PlaybackStart += _sessionManager_PlaybackStart; + + Instance = this; } public static string[] Split(string value, char separator, bool removeEmpty) @@ -162,8 +174,7 @@ namespace MediaBrowser.Api { var path = _config.ApplicationPaths.TranscodingTempPath; - foreach (var file in _fileSystem.GetFilePaths(path, true) - .ToList()) + foreach (var file in _fileSystem.GetFilePaths(path, true)) { _fileSystem.DeleteFile(file); } @@ -184,17 +195,40 @@ namespace MediaBrowser.Api /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected virtual void Dispose(bool dispose) { - var list = _activeTranscodingJobs.ToList(); - var jobCount = list.Count; + if (_disposed) + { + return; + } + + if (dispose) + { + // TODO: dispose + } - Parallel.ForEach(list, j => KillTranscodingJob(j, false, path => true)); + var jobs = _activeTranscodingJobs.ToList(); + var jobCount = jobs.Count; - // Try to allow for some time to kill the ffmpeg processes and delete the partial stream files + IEnumerable GetKillJobs() + { + foreach (var job in jobs) + { + yield return KillTranscodingJob(job, false, path => true); + } + } + + // Wait for all processes to be killed if (jobCount > 0) { - var task = Task.Delay(1000); - Task.WaitAll(task); + Task.WaitAll(GetKillJobs().ToArray()); } + + _activeTranscodingJobs.Clear(); + _transcodingLocks.Clear(); + + _sessionManager.PlaybackProgress -= _sessionManager_PlaybackProgress; + _sessionManager.PlaybackStart -= _sessionManager_PlaybackStart; + + _disposed = true; } @@ -211,12 +245,13 @@ namespace MediaBrowser.Api /// The state. /// The cancellation token source. /// TranscodingJob. - public TranscodingJob OnTranscodeBeginning(string path, + public TranscodingJob OnTranscodeBeginning( + string path, string playSessionId, string liveStreamId, string transcodingJobId, TranscodingJobType type, - IProcess process, + Process process, string deviceId, StreamState state, CancellationTokenSource cancellationTokenSource) @@ -445,7 +480,7 @@ namespace MediaBrowser.Api /// Called when [transcode kill timer stopped]. /// /// The state. - private void OnTranscodeKillTimerStopped(object state) + private async void OnTranscodeKillTimerStopped(object state) { var job = (TranscodingJob)state; @@ -462,7 +497,7 @@ namespace MediaBrowser.Api Logger.LogInformation("Transcoding kill timer stopped for JobId {0} PlaySessionId {1}. Killing transcoding", job.Id, job.PlaySessionId); - KillTranscodingJob(job, true, path => true).GetAwaiter().GetResult(); + await KillTranscodingJob(job, true, path => true); } /// @@ -550,7 +585,7 @@ namespace MediaBrowser.Api { if (job.TranscodingThrottler != null) { - job.TranscodingThrottler.Stop(); + job.TranscodingThrottler.Stop().GetAwaiter().GetResult(); } var process = job.Process; @@ -561,7 +596,7 @@ namespace MediaBrowser.Api { try { - Logger.LogInformation("Stopping ffmpeg process with q command for {path}", job.Path); + Logger.LogInformation("Stopping ffmpeg process with q command for {Path}", job.Path); //process.Kill(); process.StandardInput.WriteLine("q"); @@ -569,13 +604,13 @@ namespace MediaBrowser.Api // Need to wait because killing is asynchronous if (!process.WaitForExit(5000)) { - Logger.LogInformation("Killing ffmpeg process for {path}", job.Path); + Logger.LogInformation("Killing ffmpeg process for {Path}", job.Path); process.Kill(); } } catch (Exception ex) { - Logger.LogError(ex, "Error killing transcoding job for {path}", job.Path); + Logger.LogError(ex, "Error killing transcoding job for {Path}", job.Path); } } } @@ -605,7 +640,7 @@ namespace MediaBrowser.Api return; } - Logger.LogInformation("Deleting partial stream file(s) {0}", path); + Logger.LogInformation("Deleting partial stream file(s) {Path}", path); await Task.Delay(delayMs).ConfigureAwait(false); @@ -626,13 +661,13 @@ namespace MediaBrowser.Api } catch (IOException ex) { - Logger.LogError(ex, "Error deleting partial stream file(s) {path}", path); + Logger.LogError(ex, "Error deleting partial stream file(s) {Path}", path); await DeletePartialStreamFiles(path, jobType, retryCount + 1, 500).ConfigureAwait(false); } catch (Exception ex) { - Logger.LogError(ex, "Error deleting partial stream file(s) {path}", path); + Logger.LogError(ex, "Error deleting partial stream file(s) {Path}", path); } } @@ -673,7 +708,7 @@ namespace MediaBrowser.Api catch (IOException ex) { e = ex; - Logger.LogError(ex, "Error deleting HLS file {path}", file); + Logger.LogError(ex, "Error deleting HLS file {Path}", file); } } @@ -717,7 +752,7 @@ namespace MediaBrowser.Api /// Gets or sets the process. /// /// The process. - public IProcess Process { get; set; } + public Process Process { get; set; } public ILogger Logger { get; private set; } /// /// Gets or sets the active request count. diff --git a/MediaBrowser.Api/BaseApiService.cs b/MediaBrowser.Api/BaseApiService.cs index 4a484b7185..7b08a3c529 100644 --- a/MediaBrowser.Api/BaseApiService.cs +++ b/MediaBrowser.Api/BaseApiService.cs @@ -43,7 +43,7 @@ namespace MediaBrowser.Api public static string[] SplitValue(string value, char delim) { - if (string.IsNullOrWhiteSpace(value)) + if (value == null) { return Array.Empty(); } @@ -53,8 +53,14 @@ namespace MediaBrowser.Api public static Guid[] GetGuids(string value) { - // Unfortunately filtermenu.js was using |. This can be deprecated after a while. - return (value ?? string.Empty).Split(new[] { ',', '|' }, StringSplitOptions.RemoveEmptyEntries).Select(i => new Guid(i)).ToArray(); + if (value == null) + { + return Array.Empty(); + } + + return value.Split(new[] { ',', '|' }, StringSplitOptions.RemoveEmptyEntries) + .Select(i => new Guid(i)) + .ToArray(); } /// @@ -118,7 +124,8 @@ namespace MediaBrowser.Api options.Fields = hasFields.GetItemFields(); } - if (!options.ContainsField(Model.Querying.ItemFields.RecursiveItemCount) || !options.ContainsField(Model.Querying.ItemFields.ChildCount)) + if (!options.ContainsField(Model.Querying.ItemFields.RecursiveItemCount) + || !options.ContainsField(Model.Querying.ItemFields.ChildCount)) { var client = authContext.GetAuthorizationInfo(Request).Client ?? string.Empty; if (client.IndexOf("kodi", StringComparison.OrdinalIgnoreCase) != -1 || @@ -145,8 +152,7 @@ namespace MediaBrowser.Api } } - var hasDtoOptions = request as IHasDtoOptions; - if (hasDtoOptions != null) + if (request is IHasDtoOptions hasDtoOptions) { options.EnableImages = hasDtoOptions.EnableImages ?? true; @@ -294,7 +300,7 @@ namespace MediaBrowser.Api return pathInfo[index]; } - private List Parse(string pathUri) + private string[] Parse(string pathUri) { var actionParts = pathUri.Split(new[] { "://" }, StringSplitOptions.None); @@ -308,7 +314,7 @@ namespace MediaBrowser.Api var args = pathInfo.Split('/'); - return args.Skip(1).ToList(); + return args.Skip(1).ToArray(); } /// diff --git a/MediaBrowser.Api/Playback/BaseStreamingService.cs b/MediaBrowser.Api/Playback/BaseStreamingService.cs index ec42cad33f..641d539f2b 100644 --- a/MediaBrowser.Api/Playback/BaseStreamingService.cs +++ b/MediaBrowser.Api/Playback/BaseStreamingService.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; @@ -68,10 +69,8 @@ namespace MediaBrowser.Api.Playback protected IDeviceManager DeviceManager { get; private set; } protected ISubtitleEncoder SubtitleEncoder { get; private set; } protected IMediaSourceManager MediaSourceManager { get; private set; } - protected IZipClient ZipClient { get; private set; } protected IJsonSerializer JsonSerializer { get; private set; } - public static IServerApplicationHost AppHost; public static IHttpClient HttpClient; protected IAuthorizationContext AuthorizationContext { get; private set; } @@ -80,21 +79,33 @@ namespace MediaBrowser.Api.Playback /// /// Initializes a new instance of the class. /// - protected BaseStreamingService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IAuthorizationContext authorizationContext) + protected BaseStreamingService( + IServerConfigurationManager serverConfig, + IUserManager userManager, + ILibraryManager libraryManager, + IIsoManager isoManager, + IMediaEncoder mediaEncoder, + IFileSystem fileSystem, + IDlnaManager dlnaManager, + ISubtitleEncoder subtitleEncoder, + IDeviceManager deviceManager, + IMediaSourceManager mediaSourceManager, + IJsonSerializer jsonSerializer, + IAuthorizationContext authorizationContext) { - JsonSerializer = jsonSerializer; - AuthorizationContext = authorizationContext; - ZipClient = zipClient; - MediaSourceManager = mediaSourceManager; - DeviceManager = deviceManager; - SubtitleEncoder = subtitleEncoder; - DlnaManager = dlnaManager; - FileSystem = fileSystem; ServerConfigurationManager = serverConfig; UserManager = userManager; LibraryManager = libraryManager; IsoManager = isoManager; MediaEncoder = mediaEncoder; + FileSystem = fileSystem; + DlnaManager = dlnaManager; + SubtitleEncoder = subtitleEncoder; + DeviceManager = deviceManager; + MediaSourceManager = mediaSourceManager; + JsonSerializer = jsonSerializer; + AuthorizationContext = authorizationContext; + EncodingHelper = new EncodingHelper(MediaEncoder, FileSystem, SubtitleEncoder); } @@ -187,7 +198,8 @@ namespace MediaBrowser.Api.Playback /// The cancellation token source. /// The working directory. /// Task. - protected async Task StartFfMpeg(StreamState state, + protected async Task StartFfMpeg( + StreamState state, string outputPath, CancellationTokenSource cancellationTokenSource, string workingDirectory = null) @@ -215,24 +227,27 @@ namespace MediaBrowser.Api.Playback var transcodingId = Guid.NewGuid().ToString("N"); var commandLineArgs = GetCommandLineArguments(outputPath, encodingOptions, state, true); - var process = ApiEntryPoint.Instance.ProcessFactory.Create(new ProcessOptions + var process = new Process() { - CreateNoWindow = true, - UseShellExecute = false, - - // Must consume both stdout and stderr or deadlocks may occur - //RedirectStandardOutput = true, - RedirectStandardError = true, - RedirectStandardInput = true, - - FileName = MediaEncoder.EncoderPath, - Arguments = commandLineArgs, - - IsHidden = true, - ErrorDialog = false, - EnableRaisingEvents = true, - WorkingDirectory = !string.IsNullOrWhiteSpace(workingDirectory) ? workingDirectory : null - }); + StartInfo = new ProcessStartInfo() + { + WindowStyle = ProcessWindowStyle.Hidden, + CreateNoWindow = true, + UseShellExecute = false, + + // Must consume both stdout and stderr or deadlocks may occur + //RedirectStandardOutput = true, + RedirectStandardError = true, + RedirectStandardInput = true, + + FileName = MediaEncoder.EncoderPath, + Arguments = commandLineArgs, + WorkingDirectory = string.IsNullOrWhiteSpace(workingDirectory) ? null : workingDirectory, + + ErrorDialog = false + }, + EnableRaisingEvents = true + }; var transcodingJob = ApiEntryPoint.Instance.OnTranscodeBeginning(outputPath, state.Request.PlaySessionId, @@ -248,13 +263,17 @@ namespace MediaBrowser.Api.Playback Logger.LogInformation(commandLineLogMessage); var logFilePrefix = "ffmpeg-transcode"; - if (state.VideoRequest != null && string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase) && string.Equals(state.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase)) - { - logFilePrefix = "ffmpeg-directstream"; - } - else if (state.VideoRequest != null && string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) + if (state.VideoRequest != null) { - logFilePrefix = "ffmpeg-remux"; + if (string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase) + && string.Equals(state.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase)) + { + logFilePrefix = "ffmpeg-directstream"; + } + else if (string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) + { + logFilePrefix = "ffmpeg-remux"; + } } var logFilePath = Path.Combine(ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, logFilePrefix + "-" + Guid.NewGuid() + ".txt"); @@ -341,7 +360,7 @@ namespace MediaBrowser.Api.Playback /// The process. /// The job. /// The state. - private void OnFfMpegProcessExited(IProcess process, TranscodingJob job, StreamState state) + private void OnFfMpegProcessExited(Process process, TranscodingJob job, StreamState state) { if (job != null) { diff --git a/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs b/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs index 08a2183f83..1acc42ea52 100644 --- a/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/BaseHlsService.cs @@ -54,17 +54,6 @@ namespace MediaBrowser.Api.Playback.Hls /// The type of the transcoding job. protected override TranscodingJobType TranscodingJobType => TranscodingJobType.Hls; - /// - /// Processes the request. - /// - /// The request. - /// if set to true [is live]. - /// System.Object. - protected async Task ProcessRequest(StreamRequest request, bool isLive) - { - return await ProcessRequestAsync(request, isLive).ConfigureAwait(false); - } - /// /// Processes the request async. /// @@ -74,7 +63,7 @@ namespace MediaBrowser.Api.Playback.Hls /// A video bitrate is required /// or /// An audio bitrate is required - private async Task ProcessRequestAsync(StreamRequest request, bool isLive) + protected async Task ProcessRequestAsync(StreamRequest request, bool isLive) { var cancellationTokenSource = new CancellationTokenSource(); @@ -324,7 +313,31 @@ namespace MediaBrowser.Api.Playback.Hls return 0; } - public BaseHlsService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IAuthorizationContext authorizationContext) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer, authorizationContext) + public BaseHlsService( + IServerConfigurationManager serverConfig, + IUserManager userManager, + ILibraryManager libraryManager, + IIsoManager isoManager, + IMediaEncoder mediaEncoder, + IFileSystem fileSystem, + IDlnaManager dlnaManager, + ISubtitleEncoder subtitleEncoder, + IDeviceManager deviceManager, + IMediaSourceManager mediaSourceManager, + IJsonSerializer jsonSerializer, + IAuthorizationContext authorizationContext) + : base(serverConfig, + userManager, + libraryManager, + isoManager, + mediaEncoder, + fileSystem, + dlnaManager, + subtitleEncoder, + deviceManager, + mediaSourceManager, + jsonSerializer, + authorizationContext) { } } diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs index 30696ec979..771573d9a0 100644 --- a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs @@ -95,7 +95,32 @@ namespace MediaBrowser.Api.Playback.Hls public class DynamicHlsService : BaseHlsService { - public DynamicHlsService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IAuthorizationContext authorizationContext, INetworkManager networkManager) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer, authorizationContext) + public DynamicHlsService( + IServerConfigurationManager serverConfig, + IUserManager userManager, + ILibraryManager libraryManager, + IIsoManager isoManager, + IMediaEncoder mediaEncoder, + IFileSystem fileSystem, + IDlnaManager dlnaManager, + ISubtitleEncoder subtitleEncoder, + IDeviceManager deviceManager, + IMediaSourceManager mediaSourceManager, + IJsonSerializer jsonSerializer, + IAuthorizationContext authorizationContext, + INetworkManager networkManager) + : base(serverConfig, + userManager, + libraryManager, + isoManager, + mediaEncoder, + fileSystem, + dlnaManager, + subtitleEncoder, + deviceManager, + mediaSourceManager, + jsonSerializer, + authorizationContext) { NetworkManager = networkManager; } diff --git a/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs b/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs index 142dc2dfd4..eb1bbfb74b 100644 --- a/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Devices; using MediaBrowser.Controller.Dlna; @@ -24,9 +25,9 @@ namespace MediaBrowser.Api.Playback.Hls [Authenticated] public class VideoHlsService : BaseHlsService { - public object Get(GetLiveHlsStream request) + public Task Get(GetLiveHlsStream request) { - return ProcessRequest(request, true); + return ProcessRequestAsync(request, true); } /// @@ -130,7 +131,31 @@ namespace MediaBrowser.Api.Playback.Hls return args; } - public VideoHlsService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IAuthorizationContext authorizationContext) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer, authorizationContext) + public VideoHlsService( + IServerConfigurationManager serverConfig, + IUserManager userManager, + ILibraryManager libraryManager, + IIsoManager isoManager, + IMediaEncoder mediaEncoder, + IFileSystem fileSystem, + IDlnaManager dlnaManager, + ISubtitleEncoder subtitleEncoder, + IDeviceManager deviceManager, + IMediaSourceManager mediaSourceManager, + IJsonSerializer jsonSerializer, + IAuthorizationContext authorizationContext) + : base(serverConfig, + userManager, + libraryManager, + isoManager, + mediaEncoder, + fileSystem, + dlnaManager, + subtitleEncoder, + deviceManager, + mediaSourceManager, + jsonSerializer, + authorizationContext) { } } diff --git a/MediaBrowser.Api/Playback/Progressive/AudioService.cs b/MediaBrowser.Api/Playback/Progressive/AudioService.cs index 150571ce9a..208a5560d2 100644 --- a/MediaBrowser.Api/Playback/Progressive/AudioService.cs +++ b/MediaBrowser.Api/Playback/Progressive/AudioService.cs @@ -32,7 +32,33 @@ namespace MediaBrowser.Api.Playback.Progressive //[Authenticated] public class AudioService : BaseProgressiveStreamingService { - public AudioService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IAuthorizationContext authorizationContext, IImageProcessor imageProcessor, IEnvironmentInfo environmentInfo) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer, authorizationContext, imageProcessor, environmentInfo) + public AudioService( + IServerConfigurationManager serverConfig, + IUserManager userManager, + ILibraryManager libraryManager, + IIsoManager isoManager, + IMediaEncoder mediaEncoder, + IFileSystem fileSystem, + IDlnaManager dlnaManager, + ISubtitleEncoder subtitleEncoder, + IDeviceManager deviceManager, + IMediaSourceManager mediaSourceManager, + IJsonSerializer jsonSerializer, + IAuthorizationContext authorizationContext, + IEnvironmentInfo environmentInfo) + : base(serverConfig, + userManager, + libraryManager, + isoManager, + mediaEncoder, + fileSystem, + dlnaManager, + subtitleEncoder, + deviceManager, + mediaSourceManager, + jsonSerializer, + authorizationContext, + environmentInfo) { } diff --git a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs index 621d1ff88d..c197de1739 100644 --- a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs +++ b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs @@ -25,12 +25,35 @@ namespace MediaBrowser.Api.Playback.Progressive /// public abstract class BaseProgressiveStreamingService : BaseStreamingService { - protected readonly IImageProcessor ImageProcessor; protected readonly IEnvironmentInfo EnvironmentInfo; - public BaseProgressiveStreamingService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IAuthorizationContext authorizationContext, IImageProcessor imageProcessor, IEnvironmentInfo environmentInfo) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer, authorizationContext) + public BaseProgressiveStreamingService( + IServerConfigurationManager serverConfig, + IUserManager userManager, + ILibraryManager libraryManager, + IIsoManager isoManager, + IMediaEncoder mediaEncoder, + IFileSystem fileSystem, + IDlnaManager dlnaManager, + ISubtitleEncoder subtitleEncoder, + IDeviceManager deviceManager, + IMediaSourceManager mediaSourceManager, + IJsonSerializer jsonSerializer, + IAuthorizationContext authorizationContext, + IEnvironmentInfo environmentInfo) + : base(serverConfig, + userManager, + libraryManager, + isoManager, + mediaEncoder, + fileSystem, + dlnaManager, + subtitleEncoder, + deviceManager, + mediaSourceManager, + jsonSerializer, + authorizationContext) { - ImageProcessor = imageProcessor; EnvironmentInfo = environmentInfo; } diff --git a/MediaBrowser.Api/Playback/Progressive/VideoService.cs b/MediaBrowser.Api/Playback/Progressive/VideoService.cs index 00b79ccdd2..a0ea5c62d9 100644 --- a/MediaBrowser.Api/Playback/Progressive/VideoService.cs +++ b/MediaBrowser.Api/Playback/Progressive/VideoService.cs @@ -68,7 +68,33 @@ namespace MediaBrowser.Api.Playback.Progressive //[Authenticated] public class VideoService : BaseProgressiveStreamingService { - public VideoService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IAuthorizationContext authorizationContext, IImageProcessor imageProcessor, IEnvironmentInfo environmentInfo) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer, authorizationContext, imageProcessor, environmentInfo) + public VideoService( + IServerConfigurationManager serverConfig, + IUserManager userManager, + ILibraryManager libraryManager, + IIsoManager isoManager, + IMediaEncoder mediaEncoder, + IFileSystem fileSystem, + IDlnaManager dlnaManager, + ISubtitleEncoder subtitleEncoder, + IDeviceManager deviceManager, + IMediaSourceManager mediaSourceManager, + IJsonSerializer jsonSerializer, + IAuthorizationContext authorizationContext, + IEnvironmentInfo environmentInfo) + : base(serverConfig, + userManager, + libraryManager, + isoManager, + mediaEncoder, + fileSystem, + dlnaManager, + subtitleEncoder, + deviceManager, + mediaSourceManager, + jsonSerializer, + authorizationContext, + environmentInfo) { } diff --git a/MediaBrowser.Api/Playback/TranscodingThrottler.cs b/MediaBrowser.Api/Playback/TranscodingThrottler.cs index 97f21c8f30..9f416098af 100644 --- a/MediaBrowser.Api/Playback/TranscodingThrottler.cs +++ b/MediaBrowser.Api/Playback/TranscodingThrottler.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using MediaBrowser.Common.Configuration; using MediaBrowser.Model.Configuration; using MediaBrowser.Model.IO; @@ -36,7 +37,7 @@ namespace MediaBrowser.Api.Playback _timer = _timerFactory.Create(TimerCallback, null, 5000, 5000); } - private void TimerCallback(object state) + private async void TimerCallback(object state) { if (_job.HasExited) { @@ -48,15 +49,15 @@ namespace MediaBrowser.Api.Playback if (options.EnableThrottling && IsThrottleAllowed(_job, options.ThrottleDelaySeconds)) { - PauseTranscoding(); + await PauseTranscoding(); } else { - UnpauseTranscoding(); + await UnpauseTranscoding(); } } - private void PauseTranscoding() + private async Task PauseTranscoding() { if (!_isPaused) { @@ -64,7 +65,7 @@ namespace MediaBrowser.Api.Playback try { - _job.Process.StandardInput.Write("c"); + await _job.Process.StandardInput.WriteAsync("c"); _isPaused = true; } catch (Exception ex) @@ -74,7 +75,7 @@ namespace MediaBrowser.Api.Playback } } - public void UnpauseTranscoding() + public async Task UnpauseTranscoding() { if (_isPaused) { @@ -82,7 +83,7 @@ namespace MediaBrowser.Api.Playback try { - _job.Process.StandardInput.WriteLine(); + await _job.Process.StandardInput.WriteLineAsync(); _isPaused = false; } catch (Exception ex) @@ -153,10 +154,10 @@ namespace MediaBrowser.Api.Playback return false; } - public void Stop() + public async Task Stop() { DisposeTimer(); - UnpauseTranscoding(); + await UnpauseTranscoding(); } public void Dispose() diff --git a/MediaBrowser.Api/Playback/UniversalAudioService.cs b/MediaBrowser.Api/Playback/UniversalAudioService.cs index 1aa77792ce..e07770a4c4 100644 --- a/MediaBrowser.Api/Playback/UniversalAudioService.cs +++ b/MediaBrowser.Api/Playback/UniversalAudioService.cs @@ -287,7 +287,6 @@ namespace MediaBrowser.Api.Playback SubtitleEncoder, DeviceManager, MediaSourceManager, - ZipClient, JsonSerializer, AuthorizationContext, NetworkManager) @@ -334,10 +333,8 @@ namespace MediaBrowser.Api.Playback SubtitleEncoder, DeviceManager, MediaSourceManager, - ZipClient, JsonSerializer, AuthorizationContext, - ImageProcessor, EnvironmentInfo) { Request = Request From da2caa29027c2a0a3725c4fd3a08a6674ad374c8 Mon Sep 17 00:00:00 2001 From: Bond-009 Date: Sun, 3 Feb 2019 00:03:07 +0100 Subject: [PATCH 2/3] await async functions --- MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs | 4 ++-- MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs index 771573d9a0..45f003cae8 100644 --- a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs @@ -234,7 +234,7 @@ namespace MediaBrowser.Api.Playback.Hls // If the playlist doesn't already exist, startup ffmpeg try { - ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, p => false); + await ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, p => false); if (currentTranscodingIndex.HasValue) { @@ -258,7 +258,7 @@ namespace MediaBrowser.Api.Playback.Hls job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType); if (job.TranscodingThrottler != null) { - job.TranscodingThrottler.UnpauseTranscoding(); + await job.TranscodingThrottler.UnpauseTranscoding(); } } } diff --git a/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs b/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs index 02b8379123..6a2c7ae038 100644 --- a/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs +++ b/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs @@ -102,9 +102,9 @@ namespace MediaBrowser.Api.Playback.Hls return GetFileResult(file, file); } - public void Delete(StopEncodingProcess request) + public Task Delete(StopEncodingProcess request) { - ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, path => true); + return ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, path => true); } /// From cb7bffc233bcdb1dfb2b6be629dc8f13e482cd3b Mon Sep 17 00:00:00 2001 From: Bond-009 Date: Sun, 3 Feb 2019 00:15:28 +0100 Subject: [PATCH 3/3] Remove unused string split --- MediaBrowser.Api/BaseApiService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MediaBrowser.Api/BaseApiService.cs b/MediaBrowser.Api/BaseApiService.cs index 7b08a3c529..451ee72dd9 100644 --- a/MediaBrowser.Api/BaseApiService.cs +++ b/MediaBrowser.Api/BaseApiService.cs @@ -58,7 +58,7 @@ namespace MediaBrowser.Api return Array.Empty(); } - return value.Split(new[] { ',', '|' }, StringSplitOptions.RemoveEmptyEntries) + return value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Select(i => new Guid(i)) .ToArray(); }