DirectRecorder.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using CommonIO;
  6. using MediaBrowser.Common.IO;
  7. using MediaBrowser.Common.Net;
  8. using MediaBrowser.Model.Dto;
  9. using MediaBrowser.Model.Logging;
  10. namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV
  11. {
  12. public class DirectRecorder : IRecorder
  13. {
  14. private readonly ILogger _logger;
  15. private readonly IHttpClient _httpClient;
  16. private readonly IFileSystem _fileSystem;
  17. public DirectRecorder(ILogger logger, IHttpClient httpClient, IFileSystem fileSystem)
  18. {
  19. _logger = logger;
  20. _httpClient = httpClient;
  21. _fileSystem = fileSystem;
  22. }
  23. public async Task Record(MediaSourceInfo mediaSource, string targetFile, Action onStarted, CancellationToken cancellationToken)
  24. {
  25. var httpRequestOptions = new HttpRequestOptions()
  26. {
  27. Url = mediaSource.Path
  28. };
  29. httpRequestOptions.BufferContent = false;
  30. using (var response = await _httpClient.SendAsync(httpRequestOptions, "GET").ConfigureAwait(false))
  31. {
  32. _logger.Info("Opened recording stream from tuner provider");
  33. using (var output = _fileSystem.GetFileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.Read))
  34. {
  35. onStarted();
  36. _logger.Info("Copying recording stream to file stream");
  37. await response.Content.CopyToAsync(output, StreamDefaults.DefaultCopyToBufferSize, cancellationToken).ConfigureAwait(false);
  38. }
  39. }
  40. }
  41. }
  42. }