DirectRecorder.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.IO;
  4. using System.Net.Http;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Jellyfin.Api.Helpers;
  8. using MediaBrowser.Common.Net;
  9. using MediaBrowser.Controller.Library;
  10. using MediaBrowser.Model.Dto;
  11. using MediaBrowser.Model.IO;
  12. using Microsoft.Extensions.Logging;
  13. namespace Emby.Server.Implementations.LiveTv.EmbyTV
  14. {
  15. public class DirectRecorder : IRecorder
  16. {
  17. private readonly ILogger _logger;
  18. private readonly IHttpClientFactory _httpClientFactory;
  19. private readonly IStreamHelper _streamHelper;
  20. public DirectRecorder(ILogger logger, IHttpClientFactory httpClientFactory, IStreamHelper streamHelper)
  21. {
  22. _logger = logger;
  23. _httpClientFactory = httpClientFactory;
  24. _streamHelper = streamHelper;
  25. }
  26. public string GetOutputPath(MediaSourceInfo mediaSource, string targetFile)
  27. {
  28. return targetFile;
  29. }
  30. public Task Record(IDirectStreamProvider? directStreamProvider, MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
  31. {
  32. if (directStreamProvider is not null)
  33. {
  34. return RecordFromDirectStreamProvider(directStreamProvider, targetFile, duration, onStarted, cancellationToken);
  35. }
  36. return RecordFromMediaSource(mediaSource, targetFile, duration, onStarted, cancellationToken);
  37. }
  38. private async Task RecordFromDirectStreamProvider(IDirectStreamProvider directStreamProvider, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
  39. {
  40. Directory.CreateDirectory(Path.GetDirectoryName(targetFile) ?? throw new ArgumentException("Path can't be a root directory.", nameof(targetFile)));
  41. await using (var output = new FileStream(targetFile, FileMode.CreateNew, FileAccess.Write, FileShare.Read, IODefaults.FileStreamBufferSize, FileOptions.Asynchronous))
  42. {
  43. onStarted();
  44. _logger.LogInformation("Copying recording to file {FilePath}", targetFile);
  45. // The media source is infinite so we need to handle stopping ourselves
  46. using var durationToken = new CancellationTokenSource(duration);
  47. using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token);
  48. var linkedCancellationToken = cancellationTokenSource.Token;
  49. var fileStream = new ProgressiveFileStream(directStreamProvider.GetStream());
  50. await using (fileStream.ConfigureAwait(false))
  51. {
  52. await _streamHelper.CopyToAsync(
  53. fileStream,
  54. output,
  55. IODefaults.CopyToBufferSize,
  56. 1000,
  57. linkedCancellationToken).ConfigureAwait(false);
  58. }
  59. }
  60. _logger.LogInformation("Recording completed: {FilePath}", targetFile);
  61. }
  62. private async Task RecordFromMediaSource(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
  63. {
  64. using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
  65. .GetAsync(mediaSource.Path, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
  66. _logger.LogInformation("Opened recording stream from tuner provider");
  67. Directory.CreateDirectory(Path.GetDirectoryName(targetFile) ?? throw new ArgumentException("Path can't be a root directory.", nameof(targetFile)));
  68. await using var output = new FileStream(targetFile, FileMode.CreateNew, FileAccess.Write, FileShare.Read, IODefaults.CopyToBufferSize, FileOptions.Asynchronous);
  69. onStarted();
  70. _logger.LogInformation("Copying recording stream to file {0}", targetFile);
  71. // The media source if infinite so we need to handle stopping ourselves
  72. using var durationToken = new CancellationTokenSource(duration);
  73. using var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token);
  74. cancellationToken = linkedCancellationToken.Token;
  75. await _streamHelper.CopyUntilCancelled(
  76. await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false),
  77. output,
  78. IODefaults.CopyToBufferSize,
  79. cancellationToken).ConfigureAwait(false);
  80. _logger.LogInformation("Recording completed to file {0}", targetFile);
  81. }
  82. }
  83. }