DirectRecorder.cs 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.IO;
  5. using System.Net.Http;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  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));
  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))
  43. {
  44. onStarted();
  45. _logger.LogInformation("Copying recording stream to file {0}", 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. await directStreamProvider.CopyToAsync(output, cancellationTokenSource.Token).ConfigureAwait(false);
  50. }
  51. _logger.LogInformation("Recording completed to file {0}", targetFile);
  52. }
  53. private async Task RecordFromMediaSource(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
  54. {
  55. using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
  56. .GetAsync(mediaSource.Path, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
  57. _logger.LogInformation("Opened recording stream from tuner provider");
  58. Directory.CreateDirectory(Path.GetDirectoryName(targetFile));
  59. // use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
  60. await using var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.None);
  61. onStarted();
  62. _logger.LogInformation("Copying recording stream to file {0}", targetFile);
  63. // The media source if infinite so we need to handle stopping ourselves
  64. using var durationToken = new CancellationTokenSource(duration);
  65. using var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token);
  66. cancellationToken = linkedCancellationToken.Token;
  67. await _streamHelper.CopyUntilCancelled(
  68. await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false),
  69. output,
  70. IODefaults.CopyToBufferSize,
  71. cancellationToken).ConfigureAwait(false);
  72. _logger.LogInformation("Recording completed to file {0}", targetFile);
  73. }
  74. }
  75. }