From c8a106f485ff7e340ee8ca67adac3351ec6a31b6 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 12 Jan 2014 01:31:21 -0500 Subject: move media encoder to server project --- .../LiveTv/LiveTvDtoService.cs | 2 +- .../LiveTv/LiveTvManager.cs | 62 ++++++++++++++++++++-- .../MediaEncoder/MediaEncoder.cs | 24 ++++----- 3 files changed, 70 insertions(+), 18 deletions(-) (limited to 'MediaBrowser.Server.Implementations') diff --git a/MediaBrowser.Server.Implementations/LiveTv/LiveTvDtoService.cs b/MediaBrowser.Server.Implementations/LiveTv/LiveTvDtoService.cs index 83da28b8f..c042e7750 100644 --- a/MediaBrowser.Server.Implementations/LiveTv/LiveTvDtoService.cs +++ b/MediaBrowser.Server.Implementations/LiveTv/LiveTvDtoService.cs @@ -368,7 +368,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv return null; } - private const string InternalVersionNumber = "2"; + private const string InternalVersionNumber = "3"; public Guid GetInternalChannelId(string serviceName, string externalId) { diff --git a/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs b/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs index 3bc146bd4..17e17ab70 100644 --- a/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs +++ b/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs @@ -6,12 +6,14 @@ using MediaBrowser.Controller.Dto; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.LiveTv; +using MediaBrowser.Controller.MediaInfo; using MediaBrowser.Controller.Persistence; using MediaBrowser.Model.Entities; using MediaBrowser.Model.LiveTv; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Querying; using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; @@ -23,7 +25,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv /// /// Class LiveTvManager /// - public class LiveTvManager : ILiveTvManager + public class LiveTvManager : ILiveTvManager, IDisposable { private readonly IServerApplicationPaths _appPaths; private readonly IFileSystem _fileSystem; @@ -31,15 +33,19 @@ namespace MediaBrowser.Server.Implementations.LiveTv private readonly IItemRepository _itemRepo; private readonly IUserManager _userManager; private readonly ILibraryManager _libraryManager; + private readonly IMediaEncoder _mediaEncoder; private readonly LiveTvDtoService _tvDtoService; private readonly List _services = new List(); + private readonly ConcurrentDictionary _openStreams = + new ConcurrentDictionary(); + private List _channelIdList = new List(); private Dictionary _programs = new Dictionary(); - public LiveTvManager(IServerApplicationPaths appPaths, IFileSystem fileSystem, ILogger logger, IItemRepository itemRepo, IImageProcessor imageProcessor, IUserDataManager userDataManager, IDtoService dtoService, IUserManager userManager, ILibraryManager libraryManager) + public LiveTvManager(IServerApplicationPaths appPaths, IFileSystem fileSystem, ILogger logger, IItemRepository itemRepo, IImageProcessor imageProcessor, IUserDataManager userDataManager, IDtoService dtoService, IUserManager userManager, ILibraryManager libraryManager, IMediaEncoder mediaEncoder) { _appPaths = appPaths; _fileSystem = fileSystem; @@ -47,6 +53,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv _itemRepo = itemRepo; _userManager = userManager; _libraryManager = libraryManager; + _mediaEncoder = mediaEncoder; _tvDtoService = new LiveTvDtoService(dtoService, userDataManager, imageProcessor, logger, _itemRepo); } @@ -180,7 +187,14 @@ namespace MediaBrowser.Server.Implementations.LiveTv var recording = recordings.First(i => _tvDtoService.GetInternalRecordingId(service.Name, i.Id) == new Guid(id)); - return await service.GetRecordingStream(recording.Id, cancellationToken).ConfigureAwait(false); + var result = await service.GetRecordingStream(recording.Id, cancellationToken).ConfigureAwait(false); + + if (!string.IsNullOrEmpty(result.Id)) + { + _openStreams.AddOrUpdate(result.Id, result, (key, info) => result); + } + + return result; } public async Task GetChannelStream(string id, CancellationToken cancellationToken) @@ -189,12 +203,19 @@ namespace MediaBrowser.Server.Implementations.LiveTv var channel = GetInternalChannel(id); - return await service.GetChannelStream(channel.ChannelInfo.Id, cancellationToken).ConfigureAwait(false); + var result = await service.GetChannelStream(channel.ChannelInfo.Id, cancellationToken).ConfigureAwait(false); + + if (!string.IsNullOrEmpty(result.Id)) + { + _openStreams.AddOrUpdate(result.Id, result, (key, info) => result); + } + + return result; } private async Task GetChannel(ChannelInfo channelInfo, string serviceName, CancellationToken cancellationToken) { - var path = Path.Combine(_appPaths.ItemsByNamePath, "channels", _fileSystem.GetValidFilename(serviceName), _fileSystem.GetValidFilename(channelInfo.Name)); + var path = Path.Combine(_appPaths.ItemsByNamePath, "channels", _fileSystem.GetValidFilename(channelInfo.Name)); var fileInfo = new DirectoryInfo(path); @@ -1047,5 +1068,36 @@ namespace MediaBrowser.Server.Implementations.LiveTv EndDate = endDate }; } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + public void Dispose() + { + Dispose(true); + } + + private readonly object _disposeLock = new object(); + /// + /// Releases unmanaged and - optionally - managed resources. + /// + /// true to release both managed and unmanaged resources; false to release only unmanaged resources. + protected virtual void Dispose(bool dispose) + { + if (dispose) + { + lock (_disposeLock) + { + foreach (var stream in _openStreams.Values.ToList()) + { + var task = CloseLiveStream(stream.Id, CancellationToken.None); + + Task.WaitAll(task); + } + + _openStreams.Clear(); + } + } + } } } diff --git a/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs b/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs index bcc857a80..6f956ba20 100644 --- a/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs +++ b/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs @@ -1,6 +1,6 @@ using MediaBrowser.Common.Configuration; using MediaBrowser.Common.IO; -using MediaBrowser.Common.MediaInfo; +using MediaBrowser.Controller.MediaInfo; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Serialization; @@ -104,10 +104,10 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder /// The type. /// The cancellation token. /// Task. - public Task GetMediaInfo(string[] inputFiles, InputType type, + public Task GetMediaInfo(string[] inputFiles, InputType type, CancellationToken cancellationToken) { - return GetMediaInfoInternal(GetInputArgument(inputFiles, type), type != InputType.AudioFile, + return GetMediaInfoInternal(GetInputArgument(inputFiles, type), type != InputType.File, GetProbeSizeArgument(type), cancellationToken); } @@ -125,8 +125,7 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder switch (type) { case InputType.Dvd: - case InputType.VideoFile: - case InputType.AudioFile: + case InputType.File: inputPath = GetConcatInputArgument(inputFiles); break; case InputType.Bluray: @@ -173,7 +172,7 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder /// The cancellation token. /// Task{MediaInfoResult}. /// - private async Task GetMediaInfoInternal(string inputPath, bool extractChapters, + private async Task GetMediaInfoInternal(string inputPath, bool extractChapters, string probeSizeArgument, CancellationToken cancellationToken) { @@ -206,7 +205,7 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder await _ffProbeResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); - MediaInfoResult result; + InternalMediaInfoResult result; string standardError = null; try @@ -236,7 +235,7 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder process.BeginErrorReadLine(); } - result = _jsonSerializer.DeserializeFromStream(process.StandardOutput.BaseStream); + result = _jsonSerializer.DeserializeFromStream(process.StandardOutput.BaseStream); if (extractChapters) { @@ -307,7 +306,7 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder /// /// The result. /// The standard error. - private void AddChapters(MediaInfoResult result, string standardError) + private void AddChapters(InternalMediaInfoResult result, string standardError) { var lines = standardError.Split('\n').Select(l => l.TrimStart()); @@ -797,19 +796,20 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder /// /// The input files. /// The type. + /// if set to true [is audio]. /// The threed format. /// The offset. /// The output path. /// The cancellation token. /// Task. /// Must use inputPath list overload - public async Task ExtractImage(string[] inputFiles, InputType type, Video3DFormat? threedFormat, TimeSpan? offset, string outputPath, CancellationToken cancellationToken) + public async Task ExtractImage(string[] inputFiles, InputType type, bool isAudio, Video3DFormat? threedFormat, TimeSpan? offset, string outputPath, CancellationToken cancellationToken) { - var resourcePool = type == InputType.AudioFile ? _audioImageResourcePool : _videoImageResourcePool; + var resourcePool = isAudio ? _audioImageResourcePool : _videoImageResourcePool; var inputArgument = GetInputArgument(inputFiles, type); - if (type != InputType.AudioFile) + if (!isAudio) { try { -- cgit v1.2.3