我使用UDP進行視頻流傳輸,但由於幀大小超過了udp max的大小,我必須將它分成主要數組並且發送幾個數據包。沒關係,但是它是udp,所以數組通常不按照客戶端的順序(例如:第一幀的第一段,第二幀的第三段等等)。我可以對段進行編碼,但是經常會以錯誤幀(第一幀的第一段,第二幀的第二段,第二幀的第三段)接收段。 發送:UDP視頻流C#
while(true){
int c = CvInvoke.cvWaitKey(33);
if (c == 27)
break;
Byte[] byteData1 = rgb32Image.Bytes;
Byte[] byte1 = new Byte[65000];
Byte[] byte2 = new Byte[65000];
Byte[] byte3 = new Byte[65000];
Byte[] byte4 = new Byte[65000];
byte1[0] = 1;
byte2[0] = 2;
byte3[0] = 3;
byte4[0] = 4;
System.Buffer.BlockCopy(byteData1, 0, byte1, 1, 64999);
obj.socket.Send(byte1, byte1.Length);
System.Buffer.BlockCopy(byteData1, 64999, byte2, 1, 64999);
obj.socket.Send(byte2, byte2.Length);
System.Buffer.BlockCopy(byteData1, 64999 * 2, byte3, 1, 64999);
obj.socket.Send(byte3, byte3.Length);
System.Buffer.BlockCopy(byteData1, 64999 * 3, byte4, 1, byteData1.Length - (3 * 64999);
obj.socket.Send(byte4, byte4.Length);
}
接收:
VideoReceive(IAsyncResult ar)
{
StateObject stateobj = (StateObject)ar.AsyncState;
UdpClient client = stateobj.workSocket;
IPEndPoint senderIPEndPoint = new IPEndPoint(IPAddress.Any, 0);
Byte[] byte1 = client.EndReceive(ar, ref senderIPEndPoint);
Byte[] byte2 = client.Receive(ref senderIPEndPoint);
Byte[] byte3 = client.Receive(ref senderIPEndPoint);
Byte[] byte4 = client.Receive(ref senderIPEndPoint);
switch (byte1[0])
{
case 1:
System.Buffer.BlockCopy(byte1, 1, buffer, 0, 64999);
break;
case 2:
System.Buffer.BlockCopy(byte1, 1, buffer, 64999, 64999);
break;
case 3:
System.Buffer.BlockCopy(byte1, 1, buffer, 64999 * 2, 64999);
break;
case 4:
System.Buffer.BlockCopy(byte1, 1, buffer, 64999 * 3, 64999);
break;
}
switch (byte2[0])
{
case 1:
System.Buffer.BlockCopy(byte2, 1, buffer, 0, 64999);
break;
case 2:
System.Buffer.BlockCopy(byte2, 1, buffer, 64999, 64999);
break;
case 3:
System.Buffer.BlockCopy(byte2, 1, buffer, 64999 * 2, 64999);
break;
case 4:
System.Buffer.BlockCopy(byte2, 1, buffer, 64999 * 3, 64999);
break;
}
switch (byte3[0])
{
case 1:
System.Buffer.BlockCopy(byte3, 1, buffer, 0, 64999);
break;
case 2:
System.Buffer.BlockCopy(byte3, 1, buffer, 64999, 64999);
break;
case 3:
System.Buffer.BlockCopy(byte3, 1, buffer, 64999 * 2, 64999);
break;
case 4:
System.Buffer.BlockCopy(byte3, 1, buffer, 64999 * 3, 64999);
break;
}
switch (byte4[0])
{
case 1:
System.Buffer.BlockCopy(byte4, 1, buffer, 0, 64999);
break;
case 2:
System.Buffer.BlockCopy(byte4, 1, buffer, 64999, 64999);
break;
case 3:
System.Buffer.BlockCopy(byte4, 1, buffer, 64999 * 2, 64999);
break;
case 4:
System.Buffer.BlockCopy(byte4, 1, buffer, 64999 * 3, 64999);
break;
}
stateobj.workSocket.BeginReceive(new System.AsyncCallback(VideoReceive), stateobj);
mask2.Bytes = buffer;
pictureBox1.BeginInvoke(new MethodInvoker(() => { pictureBox1.Image = mask2.ToBitmap(); }));
}
所以,我怎麼能視頻流通過UDP套接字(不帶任何附加庫)? P.S.你可以用代碼或算法來回答。
序列號和大量的緩衝。視頻流!=平凡。 –
@MartinJames所以我問如何做到這一點 –