mirror of https://github.com/ErsatzTV/ErsatzTV.git
Browse Source
* update dependencies * add channel progress mode * implement on demand channel progress * update changelogpull/1791/head
51 changed files with 23439 additions and 67 deletions
@ -0,0 +1,6 @@
@@ -0,0 +1,6 @@
|
||||
using ErsatzTV.Core; |
||||
|
||||
namespace ErsatzTV.Application.Playouts; |
||||
|
||||
public record TimeShiftOnDemandPlayout(string ChannelNumber, DateTimeOffset Now, bool Force) |
||||
: IRequest<Option<BaseError>>, IBackgroundServiceRequest; |
@ -0,0 +1,41 @@
@@ -0,0 +1,41 @@
|
||||
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) |
||||
: 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; |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
return BaseError.New(ex.Message); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,4 @@
@@ -0,0 +1,4 @@
|
||||
namespace ErsatzTV.Application.Playouts; |
||||
|
||||
public record UpdateOnDemandCheckpoint(string ChannelNumber, DateTimeOffset Checkpoint) |
||||
: IRequest; |
@ -0,0 +1,62 @@
@@ -0,0 +1,62 @@
|
||||
using ErsatzTV.Core.Domain; |
||||
using ErsatzTV.Core.Interfaces.Repositories; |
||||
using ErsatzTV.Infrastructure.Data; |
||||
using ErsatzTV.Infrastructure.Extensions; |
||||
using Microsoft.EntityFrameworkCore; |
||||
using Microsoft.Extensions.Logging; |
||||
|
||||
namespace ErsatzTV.Application.Playouts; |
||||
|
||||
public class UpdateOnDemandCheckpointHandler( |
||||
IDbContextFactory<TvContext> dbContextFactory, |
||||
IConfigElementRepository configElementRepository, |
||||
ILogger<UpdateOnDemandCheckpointHandler> logger) |
||||
: IRequestHandler<UpdateOnDemandCheckpoint> |
||||
{ |
||||
public async Task Handle(UpdateOnDemandCheckpoint request, CancellationToken cancellationToken) |
||||
{ |
||||
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); |
||||
|
||||
Option<Playout> maybePlayout = await dbContext.Playouts |
||||
.Include(p => p.Channel) |
||||
.Include(p => p.Items) |
||||
.SelectOneAsync(p => p.Channel.Number, p => p.Channel.Number == request.ChannelNumber); |
||||
|
||||
foreach (Playout playout in maybePlayout) |
||||
{ |
||||
if (playout.Channel.ProgressMode is not ChannelProgressMode.OnDemand) |
||||
{ |
||||
return; |
||||
} |
||||
|
||||
int timeout = await (await configElementRepository.GetValue<int>(ConfigElementKey.FFmpegSegmenterTimeout)) |
||||
.IfNoneAsync(60); |
||||
|
||||
// don't move checkpoint back in time
|
||||
DateTimeOffset newCheckpoint = request.Checkpoint - TimeSpan.FromSeconds(timeout); |
||||
if (newCheckpoint > playout.OnDemandCheckpoint) |
||||
{ |
||||
playout.OnDemandCheckpoint = newCheckpoint; |
||||
} |
||||
|
||||
// don't checkpoint before the first item
|
||||
// this could happen if you watch a new playout for less time than the segmenter timeout
|
||||
if (playout.Items.Count > 0) |
||||
{ |
||||
DateTimeOffset minStart = playout.Items.Min(p => p.StartOffset); |
||||
if (playout.OnDemandCheckpoint < minStart) |
||||
{ |
||||
playout.OnDemandCheckpoint = minStart; |
||||
} |
||||
} |
||||
|
||||
logger.LogDebug( |
||||
"Updating on demand checkpoint for channel {Number} - {Name} to {Checkpoint}", |
||||
playout.Channel.Number, |
||||
playout.Channel.Name, |
||||
playout.OnDemandCheckpoint); |
||||
|
||||
await dbContext.SaveChangesAsync(cancellationToken); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,7 @@
@@ -0,0 +1,7 @@
|
||||
namespace ErsatzTV.Core.Domain; |
||||
|
||||
public enum ChannelProgressMode |
||||
{ |
||||
Always = 0, |
||||
OnDemand = 1 |
||||
} |
@ -0,0 +1,8 @@
@@ -0,0 +1,8 @@
|
||||
using ErsatzTV.Core.Domain; |
||||
|
||||
namespace ErsatzTV.Core.Interfaces.Scheduling; |
||||
|
||||
public interface IPlayoutTimeShifter |
||||
{ |
||||
public void TimeShift(Playout playout, DateTimeOffset now, bool force); |
||||
} |
@ -0,0 +1,90 @@
@@ -0,0 +1,90 @@
|
||||
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.ProgressMode is not ChannelProgressMode.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; |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,29 @@
@@ -0,0 +1,29 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations; |
||||
|
||||
#nullable disable |
||||
|
||||
namespace ErsatzTV.Infrastructure.MySql.Migrations |
||||
{ |
||||
/// <inheritdoc />
|
||||
public partial class Add_Channel_ProgressMode : Migration |
||||
{ |
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder) |
||||
{ |
||||
migrationBuilder.AddColumn<int>( |
||||
name: "ProgressMode", |
||||
table: "Channel", |
||||
type: "int", |
||||
nullable: false, |
||||
defaultValue: 0); |
||||
} |
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder) |
||||
{ |
||||
migrationBuilder.DropColumn( |
||||
name: "ProgressMode", |
||||
table: "Channel"); |
||||
} |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,29 @@
@@ -0,0 +1,29 @@
|
||||
using System; |
||||
using Microsoft.EntityFrameworkCore.Migrations; |
||||
|
||||
#nullable disable |
||||
|
||||
namespace ErsatzTV.Infrastructure.MySql.Migrations |
||||
{ |
||||
/// <inheritdoc />
|
||||
public partial class Add_Playout_OnDemandCheckpoint : Migration |
||||
{ |
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder) |
||||
{ |
||||
migrationBuilder.AddColumn<DateTimeOffset>( |
||||
name: "OnDemandCheckpoint", |
||||
table: "Playout", |
||||
type: "datetime(6)", |
||||
nullable: true); |
||||
} |
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder) |
||||
{ |
||||
migrationBuilder.DropColumn( |
||||
name: "OnDemandCheckpoint", |
||||
table: "Playout"); |
||||
} |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,29 @@
@@ -0,0 +1,29 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations; |
||||
|
||||
#nullable disable |
||||
|
||||
namespace ErsatzTV.Infrastructure.Sqlite.Migrations |
||||
{ |
||||
/// <inheritdoc />
|
||||
public partial class Add_Channel_ProgressMode : Migration |
||||
{ |
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder) |
||||
{ |
||||
migrationBuilder.AddColumn<int>( |
||||
name: "ProgressMode", |
||||
table: "Channel", |
||||
type: "INTEGER", |
||||
nullable: false, |
||||
defaultValue: 0); |
||||
} |
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder) |
||||
{ |
||||
migrationBuilder.DropColumn( |
||||
name: "ProgressMode", |
||||
table: "Channel"); |
||||
} |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,29 @@
@@ -0,0 +1,29 @@
|
||||
using System; |
||||
using Microsoft.EntityFrameworkCore.Migrations; |
||||
|
||||
#nullable disable |
||||
|
||||
namespace ErsatzTV.Infrastructure.Sqlite.Migrations |
||||
{ |
||||
/// <inheritdoc />
|
||||
public partial class Add_Playout_OnDemandCheckpoint : Migration |
||||
{ |
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder) |
||||
{ |
||||
migrationBuilder.AddColumn<DateTimeOffset>( |
||||
name: "OnDemandCheckpoint", |
||||
table: "Playout", |
||||
type: "TEXT", |
||||
nullable: true); |
||||
} |
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder) |
||||
{ |
||||
migrationBuilder.DropColumn( |
||||
name: "OnDemandCheckpoint", |
||||
table: "Playout"); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue