mirror of https://github.com/ErsatzTV/ErsatzTV.git
17 changed files with 212 additions and 174 deletions
@ -1,41 +1,22 @@
@@ -1,41 +1,22 @@
|
||||
using ErsatzTV.Core; |
||||
using ErsatzTV.Core.Domain; |
||||
using ErsatzTV.Core.Interfaces.Scheduling; |
||||
using ErsatzTV.Infrastructure.Data; |
||||
using ErsatzTV.Infrastructure.Extensions; |
||||
using Microsoft.EntityFrameworkCore; |
||||
|
||||
namespace ErsatzTV.Application.Playouts; |
||||
|
||||
public class TimeShiftOnDemandPlayoutHandler( |
||||
IPlayoutTimeShifter playoutTimeShifter, |
||||
IDbContextFactory<TvContext> dbContextFactory) |
||||
public class TimeShiftOnDemandPlayoutHandler(IPlayoutTimeShifter playoutTimeShifter) |
||||
: IRequestHandler<TimeShiftOnDemandPlayout, Option<BaseError>> |
||||
{ |
||||
public async Task<Option<BaseError>> Handle(TimeShiftOnDemandPlayout request, CancellationToken cancellationToken) |
||||
{ |
||||
try |
||||
{ |
||||
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); |
||||
|
||||
Option<Playout> maybePlayout = await dbContext.Playouts |
||||
.Include(p => p.Channel) |
||||
.Include(p => p.Items) |
||||
.Include(p => p.Anchor) |
||||
.Include(p => p.ProgramScheduleAnchors) |
||||
.SelectOneAsync(p => p.Channel.Number, p => p.Channel.Number == request.ChannelNumber); |
||||
|
||||
foreach (Playout playout in maybePlayout) |
||||
{ |
||||
playoutTimeShifter.TimeShift(playout, request.Now, request.Force); |
||||
await dbContext.SaveChangesAsync(cancellationToken); |
||||
} |
||||
|
||||
return Option<BaseError>.None; |
||||
await playoutTimeShifter.TimeShift(request.PlayoutId, request.Now, request.Force); |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
return BaseError.New(ex.Message); |
||||
} |
||||
|
||||
return Option<BaseError>.None; |
||||
} |
||||
} |
||||
|
@ -1,8 +1,6 @@
@@ -1,8 +1,6 @@
|
||||
using ErsatzTV.Core.Domain; |
||||
|
||||
namespace ErsatzTV.Core.Interfaces.Scheduling; |
||||
|
||||
public interface IPlayoutTimeShifter |
||||
{ |
||||
public void TimeShift(Playout playout, DateTimeOffset now, bool force); |
||||
public Task TimeShift(int playoutId, DateTimeOffset now, bool force); |
||||
} |
||||
|
@ -1,90 +0,0 @@
@@ -1,90 +0,0 @@
|
||||
using ErsatzTV.Core.Domain; |
||||
using ErsatzTV.Core.Interfaces.FFmpeg; |
||||
using ErsatzTV.Core.Interfaces.Scheduling; |
||||
using Microsoft.Extensions.Logging; |
||||
|
||||
namespace ErsatzTV.Core.Scheduling; |
||||
|
||||
public class PlayoutTimeShifter(IFFmpegSegmenterService segmenterService, ILogger<PlayoutTimeShifter> logger) |
||||
: IPlayoutTimeShifter |
||||
{ |
||||
public void TimeShift(Playout playout, DateTimeOffset now, bool force) |
||||
{ |
||||
if (playout.Channel.PlayoutMode is not ChannelPlayoutMode.OnDemand) |
||||
{ |
||||
return; |
||||
} |
||||
|
||||
if (!force && segmenterService.IsActive(playout.Channel.Number)) |
||||
{ |
||||
logger.LogDebug( |
||||
"Will not time shift on demand playout that is active for channel {Number} - {Name}", |
||||
playout.Channel.Number, |
||||
playout.Channel.Name); |
||||
|
||||
return; |
||||
} |
||||
|
||||
if (playout.Items.Count == 0) |
||||
{ |
||||
logger.LogDebug( |
||||
"Unable to time shift empty playout for channel {Number} - {Name}", |
||||
playout.Channel.Number, |
||||
playout.Channel.Name); |
||||
|
||||
return; |
||||
} |
||||
|
||||
if (playout.OnDemandCheckpoint is null) |
||||
{ |
||||
logger.LogDebug( |
||||
"Time shifting unwatched playout for channel {Number} - {Name}", |
||||
playout.Channel.Number, |
||||
playout.Channel.Name); |
||||
|
||||
playout.OnDemandCheckpoint = playout.Items.Min(p => p.StartOffset); |
||||
} |
||||
|
||||
TimeSpan toOffset = now - playout.OnDemandCheckpoint.IfNone(now); |
||||
|
||||
logger.LogDebug( |
||||
"Time shifting playout for channel {Number} - {Name} forward by {Time}", |
||||
playout.Channel.Number, |
||||
playout.Channel.Name, |
||||
toOffset); |
||||
|
||||
// time shift items
|
||||
foreach (PlayoutItem playoutItem in playout.Items) |
||||
{ |
||||
playoutItem.Start += toOffset; |
||||
playoutItem.Finish += toOffset; |
||||
|
||||
if (playoutItem.GuideStart.HasValue) |
||||
{ |
||||
playoutItem.GuideStart += toOffset; |
||||
} |
||||
|
||||
if (playoutItem.GuideFinish.HasValue) |
||||
{ |
||||
playoutItem.GuideFinish += toOffset; |
||||
} |
||||
} |
||||
|
||||
// time shift anchors
|
||||
foreach (PlayoutProgramScheduleAnchor anchor in playout.ProgramScheduleAnchors) |
||||
{ |
||||
if (anchor.AnchorDate.HasValue) |
||||
{ |
||||
anchor.AnchorDate += toOffset; |
||||
} |
||||
} |
||||
|
||||
// time shift anchor
|
||||
if (playout.Anchor is not null) |
||||
{ |
||||
playout.Anchor.NextStart += toOffset; |
||||
} |
||||
|
||||
playout.OnDemandCheckpoint = now; |
||||
} |
||||
} |
@ -0,0 +1,128 @@
@@ -0,0 +1,128 @@
|
||||
using ErsatzTV.Core.Domain; |
||||
using ErsatzTV.Core.Domain.Scheduling; |
||||
using ErsatzTV.Core.Interfaces.FFmpeg; |
||||
using ErsatzTV.Core.Interfaces.Scheduling; |
||||
using ErsatzTV.Infrastructure.Data; |
||||
using ErsatzTV.Infrastructure.Extensions; |
||||
using Microsoft.EntityFrameworkCore; |
||||
using Microsoft.Extensions.Logging; |
||||
|
||||
namespace ErsatzTV.Infrastructure.Scheduling; |
||||
|
||||
public class PlayoutTimeShifter( |
||||
IDbContextFactory<TvContext> dbContextFactory, |
||||
IFFmpegSegmenterService segmenterService, |
||||
ILogger<PlayoutTimeShifter> logger) |
||||
: IPlayoutTimeShifter |
||||
{ |
||||
public async Task TimeShift(int playoutId, DateTimeOffset now, bool force) |
||||
{ |
||||
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(); |
||||
|
||||
Option<Channel> maybeChannel = await dbContext.Playouts |
||||
.AsNoTracking() |
||||
.Include(p => p.Channel) |
||||
.SelectOneAsync(p => p.Id, p => p.Id == playoutId) |
||||
.MapT(p => p.Channel); |
||||
|
||||
foreach (var channel in maybeChannel.Where(c => c.PlayoutMode is ChannelPlayoutMode.OnDemand)) |
||||
{ |
||||
Option<Playout> maybePlayout = await dbContext.Playouts |
||||
.Include(p => p.Channel) |
||||
.Include(p => p.Items) |
||||
.Include(p => p.Anchor) |
||||
.Include(p => p.ProgramScheduleAnchors) |
||||
.Include(p => p.PlayoutHistory) |
||||
.SelectOneAsync(p => p.ChannelId, p => p.ChannelId == channel.Id); |
||||
|
||||
foreach (var playout in maybePlayout) |
||||
{ |
||||
if (playout.Channel.PlayoutMode is not ChannelPlayoutMode.OnDemand) |
||||
{ |
||||
return; |
||||
} |
||||
|
||||
if (!force && segmenterService.IsActive(playout.Channel.Number)) |
||||
{ |
||||
logger.LogDebug( |
||||
"Will not time shift on demand playout that is active for channel {Number} - {Name}", |
||||
playout.Channel.Number, |
||||
playout.Channel.Name); |
||||
|
||||
return; |
||||
} |
||||
|
||||
if (playout.Items.Count == 0) |
||||
{ |
||||
logger.LogDebug( |
||||
"Unable to time shift empty playout for channel {Number} - {Name}", |
||||
playout.Channel.Number, |
||||
playout.Channel.Name); |
||||
|
||||
return; |
||||
} |
||||
|
||||
if (playout.OnDemandCheckpoint is null) |
||||
{ |
||||
logger.LogDebug( |
||||
"Time shifting unwatched playout for channel {Number} - {Name}", |
||||
playout.Channel.Number, |
||||
playout.Channel.Name); |
||||
|
||||
playout.OnDemandCheckpoint = playout.Items.Min(p => p.StartOffset); |
||||
} |
||||
|
||||
TimeSpan toOffset = now - playout.OnDemandCheckpoint.IfNone(now); |
||||
|
||||
logger.LogDebug( |
||||
"Time shifting playout for channel {Number} - {Name} forward by {Time}", |
||||
playout.Channel.Number, |
||||
playout.Channel.Name, |
||||
toOffset); |
||||
|
||||
// time shift history
|
||||
foreach (PlayoutHistory history in playout.PlayoutHistory) |
||||
{ |
||||
history.When += toOffset; |
||||
history.Finish += toOffset; |
||||
} |
||||
|
||||
// time shift items
|
||||
foreach (PlayoutItem playoutItem in playout.Items) |
||||
{ |
||||
playoutItem.Start += toOffset; |
||||
playoutItem.Finish += toOffset; |
||||
|
||||
if (playoutItem.GuideStart.HasValue) |
||||
{ |
||||
playoutItem.GuideStart += toOffset; |
||||
} |
||||
|
||||
if (playoutItem.GuideFinish.HasValue) |
||||
{ |
||||
playoutItem.GuideFinish += toOffset; |
||||
} |
||||
} |
||||
|
||||
// time shift anchors
|
||||
foreach (PlayoutProgramScheduleAnchor anchor in playout.ProgramScheduleAnchors) |
||||
{ |
||||
if (anchor.AnchorDate.HasValue) |
||||
{ |
||||
anchor.AnchorDate += toOffset; |
||||
} |
||||
} |
||||
|
||||
// time shift anchor
|
||||
if (playout.Anchor is not null) |
||||
{ |
||||
playout.Anchor.NextStart += toOffset; |
||||
} |
||||
|
||||
playout.OnDemandCheckpoint = now; |
||||
|
||||
await dbContext.SaveChangesAsync(); |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue