HdHomerunHttpStream.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Model.IO;
  6. using MediaBrowser.Common.Net;
  7. using MediaBrowser.Controller;
  8. using MediaBrowser.Controller.LiveTv;
  9. using MediaBrowser.Controller.Library;
  10. using MediaBrowser.Model.Dto;
  11. using MediaBrowser.Model.Logging;
  12. using MediaBrowser.Model.MediaInfo;
  13. namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
  14. {
  15. public class HdHomerunHttpStream : LiveStream, IDirectStreamProvider
  16. {
  17. private readonly ILogger _logger;
  18. private readonly IHttpClient _httpClient;
  19. private readonly IFileSystem _fileSystem;
  20. private readonly IServerApplicationPaths _appPaths;
  21. private readonly IServerApplicationHost _appHost;
  22. private readonly CancellationTokenSource _liveStreamCancellationTokenSource = new CancellationTokenSource();
  23. private readonly TaskCompletionSource<bool> _liveStreamTaskCompletionSource = new TaskCompletionSource<bool>();
  24. private readonly MulticastStream _multicastStream;
  25. public HdHomerunHttpStream(MediaSourceInfo mediaSource, string originalStreamId, IFileSystem fileSystem, IHttpClient httpClient, ILogger logger, IServerApplicationPaths appPaths, IServerApplicationHost appHost)
  26. : base(mediaSource)
  27. {
  28. _fileSystem = fileSystem;
  29. _httpClient = httpClient;
  30. _logger = logger;
  31. _appPaths = appPaths;
  32. _appHost = appHost;
  33. OriginalStreamId = originalStreamId;
  34. _multicastStream = new MulticastStream(_logger);
  35. }
  36. protected override async Task OpenInternal(CancellationToken openCancellationToken)
  37. {
  38. _liveStreamCancellationTokenSource.Token.ThrowIfCancellationRequested();
  39. var mediaSource = OriginalMediaSource;
  40. var url = mediaSource.Path;
  41. _logger.Info("Opening HDHR Live stream from {0}", url);
  42. var taskCompletionSource = new TaskCompletionSource<bool>();
  43. StartStreaming(url, taskCompletionSource, _liveStreamCancellationTokenSource.Token);
  44. //OpenedMediaSource.Protocol = MediaProtocol.File;
  45. //OpenedMediaSource.Path = tempFile;
  46. //OpenedMediaSource.ReadAtNativeFramerate = true;
  47. OpenedMediaSource.Path = _appHost.GetLocalApiUrl("127.0.0.1") + "/LiveTv/LiveStreamFiles/" + UniqueId + "/stream.ts";
  48. OpenedMediaSource.Protocol = MediaProtocol.Http;
  49. OpenedMediaSource.SupportsDirectPlay = false;
  50. OpenedMediaSource.SupportsDirectStream = true;
  51. OpenedMediaSource.SupportsTranscoding = true;
  52. await taskCompletionSource.Task.ConfigureAwait(false);
  53. //await Task.Delay(5000).ConfigureAwait(false);
  54. }
  55. public override Task Close()
  56. {
  57. _logger.Info("Closing HDHR live stream");
  58. _liveStreamCancellationTokenSource.Cancel();
  59. return _liveStreamTaskCompletionSource.Task;
  60. }
  61. private async Task StartStreaming(string url, TaskCompletionSource<bool> openTaskCompletionSource, CancellationToken cancellationToken)
  62. {
  63. await Task.Run(async () =>
  64. {
  65. var isFirstAttempt = true;
  66. while (!cancellationToken.IsCancellationRequested)
  67. {
  68. try
  69. {
  70. using (var response = await _httpClient.SendAsync(new HttpRequestOptions
  71. {
  72. Url = url,
  73. CancellationToken = cancellationToken,
  74. BufferContent = false,
  75. // Increase a little bit
  76. TimeoutMs = 30000
  77. }, "GET").ConfigureAwait(false))
  78. {
  79. _logger.Info("Opened HDHR stream from {0}", url);
  80. if (!cancellationToken.IsCancellationRequested)
  81. {
  82. _logger.Info("Beginning multicastStream.CopyUntilCancelled");
  83. Action onStarted = null;
  84. if (isFirstAttempt)
  85. {
  86. onStarted = () => openTaskCompletionSource.TrySetResult(true);
  87. }
  88. await _multicastStream.CopyUntilCancelled(response.Content, onStarted, cancellationToken).ConfigureAwait(false);
  89. }
  90. }
  91. }
  92. catch (OperationCanceledException)
  93. {
  94. break;
  95. }
  96. catch (Exception ex)
  97. {
  98. if (isFirstAttempt)
  99. {
  100. _logger.ErrorException("Error opening live stream:", ex);
  101. openTaskCompletionSource.TrySetException(ex);
  102. break;
  103. }
  104. _logger.ErrorException("Error copying live stream, will reopen", ex);
  105. }
  106. isFirstAttempt = false;
  107. }
  108. _liveStreamTaskCompletionSource.TrySetResult(true);
  109. }).ConfigureAwait(false);
  110. }
  111. public Task CopyToAsync(Stream stream, CancellationToken cancellationToken)
  112. {
  113. return _multicastStream.CopyToAsync(stream);
  114. }
  115. }
  116. }