DirectRecorder.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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));
  40. using (var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.Read))
  41. {
  42. onStarted();
  43. _logger.LogInformation("Copying recording stream to file {0}", targetFile);
  44. // The media source is infinite so we need to handle stopping ourselves
  45. using var durationToken = new CancellationTokenSource(duration);
  46. using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token);
  47. await directStreamProvider.CopyToAsync(output, cancellationTokenSource.Token).ConfigureAwait(false);
  48. }
  49. _logger.LogInformation("Recording completed to file {0}", targetFile);
  50. }
  51. private async Task RecordFromMediaSource(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
  52. {
  53. using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
  54. .GetAsync(mediaSource.Path, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
  55. _logger.LogInformation("Opened recording stream from tuner provider");
  56. Directory.CreateDirectory(Path.GetDirectoryName(targetFile));
  57. await using var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.Read);
  58. onStarted();
  59. _logger.LogInformation("Copying recording stream to file {0}", targetFile);
  60. // The media source if infinite so we need to handle stopping ourselves
  61. using var durationToken = new CancellationTokenSource(duration);
  62. using var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token);
  63. cancellationToken = linkedCancellationToken.Token;
  64. await _streamHelper.CopyUntilCancelled(
  65. await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false),
  66. output,
  67. IODefaults.CopyToBufferSize,
  68. cancellationToken).ConfigureAwait(false);
  69. _logger.LogInformation("Recording completed to file {0}", targetFile);
  70. }
  71. }
  72. }