In the previous article we introduced some basic knowledge of the RTP protocol, below we describe how to use jrtplib this library to transmit H264 encoding.
JRTP transmission: OK, here is the example I wrote about sending H264 packets using JRTP, which can be explained in detail. The sending side can also receive RTCP packets sent by the receiving end. #define MAX_RTP_PKT_LENGTH 1360#define H264 96bool checkerror (int rtperr); class CRTPSender : Public rtpsession{public:crtpsender (void), ~crtpsender (void);p Rotected:void onapppacket (RTCPAPPPacket *apppacket,const rtptime &receivetime,const rtpaddress *senderaddress); void onbyepacket (Rtpsourcedata *srcdat); Void onbyetimeout (rtpsourcedata *srcdat);p ublic:void sendh264nalu (Unsigned char* m_h264buf,int buflen); void setparamsforsendingh264 ();}; Bool checkerror (Int rtperr) {if (rtperr < 0) {std::cout<< "ERROR: " < <rtpgeterrorstring (Rtperr) <<std::endl;return false;} Return true;} Crtpsender::crtpsender (void) {}crtpsender::~crtpsender (void) {}void crtpsender::onapppaCket (rtcpapppacket *apppacket,const rtptime &receivetime,const rtpaddress * senderaddress) {//Receive Rtcp app data std::cout<< "got rtcp packet from: " << senderaddress<<std::endl;std::cout<< "got rtcp subtype: " <<apppacket-> Getsubtype () <<std::endl;std::cout<< "got rtcp data: " << (char *) apppacket- >getappdata () <<std::endl;return ;} Void crtpsender::sendh264nalu (Unsigned char* m_h264buf,int buflen) {unsigned char *psendbuf; //send data pointer psendbuf = m_h264buf;//remove preamble 0x000001 or 0x00000001//if ( 0x01 == m_h264buf[2] )//{//psendbuf = &m_h264buf[3];//buflen -= 3;//}// else//{//psendbuf = &m_h264buf[4];//buflen -= 4;//}char sendbuf[1430]; //sent data buffer memset (sendbuf,0,1430); int status; printf ("Send packet&nbSp;length %d \n ", Buflen);if ( buflen <= MAX_RTP_PKT_LENGTH ) { memcpy (Sendbuf,psendbuf,buflen); status = this->sendpacket (void *) SendBuf, Buflen); checkerror (status);} else if (buflen > max_rtp_pkt_length) {//Set flag bit mark is 0this->setdefaultmark (false) //printf ("buflen = %d\n", buflen);//Gets the number of RTP packets that need to be max_rtp_pkt_length bytes long to send int k=0,l=0; k = buflen / MAX_RTP_PKT_LENGTH;l = buflen % MAX_RTP_PKT_LENGTH; INT&NBSP;T=0;//is currently sending the first few shard RTP packets char nalheader = psendbuf[0]; // nalu Header A?¤while ( t < k | | ( t==k && l>0 ) ) { if ( (0 == t ) | | ( t<k && 0!=t ) )//first package to the last package {/*sendbuf[0] = (nalheader & 0x60) |28;&NBSP;&NBsp;sendbuf[1] = (nalheader & 0x1f);if ( 0 == t ) {sendbuf[1] |= 0x80;} memcpy (sendbuf+2,&psendbuf[t*max_rtp_pkt_length],max_rtp_pkt_length);status = this-> Sendpacket ((void *) sendbuf,max_rtp_pkt_length+2); */memcpy (Sendbuf,&psendbuf[t*max_rtp_pkt_length],max _rtp_pkt_length); Status = this->sendpacket ((void *) sendbuf,max_rtp_pkt_length); CheckError (status); t++;} Last Bag Else if ( ( k==t && l>0 ) | | ( t== (k-1) && l==0 )) {//Set flag bit mark is 1this->setdefaultmark (true); int iSendLen;if ( l > 0) {isendlen = buflen - t*max_rtp_pkt_length ;} elseisendlen = max_rtp_pkt_length;//sendbuf[0] = (nalheader & 0x60) |28; //sendbuf[1] = (nalheader & 0x1f);//sendbuf[1] |= 0x40;//memcpy ( sendbuf+2,&psendbuf[t*Max_rtp_pkt_length],isendlen);//status = this->sendpacket ((void *) sendbuf,iSendLen+2); memcpy (Sendbuf,&psendbuf[t*max_rtp_pkt_length],isendlen);status = this-> Sendpacket ((void *) sendbuf,isendlen); CheckError (status); t++;}}} void crtpsender::setparamsforsendingh264 () {this->setdefaultpayloadtype (H264);//Set Transfer type this-> Setdefaultmark (TRUE);//Set bit this->settimestampunit (1.0/9000.0); //Set sampling interval this-> Setdefaulttimestampincrement (3600);//set timestamp increase interval}void crtpsender::onbyepacket (rtpsourcedata *srcdat) {} Void crtpsender::onbyetimeout (Rtpsourcedata *srcdat) {}main.cpp transmission after encoding in the previous blog # define SSRC 100#define DEST_IP_STR "192.168.1.252" #define dest_port 1234#define base_port 2222int inal = 0;x264_nal_t* pnals&nbsP;= null;void setrtpparams (Crtpsender& sess,uint32_t destip,uint16_t destport, Uint16_t baseport) {INT&NBSP;STATUS;&NBSP;&NBSP;//RTP+RTCP Library initializes the socket environment rtpudpv4transmissionparams Transparams; Rtpsessionparams sessparams;sessparams. Setowntimestampunit (1.0/9000.0); //timestamp Unit sessparams. Setacceptownpackets (TRUE);//receive the packet sessparams that you sent. SETUSEPREDEFINEDSSRC (TRUE); //settings use pre-defined ssrcsessparams.setpredefinedssrc (SSRC); //defines ssrc transparams. Setportbase (Baseport); status = sess. Create (Sessparams,&transparams); checkerror (status);d Estip = ntohl (Destip); RTPIPV4ADDRESS&NBSP;ADDR (Destip,destport); status = sess. Adddestination (addr); CheckError (status);//Set parameter//sess for sending H264 package. SetParamsForSendingH264 ();} Bool initsocket () {int error; word versionrequested; wsadata wsadata; Versionrequested=makeword (2,2); Error=wsastartup (versionrequested,&wsadata); //start Winsock2if (error!=0) {printf ("error:start winsock failed!\n"); return false;} Else{if (Lobyte (wsadata.wversion)!=2| | Hibyte (wsadata.whighversion)!=2) {printf ("error:the version is winsock2!\n"); WSACleanup (); return false;}} Return true;} Void closesocket (crtpsender sess) {//send a bye packet away from the session for up to a second timeout without sending Sess.byedestroy (Rtptime (3,0), 0,0); WSACleanup ();} Int main (INT&NBSP;ARGC,&NBSP;CHAR**&NBSP;ARGV) {initsocket (); crtpsender sender;string destip_str = "127.0.0.1"; uint32_t dest_ip = inet_ Addr (Destip_str.c_str ()); Setrtpparams (sender,dest_ip,dest_port,base_port); sender. SetParamsForSendingH264 ();//...x264 setting parameters, see the previous blog for (int i = 0; i < nframes ; i++ ) {//read one frame read_frame_y4m (ppicin, (hnd_t*) y4m_hnd,i); if ( i ==0 ) ppicin->i_ pts = i;elseppicin->i_pts = i - 1;//encoding int frame_size = x264_ Encoder_encode (Px264handle, &pnals,&inal,ppicin,ppicout); if (frame_size >0) {for (int i = 0; i < inal; ++i) {//writes encoded data to file T//fwrite (Pnals[i].p_payload, 1, pnals[i].i_payload, pfile);//Send encoded file sender. Sendh264nalu (Pnals[i].p_payload,pnals[i].i_payload); Rtptime::wait (Rtptime (1,0));}} closesocket (sender);//some cleanup work ...}
Using Jrtplib (RTP) to transfer a video file (2)