public UDPSender(long bytePsec, DateTime initTOT, CancellationToken cancellationToken = default(CancellationToken)) { Position = 0; bytePerSec = bytePsec; ct = cancellationToken; udp = new System.Net.Sockets.UdpClient(); packet_last = 0; init_TOT = false; synccount = 0; SendBytes = 0; HandlerArg.bytePerSec = bytePsec; HandlerArg.initialTOT = initTOT; prevSendTime = new Time_posision(DateTime.Now, 0); packet = new byte[packetlen]; }
private void checkTOT() { int offset = 0; GCHandle gch = GCHandle.Alloc(packet, GCHandleType.Pinned); try { for (var j = 0; j < packetlen; j += 188) { Position += 188; ct.ThrowIfCancellationRequested(); var TOT = (TS_packet.TOT_transport_packet)Marshal.PtrToStructure(gch.AddrOfPinnedObject() + j, typeof(TS_packet.TOT_transport_packet)); if (!TOT.IsSync) { synccount = 0; while (!TOT.IsSync && ++j < packetlen) { TOT = (TS_packet.TOT_transport_packet)Marshal.PtrToStructure(gch.AddrOfPinnedObject() + j, typeof(TS_packet.TOT_transport_packet)); offset = j; } if (!TOT.IsSync) { offset = 0; } } if (TOT.IsSync && ++synccount > 5) { synccount = 5; } if (TOT.IsTOT) { if (!init_TOT) { initialTOT = new Time_posision(TOT.JST_time, Position); InitialTOT_time = DateTime.Now; prevTOT = initialTOT; TOT_log.Enqueue(initialTOT); init_TOT = true; } else { TOT_log.Enqueue(new Time_posision(TOT.JST_time, Position)); while (TOT_log.Count > Config.SendRatebyTOTCount) { prevTOT = TOT_log.Dequeue(); } bytePerSec = (long)((Position - prevTOT.Position) / (TOT.JST_time - prevTOT.Time).TotalSeconds); } HandlerArg.TOT_JST = TOT.JST_time; HandlerArg.Position = Position; HandlerArg.bytePerSec = bytePerSec; TOTChangeHander?.Invoke(this, HandlerArg); var sendspent = TOT.JST_time - initialTOT.Time; if (SendDuration != default(TimeSpan) && sendspent >= SendDuration) { throw new PlayEOF_CanceledException(); } } } } finally { gch.Free(); } if (offset > 0) { byte[] syncpacket = new byte[packetlen]; packet_last = packetlen - offset; Array.Copy(packet, offset, syncpacket, 0, packet_last); packet = syncpacket; } }
private void SendUDP() { if (synccount < 5) { return; } ct.ThrowIfCancellationRequested(); while (Send_log.Count > Config.SendRatebySendCount) { prevSendTime = Send_log.Dequeue(); } var rate = (SendBytes - prevSendTime.Position) / (DateTime.Now - prevSendTime.Time).TotalSeconds; if (rate > bytePerSec) { var slp = ((SendBytes - prevSendTime.Position) - bytePerSec * (DateTime.Now - prevSendTime.Time).TotalSeconds) / bytePerSec; slp += Config.SendDelay * 0.001; slp *= 1000; if (waitskipforDelay > 0) { if (waitskipforDelay > slp) { waitskipforDelay -= slp; DelayforSend -= TimeSpan.FromMilliseconds(slp); slp = 0; } else { slp -= waitskipforDelay; DelayforSend -= TimeSpan.FromMilliseconds(waitskipforDelay); waitskipforDelay = 0; } } if (slp > 0) { Thread.Sleep((int)slp); } } udp.Send(packet, packetlen, Config.SendToHost, Config.SendToPort); SendBytes += packetlen; Send_log.Enqueue(new Time_posision(DateTime.Now, SendBytes)); if (init_TOT) { var StreamTime = prevTOT.Time - initialTOT.Time + TimeSpan.FromSeconds((SendBytes - prevTOT.Position) / bytePerSec); var RealTime = DateTime.Now - InitialTOT_time; if (RealTime - DelayforSend - TimeSpan.FromMilliseconds(Config.SendLongOffset) > StreamTime) { DelayforSend = RealTime - StreamTime; waitskipforDelay = DelayforSend.TotalMilliseconds; if (waitskipforDelay > Config.SendLongOffset) { waitskipforDelay = Config.SendLongOffset; } System.Diagnostics.Debug.WriteLine("delay {0} {1}", DelayforSend, waitskipforDelay); } if (StreamTime > RealTime - DelayforSend + TimeSpan.FromMilliseconds(Config.SendLongOffset)) { var slp = StreamTime - (RealTime - DelayforSend + TimeSpan.FromMilliseconds(Config.SendLongOffset)); if (slp.TotalMilliseconds > 0) { Thread.Sleep(slp); } } } }