MulticastStream.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Model.Logging;
  10. using MediaBrowser.Model.Net;
  11. namespace Emby.Server.Implementations.LiveTv.TunerHosts
  12. {
  13. public class MulticastStream
  14. {
  15. private readonly ConcurrentDictionary<Guid,QueueStream> _outputStreams = new ConcurrentDictionary<Guid, QueueStream>();
  16. private const int BufferSize = 81920;
  17. private CancellationToken _cancellationToken;
  18. private readonly ILogger _logger;
  19. public MulticastStream(ILogger logger)
  20. {
  21. _logger = logger;
  22. }
  23. public async Task CopyUntilCancelled(Stream source, Action onStarted, CancellationToken cancellationToken)
  24. {
  25. _cancellationToken = cancellationToken;
  26. byte[] buffer = new byte[BufferSize];
  27. while (!cancellationToken.IsCancellationRequested)
  28. {
  29. var bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
  30. if (bytesRead > 0)
  31. {
  32. var allStreams = _outputStreams.ToList();
  33. //if (allStreams.Count == 1)
  34. //{
  35. // await allStreams[0].Value.WriteAsync(buffer, 0, bytesRead).ConfigureAwait(false);
  36. //}
  37. //else
  38. {
  39. byte[] copy = new byte[bytesRead];
  40. Buffer.BlockCopy(buffer, 0, copy, 0, bytesRead);
  41. foreach (var stream in allStreams)
  42. {
  43. stream.Value.Queue(copy, 0, copy.Length);
  44. }
  45. }
  46. if (onStarted != null)
  47. {
  48. var onStartedCopy = onStarted;
  49. onStarted = null;
  50. Task.Run(onStartedCopy);
  51. }
  52. }
  53. else
  54. {
  55. await Task.Delay(100).ConfigureAwait(false);
  56. }
  57. }
  58. }
  59. private static int RtpHeaderBytes = 12;
  60. public async Task CopyUntilCancelled(ISocket udpClient, Action onStarted, CancellationToken cancellationToken)
  61. {
  62. _cancellationToken = cancellationToken;
  63. while (!cancellationToken.IsCancellationRequested)
  64. {
  65. var receiveToken = cancellationToken;
  66. // On the first connection attempt, put a timeout to avoid being stuck indefinitely in the event of failure
  67. if (onStarted != null)
  68. {
  69. receiveToken = CancellationTokenSource.CreateLinkedTokenSource(new CancellationTokenSource(5000).Token, cancellationToken).Token;
  70. }
  71. var data = await udpClient.ReceiveAsync(receiveToken).ConfigureAwait(false);
  72. var bytesRead = data.ReceivedBytes - RtpHeaderBytes;
  73. if (bytesRead > 0)
  74. {
  75. var allStreams = _outputStreams.ToList();
  76. if (allStreams.Count == 1)
  77. {
  78. await allStreams[0].Value.WriteAsync(data.Buffer, 0, bytesRead).ConfigureAwait(false);
  79. }
  80. else
  81. {
  82. byte[] copy = new byte[bytesRead];
  83. Buffer.BlockCopy(data.Buffer, RtpHeaderBytes, copy, 0, bytesRead);
  84. foreach (var stream in allStreams)
  85. {
  86. stream.Value.Queue(copy, 0, copy.Length);
  87. }
  88. }
  89. if (onStarted != null)
  90. {
  91. var onStartedCopy = onStarted;
  92. onStarted = null;
  93. Task.Run(onStartedCopy);
  94. }
  95. }
  96. else
  97. {
  98. await Task.Delay(100).ConfigureAwait(false);
  99. }
  100. }
  101. }
  102. public Task CopyToAsync(Stream stream)
  103. {
  104. var result = new QueueStream(stream, _logger)
  105. {
  106. OnFinished = OnFinished
  107. };
  108. _outputStreams.TryAdd(result.Id, result);
  109. result.Start(_cancellationToken);
  110. return result.TaskCompletion.Task;
  111. }
  112. public void RemoveOutputStream(QueueStream stream)
  113. {
  114. QueueStream removed;
  115. _outputStreams.TryRemove(stream.Id, out removed);
  116. }
  117. private void OnFinished(QueueStream queueStream)
  118. {
  119. RemoveOutputStream(queueStream);
  120. }
  121. }
  122. }