DirectRecorder.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 sealed 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. var output = new FileStream(
  42. targetFile,
  43. FileMode.CreateNew,
  44. FileAccess.Write,
  45. FileShare.Read,
  46. IODefaults.FileStreamBufferSize,
  47. FileOptions.Asynchronous);
  48. await using (output.ConfigureAwait(false))
  49. {
  50. onStarted();
  51. _logger.LogInformation("Copying recording to file {FilePath}", targetFile);
  52. // The media source is infinite so we need to handle stopping ourselves
  53. using var durationToken = new CancellationTokenSource(duration);
  54. using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token);
  55. var linkedCancellationToken = cancellationTokenSource.Token;
  56. var fileStream = new ProgressiveFileStream(directStreamProvider.GetStream());
  57. await using (fileStream.ConfigureAwait(false))
  58. {
  59. await _streamHelper.CopyToAsync(
  60. fileStream,
  61. output,
  62. IODefaults.CopyToBufferSize,
  63. 1000,
  64. linkedCancellationToken).ConfigureAwait(false);
  65. }
  66. }
  67. _logger.LogInformation("Recording completed: {FilePath}", targetFile);
  68. }
  69. private async Task RecordFromMediaSource(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
  70. {
  71. using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
  72. .GetAsync(mediaSource.Path, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
  73. _logger.LogInformation("Opened recording stream from tuner provider");
  74. Directory.CreateDirectory(Path.GetDirectoryName(targetFile) ?? throw new ArgumentException("Path can't be a root directory.", nameof(targetFile)));
  75. var output = new FileStream(targetFile, FileMode.CreateNew, FileAccess.Write, FileShare.Read, IODefaults.CopyToBufferSize, FileOptions.Asynchronous);
  76. await using (output.ConfigureAwait(false))
  77. {
  78. onStarted();
  79. _logger.LogInformation("Copying recording stream to file {0}", targetFile);
  80. // The media source if infinite so we need to handle stopping ourselves
  81. using var durationToken = new CancellationTokenSource(duration);
  82. using var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token);
  83. cancellationToken = linkedCancellationToken.Token;
  84. await _streamHelper.CopyUntilCancelled(
  85. await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false),
  86. output,
  87. IODefaults.CopyToBufferSize,
  88. cancellationToken).ConfigureAwait(false);
  89. _logger.LogInformation("Recording completed to file {0}", targetFile);
  90. }
  91. }
  92. /// <inheritdoc />
  93. public void Dispose()
  94. {
  95. }
  96. }
  97. }