DirectRecorder.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. if (mediaSource.RunTimeTicks.HasValue)
  42. {
  43. // The media source already has a fixed duration
  44. // But add another stop 1 minute later just in case the recording gets stuck for any reason
  45. var durationToken = new CancellationTokenSource(duration.Add(TimeSpan.FromMinutes(1)));
  46. cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token).Token;
  47. }
  48. else
  49. {
  50. // The media source if infinite so we need to handle stopping ourselves
  51. var durationToken = new CancellationTokenSource(duration);
  52. cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token).Token;
  53. }
  54. await CopyUntilCancelled(response.Content, output, cancellationToken).ConfigureAwait(false);
  55. }
  56. }
  57. _logger.Info("Recording completed to file {0}", targetFile);
  58. }
  59. private const int BufferSize = 81920;
  60. public static Task CopyUntilCancelled(Stream source, Stream target, CancellationToken cancellationToken)
  61. {
  62. return CopyUntilCancelled(source, target, null, cancellationToken);
  63. }
  64. public static async Task CopyUntilCancelled(Stream source, Stream target, Action onStarted, CancellationToken cancellationToken)
  65. {
  66. while (!cancellationToken.IsCancellationRequested)
  67. {
  68. var bytesRead = await CopyToAsyncInternal(source, target, BufferSize, onStarted, cancellationToken).ConfigureAwait(false);
  69. onStarted = null;
  70. //var position = fs.Position;
  71. //_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
  72. if (bytesRead == 0)
  73. {
  74. await Task.Delay(100).ConfigureAwait(false);
  75. }
  76. }
  77. }
  78. private static async Task<int> CopyToAsyncInternal(Stream source, Stream destination, Int32 bufferSize, Action onStarted, CancellationToken cancellationToken)
  79. {
  80. byte[] buffer = new byte[bufferSize];
  81. int bytesRead;
  82. int totalBytesRead = 0;
  83. while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
  84. {
  85. await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
  86. totalBytesRead += bytesRead;
  87. if (onStarted != null)
  88. {
  89. onStarted();
  90. }
  91. onStarted = null;
  92. }
  93. return totalBytesRead;
  94. }
  95. }
  96. }