我们在上一期使用RTP协议,并进行了配置,打包了视频数据,这一期我们就对发送的数据进行重组,并显示在接受端上。最后对其进行扩展,支持多客户端视频发送,并在接收端分屏显示。完成远程监控的模拟。
先来个效果图吧
private bool NewRTPPacket(RTPPacket packet)
{
if (!Clients.ContainsKey(packet.SSRC))//如果接受端第一次接受到某源的数据,则加入到
{
if (Clients.Count < 4)//如果发送端为4,则丢弃包
{
Clients.Add(packet.SSRC, new List<RTPPacket> {packet});
ImagesBoxMapping[ImagesBoxMapping.First(pair => pair.Value == null).Key] = packet.SSRC;
}
}
else
{
Clients[packet.SSRC].Add(packet);
} if (packet.Marker)//如果已经发送完毕
{
//丢包检测
var orderPackets = Clients[packet.SSRC].OrderBy(rtpPacket => rtpPacket.SequenceNumber);
if (Clients[packet.SSRC].Count != (orderPackets.Last().SequenceNumber - orderPackets.First().SequenceNumber + 1))
{
Clients[packet.SSRC].Clear();//清空缓存区
return true;
} //1.包重组
var count = Clients[packet.SSRC].Sum(rtpPacket => rtpPacket.DataSize);//数据总数 var newData = new byte[count]; long offSet = 0;
foreach (var rtpPacket in Clients[packet.SSRC])
{
Array.Copy(rtpPacket.DataPointer, 0, newData, offSet, rtpPacket.DataSize);
offSet += rtpPacket.DataSize;
}
Clients[packet.SSRC].Clear();//清空缓存区 var ms = new MemoryStream(newData);
try
{
var bmp = new Bitmap(Image.FromStream(ms));
var img = new Image<Bgr, Byte>(bmp);
ImagesBoxMapping.First(pair => pair.Value == packet.SSRC).Key.Image = img;
}
catch (Exception)
{ }
finally
{
ms.Close();
}
}
return true;
}
完整代码可以到百度云下载:包括发送端和接收端