diff --git a/MediaBrowser.Controller/LiveTv/RecordingInfo.cs b/MediaBrowser.Controller/LiveTv/RecordingInfo.cs index bf453ccf4d..e5817d390d 100644 --- a/MediaBrowser.Controller/LiveTv/RecordingInfo.cs +++ b/MediaBrowser.Controller/LiveTv/RecordingInfo.cs @@ -16,6 +16,12 @@ namespace MediaBrowser.Controller.LiveTv /// /// The series timer identifier. public string SeriesTimerId { get; set; } + + /// + /// Gets or sets the timer identifier. + /// + /// The timer identifier. + public string TimerId { get; set; } /// /// ChannelId of the recording. diff --git a/MediaBrowser.Server.Implementations/Channels/ChannelPostScanTask.cs b/MediaBrowser.Server.Implementations/Channels/ChannelPostScanTask.cs index d266cca6c0..baf446942b 100644 --- a/MediaBrowser.Server.Implementations/Channels/ChannelPostScanTask.cs +++ b/MediaBrowser.Server.Implementations/Channels/ChannelPostScanTask.cs @@ -12,7 +12,7 @@ using System.Threading.Tasks; namespace MediaBrowser.Server.Implementations.Channels { - public class ChannelPostScanTask : ILibraryPostScanTask + public class ChannelPostScanTask { private readonly IChannelManager _channelManager; private readonly IUserManager _userManager; diff --git a/MediaBrowser.Server.Implementations/Channels/RefreshChannelsScheduledTask.cs b/MediaBrowser.Server.Implementations/Channels/RefreshChannelsScheduledTask.cs index b1491d594e..df94580a57 100644 --- a/MediaBrowser.Server.Implementations/Channels/RefreshChannelsScheduledTask.cs +++ b/MediaBrowser.Server.Implementations/Channels/RefreshChannelsScheduledTask.cs @@ -1,6 +1,7 @@ using MediaBrowser.Common.ScheduledTasks; using MediaBrowser.Controller.Channels; -using MediaBrowser.Model.Tasks; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.Logging; using System; using System.Collections.Generic; using System.Threading.Tasks; @@ -9,11 +10,15 @@ namespace MediaBrowser.Server.Implementations.Channels { class RefreshChannelsScheduledTask : IScheduledTask, IConfigurableScheduledTask { - private readonly IChannelManager _manager; + private readonly IChannelManager _channelManager; + private readonly IUserManager _userManager; + private readonly ILogger _logger; - public RefreshChannelsScheduledTask(IChannelManager manager) + public RefreshChannelsScheduledTask(IChannelManager channelManager, IUserManager userManager, ILogger logger) { - _manager = manager; + _channelManager = channelManager; + _userManager = userManager; + _logger = logger; } public string Name @@ -31,11 +36,14 @@ namespace MediaBrowser.Server.Implementations.Channels get { return "Channels"; } } - public Task Execute(System.Threading.CancellationToken cancellationToken, IProgress progress) + public async Task Execute(System.Threading.CancellationToken cancellationToken, IProgress progress) { - var manager = (ChannelManager)_manager; + var manager = (ChannelManager)_channelManager; - return manager.RefreshChannels(progress, cancellationToken); + await manager.RefreshChannels(new Progress(), cancellationToken).ConfigureAwait(false); + + await new ChannelPostScanTask(_channelManager, _userManager, _logger).Run(progress, cancellationToken) + .ConfigureAwait(false); } public IEnumerable GetDefaultTriggers() @@ -48,7 +56,7 @@ namespace MediaBrowser.Server.Implementations.Channels public bool IsHidden { - get { return true; } + get { return false; } } public bool IsEnabled diff --git a/MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs b/MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs index 26ccdfc9a4..e1c5291870 100644 --- a/MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs +++ b/MediaBrowser.Server.Implementations/IO/LibraryMonitor.cs @@ -1,5 +1,4 @@ -using System.ComponentModel; -using MediaBrowser.Common.IO; +using MediaBrowser.Common.IO; using MediaBrowser.Common.ScheduledTasks; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; @@ -372,15 +371,12 @@ namespace MediaBrowser.Server.Implementations.IO DisposeWatcher(dw); - if (ex is Win32Exception) + if (ConfigurationManager.Configuration.EnableLibraryMonitor == AutoOnOff.Auto) { Logger.Info("Disabling realtime monitor to prevent future instability"); - if (ConfigurationManager.Configuration.EnableLibraryMonitor == AutoOnOff.Auto) - { - ConfigurationManager.Configuration.EnableLibraryMonitor = AutoOnOff.Disabled; - Stop(); - } + ConfigurationManager.Configuration.EnableLibraryMonitor = AutoOnOff.Disabled; + Stop(); } } diff --git a/MediaBrowser.Server.Implementations/LiveTv/EmbyTV/EmbyTV.cs b/MediaBrowser.Server.Implementations/LiveTv/EmbyTV/EmbyTV.cs index 0ed87bbff3..3374a3cc9c 100644 --- a/MediaBrowser.Server.Implementations/LiveTv/EmbyTV/EmbyTV.cs +++ b/MediaBrowser.Server.Implementations/LiveTv/EmbyTV/EmbyTV.cs @@ -33,8 +33,12 @@ namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV private readonly LiveTvManager _liveTvManager; + public static EmbyTV Current; + public EmbyTV(IApplicationHost appHost, ILogger logger, IJsonSerializer jsonSerializer, IHttpClient httpClient, IConfigurationManager config, ILiveTvManager liveTvManager) { + Current = this; + _appHpst = appHost; _logger = logger; _httpClient = httpClient; @@ -48,6 +52,11 @@ namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV _timerProvider.TimerFired += _timerProvider_TimerFired; } + public void Start() + { + _timerProvider.RestartTimers(); + } + public event EventHandler DataSourceChanged; public event EventHandler RecordingStatusChanged; @@ -147,7 +156,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV public Task CancelSeriesTimerAsync(string timerId, CancellationToken cancellationToken) { - var remove = _seriesTimerProvider.GetAll().SingleOrDefault(r => r.Id == timerId); + var remove = _seriesTimerProvider.GetAll().FirstOrDefault(r => string.Equals(r.Id, timerId, StringComparison.OrdinalIgnoreCase)); if (remove != null) { _seriesTimerProvider.Delete(remove); @@ -157,7 +166,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV private void CancelTimerInternal(string timerId) { - var remove = _timerProvider.GetAll().SingleOrDefault(r => r.Id == timerId); + var remove = _timerProvider.GetAll().FirstOrDefault(r => string.Equals(r.Id, timerId, StringComparison.OrdinalIgnoreCase)); if (remove != null) { _timerProvider.Delete(remove); @@ -176,11 +185,24 @@ namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV return Task.FromResult(true); } - public Task DeleteRecordingAsync(string recordingId, CancellationToken cancellationToken) + public async Task DeleteRecordingAsync(string recordingId, CancellationToken cancellationToken) { var remove = _recordingProvider.GetAll().FirstOrDefault(i => string.Equals(i.Id, recordingId, StringComparison.OrdinalIgnoreCase)); if (remove != null) { + if (!string.IsNullOrWhiteSpace(remove.TimerId)) + { + var enableDelay = _activeRecordings.ContainsKey(remove.TimerId); + + CancelTimerInternal(remove.TimerId); + + if (enableDelay) + { + // A hack yes, but need to make sure the file is closed before attempting to delete it + await Task.Delay(3000).ConfigureAwait(false); + } + } + try { File.Delete(remove.Path); @@ -195,7 +217,6 @@ namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV } _recordingProvider.Delete(remove); } - return Task.FromResult(true); } public Task CreateTimerAsync(TimerInfo info, CancellationToken cancellationToken) @@ -293,6 +314,8 @@ namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV if (list.Count > 0) { + SaveEpgDataForChannel(channelId, list); + return list; } } @@ -430,7 +453,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV } var mediaStreamInfo = await GetChannelStream(timer.ChannelId, null, CancellationToken.None); - var duration = (timer.EndDate - RecordingHelper.GetStartTime(timer)).TotalSeconds + timer.PrePaddingSeconds; + var duration = (timer.EndDate - DateTime.UtcNow).TotalSeconds + timer.PostPaddingSeconds; HttpRequestOptions httpRequestOptions = new HttpRequestOptions() { @@ -451,14 +474,14 @@ namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV recordPath = Path.Combine(recordPath, RecordingHelper.GetRecordingName(timer, info)); Directory.CreateDirectory(Path.GetDirectoryName(recordPath)); - var recording = _recordingProvider.GetAll().FirstOrDefault(x => string.Equals(x.Id, info.Id, StringComparison.OrdinalIgnoreCase)); + var recording = _recordingProvider.GetAll().FirstOrDefault(x => string.Equals(x.ProgramId, info.Id, StringComparison.OrdinalIgnoreCase)); if (recording == null) { recording = new RecordingInfo() { ChannelId = info.ChannelId, - Id = info.Id, + Id = Guid.NewGuid().ToString("N"), StartDate = info.StartDate, EndDate = info.EndDate, Genres = info.Genres ?? null, @@ -480,7 +503,8 @@ namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV OriginalAirDate = info.OriginalAirDate, Status = RecordingStatus.Scheduled, Overview = info.Overview, - SeriesTimerId = info.Id.Substring(0, 10) + SeriesTimerId = info.Id.Substring(0, 10), + TimerId = timer.Id }; _recordingProvider.Add(recording); } diff --git a/MediaBrowser.Server.Implementations/LiveTv/EmbyTV/EntryPoint.cs b/MediaBrowser.Server.Implementations/LiveTv/EmbyTV/EntryPoint.cs new file mode 100644 index 0000000000..713cb9cd30 --- /dev/null +++ b/MediaBrowser.Server.Implementations/LiveTv/EmbyTV/EntryPoint.cs @@ -0,0 +1,16 @@ +using MediaBrowser.Controller.Plugins; + +namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV +{ + public class EntryPoint : IServerEntryPoint + { + public void Run() + { + EmbyTV.Current.Start(); + } + + public void Dispose() + { + } + } +} diff --git a/MediaBrowser.Server.Implementations/LiveTv/EmbyTV/RecordingHelper.cs b/MediaBrowser.Server.Implementations/LiveTv/EmbyTV/RecordingHelper.cs index 0aa1cb2445..db89680d2d 100644 --- a/MediaBrowser.Server.Implementations/LiveTv/EmbyTV/RecordingHelper.cs +++ b/MediaBrowser.Server.Implementations/LiveTv/EmbyTV/RecordingHelper.cs @@ -1,11 +1,10 @@ -using System.Text; -using MediaBrowser.Common.Extensions; +using MediaBrowser.Common.Extensions; using MediaBrowser.Controller.LiveTv; -using MediaBrowser.Model.Extensions; using MediaBrowser.Model.Logging; using System; using System.Collections.Generic; using System.Linq; +using System.Text; namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV { @@ -49,10 +48,6 @@ namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV public static DateTime GetStartTime(TimerInfo timer) { - if (timer.StartDate.AddSeconds(-timer.PrePaddingSeconds + 1) < DateTime.UtcNow) - { - return DateTime.UtcNow.AddSeconds(1); - } return timer.StartDate.AddSeconds(-timer.PrePaddingSeconds); } diff --git a/MediaBrowser.Server.Implementations/LiveTv/EmbyTV/TimerManager.cs b/MediaBrowser.Server.Implementations/LiveTv/EmbyTV/TimerManager.cs index 323197aa5c..0c8d2ca2ba 100644 --- a/MediaBrowser.Server.Implementations/LiveTv/EmbyTV/TimerManager.cs +++ b/MediaBrowser.Server.Implementations/LiveTv/EmbyTV/TimerManager.cs @@ -24,6 +24,11 @@ namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV public void RestartTimers() { StopTimers(); + + foreach (var item in GetAll().ToList()) + { + AddTimer(item); + } } public void StopTimers() @@ -90,7 +95,16 @@ namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV private void AddTimer(TimerInfo item) { - var timespan = RecordingHelper.GetStartTime(item) - DateTime.UtcNow; + var startDate = RecordingHelper.GetStartTime(item); + var now = DateTime.UtcNow; + + if (startDate < now) + { + EventHelper.FireEventIfNotNull(TimerFired, this, new GenericEventArgs { Argument = item }, Logger); + return; + } + + var timespan = startDate - now; var timer = new Timer(TimerCallback, item.Id, timespan, TimeSpan.Zero); diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj index 5e0f374e0b..7eddf5ed15 100644 --- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj +++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj @@ -217,6 +217,7 @@ + diff --git a/MediaBrowser.Server.Implementations/ScheduledTasks/RefreshMediaLibraryTask.cs b/MediaBrowser.Server.Implementations/ScheduledTasks/RefreshMediaLibraryTask.cs index 824d859f30..ed284a90d1 100644 --- a/MediaBrowser.Server.Implementations/ScheduledTasks/RefreshMediaLibraryTask.cs +++ b/MediaBrowser.Server.Implementations/ScheduledTasks/RefreshMediaLibraryTask.cs @@ -37,7 +37,7 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks new StartupTrigger(), - new IntervalTrigger{ Interval = TimeSpan.FromHours(6)} + new IntervalTrigger{ Interval = TimeSpan.FromHours(8)} }; } diff --git a/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj b/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj index 16dd97387f..a4da8c2b77 100644 --- a/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj +++ b/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj @@ -742,9 +742,6 @@ PreserveNewest - - PreserveNewest - PreserveNewest @@ -1006,9 +1003,6 @@ PreserveNewest - - PreserveNewest - PreserveNewest