How to challenge the UDP exit bat
-
I'm studying MPEG TS files in C# 5.0/.Net Core 3.1. I know these files have time stamps and no problem getting them. The question is simple. There's a MPEG TS file, the length of this file is 60,100 ms. The next code shoots the entire file for about 5,000 ms. We need this to happen in 60,100 ms.
static void Main(string[] args) { string tsFile = @"file.ts";
byte[] bytes = File.ReadAllBytes(tsFile); UdpClient outPutUdpClient = new UdpClient { ExclusiveAddressUse=false}; IPAddress outPutIp = IPAddress.Parse("192.168.99.239"); int multicastPort = 1234; var localEp = new IPEndPoint(outPutIp, multicastPort); outPutUdpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); outPutUdpClient.Ttl = 3; outPutUdpClient.Client.Bind(localEp); var mcastAddr = IPAddress.Parse("239.1.1.1"); outPutUdpClient.Connect(mcastAddr, multicastPort); for(int i = 1315; i < bytes.Length; i+=1316) // take 7 mpeg(188*7) ts packets per UPD packet { int begin = i-1315; int end = i; var sendBytes = bytes[begin..end]; try { outPutUdpClient.Send(sendBytes, sendBytes.Length); } catch (Exception ex) { Debug.WriteLine(ex); } } Console.WriteLine("Done"); Console.ReadKey(); }
You can read a bite file and make reading delays. You can read the whole file and make delays on the shipment. What approach would be appropriate for that task? And how do you want to arrange a delay?
-
I did some research on this subject. The results are lower. Measurements were carried out with the Dektec StreamXpert v 2.2.1
Option 1: send UDP packs through a certain time interval:
var cnt = 0;
for (int i = 0; i < iteration; i++) { var sendBytes = new byte[188 * 7]; cnt++; if (cnt >= 57) // через каждые 57 пакетов - задержка в 1 мс. { Thread.Sleep(1); cnt = 0; } for (int j = 0; j < 7; j++) { Buffer.BlockCopy(packets[7 * i + j].bytes, 0, sendBytes, j * 188, 188); } outPutUdpClient.Send(sendBytes, 188 * 7); }
The analyser shall have such a picture:
PCR-related PCR-related errors of TR101290 2nd priority.
Option 2. Use the timer and, through certain time intervals, cause an event that will send a premeditated pack of Byte:
timer = new System.Timers.Timer();
timer.Interval = 10;
timer.AutoReset = true;
timer.Elapsed += Timer_Elapsed;
timer.Enabled = true;
At 10 ms, the picture on the analyzer gets a little better:
When 1 ms is allowed, the standard timer is not working normally. The overall battle falls. There was no reason for that behavior.
Option 3. Most preferable. Found a screwdriver over Multimedia timer. https://docs.microsoft.com/ru-ru/windows/win32/multimedia/multimedia-timers right here. https://github.com/eranbetzalel/SimpleMulticastAnalyzer/blob/master/Infrastructure/HighResolutionTimer.cs This timer works well when 1 ms is allowed.
timer = new HighResolutionTimer();
timer.Mode = TimerMode.Periodic;
timer.Period = 1;
timer.Resolution = 0;
timer.IsAsync = true;
timer.Tick += Timer_Elapsed;
timer.Start();
The analyser is perfectly straight:
Exclusion. I think the only way to launch the MPEG TS is through MultiMedia timer. If there are other options, please share.
ps. The standard timer is also working on low battles, but the 30+ Mbit/sek is already missing. For everything to be feng-shaped, you have to take PCR notes from the flow and to calculate the number of packages sent per unit of time. The current option would only work with a constant-bitrate flow (CBR)