private void AddTimer(TimerInfo item) { var timespan = RecordingHelper.GetStartTime(item) - DateTime.UtcNow; var timer = new Timer(TimerCallback, item.Id, timespan, TimeSpan.Zero); if (!_timers.TryAdd(item.Id, timer)) { timer.Dispose(); } }
public override void Update(TimerInfo item) { base.Update(item); Timer timer; if (_timers.TryGetValue(item.Id, out timer)) { var timespan = RecordingHelper.GetStartTime(item) - DateTime.UtcNow; timer.Change(timespan, TimeSpan.Zero); } else { AddTimer(item); } }
private async Task RecordStream(TimerInfo timer, CancellationToken cancellationToken) { var mediaStreamInfo = await GetChannelStream(timer.ChannelId, "none", CancellationToken.None); var duration = (timer.EndDate - RecordingHelper.GetStartTime(timer)).TotalSeconds + timer.PrePaddingSeconds; HttpRequestOptions httpRequestOptions = new HttpRequestOptionsMod() { Url = mediaStreamInfo.Path + "?duration=" + duration }; var info = GetProgramInfoFromCache(timer.ChannelId, timer.ProgramId); var recordPath = RecordingPath; if (info.IsMovie) { recordPath = Path.Combine(recordPath, "Movies", StringHelper.RemoveSpecialCharacters(info.Name)); } else { recordPath = Path.Combine(recordPath, "TV", StringHelper.RemoveSpecialCharacters(info.Name)); } 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)); if (recording == null) { recording = new RecordingInfo() { ChannelId = info.ChannelId, Id = info.Id, StartDate = info.StartDate, EndDate = info.EndDate, Genres = info.Genres ?? null, IsKids = info.IsKids, IsLive = info.IsLive, IsMovie = info.IsMovie, IsHD = info.IsHD, IsNews = info.IsNews, IsPremiere = info.IsPremiere, IsSeries = info.IsSeries, IsSports = info.IsSports, IsRepeat = !info.IsPremiere, Name = info.Name, EpisodeTitle = info.EpisodeTitle ?? "", ProgramId = info.Id, HasImage = info.HasImage ?? false, ImagePath = info.ImagePath ?? null, ImageUrl = info.ImageUrl, OriginalAirDate = info.OriginalAirDate, Status = RecordingStatus.Scheduled, Overview = info.Overview, SeriesTimerId = info.Id.Substring(0, 10) }; _recordingProvider.Add(recording); } recording.Path = recordPath; recording.Status = RecordingStatus.InProgress; _recordingProvider.Update(recording); try { httpRequestOptions.BufferContent = false; httpRequestOptions.CancellationToken = cancellationToken; _logger.Info("Writing file to path: " + recordPath); using (var response = await _httpClient.SendAsync(httpRequestOptions, "GET")) { using (var output = File.Open(recordPath, FileMode.Create, FileAccess.Write, FileShare.Read)) { await response.Content.CopyToAsync(output, 4096, cancellationToken); } } recording.Status = RecordingStatus.Completed; } catch (OperationCanceledException) { recording.Status = RecordingStatus.Cancelled; } catch { recording.Status = RecordingStatus.Error; } _recordingProvider.Update(recording); _timerProvider.Delete(timer); _logger.Info("Recording was a success"); }