DirectRecorder.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 string GetOutputPath(MediaSourceInfo mediaSource, string targetFile)
  24. {
  25. return targetFile;
  26. }
  27. public async Task Record(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
  28. {
  29. var httpRequestOptions = new HttpRequestOptions()
  30. {
  31. Url = mediaSource.Path
  32. };
  33. httpRequestOptions.BufferContent = false;
  34. using (var response = await _httpClient.SendAsync(httpRequestOptions, "GET").ConfigureAwait(false))
  35. {
  36. _logger.Info("Opened recording stream from tuner provider");
  37. using (var output = _fileSystem.GetFileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.Read))
  38. {
  39. onStarted();
  40. _logger.Info("Copying recording stream to file {0}", targetFile);
  41. // The media source if infinite so we need to handle stopping ourselves
  42. var durationToken = new CancellationTokenSource(duration);
  43. cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token).Token;
  44. await CopyUntilCancelled(response.Content, output, cancellationToken).ConfigureAwait(false);
  45. }
  46. }
  47. _logger.Info("Recording completed to file {0}", targetFile);
  48. }
  49. private const int BufferSize = 81920;
  50. public static Task CopyUntilCancelled(Stream source, Stream target, CancellationToken cancellationToken)
  51. {
  52. return CopyUntilCancelled(source, target, null, cancellationToken);
  53. }
  54. public static async Task CopyUntilCancelled(Stream source, Stream target, Action onStarted, CancellationToken cancellationToken)
  55. {
  56. while (!cancellationToken.IsCancellationRequested)
  57. {
  58. var bytesRead = await CopyToAsyncInternal(source, target, BufferSize, onStarted, cancellationToken).ConfigureAwait(false);
  59. onStarted = null;
  60. //var position = fs.Position;
  61. //_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
  62. if (bytesRead == 0)
  63. {
  64. await Task.Delay(100).ConfigureAwait(false);
  65. }
  66. }
  67. }
  68. private static async Task<int> CopyToAsyncInternal(Stream source, Stream destination, Int32 bufferSize, Action onStarted, CancellationToken cancellationToken)
  69. {
  70. byte[] buffer = new byte[bufferSize];
  71. int bytesRead;
  72. int totalBytesRead = 0;
  73. while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
  74. {
  75. await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
  76. totalBytesRead += bytesRead;
  77. if (onStarted != null)
  78. {
  79. onStarted();
  80. }
  81. onStarted = null;
  82. }
  83. return totalBytesRead;
  84. }
  85. }
  86. }