diff --git a/Jellyfin.Api/Controllers/LiveTvController.cs b/Jellyfin.Api/Controllers/LiveTvController.cs
index 1279d42997..1887dbbc27 100644
--- a/Jellyfin.Api/Controllers/LiveTvController.cs
+++ b/Jellyfin.Api/Controllers/LiveTvController.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
+using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Security.Cryptography;
@@ -15,7 +16,6 @@ using Jellyfin.Data.Enums;
using MediaBrowser.Common;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
-using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
@@ -26,6 +26,7 @@ using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.LiveTv;
+using MediaBrowser.Model.Net;
using MediaBrowser.Model.Querying;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
@@ -43,7 +44,6 @@ namespace Jellyfin.Api.Controllers
private readonly IHttpClient _httpClient;
private readonly ILibraryManager _libraryManager;
private readonly IDtoService _dtoService;
- private readonly IAuthorizationContext _authContext;
private readonly ISessionContext _sessionContext;
private readonly IStreamHelper _streamHelper;
private readonly IMediaSourceManager _mediaSourceManager;
@@ -57,7 +57,6 @@ namespace Jellyfin.Api.Controllers
/// Instance of the interface.
/// Instance of the interface.
/// Instance of the interface.
- /// Instance of the interface.
/// Instance of the interface.
/// Instance of the interface.
/// Instance of the interface.
@@ -68,7 +67,6 @@ namespace Jellyfin.Api.Controllers
IHttpClient httpClient,
ILibraryManager libraryManager,
IDtoService dtoService,
- IAuthorizationContext authContext,
ISessionContext sessionContext,
IStreamHelper streamHelper,
IMediaSourceManager mediaSourceManager,
@@ -79,7 +77,6 @@ namespace Jellyfin.Api.Controllers
_httpClient = httpClient;
_libraryManager = libraryManager;
_dtoService = dtoService;
- _authContext = authContext;
_sessionContext = sessionContext;
_streamHelper = streamHelper;
_mediaSourceManager = mediaSourceManager;
@@ -782,6 +779,7 @@ namespace Jellyfin.Api.Controllers
[HttpPost("Timers/{timerId}")]
[Authorize(Policy = Policies.DefaultAuthorization)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
+ [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "channelId", Justification = "Imported from ServiceStack")]
public async Task UpdateTimer([FromRoute] string timerId, [FromBody] TimerInfoDto timerInfo)
{
AssertUserCanManageLiveTv();
@@ -873,6 +871,7 @@ namespace Jellyfin.Api.Controllers
[HttpPost("SeriesTimers/{timerId}")]
[Authorize(Policy = Policies.DefaultAuthorization)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
+ [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "timerId", Justification = "Imported from ServiceStack")]
public async Task UpdateSeriesTimer([FromRoute] string timerId, [FromBody] SeriesTimerInfoDto seriesTimerInfo)
{
AssertUserCanManageLiveTv();
@@ -979,6 +978,7 @@ namespace Jellyfin.Api.Controllers
[HttpGet("ListingProviders")]
[Authorize(Policy = Policies.DefaultAuthorization)]
[ProducesResponseType(StatusCodes.Status200OK)]
+ [SuppressMessage("Microsoft.Performance", "CA5350:RemoveSha1", MessageId = "AddListingProvider", Justification = "Imported from ServiceStack")]
public async Task> AddListingProvider(
[FromQuery] bool validateLogin,
[FromQuery] bool validateListings,
@@ -1133,6 +1133,60 @@ namespace Jellyfin.Api.Controllers
return await _liveTvManager.DiscoverTuners(newDevicesOnly, CancellationToken.None).ConfigureAwait(false);
}
+ ///
+ /// Gets a live tv recording stream.
+ ///
+ /// Recording id.
+ /// Recording stream returned.
+ /// Recording not found.
+ ///
+ /// An containing the recording stream on success,
+ /// or a if recording not found.
+ ///
+ [HttpGet("LiveRecordings/{recordingId}/stream")]
+ [ProducesResponseType(StatusCodes.Status200OK)]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ public async Task GetLiveRecordingFile([FromRoute] string recordingId)
+ {
+ var path = _liveTvManager.GetEmbyTvActiveRecordingPath(recordingId);
+
+ if (string.IsNullOrWhiteSpace(path))
+ {
+ return NotFound();
+ }
+
+ await using var memoryStream = new MemoryStream();
+ await new ProgressiveFileCopier(_streamHelper, path).WriteToAsync(memoryStream, CancellationToken.None).ConfigureAwait(false);
+ return File(memoryStream, MimeTypes.GetMimeType(path));
+ }
+
+ ///
+ /// Gets a live tv channel stream.
+ ///
+ /// Stream id.
+ /// Container type.
+ /// Stream returned.
+ /// Stream not found.
+ ///
+ /// An containing the channel stream on success,
+ /// or a if stream not found.
+ ///
+ [HttpGet("LiveStreamFiles/{streamId}/stream.{container}")]
+ [ProducesResponseType(StatusCodes.Status200OK)]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ public async Task GetLiveStreamFile([FromRoute] string streamId, [FromRoute] string container)
+ {
+ var liveStreamInfo = await _mediaSourceManager.GetDirectStreamProviderByUniqueId(streamId, CancellationToken.None).ConfigureAwait(false);
+ if (liveStreamInfo == null)
+ {
+ return NotFound();
+ }
+
+ await using var memoryStream = new MemoryStream();
+ await new ProgressiveFileCopier(_streamHelper, liveStreamInfo).WriteToAsync(memoryStream, CancellationToken.None).ConfigureAwait(false);
+ return File(memoryStream, MimeTypes.GetMimeType("file." + container));
+ }
+
private void AssertUserCanManageLiveTv()
{
var user = _sessionContext.GetUser(Request);
diff --git a/Jellyfin.Api/Helpers/ProgressiveFileCopier.cs b/Jellyfin.Api/Helpers/ProgressiveFileCopier.cs
new file mode 100644
index 0000000000..e8e6966f45
--- /dev/null
+++ b/Jellyfin.Api/Helpers/ProgressiveFileCopier.cs
@@ -0,0 +1,84 @@
+using System;
+using System.IO;
+using System.Threading;
+using System.Threading.Tasks;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Model.IO;
+
+namespace Jellyfin.Api.Helpers
+{
+ ///
+ /// Progressive file copier.
+ ///
+ public class ProgressiveFileCopier
+ {
+ private readonly string? _path;
+ private readonly IDirectStreamProvider? _directStreamProvider;
+ private readonly IStreamHelper _streamHelper;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// Instance of the interface.
+ /// Filepath to stream from.
+ public ProgressiveFileCopier(IStreamHelper streamHelper, string path)
+ {
+ _path = path;
+ _streamHelper = streamHelper;
+ _directStreamProvider = null;
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// Instance of the interface.
+ /// Instance of the interface.
+ public ProgressiveFileCopier(IStreamHelper streamHelper, IDirectStreamProvider directStreamProvider)
+ {
+ _directStreamProvider = directStreamProvider;
+ _streamHelper = streamHelper;
+ _path = null;
+ }
+
+ ///
+ /// Write source stream to output.
+ ///
+ /// Output stream.
+ /// Cancellation token.
+ /// A .
+ public async Task WriteToAsync(Stream outputStream, CancellationToken cancellationToken)
+ {
+ if (_directStreamProvider != null)
+ {
+ await _directStreamProvider.CopyToAsync(outputStream, cancellationToken).ConfigureAwait(false);
+ return;
+ }
+
+ var fileOptions = FileOptions.SequentialScan;
+
+ // use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
+ if (Environment.OSVersion.Platform != PlatformID.Win32NT)
+ {
+ fileOptions |= FileOptions.Asynchronous;
+ }
+
+ await using var inputStream = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, fileOptions);
+ const int emptyReadLimit = 100;
+ var eofCount = 0;
+ while (eofCount < emptyReadLimit)
+ {
+ var bytesRead = await _streamHelper.CopyToAsync(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
+
+ if (bytesRead == 0)
+ {
+ eofCount++;
+ await Task.Delay(100, cancellationToken).ConfigureAwait(false);
+ }
+ else
+ {
+ eofCount = 0;
+ }
+ }
+ }
+ }
+}
diff --git a/Jellyfin.Api/Models/LiveTvDtos/ChannelMappingOptionsDto.cs b/Jellyfin.Api/Models/LiveTvDtos/ChannelMappingOptionsDto.cs
index 642b40e4d2..970d8acdbc 100644
--- a/Jellyfin.Api/Models/LiveTvDtos/ChannelMappingOptionsDto.cs
+++ b/Jellyfin.Api/Models/LiveTvDtos/ChannelMappingOptionsDto.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Model.Dto;
@@ -12,17 +13,20 @@ namespace Jellyfin.Api.Models.LiveTvDtos
///
/// Gets or sets list of tuner channels.
///
- public List TunerChannels { get; set; }
+ [SuppressMessage("Microsoft.Performance", "CA2227:ReadOnlyRemoveSetter", MessageId = "TunerChannels", Justification = "Imported from ServiceStack")]
+ public List TunerChannels { get; set; } = null!;
///
/// Gets or sets list of provider channels.
///
- public List ProviderChannels { get; set; }
+ [SuppressMessage("Microsoft.Performance", "CA2227:ReadOnlyRemoveSetter", MessageId = "ProviderChannels", Justification = "Imported from ServiceStack")]
+ public List ProviderChannels { get; set; } = null!;
///
/// Gets or sets list of mappings.
///
- public NameValuePair[] Mappings { get; set; }
+ [SuppressMessage("Microsoft.Performance", "CA1819:DontReturnArrays", MessageId = "Mappings", Justification = "Imported from ServiceStack")]
+ public NameValuePair[] Mappings { get; set; } = null!;
///
/// Gets or sets provider name.
diff --git a/MediaBrowser.Api/LiveTv/LiveTvService.cs b/MediaBrowser.Api/LiveTv/LiveTvService.cs
deleted file mode 100644
index 14abdcc998..0000000000
--- a/MediaBrowser.Api/LiveTv/LiveTvService.cs
+++ /dev/null
@@ -1,487 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Globalization;
-using System.IO;
-using System.Linq;
-using System.Security.Cryptography;
-using System.Text;
-using System.Threading;
-using System.Threading.Tasks;
-using Jellyfin.Data.Enums;
-using MediaBrowser.Api.UserLibrary;
-using MediaBrowser.Common;
-using MediaBrowser.Common.Configuration;
-using MediaBrowser.Common.Net;
-using MediaBrowser.Controller.Configuration;
-using MediaBrowser.Controller.Dto;
-using MediaBrowser.Controller.Entities;
-using MediaBrowser.Controller.Entities.TV;
-using MediaBrowser.Controller.Library;
-using MediaBrowser.Controller.LiveTv;
-using MediaBrowser.Controller.Net;
-using MediaBrowser.Model.Dto;
-using MediaBrowser.Model.Entities;
-using MediaBrowser.Model.IO;
-using MediaBrowser.Model.LiveTv;
-using MediaBrowser.Model.Querying;
-using MediaBrowser.Model.Services;
-using Microsoft.Extensions.Logging;
-using Microsoft.Net.Http.Headers;
-
-namespace MediaBrowser.Api.LiveTv
-{
- [Route("/LiveTv/LiveStreamFiles/{Id}/stream.{Container}", "GET", Summary = "Gets a live tv channel")]
- public class GetLiveStreamFile
- {
- public string Id { get; set; }
- public string Container { get; set; }
- }
-
- [Route("/LiveTv/LiveRecordings/{Id}/stream", "GET", Summary = "Gets a live tv channel")]
- public class GetLiveRecordingFile
- {
- public string Id { get; set; }
- }
-
- public class LiveTvService : BaseApiService
- {
- private readonly ILiveTvManager _liveTvManager;
- private readonly IUserManager _userManager;
- private readonly IHttpClient _httpClient;
- private readonly ILibraryManager _libraryManager;
- private readonly IDtoService _dtoService;
- private readonly IAuthorizationContext _authContext;
- private readonly ISessionContext _sessionContext;
- private readonly IStreamHelper _streamHelper;
- private readonly IMediaSourceManager _mediaSourceManager;
-
- public LiveTvService(
- ILogger logger,
- IServerConfigurationManager serverConfigurationManager,
- IHttpResultFactory httpResultFactory,
- IMediaSourceManager mediaSourceManager,
- IStreamHelper streamHelper,
- ILiveTvManager liveTvManager,
- IUserManager userManager,
- IHttpClient httpClient,
- ILibraryManager libraryManager,
- IDtoService dtoService,
- IAuthorizationContext authContext,
- ISessionContext sessionContext)
- : base(logger, serverConfigurationManager, httpResultFactory)
- {
- _mediaSourceManager = mediaSourceManager;
- _streamHelper = streamHelper;
- _liveTvManager = liveTvManager;
- _userManager = userManager;
- _httpClient = httpClient;
- _libraryManager = libraryManager;
- _dtoService = dtoService;
- _authContext = authContext;
- _sessionContext = sessionContext;
- }
-
- public object Get(GetTunerHostTypes request)
- {
- var list = _liveTvManager.GetTunerHostTypes();
- return ToOptimizedResult(list);
- }
-
- public object Get(GetLiveRecordingFile request)
- {
- var path = _liveTvManager.GetEmbyTvActiveRecordingPath(request.Id);
-
- if (string.IsNullOrWhiteSpace(path))
- {
- throw new FileNotFoundException();
- }
-
- var outputHeaders = new Dictionary(StringComparer.OrdinalIgnoreCase)
- {
- [HeaderNames.ContentType] = Model.Net.MimeTypes.GetMimeType(path)
- };
-
- return new ProgressiveFileCopier(_streamHelper, path, outputHeaders, Logger)
- {
- AllowEndOfFile = false
- };
- }
-
- public async Task