DirectRecorder.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #pragma warning disable CS1591
  2. #pragma warning disable SA1600
  3. using System;
  4. using System.IO;
  5. using System.Net.Http;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Common.Net;
  9. using MediaBrowser.Controller.Library;
  10. using MediaBrowser.Model.Dto;
  11. using MediaBrowser.Model.IO;
  12. using Microsoft.Extensions.Logging;
  13. namespace Emby.Server.Implementations.LiveTv.EmbyTV
  14. {
  15. public class DirectRecorder : IRecorder
  16. {
  17. private readonly ILogger _logger;
  18. private readonly IHttpClient _httpClient;
  19. private readonly IStreamHelper _streamHelper;
  20. public DirectRecorder(ILogger logger, IHttpClient httpClient, IStreamHelper streamHelper)
  21. {
  22. _logger = logger;
  23. _httpClient = httpClient;
  24. _streamHelper = streamHelper;
  25. }
  26. public string GetOutputPath(MediaSourceInfo mediaSource, string targetFile)
  27. {
  28. return targetFile;
  29. }
  30. public Task Record(IDirectStreamProvider directStreamProvider, MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
  31. {
  32. if (directStreamProvider != null)
  33. {
  34. return RecordFromDirectStreamProvider(directStreamProvider, targetFile, duration, onStarted, cancellationToken);
  35. }
  36. return RecordFromMediaSource(mediaSource, targetFile, duration, onStarted, cancellationToken);
  37. }
  38. private async Task RecordFromDirectStreamProvider(IDirectStreamProvider directStreamProvider, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
  39. {
  40. Directory.CreateDirectory(Path.GetDirectoryName(targetFile));
  41. using (var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.Read))
  42. {
  43. onStarted();
  44. _logger.LogInformation("Copying recording stream to file {0}", targetFile);
  45. // The media source is infinite so we need to handle stopping ourselves
  46. var durationToken = new CancellationTokenSource(duration);
  47. cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token).Token;
  48. await directStreamProvider.CopyToAsync(output, cancellationToken).ConfigureAwait(false);
  49. }
  50. _logger.LogInformation("Recording completed to file {0}", targetFile);
  51. }
  52. private async Task RecordFromMediaSource(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
  53. {
  54. var httpRequestOptions = new HttpRequestOptions
  55. {
  56. Url = mediaSource.Path,
  57. BufferContent = false,
  58. // Some remote urls will expect a user agent to be supplied
  59. UserAgent = "Emby/3.0",
  60. // Shouldn't matter but may cause issues
  61. DecompressionMethod = CompressionMethod.None
  62. };
  63. using (var response = await _httpClient.SendAsync(httpRequestOptions, HttpMethod.Get).ConfigureAwait(false))
  64. {
  65. _logger.LogInformation("Opened recording stream from tuner provider");
  66. Directory.CreateDirectory(Path.GetDirectoryName(targetFile));
  67. using (var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.Read))
  68. {
  69. onStarted();
  70. _logger.LogInformation("Copying recording stream to file {0}", targetFile);
  71. // The media source if infinite so we need to handle stopping ourselves
  72. var durationToken = new CancellationTokenSource(duration);
  73. cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token).Token;
  74. await _streamHelper.CopyUntilCancelled(response.Content, output, 81920, cancellationToken).ConfigureAwait(false);
  75. }
  76. }
  77. _logger.LogInformation("Recording completed to file {0}", targetFile);
  78. }
  79. }
  80. }