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