DirectRecorder.cs 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 MediaBrowser.Common.Net;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Model.Dto;
  10. using MediaBrowser.Model.IO;
  11. using Microsoft.Extensions.Logging;
  12. namespace Emby.Server.Implementations.LiveTv.EmbyTV
  13. {
  14. public class DirectRecorder : IRecorder
  15. {
  16. private readonly ILogger _logger;
  17. private readonly IHttpClientFactory _httpClientFactory;
  18. private readonly IStreamHelper _streamHelper;
  19. public DirectRecorder(ILogger logger, IHttpClientFactory httpClientFactory, IStreamHelper streamHelper)
  20. {
  21. _logger = logger;
  22. _httpClientFactory = httpClientFactory;
  23. _streamHelper = streamHelper;
  24. }
  25. public string GetOutputPath(MediaSourceInfo mediaSource, string targetFile)
  26. {
  27. return targetFile;
  28. }
  29. public Task Record(IDirectStreamProvider? directStreamProvider, MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
  30. {
  31. if (directStreamProvider != null)
  32. {
  33. return RecordFromDirectStreamProvider(directStreamProvider, targetFile, duration, onStarted, cancellationToken);
  34. }
  35. return RecordFromMediaSource(mediaSource, targetFile, duration, onStarted, cancellationToken);
  36. }
  37. private async Task RecordFromDirectStreamProvider(IDirectStreamProvider directStreamProvider, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
  38. {
  39. Directory.CreateDirectory(Path.GetDirectoryName(targetFile) ?? throw new ArgumentException("Path can't be a root directory.", nameof(targetFile)));
  40. // use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
  41. using (var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.None))
  42. {
  43. onStarted();
  44. _logger.LogInformation("Copying recording stream to file {0}", 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. await directStreamProvider.CopyToAsync(output, cancellationTokenSource.Token).ConfigureAwait(false);
  49. }
  50. _logger.LogInformation("Recording completed to file {0}", targetFile);
  51. }
  52. private async Task RecordFromMediaSource(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
  53. {
  54. using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
  55. .GetAsync(mediaSource.Path, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
  56. _logger.LogInformation("Opened recording stream from tuner provider");
  57. Directory.CreateDirectory(Path.GetDirectoryName(targetFile) ?? throw new ArgumentException("Path can't be a root directory.", nameof(targetFile)));
  58. // use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
  59. await using var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.None);
  60. onStarted();
  61. _logger.LogInformation("Copying recording stream to file {0}", targetFile);
  62. // The media source if infinite so we need to handle stopping ourselves
  63. using var durationToken = new CancellationTokenSource(duration);
  64. using var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token);
  65. cancellationToken = linkedCancellationToken.Token;
  66. await _streamHelper.CopyUntilCancelled(
  67. await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false),
  68. output,
  69. IODefaults.CopyToBufferSize,
  70. cancellationToken).ConfigureAwait(false);
  71. _logger.LogInformation("Recording completed to file {0}", targetFile);
  72. }
  73. }
  74. }