diff --git a/Emby.Server.Implementations/LiveTv/EmbyTV/DirectRecorder.cs b/Emby.Server.Implementations/LiveTv/EmbyTV/DirectRecorder.cs index ef440899cb..e43e058398 100644 --- a/Emby.Server.Implementations/LiveTv/EmbyTV/DirectRecorder.cs +++ b/Emby.Server.Implementations/LiveTv/EmbyTV/DirectRecorder.cs @@ -6,6 +6,7 @@ using MediaBrowser.Model.IO; using MediaBrowser.Common.IO; using MediaBrowser.Common.Net; using MediaBrowser.Controller.IO; +using MediaBrowser.Controller.Library; using MediaBrowser.Model.Dto; using MediaBrowser.Model.Logging; @@ -29,7 +30,35 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV return targetFile; } - public async Task Record(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken) + public Task Record(IDirectStreamProvider directStreamProvider, MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken) + { + if (directStreamProvider != null) + { + return RecordFromDirectStreamProvider(directStreamProvider, targetFile, duration, onStarted, cancellationToken); + } + + return RecordFromMediaSource(mediaSource, targetFile, duration, onStarted, cancellationToken); + } + + private async Task RecordFromDirectStreamProvider(IDirectStreamProvider directStreamProvider, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken) + { + using (var output = _fileSystem.GetFileStream(targetFile, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read)) + { + onStarted(); + + _logger.Info("Copying recording stream to file {0}", targetFile); + + // The media source if infinite so we need to handle stopping ourselves + var durationToken = new CancellationTokenSource(duration); + cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token).Token; + + await directStreamProvider.CopyToAsync(output, cancellationToken).ConfigureAwait(false); + } + + _logger.Info("Recording completed to file {0}", targetFile); + } + + private async Task RecordFromMediaSource(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken) { var httpRequestOptions = new HttpRequestOptions { diff --git a/Emby.Server.Implementations/LiveTv/EmbyTV/EncodedRecorder.cs b/Emby.Server.Implementations/LiveTv/EmbyTV/EncodedRecorder.cs index 790e6c27d1..2f449bee2b 100644 --- a/Emby.Server.Implementations/LiveTv/EmbyTV/EncodedRecorder.cs +++ b/Emby.Server.Implementations/LiveTv/EmbyTV/EncodedRecorder.cs @@ -21,6 +21,7 @@ using MediaBrowser.Model.LiveTv; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Serialization; using MediaBrowser.Common.Configuration; +using MediaBrowser.Controller.Library; namespace Emby.Server.Implementations.LiveTv.EmbyTV { @@ -64,6 +65,10 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV { return "mkv"; } + if (string.Equals(format, "ts", StringComparison.OrdinalIgnoreCase)) + { + return "ts"; + } return "mp4"; } @@ -90,7 +95,7 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV return Path.ChangeExtension(targetFile, "." + extension); } - public async Task Record(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken) + public async Task Record(IDirectStreamProvider directStreamProvider, MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken) { //var durationToken = new CancellationTokenSource(duration); //cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token).Token; @@ -177,6 +182,8 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV videoArgs = "-codec:v:0 copy"; } + videoArgs += " -fflags +genpts"; + var durationParam = " -t " + _mediaEncoder.GetTimeParameter(duration.Ticks); var flags = new List(); @@ -188,6 +195,10 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV { flags.Add("+ignidx"); } + if (mediaSource.GenPtsInput) + { + flags.Add("+genpts"); + } var inputModifiers = "-async 1 -vsync -1"; diff --git a/Emby.Server.Implementations/LiveTv/EmbyTV/IRecorder.cs b/Emby.Server.Implementations/LiveTv/EmbyTV/IRecorder.cs index 3b5e60c4a7..e639a312c0 100644 --- a/Emby.Server.Implementations/LiveTv/EmbyTV/IRecorder.cs +++ b/Emby.Server.Implementations/LiveTv/EmbyTV/IRecorder.cs @@ -1,6 +1,7 @@ using System; using System.Threading; using System.Threading.Tasks; +using MediaBrowser.Controller.Library; using MediaBrowser.Model.Dto; namespace Emby.Server.Implementations.LiveTv.EmbyTV @@ -10,13 +11,7 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV /// /// Records the specified media source. /// - /// The media source. - /// The target file. - /// The duration. - /// The on started. - /// The cancellation token. - /// Task. - Task Record(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken); + Task Record(IDirectStreamProvider directStreamProvider, MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken); string GetOutputPath(MediaSourceInfo mediaSource, string targetFile); }