DirectRecorder.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. var durationToken = new CancellationTokenSource(duration);
  62. cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token).Token;
  63. await _streamHelper.CopyUntilCancelled(
  64. await response.Content.ReadAsStreamAsync().ConfigureAwait(false),
  65. output,
  66. IODefaults.CopyToBufferSize,
  67. cancellationToken).ConfigureAwait(false);
  68. _logger.LogInformation("Recording completed to file {0}", targetFile);
  69. }
  70. }
  71. }