我正在尝试解析通过多播接收的 MPEG2-TS 数据。问题是接收方法有时会跳过数据包 - 我相信这在很大程度上取决于连续 Receive() 方法之间进行的额外处理[根据我所做的研究,如果 CPU 不在 Receive() 方法上,数据包将丢失,因此缓冲将是不立即处理的最快选择,并将其留给另一个线程完成......我对吗?]。
我目前正在使用队列来保存接收到的数据报,以便以后使用 dequeue 方法从另一个线程处理。我还切换到使用在新线程上初始化的阻塞多播接收器,而不是异步接收器,以确保没有延迟从一个线程委派到另一个线程[例如,当使用 OnReceiveFrom() 方法时]。
组播接收器的代码如下:
class Multicast
{
/// <summary>
/// The possible Operating Modes allowed for this Multicast Class File
/// </summary>
public enum OperationMode
{
Client,
Server
};
private IPAddress ipAddress;
private int portNumber;
private int interfaceIndex;
private Socket socket;
private EndPoint sourceOrDestination;
private byte[] Data;
private OperationMode operationMode;
public Queue<byte[]> q = new Queue<byte[]>();
public Multicast(string ipAddress, int portNumber, int interfaceIndex, OperationMode operationMode)
{
if (!IPAddress.TryParse(ipAddress, out this.ipAddress))
{
throw new Exception("Incorrect Argument Data. Unable to parse IP Address!");
}
this.portNumber = portNumber;
this.interfaceIndex = interfaceIndex;
this.operationMode = operationMode;
}
public void Start()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, portNumber); // Local IP and Port (if more than one NIC interface is available, this command must be altered!
//socket.SetSocketOption(SocketOptionLevel.Udp,SocketOptionName.Broadcast,true);
socket.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.NoDelay, 1);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); // Allow for loopback testing
socket.Bind(localEndPoint); // Extremly important to bind the Socket before joining multicast groups
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, Properties.Settings.Default.Network_MulticastTTL); // Set TTL (e.g.: 1 for one router hop)
try
{
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ipAddress, interfaceIndex)); // Join Multicast
}
catch (Exception) { }
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, Properties.Settings.Default.Network_FramePayloadSize);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, Properties.Settings.Default.Network_FramePayloadSize);
//socket.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.DontFragment, 1);
socket.DontFragment = true;
while (true)
{
Data = new byte[socket.ReceiveBufferSize];
socket.Receive(Data);
q.Enqueue(Data);
}
}
/// <summary>
/// This function is used to stop the socket from sending/receiving further data. The socket is therefore closed.
/// </summary>
public void Stop()
{
socket.Close();
}
}
即使从字面上将所有收到的数据报转储到一个 .ts 文件中,在 VLC 中播放时,人们可能会注意到像素化和音频跳过。实际上,由于wireshark将数据包全部按顺序显示,因此网卡成功接收数据包,并且在VLC中可以毫无问题地播放流(直接打开流时)。
你会建议什么来改善结果?