in the audio and video development of midrange video transmission is mainly usedUDPto send the video when the data sent is greater than theTime packet sent to ensure that each packet is less than theThe key is the processing when the video is received. This article is basedUDPLibrary transfer data as a code demonstration.
Intcudpsession::splitdata (char* pbuff, uint32_t nlen)
{
int nblocknum =nlen/udp_block_size;
if (nlen%udp_block_size! = 0)
{
nblocknum++;
}
int sendlen = 0;
for (int i = 0; i< nblocknum; i++)
{
int poayload_size =udp_block_size;
char* Payload =pbuff + udp_block_size * (i);
if (nlen-udp_block_size * (i) < udp_block_size)
poayload_size =nlen-udp_block_size * (i);
cpackout* pack =new cpackout;
(*pack) <<PACK_TYPE_DATA;
(*pack) <<m_nFrameIndex;
if (i = = nBlockNum-1)
{
(*pack) << 1;
}
Else
{
(*pack) << 0;
}
(*pack) <<poayload_size;
(*pack). SetBuffer (payload,poayload_size);
int ret =sendpacket (pack, M_destip, m_destport);
printf ("Sendpacketret =%d\n", ret);
Sendlen + = ret;
delete pack;
pack = NULL;
}
return sendlen;
}
Here's a detailed look at the receiving end.,becauseUDPit is unreliable not to guarantee that the data frame must arrive normally even if the order of receipt may change, such as the likelihood of a first-sent post-to-course loss of the maximum possible chaos sequence of the case is relatively few.
the right way to handle this is
&NBSP;&NBSP;&NBSP;&NBSP; Span style= "Color:rgb (80,80,80)" > Suppose a port only receives a fixed one counterpart data source This receives a packet in the buffer and sorts it in the buffer according to the sequence number of the frame ( The large serial number of each frame is the same as you can give each small piece plus a small serial number Baotou can add this data frame a total of how many pieces received a piece of statistics to determine whether to receive the When this frame is received, the header is removed to the upper layer. When the frame data has not been collected for a certain period of time, it means that the transmission process has lost the packet, you can discard the received. &NBSP;
When the upper layer should receive the callback data after the decoding can be played. Before decoding, determine whether the frame sequence is continuous. If there is something missing in the middle, discard the sequence until the next I - frame. The sequence number of each frame is best to send and receive the negotiation between sending.
It takes a few days to complete the entire process and write it all on its own. However, from many of the RTP Open Source libraries, it is very simple to deal with a lot of things. Simply take a copy of the data and append a copy to the buffer until you find Mark 1. . What we do here as a simple use of the project also uses this simple method first to complete the function after the time to optimize.
Simple Reorganization Code:
intcudpsession::reassemble (cpackin& pack, uint32_t IP, uint32_t port)
{
int nseq = 0;
int nmark = 0;
int nlen = 0;
pack >> nseq;
pack >> Nmark;
pack >> Nlen;
if (m_nrecvframeindex! = nseq)
{
if (m_buffer)
{
Evbuffer_free (m_buffer);
m_buffer = NULL;
}
M_nrecvframeindex =nseq;
}
if (m_buffer ==null)
{
M_buffer =evbuffer_new ();
}
char* pBuf = 0;
int nSize;
Pack. GetBuffer (pbuf,nsize);
Evbuffer_add (M_buffer,pbuf, nSize);
if (Nmark = = 1)
{
// Callback
if (M_PCB)
{
M_PCB (M_udpio.m_handle, (char*) (M_buffer->buffer), M_buffer->off, IP, port,
m_pparam);
}
Evbuffer_free (m_buffer);
m_buffer = NULL;
}
return 0;
}
used in the programEvbufferis fromlibeventIt 's mostly used to deal with data buffering. Very efficient and good to see .Evbuffer.hand theBuffer.cpp.
&NBSP;&NBSP;&NBSP;&NBSP; Span style= "Color:rgb (80,80,80)" > full code in git The feature that was implemented this time is native udp bind5500 port --> Camera capture --> encoded --> Send to native 5500 port --> Receive and then decode --> show.
code sent by M_sess.send ((char*) PData, Nlen, inet_addr ("127.0.0.1"), 5500);
This program can be run separately on both machines and one is to send another to be received. The sender will just replace the 127.0.0.1 in the above sentence with the IP of your target and the other machine will be able to receive and decode it.
After the end of this article the complete client function is almost the next step to finish the server -side stun, negotiate through , Real Peer and transit video. If you have other questions about audio and video development, you can log in to the forum for consultation .http://bbs.anychat.cn/forum.php
!fryuage:zh-cn;mso-bidi-language:ar-sa ' >stun,negotiated penetration, RealPeerand transit video. If you have other questions about audio and video development, you can log in to the forum for consultation.http://bbs.anychat.cn/forum.php
The subcontracting and reorganization of audio and video instant Messaging