Set the RTSP, RTP, and RTCP port numbers

Source: Internet
Author: User
Tags htons

1. Set the RTSP port number

The RTSP port number is set in the artspconnection. cpp file. First, obtain the port number from the URL. If the port number cannot be read, set it to the default port 554. Code processing is as follows:

Artspconnection: parseurl (const char * colonpos = strchr (host-> c_str (), ':'); If (colonpos! = NULL) {unsigned long X; If (! Parsesingleunsignedlong (colonpos + 1, & x) | x> = 65536) {// the RTSP port must be less than 65536 return false;} * Port = X; size_t colonoffset = colonpos-host-> c_str (); size_t trailing = Host-> size ()-colonoffset; host-> erase (colonoffset, trailing );} else {* Port = 554; // if no port is obtained from the URL, set the default port 554}

Bytes --------------------------------------------------------------------------------------------------------

2. Set the RTP and RTCP port numbers

The process of setting the RTP and RTCP port numbers is located in the artpconnection. cpp file, and the process of setting the RTSP port number is different in the file.

(1) function declaration: Create a UDP datagram socket for a pair of adjacent ports. RTP socket is an even port, and the RTCP socket port is a number larger than the RTP Port Number.

    // Creates a pair of UDP datagram sockets bound to adjacent ports    // (the rtpSocket is bound to an even port, the rtcpSocket to the    // next higher port).    static void MakePortPair(            int *rtpSocket, int *rtcpSocket, unsigned *rtpPort);

(2) Function Definition: Set the RTP and RTCP ports.

// Staticvoid artpconnection: makeportpair (int * rtpsocket, int * rtcpsocket, unsigned * rtpport) {* rtpsocket = socket (af_inet, sock_dgram, 0); check_ge (* rtpsocket, 0); bumpsocketbuffersize (* rtpsocket); * rtcpsocket = socket (af_inet, sock_dgram, 0); check_ge (* rtcpsocket, 0); bumpsocketbuffersize (* rtcpsocket ); unsigned start = (RAND () * 1000)/rand_max + 15550; // The minimum port number is 15550 start & = ~ 1; // The result is that start is changed to an even number, and the port starts from an even number for (unsigned Port = start; port <65536; port + = 2) {// The maximum RTP Port Number is 65535. The RTCP port number is 65536 struct sockaddr_in ADDR; memset (ADDR. sin_zero, 0, sizeof (ADDR. sin_zero); ADDR. sin_family = af_inet; ADDR. sin_addr.s_addr = htonl (inaddr_any); ADDR. sin_port = htons (port); If (BIND (* rtpsocket, (const struct sockaddr *) & ADDR, sizeof (ADDR) <0) {continue;} ADDR. sin_port = htons (Port + 1); If (BIND (* rtcpsocket, (const struct sockaddr *) & ADDR, sizeof (ADDR) = 0) {* rtpport = port; return; // return after the RTCP port is bound} trespass ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.