MulticastStream.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. if (source == null)
  28. {
  29. throw new ArgumentNullException("source");
  30. }
  31. while (!cancellationToken.IsCancellationRequested)
  32. {
  33. var bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
  34. if (bytesRead > 0)
  35. {
  36. var allStreams = _outputStreams.ToList();
  37. //if (allStreams.Count == 1)
  38. //{
  39. // await allStreams[0].Value.WriteAsync(buffer, 0, bytesRead).ConfigureAwait(false);
  40. //}
  41. //else
  42. {
  43. byte[] copy = new byte[bytesRead];
  44. Buffer.BlockCopy(buffer, 0, copy, 0, bytesRead);
  45. foreach (var stream in allStreams)
  46. {
  47. stream.Value.Queue(copy, 0, copy.Length);
  48. }
  49. }
  50. if (onStarted != null)
  51. {
  52. var onStartedCopy = onStarted;
  53. onStarted = null;
  54. Task.Run(onStartedCopy);
  55. }
  56. }
  57. else
  58. {
  59. await Task.Delay(100).ConfigureAwait(false);
  60. }
  61. }
  62. }
  63. private static int RtpHeaderBytes = 12;
  64. public async Task CopyUntilCancelled(ISocket udpClient, Action onStarted, CancellationToken cancellationToken)
  65. {
  66. _cancellationToken = cancellationToken;
  67. while (!cancellationToken.IsCancellationRequested)
  68. {
  69. var receiveToken = cancellationToken;
  70. // On the first connection attempt, put a timeout to avoid being stuck indefinitely in the event of failure
  71. if (onStarted != null)
  72. {
  73. receiveToken = CancellationTokenSource.CreateLinkedTokenSource(new CancellationTokenSource(5000).Token, cancellationToken).Token;
  74. }
  75. var data = await udpClient.ReceiveAsync(receiveToken).ConfigureAwait(false);
  76. var bytesRead = data.ReceivedBytes - RtpHeaderBytes;
  77. if (bytesRead > 0)
  78. {
  79. var allStreams = _outputStreams.ToList();
  80. if (allStreams.Count == 1)
  81. {
  82. await allStreams[0].Value.WriteAsync(data.Buffer, 0, bytesRead).ConfigureAwait(false);
  83. }
  84. else
  85. {
  86. byte[] copy = new byte[bytesRead];
  87. Buffer.BlockCopy(data.Buffer, RtpHeaderBytes, copy, 0, bytesRead);
  88. foreach (var stream in allStreams)
  89. {
  90. stream.Value.Queue(copy, 0, copy.Length);
  91. }
  92. }
  93. if (onStarted != null)
  94. {
  95. var onStartedCopy = onStarted;
  96. onStarted = null;
  97. Task.Run(onStartedCopy);
  98. }
  99. }
  100. else
  101. {
  102. await Task.Delay(100).ConfigureAwait(false);
  103. }
  104. }
  105. }
  106. public Task CopyToAsync(Stream stream)
  107. {
  108. var result = new QueueStream(stream, _logger)
  109. {
  110. OnFinished = OnFinished
  111. };
  112. _outputStreams.TryAdd(result.Id, result);
  113. result.Start(_cancellationToken);
  114. return result.TaskCompletion.Task;
  115. }
  116. public void RemoveOutputStream(QueueStream stream)
  117. {
  118. QueueStream removed;
  119. _outputStreams.TryRemove(stream.Id, out removed);
  120. }
  121. private void OnFinished(QueueStream queueStream)
  122. {
  123. RemoveOutputStream(queueStream);
  124. }
  125. }
  126. }